Skip to content

Commit

Permalink
rename: (coprocessor,leaf) -> (component,shard)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpwang committed Sep 18, 2023
1 parent 17d297b commit 5500e46
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 113 deletions.
137 changes: 77 additions & 60 deletions hashes/zkevm/src/keccak/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod leaf;
pub mod shard;
#[cfg(test)]
mod tests;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cell::RefCell;

use crate::{
keccak::{
coprocessor::{
component::{
encode::{
get_words_to_witness_multipliers, num_poseidon_absorb_per_keccak_f,
num_word_per_witness,
Expand Down Expand Up @@ -38,23 +38,23 @@ use halo2_base::{
};
use itertools::Itertools;

/// Keccak Coprocessor Leaf Circuit
/// Keccak Component Shard Circuit
#[derive(Getters)]
pub struct KeccakCoprocessorLeafCircuit<F: Field> {
pub struct KeccakComponentShardCircuit<F: Field> {
inputs: Vec<Vec<u8>>,

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

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

/// Parameters of KeccakCoprocessorLeafCircuit.
/// Parameters of KeccakComponentCircuit.
#[derive(Default, Clone, CopyGetters)]
pub struct KeccakCoprocessorLeafCircuitParams {
pub struct KeccakComponentShardCircuitParams {
/// This circuit has 2^k rows.
#[getset(get_copy = "pub")]
k: usize,
Expand All @@ -74,8 +74,8 @@ pub struct KeccakCoprocessorLeafCircuitParams {
pub base_circuit_params: BaseCircuitParams,
}

impl KeccakCoprocessorLeafCircuitParams {
/// Create a new KeccakCoprocessorLeafCircuitParams.
impl KeccakComponentShardCircuitParams {
/// Create a new KeccakComponentShardCircuitParams.
pub fn new(
k: usize,
num_unusable_row: usize,
Expand Down Expand Up @@ -109,17 +109,17 @@ impl KeccakCoprocessorLeafCircuitParams {
}
}

/// Circuit::Config for Keccak Coprocessor Leaf Circuit.
/// Circuit::Config for Keccak Component Shard Circuit.
#[derive(Clone)]
pub struct KeccakCoprocessorLeafConfig<F: Field> {
pub struct KeccakComponentShardConfig<F: Field> {
pub base_circuit_config: BaseConfig<F>,
pub keccak_circuit_config: KeccakCircuitConfig<F>,
}

impl<F: Field> Circuit<F> for KeccakCoprocessorLeafCircuit<F> {
type Config = KeccakCoprocessorLeafConfig<F>;
impl<F: Field> Circuit<F> for KeccakComponentShardCircuit<F> {
type Config = KeccakComponentShardConfig<F>;
type FloorPlanner = SimpleFloorPlanner;
type Params = KeccakCoprocessorLeafCircuitParams;
type Params = KeccakComponentShardCircuitParams;

fn params(&self) -> Self::Params {
self.params.clone()
Expand Down Expand Up @@ -212,11 +212,11 @@ impl<F: Field> LoadedKeccakF<F> {
}
}

impl<F: Field> KeccakCoprocessorLeafCircuit<F> {
/// Create a new KeccakCoprocessorLeafCircuit.
impl<F: Field> KeccakComponentShardCircuit<F> {
/// Create a new KeccakComponentShardCircuit.
pub fn new(
inputs: Vec<Vec<u8>>,
params: KeccakCoprocessorLeafCircuitParams,
params: KeccakComponentShardCircuitParams,
witness_gen_only: bool,
) -> Self {
let input_size = inputs.iter().map(|input| get_num_keccak_f(input.len())).sum::<usize>();
Expand Down Expand Up @@ -250,7 +250,7 @@ impl<F: Field> KeccakCoprocessorLeafCircuit<F> {
/// Simulate witness generation of the base circuit to determine BaseCircuitParams because the number of columns
/// of the base circuit can only be known after witness generation.
pub fn calculate_base_circuit_params(
params: &KeccakCoprocessorLeafCircuitParams,
params: &KeccakComponentShardCircuitParams,
) -> BaseCircuitParams {
// Create a simulation circuit to calculate base circuit parameters.
let simulation_circuit = Self::new(vec![], params.clone(), false);
Expand Down
1 change: 1 addition & 0 deletions hashes/zkevm/src/keccak/component/circuit/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod shard;
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{
halo2curves::bn256::Fr,
plonk::{keygen_pk, keygen_vk},
},
keccak::coprocessor::{
circuit::leaf::{KeccakCoprocessorLeafCircuit, KeccakCoprocessorLeafCircuitParams},
keccak::component::{
circuit::shard::{KeccakComponentShardCircuit, KeccakComponentShardCircuitParams},
output::{calculate_circuit_outputs_commit, multi_inputs_to_circuit_outputs},
},
};
Expand All @@ -19,7 +19,7 @@ use itertools::Itertools;
use rand_core::OsRng;

#[test]
fn test_mock_leaf_circuit_raw_outputs() {
fn test_mock_shard_circuit_raw_outputs() {
let k: usize = 18;
let num_unusable_row: usize = 109;
let capacity: usize = 10;
Expand All @@ -35,11 +35,11 @@ fn test_mock_leaf_circuit_raw_outputs() {
];

let mut params =
KeccakCoprocessorLeafCircuitParams::new(k, num_unusable_row, capacity, publish_raw_outputs);
KeccakComponentShardCircuitParams::new(k, num_unusable_row, capacity, publish_raw_outputs);
let base_circuit_params =
KeccakCoprocessorLeafCircuit::<Fr>::calculate_base_circuit_params(&params);
KeccakComponentShardCircuit::<Fr>::calculate_base_circuit_params(&params);
params.base_circuit_params = base_circuit_params;
let circuit = KeccakCoprocessorLeafCircuit::<Fr>::new(inputs.clone(), params.clone(), false);
let circuit = KeccakComponentShardCircuit::<Fr>::new(inputs.clone(), params.clone(), false);
let circuit_outputs = multi_inputs_to_circuit_outputs::<Fr>(&inputs, params.capacity());

let instances = vec![
Expand All @@ -53,7 +53,7 @@ fn test_mock_leaf_circuit_raw_outputs() {
}

#[test]
fn test_prove_leaf_circuit_raw_outputs() {
fn test_prove_shard_circuit_raw_outputs() {
let _ = env_logger::builder().is_test(true).try_init();

let k: usize = 18;
Expand All @@ -63,11 +63,11 @@ fn test_prove_leaf_circuit_raw_outputs() {

let inputs = vec![];
let mut circuit_params =
KeccakCoprocessorLeafCircuitParams::new(k, num_unusable_row, capacity, publish_raw_outputs);
KeccakComponentShardCircuitParams::new(k, num_unusable_row, capacity, publish_raw_outputs);
let base_circuit_params =
KeccakCoprocessorLeafCircuit::<Fr>::calculate_base_circuit_params(&circuit_params);
KeccakComponentShardCircuit::<Fr>::calculate_base_circuit_params(&circuit_params);
circuit_params.base_circuit_params = base_circuit_params;
let circuit = KeccakCoprocessorLeafCircuit::<Fr>::new(inputs, circuit_params.clone(), false);
let circuit = KeccakComponentShardCircuit::<Fr>::new(inputs, circuit_params.clone(), false);

let params = ParamsKZG::<Bn256>::setup(k as u32, OsRng);

Expand All @@ -90,7 +90,7 @@ fn test_prove_leaf_circuit_raw_outputs() {
];

let break_points = circuit.base_circuit_break_points();
let circuit = KeccakCoprocessorLeafCircuit::<Fr>::new(inputs, circuit_params, true);
let circuit = KeccakComponentShardCircuit::<Fr>::new(inputs, circuit_params, true);
circuit.set_base_circuit_break_points(break_points);

let proof = gen_proof_with_instances(
Expand All @@ -109,7 +109,7 @@ fn test_prove_leaf_circuit_raw_outputs() {
}

#[test]
fn test_mock_leaf_circuit_commit() {
fn test_mock_shard_circuit_commit() {
let k: usize = 18;
let num_unusable_row: usize = 109;
let capacity: usize = 10;
Expand All @@ -125,11 +125,11 @@ fn test_mock_leaf_circuit_commit() {
];

let mut params =
KeccakCoprocessorLeafCircuitParams::new(k, num_unusable_row, capacity, publish_raw_outputs);
KeccakComponentShardCircuitParams::new(k, num_unusable_row, capacity, publish_raw_outputs);
let base_circuit_params =
KeccakCoprocessorLeafCircuit::<Fr>::calculate_base_circuit_params(&params);
KeccakComponentShardCircuit::<Fr>::calculate_base_circuit_params(&params);
params.base_circuit_params = base_circuit_params;
let circuit = KeccakCoprocessorLeafCircuit::<Fr>::new(inputs.clone(), params.clone(), false);
let circuit = KeccakComponentShardCircuit::<Fr>::new(inputs.clone(), params.clone(), false);
let circuit_outputs = multi_inputs_to_circuit_outputs::<Fr>(&inputs, params.capacity());

let instances = vec![vec![calculate_circuit_outputs_commit(&circuit_outputs)]];
Expand All @@ -139,7 +139,7 @@ fn test_mock_leaf_circuit_commit() {
}

#[test]
fn test_prove_leaf_circuit_commit() {
fn test_prove_shard_circuit_commit() {
let _ = env_logger::builder().is_test(true).try_init();

let k: usize = 18;
Expand All @@ -149,11 +149,11 @@ fn test_prove_leaf_circuit_commit() {

let inputs = vec![];
let mut circuit_params =
KeccakCoprocessorLeafCircuitParams::new(k, num_unusable_row, capacity, publish_raw_outputs);
KeccakComponentShardCircuitParams::new(k, num_unusable_row, capacity, publish_raw_outputs);
let base_circuit_params =
KeccakCoprocessorLeafCircuit::<Fr>::calculate_base_circuit_params(&circuit_params);
KeccakComponentShardCircuit::<Fr>::calculate_base_circuit_params(&circuit_params);
circuit_params.base_circuit_params = base_circuit_params;
let circuit = KeccakCoprocessorLeafCircuit::<Fr>::new(inputs, circuit_params.clone(), false);
let circuit = KeccakComponentShardCircuit::<Fr>::new(inputs, circuit_params.clone(), false);

let params = ParamsKZG::<Bn256>::setup(k as u32, OsRng);

Expand All @@ -171,7 +171,7 @@ fn test_prove_leaf_circuit_commit() {

let break_points = circuit.base_circuit_break_points();
let circuit =
KeccakCoprocessorLeafCircuit::<Fr>::new(inputs.clone(), circuit_params.clone(), true);
KeccakComponentShardCircuit::<Fr>::new(inputs.clone(), circuit_params.clone(), true);
circuit.set_base_circuit_break_points(break_points);

let circuit_outputs = multi_inputs_to_circuit_outputs::<Fr>(&inputs, circuit_params.capacity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ use crate::{

use super::param::*;

// TODO: Abstract this module into a trait for all coprocessor circuits.
// TODO: Abstract this module into a trait for all component circuits.

/// Module to encode raw inputs into lookup keys for looking up keccak results. The encoding is
/// designed to be efficient in coprocessor circuits.
/// designed to be efficient in component circuits.
/// Encode a native input bytes into its corresponding lookup key. This function can be considered as the spec of the encoding.
pub fn encode_native_input<F: Field>(bytes: &[u8]) -> F {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ethers_core::{types::H256, utils::keccak256};
use crate::keccak::vanilla::param::NUM_BYTES_TO_ABSORB;

/// Fixed length format for one keccak_f.
/// This closely matches [zkevm_hashes::keccak::coprocessor::circuit::leaf::LoadedKeccakF].
/// This closely matches [crate::keccak::component::circuit::shard::LoadedKeccakF].
#[derive(Clone, Debug)]
pub struct KeccakIngestionFormat {
pub bytes_per_keccak_f: [u8; NUM_BYTES_TO_ABSORB],
Expand Down Expand Up @@ -39,7 +39,7 @@ impl KeccakIngestionFormat {
/// We split each input into `KeccakIngestionFormat` chunks, one for each keccak_f needed to compute `keccak(input)`.
/// We then resize so there are exactly `capacity` total chunks.
///
/// Very similar to [zkevm_hashes::keccak::coprocessor::encode::encode_native_input] except we do not do the
/// Very similar to [crate::keccak::component::encode::encode_native_input] except we do not do the
/// encoding part (that will be done in circuit, not natively).
///
/// Returns `Err(true_capacity)` if `true_capacity > capacity`, where `true_capacity` is the number of keccak_f needed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/// Module of Keccak coprocessor circuit.
/// Module of Keccak component circuit(s).
pub mod circuit;
/// Module of encoding raw inputs to coprocessor circuit lookup keys.
/// Module of encoding raw inputs to component circuit lookup keys.
pub mod encode;
/// Module for Rust native processing of input bytes into resized fixed length format to match vanilla circuit LoadedKeccakF
pub mod ingestion;
/// Module of Keccak coprocessor circuit output.
/// Module of Keccak component circuit output.
pub mod output;
/// Module of Keccak coprocessor circuit constant parameters.
/// Module of Keccak component circuit constant parameters.
pub mod param;
#[cfg(test)]
mod tests;
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use halo2_base::{
};
use itertools::Itertools;

use crate::keccak::coprocessor::{
circuit::leaf::create_hasher,
use crate::keccak::component::{
circuit::shard::create_hasher,
encode::{encode_fix_len_bytes_vec, encode_native_input, encode_var_len_bytes_vec},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::keccak::coprocessor::output::{
use crate::keccak::component::output::{
dummy_circuit_output, input_to_circuit_outputs, multi_inputs_to_circuit_outputs,
KeccakCircuitOutput,
};
Expand Down
2 changes: 0 additions & 2 deletions hashes/zkevm/src/keccak/coprocessor/circuit/tests/mod.rs

This file was deleted.

4 changes: 2 additions & 2 deletions hashes/zkevm/src/keccak/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Module for coprocessor circuits.
pub mod coprocessor;
/// Module for component circuits.
pub mod component;
/// Module for Keccak circuits in vanilla halo2.
pub mod vanilla;

0 comments on commit 5500e46

Please sign in to comment.