Skip to content

Commit

Permalink
Changes to prepare for dummy segment removal in zk_evm's continuations (
Browse files Browse the repository at this point in the history
#1587)

* Make select_proof_with_pis public

* Add API methods for dummy segment removal

* Change visibility of dummy_circuit

* Return actual verifier data

* Fix Clippy

* Apply comments

---------

Co-authored-by: Hamy Ratoanina <hamy.ratoanina@toposware.com>
LindaGuiga and hratoanina authored May 3, 2024
1 parent c4fbd3a commit dc77c77
Showing 3 changed files with 44 additions and 23 deletions.
8 changes: 8 additions & 0 deletions plonky2/src/plonk/circuit_builder.rs
Original file line number Diff line number Diff line change
@@ -531,6 +531,14 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
}

/// If `condition`, enforces that two routable `Target` values are equal, using Plonk's permutation argument.
pub fn conditional_assert_eq(&mut self, condition: Target, x: Target, y: Target) {
let zero = self.zero();
let diff = self.sub(x, y);
let constr = self.mul(condition, diff);
self.connect(constr, zero);
}

/// Enforces that a routable `Target` value is 0, using Plonk's permutation argument.
pub fn assert_zero(&mut self, x: Target) {
let zero = self.zero();
33 changes: 20 additions & 13 deletions plonky2/src/recursion/conditional_recursive_verifier.rs
Original file line number Diff line number Diff line change
@@ -34,18 +34,8 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
{
let selected_proof =
self.select_proof_with_pis(condition, proof_with_pis0, proof_with_pis1);
let selected_verifier_data = VerifierCircuitTarget {
constants_sigmas_cap: self.select_cap(
condition,
&inner_verifier_data0.constants_sigmas_cap,
&inner_verifier_data1.constants_sigmas_cap,
),
circuit_digest: self.select_hash(
condition,
inner_verifier_data0.circuit_digest,
inner_verifier_data1.circuit_digest,
),
};
let selected_verifier_data =
self.select_verifier_data(condition, inner_verifier_data0, inner_verifier_data1);

self.verify_proof::<C>(&selected_proof, &selected_verifier_data, inner_common_data);
}
@@ -75,7 +65,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}

/// Computes `if b { proof_with_pis0 } else { proof_with_pis1 }`.
fn select_proof_with_pis(
pub fn select_proof_with_pis(
&mut self,
b: BoolTarget,
proof_with_pis0: &ProofWithPublicInputsTarget<D>,
@@ -179,6 +169,23 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
.collect()
}

/// Computes `if b { vk0 } else { vk1 }`.
pub fn select_verifier_data(
&mut self,
b: BoolTarget,
vk0: &VerifierCircuitTarget,
vk1: &VerifierCircuitTarget,
) -> VerifierCircuitTarget {
VerifierCircuitTarget {
constants_sigmas_cap: self.select_cap(
b,
&vk0.constants_sigmas_cap,
&vk1.constants_sigmas_cap,
),
circuit_digest: self.select_hash(b, vk0.circuit_digest, vk1.circuit_digest),
}
}

/// Computes `if b { os0 } else { os1 }`.
fn select_opening_set(
&mut self,
26 changes: 16 additions & 10 deletions plonky2/src/recursion/dummy_circuit.rs
Original file line number Diff line number Diff line change
@@ -67,11 +67,7 @@ where
/// Generate a proof for a dummy circuit. The `public_inputs` parameter let the caller specify
/// certain public inputs (identified by their indices) which should be given specific values.
/// The rest will default to zero.
pub(crate) fn dummy_proof<
F: RichField + Extendable<D>,
C: GenericConfig<D, F = F>,
const D: usize,
>(
pub fn dummy_proof<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>(
circuit: &CircuitData<F, C, D>,
nonzero_public_inputs: HashMap<usize, F>,
) -> anyhow::Result<ProofWithPublicInputs<F, C, D>>
@@ -86,11 +82,7 @@ where
}

/// Generate a circuit matching a given `CommonCircuitData`.
pub(crate) fn dummy_circuit<
F: RichField + Extendable<D>,
C: GenericConfig<D, F = F>,
const D: usize,
>(
pub fn dummy_circuit<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>(
common_data: &CommonCircuitData<F, D>,
) -> CircuitData<F, C, D> {
let config = common_data.config.clone();
@@ -143,6 +135,20 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {

Ok((dummy_proof_with_pis_target, dummy_verifier_data_target))
}

pub fn dummy_proof_and_constant_vk_no_generator<C: GenericConfig<D, F = F> + 'static>(
&mut self,
common_data: &CommonCircuitData<F, D>,
) -> anyhow::Result<(ProofWithPublicInputsTarget<D>, VerifierCircuitTarget)>
where
C::Hasher: AlgebraicHasher<F>,
{
let dummy_circuit = dummy_circuit::<F, C, D>(common_data);
let dummy_proof_with_pis_target = self.add_virtual_proof_with_pis(common_data);
let dummy_verifier_data_target = self.constant_verifier_data(&dummy_circuit.verifier_only);

Ok((dummy_proof_with_pis_target, dummy_verifier_data_target))
}
}

#[derive(Debug)]

0 comments on commit dc77c77

Please sign in to comment.