Debian Codegraph @

third_party/rust/prio/src/vdaf/prio2/test_vector.rs

raw
1 // SPDX-License-Identifier: MPL-2.0
2
3 //! Test vectors of serialized Prio inputs, enabling backward compatibility testing.
4
5 use crate::{field::FieldPrio2, vdaf::prio2::client::ClientError};
6 use serde::{Deserialize, Serialize};
7 use std::fmt::Debug;
8
9 /// Errors propagated by functions in this module.
10 #[derive(Debug, thiserror::Error)]
11 pub(crate) enum TestVectorError {
12 /// Error from Prio client
13 #[error("Prio client error {0}")]
14 Client(#[from] ClientError),
15 }
16
17 /// A test vector of serialized Priov2 inputs, along with a reference sum. The field is always
18 /// [`FieldPrio2`].
19 #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
20 pub(crate) struct Priov2TestVector {
21 /// Dimension (number of buckets) of the inputs
22 pub dimension: usize,
23 /// Decrypted shares of Priov2 format inputs for the "first" a.k.a. "PHA"
24 /// server. The inner `Vec`s are encrypted bytes.
25 #[serde(
26 serialize_with = "base64::serialize_bytes",
27 deserialize_with = "base64::deserialize_bytes"
28 )]
29 pub server_1_decrypted_shares: Vec<Vec<u8>>,
30 /// Decrypted share of Priov2 format inputs for the non-"first" a.k.a.
31 /// "facilitator" server.
32 #[serde(
33 serialize_with = "base64::serialize_bytes",
34 deserialize_with = "base64::deserialize_bytes"
35 )]
36 pub server_2_decrypted_shares: Vec<Vec<u8>>,
37 /// The sum over the inputs.
38 #[serde(
39 serialize_with = "base64::serialize_field",
40 deserialize_with = "base64::deserialize_field"
41 )]
42 pub reference_sum: Vec<FieldPrio2>,
43 }
44
45 mod base64 {
46 //! Custom serialization module used for some members of struct
47 //! `Priov2TestVector` so that byte slices are serialized as base64 strings
48 //! instead of an array of an array of integers when serializing to JSON.
49 //
50 // Thank you, Alice! https://users.rust-lang.org/t/serialize-a-vec-u8-to-json-as-base64/57781/2
51 use crate::{
52 codec::ParameterizedDecode,
53 field::{encode_fieldvec, FieldElement, FieldPrio2},
54 vdaf::{Share, ShareDecodingParameter},
55 };
56 use assert_matches::assert_matches;
57 use base64::{engine::Engine, prelude::BASE64_STANDARD};
58 use serde::{
59 de::Error as _, ser::Error as _, Deserialize, Deserializer, Serialize, Serializer,
60 };
61
62 pub fn serialize_bytes<S: Serializer>(v: &[Vec<u8>], s: S) -> Result<S::Ok, S::Error> {
63 let base64_vec = v
64 .iter()
65 .map(|bytes| BASE64_STANDARD.encode(bytes))
66 .collect();
67 <Vec<String>>::serialize(&base64_vec, s)
68 }
69
70 pub fn deserialize_bytes<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<Vec<u8>>, D::Error> {
71 <Vec<String>>::deserialize(d)?
72 .iter()
73 .map(|s| {
74 BASE64_STANDARD
75 .decode(s.as_bytes())
76 .map_err(D::Error::custom)
77 })
78 .collect()
79 }
80
81 pub fn serialize_field<S: Serializer>(v: &[FieldPrio2], s: S) -> Result<S::Ok, S::Error> {
82 let mut bytes = Vec::new();
83 encode_fieldvec(v, &mut bytes).map_err(S::Error::custom)?;
84 String::serialize(&BASE64_STANDARD.encode(&bytes), s)
85 }
86
87 pub fn deserialize_field<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<FieldPrio2>, D::Error> {
88 let bytes = BASE64_STANDARD
89 .decode(String::deserialize(d)?.as_bytes())
90 .map_err(D::Error::custom)?;
91 let decoding_parameter =
92 ShareDecodingParameter::<32>::Leader(bytes.len() / FieldPrio2::ENCODED_SIZE);
93 let share = Share::<FieldPrio2, 32>::get_decoded_with_param(&decoding_parameter, &bytes)
94 .map_err(D::Error::custom)?;
95 assert_matches!(share, Share::Leader(vec) => Ok(vec))
96 }
97 }