From b428f80e3989be72b14b7db4fc533ca197177752 Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Tue, 10 Dec 2024 12:24:53 -0800 Subject: [PATCH] check derivation of mod_leading_zeros Signed-off-by: Andrew Whitehead --- src/modular/boxed_monty_form.rs | 6 ++++-- src/modular/const_monty_form/macros.rs | 13 +++++++++++++ src/modular/monty_form.rs | 13 +++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/modular/boxed_monty_form.rs b/src/modular/boxed_monty_form.rs index 43fbf66d..78d5774a 100644 --- a/src/modular/boxed_monty_form.rs +++ b/src/modular/boxed_monty_form.rs @@ -327,12 +327,14 @@ fn convert_to_montgomery(integer: &mut BoxedUint, params: &BoxedMontyParams) { #[cfg(test)] mod tests { - use super::{BoxedMontyForm, BoxedMontyParams, BoxedUint, Odd}; + use super::{BoxedMontyForm, BoxedMontyParams, BoxedUint, Limb, Odd}; #[test] fn new_params_with_valid_modulus() { let modulus = Odd::new(BoxedUint::from(3u8)).unwrap(); - BoxedMontyParams::new(modulus); + let params = BoxedMontyParams::new(modulus); + + assert_eq!(params.mod_leading_zeros, Limb::BITS - 2); } #[test] diff --git a/src/modular/const_monty_form/macros.rs b/src/modular/const_monty_form/macros.rs index 6b7f096d..99d22b32 100644 --- a/src/modular/const_monty_form/macros.rs +++ b/src/modular/const_monty_form/macros.rs @@ -84,3 +84,16 @@ macro_rules! const_monty_form { $crate::modular::ConstMontyForm::<$modulus, { $modulus::LIMBS }>::new(&$variable) }; } + +#[cfg(test)] +mod tests { + use crate::modular::ConstMontyParams; + use crate::{Limb, U64}; + + #[test] + fn new_params_with_valid_modulus() { + impl_modulus!(Mod, U64, "0000000000000003"); + + assert_eq!(Mod::MOD_LEADING_ZEROS, core::cmp::min(Limb::BITS - 1, 62)); + } +} diff --git a/src/modular/monty_form.rs b/src/modular/monty_form.rs index 95bf1b13..a0d90d93 100644 --- a/src/modular/monty_form.rs +++ b/src/modular/monty_form.rs @@ -337,3 +337,16 @@ impl zeroize::Zeroize for MontyForm { self.params.zeroize(); } } + +#[cfg(test)] +mod tests { + use super::{Limb, MontyParams, Odd, Uint}; + + #[test] + fn new_params_with_valid_modulus() { + let modulus = Odd::new(Uint::from(3u8)).unwrap(); + let params = MontyParams::<1>::new(modulus); + + assert_eq!(params.mod_leading_zeros, Limb::BITS - 2); + } +}