Skip to content

Commit

Permalink
Don't allow dead code in aggregator crate (#1253)
Browse files Browse the repository at this point in the history
Co-authored-by: Mason Liang <mason@scroll.io>
  • Loading branch information
z2trillion and Mason Liang authored May 21, 2024
1 parent 8aa34df commit e6cfd7b
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 271 deletions.
220 changes: 1 addition & 219 deletions aggregator/src/aggregation/rlc/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use halo2_proofs::{
use zkevm_circuits::util::Challenges;

// TODO: remove MAX_AGG_SNARKS and make this generic over N_SNARKS
use crate::{constants::LOG_DEGREE, util::assert_equal, DIGEST_LEN, MAX_AGG_SNARKS};
use crate::{DIGEST_LEN, MAX_AGG_SNARKS};

use super::RlcConfig;

Expand Down Expand Up @@ -121,45 +121,6 @@ impl RlcConfig {
}
}

#[inline]
pub(crate) fn two_cell(&self, region_index: RegionIndex) -> Cell {
Cell {
region_index,
row_offset: 2,
column: self.fixed.into(),
}
}

#[inline]
#[allow(dead_code)]
pub(crate) fn five_cell(&self, region_index: RegionIndex) -> Cell {
Cell {
region_index,
row_offset: 5,
column: self.fixed.into(),
}
}

#[inline]
#[allow(dead_code)]
pub(crate) fn nine_cell(&self, region_index: RegionIndex) -> Cell {
Cell {
region_index,
row_offset: 9,
column: self.fixed.into(),
}
}

#[inline]
#[allow(dead_code)]
pub(crate) fn thirteen_cell(&self, region_index: RegionIndex) -> Cell {
Cell {
region_index,
row_offset: 13,
column: self.fixed.into(),
}
}

#[inline]
pub(crate) fn fixed_up_to_max_agg_snarks_cell(
&self,
Expand All @@ -174,45 +135,6 @@ impl RlcConfig {
}
}

#[inline]
#[allow(dead_code)]
pub(crate) fn thirty_two_cell(&self, region_index: RegionIndex) -> Cell {
Cell {
region_index,
row_offset: FIXED_OFFSET_32,
column: self.fixed.into(),
}
}

#[inline]
#[allow(dead_code)]
pub(crate) fn one_hundred_and_sixty_eight_cell(&self, region_index: RegionIndex) -> Cell {
Cell {
region_index,
row_offset: FIXED_OFFSET_168,
column: self.fixed.into(),
}
}

#[inline]
#[allow(dead_code)]
pub(crate) fn two_hundred_and_thirty_two_cell(&self, region_index: RegionIndex) -> Cell {
Cell {
region_index,
row_offset: FIXED_OFFSET_232,
column: self.fixed.into(),
}
}

#[inline]
pub(crate) fn two_to_thirty_two_cell(&self, region_index: RegionIndex) -> Cell {
Cell {
region_index,
row_offset: FIXED_OFFSET_2_POW_32,
column: self.fixed.into(),
}
}

#[inline]
pub(crate) fn pow_of_two_hundred_and_fifty_six_cell(
&self,
Expand Down Expand Up @@ -315,7 +237,6 @@ impl RlcConfig {
}

/// Enforce res = a + b
#[allow(dead_code)]
pub(crate) fn add(
&self,
region: &mut Region<Fr>,
Expand Down Expand Up @@ -478,7 +399,6 @@ impl RlcConfig {
}

// Returns inputs[0] + challenge * inputs[1] + ... + challenge^k * inputs[k]
#[allow(dead_code)]
pub(crate) fn rlc(
&self,
region: &mut Region<Fr>,
Expand Down Expand Up @@ -512,119 +432,6 @@ impl RlcConfig {
Ok(acc)
}

// padded the columns
#[allow(dead_code)]
pub(crate) fn pad(&self, region: &mut Region<Fr>, offset: &usize) -> Result<(), Error> {
for index in *offset..(1 << LOG_DEGREE) - 1 {
region.assign_advice(
|| "pad",
self.phase_2_column,
index,
|| Value::known(Fr::zero()),
)?;
}
Ok(())
}

// decompose a field element into log_size bits of boolean cells
// require the input to be less than 2^log_size
// require log_size < 254
#[allow(dead_code)]
pub(crate) fn decomposition(
&self,
region: &mut Region<Fr>,
input: &AssignedCell<Fr, Fr>,
log_size: usize,
offset: &mut usize,
) -> Result<Vec<AssignedCell<Fr, Fr>>, Error> {
let mut input_element = Fr::default();
input.value().map(|&x| input_element = x);

let bits = input_element
.to_bytes()
.iter()
.flat_map(byte_to_bits_le)
.take(log_size)
.collect::<Vec<_>>();
// sanity check
{
let mut reconstructed = Fr::zero();
bits.iter().rev().for_each(|bit| {
reconstructed *= Fr::from(2);
reconstructed += Fr::from(*bit as u64);
});
assert_eq!(reconstructed, input_element);
}

let bit_cells = bits
.iter()
.map(|&bit| {
let cell = self.load_private(region, &Fr::from(bit as u64), offset)?;
self.enforce_binary(region, &cell, offset)?;
Ok(cell)
})
.collect::<Result<Vec<_>, Error>>()?;

let mut acc = {
let zero = self.load_private(region, &Fr::from(0), offset)?;
let zero_cell = self.zero_cell(zero.cell().region_index);
region.constrain_equal(zero_cell, zero.cell())?;
zero
};

let two = {
let two = self.load_private(region, &Fr::from(2), offset)?;
let two_cell = self.two_cell(two.cell().region_index);
region.constrain_equal(two_cell, two.cell())?;
two
};

for bit in bit_cells.iter().rev() {
acc = self.mul_add(region, &acc, &two, bit, offset)?;
}

// sanity check
assert_equal(
&acc,
input,
format!(
"acc does not match input: {:?} {:?}",
&acc.value(),
&input.value(),
)
.as_str(),
)?;

region.constrain_equal(acc.cell(), input.cell())?;

Ok(bit_cells)
}

// return a boolean if a is smaller than b
// requires that both a and b are less than 32 bits
#[allow(dead_code)]
pub(crate) fn is_smaller_than(
&self,
region: &mut Region<Fr>,
a: &AssignedCell<Fr, Fr>,
b: &AssignedCell<Fr, Fr>,
offset: &mut usize,
) -> Result<AssignedCell<Fr, Fr>, Error> {
// when a and b are both small (as in our use case)
// if a >= b, c = 2^32 + (a-b) will be >= 2^32
// we bit decompose c and check the 33-th bit
let two_to_thirty_two = self.load_private(region, &Fr::from(1 << 32), offset)?;
let two_to_thirty_two_cell =
self.two_to_thirty_two_cell(two_to_thirty_two.cell().region_index);
region.constrain_equal(two_to_thirty_two_cell, two_to_thirty_two.cell())?;

let ca = self.add(region, &two_to_thirty_two, a, offset)?;
let c = self.sub(region, &ca, b, offset)?;
let bits = self.decomposition(region, &c, 33, offset)?;
let res = self.not(region, &bits[32], offset)?;
Ok(res)
}

pub(crate) fn inner_product(
&self,
region: &mut Region<Fr>,
Expand All @@ -644,7 +451,6 @@ impl RlcConfig {
}

// return a boolean if a ?= 0
#[allow(dead_code)]
pub(crate) fn is_zero(
&self,
region: &mut Region<Fr>,
Expand Down Expand Up @@ -702,19 +508,6 @@ impl RlcConfig {
Ok(res_cell)
}

// return a boolean if a ?= b
#[allow(dead_code)]
pub(crate) fn is_equal(
&self,
region: &mut Region<Fr>,
a: &AssignedCell<Fr, Fr>,
b: &AssignedCell<Fr, Fr>,
offset: &mut usize,
) -> Result<AssignedCell<Fr, Fr>, Error> {
let diff = self.sub(region, a, b, offset)?;
self.is_zero(region, &diff, offset)
}

// lookup the input and output rlcs from the lookup table
pub(crate) fn lookup_keccak_rlcs(
&self,
Expand Down Expand Up @@ -745,14 +538,3 @@ impl RlcConfig {
Ok(())
}
}

#[inline]
fn byte_to_bits_le(byte: &u8) -> Vec<u8> {
let mut res = vec![];
let mut t = *byte;
for _ in 0..8 {
res.push(t & 1);
t >>= 1;
}
res
}
1 change: 0 additions & 1 deletion aggregator/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub struct BatchHash<const N_SNARKS: usize> {

impl<const N_SNARKS: usize> BatchHash<N_SNARKS> {
/// Build Batch hash from an ordered list of #N_SNARKS of chunks.
#[allow(dead_code)]
pub fn construct(chunks_with_padding: &[ChunkHash]) -> Self {
assert_eq!(
chunks_with_padding.len(),
Expand Down
1 change: 0 additions & 1 deletion aggregator/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// A chain_id is u64 and uses 8 bytes
#[allow(dead_code)]
pub(crate) const CHAIN_ID_LEN: usize = 8;

// ================================
Expand Down
5 changes: 0 additions & 5 deletions aggregator/src/tests/mock_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,16 @@ pub struct MockConfig {
/// This mock chunk circuit simulates a zkEVM circuit.
/// It's public inputs consists of 32 elements:
/// - public input hash
#[allow(dead_code)]
pub(crate) struct MockChunkCircuit {
// This circuit has an accumulator if it has already gone through compression
pub(crate) has_accumulator: bool,
pub(crate) is_padding: bool,
pub(crate) chunk: ChunkHash,
}

impl MockChunkCircuit {
#[allow(dead_code)]
pub(crate) fn new(has_accumulator: bool, chunk: ChunkHash) -> Self {
MockChunkCircuit {
has_accumulator,
is_padding: chunk.is_padding,
chunk,
}
}
Expand All @@ -61,7 +57,6 @@ impl MockChunkCircuit {
let chunk = ChunkHash::mock_random_chunk_hash_for_testing(r);
Self {
has_accumulator,
is_padding,
chunk: if is_padding {
ChunkHash::mock_padded_chunk_hash_for_testing(&chunk)
} else {
Expand Down
Loading

0 comments on commit e6cfd7b

Please sign in to comment.