From 40c44e3bc19f71e8b249110eb4d7cf21357a2e67 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 8 Jul 2024 12:25:06 +0200 Subject: [PATCH] Fix IetfProof deserialization --- src/ietf.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/ietf.rs b/src/ietf.rs index fe698a3..e7db65b 100644 --- a/src/ietf.rs +++ b/src/ietf.rs @@ -26,12 +26,17 @@ impl CanonicalSerialize for Proof { mut writer: W, _compress_always: ark_serialize::Compress, ) -> Result<(), ark_serialize::SerializationError> { - let buf = utils::encode_scalar::(&self.c); - if buf.len() < S::CHALLENGE_LEN { + let c_buf = utils::encode_scalar::(&self.c); + if c_buf.len() < S::CHALLENGE_LEN { // Encoded scalar length must be at least S::CHALLENGE_LEN return Err(ark_serialize::SerializationError::NotEnoughSpace); } - writer.write_all(&buf[..S::CHALLENGE_LEN])?; + let buf = if S::Codec::BIG_ENDIAN { + &c_buf[c_buf.len() - S::CHALLENGE_LEN..] + } else { + &c_buf[..S::CHALLENGE_LEN] + }; + writer.write_all(buf)?; self.s.serialize_compressed(&mut writer)?; Ok(()) } @@ -47,11 +52,11 @@ impl CanonicalDeserialize for Proof { _compress_always: ark_serialize::Compress, validate: ark_serialize::Validate, ) -> Result { - let c = as CanonicalDeserialize>::deserialize_with_mode( - &mut reader, - ark_serialize::Compress::No, - validate, - )?; + let mut c_buf = vec![0; S::CHALLENGE_LEN]; + if reader.read_exact(&mut c_buf[..]).is_err() { + return Err(ark_serialize::SerializationError::InvalidData); + } + let c = utils::decode_scalar::(&c_buf); let s = as CanonicalDeserialize>::deserialize_with_mode( &mut reader, ark_serialize::Compress::No,