Skip to content

Commit

Permalink
Introduce Uint::new_overflowing_shr
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-3milabs committed Jan 24, 2025
1 parent 1c98833 commit b04e142
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
24 changes: 15 additions & 9 deletions benches/uint.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::ops::Mul;
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use crypto_bigint::{
Limb, NonZero, Odd, Random, RandomBits, RandomMod, Reciprocal, Uint, U1024, U128, U2048, U256,
U4096, U512,
};
use crypto_bigint::{Limb, NonZero, Odd, Random, RandomBits, RandomMod, Reciprocal, Uint, U1024, U128, U2048, U256, U4096, U512, U64};
use rand_chacha::ChaCha8Rng;
use rand_core::{OsRng, RngCore, SeedableRng};

Expand Down Expand Up @@ -405,6 +403,14 @@ fn bench_shr(c: &mut Criterion) {
)
});

group.bench_function("new shr, U2048", |b| {
b.iter_batched(
|| U2048::ONE,
|x| x.new_overflowing_shr(1024 + 10),
BatchSize::SmallInput,
)
});

group.finish();
}

Expand Down Expand Up @@ -487,11 +493,11 @@ fn bench_sqrt(c: &mut Criterion) {

criterion_group!(
benches,
bench_random,
bench_mul,
bench_division,
bench_gcd,
bench_shl,
// bench_random,
// bench_mul,
// bench_division,
// bench_gcd,
// bench_shl,
bench_shr,
bench_inv_mod,
bench_sqrt
Expand Down
5 changes: 5 additions & 0 deletions src/uint/shr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ impl<const LIMBS: usize> Uint<LIMBS> {
ConstCtOption::new(Uint::select(&result, &Self::ZERO, overflow), overflow.not())
}

pub const fn new_overflowing_shr(&self, shift: u32) -> ConstCtOption<Self> {
let (intra_limb_shift, limb_shift) = (shift % Limb::BITS, shift / Limb::BITS);
self.intra_limb_carrying_shr_internal(intra_limb_shift).full_limb_shr(limb_shift)
}

/// Computes `self >> shift`, for `shift < Limb::BITS`.
///
/// Returns `None` if `shift >= Limb::BITS`.
Expand Down

0 comments on commit b04e142

Please sign in to comment.