Skip to content

Commit efaaa05

Browse files
authored
Have (Boxed)MontyParams::modulus return &Odd<_> (#517)
Notably `Odd` permits a simple reference conversion to `NonZero` which makes it possible to clean up some tests.
1 parent df8b716 commit efaaa05

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

benches/boxed_monty.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use criterion::{
44
};
55
use crypto_bigint::{
66
modular::{BoxedMontyForm, BoxedMontyParams},
7-
BoxedUint, NonZero, Odd, RandomMod,
7+
BoxedUint, Odd, RandomMod,
88
};
99
use num_bigint::BigUint;
1010
use rand_core::OsRng;
@@ -22,8 +22,10 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
2222
group.bench_function("invert, 4096-bit", |b| {
2323
b.iter_batched(
2424
|| {
25-
let modulus = NonZero::new(params.modulus().clone()).unwrap();
26-
BoxedMontyForm::new(BoxedUint::random_mod(&mut OsRng, &modulus), params.clone())
25+
BoxedMontyForm::new(
26+
BoxedUint::random_mod(&mut OsRng, params.modulus().as_nz_ref()),
27+
params.clone(),
28+
)
2729
},
2830
|x| black_box(x).invert(),
2931
BatchSize::SmallInput,

src/modular/boxed_monty_form.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl BoxedMontyParams {
106106
}
107107

108108
/// Modulus value.
109-
pub fn modulus(&self) -> &BoxedUint {
109+
pub fn modulus(&self) -> &Odd<BoxedUint> {
110110
&self.modulus
111111
}
112112

src/modular/monty_form.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ impl<const LIMBS: usize> MontyParams<LIMBS> {
6464
}
6565

6666
/// Returns the modulus which was used to initialize these parameters.
67-
pub const fn modulus(&self) -> &Uint<LIMBS> {
68-
&self.modulus.0
67+
pub const fn modulus(&self) -> &Odd<Uint<LIMBS>> {
68+
&self.modulus
6969
}
7070

7171
/// Create `MontyParams` corresponding to a `ConstMontyParams`.

tests/boxed_monty_form.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod common;
77
use common::to_biguint;
88
use crypto_bigint::{
99
modular::{BoxedMontyForm, BoxedMontyParams},
10-
BoxedUint, Integer, Inverter, Limb, NonZero, Odd, PrecomputeInverter,
10+
BoxedUint, Integer, Inverter, Limb, Odd, PrecomputeInverter,
1111
};
1212
use num_bigint::BigUint;
1313
use num_modular::ModularUnaryOps;
@@ -20,15 +20,17 @@ fn retrieve_biguint(monty_form: &BoxedMontyForm) -> BigUint {
2020

2121
fn reduce(n: &BoxedUint, p: BoxedMontyParams) -> BoxedMontyForm {
2222
let bits_precision = p.modulus().bits_precision();
23-
let modulus = NonZero::new(p.modulus().clone()).unwrap();
2423

2524
let n = match n.bits_precision().cmp(&bits_precision) {
2625
Ordering::Less => n.widen(bits_precision),
2726
Ordering::Equal => n.clone(),
2827
Ordering::Greater => n.shorten(bits_precision),
2928
};
3029

31-
let n_reduced = n.rem_vartime(&modulus).widen(p.bits_precision());
30+
let n_reduced = n
31+
.rem_vartime(p.modulus().as_nz_ref())
32+
.widen(p.bits_precision());
33+
3234
BoxedMontyForm::new(n_reduced, p)
3335
}
3436

tests/monty_form.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mod common;
44

55
use common::to_biguint;
6-
use crypto_bigint::{Integer, Invert, Inverter, NonZero, Odd, PrecomputeInverter, U256};
6+
use crypto_bigint::{Integer, Invert, Inverter, Odd, PrecomputeInverter, U256};
77
use num_bigint::BigUint;
88
use num_modular::ModularUnaryOps;
99
use proptest::prelude::*;
@@ -16,8 +16,7 @@ fn retrieve_biguint(monty_form: &MontyForm) -> BigUint {
1616
}
1717

1818
fn reduce(n: &U256, p: MontyParams) -> MontyForm {
19-
let modulus = NonZero::new(p.modulus().clone()).unwrap();
20-
let n_reduced = n.rem_vartime(&modulus);
19+
let n_reduced = n.rem_vartime(p.modulus().as_nz_ref());
2120
MontyForm::new(&n_reduced, p)
2221
}
2322

0 commit comments

Comments
 (0)