Skip to content

Commit 0541817

Browse files
authored
move pod data's deserialization to RecursivePod trait (#294), expose get_common_data (#300)
1 parent 6ab0bc5 commit 0541817

File tree

6 files changed

+87
-81
lines changed

6 files changed

+87
-81
lines changed

src/backends/plonky2/emptypod.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,6 @@ impl EmptyPod {
158158
})
159159
.map_err(|e| Error::custom(format!("EmptyPod proof verification failure: {:?}", e)))
160160
}
161-
162-
pub(crate) fn deserialize(
163-
params: Params,
164-
id: PodId,
165-
vds_root: Hash,
166-
data: serde_json::Value,
167-
) -> Result<Box<dyn RecursivePod>> {
168-
let data: Data = serde_json::from_value(data)?;
169-
let circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA;
170-
let proof = deserialize_proof(&circuit_data.common, &data.proof)?;
171-
Ok(Box::new(Self {
172-
params,
173-
id,
174-
vds_root,
175-
proof,
176-
}))
177-
}
178161
}
179162

180163
#[derive(Serialize, Deserialize)]
@@ -220,6 +203,22 @@ impl RecursivePod for EmptyPod {
220203
fn vds_root(&self) -> Hash {
221204
self.vds_root
222205
}
206+
fn deserialize_data(
207+
params: Params,
208+
data: serde_json::Value,
209+
vds_root: Hash,
210+
id: PodId,
211+
) -> Result<Box<dyn RecursivePod>, Box<DynError>> {
212+
let data: Data = serde_json::from_value(data)?;
213+
let circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA;
214+
let proof = deserialize_proof(&circuit_data.common, &data.proof)?;
215+
Ok(Box::new(Self {
216+
params,
217+
id,
218+
vds_root,
219+
proof,
220+
}))
221+
}
223222
}
224223

225224
#[cfg(test)]

src/backends/plonky2/mainpod/mod.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ pub struct MainPod {
581581
// a serialized proof. At some point in the future, this data may be available
582582
// as a constant or with static initialization, but in the meantime we can
583583
// generate it on-demand.
584-
fn get_common_data(params: &Params) -> Result<CommonCircuitData<F, D>, Error> {
584+
pub fn get_common_data(params: &Params) -> Result<CommonCircuitData<F, D>, Error> {
585585
// TODO: Cache this somehow
586586
// https://github.com/0xPARC/pod2/issues/247
587587
let rec_circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA;
@@ -643,24 +643,6 @@ impl MainPod {
643643
pub fn params(&self) -> &Params {
644644
&self.params
645645
}
646-
647-
pub(crate) fn deserialize(
648-
params: Params,
649-
id: PodId,
650-
vds_root: Hash,
651-
data: serde_json::Value,
652-
) -> Result<Box<dyn RecursivePod>> {
653-
let data: Data = serde_json::from_value(data)?;
654-
let common = get_common_data(&params)?;
655-
let proof = deserialize_proof(&common, &data.proof)?;
656-
Ok(Box::new(Self {
657-
params,
658-
id,
659-
vds_root,
660-
proof,
661-
public_statements: data.public_statements,
662-
}))
663-
}
664646
}
665647

666648
impl Pod for MainPod {
@@ -706,6 +688,23 @@ impl RecursivePod for MainPod {
706688
fn vds_root(&self) -> Hash {
707689
self.vds_root
708690
}
691+
fn deserialize_data(
692+
params: Params,
693+
data: serde_json::Value,
694+
vds_root: Hash,
695+
id: PodId,
696+
) -> Result<Box<dyn RecursivePod>, Box<DynError>> {
697+
let data: Data = serde_json::from_value(data)?;
698+
let common = get_common_data(&params)?;
699+
let proof = deserialize_proof(&common, &data.proof)?;
700+
Ok(Box::new(Self {
701+
params,
702+
id,
703+
vds_root,
704+
proof,
705+
public_statements: data.public_statements,
706+
}))
707+
}
709708
}
710709

711710
#[cfg(test)]

src/backends/plonky2/mock/emptypod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ impl MockEmptyPod {
4646
}
4747
Ok(())
4848
}
49-
pub(crate) fn deserialize(
50-
params: Params,
51-
id: PodId,
52-
_vds_root: Hash,
53-
_data: serde_json::Value,
54-
) -> Result<Box<dyn RecursivePod>> {
55-
Ok(Box::new(Self { params, id }))
56-
}
5749
}
5850

5951
impl Pod for MockEmptyPod {
@@ -88,6 +80,14 @@ impl RecursivePod for MockEmptyPod {
8880
fn vds_root(&self) -> Hash {
8981
panic!("MockEmptyPod can't be verified in a recursive MainPod circuit");
9082
}
83+
fn deserialize_data(
84+
params: Params,
85+
_data: serde_json::Value,
86+
_vds_root: Hash,
87+
id: PodId,
88+
) -> Result<Box<dyn RecursivePod>, Box<DynError>> {
89+
Ok(Box::new(Self { params, id }))
90+
}
9191
}
9292

9393
#[cfg(test)]

src/backends/plonky2/mock/mainpod.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -190,32 +190,6 @@ impl MockMainPod {
190190
})
191191
}
192192

193-
// MockMainPods include some internal private state which is necessary
194-
// for verification. In non-mock Pods, this state will not be necessary,
195-
// as the public statements can be verified using a ZK proof.
196-
pub(crate) fn deserialize(
197-
params: Params,
198-
id: PodId,
199-
vds_root: Hash,
200-
data: serde_json::Value,
201-
) -> Result<Box<dyn RecursivePod>> {
202-
let Data {
203-
public_statements,
204-
operations,
205-
statements,
206-
merkle_proofs,
207-
} = serde_json::from_value(data)?;
208-
Ok(Box::new(Self {
209-
params,
210-
id,
211-
vds_root,
212-
public_statements,
213-
operations,
214-
statements,
215-
merkle_proofs,
216-
}))
217-
}
218-
219193
fn _verify(&self) -> Result<()> {
220194
// 1. TODO: Verify input pods
221195

@@ -331,6 +305,31 @@ impl RecursivePod for MockMainPod {
331305
fn vds_root(&self) -> Hash {
332306
self.vds_root
333307
}
308+
// MockMainPods include some internal private state which is necessary
309+
// for verification. In non-mock Pods, this state will not be necessary,
310+
// as the public statements can be verified using a ZK proof.
311+
fn deserialize_data(
312+
params: Params,
313+
data: serde_json::Value,
314+
vds_root: Hash,
315+
id: PodId,
316+
) -> Result<Box<dyn RecursivePod>, Box<DynError>> {
317+
let Data {
318+
public_statements,
319+
operations,
320+
statements,
321+
merkle_proofs,
322+
} = serde_json::from_value(data)?;
323+
Ok(Box::new(Self {
324+
params,
325+
id,
326+
vds_root,
327+
public_statements,
328+
operations,
329+
statements,
330+
merkle_proofs,
331+
}))
332+
}
334333
}
335334

336335
#[cfg(test)]

src/middleware/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ pub trait Pod: fmt::Debug + DynClone + Any {
796796
/// Return this Pods data serialized into a json value. This serialization can skip `params,
797797
/// id, vds_root`
798798
fn serialize_data(&self) -> serde_json::Value;
799+
799800
/// Extract key-values from ValueOf public statements
800801
fn kvs(&self) -> HashMap<AnchoredKey, Value> {
801802
self.pub_statements()
@@ -830,6 +831,16 @@ pub trait RecursivePod: Pod {
830831
fn verifier_data(&self) -> VerifierOnlyCircuitData;
831832
fn proof(&self) -> Proof;
832833
fn vds_root(&self) -> Hash;
834+
835+
/// Returns the deserialized RecursivePod.
836+
fn deserialize_data(
837+
params: Params,
838+
data: serde_json::Value,
839+
vds_root: Hash,
840+
id: PodId,
841+
) -> Result<Box<dyn RecursivePod>, Box<DynError>>
842+
where
843+
Self: Sized;
833844
}
834845

835846
// impl Clone for Box<dyn RecursivePod>

src/middleware/pod_deserialization.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ use std::{
33
sync::{LazyLock, Mutex},
44
};
55

6-
use crate::middleware::{
7-
BackendResult, Error, Hash, Params, Pod, PodId, PodType, RecursivePod, Result,
8-
};
6+
use crate::middleware::{DynError, Error, Hash, Params, Pod, PodId, PodType, RecursivePod, Result};
97

108
type DeserializeFn = fn(
119
params: Params,
12-
id: PodId,
13-
vds_root: Hash,
1410
data: serde_json::Value,
15-
) -> BackendResult<Box<dyn RecursivePod>>;
11+
vds_root: Hash,
12+
id: PodId,
13+
) -> Result<Box<dyn RecursivePod>, Box<DynError>>;
1614

1715
static DESERIALIZERS: LazyLock<Mutex<HashMap<usize, DeserializeFn>>> =
1816
LazyLock::new(backend::deserializers_default);
@@ -41,7 +39,7 @@ pub fn deserialize_pod(
4139
pod_type
4240
)))?;
4341

44-
deserialize_fn(params, id, vds_root, data)
42+
deserialize_fn(params, data, vds_root, id)
4543
.map_err(|e| Error::custom(format!("deserialize error: {:?}", e)))
4644
}
4745

@@ -65,10 +63,10 @@ mod backend {
6563

6664
pub(super) fn deserializers_default() -> Mutex<HashMap<usize, DeserializeFn>> {
6765
let mut map: HashMap<usize, DeserializeFn> = HashMap::new();
68-
map.insert(PodType::Empty as usize, EmptyPod::deserialize);
69-
map.insert(PodType::Main as usize, MainPod::deserialize);
70-
map.insert(PodType::MockEmpty as usize, MockEmptyPod::deserialize);
71-
map.insert(PodType::MockMain as usize, MockMainPod::deserialize);
66+
map.insert(PodType::Empty as usize, EmptyPod::deserialize_data);
67+
map.insert(PodType::Main as usize, MainPod::deserialize_data);
68+
map.insert(PodType::MockEmpty as usize, MockEmptyPod::deserialize_data);
69+
map.insert(PodType::MockMain as usize, MockMainPod::deserialize_data);
7270
Mutex::new(map)
7371
}
7472

0 commit comments

Comments
 (0)