forked from Consensys/anonymous-zether
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.sol
49 lines (40 loc) · 1.61 KB
/
Utils.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
pragma solidity 0.5.4;
pragma experimental ABIEncoderV2;
library Utils {
uint256 public constant GROUP_ORDER = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
uint256 constant UNITY = 9334303377689037989442018753807510978357674015322511348041267794643984346845; // primitive 2^28th root of unity modulo GROUP_ORDER (not field!)
function add(uint256 x, uint256 y) internal pure returns (uint256) {
return addmod(x, y, GROUP_ORDER);
}
function mul(uint256 x, uint256 y) internal pure returns (uint256) {
return mulmod(x, y, GROUP_ORDER);
}
function inv(uint256 x) internal view returns (uint256) {
return exp(x, GROUP_ORDER - 2);
}
function mod(uint256 x) internal pure returns (uint256) {
return x % GROUP_ORDER;
}
function sub(uint256 x, uint256 y) internal pure returns (uint256) {
return x >= y ? x - y : GROUP_ORDER - y + x;
}
function neg(uint256 x) internal pure returns (uint256) {
return GROUP_ORDER - x;
}
function exp(uint256 base, uint256 exponent) internal view returns (uint256 output) {
uint256 order = GROUP_ORDER;
assembly {
let m := mload(0x40)
mstore(m, 0x20)
mstore(add(m, 0x20), 0x20)
mstore(add(m, 0x40), 0x20)
mstore(add(m, 0x60), base)
mstore(add(m, 0x80), exponent)
mstore(add(m, 0xa0), order)
if iszero(staticcall(gas, 0x05, m, 0xc0, m, 0x20)) { // staticcall or call?
revert(0, 0)
}
output := mload(m)
}
}
}