From 96b1c4c2f80c50cf612b72cdc7136b15ec7853a1 Mon Sep 17 00:00:00 2001 From: Hannah Davis Date: Tue, 13 Aug 2024 19:27:41 +0000 Subject: [PATCH] better encoding for SzkQueryState --- src/codec.rs | 97 -------------------------------------------------- src/flp/szk.rs | 8 ++--- src/vidpf.rs | 4 ++- 3 files changed, 7 insertions(+), 102 deletions(-) diff --git a/src/codec.rs b/src/codec.rs index fda71ac1b..98e6299ab 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -270,73 +270,6 @@ impl Encode for u64 { } } -impl Decode for [D; SIZE] { - fn decode(bytes: &mut Cursor<&[u8]>) -> Result { - let mut v = Vec::with_capacity(SIZE); - for _ in 0..SIZE { - v.push(D::decode(bytes)?); - } - Ok(v.try_into().expect("If the above for loop completes, then the vector will always contain exactly BUFFER_SIZE elements.")) - } -} - -impl Encode for [E; BUFFER_SIZE] { - fn encode(&self, bytes: &mut Vec) -> Result<(), CodecError> { - for input in self { - input.encode(bytes)? - } - Ok(()) - } - - fn encoded_len(&self) -> Option { - let mut total = 0; - for item in self { - total += item.encoded_len()? - } - Some(total) - } -} - -impl ParameterizedDecode for Vec { - fn decode_with_param(len: &usize, bytes: &mut Cursor<&[u8]>) -> Result { - let mut out = Vec::with_capacity(*len); - for _ in 0..*len { - out.push(::decode(bytes)?) - } - Ok(out) - } -} - -impl> ParameterizedDecode<(usize, P)> for Vec { - fn decode_with_param( - (len, decoding_parameter): &(usize, P), - bytes: &mut Cursor<&[u8]>, - ) -> Result { - let mut out = Vec::with_capacity(*len); - for _ in 0..*len { - out.push(::decode_with_param(decoding_parameter, bytes)?) - } - Ok(out) - } -} - -impl Encode for Vec { - fn encode(&self, bytes: &mut Vec) -> Result<(), CodecError> { - for input in self { - input.encode(bytes)? - } - Ok(()) - } - - fn encoded_len(&self) -> Option { - let mut total = 0; - for item in self { - total += item.encoded_len()? - } - Some(total) - } -} - /// Encode `items` into `bytes` as a [variable-length vector][1] with a maximum length of `0xff`. /// /// [1]: https://datatracker.ietf.org/doc/html/rfc8446#section-3.4 @@ -600,28 +533,6 @@ mod tests { assert_eq!(value, decoded); } - #[test] - fn roundtrip_vec() { - let value = vec![1u32, 2u32, 3u32, 4u32]; - let mut bytes = vec![]; - value.encode(&mut bytes).unwrap(); - assert_eq!(bytes.len(), 16); - assert_eq!(bytes, vec![0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4]); - let decoded = Vec::::decode_with_param(&4, &mut Cursor::new(&bytes)).unwrap(); - assert_eq!(value, decoded); - } - - #[test] - fn roundtrip_array() { - let value = [1u32, 2u32, 3u32, 4u32]; - let mut bytes = vec![]; - value.encode(&mut bytes).unwrap(); - assert_eq!(bytes.len(), 16); - assert_eq!(bytes, vec![0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4]); - let decoded = <[u32; 4]>::decode(&mut Cursor::new(&bytes)).unwrap(); - assert_eq!(value, decoded); - } - #[derive(Debug, Eq, PartialEq)] struct TestMessage { field_u8: u8, @@ -868,14 +779,6 @@ mod tests { 0u64.encoded_len().unwrap(), 0u64.get_encoded().unwrap().len() ); - assert_eq!( - [0u8; 7].encoded_len().unwrap(), - [0u8; 7].get_encoded().unwrap().len() - ); - assert_eq!( - vec![0u8; 7].encoded_len().unwrap(), - vec![0u8; 7].get_encoded().unwrap().len() - ); } #[test] diff --git a/src/flp/szk.rs b/src/flp/szk.rs index 42dbd74f5..842e31026 100644 --- a/src/flp/szk.rs +++ b/src/flp/szk.rs @@ -220,13 +220,13 @@ impl Encode for SzkQueryShare Option { Some( - self.flp_verifier.encoded_len()? + self.flp_verifier.len() * F::ENCODED_SIZE + match self.joint_rand_part_opt { Some(ref part) => part.encoded_len()?, None => 0, @@ -245,12 +245,12 @@ impl ParameterizedDecode<(bool if *requires_joint_rand { Ok(SzkQueryShare { joint_rand_part_opt: Some(Seed::::decode(bytes)?), - flp_verifier: Vec::::decode_with_param(verifier_len, bytes)?, + flp_verifier: decode_fieldvec(*verifier_len, bytes)?, }) } else { Ok(SzkQueryShare { joint_rand_part_opt: None, - flp_verifier: Vec::::decode_with_param(verifier_len, bytes)?, + flp_verifier: decode_fieldvec(*verifier_len, bytes)?, }) } } diff --git a/src/vidpf.rs b/src/vidpf.rs index 9de5b7973..039686b52 100644 --- a/src/vidpf.rs +++ b/src/vidpf.rs @@ -540,7 +540,9 @@ impl ParameterizedDecode<(usize, W::ValueParameter)> for VidpfPub } let mut cs = Vec::::with_capacity(*bits); for _ in 0..*bits { - cs.push(VidpfProof::decode(bytes)?); + let mut proof = [0u8; 32]; + bytes.read_exact(&mut proof)?; + cs.push(proof); } Ok(Self { cw, cs }) }