move pod data's deserialization to RecursivePod trait (#294), expose get_common_data (#300)

This commit is contained in:
arnaucube 2025-06-18 11:13:29 +02:00 committed by GitHub
parent 6ab0bc52fc
commit 0541817116
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 87 additions and 81 deletions

View file

@ -158,23 +158,6 @@ impl EmptyPod {
}) })
.map_err(|e| Error::custom(format!("EmptyPod proof verification failure: {:?}", e))) .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)] #[derive(Serialize, Deserialize)]
@ -220,6 +203,22 @@ impl RecursivePod for EmptyPod {
fn vds_root(&self) -> Hash { fn vds_root(&self) -> Hash {
self.vds_root 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)] #[cfg(test)]

View file

@ -581,7 +581,7 @@ pub struct MainPod {
// a serialized proof. At some point in the future, this data may be available // 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 // as a constant or with static initialization, but in the meantime we can
// generate it on-demand. // 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 // TODO: Cache this somehow
// https://github.com/0xPARC/pod2/issues/247 // https://github.com/0xPARC/pod2/issues/247
let rec_circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA; let rec_circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA;
@ -643,24 +643,6 @@ impl MainPod {
pub fn params(&self) -> &Params { pub fn params(&self) -> &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 { impl Pod for MainPod {
@ -706,6 +688,23 @@ impl RecursivePod for MainPod {
fn vds_root(&self) -> Hash { fn vds_root(&self) -> Hash {
self.vds_root 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)] #[cfg(test)]

View file

@ -46,14 +46,6 @@ impl MockEmptyPod {
} }
Ok(()) 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 { impl Pod for MockEmptyPod {
@ -88,6 +80,14 @@ impl RecursivePod for MockEmptyPod {
fn vds_root(&self) -> Hash { fn vds_root(&self) -> Hash {
panic!("MockEmptyPod can't be verified in a recursive MainPod circuit"); 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)] #[cfg(test)]

View file

@ -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<()> { fn _verify(&self) -> Result<()> {
// 1. TODO: Verify input pods // 1. TODO: Verify input pods
@ -331,6 +305,31 @@ impl RecursivePod for MockMainPod {
fn vds_root(&self) -> Hash { fn vds_root(&self) -> Hash {
self.vds_root 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)] #[cfg(test)]

View file

@ -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, /// Return this Pods data serialized into a json value. This serialization can skip `params,
/// id, vds_root` /// id, vds_root`
fn serialize_data(&self) -> serde_json::Value; fn serialize_data(&self) -> serde_json::Value;
/// Extract key-values from ValueOf public statements /// Extract key-values from ValueOf public statements
fn kvs(&self) -> HashMap<AnchoredKey, Value> { fn kvs(&self) -> HashMap<AnchoredKey, Value> {
self.pub_statements() self.pub_statements()
@ -830,6 +831,16 @@ pub trait RecursivePod: Pod {
fn verifier_data(&self) -> VerifierOnlyCircuitData; fn verifier_data(&self) -> VerifierOnlyCircuitData;
fn proof(&self) -> Proof; fn proof(&self) -> Proof;
fn vds_root(&self) -> Hash; 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> // impl Clone for Box<dyn RecursivePod>

View file

@ -3,16 +3,14 @@ use std::{
sync::{LazyLock, Mutex}, sync::{LazyLock, Mutex},
}; };
use crate::middleware::{ use crate::middleware::{DynError, Error, Hash, Params, Pod, PodId, PodType, RecursivePod, Result};
BackendResult, Error, Hash, Params, Pod, PodId, PodType, RecursivePod, Result,
};
type DeserializeFn = fn( type DeserializeFn = fn(
params: Params, params: Params,
id: PodId,
vds_root: Hash,
data: serde_json::Value, 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>>> = static DESERIALIZERS: LazyLock<Mutex<HashMap<usize, DeserializeFn>>> =
LazyLock::new(backend::deserializers_default); LazyLock::new(backend::deserializers_default);
@ -41,7 +39,7 @@ pub fn deserialize_pod(
pod_type 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))) .map_err(|e| Error::custom(format!("deserialize error: {:?}", e)))
} }
@ -65,10 +63,10 @@ mod backend {
pub(super) fn deserializers_default() -> Mutex<HashMap<usize, DeserializeFn>> { pub(super) fn deserializers_default() -> Mutex<HashMap<usize, DeserializeFn>> {
let mut map: HashMap<usize, DeserializeFn> = HashMap::new(); let mut map: HashMap<usize, DeserializeFn> = HashMap::new();
map.insert(PodType::Empty as usize, EmptyPod::deserialize); map.insert(PodType::Empty as usize, EmptyPod::deserialize_data);
map.insert(PodType::Main as usize, MainPod::deserialize); map.insert(PodType::Main as usize, MainPod::deserialize_data);
map.insert(PodType::MockEmpty as usize, MockEmptyPod::deserialize); map.insert(PodType::MockEmpty as usize, MockEmptyPod::deserialize_data);
map.insert(PodType::MockMain as usize, MockMainPod::deserialize); map.insert(PodType::MockMain as usize, MockMainPod::deserialize_data);
Mutex::new(map) Mutex::new(map)
} }