| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
//! Implementation of a dummy VDAF which conforms to the specification in [draft-irtf-cfrg-vdaf-06] |
| 4 |
//! but does nothing. Useful for testing. |
| 5 |
//! |
| 6 |
//! [draft-irtf-cfrg-vdaf-06]: https://datatracker.ietf.org/doc/draft-irtf-cfrg-vdaf/06/ |
| 7 |
|
| 8 |
use crate::{ |
| 9 |
codec::{CodecError, Decode, Encode}, |
| 10 |
vdaf::{self, Aggregatable, PrepareTransition, VdafError}, |
| 11 |
}; |
| 12 |
use rand::random; |
| 13 |
use std::{fmt::Debug, io::Cursor, sync::Arc}; |
| 14 |
|
| 15 |
/// The Dummy VDAF does summation modulus 256 so we can predict aggregation results. |
| 16 |
const MODULUS: u64 = u8::MAX as u64 + 1; |
| 17 |
|
| 18 |
type ArcPrepInitFn = |
| 19 |
Arc<dyn Fn(&AggregationParam) -> Result<(), VdafError> + 'static + Send + Sync>; |
| 20 |
type ArcPrepStepFn = Arc< |
| 21 |
dyn Fn(&PrepareState) -> Result<PrepareTransition<Vdaf, 0, 16>, VdafError> |
| 22 |
+ 'static |
| 23 |
+ Send |
| 24 |
+ Sync, |
| 25 |
>; |
| 26 |
|
| 27 |
/// Dummy VDAF that does nothing. |
| 28 |
#[derive(Clone)] |
| 29 |
pub struct Vdaf { |
| 30 |
prep_init_fn: ArcPrepInitFn, |
| 31 |
prep_step_fn: ArcPrepStepFn, |
| 32 |
} |
| 33 |
|
| 34 |
impl Debug for Vdaf { |
| 35 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 36 |
f.debug_struct("Vdaf") |
| 37 |
.field("prep_init_fn", &"[redacted]") |
| 38 |
.field("prep_step_fn", &"[redacted]") |
| 39 |
.finish() |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
impl Vdaf { |
| 44 |
/// The length of the verify key parameter for fake VDAF instantiations. |
| 45 |
pub const VERIFY_KEY_LEN: usize = 0; |
| 46 |
|
| 47 |
/// Construct a new instance of the dummy VDAF. |
| 48 |
pub fn new(rounds: u32) -> Self { |
| 49 |
Self { |
| 50 |
prep_init_fn: Arc::new(|_| -> Result<(), VdafError> { Ok(()) }), |
| 51 |
prep_step_fn: Arc::new( |
| 52 |
move |state| -> Result<PrepareTransition<Self, 0, 16>, VdafError> { |
| 53 |
let new_round = state.current_round + 1; |
| 54 |
if new_round == rounds { |
| 55 |
Ok(PrepareTransition::Finish(OutputShare(u64::from( |
| 56 |
state.input_share, |
| 57 |
)))) |
| 58 |
} else { |
| 59 |
Ok(PrepareTransition::Continue( |
| 60 |
PrepareState { |
| 61 |
current_round: new_round, |
| 62 |
..*state |
| 63 |
}, |
| 64 |
(), |
| 65 |
)) |
| 66 |
} |
| 67 |
}, |
| 68 |
), |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/// Provide an alternate implementation of [`vdaf::Aggregator::prepare_init`]. |
| 73 |
pub fn with_prep_init_fn<F: Fn(&AggregationParam) -> Result<(), VdafError>>( |
| 74 |
mut self, |
| 75 |
f: F, |
| 76 |
) -> Self |
| 77 |
where |
| 78 |
F: 'static + Send + Sync, |
| 79 |
{ |
| 80 |
self.prep_init_fn = Arc::new(f); |
| 81 |
self |
| 82 |
} |
| 83 |
|
| 84 |
/// Provide an alternate implementation of [`vdaf::Aggregator::prepare_next`]. |
| 85 |
pub fn with_prep_step_fn< |
| 86 |
F: Fn(&PrepareState) -> Result<PrepareTransition<Self, 0, 16>, VdafError>, |
| 87 |
>( |
| 88 |
mut self, |
| 89 |
f: F, |
| 90 |
) -> Self |
| 91 |
where |
| 92 |
F: 'static + Send + Sync, |
| 93 |
{ |
| 94 |
self.prep_step_fn = Arc::new(f); |
| 95 |
self |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
impl Default for Vdaf { |
| 100 |
fn default() -> Self { |
| 101 |
Self::new(1) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
impl vdaf::Vdaf for Vdaf { |
| 106 |
type Measurement = u8; |
| 107 |
type AggregateResult = u64; |
| 108 |
type AggregationParam = AggregationParam; |
| 109 |
type PublicShare = (); |
| 110 |
type InputShare = InputShare; |
| 111 |
type OutputShare = OutputShare; |
| 112 |
type AggregateShare = AggregateShare; |
| 113 |
|
| 114 |
fn algorithm_id(&self) -> u32 { |
| 115 |
0xFFFF0000 |
| 116 |
} |
| 117 |
|
| 118 |
fn num_aggregators(&self) -> usize { |
| 119 |
2 |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
impl vdaf::Aggregator<0, 16> for Vdaf { |
| 124 |
type PrepareState = PrepareState; |
| 125 |
type PrepareShare = (); |
| 126 |
type PrepareMessage = (); |
| 127 |
|
| 128 |
fn prepare_init( |
| 129 |
&self, |
| 130 |
_verify_key: &[u8; 0], |
| 131 |
_: usize, |
| 132 |
aggregation_param: &Self::AggregationParam, |
| 133 |
_nonce: &[u8; 16], |
| 134 |
_: &Self::PublicShare, |
| 135 |
input_share: &Self::InputShare, |
| 136 |
) -> Result<(Self::PrepareState, Self::PrepareShare), VdafError> { |
| 137 |
(self.prep_init_fn)(aggregation_param)?; |
| 138 |
Ok(( |
| 139 |
PrepareState { |
| 140 |
input_share: input_share.0, |
| 141 |
current_round: 0, |
| 142 |
}, |
| 143 |
(), |
| 144 |
)) |
| 145 |
} |
| 146 |
|
| 147 |
fn prepare_shares_to_prepare_message<M: IntoIterator<Item = Self::PrepareShare>>( |
| 148 |
&self, |
| 149 |
_: &Self::AggregationParam, |
| 150 |
_: M, |
| 151 |
) -> Result<Self::PrepareMessage, VdafError> { |
| 152 |
Ok(()) |
| 153 |
} |
| 154 |
|
| 155 |
fn prepare_next( |
| 156 |
&self, |
| 157 |
state: Self::PrepareState, |
| 158 |
_: Self::PrepareMessage, |
| 159 |
) -> Result<PrepareTransition<Self, 0, 16>, VdafError> { |
| 160 |
(self.prep_step_fn)(&state) |
| 161 |
} |
| 162 |
|
| 163 |
fn aggregate<M: IntoIterator<Item = Self::OutputShare>>( |
| 164 |
&self, |
| 165 |
_aggregation_param: &Self::AggregationParam, |
| 166 |
output_shares: M, |
| 167 |
) -> Result<Self::AggregateShare, VdafError> { |
| 168 |
let mut aggregate_share = AggregateShare(0); |
| 169 |
for output_share in output_shares { |
| 170 |
aggregate_share.accumulate(&output_share)?; |
| 171 |
} |
| 172 |
Ok(aggregate_share) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
impl vdaf::Client<16> for Vdaf { |
| 177 |
fn shard( |
| 178 |
&self, |
| 179 |
measurement: &Self::Measurement, |
| 180 |
_nonce: &[u8; 16], |
| 181 |
) -> Result<(Self::PublicShare, Vec<Self::InputShare>), VdafError> { |
| 182 |
let first_input_share = random(); |
| 183 |
let (second_input_share, _) = measurement.overflowing_sub(first_input_share); |
| 184 |
Ok(( |
| 185 |
(), |
| 186 |
Vec::from([ |
| 187 |
InputShare(first_input_share), |
| 188 |
InputShare(second_input_share), |
| 189 |
]), |
| 190 |
)) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
impl vdaf::Collector for Vdaf { |
| 195 |
fn unshard<M: IntoIterator<Item = Self::AggregateShare>>( |
| 196 |
&self, |
| 197 |
aggregation_param: &Self::AggregationParam, |
| 198 |
agg_shares: M, |
| 199 |
_num_measurements: usize, |
| 200 |
) -> Result<Self::AggregateResult, VdafError> { |
| 201 |
Ok(agg_shares |
| 202 |
.into_iter() |
| 203 |
.fold(0, |acc, share| (acc + share.0) % MODULUS) |
| 204 |
// Sum in the aggregation parameter so that collections over the same measurements with |
| 205 |
// varying parameters will yield predictable but distinct results. |
| 206 |
+ u64::from(aggregation_param.0)) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/// A dummy input share. |
| 211 |
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] |
| 212 |
pub struct InputShare(pub u8); |
| 213 |
|
| 214 |
impl Encode for InputShare { |
| 215 |
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> { |
| 216 |
self.0.encode(bytes) |
| 217 |
} |
| 218 |
|
| 219 |
fn encoded_len(&self) -> Option<usize> { |
| 220 |
self.0.encoded_len() |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
impl Decode for InputShare { |
| 225 |
fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> { |
| 226 |
Ok(Self(u8::decode(bytes)?)) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/// Dummy aggregation parameter. |
| 231 |
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 232 |
pub struct AggregationParam(pub u8); |
| 233 |
|
| 234 |
impl Encode for AggregationParam { |
| 235 |
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> { |
| 236 |
self.0.encode(bytes) |
| 237 |
} |
| 238 |
|
| 239 |
fn encoded_len(&self) -> Option<usize> { |
| 240 |
self.0.encoded_len() |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
impl Decode for AggregationParam { |
| 245 |
fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> { |
| 246 |
Ok(Self(u8::decode(bytes)?)) |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
/// Dummy output share. |
| 251 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 252 |
pub struct OutputShare(pub u64); |
| 253 |
|
| 254 |
impl Decode for OutputShare { |
| 255 |
fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> { |
| 256 |
Ok(Self(u64::decode(bytes)?)) |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
impl Encode for OutputShare { |
| 261 |
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> { |
| 262 |
self.0.encode(bytes) |
| 263 |
} |
| 264 |
|
| 265 |
fn encoded_len(&self) -> Option<usize> { |
| 266 |
self.0.encoded_len() |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
/// Dummy prepare state. |
| 271 |
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 272 |
pub struct PrepareState { |
| 273 |
input_share: u8, |
| 274 |
current_round: u32, |
| 275 |
} |
| 276 |
|
| 277 |
impl Encode for PrepareState { |
| 278 |
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> { |
| 279 |
self.input_share.encode(bytes)?; |
| 280 |
self.current_round.encode(bytes) |
| 281 |
} |
| 282 |
|
| 283 |
fn encoded_len(&self) -> Option<usize> { |
| 284 |
Some(self.input_share.encoded_len()? + self.current_round.encoded_len()?) |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
impl Decode for PrepareState { |
| 289 |
fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> { |
| 290 |
let input_share = u8::decode(bytes)?; |
| 291 |
let current_round = u32::decode(bytes)?; |
| 292 |
|
| 293 |
Ok(Self { |
| 294 |
input_share, |
| 295 |
current_round, |
| 296 |
}) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
/// Dummy aggregate share. |
| 301 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 302 |
pub struct AggregateShare(pub u64); |
| 303 |
|
| 304 |
impl Aggregatable for AggregateShare { |
| 305 |
type OutputShare = OutputShare; |
| 306 |
|
| 307 |
fn merge(&mut self, other: &Self) -> Result<(), VdafError> { |
| 308 |
self.0 = (self.0 + other.0) % MODULUS; |
| 309 |
Ok(()) |
| 310 |
} |
| 311 |
|
| 312 |
fn accumulate(&mut self, out_share: &Self::OutputShare) -> Result<(), VdafError> { |
| 313 |
self.0 = (self.0 + out_share.0) % MODULUS; |
| 314 |
Ok(()) |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
impl From<OutputShare> for AggregateShare { |
| 319 |
fn from(out_share: OutputShare) -> Self { |
| 320 |
Self(out_share.0) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
impl Decode for AggregateShare { |
| 325 |
fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> { |
| 326 |
Ok(Self(u64::decode(bytes)?)) |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
impl Encode for AggregateShare { |
| 331 |
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> { |
| 332 |
self.0.encode(bytes) |
| 333 |
} |
| 334 |
|
| 335 |
fn encoded_len(&self) -> Option<usize> { |
| 336 |
self.0.encoded_len() |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
/// Returns the aggregate result that the dummy VDAF would compute over the provided measurements, |
| 341 |
/// for the provided aggregation parameter. |
| 342 |
pub fn expected_aggregate_result<M>(aggregation_parameter: u8, measurements: M) -> u64 |
| 343 |
where |
| 344 |
M: IntoIterator<Item = u8>, |
| 345 |
{ |
| 346 |
(measurements.into_iter().map(u64::from).sum::<u64>()) % MODULUS |
| 347 |
+ u64::from(aggregation_parameter) |
| 348 |
} |
| 349 |
|
| 350 |
#[cfg(test)] |
| 351 |
mod tests { |
| 352 |
use super::*; |
| 353 |
use crate::vdaf::{test_utils::run_vdaf_sharded, Client}; |
| 354 |
use rand::prelude::*; |
| 355 |
|
| 356 |
fn run_test(rounds: u32, aggregation_parameter: u8) { |
| 357 |
let vdaf = Vdaf::new(rounds); |
| 358 |
let mut verify_key = [0; 0]; |
| 359 |
thread_rng().fill(&mut verify_key[..]); |
| 360 |
let measurements = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]; |
| 361 |
|
| 362 |
let mut sharded_measurements = Vec::new(); |
| 363 |
for measurement in measurements { |
| 364 |
let nonce = thread_rng().gen(); |
| 365 |
let (public_share, input_shares) = vdaf.shard(&measurement, &nonce).unwrap(); |
| 366 |
|
| 367 |
sharded_measurements.push((public_share, nonce, input_shares)); |
| 368 |
} |
| 369 |
|
| 370 |
let result = run_vdaf_sharded( |
| 371 |
&vdaf, |
| 372 |
&AggregationParam(aggregation_parameter), |
| 373 |
sharded_measurements.clone(), |
| 374 |
) |
| 375 |
.unwrap(); |
| 376 |
assert_eq!( |
| 377 |
result, |
| 378 |
expected_aggregate_result(aggregation_parameter, measurements) |
| 379 |
); |
| 380 |
} |
| 381 |
|
| 382 |
#[test] |
| 383 |
fn single_round_agg_param_10() { |
| 384 |
run_test(1, 10) |
| 385 |
} |
| 386 |
|
| 387 |
#[test] |
| 388 |
fn single_round_agg_param_20() { |
| 389 |
run_test(1, 20) |
| 390 |
} |
| 391 |
|
| 392 |
#[test] |
| 393 |
fn single_round_agg_param_32() { |
| 394 |
run_test(1, 32) |
| 395 |
} |
| 396 |
|
| 397 |
#[test] |
| 398 |
fn single_round_agg_param_u8_max() { |
| 399 |
run_test(1, u8::MAX) |
| 400 |
} |
| 401 |
|
| 402 |
#[test] |
| 403 |
fn two_round_agg_param_10() { |
| 404 |
run_test(2, 10) |
| 405 |
} |
| 406 |
|
| 407 |
#[test] |
| 408 |
fn two_round_agg_param_20() { |
| 409 |
run_test(2, 20) |
| 410 |
} |
| 411 |
|
| 412 |
#[test] |
| 413 |
fn two_round_agg_param_32() { |
| 414 |
run_test(2, 32) |
| 415 |
} |
| 416 |
|
| 417 |
#[test] |
| 418 |
fn two_round_agg_param_u8_max() { |
| 419 |
run_test(2, u8::MAX) |
| 420 |
} |
| 421 |
} |