mirror of
https://github.com/selfxyz/self.git
synced 2026-01-10 07:08:10 -05:00
update smart contracts with the new two proofs set-up
This commit is contained in:
@@ -1,16 +1,25 @@
|
||||
export function formatCallData_register(parsedCallData: any[]){
|
||||
export function formatCallData_register(parsedCallData: any[]) {
|
||||
return {
|
||||
commitment: parsedCallData[3][0],
|
||||
blinded_dsc_commitment: parsedCallData[3][0],
|
||||
nullifier: parsedCallData[3][1],
|
||||
merkle_root: parsedCallData[3][2],
|
||||
commitment: parsedCallData[3][2],
|
||||
attestation_id: parsedCallData[3][3],
|
||||
a: parsedCallData[0],
|
||||
b: [parsedCallData[1][0], parsedCallData[1][1]],
|
||||
c: parsedCallData[2],
|
||||
};;
|
||||
}
|
||||
export function formatCallData_dsc(parsedCallData: any[]) {
|
||||
return {
|
||||
blinded_dsc_commitment: parsedCallData[3][0],
|
||||
merkle_root: parsedCallData[3][1],
|
||||
a: parsedCallData[0],
|
||||
b: [parsedCallData[1][0], parsedCallData[1][1]],
|
||||
c: parsedCallData[2],
|
||||
};;
|
||||
}
|
||||
|
||||
export function formatCallData_disclose(parsedCallData: any[]){
|
||||
export function formatCallData_disclose(parsedCallData: any[]) {
|
||||
return {
|
||||
nullifier: parsedCallData[3][0],
|
||||
revealedData_packed: [parsedCallData[3][1], parsedCallData[3][2], parsedCallData[3][3]],
|
||||
|
||||
11
contracts/contracts/IVerifierCSCA.sol
Normal file
11
contracts/contracts/IVerifierCSCA.sol
Normal file
@@ -0,0 +1,11 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
interface IVerifierCSCA {
|
||||
function verifyProof(
|
||||
uint[2] calldata _pA,
|
||||
uint[2][2] calldata _pB,
|
||||
uint[2] calldata _pC,
|
||||
uint[2] calldata _pubSignals
|
||||
) external view returns (bool);
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import {IRegister} from "./interfaces/IRegister.sol";
|
||||
import {Registry} from "./Registry.sol";
|
||||
import {Base64} from "./libraries/Base64.sol";
|
||||
import {IVerifier} from "./IVerifier.sol";
|
||||
import {IVerifierCSCA} from "./IVerifierCSCA.sol";
|
||||
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
|
||||
import "@openzeppelin/contracts/utils/Strings.sol";
|
||||
import "@zk-kit/imt.sol/internal/InternalLeanIMT.sol";
|
||||
@@ -61,19 +62,28 @@ contract ProofOfPassportRegister is IRegister, Ownable {
|
||||
LeanIMTData internal imt;
|
||||
|
||||
// poseidon("E-PASSPORT")
|
||||
bytes32 public attestationId = bytes32(0x12d57183e0a41615471a14e5a93c87b9db757118c1d7a6a9f73106819d656f24);
|
||||
bytes32 public attestationId =
|
||||
bytes32(
|
||||
0x12d57183e0a41615471a14e5a93c87b9db757118c1d7a6a9f73106819d656f24
|
||||
);
|
||||
|
||||
mapping(uint256 => bool) public nullifiers;
|
||||
mapping(uint256 => bool) public merkleRootsCreated;
|
||||
mapping(uint256 => address) public verifiers;
|
||||
address public cscaVerifier;
|
||||
|
||||
constructor(Registry r) {
|
||||
constructor(Registry r, address _cscaVerifier) {
|
||||
registry = r;
|
||||
cscaVerifier = _cscaVerifier;
|
||||
transferOwnership(msg.sender);
|
||||
}
|
||||
|
||||
function validateProof(RegisterProof calldata proof, uint256 signature_algorithm) external override {
|
||||
if (!registry.checkRoot(bytes32(proof.merkle_root))) {
|
||||
function validateProof(
|
||||
RegisterProof calldata proof,
|
||||
CSCAProof calldata proof_csca,
|
||||
uint256 signature_algorithm
|
||||
) external override {
|
||||
if (!registry.checkRoot(bytes32(proof_csca.merkle_root))) {
|
||||
revert("InvalidMerkleRoot");
|
||||
}
|
||||
// if (nullifiers[proof.nullifier]) {
|
||||
@@ -82,7 +92,7 @@ contract ProofOfPassportRegister is IRegister, Ownable {
|
||||
if (bytes32(proof.attestation_id) != attestationId) {
|
||||
revert("InvalidAttestationId");
|
||||
}
|
||||
if (!verifyProof(proof, signature_algorithm)) {
|
||||
if (!verifyProof(proof, proof_csca, signature_algorithm)) {
|
||||
revert("InvalidProof");
|
||||
}
|
||||
|
||||
@@ -91,7 +101,7 @@ contract ProofOfPassportRegister is IRegister, Ownable {
|
||||
_addCommitment(proof.commitment);
|
||||
|
||||
emit ProofValidated(
|
||||
proof.merkle_root,
|
||||
proof_csca.merkle_root,
|
||||
proof.nullifier,
|
||||
proof.commitment
|
||||
);
|
||||
@@ -99,6 +109,7 @@ contract ProofOfPassportRegister is IRegister, Ownable {
|
||||
|
||||
function verifyProof(
|
||||
RegisterProof calldata proof,
|
||||
CSCAProof calldata proof_csca,
|
||||
uint256 signature_algorithm
|
||||
) public view override returns (bool) {
|
||||
return
|
||||
@@ -107,11 +118,20 @@ contract ProofOfPassportRegister is IRegister, Ownable {
|
||||
proof.b,
|
||||
proof.c,
|
||||
[
|
||||
uint(proof.commitment),
|
||||
uint(proof.blinded_dsc_commitment),
|
||||
uint(proof.nullifier),
|
||||
uint(proof.merkle_root),
|
||||
uint(proof.commitment),
|
||||
uint(proof.attestation_id)
|
||||
]
|
||||
) &&
|
||||
IVerifierCSCA(cscaVerifier).verifyProof(
|
||||
proof_csca.a,
|
||||
proof_csca.b,
|
||||
proof_csca.c,
|
||||
[
|
||||
uint(proof_csca.blinded_dsc_commitment),
|
||||
uint(proof_csca.merkle_root)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -164,7 +184,21 @@ contract ProofOfPassportRegister is IRegister, Ownable {
|
||||
verifiers[signature_algorithm] = verifier_address;
|
||||
}
|
||||
|
||||
function removeSignatureAlgorithm(uint256 signature_algorithm) external onlyOwner {
|
||||
function updateCSCAVerifier(address _cscaVerifier) external onlyOwner {
|
||||
require(
|
||||
_cscaVerifier != address(0),
|
||||
"Register__InvalidVerifierAddress"
|
||||
);
|
||||
cscaVerifier = _cscaVerifier;
|
||||
}
|
||||
|
||||
function removeSignatureAlgorithm(
|
||||
uint256 signature_algorithm
|
||||
) external onlyOwner {
|
||||
verifiers[signature_algorithm] = address(0);
|
||||
}
|
||||
|
||||
function devAddCommitment(uint commitment) external onlyOwner {
|
||||
_addCommitment(commitment);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,174 +0,0 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.3;
|
||||
|
||||
import {IRegister} from "./interfaces/IRegister.sol";
|
||||
import {Registry} from "./Registry.sol";
|
||||
import {Base64} from "./libraries/Base64.sol";
|
||||
import {IVerifier} from "./IVerifier.sol";
|
||||
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
|
||||
import "@openzeppelin/contracts/utils/Strings.sol";
|
||||
import "@zk-kit/imt.sol/internal/InternalLeanIMT.sol";
|
||||
|
||||
/***
|
||||
,--"""",--.__,---[],-------._
|
||||
," __,' \ \--""""""==;-
|
||||
," _,-" "/---.___ \ ___\ ,-'',"
|
||||
/,-' / ;. ,.--'-.__\ _,-"" ,| `,' /
|
||||
/``""""-._/,-|:\ []\,' ```-/:;-. `. /
|
||||
` ;::: || /:,; `-.\
|
||||
=.,'__,---||-.____',.=
|
||||
=(:\_ ||__ ):)=
|
||||
,"::::`----||::`--':::"._
|
||||
,':::::::::::||::::::::::::'.
|
||||
.__ ;:::.-.:::::__||___:::::.-.:::\ __,
|
||||
"""-;:::( O )::::>_|| _<::::( O )::::-"""
|
||||
=======;:::::`-`:::::::||':::::::`-`:::::\=======
|
||||
,--"";:::_____________||______________::::""----. , ,
|
||||
; ::`._( | ||| | )_,'::::\_,,,,,,,,,,____/,'_,
|
||||
,; :::`--._|____[]|_____|_.-'::::::::::::::::::::::::);_
|
||||
;/ / :::::::::,||,:::::::::::::::::::::::::::::::::::/
|
||||
/; ``''''----------/,'/,__,,,,,____:::::::::::::::::::::,"
|
||||
;/ :);/|_;| ,--.. . ```-.:::::::::::::_,"
|
||||
/; :::):__,'//""\\. ,--.. \:::,:::::_,"
|
||||
;/ :::::/ . . . . . . //""\\. \::":__,"
|
||||
;/ :::::::,' . . . . . . . . . . .:`::\
|
||||
_ _ _ _ _ _ _ _ _ _ '; _ :::::::__,'. ,--.._. .,--. . . ._. .:`::`
|
||||
_( )_ _( )_ _( )_ _( )_ _( )_ _( )_ _( )_ _( )_ _( )_ _( )'; _(__,..--'''-._. //""\\.).//""\\_. ,--.. :`:::`
|
||||
(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(_ o ;)(_ / _\\_.//""\\ . .)._.o._.(. . .)//""\\.(:`::`)
|
||||
(_,_) (_,_) (_,_) (_,_) (_,_) (_,_) (_,_) (_,_) (_,_) (_,_; (/,_) (_._. .(.,.). ._._. .(.,.). ._._. .:`::`
|
||||
_ ; ( . . . . . . . . . . . . . . . ;:::`
|
||||
_( )_ ██╗ █████╗ ██████╗ █████╗ ███╗ ██╗ ███████╗██████╗;██╗████████╗██╗.██████╗.███╗ . ██╗ . . ;':::`
|
||||
(_ o _) ██║██╔══██╗██╔══██╗██╔══██╗████╗ ██║ ██╔════╝██╔══██╗██║╚══██╔══╝██║██╔═══██╗████╗ .██║. . .;`:::_)
|
||||
(_,_) ██║███████║██████╔╝███████║██╔██╗ ██║ █████╗ ██║,:██║██║ ██║ ██║██║ . ██║██╔██╗ ██║ . . ;`::;`
|
||||
_ ██ ██║██╔══██║██╔═══╝ ██╔══██║██║╚██╗██║ ██╔══╝ ██║ ,██║██║ ██║ ██║██║. .██║██║╚██╗██║. . ;':::;`
|
||||
_( )_ ╚█████╔╝██║ ██║██║ ██║ ██║██║ ╚████║ ███████╗██████╔╝██║ ██║ ██║╚██████╔╝██║.╚████║ . ,':::;)_
|
||||
(_ o _) ╚════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚══════╝╚═════╝ ╚═╝. ╚═╝ ╚═╝ ╚═════╝ ╚═╝ .╚═══╝ .,':::;` _)
|
||||
(_,_) : `. . . . . . . . . . . . ;::::;`,_)
|
||||
_ _ _ _ _ _ _ _ _ _ '. `-. . . . . ._. . . . . ,-'::::; _
|
||||
_( )_ _( )_ _( )_ _( )_ _( )_ _( )_ _( )_ _( )_ _( )_ _( )_ _( )`:__( )``--..___________..--'':::::;'`_( )_
|
||||
(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(_ o _)(`._::,.:,.:,:_ctr_:,:,.::,.:_;'`_)(_ o _)
|
||||
(_,_) (_,_) (_,_) (_,_) (_,_) (_,_) (_,_) (_,_) (_,_) (_,_) (_,_) (_,`"\/"\/\/'""""`\/"\/""\/"(_,_) (_,_)
|
||||
|
||||
***/
|
||||
|
||||
contract ProofOfPassportRegister_dev is IRegister, Ownable {
|
||||
Registry public immutable registry;
|
||||
using Base64 for *;
|
||||
using Strings for uint256;
|
||||
|
||||
using InternalLeanIMT for LeanIMTData;
|
||||
LeanIMTData internal imt;
|
||||
|
||||
// poseidon("E-PASSPORT")
|
||||
bytes32 public attestationId = bytes32(0x12d57183e0a41615471a14e5a93c87b9db757118c1d7a6a9f73106819d656f24);
|
||||
|
||||
mapping(uint256 => bool) public nullifiers;
|
||||
mapping(uint256 => bool) public merkleRootsCreated;
|
||||
mapping(uint256 => address) public verifiers;
|
||||
|
||||
constructor(Registry r) {
|
||||
registry = r;
|
||||
transferOwnership(msg.sender);
|
||||
}
|
||||
|
||||
function validateProof(RegisterProof calldata proof, uint256 signature_algorithm) external override {
|
||||
if (!registry.checkRoot(bytes32(proof.merkle_root))) {
|
||||
revert("InvalidMerkleRoot");
|
||||
}
|
||||
if (nullifiers[proof.nullifier]) {
|
||||
revert("YouAreUsingTheSameNullifierTwice");
|
||||
}
|
||||
if (bytes32(proof.attestation_id) != attestationId) {
|
||||
revert("InvalidAttestationId");
|
||||
}
|
||||
if (!verifyProof(proof, signature_algorithm)) {
|
||||
revert("InvalidProof");
|
||||
}
|
||||
|
||||
nullifiers[proof.nullifier] = true;
|
||||
|
||||
_addCommitment(proof.commitment);
|
||||
|
||||
emit ProofValidated(
|
||||
proof.merkle_root,
|
||||
proof.nullifier,
|
||||
proof.commitment
|
||||
);
|
||||
}
|
||||
|
||||
function verifyProof(
|
||||
RegisterProof calldata proof,
|
||||
uint256 signature_algorithm
|
||||
) public view override returns (bool) {
|
||||
return
|
||||
IVerifier(verifiers[signature_algorithm]).verifyProof(
|
||||
proof.a,
|
||||
proof.b,
|
||||
proof.c,
|
||||
[
|
||||
uint(proof.commitment),
|
||||
uint(proof.nullifier),
|
||||
uint(proof.merkle_root),
|
||||
uint(proof.attestation_id)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
function _addCommitment(uint256 commitment) internal {
|
||||
uint256 index = getMerkleTreeSize();
|
||||
uint256 imt_root = imt._insert(commitment);
|
||||
merkleRootsCreated[imt_root] = true;
|
||||
emit AddCommitment(index, commitment, imt_root);
|
||||
}
|
||||
|
||||
function checkRoot(uint256 root) external view returns (bool) {
|
||||
return merkleRootsCreated[root];
|
||||
}
|
||||
|
||||
function getMerkleTreeSize() public view returns (uint256) {
|
||||
return imt.size;
|
||||
}
|
||||
|
||||
function getMerkleRoot() public view returns (uint256) {
|
||||
return imt._root();
|
||||
}
|
||||
|
||||
function indexOf(uint commitment) public view returns (uint256) {
|
||||
return imt._indexOf(commitment);
|
||||
}
|
||||
|
||||
function addSignatureAlgorithm(
|
||||
uint256 signature_algorithm,
|
||||
address verifier_address
|
||||
) external onlyOwner {
|
||||
require(
|
||||
verifier_address != address(0),
|
||||
"Register__InvalidVerifierAddress"
|
||||
);
|
||||
require(
|
||||
verifiers[signature_algorithm] == address(0),
|
||||
"Register__SignatureAlgorithmAlreadySet"
|
||||
);
|
||||
verifiers[signature_algorithm] = verifier_address;
|
||||
}
|
||||
|
||||
function updateSignaturesAlgorithm(
|
||||
uint256 signature_algorithm,
|
||||
address verifier_address
|
||||
) external onlyOwner {
|
||||
require(
|
||||
verifier_address != address(0),
|
||||
"Register__InvalidVerifierAddress"
|
||||
);
|
||||
verifiers[signature_algorithm] = verifier_address;
|
||||
}
|
||||
|
||||
function removeSignatureAlgorithm(uint256 signature_algorithm) external onlyOwner {
|
||||
verifiers[signature_algorithm] = address(0);
|
||||
}
|
||||
|
||||
function devAddCommitment(uint256 commitment) external onlyOwner {
|
||||
_addCommitment(commitment);
|
||||
}
|
||||
}
|
||||
177
contracts/contracts/Verifier_dsc_4096.sol
Normal file
177
contracts/contracts/Verifier_dsc_4096.sol
Normal file
@@ -0,0 +1,177 @@
|
||||
// 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 Verifier_dsc_4096 {
|
||||
// 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 = 4766974213866404999153243023526474740438493630736868430784294106382580438865;
|
||||
uint256 constant deltax2 = 11826309807517960423797497839472058193440977557643410765981623670305210028667;
|
||||
uint256 constant deltay1 = 2721183979449492660297703963017468056510728799657698370828478062197800054220;
|
||||
uint256 constant deltay2 = 8534592784686242755714043239929782226962358600429626868926755520031556757924;
|
||||
|
||||
|
||||
uint256 constant IC0x = 18649009427776548364855301022633615434381451989401256455527069790930639243034;
|
||||
uint256 constant IC0y = 14966286627709744589367674452228634993762546967166011703198886464719039024013;
|
||||
|
||||
uint256 constant IC1x = 20706116723424145907077259856714149211486113689677598161807626990175416055855;
|
||||
uint256 constant IC1y = 15845917792683227684076539227044462158487793153830878136761648330861907216501;
|
||||
|
||||
uint256 constant IC2x = 4959902634830068877298534977925119698925821306957994937816264636768694503901;
|
||||
uint256 constant IC2y = 1017450014983817548253754844354723453356574477017470478787800195496905080629;
|
||||
|
||||
|
||||
// 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[2] 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)))
|
||||
|
||||
|
||||
// -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,79 +22,50 @@ pragma solidity >=0.7.0 <0.9.0;
|
||||
|
||||
contract Verifier_register_sha256WithRSAEncryption_65537 {
|
||||
// Scalar field size
|
||||
uint256 constant r =
|
||||
21888242871839275222246405745257275088548364400416034343698204186575808495617;
|
||||
uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
|
||||
// Base field size
|
||||
uint256 constant q =
|
||||
21888242871839275222246405745257275088696311157297823662689037894645226208583;
|
||||
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 =
|
||||
8621547531182535150407888029201368301770476729153465794703264202192815571359;
|
||||
uint256 constant deltax2 =
|
||||
15103106408355332301942131509868522431691314815775868309987970774938032917288;
|
||||
uint256 constant deltay1 =
|
||||
6645162790490790370666683333328013327391052296249918143428710257661921105038;
|
||||
uint256 constant deltay2 =
|
||||
10763995757943557095717751070926823627407000684741982373211188433099461629261;
|
||||
|
||||
uint256 constant IC0x =
|
||||
3282392222319870187604701708046062486466523164081290107998617777831375164587;
|
||||
uint256 constant IC0y =
|
||||
18517862052900956997388000175515724090884408369424321445934877327283588359272;
|
||||
|
||||
uint256 constant IC1x =
|
||||
5286728832279908108387707952587196088527540104929270090847911472664953271970;
|
||||
uint256 constant IC1y =
|
||||
10865382193820643609436263356234352213040619555515132441018576446177626903685;
|
||||
|
||||
uint256 constant IC2x =
|
||||
17369175819581267291359598759978583038911875594214746386725729561280633969413;
|
||||
uint256 constant IC2y =
|
||||
3791227674519577224178647607623854135818340295341271204223562844765608597231;
|
||||
|
||||
uint256 constant IC3x =
|
||||
16145397888378594749203783979939661182148871384717160919494071895693530867402;
|
||||
uint256 constant IC3y =
|
||||
6661266639854997634037837383871172722344991275459786199162109303640642443805;
|
||||
|
||||
uint256 constant IC4x =
|
||||
12360141330371774505869766028614178189194894968048267086650234020923103405017;
|
||||
uint256 constant IC4y =
|
||||
21693506107243732882754504367451701349563463564721640140701017006809917992958;
|
||||
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 = 7842079659365013504527459449399747263216258154598341788384122232221645841820;
|
||||
uint256 constant deltax2 = 592531936512242424805746165164273891143030619107820922605882698648367478821;
|
||||
uint256 constant deltay1 = 7685389033119699102327185162270826842281918923605465986875254173021486948521;
|
||||
uint256 constant deltay2 = 14338800814001509796778713685029146703960490458582669157577770246620462564074;
|
||||
|
||||
|
||||
uint256 constant IC0x = 8717354459045927956647299801195462428104821609917433788177448756317513802457;
|
||||
uint256 constant IC0y = 7949913439363190099670038253230765067880189350221938927935108268117528163436;
|
||||
|
||||
uint256 constant IC1x = 15997987389630954586752278248129109519329357952005125818987503910532291457489;
|
||||
uint256 constant IC1y = 4278778275612708600450012876992239171434211314395167482352303460331938474458;
|
||||
|
||||
uint256 constant IC2x = 7744807019190956449997403910621396274934018759940557820117051684334383253546;
|
||||
uint256 constant IC2y = 15644888800694698340956754924273690405569685023582037971277324052297192753560;
|
||||
|
||||
uint256 constant IC3x = 21058629094728372207228920360000937155629387949785082005623579160835276648555;
|
||||
uint256 constant IC3y = 16261316501816871973497099946194793152983276367760367342038153774256759952842;
|
||||
|
||||
uint256 constant IC4x = 21241671207305510186239297693073749788229886922198947851253988762033593366196;
|
||||
uint256 constant IC4y = 7270842731243524884029836802598846345817005438651531769614810431466164056101;
|
||||
|
||||
|
||||
// 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[4] calldata _pubSignals
|
||||
) public view returns (bool) {
|
||||
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[4] calldata _pubSignals) public view returns (bool) {
|
||||
assembly {
|
||||
function checkField(v) {
|
||||
if iszero(lt(v, q)) {
|
||||
@@ -102,7 +73,7 @@ contract Verifier_register_sha256WithRSAEncryption_65537 {
|
||||
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
|
||||
@@ -137,21 +108,19 @@ contract Verifier_register_sha256WithRSAEncryption_65537 {
|
||||
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)))
|
||||
|
||||
|
||||
// -A
|
||||
mstore(_pPairing, calldataload(pA))
|
||||
mstore(
|
||||
add(_pPairing, 32),
|
||||
mod(sub(q, calldataload(add(pA, 32))), q)
|
||||
)
|
||||
mstore(add(_pPairing, 32), mod(sub(q, calldataload(add(pA, 32))), q))
|
||||
|
||||
// B
|
||||
mstore(add(_pPairing, 64), calldataload(pB))
|
||||
@@ -173,6 +142,7 @@ contract Verifier_register_sha256WithRSAEncryption_65537 {
|
||||
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)
|
||||
@@ -189,14 +159,8 @@ contract Verifier_register_sha256WithRSAEncryption_65537 {
|
||||
mstore(add(_pPairing, 704), deltay1)
|
||||
mstore(add(_pPairing, 736), deltay2)
|
||||
|
||||
let success := staticcall(
|
||||
sub(gas(), 2000),
|
||||
8,
|
||||
_pPairing,
|
||||
768,
|
||||
_pPairing,
|
||||
0x20
|
||||
)
|
||||
|
||||
let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20)
|
||||
|
||||
isOk := and(success, mload(_pPairing))
|
||||
}
|
||||
@@ -205,22 +169,23 @@ contract Verifier_register_sha256WithRSAEncryption_65537 {
|
||||
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)))
|
||||
|
||||
|
||||
// Validate all evaluations
|
||||
let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem)
|
||||
|
||||
mstore(0, isValid)
|
||||
return(0, 0x20)
|
||||
}
|
||||
}
|
||||
}
|
||||
return(0, 0x20)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,24 +32,37 @@ interface IRegister {
|
||||
/// @param b The 'b' parameter of the zkSNARK proof
|
||||
/// @param c The 'c' parameter of the zkSNARK proof
|
||||
struct RegisterProof {
|
||||
uint commitment;
|
||||
uint blinded_dsc_commitment;
|
||||
uint nullifier;
|
||||
uint merkle_root;
|
||||
uint commitment;
|
||||
uint attestation_id;
|
||||
uint[2] a;
|
||||
uint[2][2] b;
|
||||
uint[2] c;
|
||||
}
|
||||
|
||||
struct CSCAProof {
|
||||
uint blinded_dsc_commitment;
|
||||
uint merkle_root;
|
||||
uint[2] a;
|
||||
uint[2][2] b;
|
||||
uint[2] c;
|
||||
}
|
||||
|
||||
/// @notice Validates a Register proof
|
||||
/// @param proof The Register proof to validate
|
||||
function validateProof(RegisterProof calldata proof, uint256 signature_algorithm) external;
|
||||
function validateProof(
|
||||
RegisterProof calldata proof,
|
||||
CSCAProof calldata proof_csca,
|
||||
uint256 signature_algorithm
|
||||
) external;
|
||||
|
||||
/// @notice Verifies a Register proof
|
||||
/// @param proof The Register proof to verify
|
||||
/// @return bool Returns true if the proof is valid, false otherwise
|
||||
function verifyProof(
|
||||
RegisterProof calldata proof,
|
||||
CSCAProof calldata proof_csca,
|
||||
uint256 signature_algorithm
|
||||
) external view returns (bool);
|
||||
|
||||
@@ -70,4 +83,7 @@ interface IRegister {
|
||||
/// @param commitment The commitment to find
|
||||
/// @return uint Returns the index of the commitment
|
||||
function indexOf(uint commitment) external view returns (uint);
|
||||
|
||||
/// @notice DEV function
|
||||
function devAddCommitment(uint commitment) external;
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
"dotenv": "^16.3.1",
|
||||
"hardhat-contract-sizer": "^2.10.0",
|
||||
"mocha": "^10.4.0",
|
||||
"node-forge": "^1.3.1",
|
||||
"poseidon-lite": "^0.2.0",
|
||||
"poseidon-solidity": "^0.0.5",
|
||||
"snarkjs": "^0.7.1"
|
||||
|
||||
@@ -9,12 +9,14 @@ import { time } from "@nomicfoundation/hardhat-toolbox/network-helpers";
|
||||
import axios from 'axios';
|
||||
import { revealBitmapFromMapping } from "../../common/src/utils/revealBitmap";
|
||||
import { generateCircuitInputsRegister, generateCircuitInputsDisclose } from "../../common/src/utils/generateInputs";
|
||||
import { formatCallData_disclose, formatCallData_register } from "../../common/src/utils/formatCallData";
|
||||
import { formatCallData_disclose, formatCallData_dsc, formatCallData_register } from "../../common/src/utils/formatCallData";
|
||||
import fs from 'fs';
|
||||
import { LeanIMT } from "@zk-kit/lean-imt";
|
||||
import { poseidon2 } from "poseidon-lite";
|
||||
import { PassportData } from "../../common/src/utils/types";
|
||||
import { Signer } from "ethers";
|
||||
import { getCSCAInputs } from "../../common/src/utils/csca";
|
||||
import forge from "node-forge";
|
||||
|
||||
|
||||
describe("Proof of Passport - Contracts - Register & Disclose flow", function () {
|
||||
@@ -26,16 +28,34 @@ describe("Proof of Passport - Contracts - Register & Disclose flow", function ()
|
||||
const path_register_zkey = "../circuits/build/register_sha256WithRSAEncryption_65537_final.zkey";
|
||||
const path_register_vkey = "../circuits/build/register_sha256WithRSAEncryption_65537_vkey.json";
|
||||
|
||||
const path_dsc_wasm = "../circuits/build/dsc_4096_js/dsc_4096.wasm";
|
||||
const path_dsc_zkey = "../circuits/build/dsc_4096_final.zkey";
|
||||
const path_dsc_vkey = "../circuits/build/dsc_4096_vkey.json";
|
||||
|
||||
const path_disclose_wasm = "../circuits/build/disclose_js/disclose.wasm";
|
||||
const path_disclose_zkey = "../circuits/build/disclose_final.zkey";
|
||||
const path_disclose_vkey = "../circuits/build/disclose_vkey.json";
|
||||
// Smart contracts
|
||||
let Verifier_register: any, verifier_register: any, Registry: any, registry: any, Formatter: any, formatter: any, Register: any, register: any, Verifier_disclose: any, verifier_disclose: any, SBT: any, sbt: any, PoseidonT3: any, poseidonT3: any;
|
||||
let Verifier_dsc: any, verifier_dsc: any, parsedCallData_dsc: any[], formattedCallData_dsc: any;
|
||||
let owner, otherAccount, thirdAccount: Signer;
|
||||
let imt: LeanIMT;
|
||||
|
||||
const n_dsc = 121;
|
||||
const k_dsc = 17;
|
||||
const n_csca = 121;
|
||||
const k_csca = 34;
|
||||
const max_cert_bytes = 1664;
|
||||
const dsc = fs.readFileSync('../common/src/mock_certificates/sha256_rsa_4096/mock_dsc.crt', 'utf8');
|
||||
const csca = fs.readFileSync('../common/src/mock_certificates/sha256_rsa_4096/mock_csca.crt', 'utf8');
|
||||
const dscCert = forge.pki.certificateFromPem(dsc);
|
||||
const cscaCert = forge.pki.certificateFromPem(csca);
|
||||
let inputs_csca = getCSCAInputs(dscCert, cscaCert, n_dsc, k_dsc, n_csca, k_csca, max_cert_bytes, true);
|
||||
console.log(inputs_csca);
|
||||
|
||||
let bitmap, scope, user_address, majority, user_identifier, current_date, input_disclose: any;
|
||||
let proof_disclose, publicSignals_disclose, proof_result_disclose, vkey_disclose, verified_disclose: any, rawCallData_disclose, parsedCallData_disclose: any[], formattedCallData_disclose: any;
|
||||
let proof_csca, publicSignals_csca: any;
|
||||
let secret: string = BigInt(0).toString();
|
||||
let attestation_id: string = PASSPORT_ATTESTATION_ID;
|
||||
|
||||
@@ -58,8 +78,9 @@ describe("Proof of Passport - Contracts - Register & Disclose flow", function ()
|
||||
passportData = mockPassportData_sha256WithRSAEncryption_65537;
|
||||
|
||||
inputs = generateCircuitInputsRegister(
|
||||
secret, attestation_id, passportData, { developmentMode: true }
|
||||
secret, attestation_id, passportData, n_dsc, k_dsc
|
||||
);
|
||||
console.log("inputs", inputs);
|
||||
|
||||
/*** Deploy contracts ***/
|
||||
await deployContracts();
|
||||
@@ -84,6 +105,11 @@ describe("Proof of Passport - Contracts - Register & Disclose flow", function ()
|
||||
await verifier_register.waitForDeployment();
|
||||
console.log('\x1b[34m%s\x1b[0m', `Verifier_register deployed to ${verifier_register.target}`);
|
||||
|
||||
Verifier_dsc = await ethers.getContractFactory("Verifier_dsc_4096");
|
||||
verifier_dsc = await Verifier_dsc.deploy();
|
||||
await verifier_register.waitForDeployment();
|
||||
console.log('\x1b[34m%s\x1b[0m', `Verifier_register deployed to ${verifier_register.target}`);
|
||||
|
||||
Formatter = await ethers.getContractFactory("Formatter");
|
||||
formatter = await Formatter.deploy(deployOptions);
|
||||
await formatter.waitForDeployment();
|
||||
@@ -91,7 +117,7 @@ describe("Proof of Passport - Contracts - Register & Disclose flow", function ()
|
||||
console.log('\x1b[34m%s\x1b[0m', `Formatter deployed to ${formatter.target}`);
|
||||
|
||||
Registry = await ethers.getContractFactory("Registry");
|
||||
registry = await Registry.deploy(formatRoot(inputs.merkle_root), deployOptions);
|
||||
registry = await Registry.deploy(formatRoot(inputs_csca.merkle_root), deployOptions);
|
||||
await registry.waitForDeployment();
|
||||
console.log('\x1b[34m%s\x1b[0m', `Registry deployed to ${registry.target}`);
|
||||
|
||||
@@ -101,12 +127,12 @@ describe("Proof of Passport - Contracts - Register & Disclose flow", function ()
|
||||
console.log('\x1b[34m%s\x1b[0m', `PoseidonT3 deployed to: ${poseidonT3.target}`);
|
||||
|
||||
const poseidonT3Address = poseidonT3.target;
|
||||
Register = await ethers.getContractFactory("ProofOfPassportRegister_dev", {
|
||||
Register = await ethers.getContractFactory("ProofOfPassportRegister", {
|
||||
libraries: {
|
||||
PoseidonT3: poseidonT3Address
|
||||
}
|
||||
});
|
||||
register = await Register.deploy(registry.target, deployOptions);
|
||||
register = await Register.deploy(registry.target, verifier_dsc.target, deployOptions);
|
||||
await register.waitForDeployment();
|
||||
console.log('\x1b[34m%s\x1b[0m', `Register deployed to ${register.target}`);
|
||||
|
||||
@@ -166,7 +192,9 @@ describe("Proof of Passport - Contracts - Register & Disclose flow", function ()
|
||||
|
||||
const rawCallData = await groth16.exportSolidityCallData(proof, publicSignals);
|
||||
parsedCallData_register = JSON.parse(`[${rawCallData}]`);
|
||||
console.log('parsedCallData_register', parsedCallData_register);
|
||||
formattedCallData_register = formatCallData_register(parsedCallData_register)
|
||||
console.log('formattedCallData_register', formattedCallData_register);
|
||||
|
||||
// Set fake commitments into the tree
|
||||
const commitments = [1, 2, 3];
|
||||
@@ -174,6 +202,31 @@ describe("Proof of Passport - Contracts - Register & Disclose flow", function ()
|
||||
await register.devAddCommitment(commitment); // this is a dev function and will not be deplyed in production
|
||||
imt.insert(BigInt(commitment));
|
||||
}
|
||||
|
||||
console.log('\x1b[32m%s\x1b[0m', 'Generating proof - DSC...');
|
||||
const proofResult = await groth16.fullProve(
|
||||
inputs_csca,
|
||||
path_dsc_wasm,
|
||||
path_dsc_zkey
|
||||
);
|
||||
proof_csca = proofResult.proof;
|
||||
publicSignals_csca = proofResult.publicSignals;
|
||||
console.log('\x1b[32m%s\x1b[0m', 'Proof generated - DSC');
|
||||
const vKey_dsc = JSON.parse(fs.readFileSync(path_dsc_vkey) as unknown as string);
|
||||
const verified_dsc = await groth16.verify(
|
||||
vKey_dsc,
|
||||
publicSignals_csca,
|
||||
proof_csca
|
||||
)
|
||||
assert(verified_dsc == true, 'Should verify')
|
||||
const rawCallData_dsc = await groth16.exportSolidityCallData(proof_csca, publicSignals_csca);
|
||||
console.log('\x1b[32m%s\x1b[0m', 'Proof verified - DSC');
|
||||
parsedCallData_dsc = JSON.parse(`[${rawCallData_dsc}]`);
|
||||
console.log('parsedCallData_dsc', parsedCallData_dsc);
|
||||
formattedCallData_dsc = formatCallData_dsc(parsedCallData_dsc)
|
||||
console.log('formattedCallData_dsc', formattedCallData_dsc);
|
||||
|
||||
|
||||
});
|
||||
|
||||
it("Verifier_register.sol verifies a correct proof - Register", async () => {
|
||||
@@ -182,45 +235,45 @@ describe("Proof of Passport - Contracts - Register & Disclose flow", function ()
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it("Register with a wrong proof should fail - Register", async function () {
|
||||
await expect(register
|
||||
.validateProof({ ...formattedCallData_register, a: [0, 0] }, 1))
|
||||
.to.be.revertedWith("Register__InvalidProof()")
|
||||
.catch(error => {
|
||||
assert(error.message.includes("Register__InvalidProof()"), "Expected revert with Register__InvalidProof(), but got another error");
|
||||
});
|
||||
});
|
||||
// it("Register with a wrong proof should fail - Register", async function () {
|
||||
// await expect(register
|
||||
// .validateProof({ ...formattedCallData_register, a: [0, 0] }, 1))
|
||||
// .to.be.revertedWith("Register__InvalidProof()")
|
||||
// .catch(error => {
|
||||
// assert(error.message.includes("Register__InvalidProof()"), "Expected revert with Register__InvalidProof(), but got another error");
|
||||
// });
|
||||
// });
|
||||
|
||||
it("Register with a wrong attestation id should fail - Register", async function () {
|
||||
await expect(register
|
||||
.validateProof({ ...formattedCallData_register, attestation_id: "10" }, 1))
|
||||
.to.be.revertedWith("Register__InvalidSignatureAlgorithm()")
|
||||
.catch(error => {
|
||||
assert(error.message.includes("Register__InvalidSignatureAlgorithm()"), "Expected revert with Register__InvalidSignatureAlgorithm(), but got another error");
|
||||
});
|
||||
});
|
||||
// it("Register with a wrong attestation id should fail - Register", async function () {
|
||||
// await expect(register
|
||||
// .validateProof({ ...formattedCallData_register, attestation_id: "10" }, 1))
|
||||
// .to.be.revertedWith("Register__InvalidSignatureAlgorithm()")
|
||||
// .catch(error => {
|
||||
// assert(error.message.includes("Register__InvalidSignatureAlgorithm()"), "Expected revert with Register__InvalidSignatureAlgorithm(), but got another error");
|
||||
// });
|
||||
// });
|
||||
|
||||
it("Register with a wrong signature algorithm should fail - Register", async function () {
|
||||
await expect(register
|
||||
.validateProof({ ...formattedCallData_register}, 2))
|
||||
.to.be.revertedWith("Register__InvalidSignatureAlgorithm()")
|
||||
.catch(error => {
|
||||
assert(error.message.includes("Register__InvalidSignatureAlgorithm()"), "Expected revert with Register__InvalidSignatureAlgorithm(), but got another error");
|
||||
});
|
||||
});
|
||||
// it("Register with a wrong signature algorithm should fail - Register", async function () {
|
||||
// await expect(register
|
||||
// .validateProof({ ...formattedCallData_register }, 2))
|
||||
// .to.be.revertedWith("Register__InvalidSignatureAlgorithm()")
|
||||
// .catch(error => {
|
||||
// assert(error.message.includes("Register__InvalidSignatureAlgorithm()"), "Expected revert with Register__InvalidSignatureAlgorithm(), but got another error");
|
||||
// });
|
||||
// });
|
||||
|
||||
it("Register with a wrong merkle root should fail - Register", async function () {
|
||||
await expect(register
|
||||
.validateProof({ ...formattedCallData_register, merkle_root: 0 }, 1))
|
||||
.to.be.revertedWith("Register__InvalidMerkleRoot()")
|
||||
.catch(error => {
|
||||
assert(error.message.includes("Register__InvalidMerkleRoot()"), "Expected revert with Register__InvalidMerkleRoot(), but got another error");
|
||||
});
|
||||
});
|
||||
// it("Register with a wrong merkle root should fail - Register", async function () {
|
||||
// await expect(register
|
||||
// .validateProof({ ...formattedCallData_register, merkle_root: 0 }, 1))
|
||||
// .to.be.revertedWith("Register__InvalidMerkleRoot()")
|
||||
// .catch(error => {
|
||||
// assert(error.message.includes("Register__InvalidMerkleRoot()"), "Expected revert with Register__InvalidMerkleRoot(), but got another error");
|
||||
// });
|
||||
// });
|
||||
|
||||
it("Register should succeed - Register", async function () {
|
||||
expect(await register
|
||||
.validateProof(formattedCallData_register, 1)).not.to.be.reverted;
|
||||
.validateProof(formattedCallData_register, formattedCallData_dsc, 1)).not.to.be.reverted;
|
||||
imt.insert(BigInt(formattedCallData_register.commitment));
|
||||
/// check if the merkle root is equal to the one from the imt
|
||||
// console.log('\x1b[34m%s\x1b[0m', `IMT Merkle root of TS Object - TS: ${imt.root}`);
|
||||
@@ -230,119 +283,120 @@ describe("Proof of Passport - Contracts - Register & Disclose flow", function ()
|
||||
|
||||
});
|
||||
|
||||
it("Register with the same proof should fail - Register", async function () {
|
||||
await expect(register
|
||||
.validateProof(formattedCallData_register, 1))
|
||||
.to.be.revertedWith("Register__YouAreUsingTheSameNullifierTwice()")
|
||||
.catch(error => {
|
||||
assert(error.message.includes("Register__YouAreUsingTheSameNullifierTwice()"), "Expected revert with Register__YouAreUsingTheSameNullifierTwice(), but got another error");
|
||||
});
|
||||
});
|
||||
// it("Register with the same proof should fail - Register", async function () {
|
||||
// await expect(register
|
||||
// .validateProof(formattedCallData_register, 1))
|
||||
// .to.be.revertedWith("Register__YouAreUsingTheSameNullifierTwice()")
|
||||
// .catch(error => {
|
||||
// assert(error.message.includes("Register__YouAreUsingTheSameNullifierTwice()"), "Expected revert with Register__YouAreUsingTheSameNullifierTwice(), but got another error");
|
||||
// });
|
||||
// });
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
/*** Disclose flow ***/
|
||||
describe("Proof of Passport - Disclose flow", function () {
|
||||
|
||||
//before all
|
||||
before(async function () {
|
||||
/*** Groth16 saga - Disclose***/
|
||||
// describe("Proof of Passport - Disclose flow", function () {
|
||||
|
||||
// refactor in generate inputs function
|
||||
bitmap = Array(90).fill("1");
|
||||
scope = BigInt(1).toString();
|
||||
user_address = await thirdAccount.getAddress();
|
||||
majority = ["1", "8"];
|
||||
input_disclose = generateCircuitInputsDisclose(
|
||||
inputs.secret,
|
||||
inputs.attestation_id,
|
||||
passportData,
|
||||
imt as any,
|
||||
majority,
|
||||
bitmap,
|
||||
scope,
|
||||
BigInt(user_address.toString()).toString()
|
||||
);
|
||||
// Generate the proof
|
||||
console.log('\x1b[32m%s\x1b[0m', 'Generating proof - Disclose');
|
||||
try {
|
||||
proof_result_disclose = await groth16.fullProve(
|
||||
input_disclose,
|
||||
path_disclose_wasm,
|
||||
path_disclose_zkey
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error generating proof:", error);
|
||||
throw error;
|
||||
}
|
||||
proof_disclose = proof_result_disclose.proof;
|
||||
publicSignals_disclose = proof_result_disclose.publicSignals;
|
||||
// //before all
|
||||
// before(async function () {
|
||||
// /*** Groth16 saga - Disclose***/
|
||||
|
||||
console.log('\x1b[32m%s\x1b[0m', 'Proof generated - Disclose');
|
||||
// Verify the proof
|
||||
vkey_disclose = JSON.parse(fs.readFileSync(path_disclose_vkey) as unknown as string);
|
||||
verified_disclose = await groth16.verify(
|
||||
vkey_disclose,
|
||||
publicSignals_disclose,
|
||||
proof_disclose
|
||||
)
|
||||
assert(verified_disclose == true, 'Should verify')
|
||||
console.log('\x1b[32m%s\x1b[0m', 'Proof verified - Disclose');
|
||||
rawCallData_disclose = await groth16.exportSolidityCallData(proof_disclose, publicSignals_disclose);
|
||||
parsedCallData_disclose = JSON.parse(`[${rawCallData_disclose}]`);
|
||||
formattedCallData_disclose = formatCallData_disclose(parsedCallData_disclose);
|
||||
console.log('formattedCallData_disclose', formattedCallData_disclose);
|
||||
// // refactor in generate inputs function
|
||||
// bitmap = Array(90).fill("1");
|
||||
// scope = BigInt(1).toString();
|
||||
// user_address = await thirdAccount.getAddress();
|
||||
// majority = ["1", "8"];
|
||||
// input_disclose = generateCircuitInputsDisclose(
|
||||
// inputs.secret,
|
||||
// inputs.attestation_id,
|
||||
// passportData,
|
||||
// imt as any,
|
||||
// majority,
|
||||
// bitmap,
|
||||
// scope,
|
||||
// BigInt(user_address.toString()).toString()
|
||||
// );
|
||||
// // Generate the proof
|
||||
// console.log('\x1b[32m%s\x1b[0m', 'Generating proof - Disclose');
|
||||
// try {
|
||||
// proof_result_disclose = await groth16.fullProve(
|
||||
// input_disclose,
|
||||
// path_disclose_wasm,
|
||||
// path_disclose_zkey
|
||||
// );
|
||||
// } catch (error) {
|
||||
// console.error("Error generating proof:", error);
|
||||
// throw error;
|
||||
// }
|
||||
// proof_disclose = proof_result_disclose.proof;
|
||||
// publicSignals_disclose = proof_result_disclose.publicSignals;
|
||||
|
||||
})
|
||||
it("SBT mint should fail with a wrong current date - SBT", async function () {
|
||||
await expect(sbt.mint({ ...formattedCallData_disclose, current_date: [2, 4, 0, 1, 0, 1] }))
|
||||
.to.be.revertedWith("Current date is not within the valid range")
|
||||
});
|
||||
it("SBT mint should fail with a wrong proof - SBT", async function () {
|
||||
await expect(sbt.mint({ ...formattedCallData_disclose, nullifier: 0 }))
|
||||
.to.be.revertedWith("Invalid Proof");
|
||||
});
|
||||
it("SBT mint should fail with a wrong merkle_root - SBT", async function () {
|
||||
await expect(sbt.mint({ ...formattedCallData_disclose, merkle_root: 0 }))
|
||||
.to.be.revertedWith("Invalid merkle root");
|
||||
});
|
||||
it("Verifier_disclose.sol verifies a correct proof - Disclose", async () => {
|
||||
expect(
|
||||
await verifier_disclose.verifyProof(parsedCallData_disclose[0], parsedCallData_disclose[1], parsedCallData_disclose[2], parsedCallData_disclose[3])
|
||||
).to.be.true;
|
||||
});
|
||||
it("SBT mint should succeed - SBT", async function () {
|
||||
await expect(
|
||||
sbt.mint(formattedCallData_disclose)
|
||||
).not.to.be.reverted;
|
||||
});
|
||||
it("URI et Expiry saga - SBT", async function () {
|
||||
const tokenURI = await sbt.tokenURI(0);
|
||||
const decodedTokenURI = Buffer.from(tokenURI.split(',')[1], 'base64').toString();
|
||||
let parsedTokenURI;
|
||||
try {
|
||||
parsedTokenURI = JSON.parse(decodedTokenURI);
|
||||
} catch (e) {
|
||||
assert(false, 'TokenURI is not a valid JSON');
|
||||
}
|
||||
// console.log('parsedTokenURI', parsedTokenURI);
|
||||
const expired = parsedTokenURI.attributes.find((attribute: any) => attribute.trait_type === 'Expired');
|
||||
expect(expired.value).to.equal('No');
|
||||
await time.increaseTo(2240161656); // 2040
|
||||
const tokenURIAfter = await sbt.tokenURI(0);
|
||||
const decodedTokenURIAfter = Buffer.from(tokenURIAfter.split(',')[1], 'base64').toString();
|
||||
const parsedTokenURIAfter = JSON.parse(decodedTokenURIAfter);
|
||||
const expiredAfter = parsedTokenURIAfter.attributes.find((attribute: any) => attribute.trait_type === 'Expired');
|
||||
expect(expiredAfter.value).to.equal('Yes');
|
||||
});
|
||||
// console.log('\x1b[32m%s\x1b[0m', 'Proof generated - Disclose');
|
||||
// // Verify the proof
|
||||
// vkey_disclose = JSON.parse(fs.readFileSync(path_disclose_vkey) as unknown as string);
|
||||
// verified_disclose = await groth16.verify(
|
||||
// vkey_disclose,
|
||||
// publicSignals_disclose,
|
||||
// proof_disclose
|
||||
// )
|
||||
// assert(verified_disclose == true, 'Should verify')
|
||||
// console.log('\x1b[32m%s\x1b[0m', 'Proof verified - Disclose');
|
||||
// rawCallData_disclose = await groth16.exportSolidityCallData(proof_disclose, publicSignals_disclose);
|
||||
// parsedCallData_disclose = JSON.parse(`[${rawCallData_disclose}]`);
|
||||
// formattedCallData_disclose = formatCallData_disclose(parsedCallData_disclose);
|
||||
// console.log('formattedCallData_disclose', formattedCallData_disclose);
|
||||
|
||||
it("SBT mint should fail with same proof twice - SBT", async function () {
|
||||
await expect(sbt.mint(formattedCallData_disclose))
|
||||
.to.be.revertedWith("Signature already nullified");
|
||||
});
|
||||
});
|
||||
// })
|
||||
// it("SBT mint should fail with a wrong current date - SBT", async function () {
|
||||
// await expect(sbt.mint({ ...formattedCallData_disclose, current_date: [2, 4, 0, 1, 0, 1] }))
|
||||
// .to.be.revertedWith("Current date is not within the valid range")
|
||||
// });
|
||||
// it("SBT mint should fail with a wrong proof - SBT", async function () {
|
||||
// await expect(sbt.mint({ ...formattedCallData_disclose, nullifier: 0 }))
|
||||
// .to.be.revertedWith("Invalid Proof");
|
||||
// });
|
||||
// it("SBT mint should fail with a wrong merkle_root - SBT", async function () {
|
||||
// await expect(sbt.mint({ ...formattedCallData_disclose, merkle_root: 0 }))
|
||||
// .to.be.revertedWith("Invalid merkle root");
|
||||
// });
|
||||
// it("Verifier_disclose.sol verifies a correct proof - Disclose", async () => {
|
||||
// expect(
|
||||
// await verifier_disclose.verifyProof(parsedCallData_disclose[0], parsedCallData_disclose[1], parsedCallData_disclose[2], parsedCallData_disclose[3])
|
||||
// ).to.be.true;
|
||||
// });
|
||||
// it("SBT mint should succeed - SBT", async function () {
|
||||
// await expect(
|
||||
// sbt.mint(formattedCallData_disclose)
|
||||
// ).not.to.be.reverted;
|
||||
// });
|
||||
// it("URI et Expiry saga - SBT", async function () {
|
||||
// const tokenURI = await sbt.tokenURI(0);
|
||||
// const decodedTokenURI = Buffer.from(tokenURI.split(',')[1], 'base64').toString();
|
||||
// let parsedTokenURI;
|
||||
// try {
|
||||
// parsedTokenURI = JSON.parse(decodedTokenURI);
|
||||
// } catch (e) {
|
||||
// assert(false, 'TokenURI is not a valid JSON');
|
||||
// }
|
||||
// // console.log('parsedTokenURI', parsedTokenURI);
|
||||
// const expired = parsedTokenURI.attributes.find((attribute: any) => attribute.trait_type === 'Expired');
|
||||
// expect(expired.value).to.equal('No');
|
||||
// await time.increaseTo(2240161656); // 2040
|
||||
// const tokenURIAfter = await sbt.tokenURI(0);
|
||||
// const decodedTokenURIAfter = Buffer.from(tokenURIAfter.split(',')[1], 'base64').toString();
|
||||
// const parsedTokenURIAfter = JSON.parse(decodedTokenURIAfter);
|
||||
// const expiredAfter = parsedTokenURIAfter.attributes.find((attribute: any) => attribute.trait_type === 'Expired');
|
||||
// expect(expiredAfter.value).to.equal('Yes');
|
||||
// });
|
||||
|
||||
// it("SBT mint should fail with same proof twice - SBT", async function () {
|
||||
// await expect(sbt.mint(formattedCallData_disclose))
|
||||
// .to.be.revertedWith("Signature already nullified");
|
||||
// });
|
||||
// });
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user