Skip to content

Commit

Permalink
[keccak] use tag to stack lookup table cells (privacy-scaling-explora…
Browse files Browse the repository at this point in the history
…tions#569)

* try merge range and special chunks table

* stackable table done

* replace lookups with stackable

* lookup special chunk

* rm pub

* add dead code reason

* clippy fix

* fix doc
  • Loading branch information
ChihChengLiang authored Jun 23, 2022
1 parent a45c26a commit d567bb2
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 198 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions keccak256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ plotters = { version = "0.3.0", optional = true }
eth-types = { path = "../eth-types" }
lazy_static = "1.4"
gadgets = { path = "../gadgets" }
strum = "0.24"
strum_macros = "0.24"

[dev-dependencies]
pretty_assertions = "1.0"
Expand Down
27 changes: 22 additions & 5 deletions keccak256/src/permutation/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@ use crate::{
common::{NEXT_INPUTS_LANES, PERMUTATION, ROUND_CONSTANTS},
keccak_arith::*,
permutation::{
base_conversion::BaseConversionConfig, generic::GenericConfig, iota::IotaConstants,
mixing::MixingConfig, pi::pi_gate_permutation, rho::RhoConfig,
tables::FromBase9TableConfig, theta::ThetaConfig, xi::XiConfig,
base_conversion::BaseConversionConfig,
generic::GenericConfig,
iota::IotaConstants,
mixing::MixingConfig,
pi::pi_gate_permutation,
rho::RhoConfig,
tables::{FromBase9TableConfig, StackableTable},
theta::ThetaConfig,
xi::XiConfig,
},
};
use eth_types::Field;
use halo2_proofs::{
circuit::{AssignedCell, Layouter, Region},
plonk::{Advice, Column, ConstraintSystem, Error, Selector},
plonk::{Advice, Column, ConstraintSystem, Error, Selector, TableColumn},
poly::Rotation,
};
use itertools::Itertools;
use std::convert::TryInto;
#[derive(Clone, Debug)]
pub struct KeccakFConfig<F: Field> {
generic: GenericConfig<F>,
stackable: StackableTable<F>,
theta_config: ThetaConfig<F>,
rho_config: RhoConfig<F>,
xi_config: XiConfig<F>,
Expand All @@ -45,11 +52,19 @@ impl<F: Field> KeccakFConfig<F> {

let fixed = meta.fixed_column();
let generic = GenericConfig::configure(meta, state[0..3].try_into().unwrap(), fixed);
let table_cols: [TableColumn; 3] = (0..3)
.map(|_| meta.lookup_table_column())
.collect_vec()
.try_into()
.unwrap();
let stackable =
StackableTable::configure(meta, state[0..3].try_into().unwrap(), table_cols);

// theta
let theta_config = ThetaConfig::configure(meta.selector(), meta, state);
// rho
let rho_config = RhoConfig::configure(meta, state, fixed, &generic);
let rho_config =
RhoConfig::configure(meta, state, fixed, generic.clone(), stackable.clone());
// xi
let xi_config = XiConfig::configure(meta.selector(), meta, state);

Expand Down Expand Up @@ -90,6 +105,7 @@ impl<F: Field> KeccakFConfig<F> {

KeccakFConfig {
generic,
stackable,
theta_config,
rho_config,
xi_config,
Expand All @@ -103,6 +119,7 @@ impl<F: Field> KeccakFConfig<F> {
}

pub fn load(&self, layouter: &mut impl Layouter<F>) -> Result<(), Error> {
self.stackable.load(layouter)?;
self.rho_config.load(layouter)?;
self.from_b9_table.load(layouter)
}
Expand Down
60 changes: 26 additions & 34 deletions keccak256/src/permutation/rho.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::permutation::{
generic::GenericConfig,
rho_checks::{LaneRotateConversionConfig, OverflowCheckConfig},
rho_helpers::{STEP2_RANGE, STEP3_RANGE},
tables::{Base13toBase9TableConfig, RangeCheckConfig, SpecialChunkTableConfig},
rho_checks::LaneRotateConversionConfig,
tables::{Base13toBase9TableConfig, StackableTable},
};

use eth_types::Field;
Expand All @@ -15,49 +14,35 @@ use std::convert::TryInto;
#[derive(Debug, Clone)]
pub struct RhoConfig<F> {
lane_config: LaneRotateConversionConfig<F>,
overflow_check_config: OverflowCheckConfig<F>,
base13_to_9_table: Base13toBase9TableConfig<F>,
special_chunk_table: SpecialChunkTableConfig<F>,
step2_range_table: RangeCheckConfig<F, STEP2_RANGE>,
step3_range_table: RangeCheckConfig<F, STEP3_RANGE>,
stackable: StackableTable<F>,
generic: GenericConfig<F>,
}

impl<F: Field> RhoConfig<F> {
pub fn configure(
meta: &mut ConstraintSystem<F>,
state: [Column<Advice>; 25],
fixed: Column<Fixed>,
generic: &GenericConfig<F>,
generic: GenericConfig<F>,
stackable: StackableTable<F>,
) -> Self {
state.iter().for_each(|col| meta.enable_equality(*col));
let base13_to_9_table = Base13toBase9TableConfig::configure(meta);
let special_chunk_table = SpecialChunkTableConfig::configure(meta);
let step2_range_table = RangeCheckConfig::<F, STEP2_RANGE>::configure(meta);
let step3_range_table = RangeCheckConfig::<F, STEP3_RANGE>::configure(meta);

let lane_config = LaneRotateConversionConfig::configure(
meta,
&base13_to_9_table,
&special_chunk_table,
state[0..3].try_into().unwrap(),
fixed,
generic.clone(),
);

let overflow_check_config = OverflowCheckConfig::configure(
meta,
&step2_range_table,
&step3_range_table,
state[3],
generic.clone(),
stackable.clone(),
);
Self {
lane_config,
overflow_check_config,
base13_to_9_table,
special_chunk_table,
step2_range_table,
step3_range_table,
stackable,
generic,
}
}
pub fn assign_rotation_checks(
Expand Down Expand Up @@ -93,19 +78,15 @@ impl<F: Field> RhoConfig<F> {
.iter()
.flat_map(|(_, _, step3_od)| step3_od.clone())
.collect::<Vec<_>>();
self.overflow_check_config.assign_region(
&mut layouter.namespace(|| "Final overflow check"),
step2_od_join,
step3_od_join,
)?;
let step2_sum = self.generic.running_sum(layouter, step2_od_join, None)?;
let step3_sum = self.generic.running_sum(layouter, step3_od_join, None)?;
self.stackable.lookup_range_12(layouter, &[step2_sum])?;
self.stackable.lookup_range_169(layouter, &[step3_sum])?;
Ok(next_state)
}

pub fn load(&self, layouter: &mut impl Layouter<F>) -> Result<(), Error> {
self.base13_to_9_table.load(layouter)?;
self.special_chunk_table.load(layouter)?;
self.step2_range_table.load(layouter)?;
self.step3_range_table.load(layouter)?;
Ok(())
}
}
Expand All @@ -121,7 +102,7 @@ mod tests {
circuit::{Layouter, SimpleFloorPlanner},
dev::MockProver,
pairing::bn256::Fr as Fp,
plonk::{Advice, Circuit, Column, ConstraintSystem, Error, Selector},
plonk::{Advice, Circuit, Column, ConstraintSystem, Error, Selector, TableColumn},
poly::Rotation,
};
use itertools::Itertools;
Expand All @@ -139,6 +120,7 @@ mod tests {
struct MyConfig<F> {
q_enable: Selector,
rho_config: RhoConfig<F>,
stackable: StackableTable<F>,
state: [Column<Advice>; 25],
}
impl<F: Field> Circuit<F> for MyCircuit<F> {
Expand All @@ -157,10 +139,18 @@ mod tests {
.unwrap();

let fixed = meta.fixed_column();
let table_cols: [TableColumn; 3] = (0..3)
.map(|_| meta.lookup_table_column())
.collect_vec()
.try_into()
.unwrap();
let stackable =
StackableTable::configure(meta, state[0..3].try_into().unwrap(), table_cols);
let generic =
GenericConfig::configure(meta, state[0..3].try_into().unwrap(), fixed);

let rho_config = RhoConfig::configure(meta, state, fixed, &generic);
let rho_config =
RhoConfig::configure(meta, state, fixed, generic, stackable.clone());

let q_enable = meta.selector();
meta.create_gate("Check states", |meta| {
Expand All @@ -178,6 +168,7 @@ mod tests {
MyConfig {
q_enable,
rho_config,
stackable,
state,
}
}
Expand All @@ -188,6 +179,7 @@ mod tests {
mut layouter: impl Layouter<F>,
) -> Result<(), Error> {
config.rho_config.load(&mut layouter)?;
config.stackable.load(&mut layouter)?;
let state = layouter.assign_region(
|| "assign input state",
|mut region| {
Expand Down
Loading

0 comments on commit d567bb2

Please sign in to comment.