-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathLibUint1024.t.sol
437 lines (390 loc) · 11.6 KB
/
LibUint1024.t.sol
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8;
import 'forge-std/Test.sol';
import '../src/LibUint1024.sol';
contract LibUint1024Test is Test {
using LibUint1024 for *;
function testReferenceAdd(uint256[4] memory a, uint256[4] memory b)
public
noOverflow(a, b)
{
uint256[4] memory pythonResult = _decodeBigNumber(_runPythonReference('add', a, b));
assertTrue(a.add(b).eq(pythonResult));
}
function testReferenceSub(uint256[4] memory a, uint256[4] memory b)
public
{
vm.assume(a.gte(b));
uint256[4] memory pythonResult = _decodeBigNumber(_runPythonReference('sub', a, b));
assertTrue(a.sub(b).eq(pythonResult));
}
function testReferenceGte(uint256[4] memory a, uint256[4] memory b)
public
{
bool pythonResult = _decodeBool(_runPythonReference('gte', a, b));
assertEq(a.gte(b), pythonResult);
}
function testReferenceLte(uint256[4] memory a, uint256[4] memory b)
public
{
bool pythonResult = _decodeBool(_runPythonReference('lte', a, b));
assertEq(a.lte(b), pythonResult);
}
function testReferenceAddMod(
uint256[4] memory a,
uint256[4] memory b,
uint256[4] memory m
)
public
noOverflow(a, b)
{
vm.assume(a.lt(m) && b.lt(m));
uint256[4] memory pythonResult = _decodeBigNumber(_runPythonReference(
'addMod',
a,
b,
m
));
uint256[4] memory solidityResult = a.addMod(b, m);
assertTrue(solidityResult.eq(pythonResult));
}
function testReferenceSubMod(
uint256[4] memory a,
uint256[4] memory b,
uint256[4] memory m
)
public
{
vm.assume(a.lt(m) && b.lt(m));
uint256[4] memory pythonResult = _decodeBigNumber(_runPythonReference(
'subMod',
a,
b,
m
));
uint256[4] memory solidityResult = a.subMod(b, m);
assertTrue(solidityResult.eq(pythonResult));
}
function testReferenceExpMod(
uint256[4] memory a,
uint256 e,
uint256[4] memory m
)
public
{
uint256[4] memory pythonResult = _decodeBigNumber(_runPythonExpMod(
a,
e,
m
));
uint256[4] memory solidityResult = a.expMod(e, m);
assertTrue(solidityResult.eq(pythonResult));
}
function testReferenceMulMod(
uint256[4] memory a,
uint256[4] memory b,
uint256[4] memory m
)
public
noOverflow(a, b)
{
vm.assume(a.lt(m) && b.lt(m));
uint256[4] memory pythonResult = _decodeBigNumber(_runPythonMulMod(
a,
b,
m
));
uint256[4] memory solidityResult = a.mulMod(b, m);
assertTrue(solidityResult.eq(pythonResult));
}
function testMulModAlt(
uint256[4] memory a,
uint256[4] memory b,
uint256[4] memory m
)
public
noOverflow(a, b)
{
vm.assume(a.lt(m) && b.lt(m));
uint256[4] memory altResult = _mulModAlt(a, b, m);
uint256[4] memory result = a.mulMod(b, m).mulMod(4.toUint1024(), m);
assertTrue(result.eq(altResult));
}
function testGasAdd(uint256[4] memory a, uint256[4] memory b)
public
pure
noOverflow(a, b)
{
a.add(b);
}
function testGasSub(uint256[4] memory a, uint256[4] memory b)
public
pure
{
vm.assume(a.gte(b));
a.sub(b);
}
function testGasEq(uint256[4] memory a, uint256[4] memory b)
public
pure
{
a.eq(b);
}
function testGasGte(uint256[4] memory a, uint256[4] memory b)
public
pure
{
a.gte(b);
}
function testGasLte(uint256[4] memory a, uint256[4] memory b)
public
pure
{
a.lte(b);
}
function testGasAddMod(
uint256[4] memory a,
uint256[4] memory b,
uint256[4] memory m
)
public
pure
noOverflow(a, b)
{
vm.assume(a.lt(m) && b.lt(m));
a.addMod(b, m);
}
function testGasSubMod(
uint256[4] memory a,
uint256[4] memory b,
uint256[4] memory m
)
public
pure
{
vm.assume(a.lt(m) && b.lt(m));
a.subMod(b, m);
}
function testGasExpModSmall(
uint256[4] memory a,
uint256 e,
uint256[4] memory m
)
public
view
{
a.expMod(e, m);
}
function testGasExpModBig(
uint256[4] memory a,
uint256[4] memory e,
uint256[4] memory m
)
public
view
{
a.expMod(e, m);
}
function testGasMulMod(
uint256[4] memory a,
uint256[4] memory b,
uint256[4] memory m
)
public
view
noOverflow(a, b)
{
vm.assume(a.lt(m) && b.lt(m));
a.mulMod(b, m);
}
function testProveAddCommutative(uint256[4] memory a, uint256[4] memory b)
public
noOverflow(a, b)
{
uint256[4] memory sum1 = a.add(b);
uint256[4] memory sum2 = b.add(a);
assertTrue(sum1.eq(sum2));
}
function testProveAddSub(uint256[4] memory a, uint256[4] memory b)
public
noOverflow(a, b)
{
uint256[4] memory sum = a.add(b);
assertTrue(sum.sub(b).eq(a));
assertTrue(sum.sub(a).eq(b));
}
function testProveSubAdd(uint256[4] memory a, uint256[4] memory b)
public
{
vm.assume(a.gte(b));
assertTrue(a.sub(b).add(b).eq(a));
}
function testExpModSmall(uint128 a, uint128 e, uint128 m)
public
{
vm.assume(m > 0);
vm.assume(e > 0);
uint256 expectedResult = 1;
uint256 pow = uint256(a % m);
expectedResult = (e & 1 != 0) ? pow : 1;
for (uint256 i = 1; (1 << i) <= e; i++) {
pow = (pow ** 2) % m;
if (e & (1 << i) != 0) {
expectedResult = (expectedResult * pow) % m;
}
}
uint256[4] memory bigA = uint256(a).toUint1024();
uint256[4] memory bigM = m.toUint1024();
assertTrue(bigA.expMod(e, bigM).eq(expectedResult.toUint1024()));
}
function testMulModSmall(uint256 a, uint256 b, uint256 m)
public
{
vm.assume(a < m && b < m);
uint256[4] memory bigA = a.toUint1024();
uint256[4] memory bigB = b.toUint1024();
uint256[4] memory bigM = m.toUint1024();
uint256 expectedResult = mulmod(a, b, m);
assertTrue(bigA.mulMod(bigB, bigM).eq(expectedResult.toUint1024()));
}
// ================================================================
modifier noOverflow(uint256[4] memory a, uint256[4] memory b) {
unchecked {
vm.assume(a[0] + b[0] > a[0]);
vm.assume(a[0] + b[0] < type(uint256).max);
}
_;
}
// Returns 4 * a * b % modulus
function _mulModAlt(
uint256[4] memory a,
uint256[4] memory b,
uint256[4] memory modulus
)
private
view
returns (uint256[4] memory result)
{
uint256[4] memory sumSquared = a.addMod(b, modulus).expMod(2, modulus);
uint256[4] memory differenceSquared = a.subMod(b, modulus).expMod(2, modulus);
// Returns (a+b)^2 - (a-b)^2 = 4ab
return sumSquared.subMod(differenceSquared, modulus);
}
function _decodeBigNumber(bytes memory encoded)
private
pure
returns (uint256[4] memory c)
{
c = abi.decode(encoded, (uint256[4]));
return c;
}
function _decodeBool(bytes memory encoded)
private
pure
returns (bool)
{
return abi.decode(encoded, (bool));
}
function _runPythonReference(
string memory operation,
uint256[4] memory a,
uint256[4] memory b
)
private
returns (bytes memory pythonResult)
{
bytes memory packedA = abi.encodePacked(a);
bytes memory packedB = abi.encodePacked(b);
string[] memory pythonCommand = new string[](7);
pythonCommand[0] = 'python3';
pythonCommand[1] = 'test/big_math_reference.py';
pythonCommand[2] = '--operation';
pythonCommand[3] = operation;
pythonCommand[4] = '--inputs';
pythonCommand[5] = toHexString(packedA);
pythonCommand[6] = toHexString(packedB);
return vm.ffi(pythonCommand);
}
function _runPythonReference(
string memory operation,
uint256[4] memory a,
uint256[4] memory b,
uint256[4] memory c
)
private
returns (bytes memory pythonResult)
{
bytes memory packedA = abi.encodePacked(a);
bytes memory packedB = abi.encodePacked(b);
bytes memory packedC = abi.encodePacked(c);
string[] memory pythonCommand = new string[](8);
pythonCommand[0] = 'python3';
pythonCommand[1] = 'test/big_math_reference.py';
pythonCommand[2] = '--operation';
pythonCommand[3] = operation;
pythonCommand[4] = '--inputs';
pythonCommand[5] = toHexString(packedA);
pythonCommand[6] = toHexString(packedB);
pythonCommand[7] = toHexString(packedC);
return vm.ffi(pythonCommand);
}
function _runPythonExpMod(
uint256[4] memory a,
uint256 e,
uint256[4] memory m
)
private
returns (bytes memory pythonResult)
{
bytes memory packedA = abi.encodePacked(a);
bytes memory packedE = abi.encodePacked(e);
bytes memory packedM = abi.encodePacked(m);
string[] memory pythonCommand = new string[](8);
pythonCommand[0] = 'python3';
pythonCommand[1] = 'test/big_math_reference.py';
pythonCommand[2] = '--operation';
pythonCommand[3] = 'expMod';
pythonCommand[4] = '--inputs';
pythonCommand[5] = toHexString(packedA);
pythonCommand[6] = toHexString(packedE);
pythonCommand[7] = toHexString(packedM);
return vm.ffi(pythonCommand);
}
function _runPythonMulMod(
uint256[4] memory a,
uint256[4] memory b,
uint256[4] memory m
)
private
returns (bytes memory pythonResult)
{
bytes memory packedA = abi.encodePacked(a);
bytes memory packedB = abi.encodePacked(b);
bytes memory packedM = abi.encodePacked(m);
string[] memory pythonCommand = new string[](8);
pythonCommand[0] = 'python3';
pythonCommand[1] = 'test/big_math_reference.py';
pythonCommand[2] = '--operation';
pythonCommand[3] = 'mulMod';
pythonCommand[4] = '--inputs';
pythonCommand[5] = toHexString(packedA);
pythonCommand[6] = toHexString(packedB);
pythonCommand[7] = toHexString(packedM);
return vm.ffi(pythonCommand);
}
function toHexString(bytes memory input) private pure returns (string memory) {
require(input.length < type(uint256).max / 2 - 1);
bytes16 symbols = '0123456789abcdef';
bytes memory hex_buffer = new bytes(2 * input.length + 2);
hex_buffer[0] = '0';
hex_buffer[1] = 'x';
uint pos = 2;
uint256 length = input.length;
for (uint i = 0; i < length; ++i) {
uint _byte = uint8(input[i]);
hex_buffer[pos++] = symbols[_byte >> 4];
hex_buffer[pos++] = symbols[_byte & 0xf];
}
return string(hex_buffer);
}
}