Skip to content
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
33 changes: 16 additions & 17 deletions src/backends/plonky2/emptypod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,6 @@ impl EmptyPod {
})
.map_err(|e| Error::custom(format!("EmptyPod proof verification failure: {:?}", e)))
}

pub(crate) fn deserialize(
params: Params,
id: PodId,
vds_root: Hash,
data: serde_json::Value,
) -> Result<Box<dyn RecursivePod>> {
let data: Data = serde_json::from_value(data)?;
let circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA;
let proof = deserialize_proof(&circuit_data.common, &data.proof)?;
Ok(Box::new(Self {
params,
id,
vds_root,
proof,
}))
}
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -220,6 +203,22 @@ impl RecursivePod for EmptyPod {
fn vds_root(&self) -> Hash {
self.vds_root
}
fn deserialize_data(
params: Params,
data: serde_json::Value,
vds_root: Hash,
id: PodId,
) -> Result<Box<dyn RecursivePod>, Box<DynError>> {
let data: Data = serde_json::from_value(data)?;
let circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA;
let proof = deserialize_proof(&circuit_data.common, &data.proof)?;
Ok(Box::new(Self {
params,
id,
vds_root,
proof,
}))
}
}

#[cfg(test)]
Expand Down
37 changes: 18 additions & 19 deletions src/backends/plonky2/mainpod/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ pub struct MainPod {
// a serialized proof. At some point in the future, this data may be available
// as a constant or with static initialization, but in the meantime we can
// generate it on-demand.
fn get_common_data(params: &Params) -> Result<CommonCircuitData<F, D>, Error> {
pub fn get_common_data(params: &Params) -> Result<CommonCircuitData<F, D>, Error> {
// TODO: Cache this somehow
// https://github.com/0xPARC/pod2/issues/247
let rec_circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA;
Expand Down Expand Up @@ -643,24 +643,6 @@ impl MainPod {
pub fn params(&self) -> &Params {
&self.params
}

pub(crate) fn deserialize(
params: Params,
id: PodId,
vds_root: Hash,
data: serde_json::Value,
) -> Result<Box<dyn RecursivePod>> {
let data: Data = serde_json::from_value(data)?;
let common = get_common_data(&params)?;
let proof = deserialize_proof(&common, &data.proof)?;
Ok(Box::new(Self {
params,
id,
vds_root,
proof,
public_statements: data.public_statements,
}))
}
}

impl Pod for MainPod {
Expand Down Expand Up @@ -706,6 +688,23 @@ impl RecursivePod for MainPod {
fn vds_root(&self) -> Hash {
self.vds_root
}
fn deserialize_data(
params: Params,
data: serde_json::Value,
vds_root: Hash,
id: PodId,
) -> Result<Box<dyn RecursivePod>, Box<DynError>> {
let data: Data = serde_json::from_value(data)?;
let common = get_common_data(&params)?;
let proof = deserialize_proof(&common, &data.proof)?;
Ok(Box::new(Self {
params,
id,
vds_root,
proof,
public_statements: data.public_statements,
}))
}
}

#[cfg(test)]
Expand Down
16 changes: 8 additions & 8 deletions src/backends/plonky2/mock/emptypod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ impl MockEmptyPod {
}
Ok(())
}
pub(crate) fn deserialize(
params: Params,
id: PodId,
_vds_root: Hash,
_data: serde_json::Value,
) -> Result<Box<dyn RecursivePod>> {
Ok(Box::new(Self { params, id }))
}
}

impl Pod for MockEmptyPod {
Expand Down Expand Up @@ -88,6 +80,14 @@ impl RecursivePod for MockEmptyPod {
fn vds_root(&self) -> Hash {
panic!("MockEmptyPod can't be verified in a recursive MainPod circuit");
}
fn deserialize_data(
params: Params,
_data: serde_json::Value,
_vds_root: Hash,
id: PodId,
) -> Result<Box<dyn RecursivePod>, Box<DynError>> {
Ok(Box::new(Self { params, id }))
}
}

#[cfg(test)]
Expand Down
51 changes: 25 additions & 26 deletions src/backends/plonky2/mock/mainpod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,32 +190,6 @@ impl MockMainPod {
})
}

// MockMainPods include some internal private state which is necessary
// for verification. In non-mock Pods, this state will not be necessary,
// as the public statements can be verified using a ZK proof.
pub(crate) fn deserialize(
params: Params,
id: PodId,
vds_root: Hash,
data: serde_json::Value,
) -> Result<Box<dyn RecursivePod>> {
let Data {
public_statements,
operations,
statements,
merkle_proofs,
} = serde_json::from_value(data)?;
Ok(Box::new(Self {
params,
id,
vds_root,
public_statements,
operations,
statements,
merkle_proofs,
}))
}

fn _verify(&self) -> Result<()> {
// 1. TODO: Verify input pods

Expand Down Expand Up @@ -331,6 +305,31 @@ impl RecursivePod for MockMainPod {
fn vds_root(&self) -> Hash {
self.vds_root
}
// MockMainPods include some internal private state which is necessary
// for verification. In non-mock Pods, this state will not be necessary,
// as the public statements can be verified using a ZK proof.
fn deserialize_data(
params: Params,
data: serde_json::Value,
vds_root: Hash,
id: PodId,
) -> Result<Box<dyn RecursivePod>, Box<DynError>> {
let Data {
public_statements,
operations,
statements,
merkle_proofs,
} = serde_json::from_value(data)?;
Ok(Box::new(Self {
params,
id,
vds_root,
public_statements,
operations,
statements,
merkle_proofs,
}))
}
}

#[cfg(test)]
Expand Down
11 changes: 11 additions & 0 deletions src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ pub trait Pod: fmt::Debug + DynClone + Any {
/// Return this Pods data serialized into a json value. This serialization can skip `params,
/// id, vds_root`
fn serialize_data(&self) -> serde_json::Value;

/// Extract key-values from ValueOf public statements
fn kvs(&self) -> HashMap<AnchoredKey, Value> {
self.pub_statements()
Expand Down Expand Up @@ -830,6 +831,16 @@ pub trait RecursivePod: Pod {
fn verifier_data(&self) -> VerifierOnlyCircuitData;
fn proof(&self) -> Proof;
fn vds_root(&self) -> Hash;

/// Returns the deserialized RecursivePod.
fn deserialize_data(
params: Params,
data: serde_json::Value,
vds_root: Hash,
id: PodId,
) -> Result<Box<dyn RecursivePod>, Box<DynError>>
where
Self: Sized;
}

// impl Clone for Box<dyn RecursivePod>
Expand Down
20 changes: 9 additions & 11 deletions src/middleware/pod_deserialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ use std::{
sync::{LazyLock, Mutex},
};

use crate::middleware::{
BackendResult, Error, Hash, Params, Pod, PodId, PodType, RecursivePod, Result,
};
use crate::middleware::{DynError, Error, Hash, Params, Pod, PodId, PodType, RecursivePod, Result};

type DeserializeFn = fn(
params: Params,
id: PodId,
vds_root: Hash,
data: serde_json::Value,
) -> BackendResult<Box<dyn RecursivePod>>;
vds_root: Hash,
id: PodId,
) -> Result<Box<dyn RecursivePod>, Box<DynError>>;

static DESERIALIZERS: LazyLock<Mutex<HashMap<usize, DeserializeFn>>> =
LazyLock::new(backend::deserializers_default);
Expand Down Expand Up @@ -41,7 +39,7 @@ pub fn deserialize_pod(
pod_type
)))?;

deserialize_fn(params, id, vds_root, data)
deserialize_fn(params, data, vds_root, id)
.map_err(|e| Error::custom(format!("deserialize error: {:?}", e)))
}

Expand All @@ -65,10 +63,10 @@ mod backend {

pub(super) fn deserializers_default() -> Mutex<HashMap<usize, DeserializeFn>> {
let mut map: HashMap<usize, DeserializeFn> = HashMap::new();
map.insert(PodType::Empty as usize, EmptyPod::deserialize);
map.insert(PodType::Main as usize, MainPod::deserialize);
map.insert(PodType::MockEmpty as usize, MockEmptyPod::deserialize);
map.insert(PodType::MockMain as usize, MockMainPod::deserialize);
map.insert(PodType::Empty as usize, EmptyPod::deserialize_data);
map.insert(PodType::Main as usize, MainPod::deserialize_data);
map.insert(PodType::MockEmpty as usize, MockEmptyPod::deserialize_data);
map.insert(PodType::MockMain as usize, MockMainPod::deserialize_data);
Mutex::new(map)
}

Expand Down
Loading