-
Notifications
You must be signed in to change notification settings - Fork 6
/
VerifierPublicInput.sol.template
170 lines (147 loc) · 4.55 KB
/
VerifierPublicInput.sol.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
contract PairingTest {
// A
uint256 constant aG1_x =
$aG1_x;
uint256 constant aG1_y =
$aG1_y;
// B
uint256 constant bG2_x1 =
$bG2_x1;
uint256 constant bG2_x2 =
$bG2_x2;
uint256 constant bG2_y1 =
$bG2_y1;
uint256 constant bG2_y2 =
$bG2_y2;
// alpha
uint256 constant alphaG1_x =
$alphaG1_x;
uint256 constant alphaG1_y =
$alphaG1_y;
// beta
uint256 constant betaG2_x1 =
$betaG2_x1;
uint256 constant betaG2_x2 =
$betaG2_x2;
uint256 constant betaG2_y1 =
$betaG2_y1;
uint256 constant betaG2_y2 =
$betaG2_y2;
// C
uint256 constant cG1_x =
$cG1_x;
uint256 constant cG1_y =
$cG1_y;
// K1
uint256 constant k1G1_x =
$k1G1_x;
uint256 constant k1G1_y =
$k1G1_y;
// K2
uint256 constant k2G1_x =
$k2G1_x;
uint256 constant k2G1_y =
$k2G1_y;
// public input
uint256 one = $one;
uint256 out = $out;
uint256 constant G2_x1 =
10857046999023057135944570762232829481370756359578518086990519993285655852781;
uint256 constant G2_x2 =
11559732032986387107991004021392285783925812861821192530917403151452391805634;
uint256 constant G2_y1 =
8495653923123431417604973247489272438418190587263600148770280649306958101930;
uint256 constant G2_y2 =
4082367875863433681332203403145435568316851327593401208105741076214120093531;
uint256 constant Q =
21888242871839275222246405745257275088696311157297823662689037894645226208583;
struct G1Point {
uint256 x;
uint256 y;
}
struct G2Point {
uint256 x1;
uint256 x2;
uint256 y1;
uint256 y2;
}
function add(
G1Point memory p1,
G1Point memory p2
) public view returns (G1Point memory r) {
(bool ok, bytes memory result) = address(6).staticcall(
abi.encode(p1.x, p1.y, p2.x, p2.y)
);
require(ok, "g1add failed");
(uint256 x, uint256 y) = abi.decode(result, (uint256, uint256));
r = G1Point(x, y);
}
function mul(
G1Point memory p,
uint256 scalar
) public view returns (G1Point memory r) {
(bool ok, bytes memory result) = address(7).staticcall(
abi.encode(p.x, p.y, scalar)
);
require(ok, "g1mul failed");
(uint256 x, uint256 y) = abi.decode(result, (uint256, uint256));
r = G1Point(x, y);
}
function negate(G1Point memory p) internal pure returns (G1Point memory) {
// The prime q in the base field F_q for G1
if (p.x == 0 && p.y == 0) return G1Point(0, 0);
return G1Point(p.x, Q - (p.y % Q));
}
function run(bytes memory input) public view returns (bool) {
// optional, the precompile checks this too and reverts (with no error) if false, this helps narrow down possible errors
if (input.length % 192 != 0) revert("Points must be a multiple of 6");
(bool success, bytes memory data) = address(0x08).staticcall(input);
if (success) return abi.decode(data, (bool));
revert("Wrong pairing");
}
function emulate() public view returns(bool) {
G1Point memory A = G1Point(aG1_x, aG1_y);
G2Point memory B = G2Point(bG2_x1, bG2_x2, bG2_y1, bG2_y2);
G1Point memory C = G1Point(cG1_x, cG1_y);
uint256[2] memory input = [one, out];
return verify(A, B, C, input);
}
function verify(G1Point memory A, G2Point memory B, G1Point memory C, uint256[2] memory input) public view returns (bool) {
G1Point memory k1 = mul(G1Point(k1G1_x, k1G1_y), input[0]);
G1Point memory k2 = mul(G1Point(k2G1_x, k2G1_y), input[1]);
G1Point memory K = add(k1, k2);
// -A * B + alpha * beta + C * 1(G2) + K * 1(G2) = 0
bytes memory points1 = abi.encode(
A.x,
negate(A).y,
B.x2,
B.x1,
B.y2,
B.y1,
alphaG1_x,
alphaG1_y,
betaG2_x2,
betaG2_x1,
betaG2_y2,
betaG2_y1
);
bytes memory points2 = abi.encode(
C.x,
C.y,
G2_x2,
G2_x1,
G2_y2,
G2_y1,
K.x,
K.y,
G2_x2,
G2_x1,
G2_y2,
G2_y1
);
bytes memory points = abi.encodePacked(points1, points2);
return run(points);
}
}