Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add serialization and deserialization for BytesHash #1645

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion field/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gadgets/arithmetic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
if j != 0 {
current = self.square_extension(current);
}
if (exponent >> j & 1) != 0 {
if ((exponent >> j) & 1) != 0 {
product = self.mul_extension(product, current);
}
}
Expand Down
43 changes: 39 additions & 4 deletions plonky2/src/hash/hash_types.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -193,19 +195,52 @@ impl<F: RichField, const N: usize> GenericHashOut<F> for BytesHash<N> {
}

impl<const N: usize> Serialize for BytesHash<N> {
fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
todo!()
serializer.serialize_bytes(&self.0)
}
}

struct ByteHashVisitor<const N: usize>;

impl<'de, const N: usize> Visitor<'de> for ByteHashVisitor<N> {
type Value = BytesHash<N>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "an array containing exactly {} bytes", N)
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
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<E>(self, s: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
let bytes = s.try_into().unwrap();
Ok(BytesHash(bytes))
}
}

impl<'de, const N: usize> Deserialize<'de> for BytesHash<N> {
fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
todo!()
deserializer.deserialize_seq(ByteHashVisitor::<N>)
}
}
Loading