diff --git a/field/src/types.rs b/field/src/types.rs index 18c4b82d53..d714b7a84d 100644 --- a/field/src/types.rs +++ b/field/src/types.rs @@ -369,7 +369,7 @@ pub trait Field: let mut product = Self::ONE; for j in 0..bits_u64(power) { - if (power >> j & 1) != 0 { + if ((power >> j) & 1) != 0 { product *= current; } current = current.square(); diff --git a/plonky2/src/gadgets/arithmetic_extension.rs b/plonky2/src/gadgets/arithmetic_extension.rs index 355d5e253a..9d10880305 100644 --- a/plonky2/src/gadgets/arithmetic_extension.rs +++ b/plonky2/src/gadgets/arithmetic_extension.rs @@ -470,7 +470,7 @@ impl, const D: usize> CircuitBuilder { if j != 0 { current = self.square_extension(current); } - if (exponent >> j & 1) != 0 { + if ((exponent >> j) & 1) != 0 { product = self.mul_extension(product, current); } } diff --git a/plonky2/src/hash/hash_types.rs b/plonky2/src/hash/hash_types.rs index 992f36197e..9f9d976bfe 100644 --- a/plonky2/src/hash/hash_types.rs +++ b/plonky2/src/hash/hash_types.rs @@ -1,7 +1,9 @@ #[cfg(not(feature = "std"))] use alloc::vec::Vec; +use core::fmt; use anyhow::ensure; +use serde::de::{self, Visitor}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::field::goldilocks_field::GoldilocksField; @@ -193,19 +195,52 @@ impl GenericHashOut for BytesHash { } impl Serialize for BytesHash { - fn serialize(&self, _serializer: S) -> Result + fn serialize(&self, serializer: S) -> Result where S: Serializer, { - todo!() + serializer.serialize_bytes(&self.0) + } +} + +struct ByteHashVisitor; + +impl<'de, const N: usize> Visitor<'de> for ByteHashVisitor { + type Value = BytesHash; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "an array containing exactly {} bytes", N) + } + + fn visit_seq(self, mut seq: A) -> Result + where + A: de::SeqAccess<'de>, + { + let mut bytes = [0u8; N]; + for i in 0..N { + let next_element = seq.next_element()?; + match next_element { + Some(value) => bytes[i] = value, + None => return Err(de::Error::invalid_length(i, &self)), + } + } + Ok(BytesHash(bytes)) + } + + fn visit_bytes(self, s: &[u8]) -> Result + where + E: de::Error, + { + let bytes = s.try_into().unwrap(); + Ok(BytesHash(bytes)) } } impl<'de, const N: usize> Deserialize<'de> for BytesHash { - fn deserialize(_deserializer: D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { - todo!() + deserializer.deserialize_seq(ByteHashVisitor::) } }