Debian Codegraph @

third_party/rust/prio/benches/speed_tests.rs

raw
1 // SPDX-License-Identifier: MPL-2.0
2
3 use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
4 #[cfg(feature = "experimental")]
5 use criterion::{BatchSize, Throughput};
6 #[cfg(feature = "experimental")]
7 use fixed::types::{I1F15, I1F31};
8 #[cfg(feature = "experimental")]
9 use fixed_macro::fixed;
10 #[cfg(feature = "experimental")]
11 use num_bigint::BigUint;
12 #[cfg(feature = "experimental")]
13 use num_rational::Ratio;
14 #[cfg(feature = "experimental")]
15 use num_traits::ToPrimitive;
16 #[cfg(feature = "experimental")]
17 use prio::dp::distributions::DiscreteGaussian;
18 #[cfg(feature = "experimental")]
19 use prio::idpf::test_utils::generate_zipf_distributed_batch;
20 #[cfg(feature = "experimental")]
21 use prio::vdaf::prio2::Prio2;
22 use prio::{
23 benchmarked::*,
24 field::{random_vector, Field128 as F, FieldElement},
25 flp::gadgets::Mul,
26 vdaf::{prio3::Prio3, Aggregator, Client},
27 };
28 #[cfg(feature = "experimental")]
29 use prio::{
30 field::{Field255, Field64},
31 flp::types::fixedpoint_l2::FixedPointBoundedL2VecSum,
32 idpf::{Idpf, IdpfInput, RingBufferCache},
33 vdaf::poplar1::{Poplar1, Poplar1AggregationParam, Poplar1IdpfValue},
34 };
35 #[cfg(feature = "experimental")]
36 use rand::prelude::*;
37 #[cfg(feature = "experimental")]
38 use std::iter;
39 use std::time::Duration;
40
41 /// Seed for generation of random benchmark inputs.
42 ///
43 /// A fixed RNG seed is used to generate inputs in order to minimize run-to-run variability. The
44 /// seed value may be freely changed to get a different set of inputs.
45 #[cfg(feature = "experimental")]
46 const RNG_SEED: u64 = 0;
47
48 /// Speed test for generating a seed and deriving a pseudorandom sequence of field elements.
49 fn prng(c: &mut Criterion) {
50 let mut group = c.benchmark_group("rand");
51 let test_sizes = [16, 256, 1024, 4096];
52 for size in test_sizes {
53 group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, size| {
54 b.iter(|| random_vector::<F>(*size))
55 });
56 }
57 group.finish();
58 }
59
60 /// Speed test for generating samples from the discrete gaussian distribution using different
61 /// standard deviations.
62 #[cfg(feature = "experimental")]
63 pub fn dp_noise(c: &mut Criterion) {
64 let mut group = c.benchmark_group("dp_noise");
65 let mut rng = StdRng::seed_from_u64(RNG_SEED);
66
67 let test_stds = [
68 Ratio::<BigUint>::from_integer(BigUint::from(u128::MAX)).pow(2),
69 Ratio::<BigUint>::from_integer(BigUint::from(u64::MAX)),
70 Ratio::<BigUint>::from_integer(BigUint::from(u32::MAX)),
71 Ratio::<BigUint>::from_integer(BigUint::from(5u8)),
72 Ratio::<BigUint>::new(BigUint::from(10000u32), BigUint::from(23u32)),
73 ];
74 for std in test_stds {
75 let sampler = DiscreteGaussian::new(std.clone()).unwrap();
76 group.bench_function(
77 BenchmarkId::new("discrete_gaussian", std.to_f64().unwrap_or(f64::INFINITY)),
78 |b| b.iter(|| sampler.sample(&mut rng)),
79 );
80 }
81 group.finish();
82 }
83
84 /// The asymptotic cost of polynomial multiplication is `O(n log n)` using FFT and `O(n^2)` using
85 /// the naive method. This benchmark demonstrates that the latter has better concrete performance
86 /// for small polynomials. The result is used to pick the `FFT_THRESHOLD` constant in
87 /// `src/flp/gadgets.rs`.
88 fn poly_mul(c: &mut Criterion) {
89 let test_sizes = [1_usize, 30, 60, 90, 120, 150];
90
91 let mut group = c.benchmark_group("poly_mul");
92 for size in test_sizes {
93 group.bench_with_input(BenchmarkId::new("fft", size), &size, |b, size| {
94 let m = (size + 1).next_power_of_two();
95 let mut g: Mul<F> = Mul::new(*size);
96 let mut outp = vec![F::zero(); 2 * m];
97 let inp = vec![random_vector(m).unwrap(); 2];
98
99 b.iter(|| {
100 benchmarked_gadget_mul_call_poly_fft(&mut g, &mut outp, &inp).unwrap();
101 })
102 });
103
104 group.bench_with_input(BenchmarkId::new("direct", size), &size, |b, size| {
105 let m = (size + 1).next_power_of_two();
106 let mut g: Mul<F> = Mul::new(*size);
107 let mut outp = vec![F::zero(); 2 * m];
108 let inp = vec![random_vector(m).unwrap(); 2];
109
110 b.iter(|| {
111 benchmarked_gadget_mul_call_poly_direct(&mut g, &mut outp, &inp).unwrap();
112 })
113 });
114 }
115 group.finish();
116 }
117
118 /// Benchmark prio2.
119 #[cfg(feature = "experimental")]
120 fn prio2(c: &mut Criterion) {
121 let mut group = c.benchmark_group("prio2_shard");
122 for input_length in [10, 100, 1_000] {
123 group.bench_with_input(
124 BenchmarkId::from_parameter(input_length),
125 &input_length,
126 |b, input_length| {
127 let vdaf = Prio2::new(*input_length).unwrap();
128 let measurement = (0..u32::try_from(*input_length).unwrap())
129 .map(|i| i & 1)
130 .collect::<Vec<_>>();
131 let nonce = black_box([0u8; 16]);
132 b.iter(|| vdaf.shard(&measurement, &nonce).unwrap());
133 },
134 );
135 }
136 group.finish();
137
138 let mut group = c.benchmark_group("prio2_prepare_init");
139 for input_length in [10, 100, 1_000] {
140 group.bench_with_input(
141 BenchmarkId::from_parameter(input_length),
142 &input_length,
143 |b, input_length| {
144 let vdaf = Prio2::new(*input_length).unwrap();
145 let measurement = (0..u32::try_from(*input_length).unwrap())
146 .map(|i| i & 1)
147 .collect::<Vec<_>>();
148 let nonce = black_box([0u8; 16]);
149 let verify_key = black_box([0u8; 32]);
150 let (public_share, input_shares) = vdaf.shard(&measurement, &nonce).unwrap();
151 b.iter(|| {
152 vdaf.prepare_init(&verify_key, 0, &(), &nonce, &public_share, &input_shares[0])
153 .unwrap();
154 });
155 },
156 );
157 }
158 group.finish();
159 }
160
161 /// Benchmark prio3.
162 fn prio3(c: &mut Criterion) {
163 let num_shares = 2;
164
165 c.bench_function("prio3count_shard", |b| {
166 let vdaf = Prio3::new_count(num_shares).unwrap();
167 let measurement = black_box(true);
168 let nonce = black_box([0u8; 16]);
169 b.iter(|| vdaf.shard(&measurement, &nonce).unwrap());
170 });
171
172 c.bench_function("prio3count_prepare_init", |b| {
173 let vdaf = Prio3::new_count(num_shares).unwrap();
174 let measurement = black_box(true);
175 let nonce = black_box([0u8; 16]);
176 let verify_key = black_box([0u8; 16]);
177 let (public_share, input_shares) = vdaf.shard(&measurement, &nonce).unwrap();
178 b.iter(|| {
179 vdaf.prepare_init(&verify_key, 0, &(), &nonce, &public_share, &input_shares[0])
180 .unwrap()
181 });
182 });
183
184 let mut group = c.benchmark_group("prio3sum_shard");
185 for bits in [8, 32] {
186 group.bench_with_input(BenchmarkId::from_parameter(bits), &bits, |b, bits| {
187 let vdaf = Prio3::new_sum(num_shares, *bits).unwrap();
188 let measurement = (1 << bits) - 1;
189 let nonce = black_box([0u8; 16]);
190 b.iter(|| vdaf.shard(&measurement, &nonce).unwrap());
191 });
192 }
193 group.finish();
194
195 let mut group = c.benchmark_group("prio3sum_prepare_init");
196 for bits in [8, 32] {
197 group.bench_with_input(BenchmarkId::from_parameter(bits), &bits, |b, bits| {
198 let vdaf = Prio3::new_sum(num_shares, *bits).unwrap();
199 let measurement = (1 << bits) - 1;
200 let nonce = black_box([0u8; 16]);
201 let verify_key = black_box([0u8; 16]);
202 let (public_share, input_shares) = vdaf.shard(&measurement, &nonce).unwrap();
203 b.iter(|| {
204 vdaf.prepare_init(&verify_key, 0, &(), &nonce, &public_share, &input_shares[0])
205 .unwrap()
206 });
207 });
208 }
209 group.finish();
210
211 let mut group = c.benchmark_group("prio3sumvec_shard");
212 for (input_length, chunk_length) in [(10, 3), (100, 10), (1_000, 31)] {
213 group.bench_with_input(
214 BenchmarkId::new("serial", input_length),
215 &(input_length, chunk_length),
216 |b, (input_length, chunk_length)| {
217 let vdaf = Prio3::new_sum_vec(num_shares, 1, *input_length, *chunk_length).unwrap();
218 let measurement = (0..u128::try_from(*input_length).unwrap())
219 .map(|i| i & 1)
220 .collect::<Vec<_>>();
221 let nonce = black_box([0u8; 16]);
222 b.iter(|| vdaf.shard(&measurement, &nonce).unwrap());
223 },
224 );
225 }
226
227 #[cfg(feature = "multithreaded")]
228 {
229 for (input_length, chunk_length) in [(10, 3), (100, 10), (1_000, 31)] {
230 group.bench_with_input(
231 BenchmarkId::new("parallel", input_length),
232 &(input_length, chunk_length),
233 |b, (input_length, chunk_length)| {
234 let vdaf = Prio3::new_sum_vec_multithreaded(
235 num_shares,
236 1,
237 *input_length,
238 *chunk_length,
239 )
240 .unwrap();
241 let measurement = (0..u128::try_from(*input_length).unwrap())
242 .map(|i| i & 1)
243 .collect::<Vec<_>>();
244 let nonce = black_box([0u8; 16]);
245 b.iter(|| vdaf.shard(&measurement, &nonce).unwrap());
246 },
247 );
248 }
249 }
250 group.finish();
251
252 let mut group = c.benchmark_group("prio3sumvec_prepare_init");
253 for (input_length, chunk_length) in [(10, 3), (100, 10), (1_000, 31)] {
254 group.bench_with_input(
255 BenchmarkId::new("serial", input_length),
256 &(input_length, chunk_length),
257 |b, (input_length, chunk_length)| {
258 let vdaf = Prio3::new_sum_vec(num_shares, 1, *input_length, *chunk_length).unwrap();
259 let measurement = (0..u128::try_from(*input_length).unwrap())
260 .map(|i| i & 1)
261 .collect::<Vec<_>>();
262 let nonce = black_box([0u8; 16]);
263 let verify_key = black_box([0u8; 16]);
264 let (public_share, input_shares) = vdaf.shard(&measurement, &nonce).unwrap();
265 b.iter(|| {
266 vdaf.prepare_init(&verify_key, 0, &(), &nonce, &public_share, &input_shares[0])
267 .unwrap()
268 });
269 },
270 );
271 }
272
273 #[cfg(feature = "multithreaded")]
274 {
275 for (input_length, chunk_length) in [(10, 3), (100, 10), (1_000, 31)] {
276 group.bench_with_input(
277 BenchmarkId::new("parallel", input_length),
278 &(input_length, chunk_length),
279 |b, (input_length, chunk_length)| {
280 let vdaf = Prio3::new_sum_vec_multithreaded(
281 num_shares,
282 1,
283 *input_length,
284 *chunk_length,
285 )
286 .unwrap();
287 let measurement = (0..u128::try_from(*input_length).unwrap())
288 .map(|i| i & 1)
289 .collect::<Vec<_>>();
290 let nonce = black_box([0u8; 16]);
291 let verify_key = black_box([0u8; 16]);
292 let (public_share, input_shares) = vdaf.shard(&measurement, &nonce).unwrap();
293 b.iter(|| {
294 vdaf.prepare_init(
295 &verify_key,
296 0,
297 &(),
298 &nonce,
299 &public_share,
300 &input_shares[0],
301 )
302 .unwrap()
303 });
304 },
305 );
306 }
307 }
308 group.finish();
309
310 let mut group = c.benchmark_group("prio3histogram_shard");
311 for (input_length, chunk_length) in [
312 (10, 3),
313 (100, 10),
314 (1_000, 31),
315 (10_000, 100),
316 (100_000, 316),
317 ] {
318 if input_length >= 100_000 {
319 group.measurement_time(Duration::from_secs(15));
320 }
321 group.bench_with_input(
322 BenchmarkId::new("serial", input_length),
323 &(input_length, chunk_length),
324 |b, (input_length, chunk_length)| {
325 let vdaf = Prio3::new_histogram(num_shares, *input_length, *chunk_length).unwrap();
326 let measurement = black_box(0);
327 let nonce = black_box([0u8; 16]);
328 b.iter(|| vdaf.shard(&measurement, &nonce).unwrap());
329 },
330 );
331 }
332
333 #[cfg(feature = "multithreaded")]
334 {
335 for (input_length, chunk_length) in [
336 (10, 3),
337 (100, 10),
338 (1_000, 31),
339 (10_000, 100),
340 (100_000, 316),
341 ] {
342 if input_length >= 100_000 {
343 group.measurement_time(Duration::from_secs(15));
344 }
345 group.bench_with_input(
346 BenchmarkId::new("parallel", input_length),
347 &(input_length, chunk_length),
348 |b, (input_length, chunk_length)| {
349 let vdaf = Prio3::new_histogram_multithreaded(
350 num_shares,
351 *input_length,
352 *chunk_length,
353 )
354 .unwrap();
355 let measurement = black_box(0);
356 let nonce = black_box([0u8; 16]);
357 b.iter(|| vdaf.shard(&measurement, &nonce).unwrap());
358 },
359 );
360 }
361 }
362 group.finish();
363
364 let mut group = c.benchmark_group("prio3histogram_prepare_init");
365 for (input_length, chunk_length) in [
366 (10, 3),
367 (100, 10),
368 (1_000, 31),
369 (10_000, 100),
370 (100_000, 316),
371 ] {
372 if input_length >= 100_000 {
373 group.measurement_time(Duration::from_secs(15));
374 }
375 group.bench_with_input(
376 BenchmarkId::new("serial", input_length),
377 &(input_length, chunk_length),
378 |b, (input_length, chunk_length)| {
379 let vdaf = Prio3::new_histogram(num_shares, *input_length, *chunk_length).unwrap();
380 let measurement = black_box(0);
381 let nonce = black_box([0u8; 16]);
382 let verify_key = black_box([0u8; 16]);
383 let (public_share, input_shares) = vdaf.shard(&measurement, &nonce).unwrap();
384 b.iter(|| {
385 vdaf.prepare_init(&verify_key, 0, &(), &nonce, &public_share, &input_shares[0])
386 .unwrap()
387 });
388 },
389 );
390 }
391
392 #[cfg(feature = "multithreaded")]
393 {
394 for (input_length, chunk_length) in [
395 (10, 3),
396 (100, 10),
397 (1_000, 31),
398 (10_000, 100),
399 (100_000, 316),
400 ] {
401 if input_length >= 100_000 {
402 group.measurement_time(Duration::from_secs(15));
403 }
404 group.bench_with_input(
405 BenchmarkId::new("parallel", input_length),
406 &(input_length, chunk_length),
407 |b, (input_length, chunk_length)| {
408 let vdaf = Prio3::new_histogram_multithreaded(
409 num_shares,
410 *input_length,
411 *chunk_length,
412 )
413 .unwrap();
414 let measurement = black_box(0);
415 let nonce = black_box([0u8; 16]);
416 let verify_key = black_box([0u8; 16]);
417 let (public_share, input_shares) = vdaf.shard(&measurement, &nonce).unwrap();
418 b.iter(|| {
419 vdaf.prepare_init(
420 &verify_key,
421 0,
422 &(),
423 &nonce,
424 &public_share,
425 &input_shares[0],
426 )
427 .unwrap()
428 });
429 },
430 );
431 }
432 }
433 group.finish();
434
435 #[cfg(feature = "experimental")]
436 {
437 let mut group = c.benchmark_group("prio3fixedpointboundedl2vecsum_i1f15_shard");
438 for dimension in [10, 100, 1_000] {
439 group.bench_with_input(
440 BenchmarkId::new("serial", dimension),
441 &dimension,
442 |b, dimension| {
443 let vdaf: Prio3<FixedPointBoundedL2VecSum<I1F15, _, _>, _, 16> =
444 Prio3::new_fixedpoint_boundedl2_vec_sum(num_shares, *dimension).unwrap();
445 let mut measurement = vec![fixed!(0: I1F15); *dimension];
446 measurement[0] = fixed!(0.5: I1F15);
447 let nonce = black_box([0u8; 16]);
448 b.iter(|| vdaf.shard(&measurement, &nonce).unwrap());
449 },
450 );
451 }
452
453 #[cfg(feature = "multithreaded")]
454 {
455 for dimension in [10, 100, 1_000] {
456 group.bench_with_input(
457 BenchmarkId::new("parallel", dimension),
458 &dimension,
459 |b, dimension| {
460 let vdaf: Prio3<FixedPointBoundedL2VecSum<I1F15, _, _>, _, 16> =
461 Prio3::new_fixedpoint_boundedl2_vec_sum_multithreaded(
462 num_shares, *dimension,
463 )
464 .unwrap();
465 let mut measurement = vec![fixed!(0: I1F15); *dimension];
466 measurement[0] = fixed!(0.5: I1F15);
467 let nonce = black_box([0u8; 16]);
468 b.iter(|| vdaf.shard(&measurement, &nonce).unwrap());
469 },
470 );
471 }
472 }
473 group.finish();
474
475 let mut group = c.benchmark_group("prio3fixedpointboundedl2vecsum_i1f15_prepare_init");
476 for dimension in [10, 100, 1_000] {
477 group.bench_with_input(
478 BenchmarkId::new("series", dimension),
479 &dimension,
480 |b, dimension| {
481 let vdaf: Prio3<FixedPointBoundedL2VecSum<I1F15, _, _>, _, 16> =
482 Prio3::new_fixedpoint_boundedl2_vec_sum(num_shares, *dimension).unwrap();
483 let mut measurement = vec![fixed!(0: I1F15); *dimension];
484 measurement[0] = fixed!(0.5: I1F15);
485 let nonce = black_box([0u8; 16]);
486 let verify_key = black_box([0u8; 16]);
487 let (public_share, input_shares) = vdaf.shard(&measurement, &nonce).unwrap();
488 b.iter(|| {
489 vdaf.prepare_init(
490 &verify_key,
491 0,
492 &(),
493 &nonce,
494 &public_share,
495 &input_shares[0],
496 )
497 .unwrap()
498 });
499 },
500 );
501 }
502
503 #[cfg(feature = "multithreaded")]
504 {
505 for dimension in [10, 100, 1_000] {
506 group.bench_with_input(
507 BenchmarkId::new("parallel", dimension),
508 &dimension,
509 |b, dimension| {
510 let vdaf: Prio3<FixedPointBoundedL2VecSum<I1F15, _, _>, _, 16> =
511 Prio3::new_fixedpoint_boundedl2_vec_sum_multithreaded(
512 num_shares, *dimension,
513 )
514 .unwrap();
515 let mut measurement = vec![fixed!(0: I1F15); *dimension];
516 measurement[0] = fixed!(0.5: I1F15);
517 let nonce = black_box([0u8; 16]);
518 let verify_key = black_box([0u8; 16]);
519 let (public_share, input_shares) =
520 vdaf.shard(&measurement, &nonce).unwrap();
521 b.iter(|| {
522 vdaf.prepare_init(
523 &verify_key,
524 0,
525 &(),
526 &nonce,
527 &public_share,
528 &input_shares[0],
529 )
530 .unwrap()
531 });
532 },
533 );
534 }
535 }
536 group.finish();
537
538 let mut group = c.benchmark_group("prio3fixedpointboundedl2vecsum_i1f31_shard");
539 for dimension in [10, 100, 1_000] {
540 group.bench_with_input(
541 BenchmarkId::new("serial", dimension),
542 &dimension,
543 |b, dimension| {
544 let vdaf: Prio3<FixedPointBoundedL2VecSum<I1F31, _, _>, _, 16> =
545 Prio3::new_fixedpoint_boundedl2_vec_sum(num_shares, *dimension).unwrap();
546 let mut measurement = vec![fixed!(0: I1F31); *dimension];
547 measurement[0] = fixed!(0.5: I1F31);
548 let nonce = black_box([0u8; 16]);
549 b.iter(|| vdaf.shard(&measurement, &nonce).unwrap());
550 },
551 );
552 }
553
554 #[cfg(feature = "multithreaded")]
555 {
556 for dimension in [10, 100, 1_000] {
557 group.bench_with_input(
558 BenchmarkId::new("parallel", dimension),
559 &dimension,
560 |b, dimension| {
561 let vdaf: Prio3<FixedPointBoundedL2VecSum<I1F31, _, _>, _, 16> =
562 Prio3::new_fixedpoint_boundedl2_vec_sum_multithreaded(
563 num_shares, *dimension,
564 )
565 .unwrap();
566 let mut measurement = vec![fixed!(0: I1F31); *dimension];
567 measurement[0] = fixed!(0.5: I1F31);
568 let nonce = black_box([0u8; 16]);
569 b.iter(|| vdaf.shard(&measurement, &nonce).unwrap());
570 },
571 );
572 }
573 }
574 group.finish();
575
576 let mut group = c.benchmark_group("prio3fixedpointboundedl2vecsum_i1f31_prepare_init");
577 for dimension in [10, 100, 1_000] {
578 group.bench_with_input(
579 BenchmarkId::new("series", dimension),
580 &dimension,
581 |b, dimension| {
582 let vdaf: Prio3<FixedPointBoundedL2VecSum<I1F31, _, _>, _, 16> =
583 Prio3::new_fixedpoint_boundedl2_vec_sum(num_shares, *dimension).unwrap();
584 let mut measurement = vec![fixed!(0: I1F31); *dimension];
585 measurement[0] = fixed!(0.5: I1F31);
586 let nonce = black_box([0u8; 16]);
587 let verify_key = black_box([0u8; 16]);
588 let (public_share, input_shares) = vdaf.shard(&measurement, &nonce).unwrap();
589 b.iter(|| {
590 vdaf.prepare_init(
591 &verify_key,
592 0,
593 &(),
594 &nonce,
595 &public_share,
596 &input_shares[0],
597 )
598 .unwrap()
599 });
600 },
601 );
602 }
603
604 #[cfg(feature = "multithreaded")]
605 {
606 for dimension in [10, 100, 1_000] {
607 group.bench_with_input(
608 BenchmarkId::new("parallel", dimension),
609 &dimension,
610 |b, dimension| {
611 let vdaf: Prio3<FixedPointBoundedL2VecSum<I1F31, _, _>, _, 16> =
612 Prio3::new_fixedpoint_boundedl2_vec_sum_multithreaded(
613 num_shares, *dimension,
614 )
615 .unwrap();
616 let mut measurement = vec![fixed!(0: I1F31); *dimension];
617 measurement[0] = fixed!(0.5: I1F31);
618 let nonce = black_box([0u8; 16]);
619 let verify_key = black_box([0u8; 16]);
620 let (public_share, input_shares) =
621 vdaf.shard(&measurement, &nonce).unwrap();
622 b.iter(|| {
623 vdaf.prepare_init(
624 &verify_key,
625 0,
626 &(),
627 &nonce,
628 &public_share,
629 &input_shares[0],
630 )
631 .unwrap()
632 });
633 },
634 );
635 }
636 }
637 group.finish();
638 }
639 }
640
641 /// Benchmark IdpfPoplar performance.
642 #[cfg(feature = "experimental")]
643 fn idpf(c: &mut Criterion) {
644 let test_sizes = [8usize, 8 * 16, 8 * 256];
645
646 let mut group = c.benchmark_group("idpf_gen");
647 for size in test_sizes.iter() {
648 group.throughput(Throughput::Bytes(*size as u64 / 8));
649 group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
650 let bits = iter::repeat_with(random).take(size).collect::<Vec<bool>>();
651 let input = IdpfInput::from_bools(&bits);
652
653 let inner_values = random_vector::<Field64>(size - 1)
654 .unwrap()
655 .into_iter()
656 .map(|random_element| Poplar1IdpfValue::new([Field64::one(), random_element]))
657 .collect::<Vec<_>>();
658 let leaf_value = Poplar1IdpfValue::new([Field255::one(), random_vector(1).unwrap()[0]]);
659
660 let idpf = Idpf::new((), ());
661 b.iter(|| {
662 idpf.gen(&input, inner_values.clone(), leaf_value, &[0; 16])
663 .unwrap();
664 });
665 });
666 }
667 group.finish();
668
669 let mut group = c.benchmark_group("idpf_eval");
670 for size in test_sizes.iter() {
671 group.throughput(Throughput::Bytes(*size as u64 / 8));
672 group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
673 let bits = iter::repeat_with(random).take(size).collect::<Vec<bool>>();
674 let input = IdpfInput::from_bools(&bits);
675
676 let inner_values = random_vector::<Field64>(size - 1)
677 .unwrap()
678 .into_iter()
679 .map(|random_element| Poplar1IdpfValue::new([Field64::one(), random_element]))
680 .collect::<Vec<_>>();
681 let leaf_value = Poplar1IdpfValue::new([Field255::one(), random_vector(1).unwrap()[0]]);
682
683 let idpf = Idpf::new((), ());
684 let (public_share, keys) = idpf
685 .gen(&input, inner_values, leaf_value, &[0; 16])
686 .unwrap();
687
688 b.iter(|| {
689 // This is an aggressively small cache, to minimize its impact on the benchmark.
690 // In this synthetic benchmark, we are only checking one candidate prefix per level
691 // (typically there are many candidate prefixes per level) so the cache hit rate
692 // will be unaffected.
693 let mut cache = RingBufferCache::new(1);
694
695 for prefix_length in 1..=size {
696 let prefix = input[..prefix_length].to_owned().into();
697 idpf.eval(0, &public_share, &keys[0], &prefix, &[0; 16], &mut cache)
698 .unwrap();
699 }
700 });
701 });
702 }
703 group.finish();
704 }
705
706 /// Benchmark Poplar1.
707 #[cfg(feature = "experimental")]
708 fn poplar1(c: &mut Criterion) {
709 let test_sizes = [16_usize, 128, 256];
710
711 let mut group = c.benchmark_group("poplar1_shard");
712 for size in test_sizes.iter() {
713 group.throughput(Throughput::Bytes(*size as u64 / 8));
714 group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
715 let vdaf = Poplar1::new_turboshake128(size);
716 let mut rng = StdRng::seed_from_u64(RNG_SEED);
717 let nonce = rng.gen::<[u8; 16]>();
718
719 b.iter_batched(
720 || {
721 let bits = iter::repeat_with(|| rng.gen())
722 .take(size)
723 .collect::<Vec<bool>>();
724 IdpfInput::from_bools(&bits)
725 },
726 |measurement| {
727 vdaf.shard(&measurement, &nonce).unwrap();
728 },
729 BatchSize::SmallInput,
730 );
731 });
732 }
733 group.finish();
734
735 let mut group = c.benchmark_group("poplar1_prepare_init");
736 for size in test_sizes.iter() {
737 group.measurement_time(Duration::from_secs(30)); // slower benchmark
738 group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
739 let vdaf = Poplar1::new_turboshake128(size);
740 let mut rng = StdRng::seed_from_u64(RNG_SEED);
741
742 b.iter_batched(
743 || {
744 let verify_key: [u8; 16] = rng.gen();
745 let nonce: [u8; 16] = rng.gen();
746
747 // Parameters are chosen to match Chris Wood's experimental setup:
748 // https://github.com/chris-wood/heavy-hitter-comparison
749 let (measurements, prefix_tree) = generate_zipf_distributed_batch(
750 &mut rng, // rng
751 size, // bits
752 10, // threshold
753 1000, // number of measurements
754 128, // Zipf support
755 1.03, // Zipf exponent
756 );
757
758 // We are benchmarking preparation of a single report. For this test, it doesn't matter
759 // which measurement we generate a report for, so pick the first measurement
760 // arbitrarily.
761 let (public_share, input_shares) =
762 vdaf.shard(&measurements[0], &nonce).unwrap();
763
764 // For the aggregation paramter, we use the candidate prefixes from the prefix tree
765 // for the sampled measurements. Run preparation for the last step, which ought to
766 // represent the worst-case performance.
767 let agg_param =
768 Poplar1AggregationParam::try_from_prefixes(prefix_tree[size - 1].clone())
769 .unwrap();
770
771 (
772 verify_key,
773 nonce,
774 agg_param,
775 public_share,
776 input_shares.into_iter().next().unwrap(),
777 )
778 },
779 |(verify_key, nonce, agg_param, public_share, input_share)| {
780 vdaf.prepare_init(
781 &verify_key,
782 0,
783 &agg_param,
784 &nonce,
785 &public_share,
786 &input_share,
787 )
788 .unwrap();
789 },
790 BatchSize::SmallInput,
791 );
792 });
793 }
794 group.finish();
795 }
796
797 /// Benchmark VIDPF performance.
798 #[cfg(feature = "experimental")]
799 fn vidpf(c: &mut Criterion) {
800 use prio::vidpf::{Vidpf, VidpfInput, VidpfWeight};
801
802 let test_sizes = [8usize, 8 * 16, 8 * 256];
803 const NONCE_SIZE: usize = 16;
804 const NONCE: &[u8; NONCE_SIZE] = b"Test Nonce VIDPF";
805
806 let mut group = c.benchmark_group("vidpf_gen");
807 for size in test_sizes.iter() {
808 group.throughput(Throughput::Bytes(*size as u64 / 8));
809 group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
810 let bits = iter::repeat_with(random).take(size).collect::<Vec<bool>>();
811 let input = VidpfInput::from_bools(&bits);
812 let weight = VidpfWeight::from(vec![Field255::one(), Field255::one()]);
813
814 let vidpf = Vidpf::<VidpfWeight<Field255>, NONCE_SIZE>::new(2);
815
816 b.iter(|| {
817 let _ = vidpf.gen(&input, &weight, NONCE).unwrap();
818 });
819 });
820 }
821 group.finish();
822
823 let mut group = c.benchmark_group("vidpf_eval");
824 for size in test_sizes.iter() {
825 group.throughput(Throughput::Bytes(*size as u64 / 8));
826 group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
827 let bits = iter::repeat_with(random).take(size).collect::<Vec<bool>>();
828 let input = VidpfInput::from_bools(&bits);
829 let weight = VidpfWeight::from(vec![Field255::one(), Field255::one()]);
830 let vidpf = Vidpf::<VidpfWeight<Field255>, NONCE_SIZE>::new(2);
831
832 let (public, keys) = vidpf.gen(&input, &weight, NONCE).unwrap();
833
834 b.iter(|| {
835 let _ = vidpf.eval(&keys[0], &public, &input, NONCE).unwrap();
836 });
837 });
838 }
839 group.finish();
840 }
841
842 #[cfg(feature = "experimental")]
843 criterion_group!(benches, poplar1, prio3, prio2, poly_mul, prng, idpf, dp_noise, vidpf);
844 #[cfg(not(feature = "experimental"))]
845 criterion_group!(benches, prio3, prng, poly_mul);
846
847 criterion_main!(benches);