-
Notifications
You must be signed in to change notification settings - Fork 19
/
bench.rs
639 lines (592 loc) · 22.2 KB
/
bench.rs
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
use blsttc::{
group::ff::Field,
poly::{BivarPoly, Poly},
Fr,
};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
const TEST_DEGREES: [usize; 4] = [5, 10, 20, 40];
const TEST_THRESHOLDS: [usize; 4] = [5, 10, 20, 40];
const RNG_SEED: [u8; 16] = *b"0123456789abcdef";
mod poly_benches {
use super::*;
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
/// Benchmarks multiplication of two polynomials.
fn multiplication(c: &mut Criterion) {
let mut group = c.benchmark_group("Polynomial");
let mut rng = XorShiftRng::from_seed(RNG_SEED);
for deg in TEST_DEGREES {
let parameter_string = format!("{deg}");
group.bench_with_input(
BenchmarkId::new("multiplication", parameter_string),
°,
|b, deg| {
let rand_factors = || {
let lhs = Poly::random(*deg, &mut rng);
let rhs = Poly::random(*deg, &mut rng);
(lhs, rhs)
};
b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs * &rhs)
},
);
}
group.finish();
}
/// Benchmarks subtraction of two polynomials
fn subtraction(c: &mut Criterion) {
let mut group = c.benchmark_group("Polynomial");
let mut rng = XorShiftRng::from_seed(RNG_SEED);
for deg in TEST_DEGREES {
let parameter_string = format!("{deg}");
group.bench_with_input(
BenchmarkId::new("subtraction", parameter_string),
°,
|b, deg| {
let rand_factors = || {
let lhs = Poly::random(*deg, &mut rng);
let rhs = Poly::random(*deg, &mut rng);
(lhs, rhs)
};
b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs - &rhs)
},
);
}
}
/// Benchmarks addition of two polynomials
fn addition(c: &mut Criterion) {
let mut group = c.benchmark_group("Polynomial");
let mut rng = XorShiftRng::from_seed(RNG_SEED);
for deg in TEST_DEGREES {
let parameter_string = format!("{deg}");
group.bench_with_input(
BenchmarkId::new("addition", parameter_string),
°,
|b, deg| {
let rand_factors = || {
let lhs = Poly::random(*deg, &mut rng);
let rhs = Poly::random(*deg, &mut rng);
(lhs, rhs)
};
b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs + &rhs)
},
);
}
}
/// Benchmarks Lagrange interpolation for a polynomial.
fn interpolate(c: &mut Criterion) {
let mut group = c.benchmark_group("Polynomial");
let mut rng = XorShiftRng::from_seed(RNG_SEED);
for deg in TEST_DEGREES {
let parameter_string = format!("{deg}");
group.bench_with_input(
BenchmarkId::new("interpolation", parameter_string),
°,
|b, deg| {
b.iter_with_setup(
|| {
(0..=*deg)
.map(|i| (i, Fr::random(&mut rng)))
.collect::<Vec<_>>()
},
Poly::interpolate,
)
},
);
}
}
/// Benchmarks evaluation of Polynomial into Fr
fn evaluate(c: &mut Criterion) {
let mut group = c.benchmark_group("Polynomial");
let mut rng = XorShiftRng::from_seed(RNG_SEED);
for deg in TEST_DEGREES {
let parameter_string = format!("{deg}");
group.bench_with_input(
BenchmarkId::new("evaluate", parameter_string),
°,
|b, deg| {
let rand_factors = || {
let poly = Poly::random(*deg, &mut rng);
let x = Fr::random(&mut rng);
(poly, x)
};
b.iter_with_setup(rand_factors, |(poly, x)| poly.evaluate(x))
},
);
}
}
criterion_group! {
name = poly_benches;
config = Criterion::default();
targets = multiplication, interpolate, addition, subtraction, evaluate,
}
}
mod bivarpoly_benches {
use super::*;
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
/// Benchmarks evaluation of BivarPolynomial into Fr
fn evaluate(c: &mut Criterion) {
let mut group = c.benchmark_group("BivarPolynomial");
let mut rng = XorShiftRng::from_seed(RNG_SEED);
for deg in TEST_DEGREES {
let parameter_string = format!("{deg}");
group.bench_with_input(
BenchmarkId::new("evaluate", parameter_string),
°,
|b, deg| {
let rand_factors = || {
let poly = BivarPoly::random(*deg, &mut rng);
let x = Fr::random(&mut rng);
let y = Fr::random(&mut rng);
(poly, x, y)
};
b.iter_with_setup(rand_factors, |(poly, x, y)| poly.evaluate(x, y))
},
);
}
}
/// Benchmarks evaluation of BivarPolynomial row
fn row(c: &mut Criterion) {
let mut group = c.benchmark_group("BivarPolynomial");
let mut rng = XorShiftRng::from_seed(RNG_SEED);
for deg in TEST_DEGREES {
let parameter_string = format!("{deg}");
group.bench_with_input(BenchmarkId::new("row", parameter_string), °, |b, deg| {
let rand_factors = || {
let poly = BivarPoly::random(*deg, &mut rng);
let x = Fr::random(&mut rng);
(poly, x)
};
b.iter_with_setup(rand_factors, |(poly, x)| poly.row(x))
});
}
}
criterion_group! {
name = bivarpoly_benches;
config = Criterion::default();
targets = evaluate, row,
}
}
mod commitment_benches {
use super::*;
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
/// Benchmarks addition of two commitments
fn addition(c: &mut Criterion) {
let mut group = c.benchmark_group("Commitment");
let mut rng = XorShiftRng::from_seed(RNG_SEED);
for deg in TEST_DEGREES {
let parameter_string = format!("{deg}");
group.bench_with_input(
BenchmarkId::new("addition", parameter_string),
°,
|b, deg| {
let rand_factors = || {
let lhs = Poly::random(*deg, &mut rng).commitment();
let rhs = Poly::random(*deg, &mut rng).commitment();
(lhs, rhs)
};
b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs + &rhs)
},
);
}
}
/// Benchmarks evaluation of Commitment into P1
fn evaluate(c: &mut Criterion) {
let mut group = c.benchmark_group("Commitment");
let mut rng = XorShiftRng::from_seed(RNG_SEED);
for deg in TEST_DEGREES {
let parameter_string = format!("{deg}");
group.bench_with_input(
BenchmarkId::new("evaluate", parameter_string),
°,
|b, deg| {
let rand_factors = || {
let commit = Poly::random(*deg, &mut rng).commitment();
let x = Fr::random(&mut rng);
(commit, x)
};
b.iter_with_setup(rand_factors, |(commit, x)| commit.evaluate(x))
},
);
}
}
criterion_group! {
name = commitment_benches;
config = Criterion::default();
targets = addition, evaluate,
}
}
mod bivarcommitment_benches {
use super::*;
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
/// Benchmarks evaluation of BivarCommitment into P1
fn evaluate(c: &mut Criterion) {
let mut group = c.benchmark_group("BivarCommitment");
let mut rng = XorShiftRng::from_seed(RNG_SEED);
for deg in TEST_DEGREES {
let parameter_string = format!("{deg}");
group.bench_with_input(
BenchmarkId::new("evaluate", parameter_string),
°,
|b, deg| {
let rand_factors = || {
let commit = BivarPoly::random(*deg, &mut rng).commitment();
let x = Fr::random(&mut rng);
let y = Fr::random(&mut rng);
(commit, x, y)
};
b.iter_with_setup(rand_factors, |(commit, x, y)| commit.evaluate(x, y))
},
);
}
}
/// Benchmarks evaluation of BivarCommitment row
fn row(c: &mut Criterion) {
let mut group = c.benchmark_group("BivarCommitment");
let mut rng = XorShiftRng::from_seed(RNG_SEED);
for deg in TEST_DEGREES {
let parameter_string = format!("{deg}");
group.bench_with_input(BenchmarkId::new("row", parameter_string), °, |b, deg| {
let rand_factors = || {
let commit = BivarPoly::random(*deg, &mut rng).commitment();
let x = Fr::random(&mut rng);
(commit, x)
};
b.iter_with_setup(rand_factors, |(commit, x)| commit.row(x))
});
}
}
criterion_group! {
name = bivarcommitment_benches;
config = Criterion::default();
targets = evaluate, row,
}
}
mod public_key_set_benches {
use super::*;
use blsttc::{PublicKeySet, SecretKeySet};
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
use std::collections::BTreeMap;
/// Benchmarks combining signatures
fn combine_signatures(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("PublicKeySet");
let msg = "Test message";
for threshold in TEST_THRESHOLDS {
let parameter_string = format!("{threshold}");
group.bench_with_input(
BenchmarkId::new("combine signatures", parameter_string),
&threshold,
|b, threshold| {
let sk_set = SecretKeySet::random(*threshold, &mut rng);
let pk_set = sk_set.public_keys();
let mut sigs = BTreeMap::default();
for i in 0..=*threshold {
let sig = sk_set.secret_key_share(i).sign(msg);
sigs.insert(i, sig);
}
b.iter(|| {
pk_set
.combine_signatures(&sigs)
.expect("could not combine signatures");
})
},
);
}
}
/// Benchmarks combining decryption shares
fn combine_decryption_shares(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("PublicKeySet");
let msg = "Test message";
for threshold in TEST_THRESHOLDS {
let parameter_string = format!("{threshold}");
group.bench_with_input(
BenchmarkId::new("combine decryption_shares", parameter_string),
&threshold,
|b, threshold| {
let sk_set = SecretKeySet::random(*threshold, &mut rng);
let sk = sk_set.secret_key();
let pk = sk.public_key();
let ct = pk.encrypt(msg);
let pk_set = sk_set.public_keys();
let decryption_shares: BTreeMap<_, _> = (0..=*threshold)
.map(|i| {
let keyshare = sk_set.secret_key_share(i);
let share = keyshare.decrypt_share(&ct).expect("ct did not verify");
(i, share)
})
.collect();
b.iter(|| {
pk_set
.decrypt(&decryption_shares, &ct)
.expect("could not decrypt from decryption_shares");
})
},
);
}
}
/// Benchmarks serialization of a PublicKeySet
fn serialize(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("PublicKeySet");
for threshold in TEST_THRESHOLDS {
let parameter_string = format!("{threshold}");
group.bench_with_input(
BenchmarkId::new("serialize", parameter_string),
&threshold,
|b, threshold| {
let sk_set = SecretKeySet::random(*threshold, &mut rng);
let pk_set = sk_set.public_keys();
b.iter(|| {
let _bytes = bincode::serialize(&pk_set).unwrap();
})
},
);
}
}
/// Benchmarks deserialization of a PublicKeySet
fn deserialize(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("PublicKeySet");
for threshold in TEST_THRESHOLDS {
let parameter_string = format!("{threshold}");
group.bench_with_input(
BenchmarkId::new("deserialize", parameter_string),
&threshold,
|b, threshold| {
let sk_set = SecretKeySet::random(*threshold, &mut rng);
let pk_set = sk_set.public_keys();
let bytes = bincode::serialize(&pk_set).unwrap();
b.iter(|| {
let _pks: PublicKeySet = bincode::deserialize(&bytes).unwrap();
})
},
);
}
}
criterion_group! {
name = public_key_set_benches;
config = Criterion::default();
targets = combine_signatures, combine_decryption_shares, serialize, deserialize,
}
}
mod public_key_benches {
use super::*;
use blsttc::{hash_g2, SecretKey};
use rand::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
/// Benchmarks verifying a 1000 byte message signature
fn verify(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("PublicKey");
group.bench_function("verify", |b| {
let rand_factors = || {
let sk = SecretKey::random();
let pk = sk.public_key();
let mut msg = [0u8; 1000];
rng.fill_bytes(&mut msg);
let sig = sk.sign(msg);
(pk, msg, sig)
};
b.iter_with_setup(rand_factors, |(pk, msg, sig)| pk.verify(&sig, msg));
});
}
/// Benchmarks verifying a signature for a point p2
fn verify_g2(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("PublicKey");
group.bench_function("verify_g2", |b| {
let rand_factors = || {
let sk = SecretKey::random();
let pk = sk.public_key();
let mut msg = [0u8; 1000];
rng.fill_bytes(&mut msg);
let hash = hash_g2(msg);
let sig = sk.sign_g2(hash);
(pk, hash, sig)
};
b.iter_with_setup(rand_factors, |(pk, hash, sig)| pk.verify_g2(&sig, hash));
});
}
/// Benchmarks deriving a child public key
fn derive_child(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("PublicKey");
group.bench_function("derive_child", |b| {
let rand_factors = || {
let sk = SecretKey::random();
let pk = sk.public_key();
let mut index = [0u8; 32];
rng.fill_bytes(&mut index);
(pk, index)
};
b.iter_with_setup(rand_factors, |(pk, index)| pk.derive_child(&index[..]));
});
}
/// Benchmarks encrypting a 1000 byte message
fn encrypt(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("PublicKey");
group.bench_function("encrypt", |b| {
let rand_factors = || {
let sk = SecretKey::random();
let pk = sk.public_key();
let mut msg = [0u8; 1000];
rng.fill_bytes(&mut msg);
(pk, msg)
};
b.iter_with_setup(rand_factors, |(pk, msg)| pk.encrypt(msg));
});
}
criterion_group! {
name = public_key_benches;
config = Criterion::default();
targets = verify, verify_g2, derive_child, encrypt,
}
}
mod secret_key_benches {
use super::*;
use blsttc::{hash_g2, SecretKey};
use rand::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
/// Benchmarks signing a 1000 byte message
fn sign(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("SecretKey");
group.bench_function("sign", |b| {
let rand_factors = || {
let sk = SecretKey::random();
let mut msg = [0u8; 1000];
rng.fill_bytes(&mut msg);
(sk, msg)
};
b.iter_with_setup(rand_factors, |(sk, msg)| sk.sign(msg));
});
}
/// Benchmarks signing a point p2
fn sign_g2(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("SecretKey");
group.bench_function("sign_g2", |b| {
let rand_factors = || {
let sk = SecretKey::random();
let mut msg = [0u8; 1000];
rng.fill_bytes(&mut msg);
let hash = hash_g2(msg);
(sk, hash)
};
b.iter_with_setup(rand_factors, |(sk, hash)| sk.sign_g2(hash));
});
}
/// Benchmarks deriving a child secret key
fn derive_child(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("SecretKey");
group.bench_function("sign_g2", |b| {
let rand_factors = || {
let sk = SecretKey::random();
let mut msg = [0u8; 1000];
rng.fill_bytes(&mut msg);
let hash = hash_g2(msg);
(sk, hash)
};
b.iter_with_setup(rand_factors, |(sk, hash)| sk.sign_g2(hash));
});
}
/// Benchmarks decrypting a 1000 byte message
fn decrypt(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("SecretKey");
group.bench_function("decrypt", |b| {
let rand_factors = || {
let sk = SecretKey::random();
let pk = sk.public_key();
let mut msg = [0u8; 1000];
rng.fill_bytes(&mut msg);
let ct = pk.encrypt(msg);
(sk, ct)
};
b.iter_with_setup(rand_factors, |(sk, ct)| sk.decrypt(&ct));
});
}
criterion_group! {
name = secret_key_benches;
config = Criterion::default();
targets = sign, sign_g2, derive_child, decrypt,
}
}
mod ciphertext_benches {
use super::*;
use blsttc::SecretKey;
use rand::RngCore;
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
/// Benchmarks verify of ciphertext
fn verify(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("Ciphertext");
group.bench_function("verify", |b| {
let rand_factors = || {
let sk = SecretKey::random();
let pk = sk.public_key();
let mut msg = [0u8; 1000];
rng.fill_bytes(&mut msg);
pk.encrypt(msg)
};
b.iter_with_setup(rand_factors, |ct| ct.verify());
});
}
criterion_group! {
name = ciphertext_benches;
config = Criterion::default();
targets = verify,
}
}
mod lib_benches {
use super::*;
use blsttc::{hash_g2, Fr};
use rand::RngCore;
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
/// Benchmarks hashing a message to a point in G2
fn bench_hash_g2(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("lib");
group.bench_function("hash_g2", |b| {
let rand_factors = || {
let mut msg = [0u8; 1000];
rng.fill_bytes(&mut msg);
msg
};
b.iter_with_setup(rand_factors, hash_g2);
});
}
/// Benchmarks generating a random fr
fn bench_fr_random(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let mut group = c.benchmark_group("lib");
group.bench_function("fr_random", |b| {
b.iter(|| Fr::random(&mut rng));
});
}
criterion_group! {
name = lib_benches;
config = Criterion::default();
targets = bench_hash_g2, bench_fr_random,
}
}
criterion_main!(
poly_benches::poly_benches,
commitment_benches::commitment_benches,
bivarpoly_benches::bivarpoly_benches,
bivarcommitment_benches::bivarcommitment_benches,
secret_key_benches::secret_key_benches,
public_key_benches::public_key_benches,
ciphertext_benches::ciphertext_benches,
public_key_set_benches::public_key_set_benches,
lib_benches::lib_benches,
);