File size: 12,746 Bytes
f998fcd |
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 |
// This contract is part of Zellic’s smart contract dataset, which is a collection of publicly available contract code gathered as of March 2023.
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.17;
uint256 constant n = 4;
uint256 constant N = 1 << n;
uint256 constant m = 5;
uint256 constant M = 1 << m;
library Utils {
uint256 constant GROUP_ORDER = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001;
uint256 constant FIELD_ORDER = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47;
uint256 constant PPLUS1DIV4 = 0x0c19139cb84c680a6e14116da060561765e05aa45a1c72a34f082305b61f3f52;
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 location := mload(0x40)
mstore(location, 0x20)
mstore(add(location, 0x20), 0x20)
mstore(add(location, 0x40), 0x20)
mstore(add(location, 0x60), base)
mstore(add(location, 0x80), exponent)
mstore(add(location, 0xa0), order)
if iszero(staticcall(gas(), 0x05, location, 0xc0, location, 0x20)) {
revert(0, 0)
}
output := mload(location)
}
}
function fieldExp(uint256 base, uint256 exponent) internal view returns (uint256 output) { // warning: mod p, not q
uint256 order = FIELD_ORDER;
assembly {
let location := mload(0x40)
mstore(location, 0x20)
mstore(add(location, 0x20), 0x20)
mstore(add(location, 0x40), 0x20)
mstore(add(location, 0x60), base)
mstore(add(location, 0x80), exponent)
mstore(add(location, 0xa0), order)
if iszero(staticcall(gas(), 0x05, location, 0xc0, location, 0x20)) {
revert(0, 0)
}
output := mload(location)
}
}
struct Point {
bytes32 x;
bytes32 y;
}
function add(Point memory p1, Point memory p2) internal view returns (Point memory r) {
assembly {
let location := mload(0x40)
mstore(location, mload(p1))
mstore(add(location, 0x20), mload(add(p1, 0x20)))
mstore(add(location, 0x40), mload(p2))
mstore(add(location, 0x60), mload(add(p2, 0x20)))
if iszero(staticcall(gas(), 0x06, location, 0x80, r, 0x40)) {
revert(0, 0)
}
}
}
function mul(Point memory p, uint256 s) internal view returns (Point memory r) {
assembly {
let location := mload(0x40)
mstore(location, mload(p))
mstore(add(location, 0x20), mload(add(p, 0x20)))
mstore(add(location, 0x40), s)
if iszero(staticcall(gas(), 0x07, location, 0x60, r, 0x40)) {
revert(0, 0)
}
}
}
function neg(Point memory p) internal pure returns (Point memory) {
return Point(p.x, bytes32(FIELD_ORDER - uint256(p.y))); // p.y should already be reduced mod P?
}
function eq(Point memory p1, Point memory p2) internal pure returns (bool) {
return p1.x == p2.x && p1.y == p2.y;
}
function decompress(bytes32 input) internal view returns (Point memory) {
if (input == 0x00) return Point(0x00, 0x00);
uint256 x = uint256(input);
uint256 sign = (x & 0x8000000000000000000000000000000000000000000000000000000000000000) >> 255;
x &= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
uint256 ySquared = fieldExp(x, 3) + 3;
uint256 y = fieldExp(ySquared, PPLUS1DIV4);
Point memory result = Point(bytes32(x), bytes32(y));
if (sign != y & 0x01) return neg(result);
return result;
}
function compress(Point memory input) internal pure returns (bytes32) {
uint256 result = uint256(input.x);
if (uint256(input.y) & 0x01 == 0x01) result |= 0x8000000000000000000000000000000000000000000000000000000000000000;
return bytes32(result);
}
function mapInto(uint256 seed) internal view returns (Point memory) {
uint256 y;
while (true) {
uint256 ySquared = fieldExp(seed, 3) + 3; // addmod instead of add: waste of gas, plus function overhead cost
y = fieldExp(ySquared, PPLUS1DIV4);
if (fieldExp(y, 2) == ySquared) {
break;
}
seed += 1;
}
return Point(bytes32(seed), bytes32(y));
}
function mapInto(string memory input) internal view returns (Point memory) {
return mapInto(uint256(keccak256(abi.encodePacked(input))) % FIELD_ORDER);
}
function mapInto(string memory input, uint256 i) internal view returns (Point memory) {
return mapInto(uint256(keccak256(abi.encodePacked(input, i))) % FIELD_ORDER);
}
function slice(bytes memory input, uint256 start) internal pure returns (bytes32 result) {
assembly {
result := mload(add(add(input, 0x20), start))
}
}
struct Statement {
Point[N] Y;
Point[N] CLn;
Point[N] CRn;
Point[N] C;
Point D;
uint256 epoch;
Point u;
uint256 fee;
}
struct DepositProof {
Point A;
Point B;
Point[n] C_XG;
Point[n] y_XG;
uint256[n] f;
uint256 z_A;
uint256 c;
uint256 s_r;
}
function deserializeDeposit(bytes memory arr) internal view returns (DepositProof memory proof) {
proof.A = decompress(slice(arr, 0));
proof.B = decompress(slice(arr, 32));
for (uint256 k = 0; k < n; k++) {
proof.C_XG[k] = decompress(slice(arr, 64 + k * 32));
proof.y_XG[k] = decompress(slice(arr, 64 + (k + n) * 32));
proof.f[k] = uint256(slice(arr, 64 + n * 64 + k * 32));
}
uint256 starting = n * 96;
proof.z_A = uint256(slice(arr, 64 + starting));
proof.c = uint256(slice(arr, 96 + starting));
proof.s_r = uint256(slice(arr, 128 + starting));
return proof;
}
struct TransferProof {
Point BA;
Point BS;
Point A;
Point B;
Point[n] CLnG;
Point[n] CRnG;
Point[n] C_0G;
Point[n] DG;
Point[n] y_0G;
Point[n] gG;
Point[n] C_XG;
Point[n] y_XG;
uint256[n][2] f;
uint256 z_A;
Point T_1;
Point T_2;
uint256 tHat;
uint256 mu;
uint256 c;
uint256 s_sk;
uint256 s_r;
uint256 s_b;
uint256 s_tau;
InnerProductProof ip;
}
function deserializeTransfer(bytes memory arr) internal view returns (TransferProof memory proof) {
proof.BA = decompress(slice(arr, 0));
proof.BS = decompress(slice(arr, 32));
proof.A = decompress(slice(arr, 64));
proof.B = decompress(slice(arr, 96));
for (uint256 k = 0; k < n; k++) {
proof.CLnG[k] = decompress(slice(arr, 128 + k * 32));
proof.CRnG[k] = decompress(slice(arr, 128 + (k + n) * 32));
proof.C_0G[k] = decompress(slice(arr, 128 + n * 64 + k * 32));
proof.DG[k] = decompress(slice(arr, 128 + n * 96 + k * 32));
proof.y_0G[k] = decompress(slice(arr, 128 + n * 128 + k * 32));
proof.gG[k] = decompress(slice(arr, 128 + n * 160 + k * 32));
proof.C_XG[k] = decompress(slice(arr, 128 + n * 192 + k * 32));
proof.y_XG[k] = decompress(slice(arr, 128 + n * 224 + k * 32));
proof.f[0][k] = uint256(slice(arr, 128 + n * 256 + k * 32));
proof.f[1][k] = uint256(slice(arr, 128 + n * 288 + k * 32));
}
uint256 starting = n * 320;
proof.z_A = uint256(slice(arr, 128 + starting));
proof.T_1 = decompress(slice(arr, 160 + starting));
proof.T_2 = decompress(slice(arr, 192 + starting));
proof.tHat = uint256(slice(arr, 224 + starting));
proof.mu = uint256(slice(arr, 256 + starting));
proof.c = uint256(slice(arr, 288 + starting));
proof.s_sk = uint256(slice(arr, 320 + starting));
proof.s_r = uint256(slice(arr, 352 + starting));
proof.s_b = uint256(slice(arr, 384 + starting));
proof.s_tau = uint256(slice(arr, 416 + starting));
for (uint256 i = 0; i < m + 1; i++) {
proof.ip.L[i] = decompress(slice(arr, 448 + starting + i * 32));
proof.ip.R[i] = decompress(slice(arr, 448 + starting + (i + m + 1) * 32));
}
proof.ip.a = uint256(slice(arr, 448 + starting + (m + 1) * 64));
proof.ip.b = uint256(slice(arr, 480 + starting + (m + 1) * 64));
return proof;
}
struct WithdrawalProof {
Point BA;
Point BS;
Point A;
Point B;
Point[n] CLnG;
Point[n] CRnG;
Point[n] y_0G;
Point[n] gG;
Point[n] C_XG;
Point[n] y_XG;
uint256[n] f;
uint256 z_A;
Point T_1;
Point T_2;
uint256 tHat;
uint256 mu;
uint256 c;
uint256 s_sk;
uint256 s_r;
uint256 s_b;
uint256 s_tau;
InnerProductProof ip;
}
function deserializeWithdrawal(bytes memory arr) internal view returns (WithdrawalProof memory proof) {
proof.BA = decompress(slice(arr, 0));
proof.BS = decompress(slice(arr, 32));
proof.A = decompress(slice(arr, 64));
proof.B = decompress(slice(arr, 96));
for (uint256 k = 0; k < n; k++) {
proof.CLnG[k] = decompress(slice(arr, 128 + k * 32));
proof.CRnG[k] = decompress(slice(arr, 128 + (k + n) * 32));
proof.y_0G[k] = decompress(slice(arr, 128 + n * 64 + k * 32));
proof.gG[k] = decompress(slice(arr, 128 + n * 96 + k * 32));
proof.C_XG[k] = decompress(slice(arr, 128 + n * 128 + k * 32));
proof.y_XG[k] = decompress(slice(arr, 128 + n * 160 + k * 32));
proof.f[k] = uint256(slice(arr, 128 + n * 192 + k * 32));
}
uint256 starting = n * 224;
proof.z_A = uint256(slice(arr, 128 + starting));
proof.T_1 = decompress(slice(arr, 160 + starting));
proof.T_2 = decompress(slice(arr, 192 + starting));
proof.tHat = uint256(slice(arr, 224 + starting));
proof.mu = uint256(slice(arr, 256 + starting));
proof.c = uint256(slice(arr, 288 + starting));
proof.s_sk = uint256(slice(arr, 320 + starting));
proof.s_r = uint256(slice(arr, 352 + starting));
proof.s_b = uint256(slice(arr, 384 + starting));
proof.s_tau = uint256(slice(arr, 416 + starting));
for (uint256 i = 0; i < m; i++) { // will leave the `m`th element empty
proof.ip.L[i] = decompress(slice(arr, 448 + starting + i * 32));
proof.ip.R[i] = decompress(slice(arr, 448 + starting + (i + m) * 32));
}
proof.ip.a = uint256(slice(arr, 448 + starting + m * 64));
proof.ip.b = uint256(slice(arr, 480 + starting + m * 64));
return proof;
}
struct InnerProductStatement {
uint256 salt;
Point[M << 1] hs; // "overridden" parameters.
Point u;
Point P;
}
struct InnerProductProof {
Point[m + 1] L;
Point[m + 1] R;
uint256 a;
uint256 b;
}
function assemblePolynomials(uint256[n][2] memory f) internal pure returns (uint256[N] memory result) {
// f is a 2m-by-2 array... containing the f's and x - f's, twice (i.e., concatenated).
// output contains two "rows", each of length N.
result[0] = 1;
for (uint256 k = 0; k < n; k++) {
for (uint256 i = 0; i < N; i += 1 << n - k) {
result[i + (1 << n - 1 - k)] = mul(result[i], f[1][n - 1 - k]);
result[i] = mul(result[i], f[0][n - 1 - k]);
}
}
}
} |