@@ -190,3 +190,156 @@ pub fn expr_convert_to_witins<E: ExtensionField>(
190190 Expression :: Challenge ( ..) => ( ) ,
191191 }
192192}
193+
194+ pub const DagLoadWit : usize = 0 ;
195+ pub const DagLoadScalar : usize = 1 ;
196+ pub const DagAdd : usize = 2 ;
197+ pub const DagMul : usize = 3 ;
198+
199+ pub fn expr_compression_to_dag < E : ExtensionField > (
200+ expr : & Expression < E > ,
201+ challenges_offset : usize ,
202+ constant_offset : usize ,
203+ ) -> (
204+ Vec < u32 > ,
205+ Vec < Instance > ,
206+ Vec < Expression < E > > ,
207+ Vec < Either < E :: BaseField , E > > ,
208+ ) {
209+ let mut dag = vec ! [ ] ;
210+ let mut constant = vec ! [ ] ;
211+ let mut instance_scalar = vec ! [ ] ;
212+ let mut challenges = vec ! [ ] ;
213+ expr_compression_to_dag_helper (
214+ & mut dag,
215+ & mut instance_scalar,
216+ challenges_offset,
217+ & mut challenges,
218+ constant_offset,
219+ & mut constant,
220+ expr,
221+ ) ;
222+ ( dag, instance_scalar, challenges, constant)
223+ }
224+
225+ fn expr_compression_to_dag_helper < E : ExtensionField > (
226+ dag : & mut Vec < u32 > ,
227+ instance_scalar : & mut Vec < Instance > ,
228+ challenges_offset : usize ,
229+ challenges : & mut Vec < Expression < E > > ,
230+ constant_offset : usize ,
231+ constant : & mut Vec < Either < E :: BaseField , E > > ,
232+ expr : & Expression < E > ,
233+ ) -> ( usize , usize ) {
234+ // (max_degree, max_depth)
235+ match expr {
236+ Expression :: Fixed ( _) => unimplemented ! ( ) ,
237+ Expression :: WitIn ( wit_id) => {
238+ dag. extend ( vec ! [ DagLoadWit as u32 , * wit_id as u32 ] ) ;
239+ ( 1 , 1 )
240+ }
241+ Expression :: StructuralWitIn ( _, ..) => unimplemented ! ( ) ,
242+ Expression :: Instance ( _) => unimplemented ! ( ) ,
243+ Expression :: InstanceScalar ( inst) => {
244+ instance_scalar. push ( inst. clone ( ) ) ;
245+ dag. extend ( vec ! [ DagLoadScalar as u32 , instance_scalar. len( ) as u32 - 1 ] ) ;
246+ ( 0 , 1 )
247+ }
248+ Expression :: Constant ( value) => {
249+ constant. push ( value. clone ( ) ) ;
250+ dag. extend ( vec ! [
251+ DagLoadScalar as u32 ,
252+ ( constant_offset + constant. len( ) ) as u32 - 1 ,
253+ ] ) ;
254+ ( 0 , 1 )
255+ }
256+ Expression :: Sum ( a, b) => {
257+ let ( max_degree_a, max_depth_a) = expr_compression_to_dag_helper (
258+ dag,
259+ instance_scalar,
260+ challenges_offset,
261+ challenges,
262+ constant_offset,
263+ constant,
264+ a,
265+ ) ;
266+ let ( max_degree_b, max_depth_b) = expr_compression_to_dag_helper (
267+ dag,
268+ instance_scalar,
269+ challenges_offset,
270+ challenges,
271+ constant_offset,
272+ constant,
273+ b,
274+ ) ;
275+ dag. extend ( vec ! [ DagAdd as u32 ] ) ;
276+ (
277+ max_degree_a. max ( max_degree_b) ,
278+ ( max_depth_a + 1 ) . max ( max_depth_b) ,
279+ ) // 1 comes from store result of `a`
280+ }
281+ Expression :: Product ( a, b) => {
282+ let ( max_degree_a, max_depth_a) = expr_compression_to_dag_helper (
283+ dag,
284+ instance_scalar,
285+ challenges_offset,
286+ challenges,
287+ constant_offset,
288+ constant,
289+ a,
290+ ) ;
291+ let ( max_degree_b, max_depth_b) = expr_compression_to_dag_helper (
292+ dag,
293+ instance_scalar,
294+ challenges_offset,
295+ challenges,
296+ constant_offset,
297+ constant,
298+ b,
299+ ) ;
300+ dag. extend ( vec ! [ DagMul as u32 ] ) ;
301+ (
302+ max_degree_a + max_degree_b,
303+ ( max_depth_a + 1 ) . max ( max_depth_b) ,
304+ ) // 1 comes from store result of `a`
305+ }
306+ Expression :: ScaledSum ( x, a, b) => {
307+ expr_compression_to_dag_helper (
308+ dag,
309+ instance_scalar,
310+ challenges_offset,
311+ challenges,
312+ constant_offset,
313+ constant,
314+ x,
315+ ) ;
316+ expr_compression_to_dag_helper (
317+ dag,
318+ instance_scalar,
319+ challenges_offset,
320+ challenges,
321+ constant_offset,
322+ constant,
323+ a,
324+ ) ;
325+ dag. extend ( vec ! [ DagMul as u32 ] ) ;
326+ expr_compression_to_dag_helper (
327+ dag,
328+ instance_scalar,
329+ challenges_offset,
330+ challenges,
331+ constant_offset,
332+ constant,
333+ b,
334+ ) ;
335+ dag. extend ( vec ! [ DagAdd as u32 ] ) ;
336+ }
337+ c @ Expression :: Challenge ( ..) => {
338+ challenges. push ( c. clone ( ) ) ;
339+ dag. extend ( vec ! [
340+ DagLoadScalar as u32 ,
341+ ( challenges_offset + challenges. len( ) ) as u32 - 1 ,
342+ ] )
343+ }
344+ }
345+ }
0 commit comments