|
| 1 | +// SPDX-License-Identifier: GPL-3.0 |
| 2 | +/* |
| 3 | + Copyright 2021 0KIMS association. |
| 4 | +
|
| 5 | + This file is generated with [snarkJS](https://github.com/iden3/snarkjs). |
| 6 | +
|
| 7 | + snarkJS is a free software: you can redistribute it and/or modify it |
| 8 | + under the terms of the GNU General Public License as published by |
| 9 | + the Free Software Foundation, either version 3 of the License, or |
| 10 | + (at your option) any later version. |
| 11 | +
|
| 12 | + snarkJS is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 15 | + License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU General Public License |
| 18 | + along with snarkJS. If not, see <https://www.gnu.org/licenses/>. |
| 19 | +*/ |
| 20 | + |
| 21 | +pragma solidity >=0.7.0 <0.9.0; |
| 22 | + |
| 23 | +contract PUniversalPSS2048S32E17Verifier2 { |
| 24 | + // Scalar field size |
| 25 | + uint256 constant r = |
| 26 | + 21888242871839275222246405745257275088548364400416034343698204186575808495617; |
| 27 | + // Base field size |
| 28 | + uint256 constant q = |
| 29 | + 21888242871839275222246405745257275088696311157297823662689037894645226208583; |
| 30 | + |
| 31 | + // Verification Key data |
| 32 | + uint256 constant alphax = |
| 33 | + 20491192805390485299153009773594534940189261866228447918068658471970481763042; |
| 34 | + uint256 constant alphay = |
| 35 | + 9383485363053290200918347156157836566562967994039712273449902621266178545958; |
| 36 | + uint256 constant betax1 = |
| 37 | + 4252822878758300859123897981450591353533073413197771768651442665752259397132; |
| 38 | + uint256 constant betax2 = |
| 39 | + 6375614351688725206403948262868962793625744043794305715222011528459656738731; |
| 40 | + uint256 constant betay1 = |
| 41 | + 21847035105528745403288232691147584728191162732299865338377159692350059136679; |
| 42 | + uint256 constant betay2 = |
| 43 | + 10505242626370262277552901082094356697409835680220590971873171140371331206856; |
| 44 | + uint256 constant gammax1 = |
| 45 | + 11559732032986387107991004021392285783925812861821192530917403151452391805634; |
| 46 | + uint256 constant gammax2 = |
| 47 | + 10857046999023057135944570762232829481370756359578518086990519993285655852781; |
| 48 | + uint256 constant gammay1 = |
| 49 | + 4082367875863433681332203403145435568316851327593401208105741076214120093531; |
| 50 | + uint256 constant gammay2 = |
| 51 | + 8495653923123431417604973247489272438418190587263600148770280649306958101930; |
| 52 | + uint256 constant deltax1 = |
| 53 | + 2588948925551393009821379010170296839171297364674926056776563823985885238643; |
| 54 | + uint256 constant deltax2 = |
| 55 | + 14900929029555836629537848424443710435074759809914306575548486288090248959071; |
| 56 | + uint256 constant deltay1 = |
| 57 | + 10184192331507677964899435346318505861227362807562248922941844861656088009170; |
| 58 | + uint256 constant deltay2 = |
| 59 | + 20690371365387193842744737388784868315539899096039639914226997803494564139176; |
| 60 | + |
| 61 | + uint256 constant IC0x = |
| 62 | + 5513868239326817870410164712318795274398686363598458585785159688959566867815; |
| 63 | + uint256 constant IC0y = |
| 64 | + 17274949770368590455783492353820085808147868717692001355348161310925780245390; |
| 65 | + |
| 66 | + uint256 constant IC1x = |
| 67 | + 14434034932401929390907700455767908494570776801902354795813817822460977622801; |
| 68 | + uint256 constant IC1y = |
| 69 | + 7795286992377308915619457285234307608338046637131650471957877967674306633957; |
| 70 | + |
| 71 | + uint256 constant IC2x = |
| 72 | + 1799694825459276907982519593950907279200220699777334997920526844449753518122; |
| 73 | + uint256 constant IC2y = |
| 74 | + 11470964385636667567015995141048560130504477977754781484852520338465808368625; |
| 75 | + |
| 76 | + uint256 constant IC3x = |
| 77 | + 3578683655309991328789470259589189721994433645341712041653786551027643754612; |
| 78 | + uint256 constant IC3y = |
| 79 | + 20829784542914714886129884294970428296764545228596603275084466471095337855262; |
| 80 | + |
| 81 | + uint256 constant IC4x = |
| 82 | + 6881020891351067428803335583630557988181998147843537176149482539087452187677; |
| 83 | + uint256 constant IC4y = |
| 84 | + 2977612226097458279874241939745899535761337599786508025287049281840802982690; |
| 85 | + |
| 86 | + uint256 constant IC5x = |
| 87 | + 11984863926110545983033789309031949517985833075301451611747462249678731478178; |
| 88 | + uint256 constant IC5y = |
| 89 | + 9756448127773155759370621544484733330075551392606737222134416453050508622742; |
| 90 | + |
| 91 | + // Memory data |
| 92 | + uint16 constant pVk = 0; |
| 93 | + uint16 constant pPairing = 128; |
| 94 | + |
| 95 | + uint16 constant pLastMem = 896; |
| 96 | + |
| 97 | + function verifyProof( |
| 98 | + uint[2] calldata _pA, |
| 99 | + uint[2][2] calldata _pB, |
| 100 | + uint[2] calldata _pC, |
| 101 | + uint[5] calldata _pubSignals |
| 102 | + ) public view returns (bool) { |
| 103 | + assembly { |
| 104 | + function checkField(v) { |
| 105 | + if iszero(lt(v, q)) { |
| 106 | + mstore(0, 0) |
| 107 | + return(0, 0x20) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // G1 function to multiply a G1 value(x,y) to value in an address |
| 112 | + function g1_mulAccC(pR, x, y, s) { |
| 113 | + let success |
| 114 | + let mIn := mload(0x40) |
| 115 | + mstore(mIn, x) |
| 116 | + mstore(add(mIn, 32), y) |
| 117 | + mstore(add(mIn, 64), s) |
| 118 | + |
| 119 | + success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64) |
| 120 | + |
| 121 | + if iszero(success) { |
| 122 | + mstore(0, 0) |
| 123 | + return(0, 0x20) |
| 124 | + } |
| 125 | + |
| 126 | + mstore(add(mIn, 64), mload(pR)) |
| 127 | + mstore(add(mIn, 96), mload(add(pR, 32))) |
| 128 | + |
| 129 | + success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64) |
| 130 | + |
| 131 | + if iszero(success) { |
| 132 | + mstore(0, 0) |
| 133 | + return(0, 0x20) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + function checkPairing(pA, pB, pC, pubSignals, pMem) -> isOk { |
| 138 | + let _pPairing := add(pMem, pPairing) |
| 139 | + let _pVk := add(pMem, pVk) |
| 140 | + |
| 141 | + mstore(_pVk, IC0x) |
| 142 | + mstore(add(_pVk, 32), IC0y) |
| 143 | + |
| 144 | + // Compute the linear combination vk_x |
| 145 | + |
| 146 | + g1_mulAccC(_pVk, IC1x, IC1y, calldataload(add(pubSignals, 0))) |
| 147 | + |
| 148 | + g1_mulAccC(_pVk, IC2x, IC2y, calldataload(add(pubSignals, 32))) |
| 149 | + |
| 150 | + g1_mulAccC(_pVk, IC3x, IC3y, calldataload(add(pubSignals, 64))) |
| 151 | + |
| 152 | + g1_mulAccC(_pVk, IC4x, IC4y, calldataload(add(pubSignals, 96))) |
| 153 | + |
| 154 | + g1_mulAccC(_pVk, IC5x, IC5y, calldataload(add(pubSignals, 128))) |
| 155 | + |
| 156 | + // -A |
| 157 | + mstore(_pPairing, calldataload(pA)) |
| 158 | + mstore(add(_pPairing, 32), mod(sub(q, calldataload(add(pA, 32))), q)) |
| 159 | + |
| 160 | + // B |
| 161 | + mstore(add(_pPairing, 64), calldataload(pB)) |
| 162 | + mstore(add(_pPairing, 96), calldataload(add(pB, 32))) |
| 163 | + mstore(add(_pPairing, 128), calldataload(add(pB, 64))) |
| 164 | + mstore(add(_pPairing, 160), calldataload(add(pB, 96))) |
| 165 | + |
| 166 | + // alpha1 |
| 167 | + mstore(add(_pPairing, 192), alphax) |
| 168 | + mstore(add(_pPairing, 224), alphay) |
| 169 | + |
| 170 | + // beta2 |
| 171 | + mstore(add(_pPairing, 256), betax1) |
| 172 | + mstore(add(_pPairing, 288), betax2) |
| 173 | + mstore(add(_pPairing, 320), betay1) |
| 174 | + mstore(add(_pPairing, 352), betay2) |
| 175 | + |
| 176 | + // vk_x |
| 177 | + mstore(add(_pPairing, 384), mload(add(pMem, pVk))) |
| 178 | + mstore(add(_pPairing, 416), mload(add(pMem, add(pVk, 32)))) |
| 179 | + |
| 180 | + // gamma2 |
| 181 | + mstore(add(_pPairing, 448), gammax1) |
| 182 | + mstore(add(_pPairing, 480), gammax2) |
| 183 | + mstore(add(_pPairing, 512), gammay1) |
| 184 | + mstore(add(_pPairing, 544), gammay2) |
| 185 | + |
| 186 | + // C |
| 187 | + mstore(add(_pPairing, 576), calldataload(pC)) |
| 188 | + mstore(add(_pPairing, 608), calldataload(add(pC, 32))) |
| 189 | + |
| 190 | + // delta2 |
| 191 | + mstore(add(_pPairing, 640), deltax1) |
| 192 | + mstore(add(_pPairing, 672), deltax2) |
| 193 | + mstore(add(_pPairing, 704), deltay1) |
| 194 | + mstore(add(_pPairing, 736), deltay2) |
| 195 | + |
| 196 | + let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20) |
| 197 | + |
| 198 | + isOk := and(success, mload(_pPairing)) |
| 199 | + } |
| 200 | + |
| 201 | + let pMem := mload(0x40) |
| 202 | + mstore(0x40, add(pMem, pLastMem)) |
| 203 | + |
| 204 | + // Validate that all evaluations ∈ F |
| 205 | + |
| 206 | + checkField(calldataload(add(_pubSignals, 0))) |
| 207 | + |
| 208 | + checkField(calldataload(add(_pubSignals, 32))) |
| 209 | + |
| 210 | + checkField(calldataload(add(_pubSignals, 64))) |
| 211 | + |
| 212 | + checkField(calldataload(add(_pubSignals, 96))) |
| 213 | + |
| 214 | + checkField(calldataload(add(_pubSignals, 128))) |
| 215 | + |
| 216 | + checkField(calldataload(add(_pubSignals, 160))) |
| 217 | + |
| 218 | + // Validate all evaluations |
| 219 | + let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem) |
| 220 | + |
| 221 | + mstore(0, isValid) |
| 222 | + return(0, 0x20) |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments