fix and improve the proof generation and serialization.

This commit is contained in:
M Alghazwi 2025-04-10 10:55:36 +02:00
parent 5e7d210322
commit 47f831c727
No known key found for this signature in database
GPG Key ID: 646E567CAD7DB607
5 changed files with 61 additions and 39 deletions

View File

@ -1,18 +1,14 @@
use codex_plonky2_circuits::Result;
use plonky2::field::extension::Extendable;
use plonky2::field::types::Field;
use plonky2::hash::hash_types::{HashOut, HashOutTarget, NUM_HASH_OUT_ELTS, RichField};
use plonky2::iop::witness::{PartialWitness, WitnessWrite};
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::config::{AlgebraicHasher, Hasher};
use plonky2::plonk::config::AlgebraicHasher;
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
use codex_plonky2_circuits::circuits::merkle_circuit::{MerkleProofTarget, MerkleTreeCircuit, MerkleTreeTargets};
use codex_plonky2_circuits::circuits::serialization::SerializableHashOutTarget;
use codex_plonky2_circuits::circuits::utils::{assign_bool_targets, assign_hash_out_targets};
use codex_plonky2_circuits::error::CircuitError;
use crate::utils::usize_to_bits_le;
use crate::merkle_tree::merkle_safe::MerkleTree;
/// the input to the merkle tree circuit
#[derive(Clone)]
@ -122,11 +118,15 @@ mod tests {
use plonky2::hash::hash_types::HashOut;
use plonky2::hash::poseidon::PoseidonHash;
use super::*;
use crate::merkle_tree::merkle_safe::MerkleTree;
use plonky2::plonk::config::Hasher;
use crate::utils::usize_to_bits_le;
use plonky2::plonk::circuit_data::CircuitConfig;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2_field::goldilocks_field::GoldilocksField;
use plonky2::field::types::Field;
#[test]
fn test_build_circuit() -> anyhow::Result<()> {

View File

@ -3,10 +3,7 @@
// https://github.com/codex-storage/nim-codex/blob/master/codex/merkletree/merkletree.nim
use anyhow::{ensure, Result};
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::hash::hash_types::{HashOut, RichField};
use plonky2::hash::poseidon::PoseidonHash;
use plonky2::plonk::config::Hasher;
use std::ops::Shr;
use plonky2_field::extension::Extendable;
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
@ -251,6 +248,9 @@ mod tests {
use super::*;
use plonky2::field::types::Field;
use crate::merkle_tree::key_compress::key_compress;
use plonky2::plonk::config::Hasher;
use plonky2::hash::poseidon::PoseidonHash;
use plonky2::field::goldilocks_field::GoldilocksField;
// types used in all tests
type F = GoldilocksField;

View File

@ -1,31 +1,13 @@
use plonky2::hash::hash_types::{HashOut, RichField};
use plonky2_field::extension::Extendable;
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
fn digest_seq<
F: RichField + Extendable<D> + Poseidon2,
const D: usize,
>(n: usize) -> Vec<HashOut<F>> {
(0..n)
.map(|i| HashOut {
elements: [
F::from_canonical_u64((i + 1) as u64),
F::ZERO,
F::ZERO,
F::ZERO,
],
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use plonky2::hash::hash_types::{HashOut, RichField};
use plonky2_field::extension::Extendable;
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
use anyhow::Result;
use crate::merkle_tree::merkle_safe::{MerkleProof, MerkleTree};
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::field::types::Field;
use plonky2::hash::hash_types::HashOut;
type F = GoldilocksField;
const D: usize = 2;
@ -35,6 +17,22 @@ mod tests {
digest: [u64; 4],
}
fn digest_seq<
F: RichField + Extendable<D> + Poseidon2,
const D: usize,
>(n: usize) -> Vec<HashOut<F>> {
(0..n)
.map(|i| HashOut {
elements: [
F::from_canonical_u64((i + 1) as u64),
F::ZERO,
F::ZERO,
F::ZERO,
],
})
.collect()
}
#[test]
fn test_merkle_roots() -> Result<()> {
let zero = HashOut {

View File

@ -1,11 +1,12 @@
/// File constants with full paths - Prover
pub const CIRC_INPUT_JSON: &str = "prover_data/input.json";
pub const PROVER_CIRC_DATA_JSON: &str = "prover_data/prover_circ_data.json";
pub const PROVER_CIRC_DATA_JSON: &str = "prover_data/prover_circ_data.bin";
pub const TARGETS_JSON: &str = "prover_data/targets.json";
/// File constants with full paths - Verifier
pub const VERIFIER_CIRC_DATA_JSON: &str = "verifier_data/verifier_circ_data.json";
pub const VERIFIER_CIRC_DATA_JSON: &str = "verifier_data/verifier_circ_data.bin";
pub const PROOF_JSON: &str = "verifier_data/proof.json";
pub const TREE_PROOF_JSON: &str = "verifier_data/tree_proof.json";

View File

@ -1,18 +1,16 @@
use serde::{Deserialize, Serialize};
use serde::Serialize;
use std::{fs, io};
use std::path::Path;
use anyhow::Context;
use crate::gen_input::gen_testing_circuit_input;
use plonky2::hash::hash_types::RichField;
use plonky2::plonk::circuit_data::{CircuitData, ProverCircuitData, VerifierCircuitData};
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig};
use plonky2_field::extension::Extendable;
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
use codex_plonky2_circuits::circuits::sample_cells::SampleCircuitInput;
use plonky2::plonk::proof::ProofWithPublicInputs;
use serde::de::DeserializeOwned;
use plonky2_poseidon2::serialization::{DefaultGateSerializer, DefaultGeneratorSerializer};
use crate::serialization::file_paths::{PROOF_JSON, PROVER_CIRC_DATA_JSON, TARGETS_JSON, VERIFIER_CIRC_DATA_JSON};
use crate::serialization::file_paths::{PROOF_JSON, PROVER_CIRC_DATA_JSON, TARGETS_JSON, TREE_PROOF_JSON, VERIFIER_CIRC_DATA_JSON};
/// Writes the provided bytes to the specified file path using `std::fs::write`.
pub fn write_bytes_to_file<P: AsRef<Path>>(data: Vec<u8>, path: P) -> io::Result<()> {
@ -141,6 +139,17 @@ pub fn import_targets<T>() -> anyhow::Result<T>
Ok(targets)
}
/// Function to export tree proof with public input to json file
pub fn export_tree_proof_with_pi<F, C, const D: usize>(
proof_with_pis: &ProofWithPublicInputs<F, C, D>,
) -> anyhow::Result<()>
where
F: RichField + Extendable<D> + Poseidon2 + Serialize,
C: GenericConfig<D, F = F> + Serialize,
{
export_proof_with_pi_to_given_path(proof_with_pis, TREE_PROOF_JSON)
}
/// Function to export proof with public input to json file
pub fn export_proof_with_pi<F, C, const D: usize>(
proof_with_pis: &ProofWithPublicInputs<F, C, D>,
@ -148,25 +157,39 @@ pub fn export_proof_with_pi<F, C, const D: usize>(
where
F: RichField + Extendable<D> + Poseidon2 + Serialize,
C: GenericConfig<D, F = F> + Serialize,
{
export_proof_with_pi_to_given_path(proof_with_pis, PROOF_JSON)
}
/// Function to export proof with public input to json file
/// takes the path
fn export_proof_with_pi_to_given_path<F, C, const D: usize>(
proof_with_pis: &ProofWithPublicInputs<F, C, D>,
path: &str,
) -> anyhow::Result<()>
where
F: RichField + Extendable<D> + Poseidon2 + Serialize,
C: GenericConfig<D, F = F> + Serialize,
{
let proof_serialized= serde_json::to_vec(&proof_with_pis)
.map_err(|e| anyhow::anyhow!("Failed to serialize proof with public input: {:?}", e))?;
fs::write(PROOF_JSON , &proof_serialized).expect("Unable to write file");
fs::write(path , &proof_serialized).expect("Unable to write file");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::params::{C, D, F, HF, Params};
use std::time::Instant;
use codex_plonky2_circuits::circuits::sample_cells::SampleCircuit;
use codex_plonky2_circuits::circuits::sample_cells::{SampleCircuit, SampleCircuitInput};
use plonky2::plonk::circuit_data::{ ProverCircuitData, VerifierCircuitData};
use codex_plonky2_circuits::circuit_helper::Plonky2Circuit;
use codex_plonky2_circuits::circuits::utils::read_bytes_from_file;
use plonky2_poseidon2::serialization::{DefaultGateSerializer, DefaultGeneratorSerializer};
use crate::gen_input::verify_circuit_input;
use crate::gen_input::{gen_testing_circuit_input, verify_circuit_input};
use crate::serialization::circuit_input::{export_circ_input_to_json, generate_and_export_circ_input_to_json, import_circ_input_from_json};
use crate::serialization::json::{export_proof_with_pi, write_bytes_to_file};
// Test to generate the JSON file
#[test]