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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[[bench]]
name = "boxed_residue"
name = "boxed_monty"
harness = false
required-features = ["alloc"]

Expand All @@ -62,15 +62,15 @@ harness = false
required-features = ["alloc"]

[[bench]]
name = "dyn_residue"
name = "const_monty"
harness = false

[[bench]]
name = "limb"
harness = false

[[bench]]
name = "residue"
name = "monty"
harness = false

[[bench]]
Expand Down
36 changes: 19 additions & 17 deletions benches/boxed_residue.rs → benches/boxed_monty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::{
BenchmarkGroup, Criterion,
};
use crypto_bigint::{
modular::{BoxedResidue, BoxedResidueParams},
modular::{BoxedMontyForm, BoxedMontyParams},
BoxedUint, NonZero, RandomMod,
};
use num_bigint::BigUint;
Expand All @@ -17,7 +17,7 @@ fn to_biguint(uint: &BoxedUint) -> BigUint {
}

fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
let params = BoxedResidueParams::new(
let params = BoxedMontyParams::new(
BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
)
.unwrap();
Expand All @@ -26,7 +26,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
b.iter_batched(
|| {
let modulus = NonZero::new(params.modulus().clone()).unwrap();
BoxedResidue::new(BoxedUint::random_mod(&mut OsRng, &modulus), params.clone())
BoxedMontyForm::new(BoxedUint::random_mod(&mut OsRng, &modulus), params.clone())
},
|x| black_box(x).invert(),
BatchSize::SmallInput,
Expand All @@ -36,8 +36,10 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("multiplication, BoxedUint*BoxedUint", |b| {
b.iter_batched(
|| {
let x = BoxedResidue::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone());
let y = BoxedResidue::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone());
let x =
BoxedMontyForm::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone());
let y =
BoxedMontyForm::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone());
(x, y)
},
|(x, y)| black_box(x * y),
Expand All @@ -59,12 +61,12 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
});

let m = BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS);
let params = BoxedResidueParams::new(m).unwrap();
let params = BoxedMontyParams::new(m).unwrap();
group.bench_function("modpow, BoxedUint^BoxedUint", |b| {
b.iter_batched(
|| {
let x = BoxedUint::random(&mut OsRng, UINT_BITS);
let x_m = BoxedResidue::new(x, params.clone());
let x_m = BoxedMontyForm::new(x, params.clone());
let p = BoxedUint::random(&mut OsRng, UINT_BITS)
| (BoxedUint::one_with_precision(UINT_BITS) << (UINT_BITS - 1));
(x_m, p)
Expand Down Expand Up @@ -92,41 +94,41 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
}

fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("BoxedResidueParams::new", |b| {
group.bench_function("BoxedMontyParams::new", |b| {
b.iter_batched(
|| BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
|modulus| black_box(BoxedResidueParams::new(modulus)),
|modulus| black_box(BoxedMontyParams::new(modulus)),
BatchSize::SmallInput,
)
});

group.bench_function("BoxedResidueParams::new_vartime", |b| {
group.bench_function("BoxedMontyParams::new_vartime", |b| {
b.iter_batched(
|| BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
|modulus| black_box(BoxedResidueParams::new_vartime(modulus)),
|modulus| black_box(BoxedMontyParams::new_vartime(modulus)),
BatchSize::SmallInput,
)
});

let params = BoxedResidueParams::new(
let params = BoxedMontyParams::new(
BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
)
.unwrap();
group.bench_function("BoxedResidue::new", |b| {
group.bench_function("BoxedMontyForm::new", |b| {
b.iter_batched(
|| BoxedUint::random(&mut OsRng, UINT_BITS),
|x| black_box(BoxedResidue::new(x, params.clone())),
|x| black_box(BoxedMontyForm::new(x, params.clone())),
BatchSize::SmallInput,
)
});

let params = BoxedResidueParams::new(
let params = BoxedMontyParams::new(
BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
)
.unwrap();
group.bench_function("BoxedResidue::retrieve", |b| {
group.bench_function("BoxedMontyForm::retrieve", |b| {
b.iter_batched(
|| BoxedResidue::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone()),
|| BoxedMontyForm::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone()),
|x| black_box(x.retrieve()),
BatchSize::SmallInput,
)
Expand Down
30 changes: 16 additions & 14 deletions benches/residue.rs → benches/const_monty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::{
black_box, criterion_group, criterion_main, measurement::Measurement, BatchSize,
BenchmarkGroup, Criterion,
};
use crypto_bigint::{impl_modulus, modular::ResidueParams, Invert, Inverter, Random, U256};
use crypto_bigint::{impl_modulus, modular::ConstMontyParams, Invert, Inverter, Random, U256};
use rand_core::OsRng;

#[cfg(feature = "alloc")]
Expand All @@ -14,20 +14,20 @@ impl_modulus!(
"ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"
);

type Residue = crypto_bigint::modular::Residue<Modulus, { U256::LIMBS }>;
type ConstMontyForm = crypto_bigint::modular::ConstMontyForm<Modulus, { U256::LIMBS }>;

fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("Residue creation", |b| {
group.bench_function("ConstMontyForm creation", |b| {
b.iter_batched(
|| U256::random(&mut OsRng),
|x| black_box(Residue::new(&x)),
|x| black_box(ConstMontyForm::new(&x)),
BatchSize::SmallInput,
)
});

group.bench_function("Residue retrieve", |b| {
group.bench_function("ConstMontyForm retrieve", |b| {
b.iter_batched(
|| Residue::new(&U256::random(&mut OsRng)),
|| ConstMontyForm::new(&U256::random(&mut OsRng)),
|x| black_box(x.retrieve()),
BatchSize::SmallInput,
)
Expand All @@ -37,7 +37,7 @@ fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>
fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("invert, U256", |b| {
b.iter_batched(
|| Residue::new(&U256::random(&mut OsRng)),
|| ConstMontyForm::new(&U256::random(&mut OsRng)),
|x| black_box(x).invert(),
BatchSize::SmallInput,
)
Expand All @@ -46,7 +46,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("Bernstein-Yang invert, U256", |b| {
b.iter_batched(
|| {
let x = Residue::new(&U256::random(&mut OsRng));
let x = ConstMontyForm::new(&U256::random(&mut OsRng));
let inverter = Modulus::precompute_inverter();
(x, inverter)
},
Expand All @@ -58,8 +58,8 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("multiplication, U256*U256", |b| {
b.iter_batched(
|| {
let x = Residue::new(&U256::random(&mut OsRng));
let y = Residue::new(&U256::random(&mut OsRng));
let x = ConstMontyForm::new(&U256::random(&mut OsRng));
let y = ConstMontyForm::new(&U256::random(&mut OsRng));
(x, y)
},
|(x, y)| black_box(x * y),
Expand All @@ -71,7 +71,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
b.iter_batched(
|| {
let x = U256::random(&mut OsRng);
let x_m = Residue::new(&x);
let x_m = ConstMontyForm::new(&x);
let p = U256::random(&mut OsRng) | (U256::ONE << (U256::BITS - 1));
(x_m, p)
},
Expand All @@ -87,10 +87,10 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
|b| {
b.iter_batched(
|| {
let bases_and_exponents: Vec<(Residue, U256)> = (1..=i)
let bases_and_exponents: Vec<(ConstMontyForm, U256)> = (1..=i)
.map(|_| {
let x = U256::random(&mut OsRng);
let x_m = Residue::new(&x);
let x_m = ConstMontyForm::new(&x);
let p = U256::random(&mut OsRng) | (U256::ONE << (U256::BITS - 1));
(x_m, p)
})
Expand All @@ -99,7 +99,9 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
bases_and_exponents
},
|bases_and_exponents| {
black_box(Residue::multi_exponentiate(bases_and_exponents.as_slice()))
black_box(ConstMontyForm::multi_exponentiate(
bases_and_exponents.as_slice(),
))
},
BatchSize::SmallInput,
)
Expand Down
36 changes: 18 additions & 18 deletions benches/dyn_residue.rs → benches/monty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::{
BenchmarkGroup, Criterion,
};
use crypto_bigint::{
modular::{DynResidue, DynResidueParams},
modular::{MontyForm, MontyParams},
Invert, Inverter, PrecomputeInverter, Random, U256,
};
use rand_core::OsRng;
Expand All @@ -12,39 +12,39 @@ use rand_core::OsRng;
use crypto_bigint::MultiExponentiate;

fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("DynResidueParams creation", |b| {
group.bench_function("MontyParams creation", |b| {
b.iter_batched(
|| U256::random(&mut OsRng) | U256::ONE,
|modulus| black_box(DynResidueParams::new(&modulus)),
|modulus| black_box(MontyParams::new(&modulus)),
BatchSize::SmallInput,
)
});

let params = DynResidueParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
group.bench_function("DynResidue creation", |b| {
let params = MontyParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
group.bench_function("MontyForm creation", |b| {
b.iter_batched(
|| U256::random(&mut OsRng),
|x| black_box(DynResidue::new(&x, params)),
|x| black_box(MontyForm::new(&x, params)),
BatchSize::SmallInput,
)
});

let params = DynResidueParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
group.bench_function("DynResidue retrieve", |b| {
let params = MontyParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
group.bench_function("MontyForm retrieve", |b| {
b.iter_batched(
|| DynResidue::new(&U256::random(&mut OsRng), params),
|| MontyForm::new(&U256::random(&mut OsRng), params),
|x| black_box(x.retrieve()),
BatchSize::SmallInput,
)
});
}

fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
let params = DynResidueParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
let params = MontyParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();

group.bench_function("invert, U256", |b| {
b.iter_batched(
|| DynResidue::new(&U256::random(&mut OsRng), params),
|| MontyForm::new(&U256::random(&mut OsRng), params),
|x| black_box(x).invert(),
BatchSize::SmallInput,
)
Expand All @@ -53,7 +53,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("Bernstein-Yang invert, U256", |b| {
b.iter_batched(
|| {
let x = DynResidue::new(&U256::random(&mut OsRng), params);
let x = MontyForm::new(&U256::random(&mut OsRng), params);
let inverter = x.params().precompute_inverter();
(x, inverter)
},
Expand All @@ -65,8 +65,8 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("multiplication, U256*U256", |b| {
b.iter_batched(
|| {
let x = DynResidue::new(&U256::random(&mut OsRng), params);
let y = DynResidue::new(&U256::random(&mut OsRng), params);
let x = MontyForm::new(&U256::random(&mut OsRng), params);
let y = MontyForm::new(&U256::random(&mut OsRng), params);
(x, y)
},
|(x, y)| black_box(x * y),
Expand All @@ -78,7 +78,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
b.iter_batched(
|| {
let x = U256::random(&mut OsRng);
let x_m = DynResidue::new(&x, params);
let x_m = MontyForm::new(&x, params);
let p = U256::random(&mut OsRng) | (U256::ONE << (U256::BITS - 1));
(x_m, p)
},
Expand All @@ -94,10 +94,10 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
|b| {
b.iter_batched(
|| {
let bases_and_exponents: Vec<(DynResidue<{ U256::LIMBS }>, U256)> = (1..=i)
let bases_and_exponents: Vec<(MontyForm<{ U256::LIMBS }>, U256)> = (1..=i)
.map(|_| {
let x = U256::random(&mut OsRng);
let x_m = DynResidue::new(&x, params);
let x_m = MontyForm::new(&x, params);
let p = U256::random(&mut OsRng) | (U256::ONE << (U256::BITS - 1));
(x_m, p)
})
Expand All @@ -106,7 +106,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
bases_and_exponents
},
|bases_and_exponents| {
black_box(DynResidue::<{ U256::LIMBS }>::multi_exponentiate(
black_box(MontyForm::<{ U256::LIMBS }>::multi_exponentiate(
bases_and_exponents.as_slice(),
))
},
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@
//! assert_eq!(b, U256::ZERO);
//! ```
//!
//! It also supports modular arithmetic over constant moduli using `Residue`,
//! and over moduli set at runtime using `DynResidue`.
//! It also supports modular arithmetic over constant moduli using `ConstMontyForm`,
//! and over moduli set at runtime using `MontyForm`.
//! That includes modular exponentiation and multiplicative inverses.
//! These features are described in the [`modular`] module.
//!
Expand Down
Loading