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
|
// SPDX-License-Identifier: MPL-2.0
//! Implementation of a dummy VDAF which conforms to the specification in [draft-irtf-cfrg-vdaf-06]
//! but does nothing. Useful for testing.
//!
//! [draft-irtf-cfrg-vdaf-06]: https://datatracker.ietf.org/doc/draft-irtf-cfrg-vdaf/06/
use crate::{
codec::{CodecError, Decode, Encode},
vdaf::{self, Aggregatable, PrepareTransition, VdafError},
};
use rand::random;
use std::{fmt::Debug, io::Cursor, sync::Arc};
/// The Dummy VDAF does summation modulus 256 so we can predict aggregation results.
const MODULUS: u64 = u8::MAX as u64 + 1;
type ArcPrepInitFn =
Arc<dyn Fn(&AggregationParam) -> Result<(), VdafError> + 'static + Send + Sync>;
type ArcPrepStepFn = Arc<
dyn Fn(&PrepareState) -> Result<PrepareTransition<Vdaf, 0, 16>, VdafError>
+ 'static
+ Send
+ Sync,
>;
/// Dummy VDAF that does nothing.
#[derive(Clone)]
pub struct Vdaf {
prep_init_fn: ArcPrepInitFn,
prep_step_fn: ArcPrepStepFn,
}
impl Debug for Vdaf {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Vdaf")
.field("prep_init_fn", &"[redacted]")
.field("prep_step_fn", &"[redacted]")
.finish()
}
}
impl Vdaf {
/// The length of the verify key parameter for fake VDAF instantiations.
pub const VERIFY_KEY_LEN: usize = 0;
/// Construct a new instance of the dummy VDAF.
pub fn new(rounds: u32) -> Self {
Self {
prep_init_fn: Arc::new(|_| -> Result<(), VdafError> { Ok(()) }),
prep_step_fn: Arc::new(
move |state| -> Result<PrepareTransition<Self, 0, 16>, VdafError> {
let new_round = state.current_round + 1;
if new_round == rounds {
Ok(PrepareTransition::Finish(OutputShare(u64::from(
state.input_share,
))))
} else {
Ok(PrepareTransition::Continue(
PrepareState {
current_round: new_round,
..*state
},
(),
))
}
},
),
}
}
/// Provide an alternate implementation of [`vdaf::Aggregator::prepare_init`].
pub fn with_prep_init_fn<F: Fn(&AggregationParam) -> Result<(), VdafError>>(
mut self,
f: F,
) -> Self
where
F: 'static + Send + Sync,
{
self.prep_init_fn = Arc::new(f);
self
}
/// Provide an alternate implementation of [`vdaf::Aggregator::prepare_next`].
pub fn with_prep_step_fn<
F: Fn(&PrepareState) -> Result<PrepareTransition<Self, 0, 16>, VdafError>,
>(
mut self,
f: F,
) -> Self
where
F: 'static + Send + Sync,
{
self.prep_step_fn = Arc::new(f);
self
}
}
impl Default for Vdaf {
fn default() -> Self {
Self::new(1)
}
}
impl vdaf::Vdaf for Vdaf {
type Measurement = u8;
type AggregateResult = u64;
type AggregationParam = AggregationParam;
type PublicShare = ();
type InputShare = InputShare;
type OutputShare = OutputShare;
type AggregateShare = AggregateShare;
fn algorithm_id(&self) -> u32 {
0xFFFF0000
}
fn num_aggregators(&self) -> usize {
2
}
}
impl vdaf::Aggregator<0, 16> for Vdaf {
type PrepareState = PrepareState;
type PrepareShare = ();
type PrepareMessage = ();
fn prepare_init(
&self,
_verify_key: &[u8; 0],
_: usize,
aggregation_param: &Self::AggregationParam,
_nonce: &[u8; 16],
_: &Self::PublicShare,
input_share: &Self::InputShare,
) -> Result<(Self::PrepareState, Self::PrepareShare), VdafError> {
(self.prep_init_fn)(aggregation_param)?;
Ok((
PrepareState {
input_share: input_share.0,
current_round: 0,
},
(),
))
}
fn prepare_shares_to_prepare_message<M: IntoIterator<Item = Self::PrepareShare>>(
&self,
_: &Self::AggregationParam,
_: M,
) -> Result<Self::PrepareMessage, VdafError> {
Ok(())
}
fn prepare_next(
&self,
state: Self::PrepareState,
_: Self::PrepareMessage,
) -> Result<PrepareTransition<Self, 0, 16>, VdafError> {
(self.prep_step_fn)(&state)
}
fn aggregate<M: IntoIterator<Item = Self::OutputShare>>(
&self,
_aggregation_param: &Self::AggregationParam,
output_shares: M,
) -> Result<Self::AggregateShare, VdafError> {
let mut aggregate_share = AggregateShare(0);
for output_share in output_shares {
aggregate_share.accumulate(&output_share)?;
}
Ok(aggregate_share)
}
}
impl vdaf::Client<16> for Vdaf {
fn shard(
&self,
measurement: &Self::Measurement,
_nonce: &[u8; 16],
) -> Result<(Self::PublicShare, Vec<Self::InputShare>), VdafError> {
let first_input_share = random();
let (second_input_share, _) = measurement.overflowing_sub(first_input_share);
Ok((
(),
Vec::from([
InputShare(first_input_share),
InputShare(second_input_share),
]),
))
}
}
impl vdaf::Collector for Vdaf {
fn unshard<M: IntoIterator<Item = Self::AggregateShare>>(
&self,
aggregation_param: &Self::AggregationParam,
agg_shares: M,
_num_measurements: usize,
) -> Result<Self::AggregateResult, VdafError> {
Ok(agg_shares
.into_iter()
.fold(0, |acc, share| (acc + share.0) % MODULUS)
// Sum in the aggregation parameter so that collections over the same measurements with
// varying parameters will yield predictable but distinct results.
+ u64::from(aggregation_param.0))
}
}
/// A dummy input share.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct InputShare(pub u8);
impl Encode for InputShare {
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> {
self.0.encode(bytes)
}
fn encoded_len(&self) -> Option<usize> {
self.0.encoded_len()
}
}
impl Decode for InputShare {
fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> {
Ok(Self(u8::decode(bytes)?))
}
}
/// Dummy aggregation parameter.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AggregationParam(pub u8);
impl Encode for AggregationParam {
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> {
self.0.encode(bytes)
}
fn encoded_len(&self) -> Option<usize> {
self.0.encoded_len()
}
}
impl Decode for AggregationParam {
fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> {
Ok(Self(u8::decode(bytes)?))
}
}
/// Dummy output share.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OutputShare(pub u64);
impl Decode for OutputShare {
fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> {
Ok(Self(u64::decode(bytes)?))
}
}
impl Encode for OutputShare {
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> {
self.0.encode(bytes)
}
fn encoded_len(&self) -> Option<usize> {
self.0.encoded_len()
}
}
/// Dummy prepare state.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct PrepareState {
input_share: u8,
current_round: u32,
}
impl Encode for PrepareState {
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> {
self.input_share.encode(bytes)?;
self.current_round.encode(bytes)
}
fn encoded_len(&self) -> Option<usize> {
Some(self.input_share.encoded_len()? + self.current_round.encoded_len()?)
}
}
impl Decode for PrepareState {
fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> {
let input_share = u8::decode(bytes)?;
let current_round = u32::decode(bytes)?;
Ok(Self {
input_share,
current_round,
})
}
}
/// Dummy aggregate share.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AggregateShare(pub u64);
impl Aggregatable for AggregateShare {
type OutputShare = OutputShare;
fn merge(&mut self, other: &Self) -> Result<(), VdafError> {
self.0 = (self.0 + other.0) % MODULUS;
Ok(())
}
fn accumulate(&mut self, out_share: &Self::OutputShare) -> Result<(), VdafError> {
self.0 = (self.0 + out_share.0) % MODULUS;
Ok(())
}
}
impl From<OutputShare> for AggregateShare {
fn from(out_share: OutputShare) -> Self {
Self(out_share.0)
}
}
impl Decode for AggregateShare {
fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> {
Ok(Self(u64::decode(bytes)?))
}
}
impl Encode for AggregateShare {
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> {
self.0.encode(bytes)
}
fn encoded_len(&self) -> Option<usize> {
self.0.encoded_len()
}
}
/// Returns the aggregate result that the dummy VDAF would compute over the provided measurements,
/// for the provided aggregation parameter.
pub fn expected_aggregate_result<M>(aggregation_parameter: u8, measurements: M) -> u64
where
M: IntoIterator<Item = u8>,
{
(measurements.into_iter().map(u64::from).sum::<u64>()) % MODULUS
+ u64::from(aggregation_parameter)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::vdaf::{test_utils::run_vdaf_sharded, Client};
use rand::prelude::*;
fn run_test(rounds: u32, aggregation_parameter: u8) {
let vdaf = Vdaf::new(rounds);
let mut verify_key = [0; 0];
thread_rng().fill(&mut verify_key[..]);
let measurements = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
let mut sharded_measurements = Vec::new();
for measurement in measurements {
let nonce = thread_rng().gen();
let (public_share, input_shares) = vdaf.shard(&measurement, &nonce).unwrap();
sharded_measurements.push((public_share, nonce, input_shares));
}
let result = run_vdaf_sharded(
&vdaf,
&AggregationParam(aggregation_parameter),
sharded_measurements.clone(),
)
.unwrap();
assert_eq!(
result,
expected_aggregate_result(aggregation_parameter, measurements)
);
}
#[test]
fn single_round_agg_param_10() {
run_test(1, 10)
}
#[test]
fn single_round_agg_param_20() {
run_test(1, 20)
}
#[test]
fn single_round_agg_param_32() {
run_test(1, 32)
}
#[test]
fn single_round_agg_param_u8_max() {
run_test(1, u8::MAX)
}
#[test]
fn two_round_agg_param_10() {
run_test(2, 10)
}
#[test]
fn two_round_agg_param_20() {
run_test(2, 20)
}
#[test]
fn two_round_agg_param_32() {
run_test(2, 32)
}
#[test]
fn two_round_agg_param_u8_max() {
run_test(2, u8::MAX)
}
}
|