This repository has been archived by the owner on Feb 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathArithmeticOperationsRule.py
147 lines (105 loc) · 4.02 KB
/
ArithmeticOperationsRule.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
from functools import reduce
from typing import Callable, Tuple, Union, List
from lark import Tree
from amarna.rules.GenericRule import GenericRule
from amarna.Result import create_result, getPosition
class ArithmeticOperationsRule(GenericRule):
"""
Check arithmetic operations:
- reports ALL multiplications and divisions
- reports ONLY addition and subtraction that do not involve a register like [ap - 1]
"""
RULE_TEXT = "Cairo arithmetic is defined over a finite field and has potential for overflows."
RULE_PREFIX = "arithmetic-"
PRIME = 2**251 + 17 * 2**192 + 1
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
return None
else:
return x % m
def div(x, y):
if x == None or y == None:
return None
return x * modinv(y, PRIME) % PRIME
def recursion_gather_operands(tree: Tree, numbers: List[int]):
original_operation = tree.data
for child in tree.children:
data = child.data
if data == "atom_number":
num = child.children[0]
numbers.append(int(str(num)))
elif data == "notes":
continue
elif data == original_operation:
recursion_gather_operands(child, numbers)
else:
# print("found node: ", data)
return None
return numbers
def is_potential_overflow(
tree: Tree, op: Callable[[int, int], int]
) -> Tuple[Union[int, None], bool]:
rec = recursion_gather_operands(tree, [])
if not rec:
return None, True
result = reduce(op, rec)
if not result:
return None, True
if result < 0 or result >= PRIME:
return result, True
return result, False
class MulArithmeticOperationsRule(ArithmeticOperationsRule):
RULE_NAME = ArithmeticOperationsRule.RULE_PREFIX + "mul"
def expr_mul(self, tree: Tree) -> None:
res, overflow = is_potential_overflow(tree, lambda x, y: x * y)
if not overflow:
return
text = self.RULE_TEXT
if res:
text += f" This multiplication will overflow and return {res % PRIME}."
result = create_result(self.fname, self.RULE_NAME, text, getPosition(tree))
self.results.append(result)
class DivArithmeticOperationsRule(ArithmeticOperationsRule):
RULE_NAME = ArithmeticOperationsRule.RULE_PREFIX + "div"
def expr_div(self, tree: Tree) -> None:
res, _ = is_potential_overflow(tree, div)
text = self.RULE_TEXT
if res:
text += f" This division will return {res}."
result = create_result(self.fname, self.RULE_NAME, text, getPosition(tree))
self.results.append(result)
class AddArithmeticOperationsRule(ArithmeticOperationsRule):
RULE_NAME = ArithmeticOperationsRule.RULE_PREFIX + "add"
def expr_add(self, tree: Tree) -> None:
# ignore adding to registers
if tree.children[0].data == "atom_reg":
return
res, overflow = is_potential_overflow(tree, lambda x, y: x + y)
if not overflow:
return
text = self.RULE_TEXT
if res:
text += f" This addition will overflow and return {res % PRIME}."
result = create_result(self.fname, self.RULE_NAME, text, getPosition(tree))
self.results.append(result)
class SubArithmeticOperationsRule(ArithmeticOperationsRule):
RULE_NAME = ArithmeticOperationsRule.RULE_PREFIX + "sub"
def expr_sub(self, tree: Tree) -> None:
# ignore subtracting to registers
if tree.children[0].data == "atom_reg":
return
res, overflow = is_potential_overflow(tree, lambda x, y: x - y)
if not overflow:
return
text = self.RULE_TEXT
if res:
text += f" This subtraction will overflow and return {res % PRIME}."
result = create_result(self.fname, self.RULE_NAME, text, getPosition(tree))
self.results.append(result)