mirror of
https://github.com/CryptKeeperZK/snarkjs.git
synced 2026-01-07 22:44:02 -05:00
Refactor groth16 smart contract (#372)
* Plonk refactored with new clsses and working * change logger.in -> logger.debug * update protocol to last version * Refactor Fiat-Shamir adding preprocessed circuit dependent values * Add check functions in js and sc * refactor groth16 smart contract to assembly code * fix calldata types and test passing * fix errors. working * wip * add debug function * remove console.log import * Plonk smart contracte refactored. * Added test with circuits using +1 inputs * Added cricuits tests with +1 inputs * mod package.json * Plonk refactored with new clsses and working * change logger.in -> logger.debug * update protocol to last version * Refactor Fiat-Shamir adding preprocessed circuit dependent values * fix errors. working * wip * add debug function * Plonk smart contracte refactored. * Added cricuits tests with +1 inputs * mod package.json * update ffjavascript to 0.2.59 * Add check functions in js and sc * refactor groth16 smart contract to assembly code * fix calldata types and test passing * remove console.log import * add scalar import * Add check functions in js and sc * refactor groth16 smart contract to assembly code * fix calldata types and test passing * remove console.log import * add scalar import * remove hardhat import --------- Co-authored-by: Jordi Baylina <jordi@baylina.cat>
This commit is contained in:
91
.vscode/launch.json
vendored
91
.vscode/launch.json
vendored
@@ -12,6 +12,97 @@
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"type": "node"
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "groth16 setup",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"program": "cli.js",
|
||||
"args": [
|
||||
"groth16", "setup",
|
||||
"test/groth16/circuit.r1cs",
|
||||
"test/plonk_circuit/powersOfTau15_final.ptau",
|
||||
"test/groth16/circuit.zkey"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "groth16 prove",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"program": "cli.js",
|
||||
"args": [
|
||||
"groth16", "prove",
|
||||
"test/groth16/circuit.zkey",
|
||||
"test/groth16/witness.wtns",
|
||||
"test/groth16/proof.json",
|
||||
"test/groth16/public.json",
|
||||
"-v"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "groth16 export vk",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"program": "cli.js",
|
||||
"args": [
|
||||
"zkev",
|
||||
"test/groth16/circuit.zkey",
|
||||
"test/groth16/verification_key.json",
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "groth16 verify",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"program": "cli.js",
|
||||
"args": [
|
||||
"groth16", "verify",
|
||||
"test/groth16/verification_key.json",
|
||||
"test/groth16/public.json",
|
||||
"test/groth16/proof.json",
|
||||
"-v"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "groth16 solidity verifier",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"program": "cli.js",
|
||||
"args": [
|
||||
"zkesv",
|
||||
"test/groth16/circuit.zkey",
|
||||
"test/groth16/verifier.sol",
|
||||
]
|
||||
},
|
||||
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "groth16 solidity calldata",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"program": "cli.js",
|
||||
"args": [
|
||||
"zkesc",
|
||||
"test/groth16/public.json",
|
||||
"test/groth16/proof.json",
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
|
||||
@@ -94,7 +94,7 @@ describe("Smart contracts test suite", function () {
|
||||
await run("compile");
|
||||
|
||||
// Deploy mock groth16 verifier
|
||||
const VerifierFactory = await ethers.getContractFactory("Verifier");
|
||||
const VerifierFactory = await ethers.getContractFactory("Groth16Verifier");
|
||||
verifierContract = await VerifierFactory.deploy();
|
||||
|
||||
return await verifierContract.verifyProof(proofA, proofB, proofC, publicInputs);
|
||||
|
||||
@@ -21,6 +21,7 @@ import * as curves from "./curves.js";
|
||||
import { BigBuffer, utils } from "ffjavascript";
|
||||
import { Proof } from "./proof.js";
|
||||
import { Keccak256Transcript } from "./Keccak256Transcript.js";
|
||||
import { Scalar } from "ffjavascript";
|
||||
|
||||
const { unstringifyBigInts } = utils;
|
||||
|
||||
@@ -61,15 +62,23 @@ export default async function fflonkVerify(_vk_verifier, _publicSignals, _proof,
|
||||
// STEP 1 - Validate that all polynomial commitments ∈ G_1
|
||||
if (logger) logger.info("> Checking commitments belong to G1");
|
||||
if (!commitmentsBelongToG1(curve, proof, vk)) {
|
||||
logger.error("Proof is not well constructed");
|
||||
if (logger) logger.error("Proof commitments are not valid");
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO
|
||||
// STEP 2 - Validate that all evaluations ∈ F
|
||||
if (logger) logger.info("> Checking evaluations belong to F");
|
||||
if (!evaluationsAreValid(curve, proof)) {
|
||||
if (logger) logger.error("Proof evaluations are not valid.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO
|
||||
// STEP 3 - Validate that w_i ∈ F for i ∈ [l]
|
||||
if (logger) logger.info("> Checking public inputs belong to F");
|
||||
if (!publicInputsAreValid(curve, publicSignals)) {
|
||||
if (logger) logger.error("Public inputs are not valid.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// STEP 4 - Compute the challenges: beta, gamma, xi, alpha and y ∈ F
|
||||
// as in prover description, from the common preprocessed inputs, public inputs and elements of π_SNARK
|
||||
@@ -151,6 +160,41 @@ function commitmentsBelongToG1(curve, proof, vk) {
|
||||
&& G1.isValid(vk.C0);
|
||||
}
|
||||
|
||||
function checkValueBelongToField(curve, value) {
|
||||
return Scalar.lt(value, curve.r);
|
||||
}
|
||||
|
||||
function checkEvaluationIsValid(curve, evaluation) {
|
||||
return checkValueBelongToField(curve, Scalar.fromRprLE(evaluation));
|
||||
}
|
||||
|
||||
function evaluationsAreValid(curve, proof) {
|
||||
return checkEvaluationIsValid(curve, proof.evaluations.ql)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.qr)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.qm)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.qo)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.qc)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.s1)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.s2)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.s3)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.a)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.b)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.c)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.z)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.zw)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.t1w)
|
||||
&& checkEvaluationIsValid(curve, proof.evaluations.t2w);
|
||||
}
|
||||
|
||||
function publicInputsAreValid(curve, publicInputs) {
|
||||
for(let i = 0; i < publicInputs.length; i++) {
|
||||
if(!checkValueBelongToField(curve, publicInputs[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function computeChallenges(curve, proof, vk, publicSignals, logger) {
|
||||
const Fr = curve.Fr;
|
||||
|
||||
|
||||
@@ -41,6 +41,11 @@ export default async function groth16Verify(_vk_verifier, _publicSignals, _proof
|
||||
const IC = new Uint8Array(curve.G1.F.n8*2 * publicSignals.length);
|
||||
const w = new Uint8Array(curve.Fr.n8 * publicSignals.length);
|
||||
|
||||
if (!publicInputsAreValid(curve, publicSignals)) {
|
||||
if (logger) logger.error("Public inputs are not valid.");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i=0; i<publicSignals.length; i++) {
|
||||
const buffP = curve.G1.fromObject(vk_verifier.IC[i+1]);
|
||||
IC.set(buffP, i*curve.G1.F.n8*2);
|
||||
@@ -54,6 +59,11 @@ export default async function groth16Verify(_vk_verifier, _publicSignals, _proof
|
||||
const pi_b = curve.G2.fromObject(proof.pi_b);
|
||||
const pi_c = curve.G1.fromObject(proof.pi_c);
|
||||
|
||||
if (!isWellConstructed(curve, {pi_a, pi_b, pi_c})) {
|
||||
if(logger) logger.error("Proof commitments are not valid.");
|
||||
return false;
|
||||
}
|
||||
|
||||
const vk_gamma_2 = curve.G2.fromObject(vk_verifier.vk_gamma_2);
|
||||
const vk_delta_2 = curve.G2.fromObject(vk_verifier.vk_delta_2);
|
||||
const vk_alpha_1 = curve.G1.fromObject(vk_verifier.vk_alpha_1);
|
||||
@@ -75,3 +85,21 @@ export default async function groth16Verify(_vk_verifier, _publicSignals, _proof
|
||||
if (logger) logger.info("OK!");
|
||||
return true;
|
||||
}
|
||||
|
||||
function isWellConstructed(curve, proof) {
|
||||
const G1 = curve.G1;
|
||||
const G2 = curve.G2;
|
||||
|
||||
return G1.isValid(proof.pi_a)
|
||||
&& G2.isValid(proof.pi_b)
|
||||
&& G1.isValid(proof.pi_c);
|
||||
}
|
||||
|
||||
function publicInputsAreValid(curve, publicInputs) {
|
||||
for(let i = 0; i < publicInputs.length; i++) {
|
||||
if(!Scalar.lt(publicInputs[i], curve.r)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,250 +1,165 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// 2019 OKIMS
|
||||
// ported to solidity 0.6
|
||||
// fixed linter warnings
|
||||
// added requiere error messages
|
||||
//
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
pragma solidity ^0.6.11;
|
||||
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() internal pure returns (G1Point memory) {
|
||||
return G1Point(1, 2);
|
||||
}
|
||||
/// @return the generator of G2
|
||||
function P2() internal pure returns (G2Point memory) {
|
||||
// Original code point
|
||||
return G2Point(
|
||||
[11559732032986387107991004021392285783925812861821192530917403151452391805634,
|
||||
10857046999023057135944570762232829481370756359578518086990519993285655852781],
|
||||
[4082367875863433681332203403145435568316851327593401208105741076214120093531,
|
||||
8495653923123431417604973247489272438418190587263600148770280649306958101930]
|
||||
);
|
||||
|
||||
/*
|
||||
// Changed by Jordi point
|
||||
return G2Point(
|
||||
[10857046999023057135944570762232829481370756359578518086990519993285655852781,
|
||||
11559732032986387107991004021392285783925812861821192530917403151452391805634],
|
||||
[8495653923123431417604973247489272438418190587263600148770280649306958101930,
|
||||
4082367875863433681332203403145435568316851327593401208105741076214120093531]
|
||||
);
|
||||
*/
|
||||
}
|
||||
/// @return r the negation of p, i.e. p.addition(p.negate()) should be zero.
|
||||
function negate(G1Point memory p) internal pure returns (G1Point memory r) {
|
||||
// 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;
|
||||
// solium-disable-next-line security/no-inline-assembly
|
||||
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,"pairing-add-failed");
|
||||
}
|
||||
/// @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;
|
||||
// solium-disable-next-line security/no-inline-assembly
|
||||
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,"pairing-mul-failed");
|
||||
}
|
||||
/// @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,"pairing-lengths-failed");
|
||||
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[0];
|
||||
input[i * 6 + 3] = p2[i].X[1];
|
||||
input[i * 6 + 4] = p2[i].Y[0];
|
||||
input[i * 6 + 5] = p2[i].Y[1];
|
||||
}
|
||||
uint[1] memory out;
|
||||
bool success;
|
||||
// solium-disable-next-line security/no-inline-assembly
|
||||
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,"pairing-opcode-failed");
|
||||
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.G1Point alfa1;
|
||||
Pairing.G2Point beta2;
|
||||
Pairing.G2Point gamma2;
|
||||
Pairing.G2Point delta2;
|
||||
Pairing.G1Point[] IC;
|
||||
}
|
||||
struct Proof {
|
||||
Pairing.G1Point A;
|
||||
Pairing.G2Point B;
|
||||
Pairing.G1Point C;
|
||||
}
|
||||
function verifyingKey() internal pure returns (VerifyingKey memory vk) {
|
||||
vk.alfa1 = Pairing.G1Point(
|
||||
<%=vk_alpha_1[0]%>,
|
||||
<%=vk_alpha_1[1]%>
|
||||
);
|
||||
Copyright 2021 0KIMS association.
|
||||
|
||||
vk.beta2 = Pairing.G2Point(
|
||||
[<%=vk_beta_2[0][1]%>,
|
||||
<%=vk_beta_2[0][0]%>],
|
||||
[<%=vk_beta_2[1][1]%>,
|
||||
<%=vk_beta_2[1][0]%>]
|
||||
);
|
||||
vk.gamma2 = Pairing.G2Point(
|
||||
[<%=vk_gamma_2[0][1]%>,
|
||||
<%=vk_gamma_2[0][0]%>],
|
||||
[<%=vk_gamma_2[1][1]%>,
|
||||
<%=vk_gamma_2[1][0]%>]
|
||||
);
|
||||
vk.delta2 = Pairing.G2Point(
|
||||
[<%=vk_delta_2[0][1]%>,
|
||||
<%=vk_delta_2[0][0]%>],
|
||||
[<%=vk_delta_2[1][1]%>,
|
||||
<%=vk_delta_2[1][0]%>]
|
||||
);
|
||||
vk.IC = new Pairing.G1Point[](<%=IC.length%>);
|
||||
<% for (let i=0; i<IC.length; i++) { %>
|
||||
vk.IC[<%=i%>] = Pairing.G1Point(
|
||||
<%=IC[i][0]%>,
|
||||
<%=IC[i][1]%>
|
||||
);
|
||||
<% } %>
|
||||
}
|
||||
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.IC.length,"verifier-bad-input");
|
||||
// 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,"verifier-gte-snark-scalar-field");
|
||||
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.IC[i + 1], input[i]));
|
||||
}
|
||||
vk_x = Pairing.addition(vk_x, vk.IC[0]);
|
||||
if (!Pairing.pairingProd4(
|
||||
Pairing.negate(proof.A), proof.B,
|
||||
vk.alfa1, vk.beta2,
|
||||
vk_x, vk.gamma2,
|
||||
proof.C, vk.delta2
|
||||
)) return 1;
|
||||
return 0;
|
||||
}
|
||||
/// @return r bool true if proof is valid
|
||||
function verifyProof(
|
||||
uint[2] memory a,
|
||||
uint[2][2] memory b,
|
||||
uint[2] memory c,
|
||||
uint[<%=IC.length-1%>] memory input
|
||||
) public view returns (bool r) {
|
||||
Proof memory proof;
|
||||
proof.A = Pairing.G1Point(a[0], a[1]);
|
||||
proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]);
|
||||
proof.C = Pairing.G1Point(c[0], c[1]);
|
||||
uint[] memory inputValues = new uint[](input.length);
|
||||
for(uint i = 0; i < input.length; i++){
|
||||
inputValues[i] = input[i];
|
||||
}
|
||||
if (verify(inputValues, proof) == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
This file is generated with [snarkJS](https://github.com/iden3/snarkjs).
|
||||
|
||||
snarkJS is a free software: you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
snarkJS is distributed in the hope that it will be useful, but WITHOUT
|
||||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with snarkJS. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
pragma solidity >=0.7.0 <0.9.0;
|
||||
|
||||
contract Groth16Verifier {
|
||||
// Scalar field size
|
||||
uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
|
||||
// Base field size
|
||||
uint256 constant q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
|
||||
|
||||
// Verification Key data
|
||||
uint256 constant alphax = <%= vk_alpha_1[0] %>;
|
||||
uint256 constant alphay = <%= vk_alpha_1[1] %>;
|
||||
uint256 constant betax1 = <%= vk_beta_2[0][1] %>;
|
||||
uint256 constant betax2 = <%= vk_beta_2[0][0] %>;
|
||||
uint256 constant betay1 = <%= vk_beta_2[1][1] %>;
|
||||
uint256 constant betay2 = <%= vk_beta_2[1][0] %>;
|
||||
uint256 constant gammax1 = <%= vk_gamma_2[0][1] %>;
|
||||
uint256 constant gammax2 = <%= vk_gamma_2[0][0] %>;
|
||||
uint256 constant gammay1 = <%= vk_gamma_2[1][1] %>;
|
||||
uint256 constant gammay2 = <%= vk_gamma_2[1][0] %>;
|
||||
uint256 constant deltax1 = <%= vk_delta_2[0][1] %>;
|
||||
uint256 constant deltax2 = <%= vk_delta_2[0][0] %>;
|
||||
uint256 constant deltay1 = <%= vk_delta_2[1][1] %>;
|
||||
uint256 constant deltay2 = <%= vk_delta_2[1][0] %>;
|
||||
|
||||
<% for (let i=0; i<IC.length; i++) { %>
|
||||
uint256 constant IC<%=i%>x = <%=IC[i][0]%>;
|
||||
uint256 constant IC<%=i%>y = <%=IC[i][1]%>;
|
||||
<% } %>
|
||||
|
||||
// Memory data
|
||||
uint16 constant pVk = 0;
|
||||
uint16 constant pPairing = 128;
|
||||
|
||||
uint16 constant pLastMem = 896;
|
||||
|
||||
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[<%=IC.length-1%>] calldata _pubSignals) public view returns (bool) {
|
||||
assembly {
|
||||
function checkField(v) {
|
||||
if iszero(lt(v, q)) {
|
||||
mstore(0, 0)
|
||||
return(0, 0x20)
|
||||
}
|
||||
}
|
||||
|
||||
// G1 function to multiply a G1 value(x,y) to value in an address
|
||||
function g1_mulAccC(pR, x, y, s) {
|
||||
let success
|
||||
let mIn := mload(0x40)
|
||||
mstore(mIn, x)
|
||||
mstore(add(mIn, 32), y)
|
||||
mstore(add(mIn, 64), s)
|
||||
|
||||
success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64)
|
||||
|
||||
if iszero(success) {
|
||||
mstore(0, 0)
|
||||
return(0, 0x20)
|
||||
}
|
||||
|
||||
mstore(add(mIn, 64), mload(pR))
|
||||
mstore(add(mIn, 96), mload(add(pR, 32)))
|
||||
|
||||
success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64)
|
||||
|
||||
if iszero(success) {
|
||||
mstore(0, 0)
|
||||
return(0, 0x20)
|
||||
}
|
||||
}
|
||||
|
||||
function checkPairing(pA, pB, pC, pubSignals, pMem) -> isOk {
|
||||
let _pPairing := add(pMem, pPairing)
|
||||
let _pVk := add(pMem, pVk)
|
||||
|
||||
mstore(_pVk, IC0x)
|
||||
mstore(add(_pVk, 32), IC0y)
|
||||
|
||||
// Compute the linear combination vk_x
|
||||
<% for (let i = 1; i <= nPublic; i++) { %>
|
||||
g1_mulAccC(_pVk, IC<%=i%>x, IC<%=i%>y, calldataload(add(pubSignals, <%=(i-1)*32%>)))
|
||||
<% } %>
|
||||
|
||||
// -A
|
||||
mstore(_pPairing, calldataload(pA))
|
||||
mstore(add(_pPairing, 32), mod(sub(q, calldataload(add(pA, 32))), q))
|
||||
|
||||
// B
|
||||
mstore(add(_pPairing, 64), calldataload(pB))
|
||||
mstore(add(_pPairing, 96), calldataload(add(pB, 32)))
|
||||
mstore(add(_pPairing, 128), calldataload(add(pB, 64)))
|
||||
mstore(add(_pPairing, 160), calldataload(add(pB, 96)))
|
||||
|
||||
// alpha1
|
||||
mstore(add(_pPairing, 192), alphax)
|
||||
mstore(add(_pPairing, 224), alphay)
|
||||
|
||||
// beta2
|
||||
mstore(add(_pPairing, 256), betax1)
|
||||
mstore(add(_pPairing, 288), betax2)
|
||||
mstore(add(_pPairing, 320), betay1)
|
||||
mstore(add(_pPairing, 352), betay2)
|
||||
|
||||
// vk_x
|
||||
mstore(add(_pPairing, 384), mload(add(pMem, pVk)))
|
||||
mstore(add(_pPairing, 416), mload(add(pMem, add(pVk, 32))))
|
||||
|
||||
|
||||
// gamma2
|
||||
mstore(add(_pPairing, 448), gammax1)
|
||||
mstore(add(_pPairing, 480), gammax2)
|
||||
mstore(add(_pPairing, 512), gammay1)
|
||||
mstore(add(_pPairing, 544), gammay2)
|
||||
|
||||
// C
|
||||
mstore(add(_pPairing, 576), calldataload(pC))
|
||||
mstore(add(_pPairing, 608), calldataload(add(pC, 32)))
|
||||
|
||||
// delta2
|
||||
mstore(add(_pPairing, 640), deltax1)
|
||||
mstore(add(_pPairing, 672), deltax2)
|
||||
mstore(add(_pPairing, 704), deltay1)
|
||||
mstore(add(_pPairing, 736), deltay2)
|
||||
|
||||
|
||||
let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20)
|
||||
|
||||
isOk := and(success, mload(_pPairing))
|
||||
}
|
||||
|
||||
let pMem := mload(0x40)
|
||||
mstore(0x40, add(pMem, pLastMem))
|
||||
|
||||
// Validate that all evaluations ∈ F
|
||||
<% for (let i=0; i<IC.length; i++) { %>
|
||||
checkField(calldataload(add(_pubSignals, <%=i*32%>)))
|
||||
<% } %>
|
||||
|
||||
// Validate all evaluations
|
||||
let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem)
|
||||
|
||||
mstore(0, isValid)
|
||||
return(0, 0x20)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -707,4 +707,4 @@ contract PlonkVerifier {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user