Skip to content

Commit ad0828d

Browse files
committed
move constrain_crt_equals_bytes to RlcConfig
1 parent 8afe2f5 commit ad0828d

File tree

3 files changed

+44
-51
lines changed

3 files changed

+44
-51
lines changed

aggregator/src/aggregation/batch_data.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::{
2-
aggregation::util::constrain_crt_equals_bytes, blob_consistency::BLOB_WIDTH,
3-
constants::N_BYTES_U256, BatchHash, ChunkInfo, RlcConfig,
2+
blob_consistency::BLOB_WIDTH, constants::N_BYTES_U256, BatchHash, ChunkInfo, RlcConfig,
43
};
54
use eth_types::{H256, U256};
65
use ethers_core::utils::keccak256;
@@ -988,9 +987,8 @@ impl<const N_SNARKS: usize> BatchDataConfig<N_SNARKS> {
988987
////////////////////////////////////////////////////////////////////////////////
989988
//////////////////////////// CHALLENGE DIGEST CHECK ////////////////////////////
990989
////////////////////////////////////////////////////////////////////////////////
991-
constrain_crt_equals_bytes(
990+
rlc_config.constrain_crt_equals_bytes(
992991
region,
993-
rlc_config,
994992
assigned_challenge_digest,
995993
&challenge_digest,
996994
&mut rlc_config_offset,

aggregator/src/aggregation/rlc/gates.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use ethers_core::utils::keccak256;
2+
use halo2_ecc::bigint::CRTInteger;
23
use halo2_proofs::{
34
circuit::{AssignedCell, Cell, Region, RegionIndex, Value},
4-
halo2curves::bn256::Fr,
5+
halo2curves::{bn256::Fr, group::ff::PrimeField},
56
plonk::Error,
67
};
8+
use itertools::Itertools;
79
use zkevm_circuits::util::Challenges;
810

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

548550
Ok(())
549551
}
552+
553+
pub fn constrain_crt_equals_bytes(
554+
&self,
555+
region: &mut Region<Fr>,
556+
crt: &CRTInteger<Fr>,
557+
bytes: &[AssignedCell<Fr, Fr>],
558+
offset: &mut usize,
559+
) -> Result<(), Error> {
560+
let mut powers_of_256 = vec![];
561+
for i in 0..11 {
562+
let assigned_cell =
563+
self.load_private(region, &Fr::from_u128(256u128.pow(i)), offset)?;
564+
let region_index = assigned_cell.cell().region_index;
565+
let fixed_cell = if i == 0 {
566+
self.one_cell(region_index)
567+
} else {
568+
self.pow_of_two_hundred_and_fifty_six_cell(
569+
region_index,
570+
usize::try_from(i).unwrap(),
571+
)
572+
};
573+
region.constrain_equal(fixed_cell, assigned_cell.cell())?;
574+
powers_of_256.push(assigned_cell);
575+
}
576+
577+
let limb_from_bytes_lo =
578+
self.inner_product(region, &bytes[0..11], &powers_of_256, offset)?;
579+
let limb_from_bytes_mid =
580+
self.inner_product(region, &bytes[11..22], &powers_of_256, offset)?;
581+
let limb_from_bytes_hi =
582+
self.inner_product(region, &bytes[22..32], &powers_of_256[0..10], offset)?;
583+
584+
[limb_from_bytes_lo, limb_from_bytes_mid, limb_from_bytes_hi]
585+
.iter()
586+
.zip_eq(crt.limbs())
587+
.map(|(a, b)| region.constrain_equal(a.cell(), b.cell()))
588+
.collect()
589+
}
550590
}

aggregator/src/aggregation/util.rs

+1-46
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
use crate::RlcConfig;
21
use gadgets::util::Expr;
3-
use halo2_ecc::bigint::CRTInteger;
42
use halo2_proofs::{
5-
circuit::{AssignedCell, Region},
6-
halo2curves::{bn256::Fr, group::ff::PrimeField},
7-
plonk::{Advice, Column, ConstraintSystem, Error, Expression, VirtualCells},
3+
plonk::{Advice, Column, ConstraintSystem, Expression, VirtualCells},
84
poly::Rotation,
95
};
10-
use itertools::Itertools;
116
use zkevm_circuits::util::Field;
127

138
#[derive(Clone, Copy, Debug)]
@@ -34,43 +29,3 @@ impl BooleanAdvice {
3429
meta.query_advice(self.column, at)
3530
}
3631
}
37-
38-
pub fn constrain_crt_equals_bytes(
39-
region: &mut Region<Fr>,
40-
rlc_config: &RlcConfig,
41-
crt: &CRTInteger<Fr>,
42-
bytes: &[AssignedCell<Fr, Fr>],
43-
rlc_config_offset: &mut usize,
44-
) -> Result<(), Error> {
45-
let mut powers_of_256 = vec![];
46-
for i in 0..11 {
47-
let assigned_cell =
48-
rlc_config.load_private(region, &Fr::from_u128(256u128.pow(i)), rlc_config_offset)?;
49-
let region_index = assigned_cell.cell().region_index;
50-
let fixed_cell = if i == 0 {
51-
rlc_config.one_cell(region_index)
52-
} else {
53-
rlc_config
54-
.pow_of_two_hundred_and_fifty_six_cell(region_index, usize::try_from(i).unwrap())
55-
};
56-
region.constrain_equal(fixed_cell, assigned_cell.cell())?;
57-
powers_of_256.push(assigned_cell);
58-
}
59-
60-
let limb_from_bytes_lo =
61-
rlc_config.inner_product(region, &bytes[0..11], &powers_of_256, rlc_config_offset)?;
62-
let limb_from_bytes_mid =
63-
rlc_config.inner_product(region, &bytes[11..22], &powers_of_256, rlc_config_offset)?;
64-
let limb_from_bytes_hi = rlc_config.inner_product(
65-
region,
66-
&bytes[22..32],
67-
&powers_of_256[0..10],
68-
rlc_config_offset,
69-
)?;
70-
71-
[limb_from_bytes_lo, limb_from_bytes_mid, limb_from_bytes_hi]
72-
.iter()
73-
.zip_eq(crt.limbs())
74-
.map(|(a, b)| region.constrain_equal(a.cell(), b.cell()))
75-
.collect()
76-
}

0 commit comments

Comments
 (0)