Skip to content

Commit

Permalink
Add testing & refactor folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
nyunyunyunyu committed Sep 5, 2023
1 parent 27abb20 commit 05c5573
Show file tree
Hide file tree
Showing 16 changed files with 1,459 additions and 1,326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use super::{
param::*,
};
use crate::{
keccak::{
keccak_packed_multi::get_num_keccak_f, multi_keccak, param::*, KeccakAssignedRow,
keccak::native::{
keccak_packed_multi::get_num_keccak_f, param::*, witness::multi_keccak, KeccakAssignedRow,
KeccakCircuitConfig, KeccakConfigParams,
},
util::eth_types::Field,
};
use getset::Getters;
use getset::{CopyGetters, Getters};
use halo2_base::{
gates::{
circuit::{builder::BaseCircuitBuilder, BaseCircuitParams, BaseConfig},
Expand All @@ -33,30 +33,31 @@ pub struct KeccakCoprocessorCircuit<F: Field> {
inputs: Vec<Vec<u8>>,

/// Parameters of this circuit. The same parameters always construct the same circuit.
#[getset(get = "pub")]
params: KeccakCoprocessorCircuitParams,

base_circuit_builder: RefCell<BaseCircuitBuilder<F>>,
hasher: RefCell<PoseidonHasher<F, POSEIDON_T, POSEIDON_RATE>>,
}

/// Parameters of KeccakCoprocessorCircuit.
#[derive(Default, Clone, Getters)]
#[derive(Default, Clone, CopyGetters)]
pub struct KeccakCoprocessorCircuitParams {
/// This circuit has 2^k rows.
#[getset(get = "pub")]
#[getset(get_copy = "pub")]
k: usize,
// Number of unusable rows withhold by Halo2.
#[getset(get = "pub")]
#[getset(get_copy = "pub")]
num_unusable_row: usize,
/// The bits of lookup table for RangeChip.
#[getset(get = "pub")]
#[getset(get_copy = "pub")]
lookup_bits: usize,
/// Max keccak_f this circuits can aceept. The circuit can at most process <capacity> of inputs
/// with < NUM_BYTES_TO_ABSORB bytes or an input with <capacity> * NUM_BYTES_TO_ABSORB - 1 bytes.
#[getset(get = "pub")]
#[getset(get_copy = "pub")]
capacity: usize,
// If true, publish raw outputs. Otherwise, publish Poseidon commitment of raw outputs.
#[getset(get = "pub")]
#[getset(get_copy = "pub")]
publish_raw_outputs: bool,

// Derived parameters of sub-circuits.
Expand All @@ -74,7 +75,7 @@ impl KeccakCoprocessorCircuitParams {
) -> Self {
assert!(1 << k > num_unusable_row, "Number of unusable rows must be less than 2^k");
let max_rows = (1 << k) - num_unusable_row;
// Derived from [crate::keccak::keccak_packed_multi::get_keccak_capacity].
// Derived from [crate::keccak::native_circuit::keccak_packed_multi::get_keccak_capacity].
let rows_per_round = max_rows / (capacity * (NUM_ROUNDS + 1) + 1 + NUM_WORDS_TO_ABSORB);
assert!(rows_per_round > 0, "No enough rows for the speficied capacity");
let keccak_circuit_params = KeccakConfigParams { k: k as u32, rows_per_round };
Expand Down Expand Up @@ -157,7 +158,7 @@ impl<F: Field> Circuit<F> for KeccakCoprocessorCircuit<F> {
}

/// Witnesses to be exposed as circuit outputs.
#[derive(Clone)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct KeccakCircuitOutput<E> {
/// Key for App circuits to lookup keccak hash.
pub key: E,
Expand Down Expand Up @@ -341,7 +342,6 @@ impl<F: Field> KeccakCoprocessorCircuit<F> {
dummy_keccak_hi_witness,
loaded_keccak_f.is_final,
);
println!("In circuit: {:?}", key.value());
circuit_final_outputs.push(KeccakCircuitOutput { key, hash_lo, hash_hi });
}
circuit_final_outputs
Expand Down Expand Up @@ -388,13 +388,13 @@ impl<F: Field> KeccakCoprocessorCircuit<F> {
/// Return circuit outputs of the specified Keccak corprocessor circuit for a specified input.
pub fn multi_inputs_to_circuit_outputs<F: Field>(
inputs: &[Vec<u8>],
params: &KeccakCoprocessorCircuitParams,
capacity: usize,
) -> Vec<KeccakCircuitOutput<F>> {
assert!(u128::BITS <= F::CAPACITY);
let mut outputs =
inputs.iter().flat_map(|input| input_to_circuit_outputs::<F>(input)).collect_vec();
assert!(outputs.len() <= params.capacity);
outputs.resize(params.capacity, dummy_circuit_output());
assert!(outputs.len() <= capacity);
outputs.resize(capacity, dummy_circuit_output());
outputs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use halo2_base::{
};
use itertools::Itertools;

use crate::{keccak::param::*, util::eth_types::Field};
use crate::{keccak::native::param::*, util::eth_types::Field};

use super::{circuit::LoadedKeccakF, param::*};

Expand Down
File renamed without changes.
File renamed without changes.
131 changes: 131 additions & 0 deletions hashes/zkevm/src/keccak/coprocessor/tests/circuit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use crate::keccak::coprocessor::circuit::{
dummy_circuit_output, input_to_circuit_outputs, multi_inputs_to_circuit_outputs,
KeccakCircuitOutput,
};
use halo2_base::halo2_proofs::halo2curves::{bn256::Fr, ff::PrimeField};
use itertools::Itertools;
use lazy_static::lazy_static;

lazy_static! {
static ref OUTPUT_EMPTY: KeccakCircuitOutput<Fr> = KeccakCircuitOutput {
key: Fr::from_raw([
0x54595a1525d3534a,
0xf90e160f1b4648ef,
0x34d557ddfb89da5d,
0x04ffe3d4b8885928,
]),
hash_lo: Fr::from_u128(0xe500b653ca82273b7bfad8045d85a470),
hash_hi: Fr::from_u128(0xc5d2460186f7233c927e7db2dcc703c0),
};
static ref OUTPUT_0: KeccakCircuitOutput<Fr> = KeccakCircuitOutput {
key: Fr::from_raw([
0xc009f26a12e2f494,
0xb4a9d43c17609251,
0x68068b5344cba120,
0x1531327ea92d38ba,
]),
hash_lo: Fr::from_u128(0x6612f7b477d66591ff96a9e064bcc98a),
hash_hi: Fr::from_u128(0xbc36789e7a1e281436464229828f817d),
};
static ref OUTPUT_0_135: KeccakCircuitOutput<Fr> = KeccakCircuitOutput {
key: Fr::from_raw([
0x9a88287adab4da1c,
0xe9ff61b507cfd8c2,
0xdbf697a6a3ad66a1,
0x1eb1d5cc8cdd1532,
]),
hash_lo: Fr::from_u128(0x290b0e1706f6a82e5a595b9ce9faca62),
hash_hi: Fr::from_u128(0xcbdfd9dee5faad3818d6b06f95a219fd),
};
static ref OUTPUT_0_136: KeccakCircuitOutput<Fr> = KeccakCircuitOutput {
key: Fr::from_raw([
0x39c1a578acb62676,
0x0dc19a75e610c062,
0x3f158e809150a14a,
0x2367059ac8c80538,
]),
hash_lo: Fr::from_u128(0xff11fe3e38e17df89cf5d29c7d7f807e),
hash_hi: Fr::from_u128(0x7ce759f1ab7f9ce437719970c26b0a66),
};
static ref OUTPUT_0_200: KeccakCircuitOutput<Fr> = KeccakCircuitOutput {
key: Fr::from_raw([
0x379bfca638552583,
0x1bf7bd603adec30e,
0x05efe90ad5dbd814,
0x053c729cb8908ccb,
]),
hash_lo: Fr::from_u128(0xb4543f3d2703c0923c6901c2af57b890),
hash_hi: Fr::from_u128(0xbfb0aa97863e797943cf7c33bb7e880b),
};
}

#[test]
fn test_dummy_circuit_output() {
let KeccakCircuitOutput { key, hash_lo, hash_hi } = dummy_circuit_output::<Fr>();
assert_eq!(key, OUTPUT_EMPTY.key);
assert_eq!(hash_lo, OUTPUT_EMPTY.hash_lo);
assert_eq!(hash_hi, OUTPUT_EMPTY.hash_hi);
}

#[test]
fn test_input_to_circuit_outputs_empty() {
let result = input_to_circuit_outputs::<Fr>(&[]);
assert_eq!(result, vec![*OUTPUT_EMPTY]);
}

#[test]
fn test_input_to_circuit_outputs_1_keccak_f() {
let result = input_to_circuit_outputs::<Fr>(&[0]);
assert_eq!(result, vec![*OUTPUT_0]);
}

#[test]
fn test_input_to_circuit_outputs_1_keccak_f_full() {
let result = input_to_circuit_outputs::<Fr>(&(0..135).collect_vec());
assert_eq!(result, vec![*OUTPUT_0_135]);
}

#[test]
fn test_input_to_circuit_outputs_2_keccak_f_2nd_empty() {
let result = input_to_circuit_outputs::<Fr>(&(0..136).collect_vec());
assert_eq!(result, vec![*OUTPUT_EMPTY, *OUTPUT_0_136]);
}

#[test]
fn test_input_to_circuit_outputs_2_keccak_f() {
let result = input_to_circuit_outputs::<Fr>(&(0..200).collect_vec());
assert_eq!(result, vec![*OUTPUT_EMPTY, *OUTPUT_0_200]);
}

#[test]
fn test_multi_input_to_circuit_outputs() {
let results = multi_inputs_to_circuit_outputs::<Fr>(
&[(0..135).collect_vec(), (0..200).collect_vec(), vec![], vec![0], (0..136).collect_vec()],
10,
);
assert_eq!(
results,
vec![
*OUTPUT_0_135,
*OUTPUT_EMPTY,
*OUTPUT_0_200,
*OUTPUT_EMPTY,
*OUTPUT_0,
*OUTPUT_EMPTY,
*OUTPUT_0_136,
// Padding
*OUTPUT_EMPTY,
*OUTPUT_EMPTY,
*OUTPUT_EMPTY,
]
);
}

#[test]
#[should_panic]
fn test_multi_input_to_circuit_outputs_exceed_capacity() {
let _ = multi_inputs_to_circuit_outputs::<Fr>(
&[(0..135).collect_vec(), (0..200).collect_vec(), vec![], vec![0], (0..136).collect_vec()],
2,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use super::circuit::{
use halo2_base::halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr};
use itertools::Itertools;

#[cfg(test)]
mod circuit;

#[test]
fn test() {
fn test_mock_leaf_circuit() {
let k: usize = 18;
let num_unusable_row: usize = 109;
let lookup_bits: usize = 4;
Expand All @@ -30,7 +33,7 @@ fn test() {
publish_raw_outputs,
);
let circuit = KeccakCoprocessorCircuit::<Fr>::new(inputs.clone(), params.clone());
let circuit_outputs = multi_inputs_to_circuit_outputs::<Fr>(&inputs, &params);
let circuit_outputs = multi_inputs_to_circuit_outputs::<Fr>(&inputs, params.capacity());

let instances = vec![
circuit_outputs.iter().map(|o| o.key).collect_vec(),
Expand Down
Loading

0 comments on commit 05c5573

Please sign in to comment.