// SPDX-License-Identifier: LGPL-3.0-only // This file is LGPL3 Licensed pragma solidity ^0.8.0; /** * @title Elliptic curve operations on twist points for alt_bn128 * @author Mustafa Al-Bassam (mus@musalbas.com) * @dev Homepage: https://github.com/musalbas/solidity-BN256G2 */ library BN256G2 { uint256 internal constant FIELD_MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47; uint256 internal constant TWISTBX = 0x2b149d40ceb8aaae81be18991be06ac3b5b4c5e559dbefa33267e6dc24a138e5; uint256 internal constant TWISTBY = 0x9713b03af0fed4cd2cafadeed8fdf4a74fa084e52d1852e4a2bd0685c315d2; uint internal constant PTXX = 0; uint internal constant PTXY = 1; uint internal constant PTYX = 2; uint internal constant PTYY = 3; uint internal constant PTZX = 4; uint internal constant PTZY = 5; /** * @notice Add two twist points * @param pt1xx Coefficient 1 of x on point 1 * @param pt1xy Coefficient 2 of x on point 1 * @param pt1yx Coefficient 1 of y on point 1 * @param pt1yy Coefficient 2 of y on point 1 * @param pt2xx Coefficient 1 of x on point 2 * @param pt2xy Coefficient 2 of x on point 2 * @param pt2yx Coefficient 1 of y on point 2 * @param pt2yy Coefficient 2 of y on point 2 * @return (pt3xx, pt3xy, pt3yx, pt3yy) */ function ECTwistAdd( uint256 pt1xx, uint256 pt1xy, uint256 pt1yx, uint256 pt1yy, uint256 pt2xx, uint256 pt2xy, uint256 pt2yx, uint256 pt2yy ) public view returns ( uint256, uint256, uint256, uint256 ) { if ( pt1xx == 0 && pt1xy == 0 && pt1yx == 0 && pt1yy == 0 ) { if (!( pt2xx == 0 && pt2xy == 0 && pt2yx == 0 && pt2yy == 0 )) { assert(_isOnCurve( pt2xx, pt2xy, pt2yx, pt2yy )); } return ( pt2xx, pt2xy, pt2yx, pt2yy ); } else if ( pt2xx == 0 && pt2xy == 0 && pt2yx == 0 && pt2yy == 0 ) { assert(_isOnCurve( pt1xx, pt1xy, pt1yx, pt1yy )); return ( pt1xx, pt1xy, pt1yx, pt1yy ); } assert(_isOnCurve( pt1xx, pt1xy, pt1yx, pt1yy )); assert(_isOnCurve( pt2xx, pt2xy, pt2yx, pt2yy )); uint256[6] memory pt3 = _ECTwistAddJacobian( pt1xx, pt1xy, pt1yx, pt1yy, 1, 0, pt2xx, pt2xy, pt2yx, pt2yy, 1, 0 ); return _fromJacobian( pt3[PTXX], pt3[PTXY], pt3[PTYX], pt3[PTYY], pt3[PTZX], pt3[PTZY] ); } /** * @notice Multiply a twist point by a scalar * @param s Scalar to multiply by * @param pt1xx Coefficient 1 of x * @param pt1xy Coefficient 2 of x * @param pt1yx Coefficient 1 of y * @param pt1yy Coefficient 2 of y * @return (pt2xx, pt2xy, pt2yx, pt2yy) */ function ECTwistMul( uint256 s, uint256 pt1xx, uint256 pt1xy, uint256 pt1yx, uint256 pt1yy ) public view returns ( uint256, uint256, uint256, uint256 ) { uint256 pt1zx = 1; if ( pt1xx == 0 && pt1xy == 0 && pt1yx == 0 && pt1yy == 0 ) { pt1xx = 1; pt1yx = 1; pt1zx = 0; } else { assert(_isOnCurve( pt1xx, pt1xy, pt1yx, pt1yy )); } uint256[6] memory pt2 = _ECTwistMulJacobian( s, pt1xx, pt1xy, pt1yx, pt1yy, pt1zx, 0 ); return _fromJacobian( pt2[PTXX], pt2[PTXY], pt2[PTYX], pt2[PTYY], pt2[PTZX], pt2[PTZY] ); } /** * @notice Get the field modulus * @return The field modulus */ function GetFieldModulus() public pure returns (uint256) { return FIELD_MODULUS; } function submod(uint256 a, uint256 b, uint256 n) internal pure returns (uint256) { return addmod(a, n - b, n); } function _FQ2Mul( uint256 xx, uint256 xy, uint256 yx, uint256 yy ) internal pure returns (uint256, uint256) { return ( submod(mulmod(xx, yx, FIELD_MODULUS), mulmod(xy, yy, FIELD_MODULUS), FIELD_MODULUS), addmod(mulmod(xx, yy, FIELD_MODULUS), mulmod(xy, yx, FIELD_MODULUS), FIELD_MODULUS) ); } function _FQ2Muc( uint256 xx, uint256 xy, uint256 c ) internal pure returns (uint256, uint256) { return ( mulmod(xx, c, FIELD_MODULUS), mulmod(xy, c, FIELD_MODULUS) ); } function _FQ2Add( uint256 xx, uint256 xy, uint256 yx, uint256 yy ) internal pure returns (uint256, uint256) { return ( addmod(xx, yx, FIELD_MODULUS), addmod(xy, yy, FIELD_MODULUS) ); } function _FQ2Sub( uint256 xx, uint256 xy, uint256 yx, uint256 yy ) internal pure returns (uint256 rx, uint256 ry) { return ( submod(xx, yx, FIELD_MODULUS), submod(xy, yy, FIELD_MODULUS) ); } function _FQ2Div( uint256 xx, uint256 xy, uint256 yx, uint256 yy ) internal view returns (uint256, uint256) { (yx, yy) = _FQ2Inv(yx, yy); return _FQ2Mul(xx, xy, yx, yy); } function _FQ2Inv(uint256 x, uint256 y) internal view returns (uint256, uint256) { uint256 inv = _modInv(addmod(mulmod(y, y, FIELD_MODULUS), mulmod(x, x, FIELD_MODULUS), FIELD_MODULUS), FIELD_MODULUS); return ( mulmod(x, inv, FIELD_MODULUS), FIELD_MODULUS - mulmod(y, inv, FIELD_MODULUS) ); } function _isOnCurve( uint256 xx, uint256 xy, uint256 yx, uint256 yy ) internal pure returns (bool) { uint256 yyx; uint256 yyy; uint256 xxxx; uint256 xxxy; (yyx, yyy) = _FQ2Mul(yx, yy, yx, yy); (xxxx, xxxy) = _FQ2Mul(xx, xy, xx, xy); (xxxx, xxxy) = _FQ2Mul(xxxx, xxxy, xx, xy); (yyx, yyy) = _FQ2Sub(yyx, yyy, xxxx, xxxy); (yyx, yyy) = _FQ2Sub(yyx, yyy, TWISTBX, TWISTBY); return yyx == 0 && yyy == 0; } function _modInv(uint256 a, uint256 n) internal view returns (uint256 result) { bool success; assembly { let freemem := mload(0x40) mstore(freemem, 0x20) mstore(add(freemem,0x20), 0x20) mstore(add(freemem,0x40), 0x20) mstore(add(freemem,0x60), a) mstore(add(freemem,0x80), sub(n, 2)) mstore(add(freemem,0xA0), n) success := staticcall(sub(gas(), 2000), 5, freemem, 0xC0, freemem, 0x20) result := mload(freemem) } require(success); } function _fromJacobian( uint256 pt1xx, uint256 pt1xy, uint256 pt1yx, uint256 pt1yy, uint256 pt1zx, uint256 pt1zy ) internal view returns ( uint256 pt2xx, uint256 pt2xy, uint256 pt2yx, uint256 pt2yy ) { uint256 invzx; uint256 invzy; (invzx, invzy) = _FQ2Inv(pt1zx, pt1zy); (pt2xx, pt2xy) = _FQ2Mul(pt1xx, pt1xy, invzx, invzy); (pt2yx, pt2yy) = _FQ2Mul(pt1yx, pt1yy, invzx, invzy); } function _ECTwistAddJacobian( uint256 pt1xx, uint256 pt1xy, uint256 pt1yx, uint256 pt1yy, uint256 pt1zx, uint256 pt1zy, uint256 pt2xx, uint256 pt2xy, uint256 pt2yx, uint256 pt2yy, uint256 pt2zx, uint256 pt2zy) internal pure returns (uint256[6] memory pt3) { if (pt1zx == 0 && pt1zy == 0) { ( pt3[PTXX], pt3[PTXY], pt3[PTYX], pt3[PTYY], pt3[PTZX], pt3[PTZY] ) = ( pt2xx, pt2xy, pt2yx, pt2yy, pt2zx, pt2zy ); return pt3; } else if (pt2zx == 0 && pt2zy == 0) { ( pt3[PTXX], pt3[PTXY], pt3[PTYX], pt3[PTYY], pt3[PTZX], pt3[PTZY] ) = ( pt1xx, pt1xy, pt1yx, pt1yy, pt1zx, pt1zy ); return pt3; } (pt2yx, pt2yy) = _FQ2Mul(pt2yx, pt2yy, pt1zx, pt1zy); // U1 = y2 * z1 (pt3[PTYX], pt3[PTYY]) = _FQ2Mul(pt1yx, pt1yy, pt2zx, pt2zy); // U2 = y1 * z2 (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt1zx, pt1zy); // V1 = x2 * z1 (pt3[PTZX], pt3[PTZY]) = _FQ2Mul(pt1xx, pt1xy, pt2zx, pt2zy); // V2 = x1 * z2 if (pt2xx == pt3[PTZX] && pt2xy == pt3[PTZY]) { if (pt2yx == pt3[PTYX] && pt2yy == pt3[PTYY]) { ( pt3[PTXX], pt3[PTXY], pt3[PTYX], pt3[PTYY], pt3[PTZX], pt3[PTZY] ) = _ECTwistDoubleJacobian(pt1xx, pt1xy, pt1yx, pt1yy, pt1zx, pt1zy); return pt3; } ( pt3[PTXX], pt3[PTXY], pt3[PTYX], pt3[PTYY], pt3[PTZX], pt3[PTZY] ) = ( 1, 0, 1, 0, 0, 0 ); return pt3; } (pt2zx, pt2zy) = _FQ2Mul(pt1zx, pt1zy, pt2zx, pt2zy); // W = z1 * z2 (pt1xx, pt1xy) = _FQ2Sub(pt2yx, pt2yy, pt3[PTYX], pt3[PTYY]); // U = U1 - U2 (pt1yx, pt1yy) = _FQ2Sub(pt2xx, pt2xy, pt3[PTZX], pt3[PTZY]); // V = V1 - V2 (pt1zx, pt1zy) = _FQ2Mul(pt1yx, pt1yy, pt1yx, pt1yy); // V_squared = V * V (pt2yx, pt2yy) = _FQ2Mul(pt1zx, pt1zy, pt3[PTZX], pt3[PTZY]); // V_squared_times_V2 = V_squared * V2 (pt1zx, pt1zy) = _FQ2Mul(pt1zx, pt1zy, pt1yx, pt1yy); // V_cubed = V * V_squared (pt3[PTZX], pt3[PTZY]) = _FQ2Mul(pt1zx, pt1zy, pt2zx, pt2zy); // newz = V_cubed * W (pt2xx, pt2xy) = _FQ2Mul(pt1xx, pt1xy, pt1xx, pt1xy); // U * U (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt2zx, pt2zy); // U * U * W (pt2xx, pt2xy) = _FQ2Sub(pt2xx, pt2xy, pt1zx, pt1zy); // U * U * W - V_cubed (pt2zx, pt2zy) = _FQ2Muc(pt2yx, pt2yy, 2); // 2 * V_squared_times_V2 (pt2xx, pt2xy) = _FQ2Sub(pt2xx, pt2xy, pt2zx, pt2zy); // A = U * U * W - V_cubed - 2 * V_squared_times_V2 (pt3[PTXX], pt3[PTXY]) = _FQ2Mul(pt1yx, pt1yy, pt2xx, pt2xy); // newx = V * A (pt1yx, pt1yy) = _FQ2Sub(pt2yx, pt2yy, pt2xx, pt2xy); // V_squared_times_V2 - A (pt1yx, pt1yy) = _FQ2Mul(pt1xx, pt1xy, pt1yx, pt1yy); // U * (V_squared_times_V2 - A) (pt1xx, pt1xy) = _FQ2Mul(pt1zx, pt1zy, pt3[PTYX], pt3[PTYY]); // V_cubed * U2 (pt3[PTYX], pt3[PTYY]) = _FQ2Sub(pt1yx, pt1yy, pt1xx, pt1xy); // newy = U * (V_squared_times_V2 - A) - V_cubed * U2 } function _ECTwistDoubleJacobian( uint256 pt1xx, uint256 pt1xy, uint256 pt1yx, uint256 pt1yy, uint256 pt1zx, uint256 pt1zy ) internal pure returns ( uint256 pt2xx, uint256 pt2xy, uint256 pt2yx, uint256 pt2yy, uint256 pt2zx, uint256 pt2zy ) { (pt2xx, pt2xy) = _FQ2Muc(pt1xx, pt1xy, 3); // 3 * x (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt1xx, pt1xy); // W = 3 * x * x (pt1zx, pt1zy) = _FQ2Mul(pt1yx, pt1yy, pt1zx, pt1zy); // S = y * z (pt2yx, pt2yy) = _FQ2Mul(pt1xx, pt1xy, pt1yx, pt1yy); // x * y (pt2yx, pt2yy) = _FQ2Mul(pt2yx, pt2yy, pt1zx, pt1zy); // B = x * y * S (pt1xx, pt1xy) = _FQ2Mul(pt2xx, pt2xy, pt2xx, pt2xy); // W * W (pt2zx, pt2zy) = _FQ2Muc(pt2yx, pt2yy, 8); // 8 * B (pt1xx, pt1xy) = _FQ2Sub(pt1xx, pt1xy, pt2zx, pt2zy); // H = W * W - 8 * B (pt2zx, pt2zy) = _FQ2Mul(pt1zx, pt1zy, pt1zx, pt1zy); // S_squared = S * S (pt2yx, pt2yy) = _FQ2Muc(pt2yx, pt2yy, 4); // 4 * B (pt2yx, pt2yy) = _FQ2Sub(pt2yx, pt2yy, pt1xx, pt1xy); // 4 * B - H (pt2yx, pt2yy) = _FQ2Mul(pt2yx, pt2yy, pt2xx, pt2xy); // W * (4 * B - H) (pt2xx, pt2xy) = _FQ2Muc(pt1yx, pt1yy, 8); // 8 * y (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt1yx, pt1yy); // 8 * y * y (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt2zx, pt2zy); // 8 * y * y * S_squared (pt2yx, pt2yy) = _FQ2Sub(pt2yx, pt2yy, pt2xx, pt2xy); // newy = W * (4 * B - H) - 8 * y * y * S_squared (pt2xx, pt2xy) = _FQ2Muc(pt1xx, pt1xy, 2); // 2 * H (pt2xx, pt2xy) = _FQ2Mul(pt2xx, pt2xy, pt1zx, pt1zy); // newx = 2 * H * S (pt2zx, pt2zy) = _FQ2Mul(pt1zx, pt1zy, pt2zx, pt2zy); // S * S_squared (pt2zx, pt2zy) = _FQ2Muc(pt2zx, pt2zy, 8); // newz = 8 * S * S_squared } function _ECTwistMulJacobian( uint256 d, uint256 pt1xx, uint256 pt1xy, uint256 pt1yx, uint256 pt1yy, uint256 pt1zx, uint256 pt1zy ) internal pure returns (uint256[6] memory pt2) { while (d != 0) { if ((d & 1) != 0) { pt2 = _ECTwistAddJacobian( pt2[PTXX], pt2[PTXY], pt2[PTYX], pt2[PTYY], pt2[PTZX], pt2[PTZY], pt1xx, pt1xy, pt1yx, pt1yy, pt1zx, pt1zy); } ( pt1xx, pt1xy, pt1yx, pt1yy, pt1zx, pt1zy ) = _ECTwistDoubleJacobian( pt1xx, pt1xy, pt1yx, pt1yy, pt1zx, pt1zy ); d = d / 2; } } } // This file is MIT Licensed. // // Copyright 2017 Christian Reitwiessner // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pragma solidity ^0.8.0; library Pairing { struct G1Point { uint X; uint Y; } // Encoding of field elements is: X[0] * z + X[1] struct G2Point { uint[2] X; uint[2] Y; } /// @return the generator of G1 function P1() pure internal returns (G1Point memory) { return G1Point(1, 2); } /// @return the generator of G2 function P2() pure internal returns (G2Point memory) { return G2Point( [10857046999023057135944570762232829481370756359578518086990519993285655852781, 11559732032986387107991004021392285783925812861821192530917403151452391805634], [8495653923123431417604973247489272438418190587263600148770280649306958101930, 4082367875863433681332203403145435568316851327593401208105741076214120093531] ); } /// @return the negation of p, i.e. p.addition(p.negate()) should be zero. function negate(G1Point memory p) pure internal returns (G1Point memory) { // The prime q in the base field F_q for G1 uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; if (p.X == 0 && p.Y == 0) return G1Point(0, 0); return G1Point(p.X, q - (p.Y % q)); } /// @return r the sum of two points of G1 function addition(G1Point memory p1, G1Point memory p2) internal view returns (G1Point memory r) { uint[4] memory input; input[0] = p1.X; input[1] = p1.Y; input[2] = p2.X; input[3] = p2.Y; bool success; assembly { success := staticcall(sub(gas(), 2000), 6, input, 0xc0, r, 0x60) // Use "invalid" to make gas estimation work switch success case 0 { invalid() } } require(success); } /// @return r the sum of two points of G2 function addition(G2Point memory p1, G2Point memory p2) internal view returns (G2Point memory r) { (r.X[0], r.X[1], r.Y[0], r.Y[1]) = BN256G2.ECTwistAdd(p1.X[0],p1.X[1],p1.Y[0],p1.Y[1],p2.X[0],p2.X[1],p2.Y[0],p2.Y[1]); } /// @return r the product of a point on G1 and a scalar, i.e. /// p == p.scalar_mul(1) and p.addition(p) == p.scalar_mul(2) for all points p. function scalar_mul(G1Point memory p, uint s) internal view returns (G1Point memory r) { uint[3] memory input; input[0] = p.X; input[1] = p.Y; input[2] = s; bool success; assembly { success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60) // Use "invalid" to make gas estimation work switch success case 0 { invalid() } } require (success); } /// @return the result of computing the pairing check /// e(p1[0], p2[0]) * .... * e(p1[n], p2[n]) == 1 /// For example pairing([P1(), P1().negate()], [P2(), P2()]) should /// return true. function pairing(G1Point[] memory p1, G2Point[] memory p2) internal view returns (bool) { require(p1.length == p2.length); uint elements = p1.length; uint inputSize = elements * 6; uint[] memory input = new uint[](inputSize); for (uint i = 0; i < elements; i++) { input[i * 6 + 0] = p1[i].X; input[i * 6 + 1] = p1[i].Y; input[i * 6 + 2] = p2[i].X[1]; input[i * 6 + 3] = p2[i].X[0]; input[i * 6 + 4] = p2[i].Y[1]; input[i * 6 + 5] = p2[i].Y[0]; } uint[1] memory out; bool success; assembly { success := staticcall(sub(gas(), 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20) // Use "invalid" to make gas estimation work switch success case 0 { invalid() } } require(success); return out[0] != 0; } /// Convenience method for a pairing check for two pairs. function pairingProd2(G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2) internal view returns (bool) { G1Point[] memory p1 = new G1Point[](2); G2Point[] memory p2 = new G2Point[](2); p1[0] = a1; p1[1] = b1; p2[0] = a2; p2[1] = b2; return pairing(p1, p2); } /// Convenience method for a pairing check for three pairs. function pairingProd3( G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2, G1Point memory c1, G2Point memory c2 ) internal view returns (bool) { G1Point[] memory p1 = new G1Point[](3); G2Point[] memory p2 = new G2Point[](3); p1[0] = a1; p1[1] = b1; p1[2] = c1; p2[0] = a2; p2[1] = b2; p2[2] = c2; return pairing(p1, p2); } /// Convenience method for a pairing check for four pairs. function pairingProd4( G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2, G1Point memory c1, G2Point memory c2, G1Point memory d1, G2Point memory d2 ) internal view returns (bool) { G1Point[] memory p1 = new G1Point[](4); G2Point[] memory p2 = new G2Point[](4); p1[0] = a1; p1[1] = b1; p1[2] = c1; p1[3] = d1; p2[0] = a2; p2[1] = b2; p2[2] = c2; p2[3] = d2; return pairing(p1, p2); } } contract Verifier { using Pairing for *; struct VerifyingKey { Pairing.G2Point h; Pairing.G1Point g_alpha; Pairing.G2Point h_beta; Pairing.G1Point g_gamma; Pairing.G2Point h_gamma; Pairing.G1Point[] query; } struct Proof { Pairing.G1Point a; Pairing.G2Point b; Pairing.G1Point c; } function verifyingKey() pure internal returns (VerifyingKey memory vk) { vk.h= Pairing.G2Point([uint256(0x22a143b711ff4a90face484ff1496c2a008b70edb7ca94eb56d71ea91cccc2fa), uint256(0x20871b75c91ec5edcfced7c24ffdf041b4105f07fdf9155edd905e812e7f42cc)], [uint256(0x0785ab3ea76cee3828a9e1175c05c84283fa222ac3fce8191f87a82b64e31636), uint256(0x09096823f3bb349509a9c15dbba6e7eb5690dea94a81f5f97809a3ee934f6eab)]); vk.g_alpha = Pairing.G1Point(uint256(0x2d3d82c22c65b92a1b1235ff060c593c19e88535116e4f37030f1599ba0b0857), uint256(0x17d67e76fdad58f9a4bab028b87b1e69dc43b90d932c617874fcc6fd0e29e7fd)); vk.h_beta = Pairing.G2Point([uint256(0x04eaf701cae25b7d2f8c77341b87beae57726bd25292818f4c1968c76ccb9c14), uint256(0x12637630d3c9bcfa124bb60f9f1a370533f48a4227b3f73ab8d363c3c5bec243)], [uint256(0x0450bf92a0622add3f4c91cd9eaad4ebd6fbba315b21090992e3285e397a83fc), uint256(0x0bb827c12688c53d9559bee1769d68bdf279a84828ce44dbfd970aa7920065d4)]); vk.g_gamma = Pairing.G1Point(uint256(0x11fdbbc4227f2f61b37623ef81514c36ef27b1dc55cbed203dce97d108c7a005), uint256(0x281625f041042595d0c7d62f2ce0f958fc59cc4f0d051f6e587248e917e61e2c)); vk.h_gamma = Pairing.G2Point([uint256(0x22a143b711ff4a90face484ff1496c2a008b70edb7ca94eb56d71ea91cccc2fa), uint256(0x20871b75c91ec5edcfced7c24ffdf041b4105f07fdf9155edd905e812e7f42cc)], [uint256(0x0785ab3ea76cee3828a9e1175c05c84283fa222ac3fce8191f87a82b64e31636), uint256(0x09096823f3bb349509a9c15dbba6e7eb5690dea94a81f5f97809a3ee934f6eab)]); vk.query = new Pairing.G1Point[](14); vk.query[0] = Pairing.G1Point(uint256(0x1b947842fa89dd24916e414947c05670d9ba0dbcf6f9682f2e37f95507db0dc8), uint256(0x096cbf7107898a03be81bbec80e6d3772b3e01584d856dd4af7876fba6f90ea7)); vk.query[1] = Pairing.G1Point(uint256(0x1f732aaf3d2f205783612c1ecdde48e2ddfd082eda2c796950ef23af1fb44b8e), uint256(0x08edccc6e33c9ab7c81bf58033a217c7f65e2984ba0c8fea06e1ad1a916d699f)); vk.query[2] = Pairing.G1Point(uint256(0x1bd620813ca0ccfbe2701abc703c0c2a7ecabe39935145292bee3abc4e5ee157), uint256(0x1828f72b551ba67c9cd2c763c23426b117f850786af7dae5e2a957e0665b8474)); vk.query[3] = Pairing.G1Point(uint256(0x21914239a94e727d8ee788aecef446cba53a2e2596916a6946578999a2a07505), uint256(0x2218f81765b28773690e9e9b5be4ceb49c4472c514921dd222a12c718c61f121)); vk.query[4] = Pairing.G1Point(uint256(0x1246a97ee147f11d472967ae104e0797671557aad3954ef6fc4bebdb0a048bf9), uint256(0x2223e3b2e9a9bc94b8bd3061a1ba48d389666da3f7f884f93c041d0f40818282)); vk.query[5] = Pairing.G1Point(uint256(0x228ac68e80487613cabec4a0b235fda5c05fcc2bca8c367ba4ed6171924df3eb), uint256(0x1a36a43f0497edadbf08039f21fb55b99d48a8f69936a51c87d03893439310a5)); vk.query[6] = Pairing.G1Point(uint256(0x1a6e4aeb8628b47ba9992a931bf2def678060f36a16f141bd649b056321ab615), uint256(0x03b558e2c5ecd75194b4394abce234d886f9701ed5946edbb16e4e7f07ec0ba4)); vk.query[7] = Pairing.G1Point(uint256(0x03349d90dcad9369885487dee75837ab40c0e8ade24a686feceae3fc0953469b), uint256(0x0cb0ce11ba655c3f2a97e599be74a5779642a72d6788d204410e54042fdfebb8)); vk.query[8] = Pairing.G1Point(uint256(0x2c1db29c185b5bef50139e20e934bbac9f42af267daf64fc1e375bd1209cb365), uint256(0x0228adc7030bbdf75ebd6bd69e6f7e99389d9a27d8eae0516dcd7039d80c6154)); vk.query[9] = Pairing.G1Point(uint256(0x2163212e52702be32b7bce81c67701cd98ad83e6badc003a3b5a59eee12558ec), uint256(0x005b17d0532a07966787f8e654cf0497420950a5a5dbe948fe4191b83ab0bcd3)); vk.query[10] = Pairing.G1Point(uint256(0x141e3358d23685f57978419d07fccd67c9e6981c26adf5f34fea035c3ebc6e89), uint256(0x2abd0f7542f02165c7282026e57bdcd9f8e44afb3770bd9cf5c3ae3356c6a89c)); vk.query[11] = Pairing.G1Point(uint256(0x140774a33658eaa7f5d195972a8e176ca26c26e7d6cd6fe1ea7f837c81f082cf), uint256(0x0d084ac16c74a3095017e619a1fdd1edc4b57b6cd4105e8a4f72ffc772ce49c0)); vk.query[12] = Pairing.G1Point(uint256(0x044775db49f089912ab525e4565ef30c592d77f53ca0ac67260ea8f693ed9d07), uint256(0x151f986852bec8685540f1c82e0a0107e0e255e2e684d603516636e7512c6bc0)); vk.query[13] = Pairing.G1Point(uint256(0x29822830733b283435c231b094b6c363c6ff95e266073f0eec9c1d91c475258d), uint256(0x0e41c9ad81ec496d63aeff4a937c302340fee326963d793348e6fe8094c43ee2)); } function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; VerifyingKey memory vk = verifyingKey(); require(input.length + 1 == vk.query.length); // Compute the linear combination vk_x Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0); for (uint i = 0; i < input.length; i++) { require(input[i] < snark_scalar_field); vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.query[i + 1], input[i])); } vk_x = Pairing.addition(vk_x, vk.query[0]); /** * e(A*G^{alpha}, B*H^{beta}) = e(G^{alpha}, H^{beta}) * e(G^{psi}, H^{gamma}) * * e(C, H) * where psi = \sum_{i=0}^l input_i pvk.query[i] */ if (!Pairing.pairingProd4(vk.g_alpha, vk.h_beta, vk_x, vk.h_gamma, proof.c, vk.h, Pairing.negate(Pairing.addition(proof.a, vk.g_alpha)), Pairing.addition(proof.b, vk.h_beta))) return 1; /** * e(A, H^{gamma}) = e(G^{gamma}, B) */ if (!Pairing.pairingProd2(proof.a, vk.h_gamma, Pairing.negate(vk.g_gamma), proof.b)) return 2; return 0; } function verifyTx( Proof memory proof, uint[13] memory input ) public view returns (bool r) { uint[] memory inputValues = new uint[](13); for(uint i = 0; i < input.length; i++){ inputValues[i] = input[i]; } if (verify(inputValues, proof) == 0) { return true; } else { return false; } } }