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
57 changes: 52 additions & 5 deletions benches/boxed_uint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use crypto_bigint::{BoxedUint, Limb, NonZero, RandomBits};
use num_bigint::BigUint;
use rand_core::OsRng;

/// Size of `BoxedUint` to use in benchmark.
Expand All @@ -19,7 +20,7 @@ fn bench_shifts(c: &mut Criterion) {
group.bench_function("shl", |b| {
b.iter_batched(
|| BoxedUint::random_bits(&mut OsRng, UINT_BITS),
|x| x.overflowing_shl(UINT_BITS / 2 + 10),
|x| black_box(x.overflowing_shl(UINT_BITS / 2 + 10)),
BatchSize::SmallInput,
)
});
Expand All @@ -35,7 +36,7 @@ fn bench_shifts(c: &mut Criterion) {
group.bench_function("shr", |b| {
b.iter_batched(
|| BoxedUint::random_bits(&mut OsRng, UINT_BITS),
|x| x.overflowing_shr(UINT_BITS / 2 + 10),
|x| black_box(x.overflowing_shr(UINT_BITS / 2 + 10)),
BatchSize::SmallInput,
)
});
Expand Down Expand Up @@ -167,26 +168,72 @@ fn bench_boxed_sqrt(c: &mut Criterion) {
group.bench_function("boxed_sqrt, 4096", |b| {
b.iter_batched(
|| BoxedUint::random_bits(&mut OsRng, UINT_BITS),
|x| x.sqrt(),
|x| black_box(x.sqrt()),
BatchSize::SmallInput,
)
});

group.bench_function("boxed_sqrt_vartime, 4096", |b| {
b.iter_batched(
|| BoxedUint::random_bits(&mut OsRng, UINT_BITS),
|x| x.sqrt_vartime(),
|x| black_box(x.sqrt_vartime()),
BatchSize::SmallInput,
)
});
}

fn bench_radix_encoding(c: &mut Criterion) {
let mut group = c.benchmark_group("boxed_radix_encode");

for radix in [2, 8, 10] {
group.bench_function(format!("from_str_radix_vartime, {radix}"), |b| {
b.iter_batched(
|| BoxedUint::random_bits(&mut OsRng, UINT_BITS).to_string_radix_vartime(10),
|x| {
black_box(BoxedUint::from_str_radix_with_precision_vartime(
&x, radix, UINT_BITS,
))
},
BatchSize::SmallInput,
)
});

group.bench_function(format!("parse_bytes, {radix} (num-bigint-dig)"), |b| {
b.iter_batched(
|| BoxedUint::random_bits(&mut OsRng, UINT_BITS).to_string_radix_vartime(10),
|x| black_box(BigUint::parse_bytes(x.as_bytes(), radix)),
BatchSize::SmallInput,
)
});

group.bench_function(format!("to_str_radix_vartime, {radix}"), |b| {
b.iter_batched(
|| BoxedUint::random_bits(&mut OsRng, UINT_BITS),
|x| black_box(x.to_string_radix_vartime(radix)),
BatchSize::SmallInput,
)
});

group.bench_function(format!("to_str_radix, {radix} (num-bigint-dig)"), |b| {
b.iter_batched(
|| {
let u = BoxedUint::random_bits(&mut OsRng, UINT_BITS);
BigUint::from_bytes_be(&u.to_be_bytes())
},
|x| black_box(x.to_str_radix(radix)),
BatchSize::SmallInput,
)
});
}
}

criterion_group!(
benches,
bench_mul,
bench_division,
bench_shifts,
bench_boxed_sqrt
bench_boxed_sqrt,
bench_radix_encoding,
);

criterion_main!(benches);
2 changes: 1 addition & 1 deletion src/uint/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod bit_xor;
mod bits;
mod cmp;
mod ct;
mod div;
pub(crate) mod div;
mod div_limb;
pub(crate) mod encoding;
mod from;
Expand Down
33 changes: 30 additions & 3 deletions src/uint/boxed/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::BoxedUint;
use crate::{uint::encoding, DecodeError, Limb, Word};
use alloc::{boxed::Box, vec::Vec};
use alloc::{boxed::Box, string::String, vec::Vec};
use subtle::{Choice, CtOption};

impl BoxedUint {
Expand Down Expand Up @@ -147,7 +147,7 @@ impl BoxedUint {
/// Panics if `radix` is not in the range from 2 to 36.
pub fn from_str_radix_vartime(src: &str, radix: u32) -> Result<Self, DecodeError> {
let mut dec = VecDecodeByLimb::default();
encoding::decode_str_radix(src, radix, &mut dec)?;
encoding::radix_decode_str(src, radix, &mut dec)?;
Ok(Self {
limbs: dec.limbs.into(),
})
Expand Down Expand Up @@ -177,7 +177,7 @@ impl BoxedUint {
bits_precision: u32,
) -> Result<Self, DecodeError> {
let mut ret = Self::zero_with_precision(bits_precision);
encoding::decode_str_radix(
encoding::radix_decode_str(
src,
radix,
&mut encoding::SliceDecodeByLimb::new(&mut ret.limbs),
Expand All @@ -187,6 +187,13 @@ impl BoxedUint {
}
Ok(ret)
}

/// Format a [`BoxedUint`] as a string in a given base.
///
/// Panics if `radix` is not in the range from 2 to 36.
pub fn to_string_radix_vartime(&self, radix: u32) -> String {
encoding::radix_encode_limbs_to_string(radix, &self.limbs)
}
}

/// Decoder target producing a Vec<Limb>
Expand Down Expand Up @@ -458,4 +465,24 @@ mod tests {
let res = BoxedUint::from_str_radix_vartime(hex, 16).expect("error decoding");
assert_eq!(hex, format!("{res:x}"));
}

#[test]
#[cfg(feature = "rand_core")]
fn encode_radix_round_trip() {
use crate::RandomBits;
use rand_core::SeedableRng;
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(1);

for _ in 0..100 {
let uint = BoxedUint::random_bits(&mut rng, 4096);
for radix in 2..=36 {
let enc = uint.to_string_radix_vartime(radix);
let res = BoxedUint::from_str_radix_vartime(&enc, radix).expect("decoding error");
assert_eq!(
res, uint,
"round trip failure: radix {radix} encoded {uint} as {enc}"
);
}
}
}
}
5 changes: 5 additions & 0 deletions src/uint/div_limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ impl Reciprocal {
}
}

#[cfg(feature = "alloc")]
pub(crate) const fn divisor(&self) -> NonZero<Limb> {
NonZero(Limb(self.divisor_normalized >> self.shift))
}

/// Get the shift value
pub const fn shift(&self) -> u32 {
self.shift
Expand Down
Loading