Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft of anon aadhaar merklized issuer contract #329

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
226 changes: 226 additions & 0 deletions contracts/lib/groth16-verifiers/Groth16VerifierAnonAadhaarV1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// 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;

Check failure on line 21 in contracts/lib/groth16-verifiers/Groth16VerifierAnonAadhaarV1.sol

View workflow job for this annotation

GitHub Actions / solhint

Compiler version >=0.7.0 <0.9.0 does not satisfy the >=0.8.4 semver requirement

contract Groth16VerifierAnonAadhaarV1 {
// Scalar field size
uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617;

Check failure on line 25 in contracts/lib/groth16-verifiers/Groth16VerifierAnonAadhaarV1.sol

View workflow job for this annotation

GitHub Actions / solhint

Replace ···= with =⏎·······
// Base field size
uint256 constant q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;

Check failure on line 27 in contracts/lib/groth16-verifiers/Groth16VerifierAnonAadhaarV1.sol

View workflow job for this annotation

GitHub Actions / solhint

Replace ··= with =⏎·······

// Verification Key data
uint256 constant alphax = 20491192805390485299153009773594534940189261866228447918068658471970481763042;

Check failure on line 30 in contracts/lib/groth16-verifiers/Groth16VerifierAnonAadhaarV1.sol

View workflow job for this annotation

GitHub Actions / solhint

Replace ·= with =⏎·······
uint256 constant alphay = 9383485363053290200918347156157836566562967994039712273449902621266178545958;

Check failure on line 31 in contracts/lib/groth16-verifiers/Groth16VerifierAnonAadhaarV1.sol

View workflow job for this annotation

GitHub Actions / solhint

Replace ·= with =⏎·······
uint256 constant betax1 = 4252822878758300859123897981450591353533073413197771768651442665752259397132;

Check failure on line 32 in contracts/lib/groth16-verifiers/Groth16VerifierAnonAadhaarV1.sol

View workflow job for this annotation

GitHub Actions / solhint

Replace ·= with =⏎·······
uint256 constant betax2 = 6375614351688725206403948262868962793625744043794305715222011528459656738731;

Check failure on line 33 in contracts/lib/groth16-verifiers/Groth16VerifierAnonAadhaarV1.sol

View workflow job for this annotation

GitHub Actions / solhint

Replace ·= with =⏎·······
uint256 constant betay1 = 21847035105528745403288232691147584728191162732299865338377159692350059136679;

Check failure on line 34 in contracts/lib/groth16-verifiers/Groth16VerifierAnonAadhaarV1.sol

View workflow job for this annotation

GitHub Actions / solhint

Replace ·= with =⏎·······
uint256 constant betay2 = 10505242626370262277552901082094356697409835680220590971873171140371331206856;

Check failure on line 35 in contracts/lib/groth16-verifiers/Groth16VerifierAnonAadhaarV1.sol

View workflow job for this annotation

GitHub Actions / solhint

Replace ··= with ·=⏎·······
uint256 constant gammax1 = 11559732032986387107991004021392285783925812861821192530917403151452391805634;

Check failure on line 36 in contracts/lib/groth16-verifiers/Groth16VerifierAnonAadhaarV1.sol

View workflow job for this annotation

GitHub Actions / solhint

Insert ⏎·······
uint256 constant gammax2 = 10857046999023057135944570762232829481370756359578518086990519993285655852781;
uint256 constant gammay1 = 4082367875863433681332203403145435568316851327593401208105741076214120093531;
uint256 constant gammay2 = 8495653923123431417604973247489272438418190587263600148770280649306958101930;
uint256 constant deltax1 = 9752776248207923469867409382048716372612962508996673790438054623489759145780;
uint256 constant deltax2 = 9452966396337616788385483674588918865821745355579743187934167481883534967251;
uint256 constant deltay1 = 15208746120897712994584783416569201488530823308245332170362178262653051227153;
uint256 constant deltay2 = 1743063522514331149547203759617585666894492323061121432061356664097165019185;


uint256 constant IC0x = 17202926283251598065231527376648019777623479314208943009386780888674303306367;
uint256 constant IC0y = 275122378331887271415548857816654626631770508982339731839029501536657326428;

uint256 constant IC1x = 6793065755890846769340018862424041607459712744183098712209475459344516081736;
uint256 constant IC1y = 9071617632382870191496047914286194606374346215985030020661707710600804984468;

uint256 constant IC2x = 8036455770137753300143301227052694753324655839004232562996994892756513744632;
uint256 constant IC2y = 19136034516434561880116093412780628312815490220057647258792780777662444980579;

uint256 constant IC3x = 5706924235985036010295393494457870846665277481967479020222798374799047750256;
uint256 constant IC3y = 16335939767066344430970552411852108062971274211549461481263778305792680100688;

uint256 constant IC4x = 13362589808591082140154747717874938555566489737803726066109447908122332925248;
uint256 constant IC4y = 9533438191675367092873436171711161489331286873030419565001344655397871220342;

uint256 constant IC5x = 13606256073504308111394692719682918711474465633647202003403244063200398917656;
uint256 constant IC5y = 14074028033425546109580220784064373333536645170395823067627266760390456677393;

uint256 constant IC6x = 19096454032987284766595414848523440262913762053347052157186315602771633028839;
uint256 constant IC6y = 7276871217582302819620304900124070838673347672010981352688103755427813080463;

uint256 constant IC7x = 18459111221168914001640566342970864469601586632412046593346422818505556097195;
uint256 constant IC7y = 13100409853915252749737243319212327344620678393375462514810315616945336813081;

uint256 constant IC8x = 9200173373433970145496240930187071045143369120243275026153868648172499323668;
uint256 constant IC8y = 7200362604079959194735981300560157143070925411981289227281356488895967807761;

uint256 constant IC9x = 14814413443159487223931208725152452842059865722271281787555352116049799452733;
uint256 constant IC9y = 17373462594478087004748496082618076921959076827177568046785097901984202939893;


// 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[9] calldata _pubSignals) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, r)) {
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)))

g1_mulAccC(_pVk, IC4x, IC4y, calldataload(add(pubSignals, 96)))

g1_mulAccC(_pVk, IC5x, IC5y, calldataload(add(pubSignals, 128)))

g1_mulAccC(_pVk, IC6x, IC6y, calldataload(add(pubSignals, 160)))

g1_mulAccC(_pVk, IC7x, IC7y, calldataload(add(pubSignals, 192)))

g1_mulAccC(_pVk, IC8x, IC8y, calldataload(add(pubSignals, 224)))

g1_mulAccC(_pVk, IC9x, IC9y, calldataload(add(pubSignals, 256)))


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

checkField(calldataload(add(_pubSignals, 128)))

checkField(calldataload(add(_pubSignals, 160)))

checkField(calldataload(add(_pubSignals, 192)))

checkField(calldataload(add(_pubSignals, 224)))

checkField(calldataload(add(_pubSignals, 256)))

checkField(calldataload(add(_pubSignals, 288)))


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

mstore(0, isValid)
return(0, 0x20)
}
}
}
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.4 <0.9.0;

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

contract Groth16VerifierAnonAadhaarV1Wrapper is Groth16VerifierAnonAadhaarV1, IVerifier {
/**
* @dev Number of public signals for atomic mtp circuit
*/
uint256 constant PUBSIGNALS_LENGTH = 9;

/**
* @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 9");

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

return this.verifyProof(a, b, c, pubSignals);
}
}
Loading
Loading