Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ChihChengLiang authored Dec 14, 2021
1 parent 1ce3f7f commit d1fa381
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
44 changes: 19 additions & 25 deletions keccak256/src/gates/rho.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::gates::{
gate_helpers::{BlockCount2, Lane},
gate_helpers::BlockCount2,
rho_checks::{
BlockCountFinalConfig, LaneRotateConversionConfig, RhoAdvices,
},
};

use halo2::{
circuit::{Layouter, Region},
circuit::{Cell, Layouter, Region},
plonk::{Advice, Column, ConstraintSystem, Error, Fixed},
};
use itertools::Itertools;
Expand Down Expand Up @@ -59,26 +59,26 @@ impl<F: FieldExt> RhoConfig<F> {
pub fn assign_rotation_checks(
&self,
layouter: &mut impl Layouter<F>,
previous_state: [Lane<F>; 25],
) -> Result<[Lane<F>; 25], Error> {
type R<F> = (Lane<F>, BlockCount2<F>);
let lane_and_bcs: Result<Vec<R<F>>, Error> = previous_state
state: [(Cell, F); 25],
) -> Result<[(Cell, F); 25], Error> {
type R<F> = ((Cell, F), BlockCount2<F>);
let lane_and_bcs: Result<Vec<R<F>>, Error> = state
.iter()
.enumerate()
.map(|(idx, lane)| -> Result<R<F>, Error> {
.map(|(idx, &lane)| -> Result<R<F>, Error> {
let (lane_next_row, bc) =
&self.state_rotate_convert_configs[idx].assign_region(
self.state_rotate_convert_configs[idx].assign_region(
&mut layouter.namespace(|| format!("arc lane {}", idx)),
lane,
)?;
Ok((lane_next_row.clone(), *bc))
Ok((lane_next_row, bc))
})
.into_iter()
.collect();
let lane_and_bcs = lane_and_bcs?;
let lane_and_bcs: [R<F>; 25] = lane_and_bcs.try_into().unwrap();

let block_counts = lane_and_bcs.clone().map(|(_, bc)| bc);
let block_counts = lane_and_bcs.map(|(_, bc)| bc);
let next_state = lane_and_bcs.map(|(lane_next_row, _)| lane_next_row);

self.final_block_count_config.assign_region(
Expand All @@ -92,16 +92,16 @@ impl<F: FieldExt> RhoConfig<F> {
&self,
region: &mut Region<'_, F>,
offset: usize,
next_state: [Lane<F>; 25],
next_state: [(Cell, F); 25],
) -> Result<(), Error> {
for (idx, next_lane) in next_state.iter().enumerate() {
let cell = region.assign_advice(
|| "lane next row",
self.state[idx],
offset + 1,
|| Ok(next_lane.value),
|| Ok(next_lane.1),
)?;
region.constrain_equal(cell, next_lane.cell)?;
region.constrain_equal(cell, next_lane.0)?;
}
Ok(())
}
Expand Down Expand Up @@ -177,23 +177,20 @@ mod tests {
|| "assign input state",
|mut region| {
let offset = 0;
let state: [Lane<F>; 25] = self
let state: [(Cell, F); 25] = self
.in_state
.iter()
.enumerate()
.map(|(idx, value)| {
.map(|(idx, &value)| {
let cell = region
.assign_advice(
|| format!("lane {}", idx),
config.state[idx],
offset,
|| Ok(*value),
|| Ok(value),
)
.unwrap();
Lane {
cell,
value: *value,
}
(cell, value)
})
.collect::<Vec<_>>()
.try_into()
Expand All @@ -203,18 +200,15 @@ mod tests {
)?;
let next_state =
config.assign_rotation_checks(&mut layouter, state)?;
assert_eq!(
next_state.clone().map(|lane| lane.value),
self.out_state
);
assert_eq!(next_state.map(|lane| lane.1), self.out_state);
layouter.assign_region(
|| "assign output state",
|mut region| {
let offset = 1;
config.assign_region(
&mut region,
offset,
next_state.clone(),
next_state,
)?;
Ok(())
},
Expand Down
16 changes: 8 additions & 8 deletions keccak256/src/gates/rho_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ use crate::gates::{
tables::{Base13toBase9TableConfig, SpecialChunkTableConfig},
};
use halo2::{
circuit::{Layouter, Region},
circuit::{Cell, Layouter, Region},
plonk::{
Advice, Column, ConstraintSystem, Error, Expression, Fixed, Selector,
},
Expand Down Expand Up @@ -344,8 +344,8 @@ impl<F: FieldExt> LaneRotateConversionConfig<F> {
pub fn assign_region(
&self,
layouter: &mut impl Layouter<F>,
lane_base_13: &Lane<F>,
) -> Result<(Lane<F>, BlockCount2<F>), Error> {
lane_base_13: (Cell, F),
) -> Result<((Cell, F), BlockCount2<F>), Error> {
let (lane, block_counts) = layouter.assign_region(
|| format!("LRCC {:?}", self.lane_xy),
|mut region| {
Expand All @@ -354,12 +354,12 @@ impl<F: FieldExt> LaneRotateConversionConfig<F> {
|| "base_13_col",
self.adv.input.acc,
offset,
|| Ok(lane_base_13.value),
|| Ok(lane_base_13.1),
)?;
region.constrain_equal(lane_base_13.cell, cell)?;
region.constrain_equal(lane_base_13.0, cell)?;

let mut rv = RotatingVariables::from(
f_to_biguint(lane_base_13.value).ok_or(Error::Synthesis)?,
f_to_biguint(lane_base_13.1).ok_or(Error::Synthesis)?,
self.rotation,
)?;
let all_block_counts: Result<Vec<BlockCount2<F>>, Error> = self
Expand Down Expand Up @@ -600,7 +600,7 @@ impl<F: FieldExt> SpecialChunkConfig<F> {
region: &mut Region<'_, F>,
offset: usize,
rv: &RotatingVariables,
) -> Result<Lane<F>, Error> {
) -> Result<(Cell, F), Error> {
self.q_enable.enable(region, offset)?;
rv.high_value.ok_or(Error::Synthesis).unwrap();
region.assign_advice(
Expand Down Expand Up @@ -637,7 +637,7 @@ impl<F: FieldExt> SpecialChunkConfig<F> {
|| Ok(value),
)?;

Ok(Lane { cell, value })
Ok((cell, value))
}
}

Expand Down

0 comments on commit d1fa381

Please sign in to comment.