1+ mod operation;
2+ mod statement;
3+
14use crate :: middleware:: {
25 self , hash_str, AnchoredKey , Hash , MainPodInputs , NativeOperation , NativeStatement , NonePod ,
36 Params , Pod , PodId , PodProver , StatementArg , ToFields , KEY_TYPE , SELF ,
47} ;
5- use anyhow:: { anyhow , Result } ;
8+ use anyhow:: Result ;
69use itertools:: Itertools ;
10+ pub use operation:: * ;
711use plonky2:: hash:: poseidon:: PoseidonHash ;
812use plonky2:: plonk:: config:: Hasher ;
13+ pub use statement:: * ;
914use std:: any:: Any ;
1015use std:: fmt;
1116
@@ -19,168 +24,6 @@ impl PodProver for MockProver {
1924 }
2025}
2126
22- #[ derive( Clone , Debug , PartialEq , Eq ) ]
23- enum OperationArg {
24- None ,
25- Index ( usize ) ,
26- }
27-
28- impl OperationArg {
29- fn is_none ( & self ) -> bool {
30- matches ! ( self , OperationArg :: None )
31- }
32- }
33-
34- #[ derive( Clone , Debug , PartialEq , Eq ) ]
35- enum OperationArgError {
36- KeyNotFound ,
37- StatementNotFound ,
38- }
39-
40- impl std:: fmt:: Display for OperationArgError {
41- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
42- match self {
43- OperationArgError :: KeyNotFound => write ! ( f, "Key not found" ) ,
44- OperationArgError :: StatementNotFound => write ! ( f, "Statement not found" ) ,
45- }
46- }
47- }
48-
49- impl std:: error:: Error for OperationArgError { }
50-
51- #[ derive( Clone , Debug , PartialEq , Eq ) ]
52- struct Operation ( pub NativeOperation , pub Vec < OperationArg > ) ;
53-
54- impl Operation {
55- pub fn deref ( & self , statements : & [ Statement ] ) -> Result < crate :: middleware:: Operation > {
56- let deref_args = self
57- . 1
58- . iter ( )
59- . flat_map ( |arg| match arg {
60- OperationArg :: None => None ,
61- OperationArg :: Index ( i) => Some ( statements[ * i] . clone ( ) . try_into ( ) ) ,
62- } )
63- . collect :: < Result < Vec < crate :: middleware:: Statement > > > ( ) ?;
64- middleware:: Operation :: op ( self . 0 , & deref_args)
65- }
66- }
67-
68- #[ derive( Clone , Debug , PartialEq , Eq ) ]
69- pub struct Statement ( pub NativeStatement , pub Vec < StatementArg > ) ;
70-
71- impl Statement {
72- pub fn is_none ( & self ) -> bool {
73- self . 0 == NativeStatement :: None
74- }
75- /// Argument method. Trailing Nones are filtered out.
76- pub fn args ( & self ) -> Vec < StatementArg > {
77- let maybe_last_arg_index = ( 0 ..self . 1 . len ( ) ) . rev ( ) . find ( |i| !self . 1 [ * i] . is_none ( ) ) ;
78- match maybe_last_arg_index {
79- None => vec ! [ ] ,
80- Some ( i) => self . 1 [ 0 ..i + 1 ] . to_vec ( ) ,
81- }
82- }
83- }
84-
85- impl ToFields for Statement {
86- fn to_fields ( self ) -> ( Vec < middleware:: F > , usize ) {
87- let ( native_statement_f, native_statement_f_len) = self . 0 . to_fields ( ) ;
88- let ( vec_statementarg_f, vec_statementarg_f_len) = self
89- . 1
90- . into_iter ( )
91- . map ( |statement_arg| statement_arg. to_fields ( ) )
92- . fold ( ( Vec :: new ( ) , 0 ) , |mut acc, ( f, l) | {
93- acc. 0 . extend ( f) ;
94- acc. 1 += l;
95- acc
96- } ) ;
97- (
98- [ native_statement_f, vec_statementarg_f] . concat ( ) ,
99- native_statement_f_len + vec_statementarg_f_len,
100- )
101- }
102- }
103-
104- impl TryFrom < Statement > for middleware:: Statement {
105- type Error = anyhow:: Error ;
106- fn try_from ( s : Statement ) -> Result < Self > {
107- type S = middleware:: Statement ;
108- type NS = NativeStatement ;
109- type SA = StatementArg ;
110- let proper_args = s. args ( ) ;
111- let args = (
112- proper_args. get ( 0 ) . cloned ( ) ,
113- proper_args. get ( 1 ) . cloned ( ) ,
114- proper_args. get ( 2 ) . cloned ( ) ,
115- ) ;
116- Ok ( match ( s. 0 , args, proper_args. len ( ) ) {
117- ( NS :: None , _, 0 ) => S :: None ,
118- ( NS :: ValueOf , ( Some ( SA :: Key ( ak) ) , Some ( SA :: Literal ( v) ) , None ) , 2 ) => S :: ValueOf ( ak, v) ,
119- ( NS :: Equal , ( Some ( SA :: Key ( ak1) ) , Some ( SA :: Key ( ak2) ) , None ) , 2 ) => S :: Equal ( ak1, ak2) ,
120- ( NS :: NotEqual , ( Some ( SA :: Key ( ak1) ) , Some ( SA :: Key ( ak2) ) , None ) , 2 ) => {
121- S :: NotEqual ( ak1, ak2)
122- }
123- ( NS :: Gt , ( Some ( SA :: Key ( ak1) ) , Some ( SA :: Key ( ak2) ) , None ) , 2 ) => S :: Gt ( ak1, ak2) ,
124- ( NS :: Lt , ( Some ( SA :: Key ( ak1) ) , Some ( SA :: Key ( ak2) ) , None ) , 2 ) => S :: Lt ( ak1, ak2) ,
125- ( NS :: Contains , ( Some ( SA :: Key ( ak1) ) , Some ( SA :: Key ( ak2) ) , None ) , 2 ) => {
126- S :: Contains ( ak1, ak2)
127- }
128- ( NS :: NotContains , ( Some ( SA :: Key ( ak1) ) , Some ( SA :: Key ( ak2) ) , None ) , 2 ) => {
129- S :: NotContains ( ak1, ak2)
130- }
131- ( NS :: SumOf , ( Some ( SA :: Key ( ak1) ) , Some ( SA :: Key ( ak2) ) , Some ( SA :: Key ( ak3) ) ) , 3 ) => {
132- S :: SumOf ( ak1, ak2, ak3)
133- }
134- ( NS :: ProductOf , ( Some ( SA :: Key ( ak1) ) , Some ( SA :: Key ( ak2) ) , Some ( SA :: Key ( ak3) ) ) , 3 ) => {
135- S :: ProductOf ( ak1, ak2, ak3)
136- }
137- ( NS :: MaxOf , ( Some ( SA :: Key ( ak1) ) , Some ( SA :: Key ( ak2) ) , Some ( SA :: Key ( ak3) ) ) , 3 ) => {
138- S :: MaxOf ( ak1, ak2, ak3)
139- }
140- _ => Err ( anyhow ! ( "Ill-formed statement expression {:?}" , s) ) ?,
141- } )
142- }
143- }
144-
145- impl From < middleware:: Statement > for Statement {
146- fn from ( s : middleware:: Statement ) -> Self {
147- Statement ( s. code ( ) , s. args ( ) . into_iter ( ) . map ( |arg| arg) . collect ( ) )
148- }
149- }
150-
151- impl fmt:: Display for Statement {
152- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
153- write ! ( f, "{:?} " , self . 0 ) ?;
154- for ( i, arg) in self . 1 . iter ( ) . enumerate ( ) {
155- if !( !f. alternate ( ) && arg. is_none ( ) ) {
156- if i != 0 {
157- write ! ( f, " " ) ?;
158- }
159- write ! ( f, "{}" , arg) ?;
160- }
161- }
162- Ok ( ( ) )
163- }
164- }
165-
166- impl fmt:: Display for Operation {
167- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
168- write ! ( f, "{:?} " , self . 0 ) ?;
169- for ( i, arg) in self . 1 . iter ( ) . enumerate ( ) {
170- if !( !f. alternate ( ) && arg. is_none ( ) ) {
171- if i != 0 {
172- write ! ( f, " " ) ?;
173- }
174- match arg {
175- OperationArg :: None => write ! ( f, "none" ) ?,
176- OperationArg :: Index ( i) => write ! ( f, "{:02}" , i) ?,
177- }
178- }
179- }
180- Ok ( ( ) )
181- }
182- }
183-
18427#[ derive( Clone , Debug ) ]
18528pub struct MockMainPod {
18629 params : Params ,
0 commit comments