Skip to content

Commit 31bd019

Browse files
authored
Add TD3QueryProofVerifier to the SDK (#42)
* Added ForeignCountryQueryProofVerifier to the SDK * Updated versions * Updated verifier name * Fixed typo. Bump solhint
1 parent 74e6c54 commit 31bd019

File tree

4 files changed

+482
-17
lines changed

4 files changed

+482
-17
lines changed

.solhintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
./contracts/mock
44
./contracts/utils/Date2Time.sol
55
./contracts/libraries/Poseidon.sol
6+
./contracts/sdk/verifier/TD3QueryProofVerifier.sol
Lines changed: 392 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,392 @@
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+
/**
24+
* @notice See docs:
25+
* - https://www.icao.int/publications/Documents/9303_p4_cons_en.pdf
26+
* - https://www.icao.int/publications/pages/publication.aspx?docnum=9303
27+
*/
28+
contract TD3QueryProofVerifier {
29+
// Scalar field size
30+
uint256 constant r =
31+
21888242871839275222246405745257275088548364400416034343698204186575808495617;
32+
// Base field size
33+
uint256 constant q =
34+
21888242871839275222246405745257275088696311157297823662689037894645226208583;
35+
36+
// Verification Key data
37+
uint256 constant alphax =
38+
20491192805390485299153009773594534940189261866228447918068658471970481763042;
39+
uint256 constant alphay =
40+
9383485363053290200918347156157836566562967994039712273449902621266178545958;
41+
uint256 constant betax1 =
42+
4252822878758300859123897981450591353533073413197771768651442665752259397132;
43+
uint256 constant betax2 =
44+
6375614351688725206403948262868962793625744043794305715222011528459656738731;
45+
uint256 constant betay1 =
46+
21847035105528745403288232691147584728191162732299865338377159692350059136679;
47+
uint256 constant betay2 =
48+
10505242626370262277552901082094356697409835680220590971873171140371331206856;
49+
uint256 constant gammax1 =
50+
11559732032986387107991004021392285783925812861821192530917403151452391805634;
51+
uint256 constant gammax2 =
52+
10857046999023057135944570762232829481370756359578518086990519993285655852781;
53+
uint256 constant gammay1 =
54+
4082367875863433681332203403145435568316851327593401208105741076214120093531;
55+
uint256 constant gammay2 =
56+
8495653923123431417604973247489272438418190587263600148770280649306958101930;
57+
uint256 constant deltax1 =
58+
7918804905557415959065306318808180222510617915496909222590026551054032447946;
59+
uint256 constant deltax2 =
60+
21570251404699465383997747291488217929717666456841626090639890781888118671301;
61+
uint256 constant deltay1 =
62+
12227424247988085355962415874861162192700194789671861533883587440189951371756;
63+
uint256 constant deltay2 =
64+
12770166122086015482601558785592896923566484431398640759266017594312924815567;
65+
66+
uint256 constant IC0x =
67+
6732148179208849637983747353365901588397162895318630817123480374080083282971;
68+
uint256 constant IC0y =
69+
19873949065566504430727995765849869146001433219930688585328131105756469657143;
70+
71+
uint256 constant IC1x =
72+
11864162617912496828298003467384864631982500302333274646587366337078555234489;
73+
uint256 constant IC1y =
74+
13243913994381161699707280472912027185689309772034751789116324640110390023371;
75+
76+
uint256 constant IC2x =
77+
2152477988175581528811718075677955115933176953626428816782400018208138687886;
78+
uint256 constant IC2y =
79+
21304911914077379900246443656312002593519535152202814009232163711234367112738;
80+
81+
uint256 constant IC3x =
82+
655824025125330549284978305816195737337932028870937776138550891442072289948;
83+
uint256 constant IC3y =
84+
20598858092559550226833520247045145762608230923451821187319214985922050846038;
85+
86+
uint256 constant IC4x =
87+
14742252465638304198936790169928024792639417386063537385069919518789823887179;
88+
uint256 constant IC4y =
89+
11337316405569947493711977431693666584029481240773156487041804459132673205450;
90+
91+
uint256 constant IC5x =
92+
12034782930432063083678167155130316689457611721454102965227744410026820074016;
93+
uint256 constant IC5y =
94+
21297736145772749832688714810567013476509932280880601567033076624935868383201;
95+
96+
uint256 constant IC6x =
97+
2706481871710765193197021099125896543139620734589944432849123628744219374892;
98+
uint256 constant IC6y =
99+
15287383566271981122068320654518465218128376525720170909726983453405044943768;
100+
101+
uint256 constant IC7x =
102+
7978645990063631634162396664760287651108002672865518575953754948353982493554;
103+
uint256 constant IC7y =
104+
20956723562136283018604754105269513044659906446896960689020416153517420775922;
105+
106+
uint256 constant IC8x =
107+
3728653732928280111249403035073119520674644229189164280202302173380243104075;
108+
uint256 constant IC8y =
109+
16163273024178534444167593159822692810788882601956973537917276827007542397221;
110+
111+
uint256 constant IC9x =
112+
13072642442623939270235765377554699919342817946694595276171859801046545521710;
113+
uint256 constant IC9y =
114+
21413388219296908334424123093894914309747625936133826120899255372632730483602;
115+
116+
uint256 constant IC10x =
117+
19238193920918256704263366411167280812130230838694574584627126259800279172487;
118+
uint256 constant IC10y =
119+
11042922968382520660623610190279180134634404688548083728225080910959607041532;
120+
121+
uint256 constant IC11x =
122+
13465041547313740870199973573586148600932466645721728925788548008697792870178;
123+
uint256 constant IC11y =
124+
2559497114387644954353974936106194823597101622468833536920942555963880853329;
125+
126+
uint256 constant IC12x =
127+
6448115210589867889850688479237974973905996197919847455498475691758817670950;
128+
uint256 constant IC12y =
129+
19610889249784031370406929000525753180537103121317363464996692338164817481106;
130+
131+
uint256 constant IC13x =
132+
8034742873640949793142891395586293753027108654291231391522184497800396109309;
133+
uint256 constant IC13y =
134+
7923448118703035000195299309905740488123789722602373073176587770765305949745;
135+
136+
uint256 constant IC14x =
137+
4412641139681422637473097733369191171360304989207951202089689244738853322110;
138+
uint256 constant IC14y =
139+
1438609447983686059884494923809890916669443886956332236564712850897233294835;
140+
141+
uint256 constant IC15x =
142+
10488201530579648685867148179262826225690700743573064742829591842009677466575;
143+
uint256 constant IC15y =
144+
4375022409712288709286561498591261826348881658590497294732909090242139678171;
145+
146+
uint256 constant IC16x =
147+
19293432214388428523763043124057133693629381745542469639893005844708617050284;
148+
uint256 constant IC16y =
149+
17843177594579490126508770229083098595769660386800499306761596137656090066332;
150+
151+
uint256 constant IC17x =
152+
6206938962385904113401091463201072701392307427359406618825216324430582525273;
153+
uint256 constant IC17y =
154+
18082540415596003368776483988779423351001376667054755928356058038277536930387;
155+
156+
uint256 constant IC18x =
157+
17890636146909933289083883133111861315882487666849322700532595530150198649671;
158+
uint256 constant IC18y =
159+
21563509439002684398882428854694060906558676506399748043993401712962351835106;
160+
161+
uint256 constant IC19x =
162+
10974696587630188372824313954397303641901217425802900893272334271312368845069;
163+
uint256 constant IC19y =
164+
12238669004314126008684602719182501746500243123046763687231219646153001389629;
165+
166+
uint256 constant IC20x =
167+
19575237823423728300205986723029875361585107315887893407358497248606540167075;
168+
uint256 constant IC20y =
169+
21856545102172887619716877437543815339890222972623263801463768298196756131238;
170+
171+
uint256 constant IC21x =
172+
9118149073253857404442977465930450917212144208873413022088319455042070716353;
173+
uint256 constant IC21y =
174+
17307300866620426678565985126286452831664332652215420691654507066477234886723;
175+
176+
uint256 constant IC22x =
177+
9546940421895954373295434859172394272026752072203716476106496545033572581678;
178+
uint256 constant IC22y =
179+
15407944163895440882179937094826152911853840893366019509021703800156910297582;
180+
181+
uint256 constant IC23x =
182+
6314518912771494436366538874810905757830781334166745139148309552057990561668;
183+
uint256 constant IC23y =
184+
20331496807385136145073919378214977596267120508758015294153216074315139935895;
185+
186+
// Memory data
187+
uint16 constant pVk = 0;
188+
uint16 constant pPairing = 128;
189+
190+
uint16 constant pLastMem = 896;
191+
192+
function verifyProof(
193+
uint[2] calldata _pA,
194+
uint[2][2] calldata _pB,
195+
uint[2] calldata _pC,
196+
uint[23] calldata _pubSignals
197+
) public view returns (bool) {
198+
assembly {
199+
function checkField(v) {
200+
if iszero(lt(v, q)) {
201+
mstore(0, 0)
202+
return(0, 0x20)
203+
}
204+
}
205+
206+
// G1 function to multiply a G1 value(x,y) to value in an address
207+
function g1_mulAccC(pR, x, y, s) {
208+
let success
209+
let mIn := mload(0x40)
210+
mstore(mIn, x)
211+
mstore(add(mIn, 32), y)
212+
mstore(add(mIn, 64), s)
213+
214+
success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64)
215+
216+
if iszero(success) {
217+
mstore(0, 0)
218+
return(0, 0x20)
219+
}
220+
221+
mstore(add(mIn, 64), mload(pR))
222+
mstore(add(mIn, 96), mload(add(pR, 32)))
223+
224+
success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64)
225+
226+
if iszero(success) {
227+
mstore(0, 0)
228+
return(0, 0x20)
229+
}
230+
}
231+
232+
function checkPairing(pA, pB, pC, pubSignals, pMem) -> isOk {
233+
let _pPairing := add(pMem, pPairing)
234+
let _pVk := add(pMem, pVk)
235+
236+
mstore(_pVk, IC0x)
237+
mstore(add(_pVk, 32), IC0y)
238+
239+
// Compute the linear combination vk_x
240+
241+
g1_mulAccC(_pVk, IC1x, IC1y, calldataload(add(pubSignals, 0)))
242+
243+
g1_mulAccC(_pVk, IC2x, IC2y, calldataload(add(pubSignals, 32)))
244+
245+
g1_mulAccC(_pVk, IC3x, IC3y, calldataload(add(pubSignals, 64)))
246+
247+
g1_mulAccC(_pVk, IC4x, IC4y, calldataload(add(pubSignals, 96)))
248+
249+
g1_mulAccC(_pVk, IC5x, IC5y, calldataload(add(pubSignals, 128)))
250+
251+
g1_mulAccC(_pVk, IC6x, IC6y, calldataload(add(pubSignals, 160)))
252+
253+
g1_mulAccC(_pVk, IC7x, IC7y, calldataload(add(pubSignals, 192)))
254+
255+
g1_mulAccC(_pVk, IC8x, IC8y, calldataload(add(pubSignals, 224)))
256+
257+
g1_mulAccC(_pVk, IC9x, IC9y, calldataload(add(pubSignals, 256)))
258+
259+
g1_mulAccC(_pVk, IC10x, IC10y, calldataload(add(pubSignals, 288)))
260+
261+
g1_mulAccC(_pVk, IC11x, IC11y, calldataload(add(pubSignals, 320)))
262+
263+
g1_mulAccC(_pVk, IC12x, IC12y, calldataload(add(pubSignals, 352)))
264+
265+
g1_mulAccC(_pVk, IC13x, IC13y, calldataload(add(pubSignals, 384)))
266+
267+
g1_mulAccC(_pVk, IC14x, IC14y, calldataload(add(pubSignals, 416)))
268+
269+
g1_mulAccC(_pVk, IC15x, IC15y, calldataload(add(pubSignals, 448)))
270+
271+
g1_mulAccC(_pVk, IC16x, IC16y, calldataload(add(pubSignals, 480)))
272+
273+
g1_mulAccC(_pVk, IC17x, IC17y, calldataload(add(pubSignals, 512)))
274+
275+
g1_mulAccC(_pVk, IC18x, IC18y, calldataload(add(pubSignals, 544)))
276+
277+
g1_mulAccC(_pVk, IC19x, IC19y, calldataload(add(pubSignals, 576)))
278+
279+
g1_mulAccC(_pVk, IC20x, IC20y, calldataload(add(pubSignals, 608)))
280+
281+
g1_mulAccC(_pVk, IC21x, IC21y, calldataload(add(pubSignals, 640)))
282+
283+
g1_mulAccC(_pVk, IC22x, IC22y, calldataload(add(pubSignals, 672)))
284+
285+
g1_mulAccC(_pVk, IC23x, IC23y, calldataload(add(pubSignals, 704)))
286+
287+
// -A
288+
mstore(_pPairing, calldataload(pA))
289+
mstore(add(_pPairing, 32), mod(sub(q, calldataload(add(pA, 32))), q))
290+
291+
// B
292+
mstore(add(_pPairing, 64), calldataload(pB))
293+
mstore(add(_pPairing, 96), calldataload(add(pB, 32)))
294+
mstore(add(_pPairing, 128), calldataload(add(pB, 64)))
295+
mstore(add(_pPairing, 160), calldataload(add(pB, 96)))
296+
297+
// alpha1
298+
mstore(add(_pPairing, 192), alphax)
299+
mstore(add(_pPairing, 224), alphay)
300+
301+
// beta2
302+
mstore(add(_pPairing, 256), betax1)
303+
mstore(add(_pPairing, 288), betax2)
304+
mstore(add(_pPairing, 320), betay1)
305+
mstore(add(_pPairing, 352), betay2)
306+
307+
// vk_x
308+
mstore(add(_pPairing, 384), mload(add(pMem, pVk)))
309+
mstore(add(_pPairing, 416), mload(add(pMem, add(pVk, 32))))
310+
311+
// gamma2
312+
mstore(add(_pPairing, 448), gammax1)
313+
mstore(add(_pPairing, 480), gammax2)
314+
mstore(add(_pPairing, 512), gammay1)
315+
mstore(add(_pPairing, 544), gammay2)
316+
317+
// C
318+
mstore(add(_pPairing, 576), calldataload(pC))
319+
mstore(add(_pPairing, 608), calldataload(add(pC, 32)))
320+
321+
// delta2
322+
mstore(add(_pPairing, 640), deltax1)
323+
mstore(add(_pPairing, 672), deltax2)
324+
mstore(add(_pPairing, 704), deltay1)
325+
mstore(add(_pPairing, 736), deltay2)
326+
327+
let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20)
328+
329+
isOk := and(success, mload(_pPairing))
330+
}
331+
332+
let pMem := mload(0x40)
333+
mstore(0x40, add(pMem, pLastMem))
334+
335+
// Validate that all evaluations ∈ F
336+
337+
checkField(calldataload(add(_pubSignals, 0)))
338+
339+
checkField(calldataload(add(_pubSignals, 32)))
340+
341+
checkField(calldataload(add(_pubSignals, 64)))
342+
343+
checkField(calldataload(add(_pubSignals, 96)))
344+
345+
checkField(calldataload(add(_pubSignals, 128)))
346+
347+
checkField(calldataload(add(_pubSignals, 160)))
348+
349+
checkField(calldataload(add(_pubSignals, 192)))
350+
351+
checkField(calldataload(add(_pubSignals, 224)))
352+
353+
checkField(calldataload(add(_pubSignals, 256)))
354+
355+
checkField(calldataload(add(_pubSignals, 288)))
356+
357+
checkField(calldataload(add(_pubSignals, 320)))
358+
359+
checkField(calldataload(add(_pubSignals, 352)))
360+
361+
checkField(calldataload(add(_pubSignals, 384)))
362+
363+
checkField(calldataload(add(_pubSignals, 416)))
364+
365+
checkField(calldataload(add(_pubSignals, 448)))
366+
367+
checkField(calldataload(add(_pubSignals, 480)))
368+
369+
checkField(calldataload(add(_pubSignals, 512)))
370+
371+
checkField(calldataload(add(_pubSignals, 544)))
372+
373+
checkField(calldataload(add(_pubSignals, 576)))
374+
375+
checkField(calldataload(add(_pubSignals, 608)))
376+
377+
checkField(calldataload(add(_pubSignals, 640)))
378+
379+
checkField(calldataload(add(_pubSignals, 672)))
380+
381+
checkField(calldataload(add(_pubSignals, 704)))
382+
383+
checkField(calldataload(add(_pubSignals, 736)))
384+
385+
// Validate all evaluations
386+
let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem)
387+
388+
mstore(0, isValid)
389+
return(0, 0x20)
390+
}
391+
}
392+
}

0 commit comments

Comments
 (0)