diff --git a/plonky2/src/util/serialization.rs b/plonky2/src/util/serialization.rs index 1cdfc39c..5291ba00 100644 --- a/plonky2/src/util/serialization.rs +++ b/plonky2/src/util/serialization.rs @@ -48,15 +48,15 @@ impl Size for Vec { } } -/// +/// Reading pub trait Read { - /// + /// Error Type type Error; - /// + /// Reads exactly the length of `bytes` from `self` and writes it to `bytes`. fn read_exact(&mut self, bytes: &mut [u8]) -> Result<(), Self::Error>; - /// + /// Reads a `u8` value from `self`. #[inline] fn read_u8(&mut self) -> Result { let mut buf = [0; core::mem::size_of::()]; @@ -64,7 +64,7 @@ pub trait Read { Ok(buf[0]) } - /// + /// Reads a `u32` value from `self`. #[inline] fn read_u32(&mut self) -> Result { let mut buf = [0; core::mem::size_of::()]; @@ -72,7 +72,7 @@ pub trait Read { Ok(u32::from_le_bytes(buf)) } - /// + /// Reads a element from the field `F` with size less than `2^64` from `self.` #[inline] fn read_field(&mut self) -> Result where @@ -83,11 +83,22 @@ pub trait Read { Ok(F::from_canonical_u64(u64::from_le_bytes(buf))) } - /// + /// Reads a vector of elements from the field `F` from `self`. + #[inline] + fn read_field_vec(&mut self, length: usize) -> Result, Self::Error> + where + F: Field64, + { + (0..length) + .map(|_| self.read_field()) + .collect::, _>>() + } + + /// Reads an element from the field extension of `F` from `self.` #[inline] fn read_field_ext(&mut self) -> Result where - F: RichField + Extendable, + F: Field64 + Extendable, { let mut arr = [F::ZERO; D]; for a in arr.iter_mut() { @@ -98,7 +109,19 @@ pub trait Read { )) } - /// + /// Reads a vector of elements from the field extension of `F` from `self`. + #[inline] + fn read_field_ext_vec( + &mut self, + length: usize, + ) -> Result, Self::Error> + where + F: RichField + Extendable, + { + (0..length).map(|_| self.read_field_ext::()).collect() + } + + /// Reads a hash value from `self`. #[inline] fn read_hash(&mut self) -> Result where @@ -110,7 +133,7 @@ pub trait Read { Ok(H::Hash::from_bytes(&buf)) } - /// + /// Reads a value of type [`MerkleCap`] from `self` with the given `cap_height`. #[inline] fn read_merkle_cap(&mut self, cap_height: usize) -> Result, Self::Error> where @@ -125,30 +148,7 @@ pub trait Read { )) } - /// - #[inline] - fn read_field_vec(&mut self, length: usize) -> Result, Self::Error> - where - F: Field64, - { - (0..length) - .map(|_| self.read_field()) - .collect::, _>>() - } - - /// - #[inline] - fn read_field_ext_vec( - &mut self, - length: usize, - ) -> Result, Self::Error> - where - F: RichField + Extendable, - { - (0..length).map(|_| self.read_field_ext::()).collect() - } - - /// + /// Reads a value of type [`OpeningSet`] from `self` with the given `common_data`. #[inline] fn read_opening_set( &mut self, @@ -180,7 +180,7 @@ pub trait Read { }) } - /// + /// Reads a value of type [`MerkleProof`] from `self`. #[inline] fn read_merkle_proof(&mut self) -> Result, Self::Error> where @@ -191,11 +191,11 @@ pub trait Read { Ok(MerkleProof { siblings: (0..length) .map(|_| self.read_hash::()) - .collect::, _>>()?, + .collect::>()?, }) } - /// + /// Reads a value of type [`FriInitialTreeProof`] from `self` with the given `common_data`. #[inline] fn read_fri_initial_proof( &mut self, @@ -232,7 +232,8 @@ pub trait Read { Ok(FriInitialTreeProof { evals_proofs }) } - /// + /// Reads a value of type [`FriQueryStep`] from `self` with the given `arity` and `compressed` + /// flag. #[inline] fn read_fri_query_step( &mut self, @@ -251,7 +252,7 @@ pub trait Read { }) } - /// + /// Reads a vector of [`FriQueryRound`]s from `self` with `common_data`. #[inline] fn read_fri_query_rounds( &mut self, @@ -279,7 +280,7 @@ pub trait Read { Ok(fqrs) } - /// + /// Reads a value of type [`FriProof`] from `self` with `common_data`. #[inline] fn read_fri_proof( &mut self, @@ -306,7 +307,7 @@ pub trait Read { }) } - /// + /// Reads a value of type [`Proof`] from `self` with `common_data`. #[inline] fn read_proof( &mut self, @@ -331,7 +332,7 @@ pub trait Read { }) } - /// + /// Reads a value of type [`ProofWithPublicInputs`] from `self` with `common_data`. #[inline] fn read_proof_with_public_inputs( &mut self, @@ -352,7 +353,7 @@ pub trait Read { }) } - /// + /// Reads a value of type [`CompressedFriQueryRounds`] from `self` with `common_data`. #[inline] fn read_compressed_fri_query_rounds( &mut self, @@ -400,7 +401,7 @@ pub trait Read { }) } - /// + /// Reads a value of type [`CompressedFriProof`] from `self` with `common_data`. #[inline] fn read_compressed_fri_proof( &mut self, @@ -427,7 +428,7 @@ pub trait Read { }) } - /// + /// Reads a value of type [`CompressedProof`] from `self` with `common_data`. #[inline] fn read_compressed_proof( &mut self, @@ -452,7 +453,7 @@ pub trait Read { }) } - /// + /// Reads a value of type [`CompressedProofWithPublicInputs`] from `self` with `common_data`. #[inline] fn read_compressed_proof_with_public_inputs( &mut self, @@ -474,27 +475,27 @@ pub trait Read { } } -/// +/// Writing pub trait Write { - /// + /// Error Type type Error; - /// + /// Writes all `bytes` to `self`. fn write_all(&mut self, bytes: &[u8]) -> Result<(), Self::Error>; - /// + /// Writes a byte `x` to `self`. #[inline] fn write_u8(&mut self, x: u8) -> Result<(), Self::Error> { self.write_all(&[x]) } - /// + /// Writes a word `x` to `self.` #[inline] fn write_u32(&mut self, x: u32) -> Result<(), Self::Error> { self.write_all(&x.to_le_bytes()) } - /// + /// Writes an element `x` from the field `F` to `self`. #[inline] fn write_field(&mut self, x: F) -> Result<(), Self::Error> where @@ -503,42 +504,7 @@ pub trait Write { self.write_all(&x.to_canonical_u64().to_le_bytes()) } - /// - #[inline] - fn write_field_ext(&mut self, x: F::Extension) -> Result<(), Self::Error> - where - F: RichField + Extendable, - { - for &a in &x.to_basefield_array() { - self.write_field(a)?; - } - Ok(()) - } - - /// - #[inline] - fn write_hash(&mut self, h: H::Hash) -> Result<(), Self::Error> - where - F: RichField, - H: Hasher, - { - self.write_all(&h.to_bytes()) - } - - /// - #[inline] - fn write_merkle_cap(&mut self, cap: &MerkleCap) -> Result<(), Self::Error> - where - F: RichField, - H: Hasher, - { - for &a in &cap.0 { - self.write_hash::(a)?; - } - Ok(()) - } - - /// + /// Writes a vector of elements #[inline] fn write_field_vec(&mut self, v: &[F]) -> Result<(), Self::Error> where @@ -550,6 +516,18 @@ pub trait Write { Ok(()) } + /// Writes an element `x` from the field extension of `F` to `self`. + #[inline] + fn write_field_ext(&mut self, x: F::Extension) -> Result<(), Self::Error> + where + F: RichField + Extendable, + { + for &a in &x.to_basefield_array() { + self.write_field(a)?; + } + Ok(()) + } + /// #[inline] fn write_field_ext_vec( @@ -565,7 +543,30 @@ pub trait Write { Ok(()) } - /// + /// Writes a hash `h` to `self`. + #[inline] + fn write_hash(&mut self, h: H::Hash) -> Result<(), Self::Error> + where + F: RichField, + H: Hasher, + { + self.write_all(&h.to_bytes()) + } + + /// Writes `cap`, a value of type [`MerkleCap`], to `self`. + #[inline] + fn write_merkle_cap(&mut self, cap: &MerkleCap) -> Result<(), Self::Error> + where + F: RichField, + H: Hasher, + { + for &a in &cap.0 { + self.write_hash::(a)?; + } + Ok(()) + } + + /// Writes a value `os` of type [`OpeningSet`] to `self.` #[inline] fn write_opening_set( &mut self, @@ -583,7 +584,7 @@ pub trait Write { self.write_field_ext_vec::(&os.quotient_polys) } - /// + /// Writes a value `p` of type [`MerkleProof`] to `self.` #[inline] fn write_merkle_proof(&mut self, p: &MerkleProof) -> Result<(), Self::Error> where @@ -602,7 +603,7 @@ pub trait Write { Ok(()) } - /// + /// Writes a value `fitp` of type [`FriInitialTreeProof`] to `self.` #[inline] fn write_fri_initial_proof( &mut self, @@ -619,7 +620,7 @@ pub trait Write { Ok(()) } - /// + /// Writes a value `fqs` of type [`FriQueryStep`] to `self.` #[inline] fn write_fri_query_step( &mut self, @@ -633,7 +634,7 @@ pub trait Write { self.write_merkle_proof(&fqs.merkle_proof) } - /// + /// Writes a value `fqrs` of type [`FriQueryRound`] to `self.` #[inline] fn write_fri_query_rounds( &mut self, @@ -652,7 +653,7 @@ pub trait Write { Ok(()) } - /// + /// Writes a value `fq` of type [`FriProof`] to `self.` #[inline] fn write_fri_proof( &mut self, @@ -670,7 +671,7 @@ pub trait Write { self.write_field(fp.pow_witness) } - /// + /// Writes a value `proof` of type [`Proof`] to `self.` #[inline] fn write_proof( &mut self, @@ -687,7 +688,7 @@ pub trait Write { self.write_fri_proof::(&proof.opening_proof) } - /// + /// Writes a value `proof_with_pis` of type [`ProofWithPublicInputs`] to `self.` #[inline] fn write_proof_with_public_inputs( &mut self, @@ -705,7 +706,7 @@ pub trait Write { self.write_field_vec(public_inputs) } - /// + /// Writes a value `cfqrs` of type [`CompressedFriQueryRounds`] to `self.` #[inline] fn write_compressed_fri_query_rounds( &mut self, @@ -733,7 +734,7 @@ pub trait Write { Ok(()) } - /// + /// Writes a value `fq` of type [`CompressedFriProof`] to `self.` #[inline] fn write_compressed_fri_proof( &mut self, @@ -751,7 +752,7 @@ pub trait Write { self.write_field(fp.pow_witness) } - /// + /// Writes a value `proof` of type [`CompressedProof`] to `self.` #[inline] fn write_compressed_proof( &mut self, @@ -768,7 +769,7 @@ pub trait Write { self.write_compressed_fri_proof::(&proof.opening_proof) } - /// + /// Writes a value `proof_with_pis` of type [`CompressedProofWithPublicInputs`] to `self.` #[inline] fn write_compressed_proof_with_public_inputs( &mut self, @@ -797,20 +798,20 @@ impl Write for Vec { } } -/// +/// Buffer #[cfg(feature = "std")] #[derive(Debug)] pub struct Buffer(Cursor>); #[cfg(feature = "std")] impl Buffer { - /// + /// Builds a new [`Buffer`] over `buffer`. #[inline] pub fn new(buffer: Vec) -> Self { Self(Cursor::new(buffer)) } - /// + /// Returns the inner buffer. #[inline] pub fn bytes(self) -> Vec { self.0.into_inner()