Skip to content

Commit

Permalink
move constrain_crt_equals_bytes to RlcConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
z2trillion committed Nov 5, 2024
1 parent 8afe2f5 commit ad0828d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 51 deletions.
6 changes: 2 additions & 4 deletions aggregator/src/aggregation/batch_data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
aggregation::util::constrain_crt_equals_bytes, blob_consistency::BLOB_WIDTH,
constants::N_BYTES_U256, BatchHash, ChunkInfo, RlcConfig,
blob_consistency::BLOB_WIDTH, constants::N_BYTES_U256, BatchHash, ChunkInfo, RlcConfig,
};
use eth_types::{H256, U256};
use ethers_core::utils::keccak256;
Expand Down Expand Up @@ -988,9 +987,8 @@ impl<const N_SNARKS: usize> BatchDataConfig<N_SNARKS> {
////////////////////////////////////////////////////////////////////////////////
//////////////////////////// CHALLENGE DIGEST CHECK ////////////////////////////
////////////////////////////////////////////////////////////////////////////////
constrain_crt_equals_bytes(
rlc_config.constrain_crt_equals_bytes(
region,
rlc_config,
assigned_challenge_digest,
&challenge_digest,
&mut rlc_config_offset,
Expand Down
42 changes: 41 additions & 1 deletion aggregator/src/aggregation/rlc/gates.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use ethers_core::utils::keccak256;
use halo2_ecc::bigint::CRTInteger;
use halo2_proofs::{
circuit::{AssignedCell, Cell, Region, RegionIndex, Value},
halo2curves::bn256::Fr,
halo2curves::{bn256::Fr, group::ff::PrimeField},
plonk::Error,
};
use itertools::Itertools;
use zkevm_circuits::util::Challenges;

// TODO: remove MAX_AGG_SNARKS and make this generic over N_SNARKS
Expand Down Expand Up @@ -547,4 +549,42 @@ impl RlcConfig {

Ok(())
}

pub fn constrain_crt_equals_bytes(
&self,
region: &mut Region<Fr>,
crt: &CRTInteger<Fr>,
bytes: &[AssignedCell<Fr, Fr>],
offset: &mut usize,
) -> Result<(), Error> {
let mut powers_of_256 = vec![];
for i in 0..11 {
let assigned_cell =
self.load_private(region, &Fr::from_u128(256u128.pow(i)), offset)?;
let region_index = assigned_cell.cell().region_index;
let fixed_cell = if i == 0 {
self.one_cell(region_index)
} else {
self.pow_of_two_hundred_and_fifty_six_cell(
region_index,
usize::try_from(i).unwrap(),
)
};
region.constrain_equal(fixed_cell, assigned_cell.cell())?;
powers_of_256.push(assigned_cell);
}

let limb_from_bytes_lo =
self.inner_product(region, &bytes[0..11], &powers_of_256, offset)?;
let limb_from_bytes_mid =
self.inner_product(region, &bytes[11..22], &powers_of_256, offset)?;
let limb_from_bytes_hi =
self.inner_product(region, &bytes[22..32], &powers_of_256[0..10], offset)?;

[limb_from_bytes_lo, limb_from_bytes_mid, limb_from_bytes_hi]
.iter()
.zip_eq(crt.limbs())
.map(|(a, b)| region.constrain_equal(a.cell(), b.cell()))
.collect()
}
}
47 changes: 1 addition & 46 deletions aggregator/src/aggregation/util.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use crate::RlcConfig;
use gadgets::util::Expr;
use halo2_ecc::bigint::CRTInteger;
use halo2_proofs::{
circuit::{AssignedCell, Region},
halo2curves::{bn256::Fr, group::ff::PrimeField},
plonk::{Advice, Column, ConstraintSystem, Error, Expression, VirtualCells},
plonk::{Advice, Column, ConstraintSystem, Expression, VirtualCells},
poly::Rotation,
};
use itertools::Itertools;
use zkevm_circuits::util::Field;

#[derive(Clone, Copy, Debug)]
Expand All @@ -34,43 +29,3 @@ impl BooleanAdvice {
meta.query_advice(self.column, at)
}
}

pub fn constrain_crt_equals_bytes(
region: &mut Region<Fr>,
rlc_config: &RlcConfig,
crt: &CRTInteger<Fr>,
bytes: &[AssignedCell<Fr, Fr>],
rlc_config_offset: &mut usize,
) -> Result<(), Error> {
let mut powers_of_256 = vec![];
for i in 0..11 {
let assigned_cell =
rlc_config.load_private(region, &Fr::from_u128(256u128.pow(i)), rlc_config_offset)?;
let region_index = assigned_cell.cell().region_index;
let fixed_cell = if i == 0 {
rlc_config.one_cell(region_index)
} else {
rlc_config
.pow_of_two_hundred_and_fifty_six_cell(region_index, usize::try_from(i).unwrap())
};
region.constrain_equal(fixed_cell, assigned_cell.cell())?;
powers_of_256.push(assigned_cell);
}

let limb_from_bytes_lo =
rlc_config.inner_product(region, &bytes[0..11], &powers_of_256, rlc_config_offset)?;
let limb_from_bytes_mid =
rlc_config.inner_product(region, &bytes[11..22], &powers_of_256, rlc_config_offset)?;
let limb_from_bytes_hi = rlc_config.inner_product(
region,
&bytes[22..32],
&powers_of_256[0..10],
rlc_config_offset,
)?;

[limb_from_bytes_lo, limb_from_bytes_mid, limb_from_bytes_hi]
.iter()
.zip_eq(crt.limbs())
.map(|(a, b)| region.constrain_equal(a.cell(), b.cell()))
.collect()
}

0 comments on commit ad0828d

Please sign in to comment.