@@ -4,7 +4,10 @@ use crate::{
44 parser:: { ConstArrayValue , parse_program} ,
55} ;
66use backend:: PrimeCharacteristicRing ;
7- use lean_vm:: { Boolean , BooleanExpr , CustomHint , EXT_OP_FUNCTIONS , FunctionName , SourceLocation , Table , TableT } ;
7+ use lean_vm:: {
8+ Boolean , BooleanExpr , CustomHint , ExtensionOpMode , FunctionName , PrecompileArgs , PrecompileCompTimeArgs ,
9+ SourceLocation , Table , TableT ,
10+ } ;
811use std:: {
912 collections:: { BTreeMap , BTreeSet } ,
1013 fmt:: { Display , Formatter } ,
@@ -56,6 +59,8 @@ impl From<Var> for VarOrConstMallocAccess {
5659 }
5760}
5861
62+ pub type SimplePrecompile = PrecompileArgs < SimpleExpr , ConstExpression > ;
63+
5964#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
6065pub enum SimpleLine {
6166 Match {
@@ -92,10 +97,7 @@ pub enum SimpleLine {
9297 FunctionRet {
9398 return_data : Vec < SimpleExpr > ,
9499 } ,
95- Precompile {
96- table : Table ,
97- args : Vec < SimpleExpr > ,
98- } ,
100+ Precompile ( SimplePrecompile ) ,
99101 Panic {
100102 message : Option < String > ,
101103 } ,
@@ -155,7 +157,7 @@ impl SimpleLine {
155157 | Self :: RawAccess { .. }
156158 | Self :: FunctionCall { .. }
157159 | Self :: FunctionRet { .. }
158- | Self :: Precompile { .. }
160+ | Self :: Precompile ( .. )
159161 | Self :: Panic { .. }
160162 | Self :: CustomHint ( ..)
161163 | Self :: Print { .. }
@@ -180,7 +182,7 @@ impl SimpleLine {
180182 | Self :: RawAccess { .. }
181183 | Self :: FunctionCall { .. }
182184 | Self :: FunctionRet { .. }
183- | Self :: Precompile { .. }
185+ | Self :: Precompile ( .. )
184186 | Self :: Panic { .. }
185187 | Self :: CustomHint ( ..)
186188 | Self :: Print { .. }
@@ -204,9 +206,8 @@ impl SimpleLine {
204206 Self :: Match { value, .. } => vec ! [ value] ,
205207 Self :: IfNotZero { condition, .. } => vec ! [ condition] ,
206208 Self :: HintMAlloc { size, .. } => vec ! [ size] ,
207- Self :: Precompile { args, .. } | Self :: FunctionCall { args, .. } | Self :: CustomHint ( _, args) => {
208- args. iter ( ) . collect ( )
209- }
209+ Self :: Precompile ( precompile) => precompile. operand_exprs ( ) . to_vec ( ) ,
210+ Self :: FunctionCall { args, .. } | Self :: CustomHint ( _, args) => args. iter ( ) . collect ( ) ,
210211 Self :: FunctionRet { return_data } => return_data. iter ( ) . collect ( ) ,
211212 Self :: Print { content, .. } => content. iter ( ) . collect ( ) ,
212213 Self :: DebugAssert ( boolean, _) => vec ! [ & boolean. left, & boolean. right] ,
@@ -2117,11 +2118,7 @@ fn simplify_lines(
21172118
21182119 // Special handling for extension_op precompile
21192120 // Signature: func(ptr_a, ptr_b, ptr_res) or func(ptr_a, ptr_b, ptr_res, length)
2120- if let Some ( mode) = EXT_OP_FUNCTIONS
2121- . iter ( )
2122- . find ( |( name, _) | * name == function_name. as_str ( ) )
2123- . map ( |( _, mode) | * mode)
2124- {
2121+ if let Some ( mode) = ExtensionOpMode :: from_name ( function_name) {
21252122 if !targets. is_empty ( ) {
21262123 return Err ( format ! (
21272124 "Precompile {function_name} should not return values, at {location}"
@@ -2133,22 +2130,24 @@ fn simplify_lines(
21332130 args. len( )
21342131 ) ) ;
21352132 }
2136- let mut simplified_args: Vec < SimpleExpr > = args[ ..3 ]
2133+ let simplified_args = args[ ..3 ]
21372134 . iter ( )
21382135 . map ( |arg| simplify_expr ( ctx, state, const_malloc, arg, & mut res) )
21392136 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
2140- // Inject size (aux_1) and mode (aux_2)
2141- let size: SimpleExpr = if args. len ( ) == 4 {
2137+
2138+ let size = if args. len ( ) == 4 {
21422139 simplify_expr ( ctx, state, const_malloc, & args[ 3 ] , & mut res) ?
2140+ . as_constant ( )
2141+ . expect ( "extension op size must be a constant" )
21432142 } else {
2144- SimpleExpr :: one ( )
2143+ ConstExpression :: one ( )
21452144 } ;
2146- simplified_args . push ( size ) ;
2147- simplified_args. push ( SimpleExpr :: Constant ( mode . into ( ) ) ) ;
2148- res . push ( SimpleLine :: Precompile {
2149- table : Table :: extension_op ( ) ,
2150- args : simplified_args ,
2151- } ) ;
2145+ res . push ( SimpleLine :: Precompile ( PrecompileArgs {
2146+ arg_0 : simplified_args[ 0 ] . clone ( ) ,
2147+ arg_1 : simplified_args [ 1 ] . clone ( ) ,
2148+ res : simplified_args [ 2 ] . clone ( ) ,
2149+ data : PrecompileCompTimeArgs :: ExtensionOp { size , mode } ,
2150+ } ) ) ;
21522151 continue ;
21532152 }
21542153
@@ -2159,14 +2158,22 @@ fn simplify_lines(
21592158 "Precompile {function_name} should not return values, at {location}"
21602159 ) ) ;
21612160 }
2161+ if args. len ( ) != 3 {
2162+ return Err ( format ! (
2163+ "Precompile {function_name} expects 3 arguments (ptr_a, ptr_b, ptr_res), got {}, at {location}" ,
2164+ args. len( )
2165+ ) ) ;
2166+ }
21622167 let simplified_args = args
21632168 . iter ( )
21642169 . map ( |arg| simplify_expr ( ctx, state, const_malloc, arg, & mut res) )
21652170 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
2166- res. push ( SimpleLine :: Precompile {
2167- table : Table :: poseidon16 ( ) ,
2168- args : simplified_args,
2169- } ) ;
2171+ res. push ( SimpleLine :: Precompile ( PrecompileArgs {
2172+ arg_0 : simplified_args[ 0 ] . clone ( ) ,
2173+ arg_1 : simplified_args[ 1 ] . clone ( ) ,
2174+ res : simplified_args[ 2 ] . clone ( ) ,
2175+ data : PrecompileCompTimeArgs :: Poseidon16 ,
2176+ } ) ) ;
21702177 continue ;
21712178 }
21722179
@@ -3962,16 +3969,7 @@ impl SimpleLine {
39623969 . join ( ", " ) ;
39633970 format ! ( "return {return_data_str}" )
39643971 }
3965- Self :: Precompile {
3966- table : precompile,
3967- args,
3968- } => {
3969- format ! (
3970- "{}({})" ,
3971- & precompile. name( ) ,
3972- args. iter( ) . map( |arg| format!( "{arg}" ) ) . collect:: <Vec <_>>( ) . join( ", " )
3973- )
3974- }
3972+ Self :: Precompile ( precompile) => format ! ( "{precompile}" ) ,
39753973 Self :: Print { line_info : _, content } => {
39763974 let content_str = content. iter ( ) . map ( |c| format ! ( "{c}" ) ) . collect :: < Vec < _ > > ( ) . join ( ", " ) ;
39773975 format ! ( "print({content_str})" )
0 commit comments