From 50ee8ad785c53232824e60b4ff6df32b69970358 Mon Sep 17 00:00:00 2001 From: dante <45801863+alexander-camuto@users.noreply.github.com> Date: Sat, 10 Dec 2022 19:26:51 +0000 Subject: [PATCH] chore: instance columns for poseidon bench (#108) --- halo2_gadgets/benches/poseidon.rs | 116 +++++++++--------------------- 1 file changed, 33 insertions(+), 83 deletions(-) diff --git a/halo2_gadgets/benches/poseidon.rs b/halo2_gadgets/benches/poseidon.rs index 744546129f..581b42fb73 100644 --- a/halo2_gadgets/benches/poseidon.rs +++ b/halo2_gadgets/benches/poseidon.rs @@ -3,9 +3,20 @@ use halo2_proofs::{ circuit::{Layouter, SimpleFloorPlanner, Value}, plonk::{ create_proof, keygen_pk, keygen_vk, verify_proof, Advice, Circuit, Column, - ConstraintSystem, Error, + ConstraintSystem, Error, Instance, + }, + poly::{ + commitment::ParamsProver, + ipa::{ + commitment::{IPACommitmentScheme, ParamsIPA}, + multiopen::ProverIPA, + strategy::SingleStrategy, + }, + VerificationStrategy, + }, + transcript::{ + Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer, }, - transcript::{Blake2bRead, Blake2bWrite, Challenge255}, }; use halo2curves::pasta::{pallas, vesta, EqAffine, Fp}; @@ -19,34 +30,19 @@ use std::marker::PhantomData; use criterion::{criterion_group, criterion_main, Criterion}; use rand::rngs::OsRng; -use halo2_proofs::{ - poly::{ - commitment::ParamsProver, - ipa::{ - commitment::{IPACommitmentScheme, ParamsIPA}, - multiopen::ProverIPA, - strategy::SingleStrategy, - }, - VerificationStrategy, - }, - transcript::{TranscriptReadBuffer, TranscriptWriterBuffer}, -}; - #[derive(Clone, Copy)] struct HashCircuit where S: Spec + Clone + Copy, { message: Value<[Fp; L]>, - // For the purpose of this test, witness the result. - // TODO: Move this into an instance column. - output: Value, _spec: PhantomData, } #[derive(Debug, Clone)] struct MyConfig { input: [Column; L], + expected: Column, poseidon_config: Pow5Config, } @@ -61,13 +57,14 @@ where fn without_witnesses(&self) -> Self { Self { message: Value::unknown(), - output: Value::unknown(), _spec: PhantomData, } } fn configure(meta: &mut ConstraintSystem) -> Self::Config { let state = (0..WIDTH).map(|_| meta.advice_column()).collect::>(); + let expected = meta.instance_column(); + meta.enable_equality(expected); let partial_sbox = meta.advice_column(); let rc_a = (0..WIDTH).map(|_| meta.fixed_column()).collect::>(); @@ -77,6 +74,7 @@ where Self::Config { input: state[..RATE].try_into().unwrap(), + expected, poseidon_config: Pow5Chip::configure::( meta, state.try_into().unwrap(), @@ -118,21 +116,14 @@ where )?; let output = hasher.hash(layouter.namespace(|| "hash"), message)?; - layouter.assign_region( - || "constrain output", - |mut region| { - let expected_var = - region.assign_advice(|| "load output", config.input[0], 0, || self.output)?; - region.constrain_equal(output.cell(), expected_var.cell()) - }, - ) + layouter.constrain_instance(output.cell(), config.expected, 0) } } #[derive(Debug, Clone, Copy)] struct MySpec; -impl Spec for MySpec<3, 2> { +impl Spec for MySpec { fn full_rounds() -> usize { 8 } @@ -150,43 +141,7 @@ impl Spec for MySpec<3, 2> { } } -impl Spec for MySpec<9, 8> { - fn full_rounds() -> usize { - 8 - } - - fn partial_rounds() -> usize { - 56 - } - - fn sbox(val: Fp) -> Fp { - val.pow_vartime(&[5]) - } - - fn secure_mds() -> usize { - 0 - } -} - -impl Spec for MySpec<12, 11> { - fn full_rounds() -> usize { - 8 - } - - fn partial_rounds() -> usize { - 56 - } - - fn sbox(val: Fp) -> Fp { - val.pow_vartime(&[5]) - } - - fn secure_mds() -> usize { - 0 - } -} - -const K: u32 = 6; +const K: u32 = 7; fn bench_poseidon( name: &str, @@ -199,7 +154,6 @@ fn bench_poseidon( let empty_circuit = HashCircuit:: { message: Value::unknown(), - output: Value::unknown(), _spec: PhantomData, }; @@ -211,7 +165,7 @@ fn bench_poseidon( let verifier_name = name.to_string() + "-verifier"; let mut rng = OsRng; - let message = (0..L) + let message: [Fp; L] = (0..L) .map(|_| pallas::Base::random(rng)) .collect::>() .try_into() @@ -220,19 +174,19 @@ fn bench_poseidon( let circuit = HashCircuit:: { message: Value::known(message), - output: Value::known(output), _spec: PhantomData, }; + // Create a proof + let mut transcript = Blake2bWrite::<_, EqAffine, Challenge255<_>>::init(vec![]); + c.bench_function(&prover_name, |b| { b.iter(|| { - // Create a proof - let mut transcript = Blake2bWrite::<_, EqAffine, Challenge255<_>>::init(vec![]); create_proof::, ProverIPA<_>, _, _, _, _>( ¶ms, &pk, &[circuit], - &[&[]], + &[&[&[output]]], &mut rng, &mut transcript, ) @@ -240,24 +194,20 @@ fn bench_poseidon( }) }); - // Create a proof - let mut transcript = Blake2bWrite::<_, EqAffine, Challenge255<_>>::init(vec![]); - create_proof::, ProverIPA<_>, _, _, _, _>( - ¶ms, - &pk, - &[circuit], - &[&[]], - &mut rng, - &mut transcript, - ) - .expect("proof generation should not fail"); let proof = transcript.finalize(); c.bench_function(&verifier_name, |b| { b.iter(|| { let strategy = SingleStrategy::new(¶ms); let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(&proof[..]); - assert!(verify_proof(¶ms, pk.get_vk(), strategy, &[&[]], &mut transcript).is_ok()); + assert!(verify_proof( + ¶ms, + pk.get_vk(), + strategy, + &[&[&[output]]], + &mut transcript + ) + .is_ok()); }); }); }