2025-01-14 10:54:43 +01:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
use plonky2::hash::hash_types::RichField;
|
2025-01-09 10:30:39 +01:00
|
|
|
use plonky2::iop::witness::{PartialWitness, WitnessWrite};
|
|
|
|
|
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
2025-02-07 11:00:29 +01:00
|
|
|
use plonky2::plonk::circuit_data::{CircuitConfig, CircuitData, CommonCircuitData, VerifierCircuitTarget, VerifierOnlyCircuitData};
|
2025-01-14 10:54:43 +01:00
|
|
|
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig};
|
2025-01-09 10:30:39 +01:00
|
|
|
use plonky2::plonk::proof::{ProofWithPublicInputs, ProofWithPublicInputsTarget};
|
2025-01-14 10:54:43 +01:00
|
|
|
use plonky2_field::extension::Extendable;
|
|
|
|
|
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
|
2025-01-10 11:29:03 +01:00
|
|
|
use crate::recursion::circuits::inner_circuit::InnerCircuit;
|
2025-01-14 10:54:43 +01:00
|
|
|
use crate::{error::CircuitError,Result};
|
2025-01-09 10:30:39 +01:00
|
|
|
|
2025-02-07 11:00:29 +01:00
|
|
|
/// recursion leaf circuit - verifies N inner proof
|
2025-01-09 10:30:39 +01:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct LeafCircuit<
|
2025-01-14 10:54:43 +01:00
|
|
|
F: RichField + Extendable<D> + Poseidon2,
|
|
|
|
|
const D: usize,
|
2025-02-07 11:00:29 +01:00
|
|
|
C: GenericConfig<D, F = F>,
|
|
|
|
|
H: AlgebraicHasher<F>,
|
|
|
|
|
const N: usize,
|
|
|
|
|
> where
|
|
|
|
|
<C as GenericConfig<D>>::Hasher: AlgebraicHasher<F>
|
|
|
|
|
{
|
|
|
|
|
inner_common_data: CommonCircuitData<F, D>,
|
|
|
|
|
phantom_data: PhantomData<(C,H)>
|
2025-01-09 10:30:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2025-01-14 10:54:43 +01:00
|
|
|
pub struct LeafTargets <
|
|
|
|
|
const D: usize,
|
|
|
|
|
>{
|
2025-02-07 11:00:29 +01:00
|
|
|
pub inner_proof: Vec<ProofWithPublicInputsTarget<D>>,
|
2025-01-09 10:30:39 +01:00
|
|
|
pub verifier_data: VerifierCircuitTarget,
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-14 10:54:43 +01:00
|
|
|
impl<
|
|
|
|
|
F: RichField + Extendable<D> + Poseidon2,
|
|
|
|
|
const D: usize,
|
2025-02-07 11:00:29 +01:00
|
|
|
C: GenericConfig<D, F = F>,
|
|
|
|
|
H: AlgebraicHasher<F>,
|
|
|
|
|
const N: usize,
|
|
|
|
|
> LeafCircuit<F,D,C,H,N> where
|
|
|
|
|
<C as GenericConfig<D>>::Hasher: AlgebraicHasher<F>
|
|
|
|
|
{
|
|
|
|
|
pub fn new(inner_common_data: CommonCircuitData<F,D>) -> Self {
|
|
|
|
|
Self{
|
|
|
|
|
inner_common_data,
|
|
|
|
|
phantom_data:PhantomData::default(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-09 10:30:39 +01:00
|
|
|
|
|
|
|
|
/// build the leaf circuit
|
2025-02-07 11:00:29 +01:00
|
|
|
pub fn build(&self, builder: &mut CircuitBuilder<F, D>) -> Result<LeafTargets<D>> {
|
2025-01-09 10:30:39 +01:00
|
|
|
|
2025-02-07 11:00:29 +01:00
|
|
|
let inner_common = self.inner_common_data.clone();
|
2025-01-09 10:30:39 +01:00
|
|
|
|
2025-02-07 11:00:29 +01:00
|
|
|
// the proof virtual targets
|
2025-01-17 10:05:04 +01:00
|
|
|
let mut pub_input = vec![];
|
2025-02-07 11:00:29 +01:00
|
|
|
let mut vir_proofs = vec![];
|
|
|
|
|
for _i in 0..N {
|
|
|
|
|
let vir_proof = builder.add_virtual_proof_with_pis(&inner_common);
|
2025-01-17 10:05:04 +01:00
|
|
|
let inner_pub_input = vir_proof.public_inputs.clone();
|
|
|
|
|
vir_proofs.push(vir_proof);
|
|
|
|
|
pub_input.extend_from_slice(&inner_pub_input);
|
|
|
|
|
}
|
2025-01-09 10:30:39 +01:00
|
|
|
|
|
|
|
|
// hash the public input & make it public
|
2025-01-17 10:05:04 +01:00
|
|
|
let hash_inner_pub_input = builder.hash_n_to_hash_no_pad::<H>(pub_input);
|
2025-01-09 10:30:39 +01:00
|
|
|
builder.register_public_inputs(&hash_inner_pub_input.elements);
|
|
|
|
|
|
|
|
|
|
// virtual target for the verifier data
|
2025-02-07 11:00:29 +01:00
|
|
|
let inner_verifier_data = builder.add_virtual_verifier_data(inner_common.config.fri_config.cap_height);
|
2025-01-09 10:30:39 +01:00
|
|
|
|
2025-02-07 11:00:29 +01:00
|
|
|
// register verifier data hash as public input.
|
|
|
|
|
let mut vd_pub_input = vec![];
|
|
|
|
|
vd_pub_input.extend_from_slice(&inner_verifier_data.circuit_digest.elements);
|
|
|
|
|
for i in 0..builder.config.fri_config.num_cap_elements() {
|
|
|
|
|
vd_pub_input.extend_from_slice(&inner_verifier_data.constants_sigmas_cap.0[i].elements);
|
2025-01-17 10:05:04 +01:00
|
|
|
}
|
2025-02-07 11:00:29 +01:00
|
|
|
let hash_inner_vd_pub_input = builder.hash_n_to_hash_no_pad::<H>(vd_pub_input);
|
|
|
|
|
builder.register_public_inputs(&hash_inner_vd_pub_input.elements);
|
2025-01-17 10:05:04 +01:00
|
|
|
|
2025-02-07 11:00:29 +01:00
|
|
|
// verify the proofs in-circuit
|
|
|
|
|
for i in 0..N {
|
|
|
|
|
builder.verify_proof::<C>(&vir_proofs[i], &inner_verifier_data, &inner_common);
|
|
|
|
|
}
|
2025-01-09 10:30:39 +01:00
|
|
|
|
|
|
|
|
// return targets
|
|
|
|
|
let t = LeafTargets {
|
2025-02-07 11:00:29 +01:00
|
|
|
inner_proof: vir_proofs,
|
2025-01-09 10:30:39 +01:00
|
|
|
verifier_data: inner_verifier_data,
|
|
|
|
|
};
|
|
|
|
|
Ok(t)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// assign the leaf targets with given input
|
2025-02-07 11:00:29 +01:00
|
|
|
pub fn assign_targets(
|
|
|
|
|
&self, pw: &mut PartialWitness<F>,
|
|
|
|
|
targets: &LeafTargets<D>,
|
|
|
|
|
inner_proof: &[ProofWithPublicInputs<F, C, D>],
|
|
|
|
|
verifier_only_data: &VerifierOnlyCircuitData<C, D>,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
assert_eq!(inner_proof.len(), N);
|
2025-01-17 10:05:04 +01:00
|
|
|
// assign the proofs
|
2025-02-07 11:00:29 +01:00
|
|
|
for i in 0..N {
|
|
|
|
|
pw.set_proof_with_pis_target(&targets.inner_proof[i], &inner_proof[i])
|
2025-01-17 10:05:04 +01:00
|
|
|
.map_err(|e| {
|
|
|
|
|
CircuitError::ProofTargetAssignmentError("inner-proof".to_string(), e.to_string())
|
|
|
|
|
})?;
|
|
|
|
|
}
|
2025-01-09 10:30:39 +01:00
|
|
|
|
|
|
|
|
// assign the verifier data
|
2025-02-07 11:00:29 +01:00
|
|
|
pw.set_verifier_data_target(&targets.verifier_data, verifier_only_data)
|
2025-01-14 10:54:43 +01:00
|
|
|
.map_err(|e| {
|
|
|
|
|
CircuitError::VerifierDataTargetAssignmentError(e.to_string())
|
|
|
|
|
})?;
|
2025-01-09 10:30:39 +01:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-14 10:54:43 +01:00
|
|
|
/// returns the leaf circuit data
|
2025-02-07 11:00:29 +01:00
|
|
|
pub fn get_circuit_data (&self) -> Result<CircuitData<F, C, D>>
|
2025-01-14 10:54:43 +01:00
|
|
|
where
|
|
|
|
|
<C as GenericConfig<D>>::Hasher: AlgebraicHasher<F>
|
|
|
|
|
{
|
|
|
|
|
let config = CircuitConfig::standard_recursion_config();
|
2025-01-30 10:28:33 +01:00
|
|
|
let mut builder = CircuitBuilder::<F, D>::new(config.clone());
|
2025-01-09 10:30:39 +01:00
|
|
|
|
2025-02-07 11:00:29 +01:00
|
|
|
self.build(&mut builder)?;
|
2025-01-09 10:30:39 +01:00
|
|
|
|
2025-01-14 10:54:43 +01:00
|
|
|
let circ_data = builder.build::<C>();
|
2025-01-09 10:30:39 +01:00
|
|
|
|
2025-01-14 10:54:43 +01:00
|
|
|
Ok(circ_data)
|
|
|
|
|
}
|
2025-01-09 10:30:39 +01:00
|
|
|
|
|
|
|
|
}
|
2025-01-14 10:54:43 +01:00
|
|
|
|
|
|
|
|
|