Skip to content

Commit

Permalink
better encoding for SzkQueryState
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannah Davis committed Aug 13, 2024
1 parent f952b4e commit 96b1c4c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 102 deletions.
97 changes: 0 additions & 97 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,73 +270,6 @@ impl Encode for u64 {
}
}

impl<D: Decode + std::fmt::Debug, const SIZE: usize> Decode for [D; SIZE] {
fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> {
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<E: Encode, const BUFFER_SIZE: usize> Encode for [E; BUFFER_SIZE] {
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> {
for input in self {
input.encode(bytes)?
}
Ok(())
}

fn encoded_len(&self) -> Option<usize> {
let mut total = 0;
for item in self {
total += item.encoded_len()?
}
Some(total)
}
}

impl<D: Decode> ParameterizedDecode<usize> for Vec<D> {
fn decode_with_param(len: &usize, bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> {
let mut out = Vec::with_capacity(*len);
for _ in 0..*len {
out.push(<D>::decode(bytes)?)
}
Ok(out)
}
}

impl<P, D: ParameterizedDecode<P>> ParameterizedDecode<(usize, P)> for Vec<D> {
fn decode_with_param(
(len, decoding_parameter): &(usize, P),
bytes: &mut Cursor<&[u8]>,
) -> Result<Self, CodecError> {
let mut out = Vec::with_capacity(*len);
for _ in 0..*len {
out.push(<D>::decode_with_param(decoding_parameter, bytes)?)
}
Ok(out)
}
}

impl<E: Encode> Encode for Vec<E> {
fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> {
for input in self {
input.encode(bytes)?
}
Ok(())
}

fn encoded_len(&self) -> Option<usize> {
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
Expand Down Expand Up @@ -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::<u32>::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,
Expand Down Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions src/flp/szk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ impl<F: FieldElement, const SEED_SIZE: usize> Encode for SzkQueryShare<F, SEED_S
part.encode(bytes)?
};

self.flp_verifier.encode(bytes)?;
encode_fieldvec(&self.flp_verifier, bytes)?;
Ok(())
}

fn encoded_len(&self) -> Option<usize> {
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,
Expand All @@ -245,12 +245,12 @@ impl<F: FieldElement + Decode, const SEED_SIZE: usize> ParameterizedDecode<(bool
if *requires_joint_rand {
Ok(SzkQueryShare {
joint_rand_part_opt: Some(Seed::<SEED_SIZE>::decode(bytes)?),
flp_verifier: Vec::<F>::decode_with_param(verifier_len, bytes)?,
flp_verifier: decode_fieldvec(*verifier_len, bytes)?,
})
} else {
Ok(SzkQueryShare {
joint_rand_part_opt: None,
flp_verifier: Vec::<F>::decode_with_param(verifier_len, bytes)?,
flp_verifier: decode_fieldvec(*verifier_len, bytes)?,
})
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/vidpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,9 @@ impl<W: VidpfValue> ParameterizedDecode<(usize, W::ValueParameter)> for VidpfPub
}
let mut cs = Vec::<VidpfProof>::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 })
}
Expand Down

0 comments on commit 96b1c4c

Please sign in to comment.