Skip to content

Commit

Permalink
NFC bigint: Remove Width.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
briansmith committed Nov 3, 2023
1 parent 8ed4860 commit 3822e73
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 41 deletions.
15 changes: 2 additions & 13 deletions src/arithmetic/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ mod private_exponent;
/// preemptively.)
pub unsafe trait Prime {}

struct Width<M> {
num_limbs: usize,

/// The modulus *m* that the width originated from.
m: PhantomData<M>,
}

/// A modulus *s* that is smaller than another modulus *l* so every element of
/// ℤ/sℤ is also an element of ℤ/lℤ.
///
Expand Down Expand Up @@ -152,10 +145,9 @@ fn from_montgomery_amm<M>(limbs: BoxedLimbs<M>, m: &Modulus<M>) -> Elem<M, Unenc
debug_assert_eq!(limbs.len(), m.limbs().len());

let mut limbs = limbs;
let num_limbs = m.width().num_limbs;
let mut one = [0; MODULUS_MAX_LIMBS];
one[0] = 1;
let one = &one[..num_limbs]; // assert!(num_limbs <= MODULUS_MAX_LIMBS);
let one = &one[..m.limbs().len()];
limbs_mont_mul(&mut limbs, one, m.limbs(), m.n0(), m.cpu_features());
Elem {
limbs,
Expand Down Expand Up @@ -1001,10 +993,7 @@ mod tests {
num_limbs: usize,
) -> Elem<M, Unencoded> {
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,
Expand Down
20 changes: 5 additions & 15 deletions src/arithmetic/bigint/boxed_limbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -76,10 +76,7 @@ impl<M> BoxedLimbs<M> {
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)
Expand All @@ -97,7 +94,7 @@ impl<M> BoxedLimbs<M> {
input: untrusted::Input,
m: &Modulus<M>,
) -> Result<Self, error::Unspecified> {
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);
Expand All @@ -110,16 +107,9 @@ impl<M> BoxedLimbs<M> {
limb::limbs_are_zero_constant_time(&self.limbs) == LimbMask::True
}

pub(super) fn zero(width: Width<M>) -> 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<M> {
Width {
num_limbs: self.limbs.len(),
limbs: vec![0; len].into_boxed_slice(),
m: PhantomData,
}
}
Expand Down
16 changes: 3 additions & 13 deletions src/arithmetic/bigint/modulus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use super::{
n0::N0,
},
BoxedLimbs, Elem, Nonnegative, One, PublicModulus, SlightlySmallerModulus, SmallerModulus,
Width,
};
use crate::{
bits, cpu, error,
Expand Down Expand Up @@ -210,14 +209,9 @@ impl<M> Modulus<M> {
&self.n0
}

#[inline]
pub(super) fn width(&self) -> Width<M> {
self.limbs.width()
}

pub(super) fn zero<E>(&self) -> Elem<M, E> {
Elem {
limbs: BoxedLimbs::zero(self.width()),
limbs: BoxedLimbs::zero(self.limbs().len()),
encoding: PhantomData,
}
}
Expand All @@ -238,7 +232,7 @@ impl<M> Modulus<M> {
M: SmallerModulus<L>,
{
// 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,
Expand Down Expand Up @@ -271,12 +265,8 @@ pub(crate) struct PartialModulus<'a, M> {
impl<M> PartialModulus<'_, M> {
// TODO: XXX Avoid duplication with `Modulus`.
pub(super) fn zero(&self) -> Elem<M, R> {
let width = Width {
num_limbs: self.limbs.len(),
m: PhantomData,
};
Elem {
limbs: BoxedLimbs::zero(width),
limbs: BoxedLimbs::zero(self.limbs.len()),
encoding: PhantomData,
}
}
Expand Down

0 comments on commit 3822e73

Please sign in to comment.