Skip to content

Commit c128bbd

Browse files
Fix modulus leading zeros calculation for (Boxed)MontyForm (#713)
Fixes #707 Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
1 parent 12d4a08 commit c128bbd

4 files changed

Lines changed: 34 additions & 6 deletions

File tree

src/modular/boxed_monty_form.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl BoxedMontyParams {
9797

9898
let mod_neg_inv = Limb(Word::MIN.wrapping_sub(inv_mod_limb.limbs[0].0));
9999

100-
let mod_leading_zeros = modulus.as_ref().leading_zeros().max(Word::BITS - 1);
100+
let mod_leading_zeros = modulus.as_ref().leading_zeros().min(Word::BITS - 1);
101101

102102
let r3 = montgomery_reduction_boxed(&mut r2.square(), &modulus, mod_neg_inv);
103103

@@ -327,12 +327,14 @@ fn convert_to_montgomery(integer: &mut BoxedUint, params: &BoxedMontyParams) {
327327

328328
#[cfg(test)]
329329
mod tests {
330-
use super::{BoxedMontyForm, BoxedMontyParams, BoxedUint, Odd};
330+
use super::{BoxedMontyForm, BoxedMontyParams, BoxedUint, Limb, Odd};
331331

332332
#[test]
333333
fn new_params_with_valid_modulus() {
334334
let modulus = Odd::new(BoxedUint::from(3u8)).unwrap();
335-
BoxedMontyParams::new(modulus);
335+
let params = BoxedMontyParams::new(modulus);
336+
337+
assert_eq!(params.mod_leading_zeros, Limb::BITS - 2);
336338
}
337339

338340
#[test]

src/modular/const_monty_form/macros.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,16 @@ macro_rules! const_monty_form {
8484
$crate::modular::ConstMontyForm::<$modulus, { $modulus::LIMBS }>::new(&$variable)
8585
};
8686
}
87+
88+
#[cfg(test)]
89+
mod tests {
90+
use crate::modular::ConstMontyParams;
91+
use crate::{Limb, U64};
92+
93+
#[test]
94+
fn new_params_with_valid_modulus() {
95+
impl_modulus!(Mod, U64, "0000000000000003");
96+
97+
assert_eq!(Mod::MOD_LEADING_ZEROS, core::cmp::min(Limb::BITS - 1, 62));
98+
}
99+
}

src/modular/monty_form.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ where
6060

6161
let mod_neg_inv = Limb(Word::MIN.wrapping_sub(inv_mod.limbs[0].0));
6262

63-
let mod_leading_zeros = modulus.as_ref().leading_zeros().max(Word::BITS - 1);
63+
let mod_leading_zeros = modulus.as_ref().leading_zeros().min(Word::BITS - 1);
6464

6565
// `R^3 mod modulus`, used for inversion in Montgomery form.
6666
let r3 = montgomery_reduction(&r2.square_wide(), &modulus, mod_neg_inv);
@@ -95,7 +95,7 @@ impl<const LIMBS: usize> MontyParams<LIMBS> {
9595

9696
let mod_neg_inv = Limb(Word::MIN.wrapping_sub(inv_mod.limbs[0].0));
9797

98-
let mod_leading_zeros = modulus.as_ref().leading_zeros().max(Word::BITS - 1);
98+
let mod_leading_zeros = modulus.as_ref().leading_zeros_vartime().min(Word::BITS - 1);
9999

100100
// `R^3 mod modulus`, used for inversion in Montgomery form.
101101
let r3 = montgomery_reduction(&r2.square_wide(), &modulus, mod_neg_inv);
@@ -337,3 +337,16 @@ impl<const LIMBS: usize> zeroize::Zeroize for MontyForm<LIMBS> {
337337
self.params.zeroize();
338338
}
339339
}
340+
341+
#[cfg(test)]
342+
mod tests {
343+
use super::{Limb, MontyParams, Odd, Uint};
344+
345+
#[test]
346+
fn new_params_with_valid_modulus() {
347+
let modulus = Odd::new(Uint::from(3u8)).unwrap();
348+
let params = MontyParams::<1>::new(modulus);
349+
350+
assert_eq!(params.mod_leading_zeros, Limb::BITS - 2);
351+
}
352+
}

src/modular/monty_form/lincomb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mod tests {
3939
use rand_core::SeedableRng;
4040

4141
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(1);
42-
for n in 0..1000 {
42+
for n in 0..1500 {
4343
let modulus = Odd::<U256>::random(&mut rng);
4444
let params = MontyParams::new_vartime(modulus);
4545
let m = modulus.as_nz_ref();

0 commit comments

Comments
 (0)