|
| 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 Groth16Verifier { |
| 24 | + // Scalar field size |
| 25 | + uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617; |
| 26 | + // Base field size |
| 27 | + uint256 constant q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; |
| 28 | + |
| 29 | + // Verification Key data |
| 30 | + uint256 constant alphax = 5626737339165458973589599457621328529733563392239816845745153285393308563504; |
| 31 | + uint256 constant alphay = 16230467856761692471715032392328021447033402595640524796664100872627369680070; |
| 32 | + uint256 constant betax1 = 4144428083991626285342649939274849449280404441912827482121791540345299786531; |
| 33 | + uint256 constant betax2 = 6325433196787304365631895536258959371015671431667950220195827813497107168985; |
| 34 | + uint256 constant betay1 = 10936661144817015255674518731163444456215109280399110754173599934895271139411; |
| 35 | + uint256 constant betay2 = 19889497587240439239166766822075868131117808253130075878786112720929398512804; |
| 36 | + uint256 constant gammax1 = 11559732032986387107991004021392285783925812861821192530917403151452391805634; |
| 37 | + uint256 constant gammax2 = 10857046999023057135944570762232829481370756359578518086990519993285655852781; |
| 38 | + uint256 constant gammay1 = 4082367875863433681332203403145435568316851327593401208105741076214120093531; |
| 39 | + uint256 constant gammay2 = 8495653923123431417604973247489272438418190587263600148770280649306958101930; |
| 40 | + uint256 constant deltax1 = 17295485814694150123783000481296321702245813170311037729305904660722399839036; |
| 41 | + uint256 constant deltax2 = 9953038103920179753358196237980982556490877057040907085330335881158104037356; |
| 42 | + uint256 constant deltay1 = 6539691989876630411606817978414648636843419931042291769251371916370543524601; |
| 43 | + uint256 constant deltay2 = 13144314962000242455271345157960851628353417380470981436942002942254462986698; |
| 44 | + |
| 45 | + |
| 46 | + uint256 constant IC0x = 18387697014443789242231700969179111271118402915177266877757196673698635297519; |
| 47 | + uint256 constant IC0y = 12436236940538570506589398884575585672721049376613458579755675085941516075140; |
| 48 | + |
| 49 | + uint256 constant IC1x = 4524932351881239948215022805183121776177868798590915616981221083576167527145; |
| 50 | + uint256 constant IC1y = 15587504486709804185620332672789255104510724034018287661184516721901064633674; |
| 51 | + |
| 52 | + uint256 constant IC2x = 14439831487577900683535901946347940295203534376602237241423095901557646925805; |
| 53 | + uint256 constant IC2y = 10717629512118312536692599889074200105443234230892842440615136924549793591272; |
| 54 | + |
| 55 | + uint256 constant IC3x = 10024533475695360204705392683236727525700242453221038868275481275771880537493; |
| 56 | + uint256 constant IC3y = 4765705996809486869329645343856463186100886171625349210938486327289623735490; |
| 57 | + |
| 58 | + |
| 59 | + // Memory data |
| 60 | + uint16 constant pVk = 0; |
| 61 | + uint16 constant pPairing = 128; |
| 62 | + |
| 63 | + uint16 constant pLastMem = 896; |
| 64 | + |
| 65 | + function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[3] calldata _pubSignals) public view returns (bool) { |
| 66 | + assembly { |
| 67 | + function checkField(v) { |
| 68 | + if iszero(lt(v, r)) { |
| 69 | + mstore(0, 0) |
| 70 | + return(0, 0x20) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // G1 function to multiply a G1 value(x,y) to value in an address |
| 75 | + function g1_mulAccC(pR, x, y, s) { |
| 76 | + let success |
| 77 | + let mIn := mload(0x40) |
| 78 | + mstore(mIn, x) |
| 79 | + mstore(add(mIn, 32), y) |
| 80 | + mstore(add(mIn, 64), s) |
| 81 | + |
| 82 | + success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64) |
| 83 | + |
| 84 | + if iszero(success) { |
| 85 | + mstore(0, 0) |
| 86 | + return(0, 0x20) |
| 87 | + } |
| 88 | + |
| 89 | + mstore(add(mIn, 64), mload(pR)) |
| 90 | + mstore(add(mIn, 96), mload(add(pR, 32))) |
| 91 | + |
| 92 | + success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64) |
| 93 | + |
| 94 | + if iszero(success) { |
| 95 | + mstore(0, 0) |
| 96 | + return(0, 0x20) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + function checkPairing(pA, pB, pC, pubSignals, pMem) -> isOk { |
| 101 | + let _pPairing := add(pMem, pPairing) |
| 102 | + let _pVk := add(pMem, pVk) |
| 103 | + |
| 104 | + mstore(_pVk, IC0x) |
| 105 | + mstore(add(_pVk, 32), IC0y) |
| 106 | + |
| 107 | + // Compute the linear combination vk_x |
| 108 | + |
| 109 | + g1_mulAccC(_pVk, IC1x, IC1y, calldataload(add(pubSignals, 0))) |
| 110 | + |
| 111 | + g1_mulAccC(_pVk, IC2x, IC2y, calldataload(add(pubSignals, 32))) |
| 112 | + |
| 113 | + g1_mulAccC(_pVk, IC3x, IC3y, calldataload(add(pubSignals, 64))) |
| 114 | + |
| 115 | + |
| 116 | + // -A |
| 117 | + mstore(_pPairing, calldataload(pA)) |
| 118 | + mstore(add(_pPairing, 32), mod(sub(q, calldataload(add(pA, 32))), q)) |
| 119 | + |
| 120 | + // B |
| 121 | + mstore(add(_pPairing, 64), calldataload(pB)) |
| 122 | + mstore(add(_pPairing, 96), calldataload(add(pB, 32))) |
| 123 | + mstore(add(_pPairing, 128), calldataload(add(pB, 64))) |
| 124 | + mstore(add(_pPairing, 160), calldataload(add(pB, 96))) |
| 125 | + |
| 126 | + // alpha1 |
| 127 | + mstore(add(_pPairing, 192), alphax) |
| 128 | + mstore(add(_pPairing, 224), alphay) |
| 129 | + |
| 130 | + // beta2 |
| 131 | + mstore(add(_pPairing, 256), betax1) |
| 132 | + mstore(add(_pPairing, 288), betax2) |
| 133 | + mstore(add(_pPairing, 320), betay1) |
| 134 | + mstore(add(_pPairing, 352), betay2) |
| 135 | + |
| 136 | + // vk_x |
| 137 | + mstore(add(_pPairing, 384), mload(add(pMem, pVk))) |
| 138 | + mstore(add(_pPairing, 416), mload(add(pMem, add(pVk, 32)))) |
| 139 | + |
| 140 | + |
| 141 | + // gamma2 |
| 142 | + mstore(add(_pPairing, 448), gammax1) |
| 143 | + mstore(add(_pPairing, 480), gammax2) |
| 144 | + mstore(add(_pPairing, 512), gammay1) |
| 145 | + mstore(add(_pPairing, 544), gammay2) |
| 146 | + |
| 147 | + // C |
| 148 | + mstore(add(_pPairing, 576), calldataload(pC)) |
| 149 | + mstore(add(_pPairing, 608), calldataload(add(pC, 32))) |
| 150 | + |
| 151 | + // delta2 |
| 152 | + mstore(add(_pPairing, 640), deltax1) |
| 153 | + mstore(add(_pPairing, 672), deltax2) |
| 154 | + mstore(add(_pPairing, 704), deltay1) |
| 155 | + mstore(add(_pPairing, 736), deltay2) |
| 156 | + |
| 157 | + |
| 158 | + let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20) |
| 159 | + |
| 160 | + isOk := and(success, mload(_pPairing)) |
| 161 | + } |
| 162 | + |
| 163 | + let pMem := mload(0x40) |
| 164 | + mstore(0x40, add(pMem, pLastMem)) |
| 165 | + |
| 166 | + // Validate that all evaluations ∈ F |
| 167 | + |
| 168 | + checkField(calldataload(add(_pubSignals, 0))) |
| 169 | + |
| 170 | + checkField(calldataload(add(_pubSignals, 32))) |
| 171 | + |
| 172 | + checkField(calldataload(add(_pubSignals, 64))) |
| 173 | + |
| 174 | + |
| 175 | + // Validate all evaluations |
| 176 | + let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem) |
| 177 | + |
| 178 | + mstore(0, isValid) |
| 179 | + return(0, 0x20) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
0 commit comments