1
1
use halo2_proofs:: {
2
- circuit:: { AssignedCell , Cell , Region , RegionIndex , Value } ,
2
+ circuit:: { AssignedCell , Region , Value } ,
3
3
halo2curves:: bn256:: Fr ,
4
4
plonk:: Error ,
5
5
} ;
@@ -10,81 +10,47 @@ use crate::{constants::LOG_DEGREE, util::assert_equal};
10
10
use super :: RlcConfig ;
11
11
12
12
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 (
20
40
|| "const eight" ,
21
41
self . fixed ,
22
42
4 ,
23
43
|| Value :: known ( Fr :: from ( 8 ) ) ,
24
- ) ?;
25
- region. assign_fixed (
44
+ ) ?) ;
45
+ self . fixed_cells . push ( region. assign_fixed (
26
46
|| "const thirty two" ,
27
47
self . fixed ,
28
48
5 ,
29
49
|| Value :: known ( Fr :: from ( 32 ) ) ,
30
- ) ?;
50
+ ) ?) ;
31
51
Ok ( ( ) )
32
52
}
33
53
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
-
88
54
pub ( crate ) fn load_private (
89
55
& self ,
90
56
region : & mut Region < Fr > ,
@@ -125,8 +91,8 @@ impl RlcConfig {
125
91
region : & mut Region < Fr > ,
126
92
f : & AssignedCell < Fr , Fr > ,
127
93
) -> 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 ( ) )
130
96
}
131
97
132
98
/// Enforce the element in f is a binary element.
@@ -150,16 +116,10 @@ impl RlcConfig {
150
116
offset : & mut usize ,
151
117
) -> Result < AssignedCell < Fr , Fr > , Error > {
152
118
self . selector . enable ( region, * offset) ?;
153
- let one_cell = self . one_cell ( a . cell ( ) . region_index ) ;
119
+ let one_cell = & self . fixed_cells [ 1 ] ;
154
120
155
121
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 ) ?;
163
123
b. copy_advice ( || "c" , region, self . phase_2_column , * offset + 2 ) ?;
164
124
let d = region. assign_advice (
165
125
|| "d" ,
@@ -181,21 +141,15 @@ impl RlcConfig {
181
141
offset : & mut usize ,
182
142
) -> Result < AssignedCell < Fr , Fr > , Error > {
183
143
self . selector . enable ( region, * offset) ?;
184
- let one_cell = self . one_cell ( a . cell ( ) . region_index ) ;
144
+ let one_cell = & self . fixed_cells [ 1 ] ;
185
145
186
146
let res = region. assign_advice (
187
147
|| "a" ,
188
148
self . phase_2_column ,
189
149
* offset,
190
150
|| a. value ( ) - b. value ( ) ,
191
151
) ?;
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 ) ?;
199
153
b. copy_advice ( || "c" , region, self . phase_2_column , * offset + 2 ) ?;
200
154
a. copy_advice ( || "d" , region, self . phase_2_column , * offset + 3 ) ?;
201
155
* offset += 4 ;
@@ -212,17 +166,11 @@ impl RlcConfig {
212
166
offset : & mut usize ,
213
167
) -> Result < AssignedCell < Fr , Fr > , Error > {
214
168
self . selector . enable ( region, * offset) ?;
215
- let zero_cell = self . zero_cell ( a . cell ( ) . region_index ) ;
169
+ let zero_cell = & self . fixed_cells [ 0 ] ;
216
170
217
171
a. copy_advice ( || "a" , region, self . phase_2_column , * offset) ?;
218
172
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 ) ?;
226
174
let d = region. assign_advice (
227
175
|| "d" ,
228
176
self . phase_2_column ,
@@ -267,10 +215,8 @@ impl RlcConfig {
267
215
a : & AssignedCell < Fr , Fr > ,
268
216
offset : & mut usize ,
269
217
) -> 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)
274
220
}
275
221
276
222
// if cond = 1 return a, else b
@@ -372,22 +318,10 @@ impl RlcConfig {
372
318
} )
373
319
. collect :: < Result < Vec < _ > , Error > > ( ) ?;
374
320
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 ] ;
389
323
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) ?;
391
325
}
392
326
393
327
// sanity check
0 commit comments