Skip to content

Commit 740bd55

Browse files
committed
fix mistakes
1 parent 7145ab0 commit 740bd55

File tree

8 files changed

+120
-205
lines changed

8 files changed

+120
-205
lines changed

src/backends/plonky2/circuits/mainpod.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,8 +1371,8 @@ fn set_targets_input_pods_self_statements(
13711371
// Padding
13721372
let mut none_st = mainpod::Statement::from(Statement::None);
13731373
pad_statement(params, &mut none_st);
1374-
for i in statements.len()..statements_target.len() {
1375-
statements_target[i].set_targets(pw, params, &none_st)?;
1374+
for statement_target in statements_target.iter().skip(statements.len()) {
1375+
statement_target.set_targets(pw, params, &none_st)?;
13761376
}
13771377
Ok(())
13781378
}
@@ -1390,7 +1390,7 @@ impl MainPodVerifyTarget {
13901390
self.signed_pods[i].set_targets(pw, signed_pod)?;
13911391
}
13921392
// Padding
1393-
if self.params.max_input_signed_pods > 0 {
1393+
if input.signed_pods.len() != self.params.max_input_signed_pods {
13941394
// TODO: Instead of using an input for padding, use a canonical minimal SignedPod,
13951395
// without it a MainPod configured to support input signed pods must have at least one
13961396
// input signed pod :(
@@ -1408,21 +1408,23 @@ impl MainPodVerifyTarget {
14081408
pw,
14091409
&self.params,
14101410
&self.input_pods_self_statements[i],
1411-
&pod_pub_statements,
1411+
pod_pub_statements,
14121412
)?;
14131413
}
14141414
// Padding
1415-
let empty_pod = EmptyPod::new(&self.params, input.vds_root)?;
1416-
let empty_pod_statements = empty_pod.pub_statements();
1417-
for i in
1418-
input.recursive_pods_pub_self_statements.len()..self.params.max_input_recursive_pods
1419-
{
1420-
set_targets_input_pods_self_statements(
1421-
pw,
1422-
&self.params,
1423-
&self.input_pods_self_statements[i],
1424-
&empty_pod_statements,
1425-
)?;
1415+
if input.recursive_pods_pub_self_statements.len() != self.params.max_input_recursive_pods {
1416+
let empty_pod = EmptyPod::new_boxed(&self.params, input.vds_root);
1417+
let empty_pod_statements = empty_pod.pub_statements();
1418+
for i in
1419+
input.recursive_pods_pub_self_statements.len()..self.params.max_input_recursive_pods
1420+
{
1421+
set_targets_input_pods_self_statements(
1422+
pw,
1423+
&self.params,
1424+
&self.input_pods_self_statements[i],
1425+
&empty_pod_statements,
1426+
)?;
1427+
}
14261428
}
14271429

14281430
assert_eq!(input.statements.len(), self.params.max_statements);

src/backends/plonky2/emptypod.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
collections::HashMap,
3-
sync::{MappedRwLockReadGuard, RwLock},
4-
};
1+
use std::{collections::HashMap, sync::Mutex};
52

63
use base64::{prelude::BASE64_STANDARD, Engine};
74
use itertools::Itertools;
@@ -141,8 +138,10 @@ fn build() -> Result<(EmptyPodVerifyTarget, CircuitData)> {
141138
// })
142139
// }
143140

141+
static EMPTY_POD_CACHE: LazyLock<Mutex<HashMap<Hash, EmptyPod>>> =
142+
LazyLock::new(|| Mutex::new(HashMap::new()));
143+
144144
impl EmptyPod {
145-
// TODO: Cache this by (params, vds_root)
146145
pub fn _prove(params: &Params, vds_root: Hash) -> Result<EmptyPod> {
147146
let (empty_pod_verify_target, data) = &*STANDARD_EMPTY_POD_DATA;
148147

@@ -159,14 +158,23 @@ impl EmptyPod {
159158
proof: proof.proof,
160159
})
161160
}
162-
pub fn new(params: &Params, vds_root: Hash) -> Result<Box<dyn RecursivePod>> {
163-
Ok(Self::_prove(params, vds_root).map(Box::new)?)
161+
pub fn new_boxed(params: &Params, vds_root: Hash) -> Box<dyn RecursivePod> {
162+
let default_params = &*DEFAULT_PARAMS;
163+
assert_eq!(default_params.id_params(), params.id_params());
164+
165+
let empty_pod = EMPTY_POD_CACHE
166+
.lock()
167+
.unwrap()
168+
.entry(vds_root)
169+
.or_insert_with(|| Self::_prove(params, vds_root).expect("prove EmptyPod"))
170+
.clone();
171+
Box::new(empty_pod)
164172
}
165173
fn _verify(&self) -> Result<()> {
166174
let statements = self
167175
.pub_self_statements()
168176
.into_iter()
169-
.map(|st| mainpod::Statement::from(st))
177+
.map(mainpod::Statement::from)
170178
.collect_vec();
171179
let id = PodId(calculate_id(&statements, &self.params));
172180
if id != self.id {
@@ -234,7 +242,7 @@ pub mod tests {
234242
fn test_empty_pod() {
235243
let params = Params::default();
236244

237-
let empty_pod = EmptyPod::new(&params, EMPTY_HASH).unwrap();
245+
let empty_pod = EmptyPod::new_boxed(&params, EMPTY_HASH).unwrap();
238246
empty_pod.verify().unwrap();
239247
}
240248
}

src/backends/plonky2/mainpod/mod.rs

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@ use itertools::Itertools;
77
pub use operation::*;
88
use plonky2::{
99
hash::poseidon::PoseidonHash,
10-
plonk::{
11-
circuit_builder::CircuitBuilder,
12-
circuit_data::{CircuitConfig, CommonCircuitData, ProverCircuitData},
13-
config::Hasher,
14-
},
10+
plonk::{circuit_data::CommonCircuitData, config::Hasher},
1511
util::serialization::{Buffer, Read},
1612
};
1713
pub use statement::*;
1814

1915
use crate::{
2016
backends::plonky2::{
21-
basetypes::{Proof, ProofWithPublicInputs, VerifierOnlyCircuitData, C, D},
17+
basetypes::{Proof, ProofWithPublicInputs, VerifierOnlyCircuitData, D},
2218
circuits::mainpod::{
2319
CustomPredicateVerification, MainPodVerifyInput, MainPodVerifyTarget, NUM_PUBLIC_INPUTS,
2420
},
@@ -281,11 +277,13 @@ pub(crate) fn layout_statements(
281277
}
282278

283279
// Input main pods region
284-
let empty_pod_box: Box<dyn RecursivePod> = if mock {
285-
MockEmptyPod::new(params)
286-
} else {
287-
EmptyPod::new(params, inputs.vds_root)?
288-
};
280+
let empty_pod_box: Box<dyn RecursivePod> =
281+
if mock || inputs.recursive_pods.len() == params.max_input_recursive_pods {
282+
// We mocking or we don't need padding so we skip creating an EmptyPod
283+
MockEmptyPod::new_boxed(params)
284+
} else {
285+
EmptyPod::new_boxed(params, inputs.vds_root)
286+
};
289287
let empty_pod = empty_pod_box.as_ref();
290288
assert!(inputs.recursive_pods.len() <= params.max_input_recursive_pods);
291289
for i in 0..params.max_input_recursive_pods {
@@ -408,32 +406,23 @@ pub struct Prover {}
408406

409407
impl Prover {
410408
fn _prove(&self, params: &Params, inputs: MainPodInputs) -> Result<MainPod> {
411-
let config = CircuitConfig::standard_recursion_config();
412-
let mut builder = CircuitBuilder::<F, D>::new(config);
413-
414409
let rec_circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA;
415410
// println!("DBG recursive MainPod build BEGIN");
416-
let (_, circuit_data) = RecursiveCircuit::<MainPodVerifyTarget>::circuit_data_padded(
417-
params.max_input_recursive_pods,
418-
&rec_circuit_data.common,
419-
params,
420-
)?;
411+
let (main_pod_target, circuit_data) =
412+
RecursiveCircuit::<MainPodVerifyTarget>::circuit_data_padded(
413+
params.max_input_recursive_pods,
414+
&rec_circuit_data.common,
415+
params,
416+
)?;
421417
let rec_params = RecursiveParams {
422418
arity: params.max_input_recursive_pods,
423419
common_data: circuit_data.common.clone(),
424420
verifier_data: circuit_data.verifier_data(),
425421
};
426-
let main_pod_target = RecursiveCircuit::<MainPodVerifyTarget>::build_targets(
427-
&mut builder,
428-
params.max_input_recursive_pods,
429-
&rec_circuit_data.common,
430-
params,
431-
)?;
432422
// println!("DBG recursive MainPod build END");
433-
let prover: ProverCircuitData<F, C, D> = builder.build_prover::<C>();
434423
let main_pod = RecursiveCircuit {
435424
params: rec_params,
436-
prover,
425+
prover: circuit_data.prover_data(),
437426
target: main_pod_target,
438427
};
439428

@@ -449,7 +438,12 @@ impl Prover {
449438
.collect_vec();
450439

451440
// Pad input recursive pods with empty pods if necessary
452-
let empty_pod = EmptyPod::new(params, inputs.vds_root)?;
441+
let empty_pod = if inputs.recursive_pods.len() == params.max_input_recursive_pods {
442+
// We don't need padding so we skip creating an EmptyPod
443+
MockEmptyPod::new_boxed(params)
444+
} else {
445+
EmptyPod::new_boxed(params, inputs.vds_root)
446+
};
453447
let inputs = MainPodInputs {
454448
recursive_pods: &inputs
455449
.recursive_pods
@@ -574,10 +568,11 @@ impl MainPod {
574568
}
575569

576570
// 1, 3, 4, 5 verification via the zkSNARK proof
571+
let rec_circuit_data = &*STANDARD_REC_MAIN_POD_CIRCUIT_DATA;
577572
// TODO: cache these artefacts
578-
let rec_params = recursion::new_params::<MainPodVerifyTarget>(
573+
let (_, circuit_data) = RecursiveCircuit::<MainPodVerifyTarget>::circuit_data_padded(
579574
self.params.max_input_recursive_pods,
580-
NUM_PUBLIC_INPUTS,
575+
&rec_circuit_data.common,
581576
&self.params,
582577
)?;
583578
let public_inputs = id
@@ -586,8 +581,7 @@ impl MainPod {
586581
.chain(self.vds_root.0.iter())
587582
.cloned()
588583
.collect_vec();
589-
rec_params
590-
.verifier_data()
584+
circuit_data
591585
.verify(ProofWithPublicInputs {
592586
proof: self.proof.clone(),
593587
public_inputs,
@@ -829,10 +823,7 @@ pub mod tests {
829823
max_statements: 26,
830824
max_public_statements: 5,
831825
max_signed_pod_values: 8,
832-
max_statement_args: 3,
833-
max_operation_args: 4,
834-
max_custom_predicate_arity: 4,
835-
max_custom_batch_size: 3,
826+
max_operation_args: 5,
836827
max_custom_predicate_wildcards: 6,
837828
max_custom_predicate_verifications: 8,
838829
..Default::default()

src/backends/plonky2/mock/emptypod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ fn type_statement() -> Statement {
2626
}
2727

2828
impl MockEmptyPod {
29-
pub fn new(params: &Params) -> Box<dyn RecursivePod> {
29+
pub fn new_boxed(params: &Params) -> Box<dyn RecursivePod> {
3030
let statements = [mainpod::Statement::from(type_statement())];
31-
let id = PodId(calculate_id(&statements, &params));
31+
let id = PodId(calculate_id(&statements, params));
3232
Box::new(Self {
3333
params: params.clone(),
3434
id,
@@ -38,7 +38,7 @@ impl MockEmptyPod {
3838
let statements = self
3939
.pub_self_statements()
4040
.into_iter()
41-
.map(|st| mainpod::Statement::from(st))
41+
.map(mainpod::Statement::from)
4242
.collect_vec();
4343
let id = PodId(calculate_id(&statements, &self.params));
4444
if id != self.id {
@@ -87,7 +87,7 @@ pub mod tests {
8787
fn test_mock_empty_pod() {
8888
let params = Params::default();
8989

90-
let empty_pod = MockEmptyPod::new(&params);
90+
let empty_pod = MockEmptyPod::new_boxed(&params);
9191
empty_pod.verify().unwrap();
9292
}
9393
}

src/backends/plonky2/mod.rs

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ pub mod primitives;
88
pub mod recursion;
99
pub mod signedpod;
1010

11-
use std::{
12-
collections::HashMap,
13-
sync::{LazyLock, MappedRwLockReadGuard, RwLock, RwLockReadGuard},
14-
};
11+
use std::sync::LazyLock;
1512

1613
pub use error::*;
1714

@@ -21,11 +18,11 @@ use crate::{
2118
circuits::mainpod::{MainPodVerifyTarget, NUM_PUBLIC_INPUTS},
2219
recursion::RecursiveCircuit,
2320
},
24-
middleware::{Hash, Params},
21+
middleware::Params,
2522
timed,
2623
};
2724

28-
pub static DEFAULT_PARAMS: LazyLock<Params> = LazyLock::new(|| Params::default());
25+
pub static DEFAULT_PARAMS: LazyLock<Params> = LazyLock::new(Params::default);
2926

3027
pub static STANDARD_REC_MAIN_POD_CIRCUIT_DATA: LazyLock<CircuitData> = LazyLock::new(|| {
3128
let params = &*DEFAULT_PARAMS;
@@ -40,68 +37,3 @@ pub static STANDARD_REC_MAIN_POD_CIRCUIT_DATA: LazyLock<CircuitData> = LazyLock:
4037
.1
4138
)
4239
});
43-
44-
// /// Generic function to build a HashMap cache from a static LazyLock.
45-
// pub fn get_or_set_map_cache<K, V>(
46-
// name: &str,
47-
// cache: &'static LazyLock<RwLock<HashMap<K, V>>>,
48-
// key: &K,
49-
// set_fn: impl Fn(&K) -> V,
50-
// ) -> MappedRwLockReadGuard<'static, V>
51-
// where
52-
// K: Clone + std::cmp::Eq + std::hash::Hash,
53-
// {
54-
// println!("DBG get_or_set_map_cache {}", name);
55-
// // Try to read from the hashmap with a readlock. If the entry doesn't exist, acquire the write
56-
// // lock, create and insert the entry and finally recurse to suceed with the read lock.
57-
// {
58-
// let read_lock = cache.read().unwrap();
59-
// println!("DBG get_or_set_map_cache read locked {}", name);
60-
// if read_lock.get(key).is_some() {
61-
// return RwLockReadGuard::map(read_lock, |m| m.get(key).unwrap());
62-
// }
63-
// println!("DBG get_or_set_map_cache read unlock {}", name);
64-
// }
65-
// {
66-
// println!("DBG get_or_set_map_cache write before lock {}", name);
67-
// let mut write_lock = cache.write().unwrap();
68-
// println!("DBG get_or_set_map_cache write locked {}", name);
69-
// // After acquiring the write lock, we check again if the data exist so that if another
70-
// // thread raced us here we don't call `set_fn` twice.
71-
// if write_lock.get(key).is_none() {
72-
// let data = set_fn(key);
73-
// write_lock.insert(key.clone(), data);
74-
// }
75-
// println!("DBG get_or_set_map_cache write unlock {}", name);
76-
// }
77-
// get_or_set_map_cache(name, cache, key, set_fn)
78-
// }
79-
//
80-
// static RECURSIVE_MAIN_POD_DATA: LazyLock<RwLock<HashMap<Params, CircuitData>>> =
81-
// LazyLock::new(|| RwLock::new(HashMap::new()));
82-
//
83-
// // TODO: Define a lazy static standard_params that will define the standard recursive main circuit.
84-
// pub fn recursive_main_pod_circuit_data(params: &Params) -> MappedRwLockReadGuard<CircuitData> {
85-
// get_or_set_map_cache(
86-
// "recursive_main_pod_circuit_data",
87-
// &RECURSIVE_MAIN_POD_DATA,
88-
// params,
89-
// |params| {
90-
// timed!(
91-
// "recursive MainPod circuit_data",
92-
// RecursiveCircuit::<MainPodVerifyTarget>::circuit_data(
93-
// params.max_input_recursive_pods,
94-
// NUM_PUBLIC_INPUTS,
95-
// params
96-
// )
97-
// .expect("calculate circuit_data")
98-
// .1
99-
// )
100-
// },
101-
// )
102-
// }
103-
//
104-
// pub fn recursive_main_pod_verifier_data_hash(params: &Params) -> Hash {
105-
// let data = recursive_main_pod_circuit_data(params);
106-
// Hash(data.verifier_only.circuit_digest.elements)
107-
// }

0 commit comments

Comments
 (0)