-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathIntegralMath.py
More file actions
254 lines (234 loc) · 7.83 KB
/
IntegralMath.py
File metadata and controls
254 lines (234 loc) · 7.83 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from .common.BuiltIn import *
from .common.Uint256 import *
'''
@dev Compute the largest integer smaller than or equal to the binary logarithm of `n`
'''
def floorLog2(n):
x = 0;
if (n > 0):
x = n.bit_length() - 1;
return x;
'''
@dev Compute the largest integer smaller than or equal to the square root of `n`
'''
def floorSqrt(n):
if (n > 63):
x = n;
y = 1 << (floorLog2(n) // 2 + 1);
while (x > y):
x = y;
y = (x + n // x) >> 1;
return x;
return 0x7777777777777776666666666666555555555554444444443333333222221110 >> (n * 4) & 0xf;
'''
@dev Compute the smallest integer larger than or equal to the square root of `n`
'''
def ceilSqrt(n):
x = floorSqrt(n);
return x if x ** 2 == n else x + 1;
'''
@dev Compute the largest integer smaller than or equal to the cubic root of `n`
'''
def floorCbrt(n):
if (n > 84):
x = n;
y = 1 << (floorLog2(n) // 3 + 1);
while (x > y):
x = y;
y = ((x << 1) + n // x ** 2) // 3;
return x;
return 0x49249249249249246db6db6db6db6db6db6db6db6db692492492492492249248 >> (n * 3) & 0x7;
'''
@dev Compute the smallest integer larger than or equal to the cubic root of `n`
'''
def ceilCbrt(n):
x = floorCbrt(n);
return x if x ** 3 == n else x + 1;
'''
@dev Compute the nearest integer (half being rounded upwards) to `n / d`
'''
def roundDiv(n, d):
return n // d + (n % d) // (d - d // 2);
'''
@dev Compute the smallest integer `z` such that `x * y / z <= 2 ^ 256 - 1`
'''
def minFactor(x, y):
(hi, lo) = mul512(x, y);
return hi + 2 if hi > bitwiseNot(lo) else hi + 1;
# General:
# - find the smallest integer `z` such that `x * y / z <= 2 ^ 256 - 1`
# - the value of `x * y` is represented via `2 ^ 256 * hi + lo`
# - the expression `~lo` is equivalent to `2 ^ 256 - 1 - lo`
#
# Safety:
# - if `x < 2 ^ 256 - 1` or `y < 2 ^ 256 - 1`
# then `hi < 2 ^ 256 - 2`
# hence neither `hi + 1` nor `hi + 2` overflows
# - if `x = 2 ^ 256 - 1` and `y = 2 ^ 256 - 1`
# then `hi = 2 ^ 256 - 2 = ~lo`
# hence `hi + 1`, which does not overflow, is computed
#
# Symbols:
# - let `H` denote `hi`
# - let `L` denote `lo`
# - let `N` denote `2 ^ 256 - 1`
#
# Inference:
# `x * y / z <= 2 ^ 256 - 1` <-->
# `x * y / (2 ^ 256 - 1) <= z` <-->
# `((N + 1) * H + L) / N <= z` <-->
# `(N * H + H + L) / N <= z` <-->
# `H + (H + L) / N <= z`
#
# Inference:
# `0 <= H <= N && 0 <= L <= N` <-->
# `0 <= H + L <= N + N` <-->
# `0 <= H + L <= N * 2` <-->
# `0 <= (H + L) / N <= 2`
#
# Inference:
# - `0 = (H + L) / N` --> `H + L = 0` --> `x * y = 0` --> `z = 1 = H + 1`
# - `0 < (H + L) / N <= 1` --> `H + (H + L) / N <= H + 1` --> `z = H + 1`
# - `1 < (H + L) / N <= 2` --> `H + (H + L) / N <= H + 2` --> `z = H + 2`
#
# Implementation:
# - if `hi > ~lo`:
# `~L < H <= N` <-->
# `N - L < H <= N` <-->
# `N < H + L <= N + L` <-->
# `1 < (H + L) / N <= 2` <-->
# `H + 1 < H + (H + L) / N <= H + 2` <-->
# `z = H + 2`
# - if `hi <= ~lo`:
# `H <= ~L` <-->
# `H <= N - L` <-->
# `H + L <= N` <-->
# `(H + L) / N <= 1` <-->
# `H + (H + L) / N <= H + 1` <-->
# `z = H + 1`
'''
@dev Compute the largest integer smaller than or equal to `x * y / 2 ^ s`
'''
def mulShrF(x, y, s):
(xyh, xyl) = mul512(x, y);
require(xyh < 1 << s, "Overflow()");
return (xyh << (256 - s)) | (xyl >> s);
'''
@dev Compute the smallest integer larger than or equal to `x * y / 2 ^ s`
'''
def mulShrC(x, y, s):
w = mulShrF(x, y, s);
if (mulmod(x, y, 1 << s) > 0):
return inc256(w);
return w;
'''
@dev Compute the largest integer smaller than or equal to `x * y / z`
'''
def mulDivF(x, y, z):
(xyh, xyl) = mul512(x, y);
if (xyh == 0): # `x * y < 2 ^ 256`
return xyl // z;
if (xyh < z): # `x * y / z < 2 ^ 256`
m = mulmod(x, y, z); # `m = x * y % z`
(nh, nl) = sub512(xyh, xyl, m); # `n = x * y - m` hence `n / z = floor(x * y / z)`
if (nh == 0): # `n < 2 ^ 256`
return nl // z;
p = unsafeSub(0, z) & z; # `p` is the largest power of 2 which `z` is divisible by
q = div512(nh, nl, p); # `n` is divisible by `p` because `n` is divisible by `z` and `z` is divisible by `p`
r = inv256(z // p); # `z / p = 1 mod 2` hence `inverse(z / p) = 1 mod 2 ^ 256`
return unsafeMul(q, r); # `q * r = (n / p) * inverse(z / p) = n / z`
revert("Overflow()"); # `x * y / z >= 2 ^ 256`
'''
@dev Compute the smallest integer larger than or equal to `x * y / z`
'''
def mulDivC(x, y, z):
w = mulDivF(x, y, z);
if (mulmod(x, y, z) > 0):
return inc256(w);
return w;
'''
@dev Compute the nearest integer (half being rounded upwards) to `x * y / z`
'''
def mulDivR(x, y, z):
w = mulDivF(x, y, z);
if (mulmod(x, y, z) > (z - 1) // 2):
return inc256(w);
return w;
'''
@dev Compute the largest integer smaller than or equal to `(x * y) / (z * w)`
'''
def mulDivExF(x, y, z, w):
(zwh, zwl) = mul512(z, w);
if (zwh > 0):
res = 0;
(xyh, xyl) = mul512(x, y);
if (xyh > zwh):
xyhn = floorLog2(xyh);
zwhn = floorLog2(zwh);
while (xyhn > zwhn):
n = xyhn - zwhn - 1;
zwhshl = shiftLeft(zwh, n);
zwlshl = shiftLeft(zwl, n);
zwlshr = zwl >> (256 - n);
res += 1 << n; # set `res = res + 2 ^ n`
(xyh, xyl) = sub512Ex(xyh, xyl, zwhshl | zwlshr, zwlshl); # set `xy = xy - zw * 2 ^ n`
xyhn = floorLog2(xyh);
if (xyh > zwh or (xyh == zwh and xyl >= zwl)): # `xy >= zw`
return res + 1;
return res;
return mulDivF(x, y, zwl);
'''
@dev Compute the smallest integer larger than or equal to `(x * y) / (z * w)`
'''
def mulDivExC(x, y, z, w):
v = mulDivExF(x, y, z, w);
(xyh, xyl) = mul512(x, y);
(zwh, zwl) = mul512(z, w);
(vzwlh, vzwll) = mul512(v, zwl);
if (xyh == v * zwh + vzwlh and xyl == vzwll):
return v;
return inc256(v);
'''
@dev Compute the value of `x + 1`
'''
def inc256(x):
require(x < uint256.max, "Overflow()");
return x + 1;
'''
@dev Compute the value of `x * y`
'''
def mul512(x, y):
p = mulmod(x, y, uint256.max);
q = unsafeMul(x, y);
if (p >= q):
return (p - q, q);
return (unsafeSub(p, q) - 1, q);
'''
@dev Compute the value of `2 ^ 256 * xh + xl - y`, where `2 ^ 256 * xh + xl >= y`
'''
def sub512(xh, xl, y):
if (xl >= y):
return (xh, xl - y);
return (xh - 1, unsafeSub(xl, y));
'''
@dev Compute the value of `(2 ^ 256 * xh + xl) / pow2n`, where `xl` is divisible by `pow2n`
'''
def div512(xh, xl, pow2n):
pow2nInv = unsafeAdd(unsafeSub(0, pow2n) // pow2n, 1); # `1 << (256 - n)`
return unsafeMul(xh, pow2nInv) | (xl // pow2n); # `(xh << (256 - n)) | (xl >> n)`
'''
@dev Compute the inverse of `d` modulo `2 ^ 256`, where `d` is congruent to `1` modulo `2`
'''
def inv256(d):
# approximate the root of `f(x) = 1 / x - d` using the newton–raphson convergence method
x = 1;
for i in range(8):
x = unsafeMul(x, unsafeSub(2, unsafeMul(x, d))); # `x = x * (2 - x * d) mod 2 ^ 256`
return x;
'''
@dev Compute the value of `(2 ^ 256 * xh + xl) - (2 ^ 256 * yh + yl)`, where `2 ^ 256 * xh + xl >= 2 ^ 256 * yh + yl`
'''
def sub512Ex(xh, xl, yh, yl):
if (xl >= yl):
return (xh - yh, xl - yl);
return (xh - yh - 1, unsafeSub(xl, yl));