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

feat: Allow deserialization of StarkGenericConfigs #218

Merged
merged 3 commits into from
Feb 14, 2024
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 core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl SP1Stdin {
}

/// Write a value to the buffer.
pub fn write<T: Serialize + DeserializeOwned>(&mut self, data: &T) {
pub fn write<T: Serialize>(&mut self, data: &T) {
self.buffer.write(data);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/utils/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Buffer {
}

/// Write the serializable object from the buffer.
pub fn write<T: Serialize + DeserializeOwned>(&mut self, data: &T) {
pub fn write<T: Serialize>(&mut self, data: &T) {
let mut tmp = Vec::new();
bincode::serialize_into(&mut tmp, data).expect("serialization failed");
self.data.extend(tmp);
Expand Down
40 changes: 33 additions & 7 deletions core/src/utils/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub(super) mod baby_bear_poseidon2 {
use p3_merkle_tree::FieldMerkleTreeMmcs;
use p3_poseidon2::{DiffusionMatrixBabybear, Poseidon2};
use p3_symmetric::{PaddingFreeSponge, TruncatedPermutation};
use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::stark::StarkGenericConfig;

Expand All @@ -201,17 +201,26 @@ pub(super) mod baby_bear_poseidon2 {
type Pcs =
TwoAdicFriPcs<TwoAdicFriPcsConfig<Val, Challenge, Challenger, Dft, ValMmcs, ChallengeMmcs>>;

#[derive(Deserialize)]
#[serde(from = "std::marker::PhantomData<BabyBearPoseidon2>")]
pub struct BabyBearPoseidon2 {
perm: Perm,
pcs: Pcs,
}

// Implement serialization manually instead of using serde(into) to avoid cloing the config.
impl Serialize for BabyBearPoseidon2 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_none()
std::marker::PhantomData::<BabyBearPoseidon2>.serialize(serializer)
}
}

impl From<std::marker::PhantomData<BabyBearPoseidon2>> for BabyBearPoseidon2 {
fn from(_: std::marker::PhantomData<BabyBearPoseidon2>) -> Self {
Self::new()
}
}

Expand Down Expand Up @@ -299,7 +308,7 @@ pub(super) mod baby_bear_keccak {
use p3_merkle_tree::FieldMerkleTreeMmcs;
use p3_poseidon2::{DiffusionMatrixBabybear, Poseidon2};
use p3_symmetric::{SerializingHasher32, TruncatedPermutation};
use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::stark::StarkGenericConfig;

Expand All @@ -325,17 +334,25 @@ pub(super) mod baby_bear_keccak {
type Pcs =
TwoAdicFriPcs<TwoAdicFriPcsConfig<Val, Challenge, Challenger, Dft, ValMmcs, ChallengeMmcs>>;

#[derive(Deserialize)]
#[serde(from = "std::marker::PhantomData<BabyBearKeccak>")]
pub struct BabyBearKeccak {
perm: Perm,
pcs: Pcs,
}

// Implement serialization manually instead of using serde(into) to avoid cloing the config
impl Serialize for BabyBearKeccak {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_none()
std::marker::PhantomData::<BabyBearKeccak>.serialize(serializer)
}
}

impl From<std::marker::PhantomData<BabyBearKeccak>> for BabyBearKeccak {
fn from(_: std::marker::PhantomData<BabyBearKeccak>) -> Self {
Self::new()
}
}

Expand Down Expand Up @@ -424,7 +441,7 @@ pub(super) mod baby_bear_blake3 {
use p3_merkle_tree::FieldMerkleTreeMmcs;
use p3_poseidon2::{DiffusionMatrixBabybear, Poseidon2};
use p3_symmetric::{SerializingHasher32, TruncatedPermutation};
use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::stark::StarkGenericConfig;

Expand All @@ -450,17 +467,26 @@ pub(super) mod baby_bear_blake3 {
type Pcs =
TwoAdicFriPcs<TwoAdicFriPcsConfig<Val, Challenge, Challenger, Dft, ValMmcs, ChallengeMmcs>>;

#[derive(Deserialize)]
#[serde(from = "std::marker::PhantomData<BabyBearBlake3>")]
pub struct BabyBearBlake3 {
perm: Perm,
pcs: Pcs,
}

// Implement serialization manually instead of using serde(into) to avoid cloing the config
impl Serialize for BabyBearBlake3 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_none()
std::marker::PhantomData::<Self>.serialize(serializer)
}
}

impl From<std::marker::PhantomData<BabyBearBlake3>> for BabyBearBlake3 {
fn from(_: std::marker::PhantomData<BabyBearBlake3>) -> Self {
Self::new()
}
}

Expand Down
Loading