From 65508f3da935d8c87c5bb55892c20a4435ee7eea Mon Sep 17 00:00:00 2001 From: M Alghazwi Date: Thu, 3 Apr 2025 11:15:09 +0200 Subject: [PATCH] clean up tests and bench --- plonky2_poseidon2/Cargo.toml | 2 +- plonky2_poseidon2/benches/poseidon2_perm.rs | 14 +++++-------- plonky2_poseidon2/src/gate/poseidon2.rs | 21 +++++++++++-------- .../poseidon2_hash/poseidon2_goldilocks.rs | 5 +---- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/plonky2_poseidon2/Cargo.toml b/plonky2_poseidon2/Cargo.toml index 0b82beb..7f98333 100644 --- a/plonky2_poseidon2/Cargo.toml +++ b/plonky2_poseidon2/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" [dependencies] anyhow = { workspace = true } unroll = { workspace = true } -plonky2 = { workspace = true } +plonky2 = { workspace = true , features = ["gate_testing"]} plonky2_field = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/plonky2_poseidon2/benches/poseidon2_perm.rs b/plonky2_poseidon2/benches/poseidon2_perm.rs index 0c90383..2b5fa18 100644 --- a/plonky2_poseidon2/benches/poseidon2_perm.rs +++ b/plonky2_poseidon2/benches/poseidon2_perm.rs @@ -1,6 +1,4 @@ -use std::fs; use anyhow::Result; -use std::time::Instant; use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; use plonky2::field::extension::Extendable; use plonky2::field::goldilocks_field::GoldilocksField; @@ -33,7 +31,6 @@ pub struct PoseidonCircuit< > { public_input: Vec, circuit_data: CircuitData, - num_powers: usize, _hasher: PhantomData, } @@ -60,7 +57,7 @@ impl< state.set_from_slice(&initial, 0); - for k in 0..num_hashes { + for _k in 0..num_hashes { state = builder.permute::(state); } @@ -74,17 +71,16 @@ impl< Self { public_input: initial, circuit_data: data, - num_powers: num_hashes, _hasher: PhantomData::, } } - pub fn generate_proof(&self, init: F) -> Result> { + pub fn generate_proof(&self, init: Vec) -> Result> { const T: usize = 12; let mut pw = PartialWitness::::new(); for j in 0..T { - pw.set_target(self.public_input[j], F::from_canonical_usize(j)); + pw.set_target(self.public_input[j], init[j])?; } let proof = self.circuit_data.prove(pw).unwrap(); @@ -135,14 +131,14 @@ fn bench_poseidon2_perm< format!("prove circuit with 2^{} permutations", log_num_hashes).as_str(), |b| { b.iter_batched( - || F::rand(), + || F::rand_vec(12), |init| poseidon_circuit.generate_proof(init).unwrap(), BatchSize::PerIteration, ) }, ); - let proof = poseidon_circuit.generate_proof(F::rand()).unwrap(); + let proof = poseidon_circuit.generate_proof(F::rand_vec(12)).unwrap(); pretty_print!("proof size: {}", proof.to_bytes().len()); diff --git a/plonky2_poseidon2/src/gate/poseidon2.rs b/plonky2_poseidon2/src/gate/poseidon2.rs index 5a5ae8b..5d1ba2a 100644 --- a/plonky2_poseidon2/src/gate/poseidon2.rs +++ b/plonky2_poseidon2/src/gate/poseidon2.rs @@ -517,7 +517,7 @@ mod tests { } #[test] - fn generated_output() { + fn generated_output() -> Result<()>{ const D: usize = 2; type C = Poseidon2GoldilocksConfig; type F = >::F; @@ -547,7 +547,7 @@ mod tests { column: Gate::WIRE_SWAP, }, F::ZERO, - ); + )?; for i in 0..SPONGE_WIDTH { inputs.set_wire( Wire { @@ -555,7 +555,7 @@ mod tests { column: Gate::wire_input(i), }, permutation_inputs[i], - ); + )?; } let witness = generate_partial_witness(inputs, &circuit.prover_only, &circuit.common).unwrap(); @@ -568,7 +568,9 @@ mod tests { }); println!("out {} = {}", i, out.clone()); assert_eq!(out, expected_outputs[i]); - } + }; + + Ok(()) } #[test] @@ -588,7 +590,7 @@ mod tests { } #[test] - fn test_proof() { + fn test_proof() -> Result<()>{ use plonky2_field::types::Sample; use plonky2::gates::gate::Gate; use plonky2::hash::hash_types::HashOut; @@ -607,10 +609,10 @@ mod tests { let wires_t = builder.add_virtual_extension_targets(wires.len()); let constants_t = builder.add_virtual_extension_targets(constants.len()); - pw.set_extension_targets(&wires_t, &wires); - pw.set_extension_targets(&constants_t, &constants); + pw.set_extension_targets(&wires_t, &wires)?; + pw.set_extension_targets(&constants_t, &constants)?; let public_inputs_hash_t = builder.add_virtual_hash(); - pw.set_hash_target(public_inputs_hash_t, public_inputs_hash); + pw.set_hash_target(public_inputs_hash_t, public_inputs_hash)?; let vars = EvaluationVars { local_constants: &constants, @@ -625,9 +627,10 @@ mod tests { public_inputs_hash: &public_inputs_hash_t, }; let evals_t = gate.eval_unfiltered_circuit(&mut builder, vars_t); - pw.set_extension_targets(&evals_t, &evals); + pw.set_extension_targets(&evals_t, &evals)?; let data = builder.build::(); let proof = data.prove(pw); assert!(proof.is_ok()); + Ok(()) } } \ No newline at end of file diff --git a/plonky2_poseidon2/src/poseidon2_hash/poseidon2_goldilocks.rs b/plonky2_poseidon2/src/poseidon2_hash/poseidon2_goldilocks.rs index 8203a85..bb20cde 100644 --- a/plonky2_poseidon2/src/poseidon2_hash/poseidon2_goldilocks.rs +++ b/plonky2_poseidon2/src/poseidon2_hash/poseidon2_goldilocks.rs @@ -151,18 +151,15 @@ impl Poseidon2 for GoldilocksField { mod tests { use plonky2_field::goldilocks_field::GoldilocksField as F; - use plonky2_field::types::{Field, PrimeField64}; use crate::poseidon2_hash::poseidon2::test_helpers::check_test_vectors; #[test] - fn p2new_test_vectors() { + fn test_vectors() { // Test inputs are: // 1. range 0..WIDTH // expected output calculated with reference implementation here: // https://github.com/HorizenLabs/poseidon2 - let neg_one: u64 = F::NEG_ONE.to_canonical_u64(); - #[rustfmt::skip] let test_vectors12: Vec<([u64; 12], [u64; 12])> = vec![ ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ],