Skip to content

Commit ed949d7

Browse files
authored
Mastic clean up (#1187)
* mastic: Remove `verify_key` from tests that don't use it Some tests in the `mastic` module generate verification keys that aren't actually used by the test. Incidentally, the same tests were generating keys of the wrong length. * mastic: De-duplicate agg share length computation Add a function that computes the length of the aggregate share in field elements as a function of the aggregation parameter. * vidpf: Improve `VidpfPublicShare::encoded_len()` Avoid iterating over the weights to compute the length of the encoded public share; just take the length of the first weight and multiply by the number of correction words. This computation assumes the length of each weight is equal to the weight parameter at ever level of the VIDPF tree. This certainly is true, but add a test to validate this assumption anyway. * vidpf: Move `eval_prefix_tree_with_siblings()` to `impl<W: VidpfValue>` This method is currently implemented for `Vidpf<VidpfWeight<F>>`, but it applies to the more general `Vidpf<W>`. * vdaf: Remove `domain_separation_tag()` from `Vdaf` trait This method is used in Prio3 and Poplar1 for domain separation with the version of the document that specifies them. This version control is not applicable to future VDAFs defined by future documents. Remove the method from the trait and add it to implementations of `Prio3` and `Poplar1`. * vidpf: Rename `weight_parameter` to `weight_len` The associated type `ValueParameter` is likely always going to be a `usize` that expresses the length. In the future we might consider hardcoding this change in the API.
1 parent 48bf7c6 commit ed949d7

File tree

5 files changed

+138
-139
lines changed

5 files changed

+138
-139
lines changed

src/vdaf.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,6 @@ pub trait Vdaf: Clone + Debug {
197197
/// The number of Aggregators. The Client generates as many input shares as there are
198198
/// Aggregators.
199199
fn num_aggregators(&self) -> usize;
200-
201-
/// Generate the domain separation tag for this VDAF. The output is used for domain separation
202-
/// by the XOF.
203-
fn domain_separation_tag(&self, usage: u16) -> [u8; 8] {
204-
// Prefix is 8 bytes and defined by the spec. Copy these values in
205-
let mut dst = [0; 8];
206-
dst[0] = VERSION;
207-
dst[1] = 0; // algorithm class
208-
dst[2..6].clone_from_slice(self.algorithm_id().to_be_bytes().as_slice());
209-
dst[6..8].clone_from_slice(usage.to_be_bytes().as_slice());
210-
dst
211-
}
212200
}
213201

214202
/// The Client's role in the execution of a VDAF.

src/vdaf/mastic.rs

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ where
9494
bits,
9595
})
9696
}
97+
98+
fn agg_share_len(&self, agg_param: &MasticAggregationParam) -> usize {
99+
// The aggregate share consists of the counter and truncated weight for each candidate prefix.
100+
(1 + self.szk.typ.output_len()) * agg_param.level_and_prefixes.prefixes().len()
101+
}
97102
}
98103

99104
/// Mastic aggregation parameter.
@@ -158,7 +163,7 @@ where
158163
mastic: &Mastic<T, P, SEED_SIZE>,
159164
bytes: &mut Cursor<&[u8]>,
160165
) -> Result<Self, CodecError> {
161-
VidpfPublicShare::decode_with_param(&(mastic.bits, mastic.vidpf.weight_parameter), bytes)
166+
VidpfPublicShare::decode_with_param(&(mastic.bits, mastic.vidpf.weight_len), bytes)
162167
}
163168
}
164169

@@ -252,8 +257,7 @@ where
252257
(mastic, agg_param): &(&Mastic<T, P, SEED_SIZE>, &MasticAggregationParam),
253258
bytes: &mut Cursor<&[u8]>,
254259
) -> Result<Self, CodecError> {
255-
let len = (1 + mastic.szk.typ.output_len()) * agg_param.level_and_prefixes.prefixes().len();
256-
decode_fieldvec(len, bytes).map(AggregateShare)
260+
decode_fieldvec(mastic.agg_share_len(agg_param), bytes).map(AggregateShare)
257261
}
258262
}
259263

@@ -268,8 +272,7 @@ where
268272
(mastic, agg_param): &(&Mastic<T, P, SEED_SIZE>, &MasticAggregationParam),
269273
bytes: &mut Cursor<&[u8]>,
270274
) -> Result<Self, CodecError> {
271-
let len = (1 + mastic.szk.typ.output_len()) * agg_param.level_and_prefixes.prefixes().len();
272-
decode_fieldvec(len, bytes).map(OutputShare)
275+
decode_fieldvec(mastic.agg_share_len(agg_param), bytes).map(OutputShare)
273276
}
274277
}
275278

@@ -425,10 +428,10 @@ impl<'a, T: Type, P: Xof<SEED_SIZE>, const SEED_SIZE: usize>
425428
for MasticPrepareState<T::Field, SEED_SIZE>
426429
{
427430
fn decode_with_param(
428-
(mastic, agg_param): &(&Mastic<T, P, SEED_SIZE>, &MasticAggregationParam),
431+
decoder @ (mastic, agg_param): &(&Mastic<T, P, SEED_SIZE>, &MasticAggregationParam),
429432
bytes: &mut Cursor<&[u8]>,
430433
) -> Result<Self, CodecError> {
431-
let output_shares = MasticOutputShare::decode_with_param(&(*mastic, *agg_param), bytes)?;
434+
let output_shares = MasticOutputShare::decode_with_param(decoder, bytes)?;
432435
let szk_query_state = (mastic.szk.typ.joint_rand_len() > 0
433436
&& agg_param.require_weight_check)
434437
.then(|| Seed::decode(bytes))
@@ -774,14 +777,7 @@ where
774777
output_shares: M,
775778
) -> Result<MasticAggregateShare<T::Field>, VdafError> {
776779
let mut agg_share =
777-
MasticAggregateShare::<T::Field>::from(vec![
778-
T::Field::zero();
779-
(1 + self.szk.typ.output_len())
780-
* agg_param
781-
.level_and_prefixes
782-
.prefixes()
783-
.len()
784-
]);
780+
MasticAggregateShare::from(vec![T::Field::zero(); self.agg_share_len(agg_param)]);
785781
for output_share in output_shares.into_iter() {
786782
agg_share.accumulate(&output_share)?;
787783
}
@@ -803,10 +799,7 @@ where
803799
let num_prefixes = agg_param.level_and_prefixes.prefixes().len();
804800

805801
let AggregateShare(agg) = agg_shares.into_iter().try_fold(
806-
AggregateShare(vec![
807-
T::Field::zero();
808-
num_prefixes * (1 + self.szk.typ.output_len())
809-
]),
802+
AggregateShare(vec![T::Field::zero(); self.agg_share_len(agg_param)]),
810803
|mut agg, agg_share| {
811804
agg.merge(&agg_share)?;
812805
Result::<_, VdafError>::Ok(agg)
@@ -866,8 +859,6 @@ mod tests {
866859
let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sum_typ, 32).unwrap();
867860

868861
let mut nonce = [0u8; 16];
869-
let mut verify_key = [0u8; 16];
870-
thread_rng().fill(&mut verify_key[..]);
871862
thread_rng().fill(&mut nonce[..]);
872863

873864
let inputs = [
@@ -947,8 +938,6 @@ mod tests {
947938
let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sum_typ, 32).unwrap();
948939

949940
let mut nonce = [0u8; 16];
950-
let mut verify_key = [0u8; 16];
951-
thread_rng().fill(&mut verify_key[..]);
952941
thread_rng().fill(&mut nonce[..]);
953942

954943
let first_input = VidpfInput::from_bytes(&[15u8, 0u8, 1u8, 4u8][..]);
@@ -1000,8 +989,6 @@ mod tests {
1000989
let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sum_typ, 32).unwrap();
1001990

1002991
let mut nonce = [0u8; 16];
1003-
let mut verify_key = [0u8; 16];
1004-
thread_rng().fill(&mut verify_key[..]);
1005992
thread_rng().fill(&mut nonce[..]);
1006993

1007994
let first_input = VidpfInput::from_bytes(&[15u8, 0u8, 1u8, 4u8][..]);
@@ -1023,8 +1010,6 @@ mod tests {
10231010
let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, count, 32).unwrap();
10241011

10251012
let mut nonce = [0u8; 16];
1026-
let mut verify_key = [0u8; 16];
1027-
thread_rng().fill(&mut verify_key[..]);
10281013
thread_rng().fill(&mut nonce[..]);
10291014

10301015
let inputs = [
@@ -1102,8 +1087,6 @@ mod tests {
11021087
let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, count, 32).unwrap();
11031088

11041089
let mut nonce = [0u8; 16];
1105-
let mut verify_key = [0u8; 16];
1106-
thread_rng().fill(&mut verify_key[..]);
11071090
thread_rng().fill(&mut nonce[..]);
11081091
let first_input = VidpfInput::from_bytes(&[15u8, 0u8, 1u8, 4u8][..]);
11091092

@@ -1122,8 +1105,6 @@ mod tests {
11221105
let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, count, 32).unwrap();
11231106

11241107
let mut nonce = [0u8; 16];
1125-
let mut verify_key = [0u8; 16];
1126-
thread_rng().fill(&mut verify_key[..]);
11271108
thread_rng().fill(&mut nonce[..]);
11281109

11291110
let first_input = VidpfInput::from_bytes(&[15u8, 0u8, 1u8, 4u8][..]);
@@ -1144,8 +1125,6 @@ mod tests {
11441125
let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sumvec, 32).unwrap();
11451126

11461127
let mut nonce = [0u8; 16];
1147-
let mut verify_key = [0u8; 16];
1148-
thread_rng().fill(&mut verify_key[..]);
11491128
thread_rng().fill(&mut nonce[..]);
11501129

11511130
let inputs = [
@@ -1234,8 +1213,6 @@ mod tests {
12341213
let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sumvec, 32).unwrap();
12351214

12361215
let mut nonce = [0u8; 16];
1237-
let mut verify_key = [0u8; 16];
1238-
thread_rng().fill(&mut verify_key[..]);
12391216
thread_rng().fill(&mut nonce[..]);
12401217

12411218
let first_input = VidpfInput::from_bytes(&[15u8, 0u8, 1u8, 4u8][..]);
@@ -1265,8 +1242,6 @@ mod tests {
12651242
let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sumvec, 32).unwrap();
12661243

12671244
let mut nonce = [0u8; 16];
1268-
let mut verify_key = [0u8; 16];
1269-
thread_rng().fill(&mut verify_key[..]);
12701245
thread_rng().fill(&mut nonce[..]);
12711246

12721247
let first_input = VidpfInput::from_bytes(&[15u8, 0u8, 1u8, 4u8][..]);
@@ -1298,8 +1273,6 @@ mod tests {
12981273
let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sumvec, 32).unwrap();
12991274

13001275
let mut nonce = [0u8; 16];
1301-
let mut verify_key = [0u8; 16];
1302-
thread_rng().fill(&mut verify_key[..]);
13031276
thread_rng().fill(&mut nonce[..]);
13041277

13051278
let first_input = VidpfInput::from_bytes(&[15u8, 0u8, 1u8, 4u8][..]);
@@ -1323,8 +1296,6 @@ mod tests {
13231296
let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sumvec, 32).unwrap();
13241297

13251298
let mut nonce = [0u8; 16];
1326-
let mut verify_key = [0u8; 16];
1327-
thread_rng().fill(&mut verify_key[..]);
13281299
thread_rng().fill(&mut nonce[..]);
13291300

13301301
let first_input = VidpfInput::from_bytes(&[15u8, 0u8, 1u8, 4u8][..]);

src/vdaf/poplar1.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
prng::Prng,
1212
vdaf::{
1313
xof::{Seed, Xof, XofTurboShake128},
14-
Aggregatable, Aggregator, Client, Collector, PrepareTransition, Vdaf, VdafError,
14+
Aggregatable, Aggregator, Client, Collector, PrepareTransition, Vdaf, VdafError, VERSION,
1515
},
1616
};
1717
use rand_core::RngCore;
@@ -862,6 +862,18 @@ impl<P: Xof<SEED_SIZE>, const SEED_SIZE: usize> Vdaf for Poplar1<P, SEED_SIZE> {
862862
}
863863

864864
impl<P: Xof<SEED_SIZE>, const SEED_SIZE: usize> Poplar1<P, SEED_SIZE> {
865+
/// Generate the domain separation tag for this VDAF. The output is used for domain separation
866+
/// by the XOF.
867+
fn domain_separation_tag(&self, usage: u16) -> [u8; 8] {
868+
// Prefix is 8 bytes and defined by the spec. Copy these values in
869+
let mut dst = [0; 8];
870+
dst[0] = VERSION;
871+
dst[1] = 0; // algorithm class
872+
dst[2..6].clone_from_slice(self.algorithm_id().to_be_bytes().as_slice());
873+
dst[6..8].clone_from_slice(usage.to_be_bytes().as_slice());
874+
dst
875+
}
876+
865877
fn shard_with_random(
866878
&self,
867879
ctx: &[u8],

src/vdaf/prio3.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use crate::prng::Prng;
5454
use crate::vdaf::xof::{IntoFieldVec, Seed, Xof};
5555
use crate::vdaf::{
5656
Aggregatable, AggregateShare, Aggregator, Client, Collector, OutputShare, PrepareTransition,
57-
Share, ShareDecodingParameter, Vdaf, VdafError,
57+
Share, ShareDecodingParameter, Vdaf, VdafError, VERSION,
5858
};
5959
#[cfg(feature = "experimental")]
6060
use fixed::traits::Fixed;
@@ -548,6 +548,18 @@ where
548548
.into_field_vec(self.typ.query_rand_len() * self.num_proofs())
549549
}
550550

551+
/// Generate the domain separation tag for this VDAF. The output is used for domain separation
552+
/// by the XOF.
553+
fn domain_separation_tag(&self, usage: u16) -> [u8; 8] {
554+
// Prefix is 8 bytes and defined by the spec. Copy these values in
555+
let mut dst = [0; 8];
556+
dst[0] = VERSION;
557+
dst[1] = 0; // algorithm class
558+
dst[2..6].clone_from_slice(self.algorithm_id().to_be_bytes().as_slice());
559+
dst[6..8].clone_from_slice(usage.to_be_bytes().as_slice());
560+
dst
561+
}
562+
551563
fn random_size(&self) -> usize {
552564
if self.typ.joint_rand_len() == 0 {
553565
// One seed per helper (share, proof) pair, plus one seed for proving randomness

0 commit comments

Comments
 (0)