Skip to content

Commit c91e4a6

Browse files
committed
adjust recursive calculation using standard gates
1 parent 88a7598 commit c91e4a6

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

src/backends/plonky2/recursion/circuit.rs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use itertools::Itertools;
1212
use plonky2::{
1313
self,
1414
field::types::Field,
15-
gates::noop::NoopGate,
15+
gates::{gate::GateRef, noop::NoopGate},
1616
hash::hash_types::HashOutTarget,
1717
iop::{
1818
target::Target,
@@ -45,7 +45,7 @@ pub struct VerifiedProofTarget {
4545
}
4646

4747
/// Expected maximum number of constant gates
48-
const MAX_CONSTANT_GATES: usize = 64;
48+
const MAX_CONSTANT_GATES: usize = 32;
4949

5050
impl VerifiedProofTarget {
5151
fn add_virtual(builder: &mut CircuitBuilder<F, D>, num_public_inputs: usize) -> Self {
@@ -308,40 +308,34 @@ fn coset_interpolation_gate(
308308
unsafe { std::mem::transmute(gate) }
309309
}
310310

311-
pub fn common_data_for_recursion<I: InnerCircuit>(
312-
arity: usize,
313-
num_public_inputs: usize,
314-
inner_params: &I::Params,
315-
) -> Result<CommonCircuitData<F, D>> {
316-
let config = CircuitConfig::standard_recursion_config();
317-
318-
let mut builder = CircuitBuilder::<F, D>::new(config.clone());
319-
use plonky2::gates::gate::GateRef;
320-
// Add our standard set of gates
321-
for gate in [
311+
/// Returns the minimum set of gates that define our recursively verifiable circuits.
312+
/// NOTE: The overhead between verifying any proof with just the `NoopGate` and verifying a proof
313+
/// with all these standard gates is about 400 num_gates (rows), no matter the circuit size.
314+
fn standard_gates(config: &CircuitConfig) -> Vec<GateRef<F, D>> {
315+
vec![
322316
GateRef::new(plonky2::gates::noop::NoopGate {}),
323317
GateRef::new(plonky2::gates::constant::ConstantGate::new(
324318
config.num_constants,
325319
)),
326320
GateRef::new(plonky2::gates::poseidon_mds::PoseidonMdsGate::new()),
327321
GateRef::new(plonky2::gates::poseidon::PoseidonGate::new()),
328322
GateRef::new(plonky2::gates::public_input::PublicInputGate {}),
329-
GateRef::new(plonky2::gates::base_sum::BaseSumGate::<2>::new_from_config::<F>(&config)),
323+
GateRef::new(plonky2::gates::base_sum::BaseSumGate::<2>::new_from_config::<F>(config)),
330324
GateRef::new(plonky2::gates::reducing_extension::ReducingExtensionGate::new(32)),
331325
GateRef::new(plonky2::gates::reducing::ReducingGate::new(43)),
332326
GateRef::new(
333-
plonky2::gates::arithmetic_extension::ArithmeticExtensionGate::new_from_config(&config),
327+
plonky2::gates::arithmetic_extension::ArithmeticExtensionGate::new_from_config(config),
334328
),
335-
GateRef::new(plonky2::gates::arithmetic_base::ArithmeticGate::new_from_config(&config)),
329+
GateRef::new(plonky2::gates::arithmetic_base::ArithmeticGate::new_from_config(config)),
336330
GateRef::new(
337-
plonky2::gates::multiplication_extension::MulExtensionGate::new_from_config(&config),
331+
plonky2::gates::multiplication_extension::MulExtensionGate::new_from_config(config),
338332
),
339-
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(&config, 1)),
340-
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(&config, 2)),
341-
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(&config, 3)),
342-
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(&config, 4)),
343-
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(&config, 5)),
344-
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(&config, 6)),
333+
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(config, 1)),
334+
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(config, 2)),
335+
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(config, 3)),
336+
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(config, 4)),
337+
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(config, 5)),
338+
GateRef::new(plonky2::gates::random_access::RandomAccessGate::new_from_config(config, 6)),
345339
// It would be better do `CosetInterpolationGate::with_max_degree(4, 6)` but unfortunately
346340
// that plonk2 method is `pub(crate)`, so we need to get around that somehow.
347341
GateRef::new(coset_interpolation_gate(
@@ -366,7 +360,20 @@ pub fn common_data_for_recursion<I: InnerCircuit>(
366360
18446462594437939201,
367361
],
368362
)),
369-
] {
363+
]
364+
}
365+
366+
pub fn common_data_for_recursion<I: InnerCircuit>(
367+
arity: usize,
368+
num_public_inputs: usize,
369+
inner_params: &I::Params,
370+
) -> Result<CommonCircuitData<F, D>> {
371+
let config = CircuitConfig::standard_recursion_config();
372+
373+
let mut builder = CircuitBuilder::<F, D>::new(config.clone());
374+
// Add our standard set of gates
375+
let standard_gates = standard_gates(&config);
376+
for gate in standard_gates.into_iter() {
370377
builder.add_gate_to_gate_set(gate);
371378
}
372379

@@ -386,9 +393,9 @@ pub fn common_data_for_recursion<I: InnerCircuit>(
386393
let estimate_verif_num_gates = |degree_bits: usize| {
387394
// Formula obtained via linear regression using `test_measure_recursion` results with
388395
// `standard_recursion_config`.
389-
let num_gates: usize = 236 * degree_bits + 698;
390-
// Add 8% for error because the results are not a clean line
391-
num_gates * 108 / 100
396+
let num_gates: usize = 236 * degree_bits + 1099;
397+
// Add 2% for error because the results are not a clean line
398+
num_gates * 102 / 100
392399
};
393400

394401
// Loop until we find a circuit size that can verify `arity` proofs of itself
@@ -793,11 +800,12 @@ mod tests {
793800
#[test]
794801
fn test_measure_recursion() {
795802
let config = CircuitConfig::standard_recursion_config();
796-
for i in 7..18 {
803+
for i in 7..20 {
797804
let mut builder = CircuitBuilder::new(config.clone());
798-
builder.add_gate_to_gate_set(plonky2::gates::gate::GateRef::new(
799-
plonky2::gates::constant::ConstantGate::new(config.num_constants),
800-
));
805+
let standard_gates = standard_gates(&config);
806+
for gate in standard_gates.into_iter() {
807+
builder.add_gate_to_gate_set(gate);
808+
}
801809
while builder.num_gates() < (1 << i) - MAX_CONSTANT_GATES {
802810
builder.add_gate(NoopGate, vec![]);
803811
}

0 commit comments

Comments
 (0)