Skip to content

Commit 149c3d0

Browse files
committed
add mne verifier
1 parent edf747c commit 149c3d0

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
// SPDX-License-Identifier: GPL-3.0
2+
/*
3+
Copyright 2021 0KIMS association.
4+
5+
This file is generated with [snarkJS](https://github.com/iden3/snarkjs).
6+
7+
snarkJS is a free software: you can redistribute it and/or modify it
8+
under the terms of the GNU General Public License as published by
9+
the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
snarkJS is distributed in the hope that it will be useful, but WITHOUT
13+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15+
License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with snarkJS. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
pragma solidity >=0.7.0 <0.9.0;
22+
23+
contract PMNEOptVerifier2 {
24+
// Scalar field size
25+
uint256 constant r =
26+
21888242871839275222246405745257275088548364400416034343698204186575808495617;
27+
// Base field size
28+
uint256 constant q =
29+
21888242871839275222246405745257275088696311157297823662689037894645226208583;
30+
31+
// Verification Key data
32+
uint256 constant alphax =
33+
20491192805390485299153009773594534940189261866228447918068658471970481763042;
34+
uint256 constant alphay =
35+
9383485363053290200918347156157836566562967994039712273449902621266178545958;
36+
uint256 constant betax1 =
37+
4252822878758300859123897981450591353533073413197771768651442665752259397132;
38+
uint256 constant betax2 =
39+
6375614351688725206403948262868962793625744043794305715222011528459656738731;
40+
uint256 constant betay1 =
41+
21847035105528745403288232691147584728191162732299865338377159692350059136679;
42+
uint256 constant betay2 =
43+
10505242626370262277552901082094356697409835680220590971873171140371331206856;
44+
uint256 constant gammax1 =
45+
11559732032986387107991004021392285783925812861821192530917403151452391805634;
46+
uint256 constant gammax2 =
47+
10857046999023057135944570762232829481370756359578518086990519993285655852781;
48+
uint256 constant gammay1 =
49+
4082367875863433681332203403145435568316851327593401208105741076214120093531;
50+
uint256 constant gammay2 =
51+
8495653923123431417604973247489272438418190587263600148770280649306958101930;
52+
uint256 constant deltax1 =
53+
18775510689063216133553744691154437390268986238273920534357642191923884399534;
54+
uint256 constant deltax2 =
55+
3138740882803963671493524280751392232830124651107385080168659518096192747568;
56+
uint256 constant deltay1 =
57+
21611601686554111167434110819161129703380067991196296896538153252664002263702;
58+
uint256 constant deltay2 =
59+
15731366851749545531581248604591492786301792216869814015242322496262570479877;
60+
61+
uint256 constant IC0x =
62+
9977038873367262886195315489196391079966505497079977073633800248955771927893;
63+
uint256 constant IC0y =
64+
5385817397342392984864277380978550356202411093574841488207232050603850296736;
65+
66+
uint256 constant IC1x =
67+
1140910774619075569104685306135178356060035546964139713694970173836299704748;
68+
uint256 constant IC1y =
69+
2348933249983899680547085680749969045164121384439813966026217488112301238877;
70+
71+
uint256 constant IC2x =
72+
17331689126957356334349359398356442905134716151922462938270150774114689115943;
73+
uint256 constant IC2y =
74+
5088930689924209474541910882615958763907702825000420010910765073392199606376;
75+
76+
uint256 constant IC3x =
77+
9634445201649234246683123434545974656072498629317000674956793993179361214698;
78+
uint256 constant IC3y =
79+
6731939726549893499947379478593769463834525449830284348624938069013518555815;
80+
81+
uint256 constant IC4x =
82+
4335212990295266407871990284622188899632501438665773754469526208239098092051;
83+
uint256 constant IC4y =
84+
21268024059527578545842352192089225100717127580676019830766086489124307441559;
85+
86+
uint256 constant IC5x =
87+
15780995396140346604134311722038835872296707099821808617697242402223090791885;
88+
uint256 constant IC5y =
89+
6455310990522558695204193864016830645646215301175593809153774355010036058651;
90+
91+
// Memory data
92+
uint16 constant pVk = 0;
93+
uint16 constant pPairing = 128;
94+
95+
uint16 constant pLastMem = 896;
96+
97+
function verifyProof(
98+
uint[2] calldata _pA,
99+
uint[2][2] calldata _pB,
100+
uint[2] calldata _pC,
101+
uint[5] calldata _pubSignals
102+
) public view returns (bool) {
103+
assembly {
104+
function checkField(v) {
105+
if iszero(lt(v, q)) {
106+
mstore(0, 0)
107+
return(0, 0x20)
108+
}
109+
}
110+
111+
// G1 function to multiply a G1 value(x,y) to value in an address
112+
function g1_mulAccC(pR, x, y, s) {
113+
let success
114+
let mIn := mload(0x40)
115+
mstore(mIn, x)
116+
mstore(add(mIn, 32), y)
117+
mstore(add(mIn, 64), s)
118+
119+
success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64)
120+
121+
if iszero(success) {
122+
mstore(0, 0)
123+
return(0, 0x20)
124+
}
125+
126+
mstore(add(mIn, 64), mload(pR))
127+
mstore(add(mIn, 96), mload(add(pR, 32)))
128+
129+
success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64)
130+
131+
if iszero(success) {
132+
mstore(0, 0)
133+
return(0, 0x20)
134+
}
135+
}
136+
137+
function checkPairing(pA, pB, pC, pubSignals, pMem) -> isOk {
138+
let _pPairing := add(pMem, pPairing)
139+
let _pVk := add(pMem, pVk)
140+
141+
mstore(_pVk, IC0x)
142+
mstore(add(_pVk, 32), IC0y)
143+
144+
// Compute the linear combination vk_x
145+
146+
g1_mulAccC(_pVk, IC1x, IC1y, calldataload(add(pubSignals, 0)))
147+
148+
g1_mulAccC(_pVk, IC2x, IC2y, calldataload(add(pubSignals, 32)))
149+
150+
g1_mulAccC(_pVk, IC3x, IC3y, calldataload(add(pubSignals, 64)))
151+
152+
g1_mulAccC(_pVk, IC4x, IC4y, calldataload(add(pubSignals, 96)))
153+
154+
g1_mulAccC(_pVk, IC5x, IC5y, calldataload(add(pubSignals, 128)))
155+
156+
// -A
157+
mstore(_pPairing, calldataload(pA))
158+
mstore(add(_pPairing, 32), mod(sub(q, calldataload(add(pA, 32))), q))
159+
160+
// B
161+
mstore(add(_pPairing, 64), calldataload(pB))
162+
mstore(add(_pPairing, 96), calldataload(add(pB, 32)))
163+
mstore(add(_pPairing, 128), calldataload(add(pB, 64)))
164+
mstore(add(_pPairing, 160), calldataload(add(pB, 96)))
165+
166+
// alpha1
167+
mstore(add(_pPairing, 192), alphax)
168+
mstore(add(_pPairing, 224), alphay)
169+
170+
// beta2
171+
mstore(add(_pPairing, 256), betax1)
172+
mstore(add(_pPairing, 288), betax2)
173+
mstore(add(_pPairing, 320), betay1)
174+
mstore(add(_pPairing, 352), betay2)
175+
176+
// vk_x
177+
mstore(add(_pPairing, 384), mload(add(pMem, pVk)))
178+
mstore(add(_pPairing, 416), mload(add(pMem, add(pVk, 32))))
179+
180+
// gamma2
181+
mstore(add(_pPairing, 448), gammax1)
182+
mstore(add(_pPairing, 480), gammax2)
183+
mstore(add(_pPairing, 512), gammay1)
184+
mstore(add(_pPairing, 544), gammay2)
185+
186+
// C
187+
mstore(add(_pPairing, 576), calldataload(pC))
188+
mstore(add(_pPairing, 608), calldataload(add(pC, 32)))
189+
190+
// delta2
191+
mstore(add(_pPairing, 640), deltax1)
192+
mstore(add(_pPairing, 672), deltax2)
193+
mstore(add(_pPairing, 704), deltay1)
194+
mstore(add(_pPairing, 736), deltay2)
195+
196+
let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20)
197+
198+
isOk := and(success, mload(_pPairing))
199+
}
200+
201+
let pMem := mload(0x40)
202+
mstore(0x40, add(pMem, pLastMem))
203+
204+
// Validate that all evaluations ∈ F
205+
206+
checkField(calldataload(add(_pubSignals, 0)))
207+
208+
checkField(calldataload(add(_pubSignals, 32)))
209+
210+
checkField(calldataload(add(_pubSignals, 64)))
211+
212+
checkField(calldataload(add(_pubSignals, 96)))
213+
214+
checkField(calldataload(add(_pubSignals, 128)))
215+
216+
checkField(calldataload(add(_pubSignals, 160)))
217+
218+
// Validate all evaluations
219+
let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem)
220+
221+
mstore(0, isValid)
222+
return(0, 0x20)
223+
}
224+
}
225+
}

contracts/registration/types.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ bytes32 constant Z_UNIVERSAL_4096 = keccak256("Z_UNIVERSAL_4096");
1818
bytes32 constant Z_UNIVERSAL_2048 = keccak256("Z_UNIVERSAL_2048");
1919
bytes32 constant Z_INTERNAL = keccak256("Z_INTERNAL");
2020
bytes32 constant Z_INTERNAL_OPT = keccak256("Z_INTERNAL_OPT");
21+
bytes32 constant Z_MNE_OPT = keccak256("Z_MNE_OPT");

deploy/10_setup.migration.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
PRSASHA1Dispatcher__factory,
88
PInternalVerifier2__factory,
99
PInternalOptVerifier2__factory,
10+
PMNEOptVerifier2__factory,
1011
PUniversal2048Verifier2__factory,
1112
PUniversal4096Verifier2__factory,
1213
Registration2Mock__factory,
@@ -26,6 +27,7 @@ import {
2627
P_RSA_SHA1_2688_3,
2728
Z_INTERNAL,
2829
Z_INTERNAL_OPT,
30+
Z_MNE_OPT,
2931
Z_UNIVERSAL_2048,
3032
Z_UNIVERSAL_4096,
3133
} from "@/scripts/utils/types";
@@ -56,6 +58,7 @@ export = async (deployer: Deployer) => {
5658
const pUniversal4096Verifier = await deployer.deployed(PUniversal4096Verifier2__factory);
5759
const pInternalVerifier = await deployer.deployed(PInternalVerifier2__factory);
5860
const pInternalOptVerifier = await deployer.deployed(PInternalOptVerifier2__factory);
61+
const pMneOptVerifier = await deployer.deployed(PMNEOptVerifier2__factory);
5962

6063
await registration.mockAddCertificateDispatcher(C_RSA_SHA1_4096, await cRsa4096Sha1Dispatcher.getAddress());
6164
await registration.mockAddCertificateDispatcher(C_RSA_SHA1_2048, await cRsa2048Sha1Dispatcher.getAddress());
@@ -76,6 +79,7 @@ export = async (deployer: Deployer) => {
7679
await registration.mockAddPassportVerifier(Z_UNIVERSAL_4096, await pUniversal4096Verifier.getAddress());
7780
await registration.mockAddPassportVerifier(Z_INTERNAL, await pInternalVerifier.getAddress());
7881
await registration.mockAddPassportVerifier(Z_INTERNAL_OPT, await pInternalOptVerifier.getAddress());
82+
await registration.mockAddPassportVerifier(Z_MNE_OPT, await pMneOptVerifier.getAddress());
7983

8084
await stateKeeper.mockAddRegistrations([config.registrationName], [await registration.getAddress()]);
8185
};

deploy/helpers/verifiers/passport.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Deployer } from "@solarity/hardhat-migrate";
33
import {
44
PInternalVerifier2__factory,
55
PInternalOptVerifier2__factory,
6+
PMNEOptVerifier2__factory,
67
PUniversal2048Verifier2__factory,
78
PUniversal4096Verifier2__factory,
89
} from "@ethers-v6";
@@ -13,4 +14,6 @@ export const deployPVerifiers = async (deployer: Deployer) => {
1314

1415
await deployer.deploy(PInternalVerifier2__factory);
1516
await deployer.deploy(PInternalOptVerifier2__factory);
17+
18+
await deployer.deploy(PMNEOptVerifier2__factory);
1619
};

scripts/utils/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export const Z_UNIVERSAL_4096 = ethers.solidityPackedKeccak256(["string"], ["Z_U
1717
export const Z_UNIVERSAL_2048 = ethers.solidityPackedKeccak256(["string"], ["Z_UNIVERSAL_2048"]);
1818
export const Z_INTERNAL = ethers.solidityPackedKeccak256(["string"], ["Z_INTERNAL"]);
1919
export const Z_INTERNAL_OPT = ethers.solidityPackedKeccak256(["string"], ["Z_INTERNAL_OPT"]);
20+
export const Z_MNE_OPT = ethers.solidityPackedKeccak256(["string"], ["Z_MNE_OPT"]);

0 commit comments

Comments
 (0)