Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 47beb9e

Browse files
committedJul 27, 2023
address comment on fixed cells
1 parent 236b3cd commit 47beb9e

File tree

7 files changed

+64
-137
lines changed

7 files changed

+64
-137
lines changed
 

‎aggregator/src/aggregation/circuit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl Circuit<Fr> for AggregationCircuit {
154154
config: Self::Config,
155155
mut layouter: impl Layouter<Fr>,
156156
) -> Result<(), Error> {
157-
let (config, challenge) = config;
157+
let (mut config, challenge) = config;
158158

159159
let witness_time = start_timer!(|| "synthesize | Aggregation Circuit");
160160

@@ -266,7 +266,7 @@ impl Circuit<Fr> for AggregationCircuit {
266266

267267
let timer = start_timer!(|| ("assign hash cells").to_string());
268268
let (hash_digest_cells, num_valid_snarks) = assign_batch_hashes(
269-
&config,
269+
&mut config,
270270
&mut layouter,
271271
challenges,
272272
&preimages,

‎aggregator/src/aggregation/rlc/config.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use halo2_proofs::{
2+
circuit::AssignedCell,
23
halo2curves::bn256::Fr,
34
plonk::{Advice, Column, ConstraintSystem, Fixed, SecondPhase, Selector},
45
poly::Rotation,
@@ -10,7 +11,7 @@ use zkevm_circuits::util::Challenges;
1011

1112
/// This config is used to compute RLCs for bytes.
1213
/// It requires a phase 2 column
13-
#[derive(Debug, Clone, Copy)]
14+
#[derive(Debug, Clone)]
1415
pub struct RlcConfig {
1516
#[cfg(test)]
1617
// Test requires a phase 1 column before proceed to phase 2.
@@ -19,6 +20,8 @@ pub struct RlcConfig {
1920
pub(crate) selector: Selector,
2021
pub(crate) fixed: Column<Fixed>,
2122
pub(crate) enable_challenge: Selector,
23+
// fixed cells storing 0, 1, 2, 4, 8, 32
24+
pub(crate) fixed_cells: Vec<AssignedCell<Fr, Fr>>,
2225
}
2326

2427
impl RlcConfig {
@@ -73,6 +76,7 @@ impl RlcConfig {
7376
selector,
7477
fixed,
7578
enable_challenge,
79+
fixed_cells: vec![],
7680
}
7781
}
7882
}

‎aggregator/src/aggregation/rlc/gates.rs

+44-110
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use halo2_proofs::{
2-
circuit::{AssignedCell, Cell, Region, RegionIndex, Value},
2+
circuit::{AssignedCell, Region, Value},
33
halo2curves::bn256::Fr,
44
plonk::Error,
55
};
@@ -10,81 +10,47 @@ use crate::{constants::LOG_DEGREE, util::assert_equal};
1010
use super::RlcConfig;
1111

1212
impl RlcConfig {
13-
/// initialize the chip with fixed 0 and 1 cells
14-
pub(crate) fn init(&self, region: &mut Region<Fr>) -> Result<(), Error> {
15-
region.assign_fixed(|| "const zero", self.fixed, 0, || Value::known(Fr::zero()))?;
16-
region.assign_fixed(|| "const one", self.fixed, 1, || Value::known(Fr::one()))?;
17-
region.assign_fixed(|| "const two", self.fixed, 2, || Value::known(Fr::from(2)))?;
18-
region.assign_fixed(|| "const four", self.fixed, 3, || Value::known(Fr::from(4)))?;
19-
region.assign_fixed(
13+
/// initialize the chip with fixed values storing 0, 1, 2, 4, 8, 32
14+
pub(crate) fn init(&mut self, region: &mut Region<Fr>) -> Result<(), Error> {
15+
self.fixed_cells.push(region.assign_fixed(
16+
|| "const zero",
17+
self.fixed,
18+
0,
19+
|| Value::known(Fr::zero()),
20+
)?);
21+
self.fixed_cells.push(region.assign_fixed(
22+
|| "const one",
23+
self.fixed,
24+
1,
25+
|| Value::known(Fr::one()),
26+
)?);
27+
self.fixed_cells.push(region.assign_fixed(
28+
|| "const two",
29+
self.fixed,
30+
2,
31+
|| Value::known(Fr::from(2)),
32+
)?);
33+
self.fixed_cells.push(region.assign_fixed(
34+
|| "const four",
35+
self.fixed,
36+
3,
37+
|| Value::known(Fr::from(4)),
38+
)?);
39+
self.fixed_cells.push(region.assign_fixed(
2040
|| "const eight",
2141
self.fixed,
2242
4,
2343
|| Value::known(Fr::from(8)),
24-
)?;
25-
region.assign_fixed(
44+
)?);
45+
self.fixed_cells.push(region.assign_fixed(
2646
|| "const thirty two",
2747
self.fixed,
2848
5,
2949
|| Value::known(Fr::from(32)),
30-
)?;
50+
)?);
3151
Ok(())
3252
}
3353

34-
#[inline]
35-
pub(crate) fn zero_cell(&self, region_index: RegionIndex) -> Cell {
36-
Cell {
37-
region_index,
38-
row_offset: 0,
39-
column: self.fixed.into(),
40-
}
41-
}
42-
43-
#[inline]
44-
pub(crate) fn one_cell(&self, region_index: RegionIndex) -> Cell {
45-
Cell {
46-
region_index,
47-
row_offset: 1,
48-
column: self.fixed.into(),
49-
}
50-
}
51-
52-
#[inline]
53-
pub(crate) fn two_cell(&self, region_index: RegionIndex) -> Cell {
54-
Cell {
55-
region_index,
56-
row_offset: 2,
57-
column: self.fixed.into(),
58-
}
59-
}
60-
61-
#[inline]
62-
pub(crate) fn four_cell(&self, region_index: RegionIndex) -> Cell {
63-
Cell {
64-
region_index,
65-
row_offset: 3,
66-
column: self.fixed.into(),
67-
}
68-
}
69-
70-
#[inline]
71-
pub(crate) fn eight_cell(&self, region_index: RegionIndex) -> Cell {
72-
Cell {
73-
region_index,
74-
row_offset: 4,
75-
column: self.fixed.into(),
76-
}
77-
}
78-
79-
#[inline]
80-
pub(crate) fn thirty_two_cell(&self, region_index: RegionIndex) -> Cell {
81-
Cell {
82-
region_index,
83-
row_offset: 5,
84-
column: self.fixed.into(),
85-
}
86-
}
87-
8854
pub(crate) fn load_private(
8955
&self,
9056
region: &mut Region<Fr>,
@@ -125,8 +91,8 @@ impl RlcConfig {
12591
region: &mut Region<Fr>,
12692
f: &AssignedCell<Fr, Fr>,
12793
) -> Result<(), Error> {
128-
let zero_cell = self.zero_cell(f.cell().region_index);
129-
region.constrain_equal(f.cell(), zero_cell)
94+
let zero_cell = &self.fixed_cells[0];
95+
region.constrain_equal(f.cell(), zero_cell.cell())
13096
}
13197

13298
/// Enforce the element in f is a binary element.
@@ -150,16 +116,10 @@ impl RlcConfig {
150116
offset: &mut usize,
151117
) -> Result<AssignedCell<Fr, Fr>, Error> {
152118
self.selector.enable(region, *offset)?;
153-
let one_cell = self.one_cell(a.cell().region_index);
119+
let one_cell = &self.fixed_cells[1];
154120

155121
a.copy_advice(|| "a", region, self.phase_2_column, *offset)?;
156-
let one = region.assign_advice(
157-
|| "c",
158-
self.phase_2_column,
159-
*offset + 1,
160-
|| Value::known(Fr::one()),
161-
)?;
162-
region.constrain_equal(one.cell(), one_cell)?;
122+
one_cell.copy_advice(|| "b", region, self.phase_2_column, *offset + 1)?;
163123
b.copy_advice(|| "c", region, self.phase_2_column, *offset + 2)?;
164124
let d = region.assign_advice(
165125
|| "d",
@@ -181,21 +141,15 @@ impl RlcConfig {
181141
offset: &mut usize,
182142
) -> Result<AssignedCell<Fr, Fr>, Error> {
183143
self.selector.enable(region, *offset)?;
184-
let one_cell = self.one_cell(a.cell().region_index);
144+
let one_cell = &self.fixed_cells[1];
185145

186146
let res = region.assign_advice(
187147
|| "a",
188148
self.phase_2_column,
189149
*offset,
190150
|| a.value() - b.value(),
191151
)?;
192-
let one = region.assign_advice(
193-
|| "b",
194-
self.phase_2_column,
195-
*offset + 1,
196-
|| Value::known(Fr::one()),
197-
)?;
198-
region.constrain_equal(one.cell(), one_cell)?;
152+
one_cell.copy_advice(|| "b", region, self.phase_2_column, *offset + 1)?;
199153
b.copy_advice(|| "c", region, self.phase_2_column, *offset + 2)?;
200154
a.copy_advice(|| "d", region, self.phase_2_column, *offset + 3)?;
201155
*offset += 4;
@@ -212,17 +166,11 @@ impl RlcConfig {
212166
offset: &mut usize,
213167
) -> Result<AssignedCell<Fr, Fr>, Error> {
214168
self.selector.enable(region, *offset)?;
215-
let zero_cell = self.zero_cell(a.cell().region_index);
169+
let zero_cell = &self.fixed_cells[0];
216170

217171
a.copy_advice(|| "a", region, self.phase_2_column, *offset)?;
218172
b.copy_advice(|| "b", region, self.phase_2_column, *offset + 1)?;
219-
let zero = region.assign_advice(
220-
|| "b",
221-
self.phase_2_column,
222-
*offset + 2,
223-
|| Value::known(Fr::zero()),
224-
)?;
225-
region.constrain_equal(zero.cell(), zero_cell)?;
173+
zero_cell.copy_advice(|| "c", region, self.phase_2_column, *offset + 2)?;
226174
let d = region.assign_advice(
227175
|| "d",
228176
self.phase_2_column,
@@ -267,10 +215,8 @@ impl RlcConfig {
267215
a: &AssignedCell<Fr, Fr>,
268216
offset: &mut usize,
269217
) -> Result<AssignedCell<Fr, Fr>, Error> {
270-
let one_cell = self.one_cell(a.cell().region_index);
271-
let one = self.load_private(region, &Fr::one(), offset)?;
272-
region.constrain_equal(one_cell, one.cell())?;
273-
self.sub(region, &one, a, offset)
218+
let one_cell = &self.fixed_cells[1];
219+
self.sub(region, one_cell, a, offset)
274220
}
275221

276222
// if cond = 1 return a, else b
@@ -372,22 +318,10 @@ impl RlcConfig {
372318
})
373319
.collect::<Result<Vec<_>, Error>>()?;
374320

375-
let mut acc = {
376-
let zero = self.load_private(region, &Fr::from(0), offset)?;
377-
let zero_cell = self.zero_cell(zero.cell().region_index);
378-
region.constrain_equal(zero_cell, zero.cell())?;
379-
zero
380-
};
381-
382-
let two = {
383-
let two = self.load_private(region, &Fr::from(2), offset)?;
384-
let two_cell = self.two_cell(two.cell().region_index);
385-
region.constrain_equal(two_cell, two.cell())?;
386-
two
387-
};
388-
321+
let mut acc = self.fixed_cells[0].clone();
322+
let two = &self.fixed_cells[2];
389323
for bit in bit_cells.iter().rev() {
390-
acc = self.mul_add(region, &acc, &two, bit, offset)?;
324+
acc = self.mul_add(region, &acc, two, bit, offset)?;
391325
}
392326

393327
// sanity check

‎aggregator/src/core.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub(crate) fn extract_accumulators_and_proof(
103103
// 7. chunk[i]'s data_hash == "" when chunk[i] is padded
104104
#[allow(clippy::type_complexity)]
105105
pub(crate) fn assign_batch_hashes(
106-
config: &AggregationConfig,
106+
config: &mut AggregationConfig,
107107
layouter: &mut impl Layouter<Fr>,
108108
challenges: Challenges<Value<Fr>>,
109109
preimages: &[Vec<u8>],
@@ -129,7 +129,7 @@ pub(crate) fn assign_batch_hashes(
129129
// 6. chunk[i]'s prev_state_root == post_state_root when chunk[i] is padded
130130
// 7. chunk[i]'s data_hash == "" when chunk[i] is padded
131131
let num_valid_snarks = conditional_constraints(
132-
&config.rlc_config,
132+
&mut config.rlc_config,
133133
// config.flex_gate(),
134134
layouter,
135135
challenges,
@@ -408,7 +408,7 @@ fn copy_constraints(
408408
// 7. chunk[i]'s data_hash == "" when chunk[i] is padded
409409
#[allow(clippy::too_many_arguments)]
410410
pub(crate) fn conditional_constraints(
411-
rlc_config: &RlcConfig,
411+
rlc_config: &mut RlcConfig,
412412
layouter: &mut impl Layouter<Fr>,
413413
challenges: Challenges<Value<Fr>>,
414414
hash_input_cells: &[AssignedCell<Fr, Fr>],
@@ -456,29 +456,20 @@ pub(crate) fn conditional_constraints(
456456
// 5,6,7,8 | 32 | 0, 1, 0
457457
// 9,10 | 64 | 0, 0, 1
458458

459-
let four = {
460-
let four = rlc_config.load_private(&mut region, &Fr::from(4), &mut offset)?;
461-
let four_cell = rlc_config.four_cell(four.cell().region_index);
462-
region.constrain_equal(four_cell, four.cell())?;
463-
four
464-
};
465-
let eight = {
466-
let eight = rlc_config.load_private(&mut region, &Fr::from(8), &mut offset)?;
467-
let eight_cell = rlc_config.eight_cell(eight.cell().region_index);
468-
region.constrain_equal(eight_cell, eight.cell())?;
469-
eight
470-
};
459+
let four = &rlc_config.fixed_cells[3];
460+
let eight = &rlc_config.fixed_cells[4];
461+
471462
let flag1 = rlc_config.is_smaller_than(
472463
&mut region,
473464
&num_of_valid_snarks_cell[0],
474-
&four,
465+
four,
475466
&mut offset,
476467
)?;
477468
let not_flag1 = rlc_config.not(&mut region, &flag1, &mut offset)?;
478469
let not_flag3 = rlc_config.is_smaller_than(
479470
&mut region,
480471
&num_of_valid_snarks_cell[0],
481-
&eight,
472+
eight,
482473
&mut offset,
483474
)?;
484475
let flag3 = rlc_config.not(&mut region, &not_flag3, &mut offset)?;
@@ -660,13 +651,11 @@ pub(crate) fn conditional_constraints(
660651

661652
// 7. chunk[i]'s data_hash == "" when chunk[i] is padded
662653
// that means the data_hash length is 32 * number_of_valid_snarks
663-
let const32 = rlc_config.load_private(&mut region, &Fr::from(32), &mut offset)?;
664-
let const32_cell = rlc_config.thirty_two_cell(const32.cell().region_index);
665-
region.constrain_equal(const32.cell(), const32_cell)?;
654+
let const32 = &rlc_config.fixed_cells[5];
666655
let data_hash_inputs = rlc_config.mul(
667656
&mut region,
668657
&num_of_valid_snarks_cell[0],
669-
&const32,
658+
const32,
670659
&mut offset,
671660
)?;
672661

‎aggregator/src/tests/mock_chunk.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818

1919
/// This config is used to compute RLCs for bytes.
2020
/// It requires a phase 2 column
21-
#[derive(Debug, Clone, Copy)]
21+
#[derive(Debug, Clone)]
2222
pub struct MockConfig {
2323
pub(crate) rlc_config: RlcConfig,
2424
/// Instance for public input; stores

‎aggregator/src/tests/rlc/dynamic_hashes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl Circuit<Fr> for DynamicHashCircuit {
7777
config: Self::Config,
7878
mut layouter: impl Layouter<Fr>,
7979
) -> Result<(), Error> {
80-
let (config, challenges) = config;
80+
let (mut config, challenges) = config;
8181

8282
config
8383
.keccak_circuit_config

‎aggregator/src/tests/rlc/gates.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Circuit<Fr> for ArithTestCircuit {
3838

3939
fn synthesize(
4040
&self,
41-
config: Self::Config,
41+
mut config: Self::Config,
4242
mut layouter: impl Layouter<Fr>,
4343
) -> Result<(), Error> {
4444
let mut rng = test_rng();

0 commit comments

Comments
 (0)
Please sign in to comment.