Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.
41 changes: 5 additions & 36 deletions circuit-benchmarks/src/keccak_permutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ use halo2_proofs::{
circuit::{AssignedCell, Layouter, SimpleFloorPlanner},
plonk::{Circuit, ConstraintSystem, Error},
};
use keccak256::{
common::NEXT_INPUTS_LANES, keccak_arith::KeccakFArith, permutation::circuit::KeccakFConfig,
};
use keccak256::{common::NEXT_INPUTS_LANES, permutation::circuit::KeccakFConfig};

#[derive(Default, Clone)]
struct KeccakRoundTestCircuit<F> {
in_state: [F; 25],
out_state: [F; 25],
next_mixing: Option<[F; NEXT_INPUTS_LANES]>,
is_mixing: bool,
}

impl<F: Field> Circuit<F> for KeccakRoundTestCircuit<F> {
Expand Down Expand Up @@ -44,12 +40,12 @@ impl<F: Field> Circuit<F> for KeccakRoundTestCircuit<F> {
// Witness `state`
let in_state: [AssignedCell<F, F>; 25] = {
let mut state: Vec<AssignedCell<F, F>> = Vec::with_capacity(25);
for (idx, val) in self.in_state.iter().enumerate() {
for &val in self.in_state.iter() {
let cell = region.assign_advice(
|| "witness input state",
config.state[idx],
config.advice,
offset,
|| Ok(*val),
|| Ok(val),
)?;
state.push(cell)
}
Expand All @@ -59,13 +55,7 @@ impl<F: Field> Circuit<F> for KeccakRoundTestCircuit<F> {
},
)?;

config.assign_all(
&mut layouter,
in_state,
self.out_state,
self.is_mixing,
self.next_mixing,
)?;
config.assign_all(&mut layouter, in_state, self.next_mixing)?;
Ok(())
}
}
Expand Down Expand Up @@ -97,14 +87,6 @@ mod tests {
[0, 0, 0, 0, 0],
];

let next_input: State = [
[2, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
];

let mut in_state_biguint = StateBigInt::default();

// Generate in_state as `[Fr;25]`
Expand All @@ -114,23 +96,10 @@ mod tests {
in_state_biguint[(x, y)] = convert_b2_to_b13(in_state[x][y]);
}

// Compute out_state_mix
let mut out_state_mix = in_state_biguint.clone();
KeccakFArith::permute_and_absorb(&mut out_state_mix, Some(&next_input));

// Compute out_state_non_mix
let mut out_state_non_mix = in_state_biguint.clone();
KeccakFArith::permute_and_absorb(&mut out_state_non_mix, None);

// Generate out_state as `[Fr;25]`
let out_state_non_mix: [Fr; 25] = state_bigint_to_field(out_state_non_mix);

// Build the circuit
let circuit = KeccakRoundTestCircuit::<Fr> {
in_state: in_state_fp,
out_state: out_state_non_mix,
next_mixing: None,
is_mixing: false,
};

let degree: u32 = var("DEGREE")
Expand Down
4 changes: 4 additions & 0 deletions keccak256/src/arith_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ pub fn convert_b9_lane_to_b2(x: Lane9) -> u64 {
.unwrap_or(0)
}

pub fn convert_b9_lane_to_b2_biguint(x: Lane9) -> BigUint {
convert_lane(x, B9, 2, convert_b9_coef)
}

pub fn convert_b9_lane_to_b2_normal(x: Lane9) -> u64 {
convert_lane(x, B9, 2, |y| y)
.iter_u64_digits()
Expand Down
9 changes: 1 addition & 8 deletions keccak256/src/permutation.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
pub(crate) mod absorb;
pub(crate) mod base_conversion;
pub mod circuit;
pub(crate) mod components;
pub(crate) mod generic;
pub(crate) mod iota;
pub(crate) mod mixing;
pub(crate) mod pi;
pub(crate) mod rho;
pub(crate) mod rho_helpers;
pub(crate) mod tables;
pub(crate) mod theta;
pub(crate) mod xi;
Loading