mirror of
https://github.com/codex-storage/proof-aggregation.git
synced 2025-02-13 15:16:58 +00:00
update to plonky2 1.0.0
This commit is contained in:
parent
fd5d7f9407
commit
18560d13df
@ -11,8 +11,8 @@ anyhow = { version = "1.0.89" }
|
||||
unroll = { version = "0.1.5", default-features = false }
|
||||
serde = { version = "1.0.210" , features = ["rc"] }
|
||||
serde_json = { version = "1.0" }
|
||||
plonky2 = { version = "0.2.2" }
|
||||
plonky2_field = { version = "0.2.2", default-features = false }
|
||||
plonky2 = { version = "1.0.0" }
|
||||
plonky2_field = { version = "1.0.0", default-features = false }
|
||||
plonky2_poseidon2 = { path = "../plonky2_poseidon2" }
|
||||
itertools = { version = "0.12.1", default-features = false }
|
||||
plonky2_maybe_rayon = { version = "0.2.0", default-features = false }
|
||||
|
@ -3,4 +3,5 @@ pub mod sample_cells;
|
||||
pub mod utils;
|
||||
pub mod params;
|
||||
pub mod keyed_compress;
|
||||
pub mod sponge;
|
||||
pub mod sponge;
|
||||
pub mod recursion;
|
60
codex-plonky2-circuits/src/circuits/recursion.rs
Normal file
60
codex-plonky2-circuits/src/circuits/recursion.rs
Normal file
@ -0,0 +1,60 @@
|
||||
use plonky2::hash::hash_types::RichField;
|
||||
use plonky2::iop::witness::{PartialWitness, WitnessWrite};
|
||||
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
||||
use plonky2::plonk::circuit_data::{VerifierCircuitData, VerifierCircuitTarget};
|
||||
use plonky2::plonk::config::GenericConfig;
|
||||
use plonky2::plonk::proof::{ProofWithPublicInputs, ProofWithPublicInputsTarget};
|
||||
use plonky2_field::extension::Extendable;
|
||||
use plonky2_field::goldilocks_field::GoldilocksField;
|
||||
use plonky2_poseidon2::config::Poseidon2GoldilocksConfig;
|
||||
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
|
||||
|
||||
// recursion param
|
||||
|
||||
pub type F = GoldilocksField;
|
||||
pub const D: usize = 2;
|
||||
pub type C = Poseidon2GoldilocksConfig;
|
||||
pub type Plonky2Proof = ProofWithPublicInputs<F, C, D>;
|
||||
|
||||
/// aggregate sampling proofs
|
||||
pub fn aggregate_sampling_proofs<
|
||||
>(
|
||||
proofs_with_pi: Vec<Plonky2Proof>,
|
||||
verifier_data: &VerifierCircuitData<F, C, D>,
|
||||
builder: &mut CircuitBuilder::<F, D>,
|
||||
pw: &mut PartialWitness<F>,
|
||||
){
|
||||
// assert the number of proofs equals D
|
||||
assert_eq!(proofs_with_pi.len(), D, "Number of proofs to aggregate is not supported");
|
||||
|
||||
// the proof virtual targets
|
||||
let mut proof_targets = vec![];
|
||||
for i in 0..D {
|
||||
let vir_proof = builder.add_virtual_proof_with_pis(&verifier_data.common);
|
||||
proof_targets.push(vir_proof);
|
||||
}
|
||||
// assign the proofs with public input
|
||||
for i in 0..D{
|
||||
pw.set_proof_with_pis_target(&proof_targets[i],&proofs_with_pi[i]);
|
||||
}
|
||||
|
||||
let vd_target = VerifierCircuitTarget {
|
||||
constants_sigmas_cap: builder
|
||||
.add_virtual_cap(verifier_data.common.config.fri_config.cap_height),
|
||||
circuit_digest: builder.add_virtual_hash(),
|
||||
};
|
||||
pw.set_cap_target(
|
||||
&vd_target.constants_sigmas_cap,
|
||||
&verifier_data.verifier_only.constants_sigmas_cap,
|
||||
);
|
||||
pw.set_hash_target(
|
||||
vd_target.circuit_digest,
|
||||
verifier_data.verifier_only.circuit_digest,
|
||||
);
|
||||
|
||||
// verify the proofs in-circuit
|
||||
for i in 0..D {
|
||||
builder.verify_proof::<C>(&proof_targets[i],&vd_target,&verifier_data.common);
|
||||
}
|
||||
|
||||
}
|
@ -354,7 +354,7 @@ impl<
|
||||
// assign proof for that cell
|
||||
let cell_proof = witnesses.merkle_paths[i].path.clone();
|
||||
for k in 0..max_depth {
|
||||
pw.set_hash_target(targets.merkle_paths[i].path[k], cell_proof[k])
|
||||
pw.set_hash_target(targets.merkle_paths[i].path[k], cell_proof[k]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,8 +11,8 @@ anyhow = { version = "1.0.89" }
|
||||
unroll = { version = "0.1.5", default-features = false }
|
||||
serde = { version = "1.0.210" , features = ["rc"] }
|
||||
serde_json = { version = "1.0" }
|
||||
plonky2 = { version = "0.2.2", default-features = true}
|
||||
plonky2_field = { version = "0.2.2", default-features = false }
|
||||
plonky2 = { version = "1.0.0", default-features = true}
|
||||
plonky2_field = { version = "1.0.0", default-features = false }
|
||||
log = { version = "0.4.20", default-features = false }
|
||||
jemallocator = "0.5.4"
|
||||
|
||||
|
@ -392,7 +392,7 @@ impl<F: RichField + Extendable<D> + Poseidon2, const D: usize> SimpleGenerator<F
|
||||
.map(|column| Target::wire(self.row, column))
|
||||
.collect()
|
||||
}
|
||||
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
|
||||
fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) -> anyhow::Result<()> {
|
||||
let local_wire = |column| Wire {
|
||||
row: self.row,
|
||||
column,
|
||||
@ -468,6 +468,7 @@ impl<F: RichField + Extendable<D> + Poseidon2, const D: usize> SimpleGenerator<F
|
||||
for i in 0..SPONGE_WIDTH {
|
||||
out_buffer.set_wire(local_wire(Poseidon2Gate::<F, D>::wire_output(i)), state[i]);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
|
||||
@ -558,7 +559,7 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
let witness = generate_partial_witness(inputs, &circuit.prover_only, &circuit.common);
|
||||
let witness = generate_partial_witness(inputs, &circuit.prover_only, &circuit.common).unwrap();
|
||||
|
||||
let expected_outputs: [F; SPONGE_WIDTH] = F::poseidon2(permutation_inputs.try_into().unwrap());
|
||||
for i in 0..SPONGE_WIDTH {
|
||||
|
@ -10,7 +10,7 @@ clap = { version = "4.0", features = ["derive"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
anyhow = "1.0"
|
||||
plonky2 = { version = "0.2.2" }
|
||||
plonky2_field = { version = "0.2.2", default-features = false }
|
||||
plonky2 = { version = "1.0.0" }
|
||||
plonky2_field = { version = "1.0.0", default-features = false }
|
||||
plonky2_poseidon2 = { path = "../plonky2_poseidon2" }
|
||||
codex-plonky2-circuits = { path = "../codex-plonky2-circuits" }
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -390,15 +390,54 @@ impl<
|
||||
mod tests {
|
||||
use std::time::Instant;
|
||||
use super::*;
|
||||
use plonky2::plonk::circuit_data::CircuitConfig;
|
||||
use plonky2::plonk::circuit_data::{CircuitConfig, CircuitData};
|
||||
use plonky2::plonk::config::GenericConfig;
|
||||
use plonky2::iop::witness::PartialWitness;
|
||||
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
||||
use codex_plonky2_circuits::circuits::params::CircuitParams;
|
||||
use codex_plonky2_circuits::circuits::recursion::aggregate_sampling_proofs;
|
||||
use codex_plonky2_circuits::circuits::sample_cells::SampleCircuit;
|
||||
use serde::Serialize;
|
||||
use plonky2::plonk::proof::ProofWithPublicInputs;
|
||||
use crate::params::{C, D, F};
|
||||
|
||||
// build the sampling circuit and prove,
|
||||
// returns the proof and verifier ci
|
||||
pub fn build_circuit(n_samples: usize) -> anyhow::Result<(CircuitData<F, C, D>, PartialWitness<F>)>{
|
||||
// get input
|
||||
let mut params = TestParams::default();
|
||||
params.n_samples = n_samples;
|
||||
let circ_input = gen_testing_circuit_input::<F,D>(¶ms);
|
||||
|
||||
// Create the circuit
|
||||
let config = CircuitConfig::standard_recursion_config();
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||
|
||||
let mut circuit_params = CircuitParams::default();
|
||||
circuit_params.n_samples = n_samples;
|
||||
|
||||
// build the circuit
|
||||
let circ = SampleCircuit::new(circuit_params.clone());
|
||||
let mut targets = circ.sample_slot_circuit(&mut builder);
|
||||
|
||||
// Create a PartialWitness and assign
|
||||
let mut pw = PartialWitness::new();
|
||||
|
||||
// assign a witness
|
||||
circ.sample_slot_assign_witness(&mut pw, &mut targets, circ_input);
|
||||
|
||||
// Build the circuit
|
||||
let data = builder.build::<C>();
|
||||
|
||||
Ok((data, pw))
|
||||
}
|
||||
|
||||
pub fn prove_circuit(data: &CircuitData<F, C, D>, pw: &PartialWitness<F>) -> anyhow::Result<ProofWithPublicInputs<F, C, D>>{
|
||||
// Prove the circuit with the assigned witness
|
||||
let proof_with_pis = data.prove(pw.clone())?;
|
||||
|
||||
Ok(proof_with_pis)
|
||||
}
|
||||
|
||||
// Test sample cells (non-circuit)
|
||||
#[test]
|
||||
fn test_gen_verify_proof(){
|
||||
@ -450,4 +489,46 @@ mod tests {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Test recursion
|
||||
#[test]
|
||||
fn test_recursion() -> anyhow::Result<()> {
|
||||
// number of samples in each proof
|
||||
let n_samples = 10;
|
||||
// build the circuit
|
||||
let (data, pw) = build_circuit(n_samples)?;
|
||||
// get proofs
|
||||
let mut proofs_with_pi = vec![];
|
||||
for i in 0..D{
|
||||
proofs_with_pi.push(prove_circuit(&data, &pw)?);
|
||||
}
|
||||
|
||||
println!("num of public inputs inner proof = {}", proofs_with_pi[0].public_inputs.len());
|
||||
|
||||
// Create the circuit
|
||||
let config = CircuitConfig::standard_recursion_config();
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||
// Create a PartialWitness
|
||||
let mut pw_agg = PartialWitness::new();
|
||||
// aggregate proofs
|
||||
aggregate_sampling_proofs(proofs_with_pi, &data.verifier_data(), &mut builder, &mut pw_agg);
|
||||
|
||||
let data_agg = builder.build::<C>();
|
||||
|
||||
// Prove the circuit with the assigned witness
|
||||
let start_time = Instant::now();
|
||||
let proof_with_pis_agg = data_agg.prove(pw_agg)?;
|
||||
println!("prove_time = {:?}", start_time.elapsed());
|
||||
|
||||
println!("num of public inputs = {}", proof_with_pis_agg.public_inputs.len());
|
||||
|
||||
// Verify the proof
|
||||
let verifier_data = data_agg.verifier_data();
|
||||
assert!(
|
||||
verifier_data.verify(proof_with_pis_agg).is_ok(),
|
||||
"Merkle proof verification failed"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ clap = { version = "4.0", features = ["derive"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
anyhow = "1.0"
|
||||
plonky2 = { version = "0.2.2" }
|
||||
plonky2_field = { version = "0.2.2", default-features = false }
|
||||
plonky2 = { version = "1.0.0" }
|
||||
plonky2_field = { version = "1.0.0", default-features = false }
|
||||
plonky2_poseidon2 = { path = "../plonky2_poseidon2" }
|
||||
codex-plonky2-circuits = { path = "../codex-plonky2-circuits" }
|
||||
proof-input = { path = "../proof-input" }
|
||||
|
BIN
workflow/circ_data.bin
Normal file
BIN
workflow/circ_data.bin
Normal file
Binary file not shown.
3096
workflow/input.json
3096
workflow/input.json
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user