Verified contractExactMIT
SieveCheck
Address0x0ff4fadd1db11e26c2827a856d9eaec09aaf5687
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.28
- Same code
Only this contract
Details
- Compiler
- solc 0.8.28+commit.7893614a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,322,787
- Creation transaction
- 0xfc01ec9799…2dcd627920
SieveCheck.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
// Links: https://sievequantum.xyz | https://x.com/sieve_quantum
//
// CERTIFICATE OF CALIBRATION No. SV-04
// SieveCheck, the SIEVE lattice verifier
//
// Instrument
// verify(n, hPointer, hook, rootSeed, recipient, salt, payload) returns (normSq, vectorHash).
// Accepts a short vector (f, g) of the lattice {(f, g) : f h = g mod (q, x^n + 1)}, q = 12289,
// n in {64, 128, 256, 512, 1024}, h read from the data contract at hPointer (runtime code
// 0x00 then h as 2-byte big-endian words, exactly 2n + 1 bytes). Reverts PayloadRefused(code) on
// anything else. schoolbook(n, hPointer, f, g) is the independent audit path: it multiplies
// out f h mod (q, x^n + 1) and compares with g. challengePoint(...) returns s from the same
// preimage text verify uses (a test build of verify that returns its own s agrees with it).
// No storage, no owner, no external call.
//
// Reference standard
// The submission is (f, g, k, t) with f h - g = q k + (x^n + 1) t over the integers.
// payload = [wf, wg, wk, wt] then n records, index n - 1 first, each f_i | g_i | k_i | t_i,
// MSB first, each field biased by 2^(w - 1). Required: 1 <= wf, wg <= 14, 1 <= wk <= 24,
// 1 <= wt <= 40; length exactly 4 + n (wf + wg + wk + wt) / 8 (n is a multiple of 8, so no
// padding bit can exist and none is read); t_(n-1) = 0; |f_i|, |g_i| <= 6144; (f, g) != 0.
// s = keccak256("SIEVE/s/v1" | chainid | hook | rootSeed | n | recipient | salt | payload)
// mod P, P = 2^255 - 19, every middle field a 32-byte word. Horner on the biased fields at s,
// the bias removed once through (s - 1) sum s^i = s^n - 1; accept iff F H = L + s^n T, with
// F = f(s), H = h(s), L = (g + q k + t)(s), T = t(s). normSq = sum f_i^2 + sum g_i^2 from
// the same decoded integers. vectorHash = keccak256(f_0 .. f_(n-1) | g_0 .. g_(n-1)), each a
// 2-byte big-endian two's-complement int16, the layout the commit hash uses.
//
// Measured value
// Worst-case execution gas of verify, called directly, cold h, cap widths 14/14/24/40:
// n = 64: 30,399; 128: 55,492; 256: 105,615; 512: 205,906; 1024: 406,854 (measured).
// Prototype B on the old preimage and caps: 30,645 / 57,552 / 111,359 / 218,997 / 434,452.
// The loop has no data-dependent branch, so cost depends on payload length only; the cap
// widths give the longest payload.
// schoolbook execution gas: 89,240 / 241,986 / 736,171 / 2,518,690 / 9,184,464 (measured),
// all under the 16,777,216 per-transaction cap.
// Runtime size: 3,421 bytes.
//
// Uncertainty
// A false (f, g) passes only if a nonzero polynomial of degree below 2n with coefficients
// below 2^42 vanishes at s: probability at most (2n - 1) / P, about 2^-244 at n = 1024, per
// keccak query; 2^80 grinding queries leave about 2^-164. The width caps carry this bound:
// without them k solved modulo P zeroes the identity for every s (tested). The caller must
// pass the h pointer of rung n; a pointer whose code is not exactly 2n + 1 bytes is refused,
// but a pointer to another instance of the same size is not detected here.
//
// Traceability
// sieve/test/check/test_check.mjs against sieve-kit/lib/sieve.mjs on hardhat 2.29.1 (prague,
// chainId 1): per rung 56 valid payloads accepted with normSq, vectorHash
// and s equal to the kit, 70 invalid payloads in 41 kinds (the kit's 24 among them) rejected,
// schoolbook equal to the JS lattice check on 112 vectors; 51 of 51 checks pass. solc 0.8.28, viaIR, optimizer 200 runs, evmVersion cancun.
contract SieveCheck {
// 1 rung, 2 header, 3 length, 4 h code size, 5 t_(n-1), 6 box, 7 zero vector, 8 s = 1, 9 identity
error PayloadRefused(uint256 code);
// An immutable keeps P one PUSH32: as a literal the optimizer rebuilds it from shifts at every use.
uint256 private immutable PRIME;
constructor() {
PRIME = (1 << 255) - 19;
}
function challengePoint(
uint256 n,
address hook,
bytes32 rootSeed,
address recipient,
bytes32 salt,
bytes calldata payload
) external view returns (uint256 s) {
uint256 pMod = PRIME;
assembly {
mstore(0x180, 0x53494556452f732f763100000000000000000000000000000000000000000000)
mstore(0x18a, chainid())
mstore(0x1aa, hook)
mstore(0x1ca, rootSeed)
mstore(0x1ea, n)
mstore(0x20a, recipient)
mstore(0x22a, salt)
calldatacopy(0x24a, payload.offset, payload.length)
s := mod(keccak256(0x180, add(202, payload.length)), pMod)
}
}
function verify(
uint256 n,
address hPointer,
address hook,
bytes32 rootSeed,
address recipient,
bytes32 salt,
bytes calldata payload
) external view returns (uint256 normSq, bytes32 vectorHash) {
uint256 pMod = PRIME;
assembly {
function reject(code) {
mstore(0, 0x992c887700000000000000000000000000000000000000000000000000000000)
mstore(4, code)
revert(0, 36)
}
// Memory. 0x00 mask t, 0x20 mask k, 0x40 mask g, 0x60 bias f, 0x80 bias g,
// 0xa0 shift g, 0xc0 shift f, 0xe0 shift k, 0x100 2W, 0x120 box offsets,
// 0x140 256 - W, 0x160 W. h_i at 0x180 + 2i. f_i is written to 0x9a0 + 2i and g_i to
// 0x11c0 + 2i as whole words in descending i, so the 30 high bytes of each store fall
// on lower indices (written later) or on the 32-byte gaps; g is then moved down
// behind f. The constants are stored through returndatasize() (zero here) so the
// optimizer cannot forward them into registers and run out of stack.
let P := pMod
if iszero(and(iszero(and(n, sub(n, 1))), and(gt(n, 63), lt(n, 1025)))) { reject(1) }
let po := payload.offset
let pl := payload.length
if lt(pl, 4) { reject(2) }
let bt := 0
let zz := returndatasize()
{
let hdr := shr(224, calldataload(po))
let wf := shr(24, hdr)
let wg := and(shr(16, hdr), 0xff)
let wk := and(shr(8, hdr), 0xff)
let wt := and(hdr, 0xff)
if or(or(or(iszero(wf), gt(wf, 14)), or(iszero(wg), gt(wg, 14))), or(or(iszero(wk), gt(wk, 24)), or(iszero(wt), gt(wt, 40)))) { reject(2) }
let W := add(add(wf, wg), add(wk, wt))
if iszero(eq(pl, add(4, shr(3, mul(n, W))))) { reject(3) }
bt := shl(sub(wt, 1), 1)
mstore(add(zz, 0x00), sub(shl(wt, 1), 1))
mstore(add(zz, 0x20), sub(shl(wk, 1), 1))
mstore(add(zz, 0x40), sub(shl(wg, 1), 1))
mstore(add(zz, 0x60), shl(sub(wf, 1), 1))
mstore(add(zz, 0x80), shl(sub(wg, 1), 1))
mstore(add(zz, 0xa0), add(wt, wk))
mstore(add(zz, 0xc0), add(add(wt, wk), wg))
mstore(add(zz, 0xe0), wt)
mstore(add(zz, 0x100), shl(1, W))
// D + 2^128 D with D = 29,354,000: bit 26 (154) of f^2 + D (g^2 + D, less a borrow)
// is set exactly when |f| (|g|) > 6144, for any 14-bit field value
mstore(add(zz, 0x120), 0x1bfe81000000000000000000000000001bfe810)
mstore(add(zz, 0x140), sub(256, W))
mstore(add(zz, 0x160), W)
}
if iszero(eq(extcodesize(hPointer), add(1, shl(1, n)))) { reject(4) }
let bp := shl(3, add(po, 4))
// t_(n-1) is the last field of the first record
if iszero(eq(and(shr(mload(0x140), shl(and(bp, 7), calldataload(shr(3, bp)))), mload(0x00)), bt)) { reject(5) }
// s, byte for byte the preimage of challengePoint; h then overwrites it from 0x180
let s := 0
{
mstore(0x180, 0x53494556452f732f763100000000000000000000000000000000000000000000)
mstore(0x18a, chainid())
mstore(0x1aa, hook)
mstore(0x1ca, rootSeed)
mstore(0x1ea, n)
mstore(0x20a, recipient)
mstore(0x22a, salt)
calldatacopy(0x24a, po, pl)
s := mod(keccak256(0x180, add(202, pl)), P)
}
extcodecopy(hPointer, 0x180, 1, shl(1, n))
// Two records per calldata word (2 W + 7 <= 191 bits). Per record: four Horner steps,
// the centered f_i and g_i stored for vectorHash, and one square of f_i + 2^64 g_i,
// which carries f_i^2, 2 f_i g_i and g_i^2 in separate 64-bit lanes.
let F := 0
let L := 0
let T := 0
let H := 0
let nrm := 0
let bx := 0
for { let hp := add(0x17e, shl(1, n)) } gt(hp, 0x17f) { hp := sub(hp, 4) } {
let x := shl(and(bp, 7), calldataload(shr(3, bp)))
bp := add(bp, mload(0x100))
{
let rec := shr(mload(0x140), x)
let ut := and(rec, mload(0x00))
T := add(mulmod(T, s, P), ut)
let ug := and(shr(mload(0xa0), rec), mload(0x40))
L := add(mulmod(L, s, P), add(add(ug, mul(12289, and(shr(mload(0xe0), rec), mload(0x20)))), ut))
H := add(mulmod(H, s, P), shr(240, mload(hp)))
let uf := shr(mload(0xc0), rec)
F := add(mulmod(F, s, P), uf)
uf := sub(uf, mload(0x60))
ug := sub(ug, mload(0x80))
mstore(add(hp, 0x802), uf)
mstore(add(hp, 0x1022), ug)
uf := add(uf, shl(64, ug))
uf := mul(uf, uf)
nrm := add(nrm, uf)
bx := or(bx, add(uf, mload(0x120)))
}
{
let rec := shr(mload(0x140), shl(mload(0x160), x))
let ut := and(rec, mload(0x00))
T := add(mulmod(T, s, P), ut)
let ug := and(shr(mload(0xa0), rec), mload(0x40))
L := add(mulmod(L, s, P), add(add(ug, mul(12289, and(shr(mload(0xe0), rec), mload(0x20)))), ut))
H := add(mulmod(H, s, P), shr(240, mload(sub(hp, 2))))
let uf := shr(mload(0xc0), rec)
F := add(mulmod(F, s, P), uf)
uf := sub(uf, mload(0x60))
ug := sub(ug, mload(0x80))
mstore(add(hp, 0x800), uf)
mstore(add(hp, 0x1020), ug)
uf := add(uf, shl(64, ug))
uf := mul(uf, uf)
nrm := add(nrm, uf)
bx := or(bx, add(uf, mload(0x120)))
}
}
if and(bx, 0x400000000000000000000000000000004000000) { reject(6) }
// nrm = A + 2^64 B + 2^128 C, A = sum f_i^2 < 2^37, |B| = |sum 2 f_i g_i| < 2^38, C = sum g_i^2
nrm := add(and(nrm, 0xffffffffffffffff), shr(64, add(shr(64, nrm), 0x8000000000000000)))
if iszero(nrm) { reject(7) }
let z := addmod(s, sub(P, 1), P)
if iszero(z) { reject(8) }
let sn := s
sn := mulmod(sn, sn, P)
sn := mulmod(sn, sn, P)
sn := mulmod(sn, sn, P)
sn := mulmod(sn, sn, P)
sn := mulmod(sn, sn, P)
sn := mulmod(sn, sn, P)
for { let e := 64 } lt(e, n) { e := shl(1, e) } { sn := mulmod(sn, sn, P) }
let e := addmod(sn, sub(P, 1), P)
let Fz := addmod(mulmod(F, z, P), sub(P, mulmod(mload(0x60), e, P)), P)
let Lz := addmod(mulmod(L, z, P), sub(P, mulmod(add(add(mload(0x80), mul(12289, shr(1, add(mload(0x20), 1)))), shr(1, add(mload(0x00), 1))), e, P)), P)
let Tz := addmod(mulmod(T, z, P), sub(P, mulmod(shr(1, add(mload(0x00), 1)), e, P)), P)
if iszero(eq(mulmod(Fz, mod(H, P), P), addmod(Lz, mulmod(sn, Tz, P), P))) { reject(9) }
let n2 := shl(1, n)
mcopy(add(0x9a0, n2), 0x11c0, n2)
mstore(0x00, nrm)
mstore(0x20, keccak256(0x9a0, shl(1, n2)))
return(0x00, 0x40)
}
}
// Approach A. f and g are the vectorHash preimage halves: n int16 big-endian words each.
// productOk is true iff every |f_i|, |g_i| <= 6144, (f, g) != 0 and f h = g mod (q, x^n + 1).
// normSq is sum f_i^2 + sum g_i^2 of the values as given. Reverts PayloadRefused(1, 3 or 4) only
// on a bad rung, a wrong length or an h pointer whose code is not 2n + 1 bytes.
function schoolbook(uint256 n, address hPointer, bytes calldata f, bytes calldata g)
external view returns (bool productOk, uint256 normSq)
{
assembly {
function reject(code) {
mstore(0, 0x992c887700000000000000000000000000000000000000000000000000000000)
mstore(4, code)
revert(0, 36)
}
if iszero(and(iszero(and(n, sub(n, 1))), and(gt(n, 63), lt(n, 1025)))) { reject(1) }
let n2 := shl(1, n)
if or(iszero(eq(f.length, n2)), iszero(eq(g.length, n2))) { reject(3) }
if iszero(eq(extcodesize(hPointer), add(1, n2))) { reject(4) }
let FA := 0x80
let GA := add(FA, shl(5, n))
let PK := add(GA, shl(5, n))
let HB := add(PK, shl(6, n))
extcodecopy(hPointer, HB, 1, n2)
let nrm := 0
let box := 0
{
let fo := f.offset
let go := g.offset
for { let i := 0 } lt(i, n) { i := add(i, 1) } {
let cf := signextend(1, shr(240, calldataload(add(fo, shl(1, i)))))
let cg := signextend(1, shr(240, calldataload(add(go, shl(1, i)))))
let sf := mul(cf, cf)
let sg := mul(cg, cg)
nrm := add(nrm, add(sf, sg))
box := or(box, or(gt(sf, 37748736), gt(sg, 37748736)))
mstore(add(FA, shl(5, i)), add(cf, mul(slt(cf, 0), 12289)))
mstore(add(GA, shl(5, i)), add(cg, mul(slt(cg, 0), 12289)))
}
}
if or(box, iszero(nrm)) {
mstore(0x00, 0)
mstore(0x20, nrm)
return(0x00, 0x40)
}
// PK[a] holds H2[a .. a + 5] in 42-bit lanes, lane l = H2[a + l], where
// H2[m] = (q - h_m) mod q for m < n, h_(m - n) for n <= m < 2n, 0 above
{
let prev := 0
for { let a := sub(n2, 1) } lt(a, n2) { a := sub(a, 1) } {
let v := 0
switch lt(a, n)
case 1 { v := mod(sub(12289, shr(240, mload(add(HB, shl(1, a))))), 12289) }
default { v := shr(240, mload(add(HB, shl(1, sub(a, n))))) }
prev := or(and(shl(42, prev), 0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff), v)
mstore(add(PK, shl(5, a)), prev)
}
}
// c_j = sum_i f_i H2[j - i + n]: six output coefficients per pass, every lane sum
// below n (q - 1)^2 < 2^38, so the 42-bit lanes never carry into each other
let bad := 0
let fEnd := add(FA, shl(5, n))
for { let j0 := 0 } lt(j0, n) { j0 := add(j0, 6) } {
let acc := 0
let pp := add(PK, shl(5, add(j0, n)))
for { let pf := FA } lt(pf, fEnd) { pf := add(pf, 0x80) } {
acc := add(acc, mul(mload(pf), mload(pp)))
acc := add(acc, mul(mload(add(pf, 0x20)), mload(sub(pp, 0x20))))
acc := add(acc, mul(mload(add(pf, 0x40)), mload(sub(pp, 0x40))))
acc := add(acc, mul(mload(add(pf, 0x60)), mload(sub(pp, 0x60))))
pp := sub(pp, 0x80)
}
for { let l := 0 } lt(l, 6) { l := add(l, 1) } {
let i := add(j0, l)
if lt(i, n) {
let c := mod(and(shr(mul(42, l), acc), 0x3ffffffffff), 12289)
bad := or(bad, iszero(eq(c, mload(add(GA, shl(5, i))))))
}
}
}
mstore(0x00, iszero(bad))
mstore(0x20, nrm)
return(0x00, 0x40)
}
}
}
Compiler settingsShow
{
"viaIR": true,
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"@uniswap/v4-core/=v4/v4-core/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.