test proof serialization

This commit is contained in:
M Alghazwi 2024-11-26 11:35:47 +01:00
parent 3a1d87d450
commit d8cf1e0af5
4 changed files with 68 additions and 1 deletions

Binary file not shown.

View File

@ -396,6 +396,7 @@ mod tests {
use plonky2::plonk::circuit_builder::CircuitBuilder;
use codex_plonky2_circuits::circuits::params::CircuitParams;
use codex_plonky2_circuits::circuits::sample_cells::SampleCircuit;
use serde::Serialize;
use crate::params::{C, D, F};
// Test sample cells (non-circuit)

View File

@ -2,7 +2,7 @@ use anyhow::{anyhow, Error, Result};
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::{fs, io};
use std::io::{BufReader, Write};
use std::io::{BufReader, BufWriter, Write};
use std::path::Path;
use crate::gen_input::{DatasetTree, gen_testing_circuit_input};
use plonky2::hash::hash_types::{HashOut, RichField};
@ -11,6 +11,8 @@ use plonky2_field::extension::Extendable;
use plonky2_field::types::Field;
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
use codex_plonky2_circuits::circuits::sample_cells::{Cell, MerklePath, SampleCircuitInput};
use plonky2::plonk::proof::CompressedProofWithPublicInputs;
use serde_json::to_writer_pretty;
use crate::params::TestParams;
/// export circuit input to json file
@ -30,6 +32,25 @@ pub fn export_circ_input_to_json<
Ok(())
}
// Function to export proof with public input to json file
fn export_proof_with_pi_to_json<F, C, const D: usize>(
instance: &CompressedProofWithPublicInputs<F, C, D>,
path: &str,
) -> io::Result<()>
where
F: RichField + Extendable<D> + Poseidon2 + Serialize,
C: GenericConfig<D, F = F> + Serialize,
{
// Create or overwrite the file at the given path
let file = File::create(path)?;
let writer = BufWriter::new(file);
// Serialize the struct to JSON and write it to the file
to_writer_pretty(writer, instance)?;
Ok(())
}
/// Function to generate circuit input and export to JSON
pub fn generate_and_export_circ_input_to_json<
@ -464,4 +485,49 @@ mod tests {
Ok(())
}
// test proof with public input serialization
#[test]
fn test_proof_with_pi_serializer() -> anyhow::Result<()> {
let params = TestParams::default();
// Create the circuit
let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);
let circuit_params = CircuitParams::default();
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();
// gen circ input
let imported_circ_input: SampleCircuitInput<F, D> = gen_testing_circuit_input::<F,D>(&params);
circ.sample_slot_assign_witness(&mut pw, &mut targets, imported_circ_input);
// Build the circuit
let data = builder.build::<C>();
println!("circuit size = {:?}", data.common.degree_bits());
// Prove the circuit with the assigned witness
let start_time = Instant::now();
let proof_with_pis = data.prove(pw)?;
println!("prove_time = {:?}", start_time.elapsed());
println!("Proof size: {} bytes", proof_with_pis.to_bytes().len());
let compressed_proof_with_pi = data.compress(proof_with_pis.clone())?;
let filename = "proof_with_pi.json";
export_proof_with_pi_to_json(&compressed_proof_with_pi,filename)?;
println!("Proof size: {} bytes", compressed_proof_with_pi.to_bytes().len());
// Verify the proof
let verifier_data = data.verifier_data();
assert!(
verifier_data.verify(proof_with_pis).is_ok(),
"Merkle proof verification failed"
);
Ok(())
}
}

Binary file not shown.