-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigint.py
100 lines (74 loc) · 1.62 KB
/
bigint.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
# Python 3.10 decided big integers are a denial of service attack vector
import sys
sys.set_int_max_str_digits(0)
def op_add(x, y):
return x + y
def op_sub(x, y):
return x - y
def op_mul(x, y):
return x * y
def op_div(x, y):
# Python rounds towards -inf instead of towards zero
# Not immediately convinced by that so round towards zero instead
if (x < 0) != (y < 0):
return op_neg(op_div(op_abs(x), op_abs(y)))
return x // y
def op_rem(x, y):
return x % y
def op_ash(x, y):
return x >> y
def op_lsh(x, y):
return x << y
def op_rsh(x, y):
# todo, work out how to represent this in python
return 0
def op_not(x):
# This may have printing challenges
return ~x
def op_and(x, y):
return x & y
def op_or(x, y):
return x | y
def op_xor(x, y):
return x ^ y
def op_incr(x):
return x + 1
def op_decr(x):
return x - 1
def op_abs(x):
if x >= 0:
return x
else:
return -1 * x
def op_neg(x):
return -1 * x
def op_cmp(x, y):
if x < y:
return -1
if y < x:
return +1
return 0
def op_equal(x, y):
return x == y
opmap = {
'add' : op_add,
'sub' : op_sub,
'mul' : op_mul,
'div' : op_div,
'rem' : op_rem,
'lsh' : op_lsh,
'shift_left' : op_lsh,
'rsh' : op_rsh,
'shift_right' : op_rsh,
'ash' : op_ash,
'bitwise_not' : op_not,
'bitwise_or' : op_or,
'bitwise_and' : op_and,
'bitwise_xor' : op_xor,
'incr' : op_incr,
'decr' : op_decr,
'absolute' : op_abs,
'negate' : op_neg,
'cmp' : op_cmp,
'equal' : op_equal,
}