@@ -2,62 +2,17 @@ use crate::arith_helpers::{convert_b2_to_b13, convert_b2_to_b9, A4};
2
2
use crate :: common:: { PERMUTATION , ROUND_CONSTANTS } ;
3
3
use crate :: gate_helpers:: biguint_to_f;
4
4
use eth_types:: Field ;
5
- use halo2_proofs:: circuit:: AssignedCell ;
6
- use halo2_proofs:: circuit:: Layouter ;
7
- use halo2_proofs:: {
8
- plonk:: { Advice , Column , ConstraintSystem , Error , Fixed , Selector } ,
9
- poly:: Rotation ,
10
- } ;
11
5
use itertools:: Itertools ;
12
6
use std:: convert:: TryInto ;
13
7
14
8
#[ derive( Clone , Debug ) ]
15
- pub struct IotaConfig < F > {
16
- q_enable : Selector ,
17
- lane00 : Column < Advice > ,
18
- flag : Column < Advice > ,
19
- round_constant : Column < Fixed > ,
20
- round_constant_b13 : F ,
21
- a4_times_round_constants_b9 : [ F ; PERMUTATION ] ,
9
+ pub struct IotaConstants < F > {
10
+ pub round_constant_b13 : F ,
11
+ pub a4_times_round_constants_b9 : [ F ; PERMUTATION ] ,
22
12
}
23
13
24
- impl < F : Field > IotaConfig < F > {
25
- /// Iota step adds a round constant to the first lane.
26
- ///
27
- /// We enable the gate to handle 3 different cases:
28
- ///
29
- /// The first case takes place in the first 23 rounds, the prover MUST add
30
- /// the `A4` times round constant in base 9. We enforce it by requiring the
31
- /// flag equal to one.
32
- ///
33
- /// The second the third cases happen in the 24-th
34
- /// round. It depends if prover wants to absorb new input or not, which is
35
- /// indicated by the flag.
36
- ///
37
- /// If prover doesn't want to absorb new input,
38
- /// then add `A4 * round_constant_b9` as the previous 23-th round did.
39
- ///
40
- /// Otherwise, apply the round constant in base 13 to the state, which has
41
- /// been mixed with new input and converted form base 9 to base 13.
42
- pub fn configure (
43
- meta : & mut ConstraintSystem < F > ,
44
- lane00 : Column < Advice > ,
45
- flag : Column < Advice > ,
46
- round_constant : Column < Fixed > ,
47
- ) -> Self {
48
- let q_enable = meta. selector ( ) ;
49
- meta. enable_equality ( lane00) ;
50
- meta. enable_equality ( flag) ;
51
-
52
- meta. create_gate ( "iota" , |meta| {
53
- let q_enable = meta. query_selector ( q_enable) ;
54
- let flag = meta. query_advice ( flag, Rotation :: cur ( ) ) ;
55
- let lane00_next = meta. query_advice ( lane00, Rotation :: next ( ) ) ;
56
- let lane00 = meta. query_advice ( lane00, Rotation :: cur ( ) ) ;
57
- let round_constant = meta. query_fixed ( round_constant, Rotation :: cur ( ) ) ;
58
- vec ! [ q_enable * ( lane00_next - lane00 - flag * round_constant) ]
59
- } ) ;
60
-
14
+ impl < F : Field > Default for IotaConstants < F > {
15
+ fn default ( ) -> Self {
61
16
let round_constant_b13 =
62
17
biguint_to_f :: < F > ( & convert_b2_to_b13 ( ROUND_CONSTANTS [ PERMUTATION - 1 ] ) ) ;
63
18
@@ -72,132 +27,8 @@ impl<F: Field> IotaConfig<F> {
72
27
. unwrap ( ) ;
73
28
74
29
Self {
75
- q_enable,
76
- lane00,
77
- flag,
78
- round_constant,
79
30
round_constant_b13,
80
31
a4_times_round_constants_b9,
81
32
}
82
33
}
83
-
84
- /// The first 23 rounds. (No mixing logic involved).
85
- ///
86
- /// Applies IotaB9 steady-step logic.
87
- /// It consists of: `new_lane_00 - (lane00 * ROUND_CTANTS[round]) == 0`.
88
- pub fn assign_round_b9 (
89
- & self ,
90
- layouter : & mut impl Layouter < F > ,
91
- lane00 : AssignedCell < F , F > ,
92
- round : usize ,
93
- ) -> Result < AssignedCell < F , F > , Error > {
94
- layouter. assign_region (
95
- || "IotaB9" ,
96
- |mut region| {
97
- let offset = 0 ;
98
- self . q_enable . enable ( & mut region, offset) ?;
99
- lane00. copy_advice ( || "lane 00" , & mut region, self . lane00 , offset) ?;
100
- // In the normal round, we must add round constant. constrain flag to 1.
101
- let flag = region. assign_advice ( || "flag" , self . flag , offset, || Ok ( F :: one ( ) ) ) ?;
102
- region. constrain_constant ( flag. cell ( ) , F :: one ( ) ) ?;
103
-
104
- let constant = self . a4_times_round_constants_b9 [ round] ;
105
- region. assign_fixed (
106
- || "A4 * round_constant_b9" ,
107
- self . round_constant ,
108
- offset,
109
- || Ok ( constant) ,
110
- ) ?;
111
-
112
- let offset = 1 ;
113
- region. assign_advice (
114
- || "lane 00 + A4 * round_constant_b9" ,
115
- self . lane00 ,
116
- offset,
117
- || Ok ( lane00. value ( ) . cloned ( ) . unwrap_or_default ( ) + constant) ,
118
- )
119
- } ,
120
- )
121
- }
122
-
123
- /// The 24-th round. Copy the flag `no_mixing` here.
124
- ///
125
- /// If `no_mixing` is true: add `A4 * round_constant_b9`
126
- /// Otherwise, do nothing and return the orignal lane value in the next cell
127
- pub fn assign_b9_last_round (
128
- & self ,
129
- layouter : & mut impl Layouter < F > ,
130
- lane00 : AssignedCell < F , F > ,
131
- flag : AssignedCell < F , F > ,
132
- ) -> Result < AssignedCell < F , F > , Error > {
133
- layouter. assign_region (
134
- || "IotaB9" ,
135
- |mut region| {
136
- let offset = 0 ;
137
- self . q_enable . enable ( & mut region, offset) ?;
138
- lane00. copy_advice ( || "lane 00" , & mut region, self . lane00 , offset) ?;
139
- flag. copy_advice ( || "flag" , & mut region, self . flag , offset) ?;
140
-
141
- let constant = self . a4_times_round_constants_b9 [ PERMUTATION - 1 ] ;
142
- region. assign_fixed (
143
- || "A4 * round_constant_b9" ,
144
- self . round_constant ,
145
- offset,
146
- || Ok ( constant) ,
147
- ) ?;
148
-
149
- let offset = 1 ;
150
- region. assign_advice (
151
- || "lane 00 + A4 * round_constant_b9" ,
152
- self . lane00 ,
153
- offset,
154
- || {
155
- let flag = flag. value ( ) . cloned ( ) . unwrap_or_default ( ) ;
156
- let lane00 = lane00. value ( ) . cloned ( ) . unwrap_or_default ( ) ;
157
- Ok ( lane00 + flag * constant)
158
- } ,
159
- )
160
- } ,
161
- )
162
- }
163
-
164
- /// The 24-th round. Copy the flag `mixing` here.
165
- ///
166
- /// If `mixing` is true: add round constant in base 13.
167
- /// Otherwise, do nothing and return the orignal lane value in the next cell
168
- pub fn assign_round_b13 (
169
- & self ,
170
- layouter : & mut impl Layouter < F > ,
171
- lane00 : AssignedCell < F , F > ,
172
- flag : AssignedCell < F , F > ,
173
- ) -> Result < AssignedCell < F , F > , Error > {
174
- layouter. assign_region (
175
- || "IotaB9" ,
176
- |mut region| {
177
- let offset = 0 ;
178
- self . q_enable . enable ( & mut region, offset) ?;
179
- lane00. copy_advice ( || "lane 00" , & mut region, self . lane00 , offset) ?;
180
- flag. copy_advice ( || "flag" , & mut region, self . flag , offset) ?;
181
-
182
- region. assign_fixed (
183
- || "round_constant_b13" ,
184
- self . round_constant ,
185
- offset,
186
- || Ok ( self . round_constant_b13 ) ,
187
- ) ?;
188
-
189
- let offset = 1 ;
190
- region. assign_advice (
191
- || "lane 00 + round_constant_b13" ,
192
- self . lane00 ,
193
- offset,
194
- || {
195
- let lane00 = lane00. value ( ) . cloned ( ) . unwrap_or_default ( ) ;
196
- let flag = flag. value ( ) . cloned ( ) . unwrap_or_default ( ) ;
197
- Ok ( lane00 + flag * self . round_constant_b13 )
198
- } ,
199
- )
200
- } ,
201
- )
202
- }
203
34
}
0 commit comments