-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathUtils.huff
90 lines (78 loc) · 3.51 KB
/
MathUtils.huff
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
/* Interface */
#define function subMod(uint256,uint256,uint256) pure returns (uint256)
#define function expMod(uint256,uint256,uint256) pure returns (uint256)
#define function powers_array(uint256) pure return (uint256[])
/* Methods */
#define macro SUB_MOD() = takes (0) returns (0) {
// a - b % n = a + (n-b) % n
0x44 calldataload // [number3] load third 32 bytes onto the stack - (n)
0x24 calldataload // [number2, number3] load second 32 bytes onto the stack - (n)
0x44 calldataload // [number3, number2, number3] load third 32 bytes onto the stack - (b)
sub // [number3-number2, number3]
0x04 calldataload // [number1, number3-number2,number3] load first 32 bytes onto the stack - (a)
addmod // [number1 + (number3-number2) % number3]
0x00 mstore // place [number1 + (number3-number2) % number3] in memory
0x20 0x00 return // return first 32 bytes (result)
}
#define macro EXP_MOD() = takes (0) returns (0) {
0x44 calldataload // [_modules] // load third 32 bytes onto the stack - (b)
0xa0 mstore
0x24 calldataload // [_exponent] // load second 32 bytes onto the stack - (n)
0x80 mstore
0x04 calldataload // [_base] // load first 32 bytes onto the stack - (a)
0x60 mstore
0x20 0x40 mstore
0x20 0x20 mstore
0x20 0x00 mstore
0x20 // [retSize]
0xc0 // [retOffset, retSize]
0xc0 // [argSize, retOffset, retSize]
0x00 // [argOffset, argSize, retOffset, retSize]
0x05 // [address, argOffset, argSize, retOffset, retSize]
gas // [gas, address, argOffset, argSize, retOffset, retSize]
staticcall // [success, address]
0x20 0xc0 return // return first 32 bytes (result)
}
// @todo: fix comments, fix array length, loop
#define macro POWERS_ARRAY() = takes (0) returns (0) {
0x04 calldataload // [beta]
0x20 mstore // [] memory: 0x20beta
0x01 0x00 mstore // store the counter to memory: [0x00 0x00, 0x20 beta]
loop:
0x20 mload // [beta]
0x00 mload // [counter,beta]
0x20 // [memsize,counter,beta]
mul // [memsize*counter,beta]
mload // [mload(memsize*counter), beta]
mul // [mul(mload(memsize*counter)*beta)] result
0x00 mload // counter + 1 store as counter
0x01 add
0x00 mstore
// mstore result to counter*memsize
// check if counter is > domain_size
0x00 mload // counter + 1 store as counter
0x20 mul
mstore
0x00 mload
0x40000
//swap1 //
gt //
loop // [loop]
jumpi // If the var reaches 0x40000 it won't loop any more
0x040000 0x20
return
}
#define macro MAIN() = takes (0) returns (0) {
// Identify which function is being called.
0x00 calldataload 0xE0 shr
dup1 __FUNC_SIG(subMod) eq submod jumpi
dup1 __FUNC_SIG(expMod) eq expmod jumpi
dup1 __FUNC_SIG(powers_array) eq powersArray jumpi
0x00 0x00 revert
submod:
SUB_MOD()
expmod:
EXP_MOD()
powersArray:
POWERS_ARRAY()
}