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
49 changes: 25 additions & 24 deletions benches/monty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use criterion::{
};
use crypto_bigint::{
Odd, Random, RandomMod, U256,
modular::{MontyForm, MontyParams},
modular::{FixedMontyForm, FixedMontyParams},
};
use rand_core::SeedableRng;

Expand All @@ -21,33 +21,33 @@ fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>
group.bench_function("MontyParams::new", |b| {
b.iter_batched(
|| Odd::<U256>::random_from_rng(&mut rng),
|modulus| black_box(MontyParams::new(modulus)),
|modulus| black_box(FixedMontyParams::new(modulus)),
BatchSize::SmallInput,
);
});

group.bench_function("MontyParams::new_vartime", |b| {
b.iter_batched(
|| Odd::<U256>::random_from_rng(&mut rng),
|modulus| black_box(MontyParams::new_vartime(modulus)),
|modulus| black_box(FixedMontyParams::new_vartime(modulus)),
BatchSize::SmallInput,
);
});

let params = MontyParams::new_vartime(Odd::<U256>::random_from_rng(&mut rng));
let params = FixedMontyParams::new_vartime(Odd::<U256>::random_from_rng(&mut rng));
group.bench_function("MontyForm::new", |b| {
b.iter_batched(
|| U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
|x| black_box(MontyForm::new(&x, &params)),
|x| black_box(FixedMontyForm::new(&x, &params)),
BatchSize::SmallInput,
);
});

let params = MontyParams::new_vartime(Odd::<U256>::random_from_rng(&mut rng));
let params = FixedMontyParams::new_vartime(Odd::<U256>::random_from_rng(&mut rng));
group.bench_function("MontyForm retrieve", |b| {
b.iter_batched(
|| {
MontyForm::new(
FixedMontyForm::new(
&U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
&params,
)
Expand All @@ -60,16 +60,16 @@ fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>

fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
let mut rng = ChaCha8Rng::from_seed([7u8; 32]);
let params = MontyParams::new_vartime(Odd::<U256>::random_from_rng(&mut rng));
let params = FixedMontyParams::new_vartime(Odd::<U256>::random_from_rng(&mut rng));

group.bench_function("add, U256", |b| {
b.iter_batched(
|| {
let a = MontyForm::new(
let a = FixedMontyForm::new(
&U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
&params,
);
let b = MontyForm::new(
let b = FixedMontyForm::new(
&U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
&params,
);
Expand All @@ -83,7 +83,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("double, U256", |b| {
b.iter_batched(
|| {
MontyForm::new(
FixedMontyForm::new(
&U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
&params,
)
Expand All @@ -96,11 +96,11 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("sub, U256", |b| {
b.iter_batched(
|| {
let a = MontyForm::new(
let a = FixedMontyForm::new(
&U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
&params,
);
let b = MontyForm::new(
let b = FixedMontyForm::new(
&U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
&params,
);
Expand All @@ -114,7 +114,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("neg, U256", |b| {
b.iter_batched(
|| {
MontyForm::new(
FixedMontyForm::new(
&U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
&params,
)
Expand All @@ -127,7 +127,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("invert, U256", |b| {
b.iter_batched(
|| {
MontyForm::new(
FixedMontyForm::new(
&U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
&params,
)
Expand All @@ -140,11 +140,11 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("multiplication, U256*U256", |b| {
b.iter_batched(
|| {
let x = MontyForm::new(
let x = FixedMontyForm::new(
&U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
&params,
);
let y = MontyForm::new(
let y = FixedMontyForm::new(
&U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
&params,
);
Expand All @@ -158,7 +158,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("square, U256", |b| {
b.iter_batched(
|| {
MontyForm::new(
FixedMontyForm::new(
&U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref()),
&params,
)
Expand All @@ -172,7 +172,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
b.iter_batched(
|| {
let x = U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref());
let x_m = MontyForm::new(&x, &params);
let x_m = FixedMontyForm::new(&x, &params);
let p = U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref())
| (U256::ONE << (U256::BITS - 1));
(x_m, p)
Expand All @@ -186,7 +186,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
b.iter_batched(
|| {
let x = U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref());
let x_m = MontyForm::new(&x, &params);
let x_m = FixedMontyForm::new(&x, &params);
let p = U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref())
| (U256::ONE << (U256::BITS - 1));
(x_m, p)
Expand All @@ -200,7 +200,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
b.iter_batched(
|| {
let x = U256::random_mod_vartime(&mut rng, params.modulus().as_nz_ref());
MontyForm::new(&x, &params)
FixedMontyForm::new(&x, &params)
},
|x| black_box(x.div_by_2()),
BatchSize::SmallInput,
Expand All @@ -214,13 +214,14 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
|b| {
b.iter_batched(
|| {
let bases_and_exponents: Vec<(MontyForm<{ U256::LIMBS }>, U256)> = (1..=i)
let bases_and_exponents: Vec<(FixedMontyForm<{ U256::LIMBS }>, U256)> = (1
..=i)
.map(|_| {
let x = U256::random_mod_vartime(
&mut rng,
params.modulus().as_nz_ref(),
);
let x_m = MontyForm::new(&x, &params);
let x_m = FixedMontyForm::new(&x, &params);
let p = U256::random_mod_vartime(
&mut rng,
params.modulus().as_nz_ref(),
Expand All @@ -232,7 +233,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
bases_and_exponents
},
|bases_and_exponents| {
black_box(MontyForm::<{ U256::LIMBS }>::multi_exponentiate(
black_box(FixedMontyForm::<{ U256::LIMBS }>::multi_exponentiate(
bases_and_exponents.as_slice(),
))
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//!
//! - [`modular::ConstMontyForm`]: stack-allocated type-safe modular arithmetic using Montgomery
//! form suitable for cases where the modulus is known at compile-time.
//! - [`modular::MontyForm`]: stack-allocated modular arithmetic using Montgomery form for cases
//! - [`modular::FixedMontyForm`]: stack-allocated modular arithmetic using Montgomery form for cases
//! where the modulus is only known at runtime.
//! - [`modular::BoxedMontyForm`]: heap-allocated modular arithmetic using Montgomery form.
//! Requires the `alloc` crate feature is enabled.
Expand Down
8 changes: 4 additions & 4 deletions src/modular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
//!
//! # Dynamic moduli chosen at runtime
//!
//! The [`MontyForm`] and [`MontyParams`] types implement support for modular arithmetic where
//! The [`FixedMontyForm`] and [`FixedMontyParams`] types implement support for modular arithmetic where
//! the modulus can vary at runtime.

mod const_monty_form;
mod fixed_monty_form;
mod lincomb;
mod monty_form;
mod reduction;

mod add;
Expand All @@ -36,8 +36,8 @@ pub(crate) mod boxed_monty_form;

pub use self::{
const_monty_form::{ConstMontyForm, ConstMontyParams},
monty_form::MontyForm,
monty_params::{GenericMontyParams, MontyParams},
fixed_monty_form::FixedMontyForm,
monty_params::{FixedMontyParams, MontyParams},
};

pub(crate) use self::safegcd::SafeGcdInverter;
Expand Down
6 changes: 3 additions & 3 deletions src/modular/boxed_monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ mod pow;
mod sub;

use super::{
BoxedMontyParams, Retrieve, div_by_2, monty_params::GenericMontyParams,
BoxedMontyParams, Retrieve, div_by_2, monty_params::MontyParams,
reduction::montgomery_retrieve_inner,
};
use crate::{BoxedUint, Choice, Monty, Odd};
use crate::{BoxedUint, Choice, MontyForm, Odd};
use mul::BoxedMontyMultiplier;

#[cfg(feature = "zeroize")]
Expand Down Expand Up @@ -159,7 +159,7 @@ impl Retrieve for BoxedMontyForm {
}
}

impl Monty for BoxedMontyForm {
impl MontyForm for BoxedMontyForm {
type Integer = BoxedUint;
type Params = BoxedMontyParams;
type Multiplier<'a> = BoxedMontyMultiplier<'a>;
Expand Down
22 changes: 11 additions & 11 deletions src/modular/boxed_monty_form/from.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! `From`-like conversions for [`BoxedMontyForm`] and [`BoxedMontyParams`].

use super::{BoxedMontyForm, BoxedMontyParams, GenericMontyParams};
use super::{BoxedMontyForm, BoxedMontyParams, MontyParams};
use crate::{
BoxedUint,
modular::{ConstMontyForm, ConstMontyParams, MontyForm, MontyParams},
modular::{ConstMontyForm, ConstMontyParams, FixedMontyForm, FixedMontyParams},
};

impl<const LIMBS: usize, Params> From<ConstMontyForm<Params, LIMBS>> for BoxedMontyForm
Expand All @@ -27,24 +27,24 @@ where
}
}

impl<const LIMBS: usize> From<MontyForm<LIMBS>> for BoxedMontyForm {
fn from(input: MontyForm<LIMBS>) -> Self {
impl<const LIMBS: usize> From<FixedMontyForm<LIMBS>> for BoxedMontyForm {
fn from(input: FixedMontyForm<LIMBS>) -> Self {
Self::from(&input)
}
}

impl<const LIMBS: usize> From<&MontyForm<LIMBS>> for BoxedMontyForm {
fn from(input: &MontyForm<LIMBS>) -> Self {
impl<const LIMBS: usize> From<&FixedMontyForm<LIMBS>> for BoxedMontyForm {
fn from(input: &FixedMontyForm<LIMBS>) -> Self {
BoxedMontyForm {
montgomery_form: input.as_montgomery().into(),
params: input.params().into(),
}
}
}

impl<const LIMBS: usize> From<&MontyParams<LIMBS>> for BoxedMontyParams {
fn from(params: &MontyParams<LIMBS>) -> Self {
GenericMontyParams::<BoxedUint> {
impl<const LIMBS: usize> From<&FixedMontyParams<LIMBS>> for BoxedMontyParams {
fn from(params: &FixedMontyParams<LIMBS>) -> Self {
MontyParams::<BoxedUint> {
modulus: params.modulus.into(),
one: params.one.into(),
r2: params.r2.into(),
Expand All @@ -55,8 +55,8 @@ impl<const LIMBS: usize> From<&MontyParams<LIMBS>> for BoxedMontyParams {
}
}

impl<const LIMBS: usize> From<MontyParams<LIMBS>> for BoxedMontyParams {
fn from(params: MontyParams<LIMBS>) -> Self {
impl<const LIMBS: usize> From<FixedMontyParams<LIMBS>> for BoxedMontyParams {
fn from(params: FixedMontyParams<LIMBS>) -> Self {
BoxedMontyParams::from(&params)
}
}
4 changes: 2 additions & 2 deletions src/modular/const_monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod reduce;
mod sub;

use super::{
MontyParams, Retrieve, div_by_2::div_by_2, mul::mul_montgomery_form,
FixedMontyParams, Retrieve, div_by_2::div_by_2, mul::mul_montgomery_form,
reduction::montgomery_retrieve,
};
use crate::{ConstOne, ConstZero, CtEq, Odd, One, Uint, Zero};
Expand Down Expand Up @@ -44,7 +44,7 @@ pub trait ConstMontyParams<const LIMBS: usize>:
const LIMBS: usize;

/// Montgomery parameters constant.
const PARAMS: MontyParams<LIMBS>;
const PARAMS: FixedMontyParams<LIMBS>;
}

/// An integer in Montgomery form modulo `MOD`, represented using `LIMBS` limbs.
Expand Down
8 changes: 4 additions & 4 deletions src/modular/const_monty_form/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ macro_rules! const_monty_params {
pub struct $name;
impl $crate::modular::ConstMontyParams<{ <$uint_type>::LIMBS }> for $name {
const LIMBS: usize = <$uint_type>::LIMBS;
const PARAMS: $crate::modular::MontyParams<{ <$uint_type>::LIMBS }> =
$crate::modular::MontyParams::new_vartime($crate::Odd::<$uint_type>::from_be_hex(
$value,
));
const PARAMS: $crate::modular::FixedMontyParams<{ <$uint_type>::LIMBS }> =
$crate::modular::FixedMontyParams::new_vartime(
$crate::Odd::<$uint_type>::from_be_hex($value),
);
}
};
}
Expand Down
Loading