Skip to content

Commit 49d7736

Browse files
authored
Merge pull request #135 from hyperledger-labs/qurrency
Qurrency implementation
2 parents 3dfb08e + ca18631 commit 49d7736

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+5223
-44
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright © 2025 Kaleido, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
pragma solidity ^0.8.27;
17+
18+
import {Groth16Verifier_AnonNullifierQurrencyTransfer} from "../verifiers/verifier_anon_nullifier_qurrency_transfer.sol";
19+
import {Commonlib} from "../lib/common.sol";
20+
21+
contract TestVerifierQurrency {
22+
Groth16Verifier_AnonNullifierQurrencyTransfer verifier;
23+
24+
bool public lastSuccess = false;
25+
26+
constructor() {
27+
verifier = new Groth16Verifier_AnonNullifierQurrencyTransfer();
28+
}
29+
30+
function verifyProof(
31+
Commonlib.Proof memory proof,
32+
uint256[9] memory publicInputs
33+
) public returns (bool) {
34+
bool result = verifier.verifyProof(
35+
proof.pA,
36+
proof.pB,
37+
proof.pC,
38+
publicInputs
39+
);
40+
lastSuccess = result;
41+
require(result, "Verification failed");
42+
return result;
43+
}
44+
}
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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 Groth16Verifier_AnonNullifierQurrencyTransfer {
24+
// Scalar field size
25+
uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
26+
// Base field size
27+
uint256 constant q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
28+
29+
// Verification Key data
30+
uint256 constant alphax = 20491192805390485299153009773594534940189261866228447918068658471970481763042;
31+
uint256 constant alphay = 9383485363053290200918347156157836566562967994039712273449902621266178545958;
32+
uint256 constant betax1 = 4252822878758300859123897981450591353533073413197771768651442665752259397132;
33+
uint256 constant betax2 = 6375614351688725206403948262868962793625744043794305715222011528459656738731;
34+
uint256 constant betay1 = 21847035105528745403288232691147584728191162732299865338377159692350059136679;
35+
uint256 constant betay2 = 10505242626370262277552901082094356697409835680220590971873171140371331206856;
36+
uint256 constant gammax1 = 11559732032986387107991004021392285783925812861821192530917403151452391805634;
37+
uint256 constant gammax2 = 10857046999023057135944570762232829481370756359578518086990519993285655852781;
38+
uint256 constant gammay1 = 4082367875863433681332203403145435568316851327593401208105741076214120093531;
39+
uint256 constant gammay2 = 8495653923123431417604973247489272438418190587263600148770280649306958101930;
40+
uint256 constant deltax1 = 11559732032986387107991004021392285783925812861821192530917403151452391805634;
41+
uint256 constant deltax2 = 10857046999023057135944570762232829481370756359578518086990519993285655852781;
42+
uint256 constant deltay1 = 4082367875863433681332203403145435568316851327593401208105741076214120093531;
43+
uint256 constant deltay2 = 8495653923123431417604973247489272438418190587263600148770280649306958101930;
44+
45+
46+
uint256 constant IC0x = 5903941879013718988081172462893635473765585811323635470514735721095700963702;
47+
uint256 constant IC0y = 21080368968148086049976470300075598267152959623874995939107123143909817947630;
48+
49+
uint256 constant IC1x = 277297534438112389180931574864286821885419665717177767601706304294709064040;
50+
uint256 constant IC1y = 11466710444362729136960387929371222359273358701587036714483949566745055621830;
51+
52+
uint256 constant IC2x = 5619666232197961834865995108107159282382923386466834757057363204096037651764;
53+
uint256 constant IC2y = 15973294500823479376020315199547194992254305102551062619795669271634881254131;
54+
55+
uint256 constant IC3x = 16994365193196666529444222678662986964837189489839761617081637536084667700567;
56+
uint256 constant IC3y = 19320170842519457147495348046335776444028148306160216329435959930056589238336;
57+
58+
uint256 constant IC4x = 8498610473957547996900321269894382432325081969475947517083968711252676854434;
59+
uint256 constant IC4y = 12728107717434555757183263731054279132059277069143869361510124517810824142633;
60+
61+
uint256 constant IC5x = 18511559663930149009135574067307212897873211022818368060918697035060259565029;
62+
uint256 constant IC5y = 20804557254714858706397640997162389384167991925053762942540110532256230134678;
63+
64+
uint256 constant IC6x = 9373814069952600731332993487809060613417950312335152043923307393139824873734;
65+
uint256 constant IC6y = 16865467207038098604194455172286157376570508997936659278795086623618594346915;
66+
67+
uint256 constant IC7x = 12145839442248256913972414977521448580312614560974030542947427181163264459741;
68+
uint256 constant IC7y = 18090427452121523061184380667980763703935420996985767299962855565988460373436;
69+
70+
uint256 constant IC8x = 10474776404215989914860932066594458738983457343662916641445752614561642689469;
71+
uint256 constant IC8y = 3378834796931378107199253750169106725596319638791117099719497080177733386540;
72+
73+
uint256 constant IC9x = 1887249157430528165374401635262372006182704854335611145585690423537839735888;
74+
uint256 constant IC9y = 7080674530581255597858145142316212286106173969722576181725459826182095923364;
75+
76+
77+
// Memory data
78+
uint16 constant pVk = 0;
79+
uint16 constant pPairing = 128;
80+
81+
uint16 constant pLastMem = 896;
82+
83+
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[9] calldata _pubSignals) public view returns (bool) {
84+
assembly {
85+
function checkField(v) {
86+
if iszero(lt(v, r)) {
87+
mstore(0, 0)
88+
return(0, 0x20)
89+
}
90+
}
91+
92+
// G1 function to multiply a G1 value(x,y) to value in an address
93+
function g1_mulAccC(pR, x, y, s) {
94+
let success
95+
let mIn := mload(0x40)
96+
mstore(mIn, x)
97+
mstore(add(mIn, 32), y)
98+
mstore(add(mIn, 64), s)
99+
100+
success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64)
101+
102+
if iszero(success) {
103+
mstore(0, 0)
104+
return(0, 0x20)
105+
}
106+
107+
mstore(add(mIn, 64), mload(pR))
108+
mstore(add(mIn, 96), mload(add(pR, 32)))
109+
110+
success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64)
111+
112+
if iszero(success) {
113+
mstore(0, 0)
114+
return(0, 0x20)
115+
}
116+
}
117+
118+
function checkPairing(pA, pB, pC, pubSignals, pMem) -> isOk {
119+
let _pPairing := add(pMem, pPairing)
120+
let _pVk := add(pMem, pVk)
121+
122+
mstore(_pVk, IC0x)
123+
mstore(add(_pVk, 32), IC0y)
124+
125+
// Compute the linear combination vk_x
126+
127+
g1_mulAccC(_pVk, IC1x, IC1y, calldataload(add(pubSignals, 0)))
128+
129+
g1_mulAccC(_pVk, IC2x, IC2y, calldataload(add(pubSignals, 32)))
130+
131+
g1_mulAccC(_pVk, IC3x, IC3y, calldataload(add(pubSignals, 64)))
132+
133+
g1_mulAccC(_pVk, IC4x, IC4y, calldataload(add(pubSignals, 96)))
134+
135+
g1_mulAccC(_pVk, IC5x, IC5y, calldataload(add(pubSignals, 128)))
136+
137+
g1_mulAccC(_pVk, IC6x, IC6y, calldataload(add(pubSignals, 160)))
138+
139+
g1_mulAccC(_pVk, IC7x, IC7y, calldataload(add(pubSignals, 192)))
140+
141+
g1_mulAccC(_pVk, IC8x, IC8y, calldataload(add(pubSignals, 224)))
142+
143+
g1_mulAccC(_pVk, IC9x, IC9y, calldataload(add(pubSignals, 256)))
144+
145+
146+
// -A
147+
mstore(_pPairing, calldataload(pA))
148+
mstore(add(_pPairing, 32), mod(sub(q, calldataload(add(pA, 32))), q))
149+
150+
// B
151+
mstore(add(_pPairing, 64), calldataload(pB))
152+
mstore(add(_pPairing, 96), calldataload(add(pB, 32)))
153+
mstore(add(_pPairing, 128), calldataload(add(pB, 64)))
154+
mstore(add(_pPairing, 160), calldataload(add(pB, 96)))
155+
156+
// alpha1
157+
mstore(add(_pPairing, 192), alphax)
158+
mstore(add(_pPairing, 224), alphay)
159+
160+
// beta2
161+
mstore(add(_pPairing, 256), betax1)
162+
mstore(add(_pPairing, 288), betax2)
163+
mstore(add(_pPairing, 320), betay1)
164+
mstore(add(_pPairing, 352), betay2)
165+
166+
// vk_x
167+
mstore(add(_pPairing, 384), mload(add(pMem, pVk)))
168+
mstore(add(_pPairing, 416), mload(add(pMem, add(pVk, 32))))
169+
170+
171+
// gamma2
172+
mstore(add(_pPairing, 448), gammax1)
173+
mstore(add(_pPairing, 480), gammax2)
174+
mstore(add(_pPairing, 512), gammay1)
175+
mstore(add(_pPairing, 544), gammay2)
176+
177+
// C
178+
mstore(add(_pPairing, 576), calldataload(pC))
179+
mstore(add(_pPairing, 608), calldataload(add(pC, 32)))
180+
181+
// delta2
182+
mstore(add(_pPairing, 640), deltax1)
183+
mstore(add(_pPairing, 672), deltax2)
184+
mstore(add(_pPairing, 704), deltay1)
185+
mstore(add(_pPairing, 736), deltay2)
186+
187+
188+
let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20)
189+
190+
isOk := and(success, mload(_pPairing))
191+
}
192+
193+
let pMem := mload(0x40)
194+
mstore(0x40, add(pMem, pLastMem))
195+
196+
// Validate that all evaluations ∈ F
197+
198+
checkField(calldataload(add(_pubSignals, 0)))
199+
200+
checkField(calldataload(add(_pubSignals, 32)))
201+
202+
checkField(calldataload(add(_pubSignals, 64)))
203+
204+
checkField(calldataload(add(_pubSignals, 96)))
205+
206+
checkField(calldataload(add(_pubSignals, 128)))
207+
208+
checkField(calldataload(add(_pubSignals, 160)))
209+
210+
checkField(calldataload(add(_pubSignals, 192)))
211+
212+
checkField(calldataload(add(_pubSignals, 224)))
213+
214+
checkField(calldataload(add(_pubSignals, 256)))
215+
216+
217+
// Validate all evaluations
218+
let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem)
219+
220+
mstore(0, isValid)
221+
return(0, 0x20)
222+
}
223+
}
224+
}

0 commit comments

Comments
 (0)