@@ -17,11 +17,7 @@ use crate::witgen::{
1717 data_structures:: identity:: Identity , jit:: variable:: MachineCallVariable , FixedData ,
1818} ;
1919
20- use super :: {
21- prover_function_heuristics:: ProverFunction ,
22- variable:: Variable ,
23- witgen_inference:: { Assignment , VariableOrValue } ,
24- } ;
20+ use super :: { prover_function_heuristics:: ProverFunction , variable:: Variable } ;
2521
2622/// Keeps track of identities that still need to be processed and
2723/// updates this list based on the occurrence of updated variables
@@ -35,20 +31,9 @@ pub struct IdentityQueue<'a, T: FieldElement> {
3531impl < ' a , T : FieldElement > IdentityQueue < ' a , T > {
3632 pub fn new (
3733 fixed_data : & ' a FixedData < ' a , T > ,
38- identities : & [ ( & ' a Identity < T > , i32 ) ] ,
39- assignments : & [ Assignment < ' a , T > ] ,
40- prover_functions : & [ ( ProverFunction < ' a , T > , i32 ) ] ,
34+ items : impl IntoIterator < Item = QueueItem < ' a , T > > ,
4135 ) -> Self {
42- let queue: BTreeSet < _ > = identities
43- . iter ( )
44- . map ( |( id, row) | QueueItem :: Identity ( id, * row) )
45- . chain ( assignments. iter ( ) . map ( |a| QueueItem :: Assignment ( a. clone ( ) ) ) )
46- . chain (
47- prover_functions
48- . iter ( )
49- . map ( |( p, row) | QueueItem :: ProverFunction ( p. clone ( ) , * row) ) ,
50- )
51- . collect ( ) ;
36+ let queue: BTreeSet < _ > = items. into_iter ( ) . collect ( ) ;
5237 let mut references = ReferencesComputer :: new ( fixed_data) ;
5338 let occurrences = Rc :: new (
5439 queue
@@ -93,25 +78,50 @@ impl<'a, T: FieldElement> IdentityQueue<'a, T> {
9378#[ derive( Clone ) ]
9479pub enum QueueItem < ' a , T : FieldElement > {
9580 Identity ( & ' a Identity < T > , i32 ) ,
96- Assignment ( Assignment < ' a , T > ) ,
81+ VariableAssignment ( VariableAssignment < ' a , T > ) ,
82+ ConstantAssignment ( ConstantAssignment < ' a , T > ) ,
9783 ProverFunction ( ProverFunction < ' a , T > , i32 ) ,
9884}
9985
100- /// Sorts identities by row and then by ID, preceded by assignments.
10186impl < T : FieldElement > Ord for QueueItem < ' _ , T > {
10287 fn cmp ( & self , other : & Self ) -> std:: cmp:: Ordering {
10388 match ( self , other) {
10489 ( QueueItem :: Identity ( id1, row1) , QueueItem :: Identity ( id2, row2) ) => {
10590 ( row1, id1. id ( ) ) . cmp ( & ( row2, id2. id ( ) ) )
10691 }
107- ( QueueItem :: Assignment ( a1) , QueueItem :: Assignment ( a2) ) => a1. cmp ( a2) ,
92+ ( QueueItem :: VariableAssignment ( a1) , QueueItem :: VariableAssignment ( a2) ) => a1. cmp ( a2) ,
93+ ( QueueItem :: ConstantAssignment ( a1) , QueueItem :: ConstantAssignment ( a2) ) => a1. cmp ( a2) ,
10894 ( QueueItem :: ProverFunction ( p1, row1) , QueueItem :: ProverFunction ( p2, row2) ) => {
10995 ( row1, p1. index ) . cmp ( & ( row2, p2. index ) )
11096 }
111- ( QueueItem :: Assignment ( ..) , _) => std:: cmp:: Ordering :: Less ,
112- ( QueueItem :: Identity ( ..) , QueueItem :: Assignment ( ..) ) => std:: cmp:: Ordering :: Greater ,
113- ( QueueItem :: Identity ( ..) , QueueItem :: ProverFunction ( ..) ) => std:: cmp:: Ordering :: Less ,
114- ( QueueItem :: ProverFunction ( ..) , _) => std:: cmp:: Ordering :: Greater ,
97+ ( a, b) => a. order ( ) . cmp ( & b. order ( ) ) ,
98+ }
99+ }
100+ }
101+
102+ impl < ' a , T : FieldElement > QueueItem < ' a , T > {
103+ pub fn constant_assignment ( lhs : & ' a Expression < T > , rhs : T , row_offset : i32 ) -> Self {
104+ QueueItem :: ConstantAssignment ( ConstantAssignment {
105+ lhs,
106+ row_offset,
107+ rhs,
108+ } )
109+ }
110+
111+ pub fn variable_assignment ( lhs : & ' a Expression < T > , rhs : Variable , row_offset : i32 ) -> Self {
112+ QueueItem :: VariableAssignment ( VariableAssignment {
113+ lhs,
114+ row_offset,
115+ rhs,
116+ } )
117+ }
118+
119+ fn order ( & self ) -> u32 {
120+ match self {
121+ QueueItem :: ConstantAssignment ( ..) => 0 ,
122+ QueueItem :: VariableAssignment ( ..) => 1 ,
123+ QueueItem :: Identity ( ..) => 2 ,
124+ QueueItem :: ProverFunction ( ..) => 3 ,
115125 }
116126 }
117127}
@@ -130,6 +140,24 @@ impl<T: FieldElement> PartialEq for QueueItem<'_, T> {
130140
131141impl < T : FieldElement > Eq for QueueItem < ' _ , T > { }
132142
143+ /// An equality constraint between an algebraic expression evaluated
144+ /// on a certain row offset and a variable.
145+ #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Debug ) ]
146+ pub struct VariableAssignment < ' a , T : FieldElement > {
147+ pub lhs : & ' a Expression < T > ,
148+ pub row_offset : i32 ,
149+ pub rhs : Variable ,
150+ }
151+
152+ /// An equality constraint between an algebraic expression evaluated
153+ /// on a certain row offset and a constant.
154+ #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Debug ) ]
155+ pub struct ConstantAssignment < ' a , T : FieldElement > {
156+ pub lhs : & ' a Expression < T > ,
157+ pub row_offset : i32 ,
158+ pub rhs : T ,
159+ }
160+
133161/// Utility to compute the variables that occur in a queue item.
134162/// Follows intermediate column references and employs caches.
135163struct ReferencesComputer < ' a , T : FieldElement > {
@@ -171,17 +199,14 @@ impl<'a, T: FieldElement> ReferencesComputer<'a, T> {
171199 ) ,
172200 Identity :: Connect ( ..) => Box :: new ( std:: iter:: empty ( ) ) ,
173201 } ,
174- QueueItem :: Assignment ( a) => {
175- let vars_in_rhs = match & a. rhs {
176- VariableOrValue :: Variable ( v) => Some ( v. clone ( ) ) ,
177- VariableOrValue :: Value ( _) => None ,
178- } ;
179- Box :: new (
180- self . variables_in_expression ( a. lhs , a. row_offset )
181- . into_iter ( )
182- . chain ( vars_in_rhs) ,
183- )
184- }
202+ QueueItem :: ConstantAssignment ( a) => Box :: new (
203+ self . variables_in_expression ( a. lhs , a. row_offset )
204+ . into_iter ( ) ,
205+ ) ,
206+ QueueItem :: VariableAssignment ( a) => Box :: new (
207+ std:: iter:: once ( a. rhs . clone ( ) )
208+ . chain ( self . variables_in_expression ( a. lhs , a. row_offset ) ) ,
209+ ) ,
185210 QueueItem :: ProverFunction ( p, row) => Box :: new (
186211 p. condition
187212 . iter ( )
0 commit comments