11use crate :: Machine ;
2+ use crate :: __internal:: ConstraintFolder ;
23use alloc:: vec;
34use alloc:: vec:: Vec ;
45use valida_util:: batch_multiplicative_inverse;
56
6- use crate :: __internal:: ConstraintFolder ;
77use p3_air:: { Air , AirBuilder , PairBuilder , PermutationAirBuilder , VirtualPairCol } ;
88use p3_field:: { AbstractExtensionField , AbstractField , ExtensionField , Field , Powers , PrimeField } ;
99use p3_matrix:: { dense:: RowMajorMatrix , Matrix , MatrixRowSlices } ;
@@ -119,6 +119,8 @@ where
119119 let ( alphas_local, alphas_global) = generate_rlc_elements ( machine, chip, & random_elements) ;
120120 let betas = random_elements[ 2 ] . powers ( ) ;
121121
122+ let preprocessed = chip. preprocessed_trace ( ) ;
123+
122124 // Compute the reciprocal columns
123125 //
124126 // Row: | q_1 | q_2 | q_3 | ... | q_n | \phi |
@@ -131,15 +133,26 @@ where
131133 let perm_width = all_interactions. len ( ) + 1 ;
132134 let mut perm_values = Vec :: with_capacity ( main. height ( ) * perm_width) ;
133135
134- for main_row in main. rows ( ) {
136+ for ( n , main_row) in main. rows ( ) . enumerate ( ) {
135137 let mut row = vec ! [ M :: EF :: ZERO ; perm_width] ;
136- for ( n , ( interaction, _) ) in all_interactions. iter ( ) . enumerate ( ) {
137- let alpha_i = if interaction. is_local ( ) {
138+ for ( m , ( interaction, _) ) in all_interactions. iter ( ) . enumerate ( ) {
139+ let alpha_m = if interaction. is_local ( ) {
138140 alphas_local[ interaction. argument_index ( ) ]
139141 } else {
140142 alphas_global[ interaction. argument_index ( ) ]
141143 } ;
142- row[ n] = reduce_row ( main_row, & interaction. fields , alpha_i, betas. clone ( ) ) ;
144+ let preprocessed_row = if preprocessed. is_some ( ) {
145+ preprocessed. as_ref ( ) . unwrap ( ) . row_slice ( n)
146+ } else {
147+ & [ ]
148+ } ;
149+ row[ m] = reduce_row (
150+ main_row,
151+ preprocessed_row,
152+ & interaction. fields ,
153+ alpha_m,
154+ betas. clone ( ) ,
155+ ) ;
143156 }
144157 perm_values. extend ( row) ;
145158 }
@@ -152,8 +165,15 @@ where
152165 if n > 0 {
153166 phi[ n] = phi[ n - 1 ] ;
154167 }
168+ let preprocessed_row = if preprocessed. is_some ( ) {
169+ preprocessed. as_ref ( ) . unwrap ( ) . row_slice ( n)
170+ } else {
171+ & [ ]
172+ } ;
155173 for ( m, ( interaction, interaction_type) ) in all_interactions. iter ( ) . enumerate ( ) {
156- let mult = interaction. count . apply :: < M :: F , M :: F > ( & [ ] , main_row) ;
174+ let mult = interaction
175+ . count
176+ . apply :: < M :: F , M :: F > ( preprocessed_row, main_row) ;
157177 match interaction_type {
158178 InteractionType :: LocalSend | InteractionType :: GlobalSend => {
159179 phi[ n] += M :: EF :: from_base ( mult) * perm_row[ m] ;
@@ -187,6 +207,7 @@ where
187207
188208 let preprocessed = builder. preprocessed ( ) ;
189209 let preprocessed_local = preprocessed. row_slice ( 0 ) ;
210+ let preprocessed_next = preprocessed. row_slice ( 1 ) ;
190211
191212 let perm = builder. permutation ( ) ;
192213 let perm_width = perm. width ( ) ;
@@ -220,8 +241,10 @@ where
220241
221242 let mult_local = interaction
222243 . count
223- . apply :: < AB :: Expr , AB :: Var > ( & [ ] , main_local) ;
224- let mult_next = interaction. count . apply :: < AB :: Expr , AB :: Var > ( & [ ] , main_next) ;
244+ . apply :: < AB :: Expr , AB :: Var > ( preprocessed_local, main_local) ;
245+ let mult_next = interaction
246+ . count
247+ . apply :: < AB :: Expr , AB :: Var > ( preprocessed_next, main_next) ;
225248
226249 // Build the RHS of the permutation constraint
227250 match interaction_type {
@@ -294,14 +317,20 @@ where
294317
295318// TODO: Use Var and Expr type bounds in place of concrete fields so that
296319// this function can be used in `eval_permutation_constraints`.
297- fn reduce_row < F , EF > ( row : & [ F ] , fields : & [ VirtualPairCol < F > ] , alpha : EF , betas : Powers < EF > ) -> EF
320+ fn reduce_row < F , EF > (
321+ main_row : & [ F ] ,
322+ preprocessed_row : & [ F ] ,
323+ fields : & [ VirtualPairCol < F > ] ,
324+ alpha : EF ,
325+ betas : Powers < EF > ,
326+ ) -> EF
298327where
299328 F : Field ,
300329 EF : ExtensionField < F > ,
301330{
302331 let mut rlc = EF :: ZERO ;
303332 for ( columns, beta) in fields. iter ( ) . zip ( betas) {
304- rlc += beta * columns. apply :: < F , F > ( & [ ] , row )
333+ rlc += beta * columns. apply :: < F , F > ( preprocessed_row , main_row )
305334 }
306335 rlc += alpha;
307336 rlc
0 commit comments