From ce10c4e01fb843c296d20697df844e7d1f4bcf0f Mon Sep 17 00:00:00 2001 From: Christopher Patton Date: Sun, 29 Dec 2024 12:09:21 -0500 Subject: [PATCH] mastic: Construct `Mastic` with a `Type` The current constructor takes an `Szk` and a `Vidpf`, which may have incompatible parameters. To Fix this, pass `Type` to the constructor and construct `Szk` and `Vidpf` ourselves. --- src/vdaf/mastic.rs | 67 +++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 49 deletions(-) diff --git a/src/vdaf/mastic.rs b/src/vdaf/mastic.rs index eccff793..8d5cfde7 100644 --- a/src/vdaf/mastic.rs +++ b/src/vdaf/mastic.rs @@ -54,14 +54,9 @@ where P: Xof, { /// Creates a new instance of Mastic, with a specific attribute length and weight type. - pub fn new( - algorithm_id: u32, - szk: Szk, - vidpf: Vidpf>, - bits: usize, - ) -> Self { - // TODO Avoid this assertion by constructing vidpf and szk from an FLP. - assert_eq!(vidpf.weight_parameter, szk.typ.input_len() + 1); + pub fn new(algorithm_id: u32, typ: T, bits: usize) -> Self { + let vidpf = Vidpf::new(typ.input_len() + 1); + let szk = Szk::new(typ, algorithm_id); Self { algorithm_id, szk, @@ -778,10 +773,11 @@ where #[cfg(test)] mod tests { use super::*; - use crate::field::Field128; + use crate::field::{Field128, Field64}; use crate::flp::gadgets::{Mul, ParallelSum}; use crate::flp::types::{Count, Sum, SumVec}; use crate::vdaf::test_utils::run_vdaf; + use crate::vdaf::xof::XofTurboShake128; use rand::{thread_rng, Rng}; const CTX_STR: &[u8] = b"mastic ctx"; @@ -791,11 +787,7 @@ mod tests { let algorithm_id = 6; let max_measurement = 29; let sum_typ = Sum::::new(max_measurement).unwrap(); - let encoded_meas_len = sum_typ.input_len(); - - let szk = Szk::new_turboshake128(sum_typ, algorithm_id); - let vidpf = Vidpf::>::new(encoded_meas_len + 1); - let mastic = Mastic::new(algorithm_id, szk, vidpf, 32); + let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sum_typ, 32); let mut nonce = [0u8; 16]; let mut verify_key = [0u8; 16]; @@ -876,11 +868,7 @@ mod tests { let algorithm_id = 6; let max_measurement = 29; let sum_typ = Sum::::new(max_measurement).unwrap(); - let encoded_meas_len = sum_typ.input_len(); - - let szk = Szk::new_turboshake128(sum_typ, algorithm_id); - let vidpf = Vidpf::>::new(encoded_meas_len + 1); - let mastic = Mastic::new(algorithm_id, szk, vidpf, 32); + let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sum_typ, 32); let mut nonce = [0u8; 16]; let mut verify_key = [0u8; 16]; @@ -933,10 +921,7 @@ mod tests { let algorithm_id = 6; let max_measurement = 29; let sum_typ = Sum::::new(max_measurement).unwrap(); - let encoded_meas_len = sum_typ.input_len(); - let szk = Szk::new_turboshake128(sum_typ, algorithm_id); - let vidpf = Vidpf::>::new(encoded_meas_len + 1); - let mastic = Mastic::new(algorithm_id, szk, vidpf, 32); + let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sum_typ, 32); let mut nonce = [0u8; 16]; let mut verify_key = [0u8; 16]; @@ -959,9 +944,7 @@ mod tests { fn test_mastic_count() { let algorithm_id = 6; let count = Count::::new(); - let szk = Szk::new_turboshake128(count, algorithm_id); - let vidpf = Vidpf::>::new(2); - let mastic = Mastic::new(algorithm_id, szk, vidpf, 32); + let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, count, 32); let mut nonce = [0u8; 16]; let mut verify_key = [0u8; 16]; @@ -1039,10 +1022,8 @@ mod tests { #[test] fn test_public_share_encoded_len() { let algorithm_id = 6; - let count = Count::::new(); - let szk = Szk::new_turboshake128(count, algorithm_id); - let vidpf = Vidpf::>::new(2); - let mastic = Mastic::new(algorithm_id, szk, vidpf, 32); + let count = Count::::new(); + let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, count, 32); let mut nonce = [0u8; 16]; let mut verify_key = [0u8; 16]; @@ -1061,10 +1042,8 @@ mod tests { #[test] fn test_public_share_roundtrip_count() { let algorithm_id = 6; - let count = Count::::new(); - let szk = Szk::new_turboshake128(count, algorithm_id); - let vidpf = Vidpf::>::new(2); - let mastic = Mastic::new(algorithm_id, szk, vidpf, 32); + let count = Count::::new(); + let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, count, 32); let mut nonce = [0u8; 16]; let mut verify_key = [0u8; 16]; @@ -1086,9 +1065,7 @@ mod tests { let algorithm_id = 6; let sumvec = SumVec::>>::new(5, 3, 3).unwrap(); - let szk = Szk::new_turboshake128(sumvec, algorithm_id); - let vidpf = Vidpf::>::new(16); - let mastic = Mastic::new(algorithm_id, szk, vidpf, 32); + let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sumvec, 32); let mut nonce = [0u8; 16]; let mut verify_key = [0u8; 16]; @@ -1178,9 +1155,7 @@ mod tests { let sumvec = SumVec::>>::new(5, 3, 3).unwrap(); let measurement = vec![1, 16, 0]; - let szk = Szk::new_turboshake128(sumvec, algorithm_id); - let vidpf = Vidpf::>::new(16); - let mastic = Mastic::new(algorithm_id, szk, vidpf, 32); + let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sumvec, 32); let mut nonce = [0u8; 16]; let mut verify_key = [0u8; 16]; @@ -1211,9 +1186,7 @@ mod tests { let sumvec = SumVec::>>::new(5, 3, 3).unwrap(); let measurement = vec![1, 16, 0]; - let szk = Szk::new_turboshake128(sumvec, algorithm_id); - let vidpf = Vidpf::>::new(16); - let mastic = Mastic::new(algorithm_id, szk, vidpf, 32); + let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sumvec, 32); let mut nonce = [0u8; 16]; let mut verify_key = [0u8; 16]; @@ -1246,9 +1219,7 @@ mod tests { let sumvec = SumVec::>>::new(5, 3, 3).unwrap(); let measurement = vec![1, 16, 0]; - let szk = Szk::new_turboshake128(sumvec, algorithm_id); - let vidpf = Vidpf::>::new(16); - let mastic = Mastic::new(algorithm_id, szk, vidpf, 32); + let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sumvec, 32); let mut nonce = [0u8; 16]; let mut verify_key = [0u8; 16]; @@ -1273,9 +1244,7 @@ mod tests { let sumvec = SumVec::>>::new(5, 3, 3).unwrap(); let measurement = vec![1, 16, 0]; - let szk = Szk::new_turboshake128(sumvec, algorithm_id); - let vidpf = Vidpf::>::new(16); - let mastic = Mastic::new(algorithm_id, szk, vidpf, 32); + let mastic = Mastic::<_, XofTurboShake128, 32>::new(algorithm_id, sumvec, 32); let mut nonce = [0u8; 16]; let mut verify_key = [0u8; 16];