forked from entropyxyz/crypto-primes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.rs
More file actions
351 lines (296 loc) · 11.1 KB
/
bench.rs
File metadata and controls
351 lines (296 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use crypto_bigint::{nlimbs, Odd, Uint, U1024};
use rand_chacha::ChaCha8Rng;
use rand_core::{CryptoRngCore, OsRng, SeedableRng};
#[cfg(feature = "tests-gmp")]
use rug::{integer::Order, Integer};
#[cfg(feature = "tests-openssl")]
use openssl::bn::BigNum;
use crypto_primes::{
generate_prime_with_rng, generate_safe_prime_with_rng,
hazmat::{
lucas_test, random_odd_uint, AStarBase, BruteForceBase, LucasCheck, MillerRabin,
SelfridgeBase, Sieve,
},
is_prime_with_rng, is_safe_prime_with_rng,
};
fn make_rng() -> ChaCha8Rng {
ChaCha8Rng::from_seed(*b"01234567890123456789012345678901")
}
fn make_sieve<const L: usize>(rng: &mut impl CryptoRngCore) -> Sieve<Uint<L>> {
let start = random_odd_uint::<Uint<L>>(rng, Uint::<L>::BITS, Uint::<L>::BITS);
Sieve::new(&start, Uint::<L>::BITS, false)
}
fn make_presieved_num<const L: usize>(rng: &mut impl CryptoRngCore) -> Odd<Uint<L>> {
let mut sieve = make_sieve(rng);
Odd::new(sieve.next().unwrap()).unwrap()
}
fn bench_sieve(c: &mut Criterion) {
let mut group = c.benchmark_group("Sieve");
group.bench_function("(U128) random start", |b| {
b.iter(|| random_odd_uint::<Uint<{ nlimbs!(128) }>>(&mut OsRng, 128, 128))
});
group.bench_function("(U128) creation", |b| {
b.iter_batched(
|| random_odd_uint::<Uint<{ nlimbs!(128) }>>(&mut OsRng, 128, 128),
|start| Sieve::new(start.as_ref(), 128, false),
BatchSize::SmallInput,
)
});
// 5 is the average number of pre-sieved samples we need to take before we encounter a prime
group.bench_function("(U128) 5 samples", |b| {
b.iter_batched(
|| make_sieve::<{ nlimbs!(128) }>(&mut OsRng),
|sieve| sieve.take(5).for_each(drop),
BatchSize::SmallInput,
)
});
group.bench_function("(U1024) random start", |b| {
b.iter(|| random_odd_uint::<Uint<{ nlimbs!(1024) }>>(&mut OsRng, 1024, 1024))
});
group.bench_function("(U1024) creation", |b| {
b.iter_batched(
|| random_odd_uint::<Uint<{ nlimbs!(1024) }>>(&mut OsRng, 1024, 1024),
|start| Sieve::new(start.as_ref(), 1024, false),
BatchSize::SmallInput,
)
});
group.bench_function("(U1024) 5 samples", |b| {
b.iter_batched(
|| make_sieve::<{ nlimbs!(1024) }>(&mut OsRng),
|sieve| sieve.take(5).for_each(drop),
BatchSize::SmallInput,
)
});
group.finish()
}
fn bench_miller_rabin(c: &mut Criterion) {
let mut group = c.benchmark_group("Miller-Rabin");
group.bench_function("(U128) creation", |b| {
b.iter_batched(
|| random_odd_uint::<Uint<{ nlimbs!(128) }>>(&mut OsRng, 128, 128),
MillerRabin::new,
BatchSize::SmallInput,
)
});
group.bench_function("(U128) random base test (pre-sieved)", |b| {
b.iter_batched(
|| MillerRabin::new(make_presieved_num::<{ nlimbs!(128) }>(&mut OsRng)),
|mr| mr.test_random_base(&mut OsRng),
BatchSize::SmallInput,
)
});
group.bench_function("(U1024) creation", |b| {
b.iter_batched(
|| random_odd_uint::<Uint<{ nlimbs!(1024) }>>(&mut OsRng, 1024, 1024),
MillerRabin::new,
BatchSize::SmallInput,
)
});
group.bench_function("(U1024) random base test (pre-sieved)", |b| {
b.iter_batched(
|| MillerRabin::new(make_presieved_num::<{ nlimbs!(1024) }>(&mut OsRng)),
|mr| mr.test_random_base(&mut OsRng),
BatchSize::SmallInput,
)
});
}
fn bench_lucas(c: &mut Criterion) {
let mut group = c.benchmark_group("Lucas");
let mut rng = make_rng();
group.bench_function("(U128) Selfridge base, strong check (pre-sieved)", |b| {
b.iter_batched(
|| make_presieved_num::<{ nlimbs!(128) }>(&mut rng),
|n| lucas_test(&n, SelfridgeBase, LucasCheck::Strong),
BatchSize::SmallInput,
)
});
let mut rng = make_rng();
group.bench_function("(U1024) Selfridge base, strong check (pre-sieved)", |b| {
b.iter_batched(
|| make_presieved_num::<{ nlimbs!(1024) }>(&mut rng),
|n| lucas_test(&n, SelfridgeBase, LucasCheck::Strong),
BatchSize::SmallInput,
)
});
let mut rng = make_rng();
group.bench_function("(U1024) A* base, Lucas-V check (pre-sieved)", |b| {
b.iter_batched(
|| make_presieved_num::<{ nlimbs!(1024) }>(&mut rng),
|n| lucas_test(&n, AStarBase, LucasCheck::LucasV),
BatchSize::SmallInput,
)
});
let mut rng = make_rng();
group.bench_function(
"(U1024) brute force base, almost extra strong (pre-sieved)",
|b| {
b.iter_batched(
|| make_presieved_num::<{ nlimbs!(1024) }>(&mut rng),
|n| lucas_test(&n, BruteForceBase, LucasCheck::AlmostExtraStrong),
BatchSize::SmallInput,
)
},
);
let mut rng = make_rng();
group.bench_function("(U1024) brute force base, extra strong (pre-sieved)", |b| {
b.iter_batched(
|| make_presieved_num::<{ nlimbs!(1024) }>(&mut rng),
|n| lucas_test(&n, BruteForceBase, LucasCheck::ExtraStrong),
BatchSize::SmallInput,
)
});
// A number triggering a slow path through the Lucas test, invoking all the available checks:
// - U_{d} checked, but is not 0
// - V_{d * 2^t} checked for t == 0..s-1, but no V = 0 found
// - s = 5, so the previous step has multiple checks
// - Q != 1 (since we're using Selfridge base)
let slow_path = Odd::new(U1024::from_be_hex(concat![
"D1CB9F1B6F3414A4B40A7E51C53C6AE4689DFCDC49FF875E7066A229D704EA8E",
"6B674231D8C5974001673C3CE7FF9D377C8564E5182165A23434BC7B7E6C0419",
"FD25C9921B0E9C90AF2570DB0772E1A9C82ACABBC8FC0F0864CE8A12124FA29B",
"7F870924041DFA13EE5F5541C1BF96CA679EFAE2C96F5F4E9DF6007185198F5F"
]))
.unwrap();
group.bench_function("(U1024) Selfridge base, strong check, slow path", |b| {
b.iter(|| {
lucas_test(&slow_path, SelfridgeBase, LucasCheck::Strong);
})
});
group.finish();
}
fn bench_presets(c: &mut Criterion) {
let mut group = c.benchmark_group("Presets");
group.bench_function("(U128) Prime test", |b| {
b.iter_batched(
|| random_odd_uint::<Uint<{ nlimbs!(128) }>>(&mut OsRng, 128, 128),
|num| is_prime_with_rng(&mut OsRng, num.as_ref()),
BatchSize::SmallInput,
)
});
group.bench_function("(U128) Safe prime test", |b| {
b.iter_batched(
|| random_odd_uint::<Uint<{ nlimbs!(128) }>>(&mut OsRng, 128, 128),
|num| is_safe_prime_with_rng(&mut OsRng, num.as_ref()),
BatchSize::SmallInput,
)
});
let mut rng = make_rng();
group.bench_function("(U128) Random prime", |b| {
b.iter(|| generate_prime_with_rng::<Uint<{ nlimbs!(128) }>>(&mut rng, 128, 128))
});
let mut rng = make_rng();
group.bench_function("(U1024) Random prime", |b| {
b.iter(|| generate_prime_with_rng::<Uint<{ nlimbs!(1024) }>>(&mut rng, 1024, 1024))
});
let mut rng = make_rng();
group.bench_function("(U128) Random safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<Uint<{ nlimbs!(128) }>>(&mut rng, 128, 128))
});
group.sample_size(20);
let mut rng = make_rng();
group.bench_function("(U1024) Random safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<Uint<{ nlimbs!(1024) }>>(&mut rng, 1024, 1024))
});
group.finish();
// A separate group for bounded tests, to make it easier to run them separately.
let mut group = c.benchmark_group("Presets (bounded)");
let mut rng = make_rng();
group.bench_function("(U128) Random safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<Uint<{ nlimbs!(128) }>>(&mut rng, 128, 128))
});
// The performance should scale with the prime size, not with the Uint size.
// So we should strive for this test's result to be as close as possible
// to that of the previous one and as far away as possible from the next one.
group.bench_function("(U256) Random 128 bit safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<Uint<{ nlimbs!(256) }>>(&mut rng, 128, 256))
});
// The upper bound for the previous test.
group.bench_function("(U256) Random 256 bit safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<Uint<{ nlimbs!(256) }>>(&mut rng, 256, 256))
});
group.finish();
}
#[cfg(feature = "tests-gmp")]
fn bench_gmp(c: &mut Criterion) {
let mut group = c.benchmark_group("GMP");
fn random<const L: usize>(rng: &mut impl CryptoRngCore) -> Integer {
let num = random_odd_uint::<Uint<L>>(rng, Uint::<L>::BITS, Uint::<L>::BITS).get();
Integer::from_digits(num.as_words(), Order::Lsf)
}
group.bench_function("(U128) Random prime", |b| {
b.iter_batched(
|| random::<{ nlimbs!(128) }>(&mut OsRng),
|n| n.next_prime(),
BatchSize::SmallInput,
)
});
group.bench_function("(U1024) Random prime", |b| {
b.iter_batched(
|| random::<{ nlimbs!(1024) }>(&mut OsRng),
|n| n.next_prime(),
BatchSize::SmallInput,
)
});
group.finish();
}
#[cfg(not(feature = "tests-gmp"))]
fn bench_gmp(_c: &mut Criterion) {}
#[cfg(feature = "tests-openssl")]
fn bench_openssl(c: &mut Criterion) {
let mut group = c.benchmark_group("OpenSSL");
group.bench_function("(U128) Random prime", |b| {
b.iter_batched(
|| BigNum::new().unwrap(),
|p| {
let mut p = p;
p.generate_prime(128, false, None, None).unwrap()
},
BatchSize::SmallInput,
)
});
group.bench_function("(U1024) Random prime", |b| {
b.iter_batched(
|| BigNum::new().unwrap(),
|p| {
let mut p = p;
p.generate_prime(1024, false, None, None).unwrap()
},
BatchSize::SmallInput,
)
});
group.bench_function("(U128) Random safe prime", |b| {
b.iter_batched(
|| BigNum::new().unwrap(),
|p| {
let mut p = p;
p.generate_prime(128, true, None, None).unwrap()
},
BatchSize::SmallInput,
)
});
group.sample_size(20);
group.bench_function("(U1024) Random safe prime", |b| {
b.iter_batched(
|| BigNum::new().unwrap(),
|p| {
let mut p = p;
p.generate_prime(1024, true, None, None).unwrap()
},
BatchSize::SmallInput,
)
});
group.finish();
}
#[cfg(not(feature = "tests-openssl"))]
fn bench_openssl(_c: &mut Criterion) {}
criterion_group!(
benches,
bench_sieve,
bench_miller_rabin,
bench_lucas,
bench_presets,
bench_gmp,
bench_openssl,
);
criterion_main!(benches);