-
Notifications
You must be signed in to change notification settings - Fork 14
/
task.rs
685 lines (616 loc) · 29.6 KB
/
task.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
use base64::{engine::general_purpose::STANDARD, Engine};
use rand::{distributions::Standard, prelude::Distribution};
use reqwest::Url;
use ring::constant_time;
use serde::{Deserialize, Serialize};
use std::str;
/// HTTP header where auth tokens are provided in messages between participants.
pub const DAP_AUTH_HEADER: &str = "DAP-Auth-Token";
/// The length of the verify key parameter for Prio3 VDAF instantiations.
pub const PRIO3_VERIFY_KEY_LENGTH: usize = 16;
/// Identifiers for supported VDAFs, corresponding to definitions in
/// [draft-irtf-cfrg-vdaf-03][1] and implementations in [`prio::vdaf::prio3`].
///
/// [1]: https://datatracker.ietf.org/doc/draft-irtf-cfrg-vdaf/03/
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[non_exhaustive]
pub enum VdafInstance {
/// A `Prio3` counter.
Prio3Count,
/// A vector of `Prio3` counters.
Prio3CountVec { length: usize },
/// A `Prio3` sum.
Prio3Sum { bits: usize },
/// A vector of `Prio3` sums.
Prio3SumVec { bits: usize, length: usize },
/// A `Prio3` histogram.
Prio3Histogram { buckets: Vec<u64> },
/// A `Prio3` 16-bit fixed point vector sum with bounded L2 norm.
#[cfg(feature = "fpvec_bounded_l2")]
Prio3FixedPoint16BitBoundedL2VecSum { length: usize },
/// A `Prio3` 32-bit fixed point vector sum with bounded L2 norm.
#[cfg(feature = "fpvec_bounded_l2")]
Prio3FixedPoint32BitBoundedL2VecSum { length: usize },
/// A `Prio3` 64-bit fixedpoint vector sum with bounded L2 norm.
#[cfg(feature = "fpvec_bounded_l2")]
Prio3FixedPoint64BitBoundedL2VecSum { length: usize },
/// The `poplar1` VDAF. Support for this VDAF is experimental.
Poplar1 { bits: usize },
/// A fake, no-op VDAF.
#[cfg(feature = "test-util")]
#[cfg_attr(docsrs, doc(cfg(feature = "test-util")))]
Fake,
/// A fake, no-op VDAF that always fails during initialization of input preparation.
#[cfg(feature = "test-util")]
#[cfg_attr(docsrs, doc(cfg(feature = "test-util")))]
FakeFailsPrepInit,
/// A fake, no-op VDAF that always fails when stepping input preparation.
#[cfg(feature = "test-util")]
#[cfg_attr(docsrs, doc(cfg(feature = "test-util")))]
FakeFailsPrepStep,
}
impl VdafInstance {
/// Returns the expected length of a VDAF verification key for a VDAF of this type.
pub fn verify_key_length(&self) -> usize {
match self {
#[cfg(feature = "test-util")]
VdafInstance::Fake
| VdafInstance::FakeFailsPrepInit
| VdafInstance::FakeFailsPrepStep => 0,
// All "real" VDAFs use a verify key of length 16 currently. (Poplar1 may not, but it's
// not yet done being specified, so choosing 16 bytes is fine for testing.)
_ => PRIO3_VERIFY_KEY_LENGTH,
}
}
}
/// Internal implementation details of [`vdaf_dispatch`](crate::vdaf_dispatch).
#[macro_export]
macro_rules! vdaf_dispatch_impl_base {
// Provide the dispatched type only, don't construct a VDAF instance.
(impl match base $vdaf_instance:expr, (_, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3Count => {
type $Vdaf = ::prio::vdaf::prio3::Prio3Count;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3CountVec { length } => {
type $Vdaf = ::prio::vdaf::prio3::Prio3SumVecMultithreaded;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3Sum { bits } => {
type $Vdaf = ::prio::vdaf::prio3::Prio3Sum;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3SumVec { bits, length } => {
type $Vdaf = ::prio::vdaf::prio3::Prio3SumVecMultithreaded;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3Histogram { buckets } => {
type $Vdaf = ::prio::vdaf::prio3::Prio3Histogram;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
_ => unreachable!(),
}
};
// Construct a VDAF instance, and provide that to the block as well.
(impl match base $vdaf_instance:expr, ($vdaf:ident, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3Count => {
let $vdaf = ::prio::vdaf::prio3::Prio3::new_count(2)?;
type $Vdaf = ::prio::vdaf::prio3::Prio3Count;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3CountVec { length } => {
// Prio3CountVec is implemented as a 1-bit sum vec
let $vdaf = ::prio::vdaf::prio3::Prio3::new_sum_vec_multithreaded(2, 1, *length)?;
type $Vdaf = ::prio::vdaf::prio3::Prio3SumVecMultithreaded;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3Sum { bits } => {
let $vdaf = ::prio::vdaf::prio3::Prio3::new_sum(2, *bits)?;
type $Vdaf = ::prio::vdaf::prio3::Prio3Sum;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3SumVec { bits, length } => {
let $vdaf =
::prio::vdaf::prio3::Prio3::new_sum_vec_multithreaded(2, *bits, *length)?;
type $Vdaf = ::prio::vdaf::prio3::Prio3SumVecMultithreaded;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3Histogram { buckets } => {
let $vdaf = ::prio::vdaf::prio3::Prio3::new_histogram(2, buckets)?;
type $Vdaf = ::prio::vdaf::prio3::Prio3Histogram;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
_ => unreachable!(),
}
};
}
/// Internal implementation details of [`vdaf_dispatch`](crate::vdaf_dispatch).
#[cfg(feature = "fpvec_bounded_l2")]
#[macro_export]
macro_rules! vdaf_dispatch_impl_fpvec_bounded_l2 {
// Provide the dispatched type only, don't construct a VDAF instance.
(impl match fpvec_bounded_l2 $vdaf_instance:expr, (_, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3FixedPoint16BitBoundedL2VecSum { length } => {
type $Vdaf = ::prio::vdaf::prio3::Prio3FixedPointBoundedL2VecSumMultithreaded<
::fixed::FixedI16<::fixed::types::extra::U15>,
>;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3FixedPoint32BitBoundedL2VecSum { length } => {
type $Vdaf = ::prio::vdaf::prio3::Prio3FixedPointBoundedL2VecSumMultithreaded<
::fixed::FixedI32<::fixed::types::extra::U31>,
>;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3FixedPoint64BitBoundedL2VecSum { length } => {
type $Vdaf = ::prio::vdaf::prio3::Prio3FixedPointBoundedL2VecSumMultithreaded<
::fixed::FixedI64<::fixed::types::extra::U63>,
>;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
_ => unreachable!(),
}
};
// Construct a VDAF instance, and provide that to the block as well.
(impl match fpvec_bounded_l2 $vdaf_instance:expr, ($vdaf:ident, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3FixedPoint16BitBoundedL2VecSum { length } => {
let $vdaf =
::prio::vdaf::prio3::Prio3::new_fixedpoint_boundedl2_vec_sum_multithreaded(
2, *length,
)?;
type $Vdaf = ::prio::vdaf::prio3::Prio3FixedPointBoundedL2VecSumMultithreaded<
::fixed::FixedI16<::fixed::types::extra::U15>,
>;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3FixedPoint32BitBoundedL2VecSum { length } => {
let $vdaf =
::prio::vdaf::prio3::Prio3::new_fixedpoint_boundedl2_vec_sum_multithreaded(
2, *length,
)?;
type $Vdaf = ::prio::vdaf::prio3::Prio3FixedPointBoundedL2VecSumMultithreaded<
::fixed::FixedI32<::fixed::types::extra::U31>,
>;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
::janus_core::task::VdafInstance::Prio3FixedPoint64BitBoundedL2VecSum { length } => {
let $vdaf =
::prio::vdaf::prio3::Prio3::new_fixedpoint_boundedl2_vec_sum_multithreaded(
2, *length,
)?;
type $Vdaf = ::prio::vdaf::prio3::Prio3FixedPointBoundedL2VecSumMultithreaded<
::fixed::FixedI64<::fixed::types::extra::U63>,
>;
const $VERIFY_KEY_LENGTH: usize = ::janus_core::task::PRIO3_VERIFY_KEY_LENGTH;
$body
}
_ => unreachable!(),
}
};
}
/// Internal implementation details of [`vdaf_dispatch`](crate::vdaf_dispatch).
#[cfg(feature = "test-util")]
#[macro_export]
macro_rules! vdaf_dispatch_impl_test_util {
// Provide the dispatched type only, don't construct a VDAF instance.
(impl match test_util $vdaf_instance:expr, (_, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Fake => {
type $Vdaf = ::janus_core::test_util::dummy_vdaf::Vdaf;
const $VERIFY_KEY_LENGTH: usize = 0;
$body
}
::janus_core::task::VdafInstance::FakeFailsPrepInit => {
type $Vdaf = ::janus_core::test_util::dummy_vdaf::Vdaf;
const $VERIFY_KEY_LENGTH: usize = 0;
$body
}
::janus_core::task::VdafInstance::FakeFailsPrepStep => {
type $Vdaf = ::janus_core::test_util::dummy_vdaf::Vdaf;
const $VERIFY_KEY_LENGTH: usize = 0;
$body
}
_ => unreachable!(),
}
};
// Construct a VDAF instance, and provide that to the block as well.
(impl match test_util $vdaf_instance:expr, ($vdaf:ident, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Fake => {
let $vdaf = ::janus_core::test_util::dummy_vdaf::Vdaf::new();
type $Vdaf = ::janus_core::test_util::dummy_vdaf::Vdaf;
const $VERIFY_KEY_LENGTH: usize = 0;
$body
}
::janus_core::task::VdafInstance::FakeFailsPrepInit => {
let $vdaf = ::janus_core::test_util::dummy_vdaf::Vdaf::new().with_prep_init_fn(
|_| -> Result<(), ::prio::vdaf::VdafError> {
::std::result::Result::Err(::prio::vdaf::VdafError::Uncategorized(
"FakeFailsPrepInit failed at prep_init".to_string(),
))
}
);
type $Vdaf = ::janus_core::test_util::dummy_vdaf::Vdaf;
const $VERIFY_KEY_LENGTH: usize = 0;
$body
}
::janus_core::task::VdafInstance::FakeFailsPrepStep => {
let $vdaf = ::janus_core::test_util::dummy_vdaf::Vdaf::new().with_prep_step_fn(
|| -> Result<
::prio::vdaf::PrepareTransition<::janus_core::test_util::dummy_vdaf::Vdaf, 0, 16>,
::prio::vdaf::VdafError,
> {
::std::result::Result::Err(::prio::vdaf::VdafError::Uncategorized(
"FakeFailsPrepStep failed at prep_step".to_string(),
))
}
);
type $Vdaf = ::janus_core::test_util::dummy_vdaf::Vdaf;
const $VERIFY_KEY_LENGTH: usize = 0;
$body
}
_ => unreachable!(),
}
};
}
/// Internal implementation details of [`vdaf_dispatch`](crate::vdaf_dispatch).
#[cfg(all(feature = "fpvec_bounded_l2", feature = "test-util"))]
#[macro_export]
macro_rules! vdaf_dispatch_impl {
// Provide the dispatched type only, don't construct a VDAF instance.
(impl match all $vdaf_instance:expr, (_, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3Count
| ::janus_core::task::VdafInstance::Prio3CountVec { .. }
| ::janus_core::task::VdafInstance::Prio3Sum { .. }
| ::janus_core::task::VdafInstance::Prio3SumVec { .. }
| ::janus_core::task::VdafInstance::Prio3Histogram { .. } => {
::janus_core::vdaf_dispatch_impl_base!(impl match base $vdaf_instance, (_, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
::janus_core::task::VdafInstance::Prio3FixedPoint16BitBoundedL2VecSum { .. }
| ::janus_core::task::VdafInstance::Prio3FixedPoint32BitBoundedL2VecSum { .. }
| ::janus_core::task::VdafInstance::Prio3FixedPoint64BitBoundedL2VecSum { .. } => {
::janus_core::vdaf_dispatch_impl_fpvec_bounded_l2!(impl match fpvec_bounded_l2 $vdaf_instance, (_, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
::janus_core::task::VdafInstance::Fake
| ::janus_core::task::VdafInstance::FakeFailsPrepInit
| ::janus_core::task::VdafInstance::FakeFailsPrepStep => {
::janus_core::vdaf_dispatch_impl_test_util!(impl match test_util $vdaf_instance, (_, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
_ => panic!("VDAF {:?} is not yet supported", $vdaf_instance),
}
};
// Construct a VDAF instance, and provide that to the block as well.
(impl match all $vdaf_instance:expr, ($vdaf:ident, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3Count
| ::janus_core::task::VdafInstance::Prio3CountVec { .. }
| ::janus_core::task::VdafInstance::Prio3Sum { .. }
| ::janus_core::task::VdafInstance::Prio3SumVec { .. }
| ::janus_core::task::VdafInstance::Prio3Histogram { .. } => {
::janus_core::vdaf_dispatch_impl_base!(impl match base $vdaf_instance, ($vdaf, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
::janus_core::task::VdafInstance::Prio3FixedPoint16BitBoundedL2VecSum { .. }
| ::janus_core::task::VdafInstance::Prio3FixedPoint32BitBoundedL2VecSum { .. }
| ::janus_core::task::VdafInstance::Prio3FixedPoint64BitBoundedL2VecSum { .. } => {
::janus_core::vdaf_dispatch_impl_fpvec_bounded_l2!(impl match fpvec_bounded_l2 $vdaf_instance, ($vdaf, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
::janus_core::task::VdafInstance::Fake
| ::janus_core::task::VdafInstance::FakeFailsPrepInit
| ::janus_core::task::VdafInstance::FakeFailsPrepStep => {
::janus_core::vdaf_dispatch_impl_test_util!(impl match test_util $vdaf_instance, ($vdaf, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
_ => panic!("VDAF {:?} is not yet supported", $vdaf_instance),
}
};
}
/// Internal implementation details of [`vdaf_dispatch`](crate::vdaf_dispatch).
#[cfg(all(feature = "fpvec_bounded_l2", not(feature = "test-util")))]
#[macro_export]
macro_rules! vdaf_dispatch_impl {
// Provide the dispatched type only, don't construct a VDAF instance.
(impl match all $vdaf_instance:expr, (_, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3Count
| ::janus_core::task::VdafInstance::Prio3CountVec { .. }
| ::janus_core::task::VdafInstance::Prio3Sum { .. }
| ::janus_core::task::VdafInstance::Prio3SumVec { .. }
| ::janus_core::task::VdafInstance::Prio3Histogram { .. } => {
::janus_core::vdaf_dispatch_impl_base!(impl match base $vdaf_instance, (_, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
::janus_core::task::VdafInstance::Prio3FixedPoint16BitBoundedL2VecSum { .. }
| ::janus_core::task::VdafInstance::Prio3FixedPoint32BitBoundedL2VecSum { .. }
| ::janus_core::task::VdafInstance::Prio3FixedPoint64BitBoundedL2VecSum { .. } => {
::janus_core::vdaf_dispatch_impl_fpvec_bounded_l2!(impl match fpvec_bounded_l2 $vdaf_instance, (_, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
_ => panic!("VDAF {:?} is not yet supported", $vdaf_instance),
}
};
// Construct a VDAF instance, and provide that to the block as well.
(impl match all $vdaf_instance:expr, ($vdaf:ident, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3Count
| ::janus_core::task::VdafInstance::Prio3CountVec { .. }
| ::janus_core::task::VdafInstance::Prio3Sum { .. }
| ::janus_core::task::VdafInstance::Prio3SumVec { .. }
| ::janus_core::task::VdafInstance::Prio3Histogram { .. } => {
::janus_core::vdaf_dispatch_impl_base!(impl match base $vdaf_instance, ($vdaf, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
::janus_core::task::VdafInstance::Prio3FixedPoint16BitBoundedL2VecSum { .. }
| ::janus_core::task::VdafInstance::Prio3FixedPoint32BitBoundedL2VecSum { .. }
| ::janus_core::task::VdafInstance::Prio3FixedPoint64BitBoundedL2VecSum { .. } => {
::janus_core::vdaf_dispatch_impl_fpvec_bounded_l2!(impl match fpvec_bounded_l2 $vdaf_instance, ($vdaf, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
_ => panic!("VDAF {:?} is not yet supported", $vdaf_instance),
}
};
}
/// Internal implementation details of [`vdaf_dispatch`](crate::vdaf_dispatch).
#[cfg(all(not(feature = "fpvec_bounded_l2"), feature = "test-util"))]
#[macro_export]
macro_rules! vdaf_dispatch_impl {
// Provide the dispatched type only, don't construct a VDAF instance.
(impl match all $vdaf_instance:expr, (_, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3Count
| ::janus_core::task::VdafInstance::Prio3CountVec { .. }
| ::janus_core::task::VdafInstance::Prio3Sum { .. }
| ::janus_core::task::VdafInstance::Prio3SumVec { .. }
| ::janus_core::task::VdafInstance::Prio3Histogram { .. } => {
::janus_core::vdaf_dispatch_impl_base!(impl match base $vdaf_instance, (_, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
::janus_core::task::VdafInstance::Fake
| ::janus_core::task::VdafInstance::FakeFailsPrepInit
| ::janus_core::task::VdafInstance::FakeFailsPrepStep => {
::janus_core::vdaf_dispatch_impl_test_util!(impl match test_util $vdaf_instance, (_, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
_ => panic!("VDAF {:?} is not yet supported", $vdaf_instance),
}
};
// Construct a VDAF instance, and provide that to the block as well.
(impl match all $vdaf_instance:expr, ($vdaf:ident, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3Count
| ::janus_core::task::VdafInstance::Prio3CountVec { .. }
| ::janus_core::task::VdafInstance::Prio3Sum { .. }
| ::janus_core::task::VdafInstance::Prio3SumVec { .. }
| ::janus_core::task::VdafInstance::Prio3Histogram { .. } => {
::janus_core::vdaf_dispatch_impl_base!(impl match base $vdaf_instance, ($vdaf, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
::janus_core::task::VdafInstance::Fake
| ::janus_core::task::VdafInstance::FakeFailsPrepInit
| ::janus_core::task::VdafInstance::FakeFailsPrepStep => {
::janus_core::vdaf_dispatch_impl_test_util!(impl match test_util $vdaf_instance, ($vdaf, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
_ => panic!("VDAF {:?} is not yet supported", $vdaf_instance),
}
};
}
/// Internal implementation details of [`vdaf_dispatch`](crate::vdaf_dispatch).
#[cfg(all(not(feature = "fpvec_bounded_l2"), not(feature = "test-util")))]
#[macro_export]
macro_rules! vdaf_dispatch_impl {
// Provide the dispatched type only, don't construct a VDAF instance.
(impl match all $vdaf_instance:expr, (_, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3Count
| ::janus_core::task::VdafInstance::Prio3CountVec { .. }
| ::janus_core::task::VdafInstance::Prio3Sum { .. }
| ::janus_core::task::VdafInstance::Prio3SumVec { .. }
| ::janus_core::task::VdafInstance::Prio3Histogram { .. } => {
::janus_core::vdaf_dispatch_impl_base!(impl match base $vdaf_instance, (_, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
_ => panic!("VDAF {:?} is not yet supported", $vdaf_instance),
}
};
// Construct a VDAF instance, and provide that to the block as well.
(impl match all $vdaf_instance:expr, ($vdaf:ident, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
match $vdaf_instance {
::janus_core::task::VdafInstance::Prio3Count
| ::janus_core::task::VdafInstance::Prio3CountVec { .. }
| ::janus_core::task::VdafInstance::Prio3Sum { .. }
| ::janus_core::task::VdafInstance::Prio3SumVec { .. }
| ::janus_core::task::VdafInstance::Prio3Histogram { .. } => {
::janus_core::vdaf_dispatch_impl_base!(impl match base $vdaf_instance, ($vdaf, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
}
_ => panic!("VDAF {:?} is not yet supported", $vdaf_instance),
}
};
}
/// Emits a match block dispatching on a [`VdafInstance`]. This must be called inside a method that
/// returns a result, with an error type that [`prio::vdaf::VdafError`] can be converted into. Takes
/// a `&VdafInstance` as the first argument, followed by a pseudo-pattern and body. The
/// pseudo-pattern takes a variable name for the constructed VDAF, a type alias name that the block
/// can use to explicitly specify the VDAF's type, and the name of a const that will be set to the
/// VDAF's verify key length, also for explicitly specifying type parameters.
///
/// # Example:
///
/// ```
/// # use janus_core::vdaf_dispatch;
/// # fn handle_request_generic<A, const SEED_SIZE: usize>(_vdaf: &A) -> Result<(), prio::vdaf::VdafError>
/// # where
/// # A: prio::vdaf::Aggregator<SEED_SIZE, 16>,
/// # {
/// # Ok(())
/// # }
/// # fn test() -> Result<(), prio::vdaf::VdafError> {
/// # let vdaf = janus_core::task::VdafInstance::Prio3Count;
/// vdaf_dispatch!(&vdaf, (vdaf, VdafType, VERIFY_KEY_LENGTH) => {
/// handle_request_generic::<VdafType, VERIFY_KEY_LENGTH>(&vdaf)
/// })
/// # }
/// ```
#[macro_export]
macro_rules! vdaf_dispatch {
// Provide the dispatched type only, don't construct a VDAF instance.
($vdaf_instance:expr, (_, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
::janus_core::vdaf_dispatch_impl!(impl match all $vdaf_instance, (_, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
};
// Construct a VDAF instance, and provide that to the block as well.
($vdaf_instance:expr, ($vdaf:ident, $Vdaf:ident, $VERIFY_KEY_LENGTH:ident) => $body:tt) => {
::janus_core::vdaf_dispatch_impl!(impl match all $vdaf_instance, ($vdaf, $Vdaf, $VERIFY_KEY_LENGTH) => $body)
};
}
/// An authentication (bearer) token used by aggregators for aggregator-to-aggregator and
/// collector-to-aggregator authentication.
#[derive(Clone)]
pub struct AuthenticationToken(Vec<u8>);
impl AuthenticationToken {
/// Constructs a bearer token string suitable for use as the value in an HTTP `Authorization`
/// header.
pub fn bearer_token(&self) -> String {
format!("Bearer {}", STANDARD.encode(self.as_ref()))
}
}
impl AsRef<[u8]> for AuthenticationToken {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl From<Vec<u8>> for AuthenticationToken {
fn from(token: Vec<u8>) -> Self {
Self(token)
}
}
impl PartialEq for AuthenticationToken {
fn eq(&self, other: &Self) -> bool {
// We attempt constant-time comparisons of the token data. Note that this function still
// leaks whether the lengths of the tokens are equal -- this is acceptable because we expect
// the content of the tokens to provide enough randomness that needs to be guessed even if
// the length is known.
constant_time::verify_slices_are_equal(&self.0, &other.0).is_ok()
}
}
impl Eq for AuthenticationToken {}
impl Distribution<AuthenticationToken> for Standard {
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> AuthenticationToken {
AuthenticationToken(Vec::from(hex::encode(rng.gen::<[u8; 16]>())))
}
}
/// Modifies a [`Url`] in place to ensure it ends with a slash.
///
/// Aggregator endpoint URLs should end with a slash if they will be used with [`Url::join`],
/// because that method will drop the last path component of the base URL if it does not end with a
/// slash.
pub fn url_ensure_trailing_slash(url: &mut Url) {
if !url.as_str().ends_with('/') {
url.set_path(&format!("{}/", url.path()));
}
}
#[cfg(test)]
mod tests {
use super::VdafInstance;
use serde_test::{assert_tokens, Token};
#[test]
fn vdaf_serialization() {
// The `Vdaf` type must have a stable serialization, as it gets stored in a JSON database
// column.
assert_tokens(
&VdafInstance::Prio3Count,
&[Token::UnitVariant {
name: "VdafInstance",
variant: "Prio3Count",
}],
);
assert_tokens(
&VdafInstance::Prio3CountVec { length: 8 },
&[
Token::StructVariant {
name: "VdafInstance",
variant: "Prio3CountVec",
len: 1,
},
Token::Str("length"),
Token::U64(8),
Token::StructVariantEnd,
],
);
assert_tokens(
&VdafInstance::Prio3Sum { bits: 64 },
&[
Token::StructVariant {
name: "VdafInstance",
variant: "Prio3Sum",
len: 1,
},
Token::Str("bits"),
Token::U64(64),
Token::StructVariantEnd,
],
);
assert_tokens(
&VdafInstance::Prio3Histogram {
buckets: Vec::from([0, 100, 200, 400]),
},
&[
Token::StructVariant {
name: "VdafInstance",
variant: "Prio3Histogram",
len: 1,
},
Token::Str("buckets"),
Token::Seq { len: Some(4) },
Token::U64(0),
Token::U64(100),
Token::U64(200),
Token::U64(400),
Token::SeqEnd,
Token::StructVariantEnd,
],
);
assert_tokens(
&VdafInstance::Poplar1 { bits: 64 },
&[
Token::StructVariant {
name: "VdafInstance",
variant: "Poplar1",
len: 1,
},
Token::Str("bits"),
Token::U64(64),
Token::StructVariantEnd,
],
);
assert_tokens(
&VdafInstance::Fake,
&[Token::UnitVariant {
name: "VdafInstance",
variant: "Fake",
}],
);
assert_tokens(
&VdafInstance::FakeFailsPrepInit,
&[Token::UnitVariant {
name: "VdafInstance",
variant: "FakeFailsPrepInit",
}],
);
assert_tokens(
&VdafInstance::FakeFailsPrepStep,
&[Token::UnitVariant {
name: "VdafInstance",
variant: "FakeFailsPrepStep",
}],
);
}
}