bigint: Add modulus bit length to Modulus.

This commit is contained in:
Brian Smith 2023-11-07 14:32:27 -08:00
parent 5ed0a45c65
commit cbcac26d00
2 changed files with 11 additions and 4 deletions

View File

@ -45,7 +45,7 @@ use super::n0::N0;
pub(crate) use super::nonnegative::Nonnegative; pub(crate) use super::nonnegative::Nonnegative;
use crate::{ use crate::{
arithmetic::montgomery::*, arithmetic::montgomery::*,
bits, c, cpu, error, c, cpu, error,
limb::{self, Limb, LimbMask, LIMB_BITS}, limb::{self, Limb, LimbMask, LIMB_BITS},
polyfill::u64_from_usize, polyfill::u64_from_usize,
}; };
@ -305,8 +305,8 @@ impl<M> One<M, RR> {
// values, using `LIMB_BITS` here, rather than `N0::LIMBS_USED * LIMB_BITS`, // values, using `LIMB_BITS` here, rather than `N0::LIMBS_USED * LIMB_BITS`,
// is correct because R**2 will still be a multiple of the latter as // is correct because R**2 will still be a multiple of the latter as
// `N0::LIMBS_USED` is either one or two. // `N0::LIMBS_USED` is either one or two.
fn newRR(m: &Modulus<M>, m_bits: bits::BitLength) -> Self { fn newRR(m: &Modulus<M>) -> Self {
let m_bits = m_bits.as_usize_bits(); let m_bits = m.len_bits().as_usize_bits();
let r = (m_bits + (LIMB_BITS - 1)) / LIMB_BITS * LIMB_BITS; let r = (m_bits + (LIMB_BITS - 1)) / LIMB_BITS * LIMB_BITS;
// base = 2**(lg m - 1). // base = 2**(lg m - 1).

View File

@ -179,11 +179,12 @@ impl<M> OwnedModulusWithOne<M> {
let partial = Modulus { let partial = Modulus {
limbs: &n, limbs: &n,
n0: n0.clone(), n0: n0.clone(),
len_bits,
m: PhantomData, m: PhantomData,
cpu_features, cpu_features,
}; };
One::newRR(&partial, len_bits) One::newRR(&partial)
}; };
Ok(Self { Ok(Self {
@ -214,6 +215,7 @@ impl<M> OwnedModulusWithOne<M> {
Modulus { Modulus {
limbs: &self.limbs, limbs: &self.limbs,
n0: self.n0.clone(), n0: self.n0.clone(),
len_bits: self.len_bits,
m: PhantomData, m: PhantomData,
cpu_features: self.cpu_features, cpu_features: self.cpu_features,
} }
@ -233,6 +235,7 @@ impl<M: PublicModulus> OwnedModulusWithOne<M> {
pub struct Modulus<'a, M> { pub struct Modulus<'a, M> {
limbs: &'a [Limb], limbs: &'a [Limb],
n0: N0, n0: N0,
len_bits: BitLength,
m: PhantomData<M>, m: PhantomData<M>,
cpu_features: cpu::Features, cpu_features: cpu::Features,
} }
@ -263,6 +266,10 @@ impl<M> Modulus<'_, M> {
&self.n0 &self.n0
} }
pub fn len_bits(&self) -> BitLength {
self.len_bits
}
#[inline] #[inline]
pub(crate) fn cpu_features(&self) -> cpu::Features { pub(crate) fn cpu_features(&self) -> cpu::Features {
self.cpu_features self.cpu_features