From 4658c27d52eb4f5b9c2fba9fb3fc1042ab34c5a0 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Thu, 2 Nov 2023 17:15:22 -0700 Subject: [PATCH] NFC bigint: Remove `Width`. The original idea of `Width` was that we'd support operatings that worked on multiple same-width but different-modulus values, and/or we'd support splitting a 2N-limb `BoxedLimb` into two N-limb `&[Limb]`, etc. However, as things are now, `Width` doesn't really serve a useful purpose. --- src/arithmetic/bigint.rs | 15 ++------------- src/arithmetic/bigint/boxed_limbs.rs | 20 +++++--------------- src/arithmetic/bigint/modulus.rs | 16 +++------------- 3 files changed, 10 insertions(+), 41 deletions(-) diff --git a/src/arithmetic/bigint.rs b/src/arithmetic/bigint.rs index de57e1a18b..d2788aa5f4 100644 --- a/src/arithmetic/bigint.rs +++ b/src/arithmetic/bigint.rs @@ -68,13 +68,6 @@ mod private_exponent; /// preemptively.) pub unsafe trait Prime {} -struct Width { - num_limbs: usize, - - /// The modulus *m* that the width originated from. - m: PhantomData, -} - /// A modulus *s* that is smaller than another modulus *l* so every element of /// ℤ/sℤ is also an element of ℤ/lℤ. /// @@ -152,10 +145,9 @@ fn from_montgomery_amm(limbs: BoxedLimbs, m: &Modulus) -> Elem Elem { let value = consume_nonnegative(test_case, name); - let mut limbs = BoxedLimbs::zero(Width { - num_limbs, - m: PhantomData, - }); + let mut limbs = BoxedLimbs::zero(num_limbs); limbs[0..value.limbs().len()].copy_from_slice(value.limbs()); Elem { limbs, diff --git a/src/arithmetic/bigint/boxed_limbs.rs b/src/arithmetic/bigint/boxed_limbs.rs index 0d5bba121b..fdb91a4410 100644 --- a/src/arithmetic/bigint/boxed_limbs.rs +++ b/src/arithmetic/bigint/boxed_limbs.rs @@ -12,7 +12,7 @@ // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -use super::{Modulus, Width}; +use super::Modulus; use crate::{ error, limb::{self, Limb, LimbMask, LIMB_BYTES}, @@ -76,10 +76,7 @@ impl BoxedLimbs { return Err(error::KeyRejected::invalid_encoding()); } let num_limbs = (input.len() + LIMB_BYTES - 1) / LIMB_BYTES; - let mut r = Self::zero(Width { - num_limbs, - m: PhantomData, - }); + let mut r = Self::zero(num_limbs); limb::parse_big_endian_and_pad_consttime(input, &mut r) .map_err(|error::Unspecified| error::KeyRejected::unexpected_error())?; Ok(r) @@ -97,7 +94,7 @@ impl BoxedLimbs { input: untrusted::Input, m: &Modulus, ) -> Result { - let mut r = Self::zero(m.width()); + let mut r = Self::zero(m.limbs().len()); limb::parse_big_endian_and_pad_consttime(input, &mut r)?; if limb::limbs_less_than_limbs_consttime(&r, m.limbs()) != LimbMask::True { return Err(error::Unspecified); @@ -110,16 +107,9 @@ impl BoxedLimbs { limb::limbs_are_zero_constant_time(&self.limbs) == LimbMask::True } - pub(super) fn zero(width: Width) -> Self { + pub(super) fn zero(len: usize) -> Self { Self { - limbs: vec![0; width.num_limbs].into_boxed_slice(), - m: PhantomData, - } - } - - pub(super) fn width(&self) -> Width { - Width { - num_limbs: self.limbs.len(), + limbs: vec![0; len].into_boxed_slice(), m: PhantomData, } } diff --git a/src/arithmetic/bigint/modulus.rs b/src/arithmetic/bigint/modulus.rs index ba6acfc958..c139e589bd 100644 --- a/src/arithmetic/bigint/modulus.rs +++ b/src/arithmetic/bigint/modulus.rs @@ -18,7 +18,6 @@ use super::{ n0::N0, }, BoxedLimbs, Elem, Nonnegative, One, PublicModulus, SlightlySmallerModulus, SmallerModulus, - Width, }; use crate::{ bits, cpu, error, @@ -210,14 +209,9 @@ impl Modulus { &self.n0 } - #[inline] - pub(super) fn width(&self) -> Width { - self.limbs.width() - } - pub(super) fn zero(&self) -> Elem { Elem { - limbs: BoxedLimbs::zero(self.width()), + limbs: BoxedLimbs::zero(self.limbs().len()), encoding: PhantomData, } } @@ -238,7 +232,7 @@ impl Modulus { M: SmallerModulus, { // TODO: Encode this assertion into the `where` above. - assert_eq!(self.width().num_limbs, l.width().num_limbs); + assert_eq!(self.limbs().len(), l.limbs().len()); Elem { limbs: BoxedLimbs::new_unchecked(self.limbs.clone().into_limbs()), encoding: PhantomData, @@ -271,12 +265,8 @@ pub(crate) struct PartialModulus<'a, M> { impl PartialModulus<'_, M> { // TODO: XXX Avoid duplication with `Modulus`. pub(super) fn zero(&self) -> Elem { - let width = Width { - num_limbs: self.limbs.len(), - m: PhantomData, - }; Elem { - limbs: BoxedLimbs::zero(width), + limbs: BoxedLimbs::zero(self.limbs.len()), encoding: PhantomData, } }