Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions benches/boxed_monty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use criterion::{
};
use crypto_bigint::{
modular::{BoxedMontyForm, BoxedMontyParams},
BoxedUint, NonZero, Odd, RandomMod,
BoxedUint, Odd, RandomMod,
};
use num_bigint::BigUint;
use rand_core::OsRng;
Expand All @@ -22,8 +22,10 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("invert, 4096-bit", |b| {
b.iter_batched(
|| {
let modulus = NonZero::new(params.modulus().clone()).unwrap();
BoxedMontyForm::new(BoxedUint::random_mod(&mut OsRng, &modulus), params.clone())
BoxedMontyForm::new(
BoxedUint::random_mod(&mut OsRng, params.modulus().as_nz_ref()),
params.clone(),
)
},
|x| black_box(x).invert(),
BatchSize::SmallInput,
Expand Down
2 changes: 1 addition & 1 deletion src/modular/boxed_monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl BoxedMontyParams {
}

/// Modulus value.
pub fn modulus(&self) -> &BoxedUint {
pub fn modulus(&self) -> &Odd<BoxedUint> {
&self.modulus
}

Expand Down
4 changes: 2 additions & 2 deletions src/modular/monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ impl<const LIMBS: usize> MontyParams<LIMBS> {
}

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

/// Create `MontyParams` corresponding to a `ConstMontyParams`.
Expand Down
8 changes: 5 additions & 3 deletions tests/boxed_monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod common;
use common::to_biguint;
use crypto_bigint::{
modular::{BoxedMontyForm, BoxedMontyParams},
BoxedUint, Integer, Inverter, Limb, NonZero, Odd, PrecomputeInverter,
BoxedUint, Integer, Inverter, Limb, Odd, PrecomputeInverter,
};
use num_bigint::BigUint;
use num_modular::ModularUnaryOps;
Expand All @@ -20,15 +20,17 @@ fn retrieve_biguint(monty_form: &BoxedMontyForm) -> BigUint {

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

let n = match n.bits_precision().cmp(&bits_precision) {
Ordering::Less => n.widen(bits_precision),
Ordering::Equal => n.clone(),
Ordering::Greater => n.shorten(bits_precision),
};

let n_reduced = n.rem_vartime(&modulus).widen(p.bits_precision());
let n_reduced = n
.rem_vartime(p.modulus().as_nz_ref())
.widen(p.bits_precision());

BoxedMontyForm::new(n_reduced, p)
}

Expand Down
5 changes: 2 additions & 3 deletions tests/monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mod common;

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

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

Expand Down