-
Notifications
You must be signed in to change notification settings - Fork 31
/
prio3.rs
1398 lines (1242 loc) · 49.9 KB
/
prio3.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MPL-2.0
//! Implementation of the Prio3 VDAF [[draft-irtf-cfrg-vdaf-03]].
//!
//! **WARNING:** Neither this code nor the cryptographic construction it implements has undergone
//! significant security analysis. Use at your own risk.
//!
//! Prio3 is based on the Prio system desigend by Dan Boneh and Henry Corrigan-Gibbs and presented
//! at NSDI 2017 [[CGB17]]. However, it incorporates a few techniques from Boneh et al., CRYPTO
//! 2019 [[BBCG+19]], that lead to substantial improvements in terms of run time and communication
//! cost.
//!
//! Prio3 is a transformation of a Fully Linear Proof (FLP) system [[draft-irtf-cfrg-vdaf-03]] into
//! a VDAF. The base type, [`Prio3`], supports a wide variety of aggregation functions, some of
//! which are instantiated here:
//!
//! - [`Prio3Aes128Count`] for aggregating a counter (*)
//! - [`Prio3Aes128CountVec`] for aggregating a vector of counters
//! - [`Prio3Aes128Sum`] for copmputing the sum of integers (*)
//! - [`Prio3Aes128Histogram`] for estimating a distribution via a histogram (*)
//!
//! Additional types can be constructed from [`Prio3`] as needed.
//!
//! (*) denotes that the type is specified in [[draft-irtf-cfrg-vdaf-03]].
//!
//! [BBCG+19]: https://ia.cr/2019/188
//! [CGB17]: https://crypto.stanford.edu/prio/
//! [draft-irtf-cfrg-vdaf-03]: https://datatracker.ietf.org/doc/draft-irtf-cfrg-vdaf/03/
#[cfg(feature = "crypto-dependencies")]
use super::prg::PrgAes128;
use super::{DST_LEN, VERSION};
use crate::codec::{CodecError, Decode, Encode, ParameterizedDecode};
use crate::field::FieldElement;
#[cfg(feature = "crypto-dependencies")]
use crate::field::{Field128, Field64};
#[cfg(feature = "multithreaded")]
use crate::flp::gadgets::ParallelSumMultithreaded;
#[cfg(feature = "crypto-dependencies")]
use crate::flp::gadgets::{BlindPolyEval, ParallelSum, PolyEval};
#[cfg(feature = "crypto-dependencies")]
use crate::flp::types::fixedpoint_l2::compatible_float::CompatibleFloat;
#[cfg(feature = "crypto-dependencies")]
use crate::flp::types::fixedpoint_l2::FixedPointBoundedL2VecSum;
#[cfg(feature = "crypto-dependencies")]
use crate::flp::types::{Average, Count, CountVec, Histogram, Sum};
use crate::flp::Type;
use crate::prng::Prng;
use crate::vdaf::prg::{Prg, RandSource, Seed};
use crate::vdaf::{
Aggregatable, AggregateShare, Aggregator, Client, Collector, OutputShare, PrepareTransition,
Share, ShareDecodingParameter, Vdaf, VdafError,
};
#[cfg(feature = "crypto-dependencies")]
use fixed::traits::Fixed;
use std::convert::TryFrom;
use std::fmt::Debug;
use std::io::Cursor;
use std::iter::IntoIterator;
use std::marker::PhantomData;
/// The count type. Each measurement is an integer in `[0,2)` and the aggregate result is the sum.
#[cfg(feature = "crypto-dependencies")]
pub type Prio3Aes128Count = Prio3<Count<Field64>, PrgAes128, 16>;
#[cfg(feature = "crypto-dependencies")]
impl Prio3Aes128Count {
/// Construct an instance of Prio3Aes128Count with the given number of aggregators.
pub fn new_aes128_count(num_aggregators: u8) -> Result<Self, VdafError> {
Prio3::new(num_aggregators, Count::new())
}
}
/// The count-vector type. Each measurement is a vector of integers in `[0,2)` and the aggregate is
/// the element-wise sum.
#[cfg(feature = "crypto-dependencies")]
pub type Prio3Aes128CountVec =
Prio3<CountVec<Field128, ParallelSum<Field128, BlindPolyEval<Field128>>>, PrgAes128, 16>;
#[cfg(feature = "crypto-dependencies")]
impl Prio3Aes128CountVec {
/// Construct an instance of Prio3Aes1238CountVec with the given number of aggregators. `len`
/// defines the length of each measurement.
pub fn new_aes128_count_vec(num_aggregators: u8, len: usize) -> Result<Self, VdafError> {
Prio3::new(num_aggregators, CountVec::new(len))
}
}
/// Like [`Prio3Aes128CountVec`] except this type uses multithreading to improve sharding and
/// preparation time. Note that the improvement is only noticeable for very large input lengths,
/// e.g., 201 and up. (Your system's mileage may vary.)
#[cfg(feature = "multithreaded")]
#[cfg(feature = "crypto-dependencies")]
#[cfg_attr(docsrs, doc(cfg(feature = "multithreaded")))]
pub type Prio3Aes128CountVecMultithreaded = Prio3<
CountVec<Field128, ParallelSumMultithreaded<Field128, BlindPolyEval<Field128>>>,
PrgAes128,
16,
>;
#[cfg(feature = "multithreaded")]
#[cfg(feature = "crypto-dependencies")]
#[cfg_attr(docsrs, doc(cfg(feature = "multithreaded")))]
impl Prio3Aes128CountVecMultithreaded {
/// Construct an instance of Prio3Aes1238CountVecMultithreaded with the given number of
/// aggregators. `len` defines the length of each measurement.
pub fn new_aes128_count_vec_multithreaded(
num_aggregators: u8,
len: usize,
) -> Result<Self, VdafError> {
Prio3::new(num_aggregators, CountVec::new(len))
}
}
/// The sum type. Each measurement is an integer in `[0,2^bits)` for some `0 < bits < 64` and the
/// aggregate is the sum.
#[cfg(feature = "crypto-dependencies")]
pub type Prio3Aes128Sum = Prio3<Sum<Field128>, PrgAes128, 16>;
#[cfg(feature = "crypto-dependencies")]
impl Prio3Aes128Sum {
/// Construct an instance of Prio3Aes128Sum with the given number of aggregators and required
/// bit length. The bit length must not exceed 64.
pub fn new_aes128_sum(num_aggregators: u8, bits: u32) -> Result<Self, VdafError> {
if bits > 64 {
return Err(VdafError::Uncategorized(format!(
"bit length ({}) exceeds limit for aggregate type (64)",
bits
)));
}
Prio3::new(num_aggregators, Sum::new(bits as usize)?)
}
}
/// The fixed point vector sum type. Each measurement is a vector of fixed point numbers
/// and the aggregate is the sum represented as 64-bit floats. The preparation phase
/// ensures the L2 norm of the input vector is < 1.
///
/// This is useful for aggregating gradients in a federated version of
/// [gradient descent](https://en.wikipedia.org/wiki/Gradient_descent) with
/// [differential privacy](https://en.wikipedia.org/wiki/Differential_privacy),
/// useful, e.g., for [differentially private deep learning](https://arxiv.org/pdf/1607.00133.pdf).
/// The bound on input norms is required for differential privacy. The fixed point representation
/// allows an easy conversion to the integer type used in internal computation, while leaving
/// conversion to the client. The model itself will have floating point parameters, so the output
/// sum has that type as well.
#[cfg(feature = "crypto-dependencies")]
pub type Prio3Aes128FixedPointBoundedL2VecSum<Fx> = Prio3<
FixedPointBoundedL2VecSum<
Fx,
Field128,
ParallelSum<Field128, PolyEval<Field128>>,
ParallelSum<Field128, BlindPolyEval<Field128>>,
>,
PrgAes128,
16,
>;
#[cfg(feature = "crypto-dependencies")]
impl<Fx: Fixed + CompatibleFloat<Field128>> Prio3Aes128FixedPointBoundedL2VecSum<Fx> {
/// Construct an instance of this VDAF with the given number of aggregators and number of
/// vector entries.
pub fn new_aes128_fixedpoint_boundedl2_vec_sum(
num_aggregators: u8,
entries: usize,
) -> Result<Self, VdafError> {
check_num_aggregators(num_aggregators)?;
Prio3::new(num_aggregators, FixedPointBoundedL2VecSum::new(entries)?)
}
}
/// The fixed point vector sum type. Each measurement is a vector of fixed point numbers
/// and the aggregate is the sum represented as 64-bit floats. The verification function
/// ensures the L2 norm of the input vector is < 1.
#[cfg(feature = "multithreaded")]
#[cfg(feature = "crypto-dependencies")]
#[cfg_attr(docsrs, doc(cfg(feature = "multithreaded")))]
pub type Prio3Aes128FixedPointBoundedL2VecSumMultithreaded<Fx> = Prio3<
FixedPointBoundedL2VecSum<
Fx,
Field128,
ParallelSumMultithreaded<Field128, PolyEval<Field128>>,
ParallelSumMultithreaded<Field128, BlindPolyEval<Field128>>,
>,
PrgAes128,
16,
>;
#[cfg(feature = "multithreaded")]
#[cfg(feature = "crypto-dependencies")]
#[cfg_attr(docsrs, doc(cfg(feature = "multithreaded")))]
impl<Fx: Fixed + CompatibleFloat<Field128>> Prio3Aes128FixedPointBoundedL2VecSumMultithreaded<Fx> {
/// Construct an instance of this VDAF with the given number of aggregators and number of
/// vector entries.
pub fn new_aes128_fixedpoint_boundedl2_vec_sum_multithreaded(
num_aggregators: u8,
entries: usize,
) -> Result<Self, VdafError> {
check_num_aggregators(num_aggregators)?;
Prio3::new(num_aggregators, FixedPointBoundedL2VecSum::new(entries)?)
}
}
/// The histogram type. Each measurement is an unsigned integer and the result is a histogram
/// representation of the distribution. The bucket boundaries are fixed in advance.
#[cfg(feature = "crypto-dependencies")]
pub type Prio3Aes128Histogram = Prio3<Histogram<Field128>, PrgAes128, 16>;
#[cfg(feature = "crypto-dependencies")]
impl Prio3Aes128Histogram {
/// Constructs an instance of Prio3Aes128Histogram with the given number of aggregators and
/// desired histogram bucket boundaries.
pub fn new_aes128_histogram(num_aggregators: u8, buckets: &[u64]) -> Result<Self, VdafError> {
let buckets = buckets.iter().map(|bucket| *bucket as u128).collect();
Prio3::new(num_aggregators, Histogram::new(buckets)?)
}
}
/// The average type. Each measurement is an integer in `[0,2^bits)` for some `0 < bits < 64` and
/// the aggregate is the arithmetic average.
#[cfg(feature = "crypto-dependencies")]
pub type Prio3Aes128Average = Prio3<Average<Field128>, PrgAes128, 16>;
#[cfg(feature = "crypto-dependencies")]
impl Prio3Aes128Average {
/// Construct an instance of Prio3Aes128Average with the given number of aggregators and
/// required bit length. The bit length must not exceed 64.
pub fn new_aes128_average(num_aggregators: u8, bits: u32) -> Result<Self, VdafError> {
check_num_aggregators(num_aggregators)?;
if bits > 64 {
return Err(VdafError::Uncategorized(format!(
"bit length ({}) exceeds limit for aggregate type (64)",
bits
)));
}
Ok(Prio3 {
num_aggregators,
typ: Average::new(bits as usize)?,
phantom: PhantomData,
})
}
}
/// The base type for Prio3.
///
/// An instance of Prio3 is determined by:
///
/// - a [`Type`](crate::flp::Type) that defines the set of valid input measurements; and
/// - a [`Prg`](crate::vdaf::prg::Prg) for deriving vectors of field elements from seeds.
///
/// New instances can be defined by aliasing the base type. For example, [`Prio3Aes128Count`] is an
/// alias for `Prio3<Count<Field64>, PrgAes128, 16>`.
///
/// ```
/// use prio::vdaf::{
/// Aggregator, Client, Collector, PrepareTransition,
/// prio3::Prio3,
/// };
/// use rand::prelude::*;
///
/// let num_shares = 2;
/// let vdaf = Prio3::new_aes128_count(num_shares).unwrap();
///
/// let mut out_shares = vec![vec![]; num_shares.into()];
/// let mut rng = thread_rng();
/// let verify_key = rng.gen();
/// let measurements = [0, 1, 1, 1, 0];
/// for measurement in measurements {
/// // Shard
/// let (public_share, input_shares) = vdaf.shard(&measurement).unwrap();
/// let mut nonce = [0; 16];
/// rng.fill(&mut nonce);
///
/// // Prepare
/// let mut prep_states = vec![];
/// let mut prep_shares = vec![];
/// for (agg_id, input_share) in input_shares.iter().enumerate() {
/// let (state, share) = vdaf.prepare_init(
/// &verify_key,
/// agg_id,
/// &(),
/// &nonce,
/// &public_share,
/// input_share
/// ).unwrap();
/// prep_states.push(state);
/// prep_shares.push(share);
/// }
/// let prep_msg = vdaf.prepare_preprocess(prep_shares).unwrap();
///
/// for (agg_id, state) in prep_states.into_iter().enumerate() {
/// let out_share = match vdaf.prepare_step(state, prep_msg.clone()).unwrap() {
/// PrepareTransition::Finish(out_share) => out_share,
/// _ => panic!("unexpected transition"),
/// };
/// out_shares[agg_id].push(out_share);
/// }
/// }
///
/// // Aggregate
/// let agg_shares = out_shares.into_iter()
/// .map(|o| vdaf.aggregate(&(), o).unwrap());
///
/// // Unshard
/// let agg_res = vdaf.unshard(&(), agg_shares, measurements.len()).unwrap();
/// assert_eq!(agg_res, 3);
/// ```
///
/// [draft-irtf-cfrg-vdaf-03]: https://datatracker.ietf.org/doc/draft-irtf-cfrg-vdaf/03/
#[derive(Clone, Debug)]
pub struct Prio3<T, P, const L: usize>
where
T: Type,
P: Prg<L>,
{
num_aggregators: u8,
typ: T,
phantom: PhantomData<P>,
}
impl<T, P, const L: usize> Prio3<T, P, L>
where
T: Type,
P: Prg<L>,
{
/// Construct an instance of this Prio3 VDAF with the given number of aggregators and the
/// underlying type.
pub fn new(num_aggregators: u8, typ: T) -> Result<Self, VdafError> {
check_num_aggregators(num_aggregators)?;
Ok(Self {
num_aggregators,
typ,
phantom: PhantomData,
})
}
/// The output length of the underlying FLP.
pub fn output_len(&self) -> usize {
self.typ.output_len()
}
/// The verifier length of the underlying FLP.
pub fn verifier_len(&self) -> usize {
self.typ.verifier_len()
}
fn derive_joint_randomness<'a>(parts: impl Iterator<Item = &'a Seed<L>>) -> Seed<L> {
let mut info = [0; VERSION.len() + 5];
info[..VERSION.len()].copy_from_slice(VERSION);
info[VERSION.len()..VERSION.len() + 4].copy_from_slice(&Self::ID.to_be_bytes());
info[VERSION.len() + 4] = 255;
let mut deriver = P::init(&[0; L]);
deriver.update(&info);
for part in parts {
deriver.update(part.as_ref());
}
deriver.into_seed()
}
fn shard_with_rand_source(
&self,
measurement: &T::Measurement,
rand_source: RandSource,
) -> Result<Vec<Prio3InputShare<T::Field, L>>, VdafError> {
let mut info = [0; DST_LEN + 1];
info[..VERSION.len()].copy_from_slice(VERSION);
info[VERSION.len()..DST_LEN].copy_from_slice(&Self::ID.to_be_bytes());
let num_aggregators = self.num_aggregators;
let input = self.typ.encode_measurement(measurement)?;
// Generate the input shares and compute the joint randomness.
let mut helper_shares = Vec::with_capacity(num_aggregators as usize - 1);
let mut helper_joint_rand_parts = if self.typ.joint_rand_len() > 0 {
Some(Vec::with_capacity(num_aggregators as usize - 1))
} else {
None
};
let mut leader_input_share = input.clone();
for agg_id in 1..num_aggregators {
let helper = HelperShare::from_rand_source(rand_source)?;
let mut deriver = P::init(helper.joint_rand_param.blind.as_ref());
info[DST_LEN] = agg_id;
deriver.update(&info);
let prng: Prng<T::Field, _> =
Prng::from_seed_stream(P::seed_stream(&helper.input_share, &info));
for (x, y) in leader_input_share
.iter_mut()
.zip(prng)
.take(self.typ.input_len())
{
*x -= y;
deriver.update(&y.into());
}
if let Some(helper_joint_rand_parts) = helper_joint_rand_parts.as_mut() {
helper_joint_rand_parts.push(deriver.into_seed());
}
helper_shares.push(helper);
}
let leader_blind = Seed::from_rand_source(rand_source)?;
info[DST_LEN] = 0; // ID of the leader
let mut deriver = P::init(leader_blind.as_ref());
deriver.update(&info);
for x in leader_input_share.iter() {
deriver.update(&(*x).into());
}
let leader_joint_rand_seed_part = deriver.into_seed();
// Compute the joint randomness seed.
let joint_rand_seed = helper_joint_rand_parts.as_ref().map(|parts| {
Self::derive_joint_randomness(
std::iter::once(&leader_joint_rand_seed_part).chain(parts.iter()),
)
});
// Run the proof-generation algorithm.
let domain_separation_tag = &info[..DST_LEN];
let joint_rand: Vec<T::Field> = joint_rand_seed
.map(|joint_rand_seed| {
let prng: Prng<T::Field, _> =
Prng::from_seed_stream(P::seed_stream(&joint_rand_seed, domain_separation_tag));
prng.take(self.typ.joint_rand_len()).collect()
})
.unwrap_or_default();
let prng: Prng<T::Field, _> = Prng::from_seed_stream(P::seed_stream(
&Seed::from_rand_source(rand_source)?,
domain_separation_tag,
));
let prove_rand: Vec<T::Field> = prng.take(self.typ.prove_rand_len()).collect();
let mut leader_proof_share = self.typ.prove(&input, &prove_rand, &joint_rand)?;
// Generate the proof shares and distribute the joint randomness seed hints.
for (j, helper) in helper_shares.iter_mut().enumerate() {
info[DST_LEN] = j as u8 + 1;
let prng: Prng<T::Field, _> =
Prng::from_seed_stream(P::seed_stream(&helper.proof_share, &info));
for (x, y) in leader_proof_share
.iter_mut()
.zip(prng)
.take(self.typ.proof_len())
{
*x -= y;
}
if let Some(helper_joint_rand_parts) = helper_joint_rand_parts.as_ref() {
let mut hint = Vec::with_capacity(num_aggregators as usize - 1);
hint.push(leader_joint_rand_seed_part.clone());
hint.extend(helper_joint_rand_parts[..j].iter().cloned());
hint.extend(helper_joint_rand_parts[j + 1..].iter().cloned());
helper.joint_rand_param.seed_hint = hint;
}
}
let leader_joint_rand_param = if self.typ.joint_rand_len() > 0 {
Some(JointRandParam {
seed_hint: helper_joint_rand_parts.unwrap_or_default(),
blind: leader_blind,
})
} else {
None
};
// Prep the output messages.
let mut out = Vec::with_capacity(num_aggregators as usize);
out.push(Prio3InputShare {
input_share: Share::Leader(leader_input_share),
proof_share: Share::Leader(leader_proof_share),
joint_rand_param: leader_joint_rand_param,
});
for helper in helper_shares.into_iter() {
let helper_joint_rand_param = if self.typ.joint_rand_len() > 0 {
Some(helper.joint_rand_param)
} else {
None
};
out.push(Prio3InputShare {
input_share: Share::Helper(helper.input_share),
proof_share: Share::Helper(helper.proof_share),
joint_rand_param: helper_joint_rand_param,
});
}
Ok(out)
}
/// Shard measurement with constant randomness of repeated bytes.
/// This method is not secure. It is used for running test vectors for Prio3.
#[cfg(feature = "test-util")]
pub fn test_vec_shard(
&self,
measurement: &T::Measurement,
) -> Result<Vec<Prio3InputShare<T::Field, L>>, VdafError> {
self.shard_with_rand_source(measurement, |buf| {
buf.fill(1);
Ok(())
})
}
fn role_try_from(&self, agg_id: usize) -> Result<u8, VdafError> {
if agg_id >= self.num_aggregators as usize {
return Err(VdafError::Uncategorized("unexpected aggregator id".into()));
}
Ok(u8::try_from(agg_id).unwrap())
}
}
impl<T, P, const L: usize> Vdaf for Prio3<T, P, L>
where
T: Type,
P: Prg<L>,
{
const ID: u32 = T::ID;
type Measurement = T::Measurement;
type AggregateResult = T::AggregateResult;
type AggregationParam = ();
type PublicShare = ();
type InputShare = Prio3InputShare<T::Field, L>;
type OutputShare = OutputShare<T::Field>;
type AggregateShare = AggregateShare<T::Field>;
fn num_aggregators(&self) -> usize {
self.num_aggregators as usize
}
}
/// Message sent by the [`Client`](crate::vdaf::Client) to each
/// [`Aggregator`](crate::vdaf::Aggregator) during the Sharding phase.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Prio3InputShare<F, const L: usize> {
/// The input share.
input_share: Share<F, L>,
/// The proof share.
proof_share: Share<F, L>,
/// Parameters used by the Aggregator to compute the joint randomness. This field is optional
/// because not every [`Type`](`crate::flp::Type`) requires joint randomness.
joint_rand_param: Option<JointRandParam<L>>,
}
impl<F: FieldElement, const L: usize> Encode for Prio3InputShare<F, L> {
fn encode(&self, bytes: &mut Vec<u8>) {
if matches!(
(&self.input_share, &self.proof_share),
(Share::Leader(_), Share::Helper(_)) | (Share::Helper(_), Share::Leader(_))
) {
panic!("tried to encode input share with ambiguous encoding")
}
self.input_share.encode(bytes);
self.proof_share.encode(bytes);
if let Some(ref param) = self.joint_rand_param {
param.blind.encode(bytes);
for part in param.seed_hint.iter() {
part.encode(bytes);
}
}
}
}
impl<'a, T, P, const L: usize> ParameterizedDecode<(&'a Prio3<T, P, L>, usize)>
for Prio3InputShare<T::Field, L>
where
T: Type,
P: Prg<L>,
{
fn decode_with_param(
(prio3, agg_id): &(&'a Prio3<T, P, L>, usize),
bytes: &mut Cursor<&[u8]>,
) -> Result<Self, CodecError> {
let agg_id = prio3
.role_try_from(*agg_id)
.map_err(|e| CodecError::Other(Box::new(e)))?;
let (input_decoder, proof_decoder) = if agg_id == 0 {
(
ShareDecodingParameter::Leader(prio3.typ.input_len()),
ShareDecodingParameter::Leader(prio3.typ.proof_len()),
)
} else {
(
ShareDecodingParameter::Helper,
ShareDecodingParameter::Helper,
)
};
let input_share = Share::decode_with_param(&input_decoder, bytes)?;
let proof_share = Share::decode_with_param(&proof_decoder, bytes)?;
let joint_rand_param = if prio3.typ.joint_rand_len() > 0 {
let num_aggregators = prio3.num_aggregators();
let blind = Seed::decode(bytes)?;
let seed_hint = std::iter::repeat_with(|| Seed::decode(bytes))
.take(num_aggregators - 1)
.collect::<Result<Vec<_>, _>>()?;
Some(JointRandParam { blind, seed_hint })
} else {
None
};
Ok(Prio3InputShare {
input_share,
proof_share,
joint_rand_param,
})
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
/// Message broadcast by each [`Aggregator`](crate::vdaf::Aggregator) in each round of the
/// Preparation phase.
pub struct Prio3PrepareShare<F, const L: usize> {
/// A share of the FLP verifier message. (See [`Type`](crate::flp::Type).)
verifier: Vec<F>,
/// A part of the joint randomness seed.
joint_rand_part: Option<Seed<L>>,
}
impl<F: FieldElement, const L: usize> Encode for Prio3PrepareShare<F, L> {
fn encode(&self, bytes: &mut Vec<u8>) {
for x in &self.verifier {
x.encode(bytes);
}
if let Some(ref seed) = self.joint_rand_part {
seed.encode(bytes);
}
}
}
impl<F: FieldElement, const L: usize> ParameterizedDecode<Prio3PrepareState<F, L>>
for Prio3PrepareShare<F, L>
{
fn decode_with_param(
decoding_parameter: &Prio3PrepareState<F, L>,
bytes: &mut Cursor<&[u8]>,
) -> Result<Self, CodecError> {
let mut verifier = Vec::with_capacity(decoding_parameter.verifier_len);
for _ in 0..decoding_parameter.verifier_len {
verifier.push(F::decode(bytes)?);
}
let joint_rand_part = if decoding_parameter.joint_rand_seed.is_some() {
Some(Seed::decode(bytes)?)
} else {
None
};
Ok(Prio3PrepareShare {
verifier,
joint_rand_part,
})
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
/// Result of combining a round of [`Prio3PrepareShare`] messages.
pub struct Prio3PrepareMessage<const L: usize> {
/// The joint randomness seed computed by the Aggregators.
joint_rand_seed: Option<Seed<L>>,
}
impl<const L: usize> Encode for Prio3PrepareMessage<L> {
fn encode(&self, bytes: &mut Vec<u8>) {
if let Some(ref seed) = self.joint_rand_seed {
seed.encode(bytes);
}
}
}
impl<F: FieldElement, const L: usize> ParameterizedDecode<Prio3PrepareState<F, L>>
for Prio3PrepareMessage<L>
{
fn decode_with_param(
decoding_parameter: &Prio3PrepareState<F, L>,
bytes: &mut Cursor<&[u8]>,
) -> Result<Self, CodecError> {
let joint_rand_seed = if decoding_parameter.joint_rand_seed.is_some() {
Some(Seed::decode(bytes)?)
} else {
None
};
Ok(Prio3PrepareMessage { joint_rand_seed })
}
}
impl<T, P, const L: usize> Client for Prio3<T, P, L>
where
T: Type,
P: Prg<L>,
{
#[allow(clippy::type_complexity)]
fn shard(
&self,
measurement: &T::Measurement,
) -> Result<((), Vec<Prio3InputShare<T::Field, L>>), VdafError> {
self.shard_with_rand_source(measurement, getrandom::getrandom)
.map(|input_shares| ((), input_shares))
}
}
/// State of each [`Aggregator`](crate::vdaf::Aggregator) during the Preparation phase.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Prio3PrepareState<F, const L: usize> {
input_share: Share<F, L>,
joint_rand_seed: Option<Seed<L>>,
agg_id: u8,
verifier_len: usize,
}
impl<F: FieldElement, const L: usize> Encode for Prio3PrepareState<F, L> {
/// Append the encoded form of this object to the end of `bytes`, growing the vector as needed.
fn encode(&self, bytes: &mut Vec<u8>) {
self.input_share.encode(bytes);
if let Some(ref seed) = self.joint_rand_seed {
seed.encode(bytes);
}
}
}
impl<'a, T, P, const L: usize> ParameterizedDecode<(&'a Prio3<T, P, L>, usize)>
for Prio3PrepareState<T::Field, L>
where
T: Type,
P: Prg<L>,
{
fn decode_with_param(
(prio3, agg_id): &(&'a Prio3<T, P, L>, usize),
bytes: &mut Cursor<&[u8]>,
) -> Result<Self, CodecError> {
let agg_id = prio3
.role_try_from(*agg_id)
.map_err(|e| CodecError::Other(Box::new(e)))?;
let share_decoder = if agg_id == 0 {
ShareDecodingParameter::Leader(prio3.typ.input_len())
} else {
ShareDecodingParameter::Helper
};
let input_share = Share::decode_with_param(&share_decoder, bytes)?;
let joint_rand_seed = if prio3.typ.joint_rand_len() > 0 {
Some(Seed::decode(bytes)?)
} else {
None
};
Ok(Self {
input_share,
joint_rand_seed,
agg_id,
verifier_len: prio3.typ.verifier_len(),
})
}
}
impl<T, P, const L: usize> Aggregator<L> for Prio3<T, P, L>
where
T: Type,
P: Prg<L>,
{
type PrepareState = Prio3PrepareState<T::Field, L>;
type PrepareShare = Prio3PrepareShare<T::Field, L>;
type PrepareMessage = Prio3PrepareMessage<L>;
/// Begins the Prep process with the other aggregators. The result of this process is
/// the aggregator's output share.
#[allow(clippy::type_complexity)]
fn prepare_init(
&self,
verify_key: &[u8; L],
agg_id: usize,
_agg_param: &(),
nonce: &[u8],
_public_share: &(),
msg: &Prio3InputShare<T::Field, L>,
) -> Result<
(
Prio3PrepareState<T::Field, L>,
Prio3PrepareShare<T::Field, L>,
),
VdafError,
> {
let agg_id = self.role_try_from(agg_id)?;
let mut info = [0; DST_LEN + 1];
info[..VERSION.len()].copy_from_slice(VERSION);
info[VERSION.len()..DST_LEN].copy_from_slice(&Self::ID.to_be_bytes());
info[DST_LEN] = agg_id;
let domain_separation_tag = &info[..DST_LEN];
let mut deriver = P::init(verify_key);
deriver.update(domain_separation_tag);
deriver.update(&[255]);
deriver.update(nonce);
let query_rand_prng = Prng::from_seed_stream(deriver.into_seed_stream());
// Create a reference to the (expanded) input share.
let expanded_input_share: Option<Vec<T::Field>> = match msg.input_share {
Share::Leader(_) => None,
Share::Helper(ref seed) => {
let prng = Prng::from_seed_stream(P::seed_stream(seed, &info));
Some(prng.take(self.typ.input_len()).collect())
}
};
let input_share = match msg.input_share {
Share::Leader(ref data) => data,
Share::Helper(_) => expanded_input_share.as_ref().unwrap(),
};
// Create a reference to the (expanded) proof share.
let expanded_proof_share: Option<Vec<T::Field>> = match msg.proof_share {
Share::Leader(_) => None,
Share::Helper(ref seed) => {
let prng = Prng::from_seed_stream(P::seed_stream(seed, &info));
Some(prng.take(self.typ.proof_len()).collect())
}
};
let proof_share = match msg.proof_share {
Share::Leader(ref data) => data,
Share::Helper(_) => expanded_proof_share.as_ref().unwrap(),
};
// Compute the joint randomness.
let (joint_rand_seed, joint_rand_seed_part, joint_rand) = if self.typ.joint_rand_len() > 0 {
let mut deriver = P::init(msg.joint_rand_param.as_ref().unwrap().blind.as_ref());
deriver.update(&info);
for x in input_share {
deriver.update(&(*x).into());
}
let joint_rand_seed_part = deriver.into_seed();
let hints = &msg.joint_rand_param.as_ref().unwrap().seed_hint;
let joint_rand_seed = Self::derive_joint_randomness(
hints[..agg_id as usize]
.iter()
.chain(std::iter::once(&joint_rand_seed_part))
.chain(hints[agg_id as usize..].iter()),
);
let prng: Prng<T::Field, _> =
Prng::from_seed_stream(P::seed_stream(&joint_rand_seed, domain_separation_tag));
(
Some(joint_rand_seed),
Some(joint_rand_seed_part),
prng.take(self.typ.joint_rand_len()).collect(),
)
} else {
(None, None, Vec::new())
};
// Compute the query randomness.
let query_rand: Vec<T::Field> = query_rand_prng.take(self.typ.query_rand_len()).collect();
// Run the query-generation algorithm.
let verifier_share = self.typ.query(
input_share,
proof_share,
&query_rand,
&joint_rand,
self.num_aggregators as usize,
)?;
Ok((
Prio3PrepareState {
input_share: msg.input_share.clone(),
joint_rand_seed,
agg_id,
verifier_len: verifier_share.len(),
},
Prio3PrepareShare {
verifier: verifier_share,
joint_rand_part: joint_rand_seed_part,
},
))
}
fn prepare_preprocess<M: IntoIterator<Item = Prio3PrepareShare<T::Field, L>>>(
&self,
inputs: M,
) -> Result<Prio3PrepareMessage<L>, VdafError> {
let mut verifier = vec![T::Field::zero(); self.typ.verifier_len()];
let mut joint_rand_parts = Vec::with_capacity(self.num_aggregators());
let mut count = 0;
for share in inputs.into_iter() {
count += 1;
if share.verifier.len() != verifier.len() {
return Err(VdafError::Uncategorized(format!(
"unexpected verifier share length: got {}; want {}",
share.verifier.len(),
verifier.len(),
)));
}
if self.typ.joint_rand_len() > 0 {
let joint_rand_seed_part = share.joint_rand_part.unwrap();
joint_rand_parts.push(joint_rand_seed_part);
}
for (x, y) in verifier.iter_mut().zip(share.verifier) {
*x += y;
}
}
if count != self.num_aggregators {
return Err(VdafError::Uncategorized(format!(
"unexpected message count: got {}; want {}",
count, self.num_aggregators,
)));
}
// Check the proof verifier.
match self.typ.decide(&verifier) {
Ok(true) => (),
Ok(false) => {
return Err(VdafError::Uncategorized(
"proof verifier check failed".into(),
))
}
Err(err) => return Err(VdafError::from(err)),
};
let joint_rand_seed = if self.typ.joint_rand_len() > 0 {
Some(Self::derive_joint_randomness(joint_rand_parts.iter()))
} else {
None
};
Ok(Prio3PrepareMessage { joint_rand_seed })
}
fn prepare_step(
&self,
step: Prio3PrepareState<T::Field, L>,
msg: Prio3PrepareMessage<L>,
) -> Result<PrepareTransition<Self, L>, VdafError> {
if self.typ.joint_rand_len() > 0 {
// Check that the joint randomness was correct.
if step.joint_rand_seed.as_ref().unwrap() != msg.joint_rand_seed.as_ref().unwrap() {
return Err(VdafError::Uncategorized(
"joint randomness mismatch".to_string(),
));
}
}
// Compute the output share.
let input_share = match step.input_share {
Share::Leader(data) => data,
Share::Helper(seed) => {
let mut info = [0; DST_LEN + 1];
info[..VERSION.len()].copy_from_slice(VERSION);
info[VERSION.len()..DST_LEN].copy_from_slice(&Self::ID.to_be_bytes());
info[DST_LEN] = step.agg_id;
let prng = Prng::from_seed_stream(P::seed_stream(&seed, &info));
prng.take(self.typ.input_len()).collect()
}
};
let output_share = match self.typ.truncate(input_share) {
Ok(data) => OutputShare(data),
Err(err) => {
return Err(VdafError::from(err));
}
};
Ok(PrepareTransition::Finish(output_share))
}
/// Aggregates a sequence of output shares into an aggregate share.
fn aggregate<It: IntoIterator<Item = OutputShare<T::Field>>>(
&self,
_agg_param: &(),
output_shares: It,
) -> Result<AggregateShare<T::Field>, VdafError> {
let mut agg_share = AggregateShare(vec![T::Field::zero(); self.typ.output_len()]);
for output_share in output_shares.into_iter() {
agg_share.accumulate(&output_share)?;
}
Ok(agg_share)
}
}
impl<T, P, const L: usize> Collector for Prio3<T, P, L>
where
T: Type,
P: Prg<L>,
{
/// Combines aggregate shares into the aggregate result.
fn unshard<It: IntoIterator<Item = AggregateShare<T::Field>>>(