Skip to content

Commit

Permalink
Fix benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Dec 21, 2023
1 parent 546fa3b commit 57c18de
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
25 changes: 8 additions & 17 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, RandomMod,
BoxedUint, NonZero, Odd, RandomMod,
};
use num_bigint::BigUint;
use rand_core::OsRng;
Expand All @@ -17,10 +17,7 @@ fn to_biguint(uint: &BoxedUint) -> BigUint {
}

fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
let params = BoxedMontyParams::new(
BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
)
.unwrap();
let params = BoxedMontyParams::new(Odd::<BoxedUint>::random(&mut OsRng, UINT_BITS));

group.bench_function("invert, U256", |b| {
b.iter_batched(
Expand Down Expand Up @@ -60,8 +57,8 @@ 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 = BoxedMontyParams::new(m).unwrap();
let m = Odd::<BoxedUint>::random(&mut OsRng, UINT_BITS);
let params = BoxedMontyParams::new(m);
group.bench_function("modpow, BoxedUint^BoxedUint", |b| {
b.iter_batched(
|| {
Expand Down Expand Up @@ -96,24 +93,21 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
group.bench_function("BoxedMontyParams::new", |b| {
b.iter_batched(
|| BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
|| Odd::<BoxedUint>::random(&mut OsRng, UINT_BITS),
|modulus| black_box(BoxedMontyParams::new(modulus)),
BatchSize::SmallInput,
)
});

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

let params = BoxedMontyParams::new(
BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
)
.unwrap();
let params = BoxedMontyParams::new(Odd::<BoxedUint>::random(&mut OsRng, UINT_BITS));
group.bench_function("BoxedMontyForm::new", |b| {
b.iter_batched(
|| BoxedUint::random(&mut OsRng, UINT_BITS),
Expand All @@ -122,10 +116,7 @@ fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>
)
});

let params = BoxedMontyParams::new(
BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
)
.unwrap();
let params = BoxedMontyParams::new(Odd::<BoxedUint>::random(&mut OsRng, UINT_BITS));
group.bench_function("BoxedMontyForm::retrieve", |b| {
b.iter_batched(
|| BoxedMontyForm::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone()),
Expand Down
14 changes: 13 additions & 1 deletion src/limb/bit_and.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Limb bit and operations.
use super::Limb;
use core::ops::BitAnd;
use core::ops::{BitAnd, BitAndAssign};

impl Limb {
/// Calculates `a & b`.
Expand All @@ -19,3 +19,15 @@ impl BitAnd for Limb {
self.bitand(rhs)
}
}

impl BitAndAssign for Limb {
fn bitand_assign(&mut self, rhs: Self) {
self.0 &= rhs.0;
}
}

impl BitAndAssign<&Limb> for Limb {
fn bitand_assign(&mut self, rhs: &Limb) {
self.0 &= rhs.0;
}
}
8 changes: 7 additions & 1 deletion src/limb/bit_xor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Limb bit xor operations.
use super::Limb;
use core::ops::BitXor;
use core::ops::{BitXor, BitXorAssign};

impl Limb {
/// Calculates `a ^ b`.
Expand All @@ -18,3 +18,9 @@ impl BitXor for Limb {
self.bitxor(rhs)
}
}

impl BitXorAssign for Limb {
fn bitxor_assign(&mut self, rhs: Self) {
self.0 ^= rhs.0;
}
}
19 changes: 17 additions & 2 deletions src/odd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use subtle::{Choice, ConditionallySelectable, CtOption};
use crate::BoxedUint;

#[cfg(feature = "rand_core")]
use {crate::Random, rand_core::CryptoRngCore};
use {
crate::{Limb, Random},
rand_core::CryptoRngCore,
};

/// Wrapper type for odd integers.
///
Expand Down Expand Up @@ -125,6 +128,18 @@ impl PartialOrd<Odd<BoxedUint>> for BoxedUint {
impl<const LIMBS: usize> Random for Odd<Uint<LIMBS>> {
/// Generate a random `NonZero<Uint<T>>`.
fn random(rng: &mut impl CryptoRngCore) -> Self {
Odd(Uint::random(rng) | Uint::ONE)
let mut ret = Uint::random(rng);
ret.limbs[0] &= Limb::ONE;
Odd(ret)
}
}

#[cfg(all(feature = "alloc", feature = "rand_core"))]
impl Odd<BoxedUint> {
/// Generate a random `NonZero<Uint<T>>`.
pub fn random(rng: &mut impl CryptoRngCore, bits_precision: u32) -> Self {
let mut ret = BoxedUint::random(rng, bits_precision);
ret.limbs[0] &= Limb::ONE;
Odd(ret)
}
}

0 comments on commit 57c18de

Please sign in to comment.