Skip to content

Commit 5519a2c

Browse files
authored
DynResidue: preliminary Bernstein-Yang benchmarks (#451)
Montgomery arithmetic/invert, U256 time: [5.1410 µs 5.1513 µs 5.1637 µs] Montgomery arithmetic/Bernstein-Yang invert, U256 time: [1.9483 µs 1.9645 µs 1.9863 µs] A 2.6X improvement
1 parent 43dba9c commit 5519a2c

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

benches/dyn_residue.rs

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,64 @@ use criterion::{
44
};
55
use crypto_bigint::{
66
modular::{DynResidue, DynResidueParams},
7-
Random, U256,
7+
Inverter, PrecomputeInverter, Random, U256,
88
};
99
use rand_core::OsRng;
1010

1111
#[cfg(feature = "alloc")]
1212
use crypto_bigint::MultiExponentiate;
1313

14+
fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
15+
group.bench_function("DynResidueParams creation", |b| {
16+
b.iter_batched(
17+
|| U256::random(&mut OsRng) | U256::ONE,
18+
|modulus| black_box(DynResidueParams::new(&modulus)),
19+
BatchSize::SmallInput,
20+
)
21+
});
22+
23+
let params = DynResidueParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
24+
group.bench_function("DynResidue creation", |b| {
25+
b.iter_batched(
26+
|| U256::random(&mut OsRng),
27+
|x| black_box(DynResidue::new(&x, params)),
28+
BatchSize::SmallInput,
29+
)
30+
});
31+
32+
let params = DynResidueParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
33+
group.bench_function("DynResidue retrieve", |b| {
34+
b.iter_batched(
35+
|| DynResidue::new(&U256::random(&mut OsRng), params),
36+
|x| black_box(x.retrieve()),
37+
BatchSize::SmallInput,
38+
)
39+
});
40+
}
41+
1442
fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
1543
let params = DynResidueParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
44+
45+
group.bench_function("invert, U256", |b| {
46+
b.iter_batched(
47+
|| DynResidue::new(&U256::random(&mut OsRng), params),
48+
|x| black_box(x).invert(),
49+
BatchSize::SmallInput,
50+
)
51+
});
52+
53+
group.bench_function("Bernstein-Yang invert, U256", |b| {
54+
b.iter_batched(
55+
|| {
56+
let x = DynResidue::new(&U256::random(&mut OsRng), params);
57+
let inverter = x.params().precompute_inverter();
58+
(x, inverter)
59+
},
60+
|(x, inverter)| inverter.invert(&black_box(x)),
61+
BatchSize::SmallInput,
62+
)
63+
});
64+
1665
group.bench_function("multiplication, U256*U256", |b| {
1766
b.iter_batched(
1867
|| {
@@ -25,8 +74,6 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
2574
)
2675
});
2776

28-
let m = U256::random(&mut OsRng) | U256::ONE;
29-
let params = DynResidueParams::new(&m).unwrap();
3077
group.bench_function("modpow, U256^U256", |b| {
3178
b.iter_batched(
3279
|| {
@@ -70,34 +117,6 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
70117
}
71118
}
72119

73-
fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
74-
group.bench_function("DynResidueParams creation", |b| {
75-
b.iter_batched(
76-
|| U256::random(&mut OsRng) | U256::ONE,
77-
|modulus| black_box(DynResidueParams::new(&modulus)),
78-
BatchSize::SmallInput,
79-
)
80-
});
81-
82-
let params = DynResidueParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
83-
group.bench_function("DynResidue creation", |b| {
84-
b.iter_batched(
85-
|| U256::random(&mut OsRng),
86-
|x| black_box(DynResidue::new(&x, params)),
87-
BatchSize::SmallInput,
88-
)
89-
});
90-
91-
let params = DynResidueParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
92-
group.bench_function("DynResidue retrieve", |b| {
93-
b.iter_batched(
94-
|| DynResidue::new(&U256::random(&mut OsRng), params),
95-
|x| black_box(x.retrieve()),
96-
BatchSize::SmallInput,
97-
)
98-
});
99-
}
100-
101120
fn bench_montgomery(c: &mut Criterion) {
102121
let mut group = c.benchmark_group("Montgomery arithmetic");
103122
bench_montgomery_conversion(&mut group);

0 commit comments

Comments
 (0)