-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring the overflow behavior in bit shifts in sync with
std
(#395)
- `const fn` bit shifts for `Uint` return the overflow status as `CtChoice` (and set the result to zero in that case, which is documented, so it's a part of the API now). `Option` would be better for the vartime shifts, but its methods are not `const` yet in stable. - `shl/shr` for `BoxedUint` return `(Self, Choice)` (not `CtOption` since most of its methods need the type to be `ConditionallySelectable`, which `BoxedUint` isn't). The vartime equivalents return `Option<Self>`. - operator impls panic on overflow (which is the default behavior for built-in integers) - made the implementations in `uint/shl.rs` and `shr.rs` more uniform and improved vartime shift performance (before it was calling a constant-time shift-by-no-more-than-limb which added some overhead) - improved constant-time shift performance for `BoxedUint` by reducing the amount of allocations - added an optimized `BoxedUint::shl1()` implementation - added some inlines for `Limb` methods which improved shift performance noticeably - added more benchmarks for shifts and simplify benchmark hierarchy a little (create test group directly in the respective function) - fixed an inefficiency in `Uint` shifts: we need to iterate to log2(BITS-1), not log2(BITS), because that's the maximum size of the shift. - Renamed `sh(r/l)1_with_overflow()` to `sh(r/l)1_with_carry` to avoid confusion - in the context of shifts we call the shift being too large an overflow.
- Loading branch information
Showing
26 changed files
with
641 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion}; | ||
use crypto_bigint::BoxedUint; | ||
use rand_core::OsRng; | ||
|
||
/// Size of `BoxedUint` to use in benchmark. | ||
const UINT_BITS: u32 = 4096; | ||
|
||
fn bench_shifts(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("bit shifts"); | ||
|
||
group.bench_function("shl_vartime", |b| { | ||
b.iter_batched( | ||
|| BoxedUint::random(&mut OsRng, UINT_BITS), | ||
|x| black_box(x.shl_vartime(UINT_BITS / 2 + 10)), | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
|
||
group.bench_function("shl", |b| { | ||
b.iter_batched( | ||
|| BoxedUint::random(&mut OsRng, UINT_BITS), | ||
|x| x.shl(UINT_BITS / 2 + 10), | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
|
||
group.bench_function("shr_vartime", |b| { | ||
b.iter_batched( | ||
|| BoxedUint::random(&mut OsRng, UINT_BITS), | ||
|x| black_box(x.shr_vartime(UINT_BITS / 2 + 10)), | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
|
||
group.bench_function("shr", |b| { | ||
b.iter_batched( | ||
|| BoxedUint::random(&mut OsRng, UINT_BITS), | ||
|x| x.shr(UINT_BITS / 2 + 10), | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, bench_shifts); | ||
|
||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.