From cbcac26d00428622a80887293a8b27c62f33942c Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Tue, 7 Nov 2023 14:32:27 -0800 Subject: [PATCH] bigint: Add modulus bit length to `Modulus`. --- src/arithmetic/bigint.rs | 6 +++--- src/arithmetic/bigint/modulus.rs | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/arithmetic/bigint.rs b/src/arithmetic/bigint.rs index 59f91e0de6..f439df8a7b 100644 --- a/src/arithmetic/bigint.rs +++ b/src/arithmetic/bigint.rs @@ -45,7 +45,7 @@ use super::n0::N0; pub(crate) use super::nonnegative::Nonnegative; use crate::{ arithmetic::montgomery::*, - bits, c, cpu, error, + c, cpu, error, limb::{self, Limb, LimbMask, LIMB_BITS}, polyfill::u64_from_usize, }; @@ -305,8 +305,8 @@ impl One { // 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 // `N0::LIMBS_USED` is either one or two. - fn newRR(m: &Modulus, m_bits: bits::BitLength) -> Self { - let m_bits = m_bits.as_usize_bits(); + fn newRR(m: &Modulus) -> Self { + let m_bits = m.len_bits().as_usize_bits(); let r = (m_bits + (LIMB_BITS - 1)) / LIMB_BITS * LIMB_BITS; // base = 2**(lg m - 1). diff --git a/src/arithmetic/bigint/modulus.rs b/src/arithmetic/bigint/modulus.rs index 2071c1bfa1..51d874dc2d 100644 --- a/src/arithmetic/bigint/modulus.rs +++ b/src/arithmetic/bigint/modulus.rs @@ -179,11 +179,12 @@ impl OwnedModulusWithOne { let partial = Modulus { limbs: &n, n0: n0.clone(), + len_bits, m: PhantomData, cpu_features, }; - One::newRR(&partial, len_bits) + One::newRR(&partial) }; Ok(Self { @@ -214,6 +215,7 @@ impl OwnedModulusWithOne { Modulus { limbs: &self.limbs, n0: self.n0.clone(), + len_bits: self.len_bits, m: PhantomData, cpu_features: self.cpu_features, } @@ -233,6 +235,7 @@ impl OwnedModulusWithOne { pub struct Modulus<'a, M> { limbs: &'a [Limb], n0: N0, + len_bits: BitLength, m: PhantomData, cpu_features: cpu::Features, } @@ -263,6 +266,10 @@ impl Modulus<'_, M> { &self.n0 } + pub fn len_bits(&self) -> BitLength { + self.len_bits + } + #[inline] pub(crate) fn cpu_features(&self) -> cpu::Features { self.cpu_features