diff --git a/src/backends/plonky2/emptypod.rs b/src/backends/plonky2/emptypod.rs index 5a606747..eeae2c92 100644 --- a/src/backends/plonky2/emptypod.rs +++ b/src/backends/plonky2/emptypod.rs @@ -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> { - 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)] @@ -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> { + 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)] diff --git a/src/backends/plonky2/mainpod/mod.rs b/src/backends/plonky2/mainpod/mod.rs index 06525acb..591f2fed 100644 --- a/src/backends/plonky2/mainpod/mod.rs +++ b/src/backends/plonky2/mainpod/mod.rs @@ -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, Error> { +pub fn get_common_data(params: &Params) -> Result, Error> { // TODO: Cache this somehow // https://github.com/0xPARC/pod2/issues/247 let rec_circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA; @@ -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> { - let data: Data = serde_json::from_value(data)?; - let common = get_common_data(¶ms)?; - 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 { @@ -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> { + let data: Data = serde_json::from_value(data)?; + let common = get_common_data(¶ms)?; + let proof = deserialize_proof(&common, &data.proof)?; + Ok(Box::new(Self { + params, + id, + vds_root, + proof, + public_statements: data.public_statements, + })) + } } #[cfg(test)] diff --git a/src/backends/plonky2/mock/emptypod.rs b/src/backends/plonky2/mock/emptypod.rs index aa4387c1..c8488a04 100644 --- a/src/backends/plonky2/mock/emptypod.rs +++ b/src/backends/plonky2/mock/emptypod.rs @@ -46,14 +46,6 @@ impl MockEmptyPod { } Ok(()) } - pub(crate) fn deserialize( - params: Params, - id: PodId, - _vds_root: Hash, - _data: serde_json::Value, - ) -> Result> { - Ok(Box::new(Self { params, id })) - } } impl Pod for MockEmptyPod { @@ -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> { + Ok(Box::new(Self { params, id })) + } } #[cfg(test)] diff --git a/src/backends/plonky2/mock/mainpod.rs b/src/backends/plonky2/mock/mainpod.rs index 1760ed02..8c251771 100644 --- a/src/backends/plonky2/mock/mainpod.rs +++ b/src/backends/plonky2/mock/mainpod.rs @@ -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> { - 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 @@ -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> { + 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)] diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs index bc8e7f84..8576fa59 100644 --- a/src/middleware/mod.rs +++ b/src/middleware/mod.rs @@ -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 { self.pub_statements() @@ -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> + where + Self: Sized; } // impl Clone for Box diff --git a/src/middleware/pod_deserialization.rs b/src/middleware/pod_deserialization.rs index 47c53123..76a70c02 100644 --- a/src/middleware/pod_deserialization.rs +++ b/src/middleware/pod_deserialization.rs @@ -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>; + vds_root: Hash, + id: PodId, +) -> Result, Box>; static DESERIALIZERS: LazyLock>> = LazyLock::new(backend::deserializers_default); @@ -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))) } @@ -65,10 +63,10 @@ mod backend { pub(super) fn deserializers_default() -> Mutex> { let mut map: HashMap = 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) }