-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
224 lines (184 loc) · 7.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from starkware.cairo.lang.compiler.parser import parse_file
from starkware.cairo.lang.compiler.ast.code_elements import *
from starkware.cairo.lang.compiler.ast.expr import *
from starkware.cairo.lang.compiler.ast.expr_func_call import *
import requests
global array_read_offset
global constants
functions = {
'eval_composition_polynomial': """\
fn eval_composition_polynomial_inner(
mut mask_values: Span<felt252>,
mut constraint_coefficients: Span<felt252>,
point: felt252,
trace_generator: felt252,
global_values: GlobalValues,
) -> felt252""",
'eval_oods_polynomial': """\
fn eval_oods_polynomial_inner(
mut column_values: Span<felt252>,
mut oods_values: Span<felt252>,
mut constraint_coefficients: Span<felt252>,
point: felt252,
oods_point: felt252,
trace_generator: felt252,
) -> felt252""",
}
def eval(node: AstNode) -> int:
match node:
case ExprOperator(a=a, b=b, op='+'):
return eval(a) + eval(b)
case ExprIdentifier(name=name) if name in constants:
return constants[name]
case ExprConst(val=val):
return val
print(node.__class__.__name__, 'not implemented in eval')
print(node, "\n")
return 0
def rename_var(name: str) -> str:
return name.replace("__", "_")
def parse(node: AstNode, comment: str = '') -> str:
global array_read_offset
match node:
case CodeBlock(code_elements=code_elements):
acc = ''
for ce in code_elements:
acc += parse(ce)
return acc
case CodeElementAllocLocals(): # alloc_locals
return ''
case CodeElementLocalVariable( # local x
typed_identifier=TypedIdentifier(
identifier=ExprIdentifier(name=name)
),
expr=expr
) | CodeElementUnpackBinding( # let (local x)
unpacking_list=IdentifierList(
identifiers=[
TypedIdentifier(identifier=ExprIdentifier(name=name))
]
),
rvalue=expr
) | CodeElementTemporaryVariable( # tempvar x
typed_identifier=TypedIdentifier(
identifier=ExprIdentifier(name=name)
),
expr=expr
):
com = '' if comment is None else (' //' + comment)
return f"let {name} = {parse(expr)};{com}\n\t"
case RvalueFuncCall( # safe_div(x, y)
func_ident=ExprIdentifier(name='safe_div'),
arguments=ArgList(args=[
lv,
rv
])
):
# TODO: should this be safe_div?
return f"{parse(lv)} / {parse(rv)}"
case RvalueFuncCall( # safe_mult(x,y)
func_ident=ExprIdentifier(name='safe_mult'),
arguments=ArgList(args=[
lv,
rv
])
):
# TODO: should this be safe_mult?
return f"{parse(lv)} * {parse(rv)}"
case RvalueFuncCall( # f(x, y, ...)
func_ident=ExprIdentifier(name=name),
arguments=ArgList(args=args),
):
def remove_parenthesis(arg):
match arg:
case ExprAssignment(expr=ExprParentheses(val=val)):
return val
return arg
return f"{name}({', '.join([parse(remove_parenthesis(arg)) for arg in args])})"
case ExprOperator(a=a, b=b, op=op):
return f"{parse(a)} {op} {parse(b)}"
case ExprSubscript( # x[0]
expr=ExprIdentifier(name=name),
offset=ExprConst(val=val)
) if val == array_read_offset.get(name, 0):
array_read_offset[name] = array_read_offset.get(name, 0) + 1
return f"*{name}.pop_front().unwrap()"
case ExprSubscript( # x[CONST_VAR]
expr=ExprIdentifier(name=name),
offset=offset
):
evaluated_offset = eval(offset)
curr = array_read_offset.get(name, 0)
if curr != evaluated_offset:
print(f"Array read not subsequent. Expected {curr}, actual {evaluated_offset}")
else:
array_read_offset[name] = curr + 1
return f"*{name}.pop_front().unwrap()"
case CodeElementStaticAssert(a=a, b=b): # static assert x == y
return f"assert({parse(a)} == {parse(b)}, 'Assert failed');\n\t"
case CodeElementReturn( # return (res=x)
expr=ExprTuple(
members=ArgList(
args=[
ExprAssignment(
identifier=ExprIdentifier(name='res'),
expr=ExprIdentifier(name=var),
)
]
)
)
):
return f"return {var};\n"
case ExprParentheses(val=val): # (x)
return f"({parse(val)})"
case ExprIdentifier(name=name): # x
return rename_var(name)
case ExprConst(format_str=format_str):
return format_str
case ExprAssignment(expr=expr):
return parse(expr)
case ExprFuncCall(rvalue=rvalue):
return parse(rvalue)
case CommentedCodeElement(code_elm=code_elm, comment=comment):
return parse(code_elm, comment)
case CodeElementEmptyLine():
if comment is None:
return '\n\t'
return '//' + comment + '\n\t'
print(node.__class__.__name__, 'not implemented')
print(node, "\n")
return ''
def handle_github_file(url, output_file):
global array_read_offset
response = requests.get(url)
if response.status_code != 200:
raise Exception(f"Failed to fetch {url}")
ast = parse_file(response.text, filename='autogenerated.cairo')
global constants
constants = {}
functions_result = {}
for commented_code_element in ast.code_block.code_elements:
match commented_code_element.code_elm:
case CodeElementFunction(
element_type='func',
identifier=ExprIdentifier(name=name),
code_block=code_block
) if name in functions:
array_read_offset = {}
parsed = parse(code_block)
if name in functions_result:
raise Exception(name + ' defined multiple times')
functions_result[name] = functions[name] + ' {' + parsed + "}\n"
case CodeElementConst(identifier=ExprIdentifier(name=name), expr=expr):
constants[name] = eval(expr)
with open(output_file, 'w') as f:
f.write('\n'.join(functions_result.values()))
def main():
layouts = ('recursive', 'recursive_with_poseidon', 'small', 'dex', 'starknet', 'starknet_with_keccak')
for layout in layouts:
handle_github_file(
f"https://raw.githubusercontent.com/starkware-libs/cairo-lang/master/src/starkware/cairo/stark_verifier/air/layouts/{layout}/autogenerated.cairo",
f"output/{layout}.cairo"
)
if __name__ == '__main__':
main()