| 1 |
// Copyright (c) 2020 Apple Inc. |
| 2 |
// SPDX-License-Identifier: MPL-2.0 |
| 3 |
|
| 4 |
//! Primitives for the Prio2 client. |
| 5 |
|
| 6 |
use crate::{ |
| 7 |
codec::CodecError, |
| 8 |
field::FftFriendlyFieldElement, |
| 9 |
polynomial::{poly_fft, PolyAuxMemory}, |
| 10 |
prng::{Prng, PrngError}, |
| 11 |
vdaf::{ |
| 12 |
xof::{Seed, SeedStreamAes128}, |
| 13 |
VdafError, |
| 14 |
}, |
| 15 |
}; |
| 16 |
|
| 17 |
use std::convert::TryFrom; |
| 18 |
|
| 19 |
/// Errors that might be emitted by the client. |
| 20 |
#[derive(Debug, thiserror::Error)] |
| 21 |
pub(crate) enum ClientError { |
| 22 |
/// PRNG error |
| 23 |
#[error("prng error: {0}")] |
| 24 |
Prng(#[from] PrngError), |
| 25 |
/// VDAF error |
| 26 |
#[error("vdaf error: {0}")] |
| 27 |
Vdaf(#[from] VdafError), |
| 28 |
/// failure when calling getrandom(). |
| 29 |
#[error("getrandom: {0}")] |
| 30 |
GetRandom(#[from] getrandom::Error), |
| 31 |
} |
| 32 |
|
| 33 |
/// Serialization errors |
| 34 |
#[derive(Debug, thiserror::Error)] |
| 35 |
pub enum SerializeError { |
| 36 |
/// Emitted by `unpack_proof[_mut]` if the serialized share+proof has the wrong length |
| 37 |
#[error("serialized input has wrong length")] |
| 38 |
UnpackInputSizeMismatch, |
| 39 |
/// Codec error. |
| 40 |
#[error(transparent)] |
| 41 |
Codec(#[from] CodecError), |
| 42 |
} |
| 43 |
|
| 44 |
#[derive(Debug)] |
| 45 |
pub(crate) struct ClientMemory<F> { |
| 46 |
prng: Prng<F, SeedStreamAes128>, |
| 47 |
points_f: Vec<F>, |
| 48 |
points_g: Vec<F>, |
| 49 |
evals_f: Vec<F>, |
| 50 |
evals_g: Vec<F>, |
| 51 |
poly_mem: PolyAuxMemory<F>, |
| 52 |
} |
| 53 |
|
| 54 |
impl<F: FftFriendlyFieldElement> ClientMemory<F> { |
| 55 |
pub(crate) fn new(dimension: usize) -> Result<Self, VdafError> { |
| 56 |
let n = (dimension + 1).next_power_of_two(); |
| 57 |
if let Ok(size) = F::Integer::try_from(2 * n) { |
| 58 |
if size > F::generator_order() { |
| 59 |
return Err(VdafError::Uncategorized( |
| 60 |
"input size exceeds field capacity".into(), |
| 61 |
)); |
| 62 |
} |
| 63 |
} else { |
| 64 |
return Err(VdafError::Uncategorized( |
| 65 |
"input size exceeds field capacity".into(), |
| 66 |
)); |
| 67 |
} |
| 68 |
|
| 69 |
Ok(Self { |
| 70 |
prng: Prng::from_prio2_seed(Seed::<32>::generate()?.as_ref()), |
| 71 |
points_f: vec![F::zero(); n], |
| 72 |
points_g: vec![F::zero(); n], |
| 73 |
evals_f: vec![F::zero(); 2 * n], |
| 74 |
evals_g: vec![F::zero(); 2 * n], |
| 75 |
poly_mem: PolyAuxMemory::new(n), |
| 76 |
}) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl<F: FftFriendlyFieldElement> ClientMemory<F> { |
| 81 |
pub(crate) fn prove_with<G>(&mut self, dimension: usize, init_function: G) -> Vec<F> |
| 82 |
where |
| 83 |
G: FnOnce(&mut [F]), |
| 84 |
{ |
| 85 |
let mut proof = vec![F::zero(); proof_length(dimension)]; |
| 86 |
// unpack one long vector to different subparts |
| 87 |
let unpacked = unpack_proof_mut(&mut proof, dimension).unwrap(); |
| 88 |
// initialize the data part |
| 89 |
init_function(unpacked.data); |
| 90 |
// fill in the rest |
| 91 |
construct_proof( |
| 92 |
unpacked.data, |
| 93 |
dimension, |
| 94 |
unpacked.f0, |
| 95 |
unpacked.g0, |
| 96 |
unpacked.h0, |
| 97 |
unpacked.points_h_packed, |
| 98 |
self, |
| 99 |
); |
| 100 |
|
| 101 |
proof |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/// Returns the number of field elements in the proof for given dimension of |
| 106 |
/// data elements |
| 107 |
/// |
| 108 |
/// Proof is a vector, where the first `dimension` elements are the data |
| 109 |
/// elements, the next 3 elements are the zero terms for polynomials f, g and h |
| 110 |
/// and the remaining elements are non-zero points of h(x). |
| 111 |
pub(crate) fn proof_length(dimension: usize) -> usize { |
| 112 |
// number of data items + number of zero terms + N |
| 113 |
dimension + 3 + (dimension + 1).next_power_of_two() |
| 114 |
} |
| 115 |
|
| 116 |
/// Unpacked proof with subcomponents |
| 117 |
#[derive(Debug)] |
| 118 |
pub(crate) struct UnpackedProof<'a, F: FftFriendlyFieldElement> { |
| 119 |
/// Data |
| 120 |
pub data: &'a [F], |
| 121 |
/// Zeroth coefficient of polynomial f |
| 122 |
pub f0: &'a F, |
| 123 |
/// Zeroth coefficient of polynomial g |
| 124 |
pub g0: &'a F, |
| 125 |
/// Zeroth coefficient of polynomial h |
| 126 |
pub h0: &'a F, |
| 127 |
/// Non-zero points of polynomial h |
| 128 |
pub points_h_packed: &'a [F], |
| 129 |
} |
| 130 |
|
| 131 |
/// Unpacked proof with mutable subcomponents |
| 132 |
#[derive(Debug)] |
| 133 |
pub(crate) struct UnpackedProofMut<'a, F: FftFriendlyFieldElement> { |
| 134 |
/// Data |
| 135 |
pub data: &'a mut [F], |
| 136 |
/// Zeroth coefficient of polynomial f |
| 137 |
pub f0: &'a mut F, |
| 138 |
/// Zeroth coefficient of polynomial g |
| 139 |
pub g0: &'a mut F, |
| 140 |
/// Zeroth coefficient of polynomial h |
| 141 |
pub h0: &'a mut F, |
| 142 |
/// Non-zero points of polynomial h |
| 143 |
pub points_h_packed: &'a mut [F], |
| 144 |
} |
| 145 |
|
| 146 |
/// Unpacks the proof vector into subcomponents |
| 147 |
pub(crate) fn unpack_proof<F: FftFriendlyFieldElement>( |
| 148 |
proof: &[F], |
| 149 |
dimension: usize, |
| 150 |
) -> Result<UnpackedProof<F>, SerializeError> { |
| 151 |
// check the proof length |
| 152 |
if proof.len() != proof_length(dimension) { |
| 153 |
return Err(SerializeError::UnpackInputSizeMismatch); |
| 154 |
} |
| 155 |
// split share into components |
| 156 |
let (data, rest) = proof.split_at(dimension); |
| 157 |
if let ([f0, g0, h0], points_h_packed) = rest.split_at(3) { |
| 158 |
Ok(UnpackedProof { |
| 159 |
data, |
| 160 |
f0, |
| 161 |
g0, |
| 162 |
h0, |
| 163 |
points_h_packed, |
| 164 |
}) |
| 165 |
} else { |
| 166 |
Err(SerializeError::UnpackInputSizeMismatch) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/// Unpacks a mutable proof vector into mutable subcomponents |
| 171 |
pub(crate) fn unpack_proof_mut<F: FftFriendlyFieldElement>( |
| 172 |
proof: &mut [F], |
| 173 |
dimension: usize, |
| 174 |
) -> Result<UnpackedProofMut<F>, SerializeError> { |
| 175 |
// check the share length |
| 176 |
if proof.len() != proof_length(dimension) { |
| 177 |
return Err(SerializeError::UnpackInputSizeMismatch); |
| 178 |
} |
| 179 |
// split share into components |
| 180 |
let (data, rest) = proof.split_at_mut(dimension); |
| 181 |
if let ([f0, g0, h0], points_h_packed) = rest.split_at_mut(3) { |
| 182 |
Ok(UnpackedProofMut { |
| 183 |
data, |
| 184 |
f0, |
| 185 |
g0, |
| 186 |
h0, |
| 187 |
points_h_packed, |
| 188 |
}) |
| 189 |
} else { |
| 190 |
Err(SerializeError::UnpackInputSizeMismatch) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
fn interpolate_and_evaluate_at_2n<F: FftFriendlyFieldElement>( |
| 195 |
n: usize, |
| 196 |
points_in: &[F], |
| 197 |
evals_out: &mut [F], |
| 198 |
mem: &mut PolyAuxMemory<F>, |
| 199 |
) { |
| 200 |
// interpolate through roots of unity |
| 201 |
poly_fft( |
| 202 |
&mut mem.coeffs, |
| 203 |
points_in, |
| 204 |
&mem.roots_n_inverted, |
| 205 |
n, |
| 206 |
true, |
| 207 |
&mut mem.fft_memory, |
| 208 |
); |
| 209 |
// evaluate at 2N roots of unity |
| 210 |
poly_fft( |
| 211 |
evals_out, |
| 212 |
&mem.coeffs, |
| 213 |
&mem.roots_2n, |
| 214 |
2 * n, |
| 215 |
false, |
| 216 |
&mut mem.fft_memory, |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
/// Proof construction |
| 221 |
/// |
| 222 |
/// Based on Theorem 2.3.3 from Henry Corrigan-Gibbs' dissertation |
| 223 |
/// This constructs the output \pi by doing the necessesary calculations |
| 224 |
fn construct_proof<F: FftFriendlyFieldElement>( |
| 225 |
data: &[F], |
| 226 |
dimension: usize, |
| 227 |
f0: &mut F, |
| 228 |
g0: &mut F, |
| 229 |
h0: &mut F, |
| 230 |
points_h_packed: &mut [F], |
| 231 |
mem: &mut ClientMemory<F>, |
| 232 |
) { |
| 233 |
let n = (dimension + 1).next_power_of_two(); |
| 234 |
|
| 235 |
// set zero terms to random |
| 236 |
*f0 = mem.prng.get(); |
| 237 |
*g0 = mem.prng.get(); |
| 238 |
mem.points_f[0] = *f0; |
| 239 |
mem.points_g[0] = *g0; |
| 240 |
|
| 241 |
// set zero term for the proof polynomial |
| 242 |
*h0 = *f0 * *g0; |
| 243 |
|
| 244 |
// set f_i = data_(i - 1) |
| 245 |
// set g_i = f_i - 1 |
| 246 |
for ((f_coeff, g_coeff), data_val) in mem.points_f[1..1 + dimension] |
| 247 |
.iter_mut() |
| 248 |
.zip(mem.points_g[1..1 + dimension].iter_mut()) |
| 249 |
.zip(data[..dimension].iter()) |
| 250 |
{ |
| 251 |
*f_coeff = *data_val; |
| 252 |
*g_coeff = *data_val - F::one(); |
| 253 |
} |
| 254 |
|
| 255 |
// interpolate and evaluate at roots of unity |
| 256 |
interpolate_and_evaluate_at_2n(n, &mem.points_f, &mut mem.evals_f, &mut mem.poly_mem); |
| 257 |
interpolate_and_evaluate_at_2n(n, &mem.points_g, &mut mem.evals_g, &mut mem.poly_mem); |
| 258 |
|
| 259 |
// calculate the proof polynomial as evals_f(r) * evals_g(r) |
| 260 |
// only add non-zero points |
| 261 |
let mut j: usize = 0; |
| 262 |
let mut i: usize = 1; |
| 263 |
while i < 2 * n { |
| 264 |
points_h_packed[j] = mem.evals_f[i] * mem.evals_g[i]; |
| 265 |
j += 1; |
| 266 |
i += 2; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
#[cfg(test)] |
| 271 |
mod tests { |
| 272 |
use assert_matches::assert_matches; |
| 273 |
|
| 274 |
use crate::{ |
| 275 |
field::{Field64, FieldPrio2}, |
| 276 |
vdaf::prio2::client::{proof_length, unpack_proof, unpack_proof_mut, SerializeError}, |
| 277 |
}; |
| 278 |
|
| 279 |
#[test] |
| 280 |
fn test_unpack_share_mut() { |
| 281 |
let dim = 15; |
| 282 |
let len = proof_length(dim); |
| 283 |
|
| 284 |
let mut share = vec![FieldPrio2::from(0); len]; |
| 285 |
let unpacked = unpack_proof_mut(&mut share, dim).unwrap(); |
| 286 |
*unpacked.f0 = FieldPrio2::from(12); |
| 287 |
assert_eq!(share[dim], 12); |
| 288 |
|
| 289 |
let mut short_share = vec![FieldPrio2::from(0); len - 1]; |
| 290 |
assert_matches!( |
| 291 |
unpack_proof_mut(&mut short_share, dim), |
| 292 |
Err(SerializeError::UnpackInputSizeMismatch) |
| 293 |
); |
| 294 |
} |
| 295 |
|
| 296 |
#[test] |
| 297 |
fn test_unpack_share() { |
| 298 |
let dim = 15; |
| 299 |
let len = proof_length(dim); |
| 300 |
|
| 301 |
let share = vec![Field64::from(0); len]; |
| 302 |
unpack_proof(&share, dim).unwrap(); |
| 303 |
|
| 304 |
let short_share = vec![Field64::from(0); len - 1]; |
| 305 |
assert_matches!( |
| 306 |
unpack_proof(&short_share, dim), |
| 307 |
Err(SerializeError::UnpackInputSizeMismatch) |
| 308 |
); |
| 309 |
} |
| 310 |
} |