Skip to content

Commit

Permalink
Merge branch 'master' into fix/temp-update-request
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Nov 6, 2024
2 parents bf871f4 + 13d80ab commit 9ebda74
Show file tree
Hide file tree
Showing 25 changed files with 577 additions and 86 deletions.
14 changes: 12 additions & 2 deletions contracts/lib/VerifierLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import {ICircuitValidator} from "../interfaces/ICircuitValidator.sol";
* @dev A library for writing proof results.
*/
library VerifierLib {
/// @dev Struct to store ZKP proof and associated data
struct Proof {
bool isVerified;
mapping(string key => uint256 inputValue) storageFields;
string validatorVersion;
uint256 blockNumber;
uint256 blockTimestamp;
mapping(string key => bytes) metadata;
}

/**
* @dev Writes proof results.
* @param self The ZKPVerifierStorage storage pointer
Expand All @@ -24,7 +34,7 @@ library VerifierLib {
ICircuitValidator.KeyToInputIndex[] memory keyToInpIdxs,
uint256[] memory inputs
) public {
ZKPVerifierBase.Proof storage proof = self._proofs[sender][requestId];
Proof storage proof = self._proofs[sender][requestId];
for (uint256 i = 0; i < keyToInpIdxs.length; i++) {
proof.storageFields[keyToInpIdxs[i].key] = inputs[keyToInpIdxs[i].inputIndex];
}
Expand All @@ -48,7 +58,7 @@ library VerifierLib {
uint64 requestId,
ICircuitValidator.Signal[] memory signals
) public {
ZKPVerifierBase.Proof storage proof = self._proofs[sender][requestId];
Proof storage proof = self._proofs[sender][requestId];
for (uint256 i = 0; i < signals.length; i++) {
proof.storageFields[signals[i].name] = signals[i].value;
}
Expand Down
206 changes: 206 additions & 0 deletions contracts/lib/groth16-verifiers/Groth16VerifierAuthV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// 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.8.4 <0.9.0;
contract Groth16VerifierAuthV2 {
// Scalar field size
uint256 constant r =
21888242871839275222246405745257275088548364400416034343698204186575808495617;
// Base field size
uint256 constant q =
21888242871839275222246405745257275088696311157297823662689037894645226208583;

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 =
13386788725021602198567425385006899728203544659933593917276469726154154017730;
uint256 constant deltax2 =
15934125614912710821614323121670433574627734468332981610453472911976383177228;
uint256 constant deltay1 =
13205305607413475134301212820100793870092003365382735436692046794406857938024;
uint256 constant deltay2 =
8759505107016263108323717548646403750748432711908544803866765373342463765424;

uint256 constant IC0x =
12385314984359904314257455036963499193805822249900169493212773820637861017270;
uint256 constant IC0y =
13455871848617958073752171682190449799364399689372987044617812281838570851280;

uint256 constant IC1x =
1493564767784757620464057507283285365409721187164502463730502309417194080296;
uint256 constant IC1y =
6377944811748764752279954590131952700069491229367911408873461121555475171995;

uint256 constant IC2x =
17810471156883173964067651564103955395454521925125801510057769541384109536787;
uint256 constant IC2y =
5548963437503981062668882632052452068705295424483999545932010198708798592260;

uint256 constant IC3x =
13853274336731202523728826661915506795333516652854674163618978302237601632434;
uint256 constant IC3y =
15420320918214290109713867361085955935385737854012308761626909938871786338011;

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

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

mstore(0, isValid)
return(0, 0x20)
}
}
}
59 changes: 59 additions & 0 deletions contracts/lib/groth16-verifiers/Groth16VerifierAuthV2Wrapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// 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.8.27;

import {Groth16VerifierAuthV2} from "./Groth16VerifierAuthV2.sol";
import {IVerifier} from "../../interfaces/IVerifier.sol";

contract Groth16VerifierAuthV2Wrapper is Groth16VerifierAuthV2, IVerifier {
/**
* @dev Number of public signals for atomic mtp circuit
*/
uint256 constant PUBSIGNALS_LENGTH = 3;

/**
* @dev Verify the circuit with the groth16 proof π=([πa]1,[πb]2,[πc]1).
* @param a πa element of the groth16 proof.
* @param b πb element of the groth16 proof.
* @param c πc element of the groth16 proof.
* @param input Public inputs of the circuit.
* @return r true if the proof is valid.
*/
function verify(
uint256[2] calldata a,
uint256[2][2] calldata b,
uint256[2] calldata c,
uint256[] calldata input
) public view returns (bool r) {
uint[PUBSIGNALS_LENGTH] memory pubSignals;

require(input.length == PUBSIGNALS_LENGTH, "expected array length is 3");

for (uint256 i = 0; i < PUBSIGNALS_LENGTH; i++) {
pubSignals[i] = input[i];
}

return this.verifyProof(a, b, c, pubSignals);
}
}
4 changes: 2 additions & 2 deletions contracts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@iden3/contracts",
"description": "Smart Contract library for Solidity",
"version": "2.4.6",
"version": "2.5.0",
"files": [
"**/*.sol",
"/build/contracts/*.json",
Expand Down
Loading

0 comments on commit 9ebda74

Please sign in to comment.