Skip to content

Commit

Permalink
feat(keccak): use configure_with_params
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpwang committed Aug 15, 2023
1 parent b45ced8 commit a73f2f8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
6 changes: 1 addition & 5 deletions hashes/zkevm-keccak/src/keccak_packed_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use halo2_base::halo2_proofs::{circuit::AssignedCell, plonk::Assigned};
use itertools::Itertools;
use log::{debug, info};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use std::{cell::RefCell, marker::PhantomData};
use std::marker::PhantomData;

#[cfg(test)]
mod tests;
Expand All @@ -31,10 +31,6 @@ const THETA_C_LOOKUP_RANGE: usize = 6;
const RHO_PI_LOOKUP_RANGE: usize = 4;
const CHI_BASE_LOOKUP_RANGE: usize = 5;

thread_local! {
pub static KECCAK_CONFIG_PARAMS: RefCell<KeccakConfigParams> = RefCell::new(Default::default());
}

fn get_num_bits_per_absorb_lookup(k: u32) -> usize {
get_num_bits_per_lookup(ABSORB_LOOKUP_RANGE, k)
}
Expand Down
39 changes: 23 additions & 16 deletions hashes/zkevm-keccak/src/keccak_packed_multi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use test_case::test_case;
/// KeccakCircuit
#[derive(Default, Clone, Debug)]
pub struct KeccakCircuit<F: Field> {
config: KeccakConfigParams,
inputs: Vec<Vec<u8>>,
num_rows: Option<usize>,
_marker: PhantomData<F>,
Expand All @@ -34,20 +35,28 @@ pub struct KeccakCircuit<F: Field> {
impl<F: Field> Circuit<F> for KeccakCircuit<F> {
type Config = KeccakCircuitConfig<F>;
type FloorPlanner = SimpleFloorPlanner;
type Params = KeccakConfigParams;

fn params(&self) -> Self::Params {
self.config
}

fn without_witnesses(&self) -> Self {
Self::default()
}

fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config {
fn configure_with_params(meta: &mut ConstraintSystem<F>, params: Self::Params) -> Self::Config {
// MockProver complains if you only have columns in SecondPhase, so let's just make an empty column in FirstPhase
meta.advice_column();

let challenge = meta.challenge_usable_after(FirstPhase);
let params = KECCAK_CONFIG_PARAMS.with(|conf| *conf.borrow());
KeccakCircuitConfig::new(meta, challenge, params)
}

fn configure(_: &mut ConstraintSystem<F>) -> Self::Config {
unreachable!()
}

fn synthesize(
&self,
config: Self::Config,
Expand Down Expand Up @@ -95,24 +104,25 @@ impl<F: Field> Circuit<F> for KeccakCircuit<F> {

impl<F: Field> KeccakCircuit<F> {
/// Creates a new circuit instance
pub fn new(num_rows: Option<usize>, inputs: Vec<Vec<u8>>) -> Self {
KeccakCircuit { inputs, num_rows, _marker: PhantomData }
pub fn new(config: KeccakConfigParams, num_rows: Option<usize>, inputs: Vec<Vec<u8>>) -> Self {
KeccakCircuit { config, inputs, num_rows, _marker: PhantomData }
}
}

fn verify<F: Field + Ord + FromUniformBytes<64>>(k: u32, inputs: Vec<Vec<u8>>, _success: bool) {
let circuit = KeccakCircuit::new(Some(2usize.pow(k) - 109), inputs);
fn verify<F: Field + Ord + FromUniformBytes<64>>(
config: KeccakConfigParams,
inputs: Vec<Vec<u8>>,
_success: bool,
) {
let k = config.k;
let circuit = KeccakCircuit::new(config, Some(2usize.pow(k) - 109), inputs);

let prover = MockProver::<F>::run(k, &circuit, vec![]).unwrap();
prover.assert_satisfied();
}

#[test_case(14, 28; "k: 14, rows_per_round: 28")]
fn packed_multi_keccak_simple(k: u32, rows_per_round: usize) {
KECCAK_CONFIG_PARAMS.with(|conf| {
conf.borrow_mut().k = k;
conf.borrow_mut().rows_per_round = rows_per_round;
});
let _ = env_logger::builder().is_test(true).try_init();

let inputs = vec![
Expand All @@ -122,16 +132,12 @@ fn packed_multi_keccak_simple(k: u32, rows_per_round: usize) {
(0u8..136).collect::<Vec<_>>(),
(0u8..200).collect::<Vec<_>>(),
];
verify::<Fr>(k, inputs, true);
verify::<Fr>(KeccakConfigParams { k, rows_per_round }, inputs, true);
}

#[test_case(14, 25 ; "k: 14, rows_per_round: 25")]
#[test_case(18, 9 ; "k: 18, rows_per_round: 9")]
fn packed_multi_keccak_prover(k: u32, rows_per_round: usize) {
KECCAK_CONFIG_PARAMS.with(|conf| {
conf.borrow_mut().k = k;
conf.borrow_mut().rows_per_round = rows_per_round;
});
let _ = env_logger::builder().is_test(true).try_init();

let params = ParamsKZG::<Bn256>::setup(k, OsRng);
Expand All @@ -143,7 +149,8 @@ fn packed_multi_keccak_prover(k: u32, rows_per_round: usize) {
(0u8..136).collect::<Vec<_>>(),
(0u8..200).collect::<Vec<_>>(),
];
let circuit = KeccakCircuit::new(Some(2usize.pow(k)), inputs);
let circuit =
KeccakCircuit::new(KeccakConfigParams { k, rows_per_round }, Some(2usize.pow(k)), inputs);

let vk = keygen_vk(&params, &circuit).unwrap();
let pk = keygen_pk(&params, vk, &circuit).unwrap();
Expand Down

0 comments on commit a73f2f8

Please sign in to comment.