| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
//! Verifiable Incremental Distributed Point Function (VIDPF). |
| 4 |
//! |
| 5 |
//! The VIDPF construction is specified in [[draft-mouris-cfrg-mastic]] and builds |
| 6 |
//! on techniques from [[MST23]] and [[CP22]] to lift an IDPF to a VIDPF. |
| 7 |
//! |
| 8 |
//! [CP22]: https://eprint.iacr.org/2021/580 |
| 9 |
//! [MST23]: https://eprint.iacr.org/2023/080 |
| 10 |
//! [draft-mouris-cfrg-mastic]: https://datatracker.ietf.org/doc/draft-mouris-cfrg-mastic/02/ |
| 11 |
|
| 12 |
use core::{ |
| 13 |
iter::zip, |
| 14 |
ops::{Add, AddAssign, BitXor, BitXorAssign, Index, Sub}, |
| 15 |
}; |
| 16 |
|
| 17 |
use bitvec::field::BitField; |
| 18 |
use rand_core::RngCore; |
| 19 |
use std::io::Cursor; |
| 20 |
use subtle::{Choice, ConditionallyNegatable, ConditionallySelectable}; |
| 21 |
|
| 22 |
use crate::{ |
| 23 |
codec::{CodecError, Encode, ParameterizedDecode}, |
| 24 |
field::FieldElement, |
| 25 |
idpf::{ |
| 26 |
conditional_select_seed, conditional_swap_seed, conditional_xor_seeds, xor_seeds, |
| 27 |
IdpfInput, IdpfValue, |
| 28 |
}, |
| 29 |
vdaf::xof::{Seed, Xof, XofFixedKeyAes128, XofTurboShake128}, |
| 30 |
}; |
| 31 |
|
| 32 |
/// VIDPF-related errors. |
| 33 |
#[derive(Debug, thiserror::Error)] |
| 34 |
#[non_exhaustive] |
| 35 |
pub enum VidpfError { |
| 36 |
/// Error when key's identifier are equal. |
| 37 |
#[error("key's identifier should be different")] |
| 38 |
SameKeyId, |
| 39 |
|
| 40 |
/// Error when level does not fit in a 32-bit number. |
| 41 |
#[error("level is not representable as a 32-bit integer")] |
| 42 |
LevelTooBig, |
| 43 |
|
| 44 |
/// Error during VIDPF evaluation: tried to access a level index out of bounds. |
| 45 |
#[error("level index out of bounds")] |
| 46 |
IndexLevel, |
| 47 |
|
| 48 |
/// Error when weight's length mismatches the length in weight's parameter. |
| 49 |
#[error("invalid weight length")] |
| 50 |
InvalidWeightLength, |
| 51 |
|
| 52 |
/// Failure when calling getrandom(). |
| 53 |
#[error("getrandom: {0}")] |
| 54 |
GetRandom(#[from] getrandom::Error), |
| 55 |
} |
| 56 |
|
| 57 |
/// Represents the domain of an incremental point function. |
| 58 |
pub type VidpfInput = IdpfInput; |
| 59 |
|
| 60 |
/// Represents the codomain of an incremental point function. |
| 61 |
pub trait VidpfValue: IdpfValue + Clone {} |
| 62 |
|
| 63 |
/// A VIDPF instance. |
| 64 |
pub struct Vidpf<W: VidpfValue, const NONCE_SIZE: usize> { |
| 65 |
/// Any parameters required to instantiate a weight value. |
| 66 |
weight_parameter: W::ValueParameter, |
| 67 |
} |
| 68 |
|
| 69 |
impl<W: VidpfValue, const NONCE_SIZE: usize> Vidpf<W, NONCE_SIZE> { |
| 70 |
/// Creates a VIDPF instance. |
| 71 |
/// |
| 72 |
/// # Arguments |
| 73 |
/// |
| 74 |
/// * `weight_parameter`, any parameters required to instantiate a weight value. |
| 75 |
pub const fn new(weight_parameter: W::ValueParameter) -> Self { |
| 76 |
Self { weight_parameter } |
| 77 |
} |
| 78 |
|
| 79 |
/// The [`Vidpf::gen`] method splits an incremental point function `F` into two private keys |
| 80 |
/// used by the aggregation servers, and a common public share. |
| 81 |
/// |
| 82 |
/// The incremental point function is defined as `F`: [`VidpfInput`] --> [`VidpfValue`] |
| 83 |
/// such that: |
| 84 |
/// |
| 85 |
/// ```txt |
| 86 |
/// F(x) = weight, if x is a prefix of the input. |
| 87 |
/// F(x) = 0, if x is not a prefix of the input. |
| 88 |
/// ``` |
| 89 |
/// |
| 90 |
/// # Arguments |
| 91 |
/// |
| 92 |
/// * `input`, determines the input of the function. |
| 93 |
/// * `weight`, determines the input's weight of the function. |
| 94 |
/// * `nonce`, used to cryptographically bind some information. |
| 95 |
pub fn gen( |
| 96 |
&self, |
| 97 |
input: &VidpfInput, |
| 98 |
weight: &W, |
| 99 |
nonce: &[u8; NONCE_SIZE], |
| 100 |
) -> Result<(VidpfPublicShare<W>, [VidpfKey; 2]), VidpfError> { |
| 101 |
let keys = [ |
| 102 |
VidpfKey::gen(VidpfServerId::S0)?, |
| 103 |
VidpfKey::gen(VidpfServerId::S1)?, |
| 104 |
]; |
| 105 |
let public = self.gen_with_keys(&keys, input, weight, nonce)?; |
| 106 |
Ok((public, keys)) |
| 107 |
} |
| 108 |
|
| 109 |
/// [`Vidpf::gen_with_keys`] works as the [`Vidpf::gen`] method, except that two different |
| 110 |
/// keys must be provided. |
| 111 |
fn gen_with_keys( |
| 112 |
&self, |
| 113 |
keys: &[VidpfKey; 2], |
| 114 |
input: &VidpfInput, |
| 115 |
weight: &W, |
| 116 |
nonce: &[u8; NONCE_SIZE], |
| 117 |
) -> Result<VidpfPublicShare<W>, VidpfError> { |
| 118 |
if keys[0].id == keys[1].id { |
| 119 |
return Err(VidpfError::SameKeyId); |
| 120 |
} |
| 121 |
|
| 122 |
let mut s_i = [keys[0].value, keys[1].value]; |
| 123 |
let mut t_i = [Choice::from(keys[0].id), Choice::from(keys[1].id)]; |
| 124 |
|
| 125 |
let n = input.len(); |
| 126 |
let mut cw = Vec::with_capacity(n); |
| 127 |
let mut cs = Vec::with_capacity(n); |
| 128 |
|
| 129 |
for level in 0..n { |
| 130 |
let alpha_i = Choice::from(u8::from(input.get(level).ok_or(VidpfError::IndexLevel)?)); |
| 131 |
|
| 132 |
// If alpha_i == 0 then |
| 133 |
// (same_seed, diff_seed) = (right_seed, left_seed) |
| 134 |
// else |
| 135 |
// (same_seed, diff_seed) = (left_seed, right_seed) |
| 136 |
let seq_0 = Self::prg(&s_i[0], nonce); |
| 137 |
let (same_seed_0, diff_seed_0) = &mut (seq_0.right_seed, seq_0.left_seed); |
| 138 |
conditional_swap_seed(same_seed_0, diff_seed_0, alpha_i); |
| 139 |
|
| 140 |
let seq_1 = Self::prg(&s_i[1], nonce); |
| 141 |
let (same_seed_1, diff_seed_1) = &mut (seq_1.right_seed, seq_1.left_seed); |
| 142 |
conditional_swap_seed(same_seed_1, diff_seed_1, alpha_i); |
| 143 |
|
| 144 |
// If alpha_i == 0 then |
| 145 |
// diff_control_bit = left_control_bit |
| 146 |
// else |
| 147 |
// diff_control_bit = right_control_bit |
| 148 |
let diff_control_bit_0 = Choice::conditional_select( |
| 149 |
&seq_0.left_control_bit, |
| 150 |
&seq_0.right_control_bit, |
| 151 |
alpha_i, |
| 152 |
); |
| 153 |
let diff_control_bit_1 = Choice::conditional_select( |
| 154 |
&seq_1.left_control_bit, |
| 155 |
&seq_1.right_control_bit, |
| 156 |
alpha_i, |
| 157 |
); |
| 158 |
|
| 159 |
let s_cw = xor_seeds(same_seed_0, same_seed_1); |
| 160 |
let t_cw_l = |
| 161 |
seq_0.left_control_bit ^ seq_1.left_control_bit ^ alpha_i ^ Choice::from(1); |
| 162 |
let t_cw_r = seq_0.right_control_bit ^ seq_1.right_control_bit ^ alpha_i; |
| 163 |
let t_cw_diff = Choice::conditional_select(&t_cw_l, &t_cw_r, alpha_i); |
| 164 |
|
| 165 |
let s_tilde_i_0 = conditional_xor_seeds(diff_seed_0, &s_cw, t_i[0]); |
| 166 |
let s_tilde_i_1 = conditional_xor_seeds(diff_seed_1, &s_cw, t_i[1]); |
| 167 |
|
| 168 |
t_i[0] = diff_control_bit_0 ^ (t_i[0] & t_cw_diff); |
| 169 |
t_i[1] = diff_control_bit_1 ^ (t_i[1] & t_cw_diff); |
| 170 |
|
| 171 |
let w_i_0; |
| 172 |
let w_i_1; |
| 173 |
(s_i[0], w_i_0) = self.convert(s_tilde_i_0, nonce); |
| 174 |
(s_i[1], w_i_1) = self.convert(s_tilde_i_1, nonce); |
| 175 |
|
| 176 |
let mut w_cw = w_i_1 - w_i_0 + weight.clone(); |
| 177 |
w_cw.conditional_negate(t_i[1]); |
| 178 |
|
| 179 |
let cw_i = VidpfCorrectionWord { |
| 180 |
seed: s_cw, |
| 181 |
left_control_bit: t_cw_l, |
| 182 |
right_control_bit: t_cw_r, |
| 183 |
weight: w_cw, |
| 184 |
}; |
| 185 |
cw.push(cw_i); |
| 186 |
|
| 187 |
let pi_tilde_0 = Self::node_proof(input, level, &s_i[0])?; |
| 188 |
let pi_tilde_1 = Self::node_proof(input, level, &s_i[1])?; |
| 189 |
let cs_i = xor_proof(pi_tilde_0, &pi_tilde_1); |
| 190 |
cs.push(cs_i); |
| 191 |
} |
| 192 |
|
| 193 |
Ok(VidpfPublicShare { cw, cs }) |
| 194 |
} |
| 195 |
|
| 196 |
/// [`Vidpf::eval`] evaluates the entire `input` and produces a share of the |
| 197 |
/// input's weight. |
| 198 |
pub fn eval( |
| 199 |
&self, |
| 200 |
key: &VidpfKey, |
| 201 |
public: &VidpfPublicShare<W>, |
| 202 |
input: &VidpfInput, |
| 203 |
nonce: &[u8; NONCE_SIZE], |
| 204 |
) -> Result<VidpfValueShare<W>, VidpfError> { |
| 205 |
let mut state = VidpfEvalState::init_from_key(key); |
| 206 |
let mut share = W::zero(&self.weight_parameter); |
| 207 |
|
| 208 |
let n = input.len(); |
| 209 |
for level in 0..n { |
| 210 |
(state, share) = self.eval_next(key.id, public, input, level, &state, nonce)?; |
| 211 |
} |
| 212 |
|
| 213 |
Ok(VidpfValueShare { |
| 214 |
share, |
| 215 |
proof: state.proof, |
| 216 |
}) |
| 217 |
} |
| 218 |
|
| 219 |
/// [`Vidpf::eval_next`] evaluates the `input` at the given level using the provided initial |
| 220 |
/// state, and returns a new state and a share of the input's weight at that level. |
| 221 |
fn eval_next( |
| 222 |
&self, |
| 223 |
id: VidpfServerId, |
| 224 |
public: &VidpfPublicShare<W>, |
| 225 |
input: &VidpfInput, |
| 226 |
level: usize, |
| 227 |
state: &VidpfEvalState, |
| 228 |
nonce: &[u8; NONCE_SIZE], |
| 229 |
) -> Result<(VidpfEvalState, W), VidpfError> { |
| 230 |
let cw = public.cw.get(level).ok_or(VidpfError::IndexLevel)?; |
| 231 |
|
| 232 |
let seq_tilde = Self::prg(&state.seed, nonce); |
| 233 |
|
| 234 |
let t_i = state.control_bit; |
| 235 |
let sl = conditional_xor_seeds(&seq_tilde.left_seed, &cw.seed, t_i); |
| 236 |
let sr = conditional_xor_seeds(&seq_tilde.right_seed, &cw.seed, t_i); |
| 237 |
let tl = seq_tilde.left_control_bit ^ (t_i & cw.left_control_bit); |
| 238 |
let tr = seq_tilde.right_control_bit ^ (t_i & cw.right_control_bit); |
| 239 |
|
| 240 |
let x_i = Choice::from(u8::from(input.get(level).ok_or(VidpfError::IndexLevel)?)); |
| 241 |
let s_tilde_i = conditional_select_seed(x_i, &[sl, sr]); |
| 242 |
|
| 243 |
let next_control_bit = Choice::conditional_select(&tl, &tr, x_i); |
| 244 |
let (next_seed, w_i) = self.convert(s_tilde_i, nonce); |
| 245 |
|
| 246 |
let zero = <W as IdpfValue>::zero(&self.weight_parameter); |
| 247 |
let mut y = <W as IdpfValue>::conditional_select(&zero, &cw.weight, next_control_bit); |
| 248 |
y += w_i; |
| 249 |
y.conditional_negate(Choice::from(id)); |
| 250 |
|
| 251 |
let pi_i = &state.proof; |
| 252 |
let cs_i = public.cs.get(level).ok_or(VidpfError::IndexLevel)?; |
| 253 |
let pi_tilde = Self::node_proof(input, level, &next_seed)?; |
| 254 |
let h2_input = xor_proof( |
| 255 |
conditional_xor_proof(pi_tilde, cs_i, next_control_bit), |
| 256 |
pi_i, |
| 257 |
); |
| 258 |
let next_proof = xor_proof(Self::node_proof_adjustment(h2_input), pi_i); |
| 259 |
|
| 260 |
let next_state = VidpfEvalState { |
| 261 |
seed: next_seed, |
| 262 |
control_bit: next_control_bit, |
| 263 |
proof: next_proof, |
| 264 |
}; |
| 265 |
|
| 266 |
Ok((next_state, y)) |
| 267 |
} |
| 268 |
|
| 269 |
fn prg(seed: &VidpfSeed, nonce: &[u8]) -> VidpfPrgOutput { |
| 270 |
let mut rng = XofFixedKeyAes128::seed_stream(&Seed(*seed), VidpfDomainSepTag::PRG, nonce); |
| 271 |
|
| 272 |
let mut left_seed = VidpfSeed::default(); |
| 273 |
let mut right_seed = VidpfSeed::default(); |
| 274 |
rng.fill_bytes(&mut left_seed); |
| 275 |
rng.fill_bytes(&mut right_seed); |
| 276 |
// Use the LSB of seeds as control bits, and clears the bit, |
| 277 |
// i.e., seeds produced by `prg` always have their LSB = 0. |
| 278 |
// This ensures `prg` costs two AES calls only. |
| 279 |
let left_control_bit = Choice::from(left_seed[0] & 0x01); |
| 280 |
let right_control_bit = Choice::from(right_seed[0] & 0x01); |
| 281 |
left_seed[0] &= 0xFE; |
| 282 |
right_seed[0] &= 0xFE; |
| 283 |
|
| 284 |
VidpfPrgOutput { |
| 285 |
left_seed, |
| 286 |
left_control_bit, |
| 287 |
right_seed, |
| 288 |
right_control_bit, |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
fn convert(&self, seed: VidpfSeed, nonce: &[u8; NONCE_SIZE]) -> (VidpfSeed, W) { |
| 293 |
let mut rng = |
| 294 |
XofFixedKeyAes128::seed_stream(&Seed(seed), VidpfDomainSepTag::CONVERT, nonce); |
| 295 |
|
| 296 |
let mut out_seed = VidpfSeed::default(); |
| 297 |
rng.fill_bytes(&mut out_seed); |
| 298 |
let value = <W as IdpfValue>::generate(&mut rng, &self.weight_parameter); |
| 299 |
|
| 300 |
(out_seed, value) |
| 301 |
} |
| 302 |
|
| 303 |
fn node_proof( |
| 304 |
input: &VidpfInput, |
| 305 |
level: usize, |
| 306 |
seed: &VidpfSeed, |
| 307 |
) -> Result<VidpfProof, VidpfError> { |
| 308 |
let mut shake = XofTurboShake128::init(seed, VidpfDomainSepTag::NODE_PROOF); |
| 309 |
for chunk128 in input |
| 310 |
.index(..=level) |
| 311 |
.chunks(128) |
| 312 |
.map(BitField::load_le::<u128>) |
| 313 |
.map(u128::to_le_bytes) |
| 314 |
{ |
| 315 |
shake.update(&chunk128); |
| 316 |
} |
| 317 |
shake.update( |
| 318 |
&u16::try_from(level) |
| 319 |
.map_err(|_e| VidpfError::LevelTooBig)? |
| 320 |
.to_le_bytes(), |
| 321 |
); |
| 322 |
let mut rng = shake.into_seed_stream(); |
| 323 |
|
| 324 |
let mut proof = VidpfProof::default(); |
| 325 |
rng.fill_bytes(&mut proof); |
| 326 |
|
| 327 |
Ok(proof) |
| 328 |
} |
| 329 |
|
| 330 |
fn node_proof_adjustment(mut proof: VidpfProof) -> VidpfProof { |
| 331 |
let mut rng = XofTurboShake128::seed_stream( |
| 332 |
&Seed(Default::default()), |
| 333 |
VidpfDomainSepTag::NODE_PROOF_ADJUST, |
| 334 |
&proof, |
| 335 |
); |
| 336 |
rng.fill_bytes(&mut proof); |
| 337 |
|
| 338 |
proof |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
/// Contains the domain separation tags for invoking different oracles. |
| 343 |
struct VidpfDomainSepTag; |
| 344 |
impl VidpfDomainSepTag { |
| 345 |
const PRG: &'static [u8] = b"Prg"; |
| 346 |
const CONVERT: &'static [u8] = b"Convert"; |
| 347 |
const NODE_PROOF: &'static [u8] = b"NodeProof"; |
| 348 |
const NODE_PROOF_ADJUST: &'static [u8] = b"NodeProofAdjust"; |
| 349 |
} |
| 350 |
|
| 351 |
/// Private key of an aggregation server. |
| 352 |
pub struct VidpfKey { |
| 353 |
id: VidpfServerId, |
| 354 |
value: [u8; 16], |
| 355 |
} |
| 356 |
|
| 357 |
impl VidpfKey { |
| 358 |
/// Generates a key at random. |
| 359 |
/// |
| 360 |
/// # Errors |
| 361 |
/// Triggers an error if the random generator fails. |
| 362 |
pub(crate) fn gen(id: VidpfServerId) -> Result<Self, VidpfError> { |
| 363 |
let mut value = [0; 16]; |
| 364 |
getrandom::getrandom(&mut value)?; |
| 365 |
Ok(Self { id, value }) |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
/// Identifies the two aggregation servers. |
| 370 |
#[derive(Clone, Copy, PartialEq, Eq)] |
| 371 |
pub(crate) enum VidpfServerId { |
| 372 |
/// S0 is the first server. |
| 373 |
S0, |
| 374 |
/// S1 is the second server. |
| 375 |
S1, |
| 376 |
} |
| 377 |
|
| 378 |
impl From<VidpfServerId> for Choice { |
| 379 |
fn from(value: VidpfServerId) -> Self { |
| 380 |
match value { |
| 381 |
VidpfServerId::S0 => Self::from(0), |
| 382 |
VidpfServerId::S1 => Self::from(1), |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
/// Adjusts values of shares during the VIDPF evaluation. |
| 388 |
#[derive(Debug)] |
| 389 |
struct VidpfCorrectionWord<W: VidpfValue> { |
| 390 |
seed: VidpfSeed, |
| 391 |
left_control_bit: Choice, |
| 392 |
right_control_bit: Choice, |
| 393 |
weight: W, |
| 394 |
} |
| 395 |
|
| 396 |
/// Common public information used by aggregation servers. |
| 397 |
#[derive(Debug)] |
| 398 |
pub struct VidpfPublicShare<W: VidpfValue> { |
| 399 |
cw: Vec<VidpfCorrectionWord<W>>, |
| 400 |
cs: Vec<VidpfProof>, |
| 401 |
} |
| 402 |
|
| 403 |
/// Contains the values produced during input evaluation at a given level. |
| 404 |
pub struct VidpfEvalState { |
| 405 |
seed: VidpfSeed, |
| 406 |
control_bit: Choice, |
| 407 |
proof: VidpfProof, |
| 408 |
} |
| 409 |
|
| 410 |
impl VidpfEvalState { |
| 411 |
fn init_from_key(key: &VidpfKey) -> Self { |
| 412 |
Self { |
| 413 |
seed: key.value, |
| 414 |
control_bit: Choice::from(key.id), |
| 415 |
proof: VidpfProof::default(), |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
/// Contains a share of the input's weight together with a proof for verification. |
| 421 |
pub struct VidpfValueShare<W: VidpfValue> { |
| 422 |
/// Secret share of the input's weight. |
| 423 |
pub share: W, |
| 424 |
/// Proof used to verify the share. |
| 425 |
pub proof: VidpfProof, |
| 426 |
} |
| 427 |
|
| 428 |
/// Proof size in bytes. |
| 429 |
const VIDPF_PROOF_SIZE: usize = 32; |
| 430 |
|
| 431 |
/// Allows to validate user input and shares after evaluation. |
| 432 |
type VidpfProof = [u8; VIDPF_PROOF_SIZE]; |
| 433 |
|
| 434 |
fn xor_proof(mut lhs: VidpfProof, rhs: &VidpfProof) -> VidpfProof { |
| 435 |
zip(&mut lhs, rhs).for_each(|(a, b)| a.bitxor_assign(b)); |
| 436 |
lhs |
| 437 |
} |
| 438 |
|
| 439 |
fn conditional_xor_proof(mut lhs: VidpfProof, rhs: &VidpfProof, choice: Choice) -> VidpfProof { |
| 440 |
zip(&mut lhs, rhs).for_each(|(a, b)| a.conditional_assign(&a.bitxor(b), choice)); |
| 441 |
lhs |
| 442 |
} |
| 443 |
|
| 444 |
/// Feeds a pseudorandom generator during evaluation. |
| 445 |
type VidpfSeed = [u8; 16]; |
| 446 |
|
| 447 |
/// Contains the seeds and control bits produced by [`Vidpf::prg`]. |
| 448 |
struct VidpfPrgOutput { |
| 449 |
left_seed: VidpfSeed, |
| 450 |
left_control_bit: Choice, |
| 451 |
right_seed: VidpfSeed, |
| 452 |
right_control_bit: Choice, |
| 453 |
} |
| 454 |
|
| 455 |
/// Represents an array of field elements that implements the [`VidpfValue`] trait. |
| 456 |
#[derive(Debug, PartialEq, Eq, Clone)] |
| 457 |
pub struct VidpfWeight<F: FieldElement>(Vec<F>); |
| 458 |
|
| 459 |
impl<F: FieldElement> From<Vec<F>> for VidpfWeight<F> { |
| 460 |
fn from(value: Vec<F>) -> Self { |
| 461 |
Self(value) |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
impl<F: FieldElement> VidpfValue for VidpfWeight<F> {} |
| 466 |
|
| 467 |
impl<F: FieldElement> IdpfValue for VidpfWeight<F> { |
| 468 |
/// The parameter determines the number of field elements in the vector. |
| 469 |
type ValueParameter = usize; |
| 470 |
|
| 471 |
fn generate<S: RngCore>(seed_stream: &mut S, length: &Self::ValueParameter) -> Self { |
| 472 |
Self( |
| 473 |
(0..*length) |
| 474 |
.map(|_| <F as IdpfValue>::generate(seed_stream, &())) |
| 475 |
.collect(), |
| 476 |
) |
| 477 |
} |
| 478 |
|
| 479 |
fn zero(length: &Self::ValueParameter) -> Self { |
| 480 |
Self((0..*length).map(|_| <F as IdpfValue>::zero(&())).collect()) |
| 481 |
} |
| 482 |
|
| 483 |
/// Panics if weight lengths are different. |
| 484 |
fn conditional_select(lhs: &Self, rhs: &Self, choice: Choice) -> Self { |
| 485 |
assert_eq!( |
| 486 |
lhs.0.len(), |
| 487 |
rhs.0.len(), |
| 488 |
"{}", |
| 489 |
VidpfError::InvalidWeightLength |
| 490 |
); |
| 491 |
|
| 492 |
Self( |
| 493 |
zip(&lhs.0, &rhs.0) |
| 494 |
.map(|(a, b)| <F as IdpfValue>::conditional_select(a, b, choice)) |
| 495 |
.collect(), |
| 496 |
) |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
impl<F: FieldElement> ConditionallyNegatable for VidpfWeight<F> { |
| 501 |
fn conditional_negate(&mut self, choice: Choice) { |
| 502 |
self.0.iter_mut().for_each(|a| a.conditional_negate(choice)); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
impl<F: FieldElement> Add for VidpfWeight<F> { |
| 507 |
type Output = Self; |
| 508 |
|
| 509 |
/// Panics if weight lengths are different. |
| 510 |
fn add(self, rhs: Self) -> Self::Output { |
| 511 |
assert_eq!( |
| 512 |
self.0.len(), |
| 513 |
rhs.0.len(), |
| 514 |
"{}", |
| 515 |
VidpfError::InvalidWeightLength |
| 516 |
); |
| 517 |
|
| 518 |
Self(zip(self.0, rhs.0).map(|(a, b)| a.add(b)).collect()) |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
impl<F: FieldElement> AddAssign for VidpfWeight<F> { |
| 523 |
/// Panics if weight lengths are different. |
| 524 |
fn add_assign(&mut self, rhs: Self) { |
| 525 |
assert_eq!( |
| 526 |
self.0.len(), |
| 527 |
rhs.0.len(), |
| 528 |
"{}", |
| 529 |
VidpfError::InvalidWeightLength |
| 530 |
); |
| 531 |
|
| 532 |
zip(&mut self.0, rhs.0).for_each(|(a, b)| a.add_assign(b)); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
impl<F: FieldElement> Sub for VidpfWeight<F> { |
| 537 |
type Output = Self; |
| 538 |
|
| 539 |
/// Panics if weight lengths are different. |
| 540 |
fn sub(self, rhs: Self) -> Self::Output { |
| 541 |
assert_eq!( |
| 542 |
self.0.len(), |
| 543 |
rhs.0.len(), |
| 544 |
"{}", |
| 545 |
VidpfError::InvalidWeightLength |
| 546 |
); |
| 547 |
|
| 548 |
Self(zip(self.0, rhs.0).map(|(a, b)| a.sub(b)).collect()) |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
impl<F: FieldElement> Encode for VidpfWeight<F> { |
| 553 |
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> { |
| 554 |
for e in &self.0 { |
| 555 |
F::encode(e, bytes)?; |
| 556 |
} |
| 557 |
Ok(()) |
| 558 |
} |
| 559 |
|
| 560 |
fn encoded_len(&self) -> Option<usize> { |
| 561 |
Some(self.0.len() * F::ENCODED_SIZE) |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
impl<F: FieldElement> ParameterizedDecode<<Self as IdpfValue>::ValueParameter> for VidpfWeight<F> { |
| 566 |
fn decode_with_param( |
| 567 |
decoding_parameter: &<Self as IdpfValue>::ValueParameter, |
| 568 |
bytes: &mut Cursor<&[u8]>, |
| 569 |
) -> Result<Self, CodecError> { |
| 570 |
let mut v = Vec::with_capacity(*decoding_parameter); |
| 571 |
for _ in 0..*decoding_parameter { |
| 572 |
v.push(F::decode_with_param(&(), bytes)?); |
| 573 |
} |
| 574 |
|
| 575 |
Ok(Self(v)) |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
#[cfg(test)] |
| 580 |
mod tests { |
| 581 |
use crate::field::Field128; |
| 582 |
|
| 583 |
use super::VidpfWeight; |
| 584 |
|
| 585 |
type TestWeight = VidpfWeight<Field128>; |
| 586 |
const TEST_WEIGHT_LEN: usize = 3; |
| 587 |
const TEST_NONCE_SIZE: usize = 16; |
| 588 |
const TEST_NONCE: &[u8; TEST_NONCE_SIZE] = b"Test Nonce VIDPF"; |
| 589 |
|
| 590 |
mod vidpf { |
| 591 |
use crate::{ |
| 592 |
idpf::IdpfValue, |
| 593 |
vidpf::{ |
| 594 |
Vidpf, VidpfError, VidpfEvalState, VidpfInput, VidpfKey, VidpfPublicShare, |
| 595 |
VidpfServerId, |
| 596 |
}, |
| 597 |
}; |
| 598 |
|
| 599 |
use super::{TestWeight, TEST_NONCE, TEST_NONCE_SIZE, TEST_WEIGHT_LEN}; |
| 600 |
|
| 601 |
fn vidpf_gen_setup( |
| 602 |
input: &VidpfInput, |
| 603 |
weight: &TestWeight, |
| 604 |
) -> ( |
| 605 |
Vidpf<TestWeight, TEST_NONCE_SIZE>, |
| 606 |
VidpfPublicShare<TestWeight>, |
| 607 |
[VidpfKey; 2], |
| 608 |
[u8; TEST_NONCE_SIZE], |
| 609 |
) { |
| 610 |
let vidpf = Vidpf::new(TEST_WEIGHT_LEN); |
| 611 |
let (public, keys) = vidpf.gen(input, weight, TEST_NONCE).unwrap(); |
| 612 |
(vidpf, public, keys, *TEST_NONCE) |
| 613 |
} |
| 614 |
|
| 615 |
#[test] |
| 616 |
fn gen_with_keys() { |
| 617 |
let input = VidpfInput::from_bytes(&[0xFF]); |
| 618 |
let weight = TestWeight::from(vec![21.into(), 22.into(), 23.into()]); |
| 619 |
let vidpf = Vidpf::new(TEST_WEIGHT_LEN); |
| 620 |
let keys_with_same_id = [ |
| 621 |
VidpfKey::gen(VidpfServerId::S0).unwrap(), |
| 622 |
VidpfKey::gen(VidpfServerId::S0).unwrap(), |
| 623 |
]; |
| 624 |
|
| 625 |
let err = vidpf |
| 626 |
.gen_with_keys(&keys_with_same_id, &input, &weight, TEST_NONCE) |
| 627 |
.unwrap_err(); |
| 628 |
|
| 629 |
assert_eq!(err.to_string(), VidpfError::SameKeyId.to_string()); |
| 630 |
} |
| 631 |
|
| 632 |
#[test] |
| 633 |
fn correctness_at_last_level() { |
| 634 |
let input = VidpfInput::from_bytes(&[0xFF]); |
| 635 |
let weight = TestWeight::from(vec![21.into(), 22.into(), 23.into()]); |
| 636 |
let (vidpf, public, [key_0, key_1], nonce) = vidpf_gen_setup(&input, &weight); |
| 637 |
|
| 638 |
let value_share_0 = vidpf.eval(&key_0, &public, &input, &nonce).unwrap(); |
| 639 |
let value_share_1 = vidpf.eval(&key_1, &public, &input, &nonce).unwrap(); |
| 640 |
|
| 641 |
assert_eq!( |
| 642 |
value_share_0.share + value_share_1.share, |
| 643 |
weight, |
| 644 |
"shares must add up to the expected weight", |
| 645 |
); |
| 646 |
|
| 647 |
assert_eq!( |
| 648 |
value_share_0.proof, value_share_1.proof, |
| 649 |
"proofs must be equal" |
| 650 |
); |
| 651 |
|
| 652 |
let bad_input = VidpfInput::from_bytes(&[0x00]); |
| 653 |
let zero = TestWeight::zero(&TEST_WEIGHT_LEN); |
| 654 |
let value_share_0 = vidpf.eval(&key_0, &public, &bad_input, &nonce).unwrap(); |
| 655 |
let value_share_1 = vidpf.eval(&key_1, &public, &bad_input, &nonce).unwrap(); |
| 656 |
|
| 657 |
assert_eq!( |
| 658 |
value_share_0.share + value_share_1.share, |
| 659 |
zero, |
| 660 |
"shares must add up to zero", |
| 661 |
); |
| 662 |
|
| 663 |
assert_eq!( |
| 664 |
value_share_0.proof, value_share_1.proof, |
| 665 |
"proofs must be equal" |
| 666 |
); |
| 667 |
} |
| 668 |
|
| 669 |
#[test] |
| 670 |
fn correctness_at_each_level() { |
| 671 |
let input = VidpfInput::from_bytes(&[0xFF]); |
| 672 |
let weight = TestWeight::from(vec![21.into(), 22.into(), 23.into()]); |
| 673 |
let (vidpf, public, keys, nonce) = vidpf_gen_setup(&input, &weight); |
| 674 |
|
| 675 |
assert_eval_at_each_level(&vidpf, &keys, &public, &input, &weight, &nonce); |
| 676 |
|
| 677 |
let bad_input = VidpfInput::from_bytes(&[0x00]); |
| 678 |
let zero = TestWeight::zero(&TEST_WEIGHT_LEN); |
| 679 |
|
| 680 |
assert_eval_at_each_level(&vidpf, &keys, &public, &bad_input, &zero, &nonce); |
| 681 |
} |
| 682 |
|
| 683 |
fn assert_eval_at_each_level( |
| 684 |
vidpf: &Vidpf<TestWeight, TEST_NONCE_SIZE>, |
| 685 |
[key_0, key_1]: &[VidpfKey; 2], |
| 686 |
public: &VidpfPublicShare<TestWeight>, |
| 687 |
input: &VidpfInput, |
| 688 |
weight: &TestWeight, |
| 689 |
nonce: &[u8; TEST_NONCE_SIZE], |
| 690 |
) { |
| 691 |
let mut state_0 = VidpfEvalState::init_from_key(key_0); |
| 692 |
let mut state_1 = VidpfEvalState::init_from_key(key_1); |
| 693 |
|
| 694 |
let n = input.len(); |
| 695 |
for level in 0..n { |
| 696 |
let share_0; |
| 697 |
let share_1; |
| 698 |
(state_0, share_0) = vidpf |
| 699 |
.eval_next(key_0.id, public, input, level, &state_0, nonce) |
| 700 |
.unwrap(); |
| 701 |
(state_1, share_1) = vidpf |
| 702 |
.eval_next(key_1.id, public, input, level, &state_1, nonce) |
| 703 |
.unwrap(); |
| 704 |
|
| 705 |
assert_eq!( |
| 706 |
share_0 + share_1, |
| 707 |
*weight, |
| 708 |
"shares must add up to the expected weight at the current level: {:?}", |
| 709 |
level |
| 710 |
); |
| 711 |
|
| 712 |
assert_eq!( |
| 713 |
state_0.proof, state_1.proof, |
| 714 |
"proofs must be equal at the current level: {:?}", |
| 715 |
level |
| 716 |
); |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
mod weight { |
| 722 |
use std::io::Cursor; |
| 723 |
use subtle::{Choice, ConditionallyNegatable}; |
| 724 |
|
| 725 |
use crate::{ |
| 726 |
codec::{Encode, ParameterizedDecode}, |
| 727 |
idpf::IdpfValue, |
| 728 |
vdaf::xof::{Seed, Xof, XofTurboShake128}, |
| 729 |
}; |
| 730 |
|
| 731 |
use super::{TestWeight, TEST_WEIGHT_LEN}; |
| 732 |
|
| 733 |
#[test] |
| 734 |
fn roundtrip_codec() { |
| 735 |
let weight = TestWeight::from(vec![21.into(), 22.into(), 23.into()]); |
| 736 |
|
| 737 |
let mut bytes = vec![]; |
| 738 |
weight.encode(&mut bytes).unwrap(); |
| 739 |
|
| 740 |
let expected_bytes = [ |
| 741 |
[vec![21], vec![0u8; 15]].concat(), |
| 742 |
[vec![22], vec![0u8; 15]].concat(), |
| 743 |
[vec![23], vec![0u8; 15]].concat(), |
| 744 |
] |
| 745 |
.concat(); |
| 746 |
|
| 747 |
assert_eq!(weight.encoded_len().unwrap(), expected_bytes.len()); |
| 748 |
// Check endianness of encoding |
| 749 |
assert_eq!(bytes, expected_bytes); |
| 750 |
|
| 751 |
let decoded = |
| 752 |
TestWeight::decode_with_param(&TEST_WEIGHT_LEN, &mut Cursor::new(&bytes)).unwrap(); |
| 753 |
assert_eq!(weight, decoded); |
| 754 |
} |
| 755 |
|
| 756 |
#[test] |
| 757 |
fn add_sub() { |
| 758 |
let [a, b] = compatible_weights(); |
| 759 |
let mut c = a.clone(); |
| 760 |
c += a.clone(); |
| 761 |
|
| 762 |
assert_eq!( |
| 763 |
(a.clone() + b.clone()) + (a.clone() - b.clone()), |
| 764 |
c, |
| 765 |
"a: {:?} b:{:?}", |
| 766 |
a, |
| 767 |
b |
| 768 |
); |
| 769 |
} |
| 770 |
|
| 771 |
#[test] |
| 772 |
fn conditional_negate() { |
| 773 |
let [a, _] = compatible_weights(); |
| 774 |
let mut c = a.clone(); |
| 775 |
c.conditional_negate(Choice::from(0)); |
| 776 |
let mut d = a.clone(); |
| 777 |
d.conditional_negate(Choice::from(1)); |
| 778 |
let zero = TestWeight::zero(&TEST_WEIGHT_LEN); |
| 779 |
|
| 780 |
assert_eq!(c + d, zero, "a: {:?}", a); |
| 781 |
} |
| 782 |
|
| 783 |
#[test] |
| 784 |
#[should_panic = "invalid weight length"] |
| 785 |
fn add_panics() { |
| 786 |
let [w0, w1] = incompatible_weights(); |
| 787 |
let _ = w0 + w1; |
| 788 |
} |
| 789 |
|
| 790 |
#[test] |
| 791 |
#[should_panic = "invalid weight length"] |
| 792 |
fn add_assign_panics() { |
| 793 |
let [mut w0, w1] = incompatible_weights(); |
| 794 |
w0 += w1; |
| 795 |
} |
| 796 |
|
| 797 |
#[test] |
| 798 |
#[should_panic = "invalid weight length"] |
| 799 |
fn sub_panics() { |
| 800 |
let [w0, w1] = incompatible_weights(); |
| 801 |
let _ = w0 - w1; |
| 802 |
} |
| 803 |
|
| 804 |
#[test] |
| 805 |
#[should_panic = "invalid weight length"] |
| 806 |
fn conditional_select_panics() { |
| 807 |
let [w0, w1] = incompatible_weights(); |
| 808 |
TestWeight::conditional_select(&w0, &w1, Choice::from(0)); |
| 809 |
} |
| 810 |
|
| 811 |
fn compatible_weights() -> [TestWeight; 2] { |
| 812 |
let mut xof = XofTurboShake128::seed_stream(&Seed(Default::default()), &[], &[]); |
| 813 |
[ |
| 814 |
TestWeight::generate(&mut xof, &TEST_WEIGHT_LEN), |
| 815 |
TestWeight::generate(&mut xof, &TEST_WEIGHT_LEN), |
| 816 |
] |
| 817 |
} |
| 818 |
|
| 819 |
fn incompatible_weights() -> [TestWeight; 2] { |
| 820 |
let mut xof = XofTurboShake128::seed_stream(&Seed(Default::default()), &[], &[]); |
| 821 |
[ |
| 822 |
TestWeight::generate(&mut xof, &TEST_WEIGHT_LEN), |
| 823 |
TestWeight::generate(&mut xof, &(2 * TEST_WEIGHT_LEN)), |
| 824 |
] |
| 825 |
} |
| 826 |
} |
| 827 |
} |