2024-12-30 11:40:13 +03:00
|
|
|
// some tests for approach 2 of the tree recursion
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use std::time::Instant;
|
2025-01-14 11:13:51 +01:00
|
|
|
use plonky2::hash::hash_types::HashOut;
|
2024-12-30 11:40:13 +03:00
|
|
|
use plonky2::iop::witness::PartialWitness;
|
|
|
|
|
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
2025-01-14 11:13:51 +01:00
|
|
|
use plonky2::plonk::circuit_data::CircuitConfig;
|
|
|
|
|
use plonky2::plonk::config::{GenericConfig, Hasher};
|
|
|
|
|
use plonky2::plonk::proof::{ProofWithPublicInputs};
|
|
|
|
|
use codex_plonky2_circuits::circuits::sample_cells::SampleCircuit;
|
|
|
|
|
use crate::params::{F, D, C, HF};
|
2025-01-10 11:30:04 +01:00
|
|
|
use codex_plonky2_circuits::recursion::circuits::sampling_inner_circuit::SamplingRecursion;
|
|
|
|
|
use codex_plonky2_circuits::recursion::circuits::inner_circuit::InnerCircuit;
|
2025-01-14 11:13:51 +01:00
|
|
|
use codex_plonky2_circuits::recursion::tree2::leaf_circuit::{LeafCircuit, LeafInput};
|
|
|
|
|
// use plonky2_poseidon2::poseidon2_hash::poseidon2::{Poseidon2, Poseidon2Hash};
|
2024-12-30 11:40:13 +03:00
|
|
|
use crate::gen_input::gen_testing_circuit_input;
|
2025-01-14 11:13:51 +01:00
|
|
|
use crate::params::Params;
|
|
|
|
|
use codex_plonky2_circuits::recursion::tree2::{node_circuit::NodeCircuit, tree_circuit::TreeRecursion};
|
|
|
|
|
use codex_plonky2_circuits::recursion::tree2::dummy_gen::DummyProofGen;
|
|
|
|
|
use codex_plonky2_circuits::circuits::utils::vec_to_array;
|
|
|
|
|
|
2024-12-30 11:40:13 +03:00
|
|
|
|
|
|
|
|
/// Uses node recursion to sample the dataset
|
|
|
|
|
#[test]
|
2025-01-14 11:13:51 +01:00
|
|
|
fn test_leaf_circuit() -> anyhow::Result<()> {
|
2024-12-30 11:40:13 +03:00
|
|
|
const M: usize = 1;
|
|
|
|
|
const N: usize = 2;
|
|
|
|
|
|
|
|
|
|
let config = CircuitConfig::standard_recursion_config();
|
|
|
|
|
let mut builder = CircuitBuilder::<F, D>::new(config);
|
2025-01-14 11:13:51 +01:00
|
|
|
let params = Params::default();
|
2024-12-30 11:40:13 +03:00
|
|
|
|
2025-01-14 11:13:51 +01:00
|
|
|
let one_circ_input = gen_testing_circuit_input::<F,D>(¶ms.input_params);
|
|
|
|
|
let samp_circ = SampleCircuit::<F,D, HF>::new(params.circuit_params);
|
|
|
|
|
let inner_tar = samp_circ.sample_slot_circuit_with_public_input(&mut builder)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
let mut pw = PartialWitness::<F>::new();
|
|
|
|
|
samp_circ.sample_slot_assign_witness(&mut pw,&inner_tar,&one_circ_input);
|
|
|
|
|
let inner_d = builder.build::<C>();
|
|
|
|
|
let inner_prf = inner_d.prove(pw)?;
|
|
|
|
|
|
|
|
|
|
let leaf_in = LeafInput{
|
|
|
|
|
inner_proof:inner_prf,
|
|
|
|
|
verifier_data: inner_d.verifier_data(),
|
|
|
|
|
};
|
|
|
|
|
let config2 = CircuitConfig::standard_recursion_config();
|
|
|
|
|
let mut builder = CircuitBuilder::<F, D>::new(config2);
|
|
|
|
|
|
2025-01-14 11:13:51 +01:00
|
|
|
let inner_circ = SamplingRecursion::<F,D,HF,C>::new(Params::default().circuit_params);
|
|
|
|
|
let leaf_circuit = LeafCircuit::<F,D,_>::new(inner_circ);
|
2024-12-30 11:40:13 +03:00
|
|
|
|
|
|
|
|
let s = Instant::now();
|
2025-01-14 11:13:51 +01:00
|
|
|
let leaf_tar = leaf_circuit.build::<C,HF>(&mut builder)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
let circ_data = builder.build::<C>();
|
|
|
|
|
println!("build = {:?}", s.elapsed());
|
2025-01-14 11:13:51 +01:00
|
|
|
println!("sampling circuit size = {:?}", circ_data.common.degree_bits());
|
2024-12-30 11:40:13 +03:00
|
|
|
let s = Instant::now();
|
|
|
|
|
let mut pw = PartialWitness::<F>::new();
|
2025-01-14 11:13:51 +01:00
|
|
|
leaf_circuit.assign_targets::<C,HF>(&mut pw, &leaf_tar, &leaf_in)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
let proof = circ_data.prove(pw)?;
|
|
|
|
|
println!("prove = {:?}", s.elapsed());
|
|
|
|
|
println!("num of pi = {}", proof.public_inputs.len());
|
|
|
|
|
let s = Instant::now();
|
|
|
|
|
assert!(
|
|
|
|
|
circ_data.verify(proof).is_ok(),
|
|
|
|
|
"proof verification failed"
|
|
|
|
|
);
|
|
|
|
|
println!("verify = {:?}", s.elapsed());
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2025-01-14 11:13:51 +01:00
|
|
|
fn test_node_circuit_approach2() -> anyhow::Result<()> {
|
2024-12-30 11:40:13 +03:00
|
|
|
const N: usize = 2; // binary tree
|
|
|
|
|
|
|
|
|
|
let config = CircuitConfig::standard_recursion_config();
|
|
|
|
|
let mut sampling_builder = CircuitBuilder::<F, D>::new(config);
|
|
|
|
|
|
|
|
|
|
//------------ sampling inner circuit ----------------------
|
|
|
|
|
// Circuit that does the sampling - default input
|
2025-01-14 11:13:51 +01:00
|
|
|
let mut params = Params::default();
|
|
|
|
|
let one_circ_input = gen_testing_circuit_input::<F,D>(¶ms.input_params);
|
|
|
|
|
let samp_circ = SampleCircuit::<F,D,HF>::new(params.circuit_params);
|
|
|
|
|
let inner_tar = samp_circ.sample_slot_circuit_with_public_input(&mut sampling_builder)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
// get generate a sampling proof
|
|
|
|
|
let mut pw = PartialWitness::<F>::new();
|
|
|
|
|
samp_circ.sample_slot_assign_witness(&mut pw,&inner_tar,&one_circ_input);
|
|
|
|
|
let inner_data = sampling_builder.build::<C>();
|
|
|
|
|
let inner_proof = inner_data.prove(pw)?;
|
|
|
|
|
|
|
|
|
|
// ------------------- leaf --------------------
|
|
|
|
|
// leaf circuit that verifies the sampling proof
|
2025-01-14 11:13:51 +01:00
|
|
|
let inner_circ = SamplingRecursion::<F,D,HF,C>::new(Params::default().circuit_params);
|
|
|
|
|
let leaf_circuit = LeafCircuit::<F,D,_>::new(inner_circ);
|
2024-12-30 11:40:13 +03:00
|
|
|
|
|
|
|
|
let leaf_in = LeafInput{
|
|
|
|
|
inner_proof,
|
|
|
|
|
verifier_data: inner_data.verifier_data(),
|
|
|
|
|
};
|
|
|
|
|
let config = CircuitConfig::standard_recursion_config();
|
|
|
|
|
let mut leaf_builder = CircuitBuilder::<F, D>::new(config);
|
|
|
|
|
// build
|
|
|
|
|
let s = Instant::now();
|
2025-01-14 11:13:51 +01:00
|
|
|
let leaf_targets = leaf_circuit.build::<C,HF>(&mut leaf_builder)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
let leaf_circ_data = leaf_builder.build::<C>();
|
|
|
|
|
println!("build = {:?}", s.elapsed());
|
2025-01-14 11:13:51 +01:00
|
|
|
println!("leaf circuit size = {:?}", leaf_circ_data.common.degree_bits());
|
2024-12-30 11:40:13 +03:00
|
|
|
// prove
|
|
|
|
|
let s = Instant::now();
|
|
|
|
|
let mut pw = PartialWitness::<F>::new();
|
2025-01-14 11:13:51 +01:00
|
|
|
leaf_circuit.assign_targets::<C,HF>(&mut pw, &leaf_targets, &leaf_in)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
let leaf_proof = leaf_circ_data.prove(pw)?;
|
|
|
|
|
println!("prove = {:?}", s.elapsed());
|
|
|
|
|
println!("num of pi = {}", leaf_proof.public_inputs.len());
|
|
|
|
|
// verify
|
|
|
|
|
let s = Instant::now();
|
|
|
|
|
assert!(
|
|
|
|
|
leaf_circ_data.verify(leaf_proof.clone()).is_ok(),
|
|
|
|
|
"proof verification failed"
|
|
|
|
|
);
|
|
|
|
|
println!("verify = {:?}", s.elapsed());
|
|
|
|
|
|
|
|
|
|
// ------------- Node circuit ------------------
|
|
|
|
|
// node circuit that verifies leafs or itself
|
|
|
|
|
// build
|
|
|
|
|
let s = Instant::now();
|
2025-01-14 11:13:51 +01:00
|
|
|
let mut node = NodeCircuit::<F,D,C,N>::build_circuit::<_,HF>(leaf_circuit)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
println!("build = {:?}", s.elapsed());
|
2025-01-14 11:13:51 +01:00
|
|
|
println!("leaf circuit size = {:?}", node.node_data.node_circuit_data.common.degree_bits());
|
2024-12-30 11:40:13 +03:00
|
|
|
|
|
|
|
|
// prove leaf
|
|
|
|
|
let s = Instant::now();
|
|
|
|
|
let mut pw = PartialWitness::<F>::new();
|
2025-01-14 11:13:51 +01:00
|
|
|
let leaf_proofs: [ProofWithPublicInputs<F, C, D>; N] =
|
|
|
|
|
vec_to_array::<N, ProofWithPublicInputs<F, C, D>>(
|
|
|
|
|
(0..N)
|
|
|
|
|
.map(|_| {
|
|
|
|
|
leaf_proof.clone()
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
)?;
|
|
|
|
|
let dummy_node_proofs: [ProofWithPublicInputs<F, C, D>; N] =
|
|
|
|
|
DummyProofGen::<F, D, C>::gen_n_dummy_node_proofs::<N>(
|
|
|
|
|
&node.node_data.inner_node_common_data,
|
|
|
|
|
&node.node_data.node_circuit_data.verifier_only,
|
|
|
|
|
)?;
|
|
|
|
|
NodeCircuit::<F,D,C,N>::assign_targets(
|
2024-12-30 11:40:13 +03:00
|
|
|
node.node_targets.clone(), //targets
|
2025-01-14 11:13:51 +01:00
|
|
|
leaf_proofs, // leaf proofs
|
|
|
|
|
dummy_node_proofs, // node proofs (dummy here)
|
2024-12-30 11:40:13 +03:00
|
|
|
&node.node_data.leaf_circuit_data.verifier_only, // leaf verifier data
|
|
|
|
|
&mut pw, // partial witness
|
|
|
|
|
true // is leaf
|
|
|
|
|
)?;
|
|
|
|
|
let node_proof = node.node_data.node_circuit_data.prove(pw)?;
|
|
|
|
|
println!("prove = {:?}", s.elapsed());
|
|
|
|
|
println!("num of pi = {}", node_proof.public_inputs.len());
|
|
|
|
|
let s = Instant::now();
|
|
|
|
|
assert!(
|
|
|
|
|
node.node_data.node_circuit_data.verify(node_proof.clone()).is_ok(),
|
|
|
|
|
"proof verification failed"
|
|
|
|
|
);
|
|
|
|
|
println!("verify = {:?}", s.elapsed());
|
|
|
|
|
|
2025-01-14 11:13:51 +01:00
|
|
|
|
2024-12-30 11:40:13 +03:00
|
|
|
// prove node
|
|
|
|
|
let s = Instant::now();
|
|
|
|
|
let mut pw = PartialWitness::<F>::new();
|
2025-01-14 11:13:51 +01:00
|
|
|
let node_proofs: [ProofWithPublicInputs<F, C, D>; N] =
|
|
|
|
|
vec_to_array::<N, ProofWithPublicInputs<F, C, D>>(
|
|
|
|
|
(0..N)
|
|
|
|
|
.map(|_| {
|
|
|
|
|
node_proof.clone()
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
)?;
|
|
|
|
|
let dummy_leaf_proofs: [ProofWithPublicInputs<F, C, D>; N] =
|
|
|
|
|
DummyProofGen::<F, D, C>::gen_n_dummy_leaf_proofs::<N>(
|
|
|
|
|
&node.node_data.leaf_circuit_data.common
|
|
|
|
|
)?;
|
|
|
|
|
NodeCircuit::<F,D,C,N>::assign_targets(
|
2024-12-30 11:40:13 +03:00
|
|
|
node.node_targets.clone(), //targets
|
2025-01-14 11:13:51 +01:00
|
|
|
dummy_leaf_proofs, // leaf proofs
|
|
|
|
|
node_proofs, // node proofs (dummy here)
|
2024-12-30 11:40:13 +03:00
|
|
|
&node.node_data.leaf_circuit_data.verifier_only, // leaf verifier data
|
|
|
|
|
&mut pw, // partial witness
|
|
|
|
|
false // is leaf
|
|
|
|
|
)?;
|
|
|
|
|
let node_proof = node.node_data.node_circuit_data.prove(pw)?;
|
|
|
|
|
// let node_proof = node_d.prove(pw)?;
|
|
|
|
|
println!("prove = {:?}", s.elapsed());
|
|
|
|
|
println!("num of pi = {}", node_proof.public_inputs.len());
|
|
|
|
|
let s = Instant::now();
|
|
|
|
|
assert!(
|
|
|
|
|
node.node_data.node_circuit_data.verify(node_proof.clone()).is_ok(),
|
|
|
|
|
"proof verification failed"
|
|
|
|
|
);
|
|
|
|
|
println!("verify = {:?}", s.elapsed());
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2025-01-14 11:13:51 +01:00
|
|
|
fn test_tree_recursion_approach2() -> anyhow::Result<()> {
|
2024-12-30 11:40:13 +03:00
|
|
|
const N: usize = 2; // binary tree
|
|
|
|
|
const K: usize = 4; // number of leaves/slots sampled - should be power of 2
|
|
|
|
|
|
|
|
|
|
let config = CircuitConfig::standard_recursion_config();
|
|
|
|
|
let mut sampling_builder = CircuitBuilder::<F, D>::new(config);
|
|
|
|
|
|
|
|
|
|
//------------ sampling inner circuit ----------------------
|
|
|
|
|
// Circuit that does the sampling - default input
|
2025-01-14 11:13:51 +01:00
|
|
|
let mut params = Params::default();
|
|
|
|
|
let one_circ_input = gen_testing_circuit_input::<F,D>(¶ms.input_params);
|
|
|
|
|
let samp_circ = SampleCircuit::<F,D,HF>::new(params.circuit_params);
|
|
|
|
|
let inner_tar = samp_circ.sample_slot_circuit_with_public_input(&mut sampling_builder)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
// get generate a sampling proof
|
|
|
|
|
let mut pw = PartialWitness::<F>::new();
|
2025-01-14 11:13:51 +01:00
|
|
|
samp_circ.sample_slot_assign_witness(&mut pw,&inner_tar,&one_circ_input)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
let inner_data = sampling_builder.build::<C>();
|
|
|
|
|
println!("sampling circuit degree bits = {:?}", inner_data.common.degree_bits());
|
|
|
|
|
let inner_proof = inner_data.prove(pw)?;
|
|
|
|
|
|
|
|
|
|
// ------------------- leaf --------------------
|
|
|
|
|
// leaf circuit that verifies the sampling proof
|
2025-01-14 11:13:51 +01:00
|
|
|
let inner_circ = SamplingRecursion::<F,D,HF,C>::new(Params::default().circuit_params);
|
|
|
|
|
let leaf_circuit = LeafCircuit::<F,D,_>::new(inner_circ);
|
2024-12-30 11:40:13 +03:00
|
|
|
|
|
|
|
|
let leaf_in = LeafInput{
|
|
|
|
|
inner_proof,
|
|
|
|
|
verifier_data: inner_data.verifier_data(),
|
|
|
|
|
};
|
|
|
|
|
let config = CircuitConfig::standard_recursion_config();
|
|
|
|
|
let mut leaf_builder = CircuitBuilder::<F, D>::new(config);
|
|
|
|
|
// build
|
|
|
|
|
let s = Instant::now();
|
2025-01-14 11:13:51 +01:00
|
|
|
let leaf_targets = leaf_circuit.build::<C,HF>(&mut leaf_builder)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
let leaf_circ_data = leaf_builder.build::<C>();
|
|
|
|
|
println!("build = {:?}", s.elapsed());
|
2025-01-14 11:13:51 +01:00
|
|
|
println!("leaf circuit size = {:?}", leaf_circ_data.common.degree_bits());
|
2024-12-30 11:40:13 +03:00
|
|
|
// prove
|
|
|
|
|
let s = Instant::now();
|
|
|
|
|
let mut pw = PartialWitness::<F>::new();
|
2025-01-14 11:13:51 +01:00
|
|
|
leaf_circuit.assign_targets::<C,HF>(&mut pw, &leaf_targets, &leaf_in)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
let leaf_proof = leaf_circ_data.prove(pw)?;
|
|
|
|
|
println!("prove = {:?}", s.elapsed());
|
|
|
|
|
println!("num of pi = {}", leaf_proof.public_inputs.len());
|
|
|
|
|
// verify
|
|
|
|
|
let s = Instant::now();
|
|
|
|
|
assert!(
|
|
|
|
|
leaf_circ_data.verify(leaf_proof.clone()).is_ok(),
|
|
|
|
|
"proof verification failed"
|
|
|
|
|
);
|
|
|
|
|
println!("verify = {:?}", s.elapsed());
|
|
|
|
|
|
|
|
|
|
// ------------- tree circuit ------------------
|
|
|
|
|
// node circuit that verifies leafs or itself
|
|
|
|
|
// build
|
|
|
|
|
let s = Instant::now();
|
2025-01-14 11:13:51 +01:00
|
|
|
let mut tree = TreeRecursion::<F,D,C,N>::build::<_,HF>(leaf_circuit)?;
|
2024-12-30 11:40:13 +03:00
|
|
|
println!("build = {:?}", s.elapsed());
|
2025-01-14 11:13:51 +01:00
|
|
|
println!("node circuit degree bits = {:?}", tree.node.node_data.node_circuit_data.common.degree_bits());
|
2024-12-30 11:40:13 +03:00
|
|
|
|
|
|
|
|
// prove leaf
|
|
|
|
|
let s = Instant::now();
|
|
|
|
|
let leaf_proofs: Vec<ProofWithPublicInputs<F, C, D>> = (0..K)
|
|
|
|
|
.map(|_| {
|
|
|
|
|
leaf_proof.clone()
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
2025-01-14 11:13:51 +01:00
|
|
|
let tree_root_proof = tree.prove_tree(leaf_proofs.clone())?;
|
2024-12-30 11:40:13 +03:00
|
|
|
println!("prove = {:?}", s.elapsed());
|
|
|
|
|
println!("num of pi = {}", tree_root_proof.public_inputs.len());
|
|
|
|
|
let s = Instant::now();
|
|
|
|
|
assert!(
|
2025-01-14 11:13:51 +01:00
|
|
|
tree.verify_proof(tree_root_proof.clone(),false).is_ok(),
|
2024-12-30 11:40:13 +03:00
|
|
|
"proof verification failed"
|
|
|
|
|
);
|
2025-01-14 11:13:51 +01:00
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
tree_root_proof.public_inputs[0..4].to_vec(),
|
|
|
|
|
get_expected_tree_root_pi_hash::<N>(leaf_proofs),
|
|
|
|
|
"Public input of tree_root_proof does not match the expected root hash"
|
|
|
|
|
);
|
2024-12-30 11:40:13 +03:00
|
|
|
println!("verify = {:?}", s.elapsed());
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-01-14 11:13:51 +01:00
|
|
|
|
|
|
|
|
// ------------ Public Input Verification ------------
|
|
|
|
|
/// Recompute the expected root public input hash outside the circuit
|
|
|
|
|
fn get_expected_tree_root_pi_hash<const N:usize>(leaf_proofs: Vec<ProofWithPublicInputs<F, C, D>>)
|
|
|
|
|
-> Vec<F>{
|
|
|
|
|
// Step 1: Extract relevant public inputs from each leaf proof
|
|
|
|
|
// Assuming the first public input is the hash used for tree hashing
|
|
|
|
|
let mut current_hashes: Vec<HashOut<F>> = leaf_proofs
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|p|HashOut::from_vec(p.public_inputs.clone())) // Adjust index if different
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
// Step 2: Iteratively compute parent hashes until one root hash remains
|
|
|
|
|
while current_hashes.len() > 1 {
|
|
|
|
|
let mut next_level_hashes = Vec::new();
|
|
|
|
|
|
|
|
|
|
for chunk in current_hashes.chunks(N) {
|
|
|
|
|
// Ensure each chunk has exactly N elements
|
|
|
|
|
assert!(
|
|
|
|
|
chunk.len() == N,
|
|
|
|
|
"Number of proofs is not divisible by N"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// collect field elements
|
|
|
|
|
let chunk_f: Vec<F> = chunk.iter()
|
|
|
|
|
.flat_map(|h| h.elements.iter().cloned())
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
// Compute Poseidon2 hash of the concatenated chunk
|
|
|
|
|
let hash = HF::hash_no_pad(&chunk_f);
|
|
|
|
|
next_level_hashes.push(hash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_hashes = next_level_hashes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The final hash is the expected root hash
|
|
|
|
|
current_hashes[0].elements.to_vec()
|
|
|
|
|
}
|
2024-12-30 11:40:13 +03:00
|
|
|
}
|