Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 184 additions & 0 deletions circuits/batchverifier1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// SPDX-License-Identifier: GPL-3.0
/*
Copyright 2021 0KIMS association.

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 = 20491192805390485299153009773594534940189261866228447918068658471970481763042;
uint256 constant alphay = 9383485363053290200918347156157836566562967994039712273449902621266178545958;
uint256 constant betax1 = 4252822878758300859123897981450591353533073413197771768651442665752259397132;
uint256 constant betax2 = 6375614351688725206403948262868962793625744043794305715222011528459656738731;
uint256 constant betay1 = 21847035105528745403288232691147584728191162732299865338377159692350059136679;
uint256 constant betay2 = 10505242626370262277552901082094356697409835680220590971873171140371331206856;
uint256 constant gammax1 = 11559732032986387107991004021392285783925812861821192530917403151452391805634;
uint256 constant gammax2 = 10857046999023057135944570762232829481370756359578518086990519993285655852781;
uint256 constant gammay1 = 4082367875863433681332203403145435568316851327593401208105741076214120093531;
uint256 constant gammay2 = 8495653923123431417604973247489272438418190587263600148770280649306958101930;
uint256 constant deltax1 = 4435469043957487417038141289131269923434297458057904100693671256286197273892;
uint256 constant deltax2 = 9827808285106497637568464631261223348400727783807511472110832690770975906682;
uint256 constant deltay1 = 15253496319533498224152796222859543029296185536582117816663985779482027451893;
uint256 constant deltay2 = 16115776549853112567778588688875482453470603144224787492104891280864335611844;


uint256 constant IC0x = 4224902222700994387480180980949991613269784287044266770963953844274825715580;
uint256 constant IC0y = 15248044047721673131945741692447140601446067264346863759081082058477172982853;

uint256 constant IC1x = 12193790083378503268158446849147034507335605541327707516017635499684920886378;
uint256 constant IC1y = 7967970252678974542390531080772135934618637463391054340505721868408220308185;

uint256 constant IC2x = 15610582805480723294398004134844248724660027627130348591964937122755749540673;
uint256 constant IC2y = 11833854975233496239957752057959035235664172599121732606884834560742390605813;

uint256 constant IC3x = 2367290916347245572941520239995303640341559606640617243140868238435957449000;
uint256 constant IC3y = 11526665179101151714026575885584763832977431032111032110698564957745014368228;


// 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[3] 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

g1_mulAccC(_pVk, IC1x, IC1y, calldataload(add(pubSignals, 0)))

g1_mulAccC(_pVk, IC2x, IC2y, calldataload(add(pubSignals, 32)))

g1_mulAccC(_pVk, IC3x, IC3y, calldataload(add(pubSignals, 64)))


// -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

checkField(calldataload(add(_pubSignals, 0)))

checkField(calldataload(add(_pubSignals, 32)))

checkField(calldataload(add(_pubSignals, 64)))

checkField(calldataload(add(_pubSignals, 96)))


// Validate all evaluations
let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem)

mstore(0, isValid)
return(0, 0x20)
}
}
}
28 changes: 28 additions & 0 deletions circuits/proof_10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"pi_a": [
"2541659953744083808330930972707283351117270591546390083371398064307987700923",
"17751065425028628250223072509464696856069976025607646697402691240926639469703",
"1"
],
"pi_b": [
[
"5268039866799958069412122942901958991561063692670000129082404874911686125876",
"7573293306007867592417339143828356963780899406296444354484599866292612507039"
],
[
"7468299507989951581851534327283021853879609238626059039602962768640376427629",
"16760699754142298984957622306754507516267821078021413652344549142441107337448"
],
[
"1",
"0"
]
],
"pi_c": [
"18392589399102775185935395616022675791927724844494919033175406302003150040462",
"109993435133455740084964529248870978403750162864577547068002437045619504862",
"1"
],
"protocol": "groth16",
"curve": "bn128"
}
5 changes: 5 additions & 0 deletions circuits/public_10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"0",
"130306987671880172849933313811916572573",
"133424297749271722791103495121087598259"
]
1 change: 1 addition & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"msg":[[111,8,2,17,18,1,0,0,0,0,0,0,34,72,10,32,68,135,192,117,33,132,100,227,120,233,227,68,125,253,73,58,2,201,186,106,217,242,39,184,84,199,169,254,59,161,167,13,18,36,8,1,18,32,31,61,8,196,243,240,219,3,13,215,23,57,62,151,208,97,26,67,210,170,83,232,116,11,54,41,0,124,97,234,210,101,42,12,8,248,160,206,174,6,16,128,128,128,128,1,50,10,99,101,110,116,97,117,114,105,45,49]],"sigs":[[42,179,247,118,182,122,236,16,97,93,233,64,178,31,191,227,154,133,10,183,87,38,198,204,240,248,13,50,158,78,132,73,234,59,47,67,210,220,223,217,138,161,37,139,163,94,121,87,108,229,58,8,84,27,102,40,63,129,108,175,205,88,141,12]],"pubkeys":[[89,124,200,238,30,223,96,186,66,105,103,103,187,156,20,61,45,57,36,123,251,21,194,132,152,150,186,164,97,19,87,32]]}
40 changes: 33 additions & 7 deletions test/testH2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const wasmTester = require("circom_tester").wasm;
const crypto = require("crypto");
const utils = require("./utils");
const { bigInt } = require("fast-check");
const { writeFileSync } = require("fs");
const { writeFileSync, readFileSync } = require("fs");

describe("Test H2", () => {
describe("when testing against one test vectors with 16 bits of message", () => {
Expand All @@ -13,16 +13,41 @@ describe("Test H2", () => {
path.join(__dirname, "circuits", "testH2.circom")
);


const fileData = readFileSync(`./data.json`, "utf-8");
const inputJson2 = JSON.parse(fileData);
console.log("inputJson2:", inputJson2);

const msgs = [Buffer.from(new Uint8Array(inputJson2.msg[0])).reverse()];
let sigs = [new Uint8Array(inputJson2.sigs[0])];
let public_keys = [new Uint8Array(inputJson2.pubkeys[0])];
console.log("msgs1:", msgs);
console.log("sigs2:", sigs);
console.log("public_keys3:", public_keys);

// get input data
const { msgs, sigs, public_keys } = utils.get_input_data(4);
// const { msgs, sigs, public_keys } = utils.get_input_data(4);



// // get input json
// console.log("msgs:", msgs[0]);
// console.log("sigs:", sigs);
// console.log("public_keys:", public_keys);



// get input json
const { bitsMsg, bitsS, PointA, PointR } = utils.get_input(
msgs,
sigs,
public_keys
);

// console.log("bitsMsg:", bitsMsg);
// console.log("bitsS:", bitsS);
// console.log("PointA:", PointA);
// console.log("PointR:", PointR);

for (let i in PointA) {
if (PointA[i].length == 4) {
for (let j in PointA[i]) {
Expand Down Expand Up @@ -70,12 +95,13 @@ describe("Test H2", () => {
};

// write batchinput.json to a file
writeFileSync(`./batchinput.json`, JSON.stringify(inputJson, null, 2));
// writeFileSync(`./batchinputNikita.json`, JSON.stringify(inputJson, null, 2));
writeFileSync(`./circuits/batchverify_js/batchinput.json`, JSON.stringify(inputJson, null, 2));

const witness = await cir.calculateWitness(inputJson, true);
// console.log("witness:", witness);
// const witness = await cir.calculateWitness(inputJson, true);
// // console.log("witness:", witness);

assert.ok(witness[3] === 0n);
// assert.ok(witness[3] === 0n);
});
});
});
20 changes: 17 additions & 3 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,23 @@ function get_input(msg, sigs, public_keys) {
}

function get_input_data(n) {
const msgs = [];
const sigs = [];
const public_keys = [];
// const msgs = [];
// const sigs = [];
// const public_keys = [];




const msgs = [Buffer.from(new Uint8Array([111, 8, 2, 17, 36, 0, 0, 0, 0, 0, 0, 0, 34, 72, 10, 32, 66, 15, 32, 152, 208, 7, 69, 219, 166, 26, 244, 231, 127, 247, 196, 94, 155, 127, 207, 55, 136, 84, 147, 228, 153, 98, 60, 132, 217, 5, 138, 158, 18, 36, 8, 1, 18, 32, 11, 240, 43, 121, 197, 33, 157, 232, 109, 180, 116, 4, 121, 90, 247, 22, 40, 58, 232, 238, 206, 228, 227, 85, 28, 80, 93, 168, 110, 223, 49, 183, 42, 12, 8, 176, 194, 170, 174, 6, 16, 144, 206, 240, 168, 2, 50, 10, 99, 101, 110, 116, 97, 117, 114, 105, 45, 49])).reverse()];
let sigs = [new Uint8Array([101, 168, 183, 134, 143, 54, 1, 63, 148, 125, 195, 152, 170, 120, 195, 218, 68, 126, 139, 93, 180, 148, 179, 82, 108, 189, 188, 127, 111, 147, 233, 36, 70, 169, 214, 104, 165, 109, 216, 165, 7, 246, 111, 41, 104, 152, 168, 234, 251, 37, 43, 20, 57, 185, 154, 38, 159, 204, 186, 76, 153, 87, 85, 14])];
let public_keys = [new Uint8Array([16, 147, 45, 80, 193, 165, 204, 196, 89, 59, 167, 137, 249, 34, 61, 211, 57, 184, 100, 157, 6, 199, 160, 237, 149, 254, 14, 202, 131, 61, 183, 163])];

return {
msgs,
sigs,
public_keys,
};

if (n < 1) {
n = 1;
}
Expand Down