-
Notifications
You must be signed in to change notification settings - Fork 7
/
VerifierPublicInputGammaDelta.sol.template
142 lines (124 loc) · 4.23 KB
/
VerifierPublicInputGammaDelta.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
contract PairingTest {
uint256 constant Q =
21888242871839275222246405745257275088696311157297823662689037894645226208583;
struct G1Point {
uint256 x;
uint256 y;
}
struct G2Point {
uint256 x1;
uint256 x2;
uint256 y1;
uint256 y2;
}
struct Proof {
G1Point A;
G2Point B;
G1Point C;
}
struct VerifierKey {
G1Point alpha;
G2Point beta;
G2Point gamma;
G2Point delta;
G1Point IC0;
G1Point IC1;
}
// proof has to be supplied by the user, but for demo purposes we just hardcode it
function proof() public pure returns (Proof memory p) {
p = Proof(
G1Point($aG1_x, $aG1_y),
G2Point($bG2_x1, $bG2_x2, $bG2_y1, $bG2_y2),
G1Point($cG1_x, $cG1_y)
);
}
function input() public pure returns (uint256[2] memory _input) {
_input[0] = $one;
_input[1] = $out;
}
function verifierKey() public pure returns (VerifierKey memory vk) {
vk = VerifierKey(
G1Point($alphaG1_x, $alphaG1_y),
G2Point($betaG2_x1, $betaG2_x2, $betaG2_y1, $betaG2_y2),
G2Point($gammaG2_x1, $gammaG2_x2, $gammaG2_y1, $gammaG2_y2),
G2Point($deltaG2_x1, $deltaG2_x2, $deltaG2_y1, $deltaG2_y2),
G1Point($k1G1_x, $k1G1_y),
G1Point($k2G1_x, $k2G1_y)
);
}
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) {
return verify(proof().A, proof().B, proof().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(verifierKey().IC0, _input[0]);
G1Point memory k2 = mul(verifierKey().IC1, _input[1]);
G1Point memory K = add(k1, k2);
// -A * B + alpha * beta + C * delta + K * gamma = 0
bytes memory points1 = abi.encode(
A.x,
negate(A).y,
B.x2,
B.x1,
B.y2,
B.y1,
verifierKey().alpha.x,
verifierKey().alpha.y,
verifierKey().beta.x2,
verifierKey().beta.x1,
verifierKey().beta.y2,
verifierKey().beta.y1
);
bytes memory points2 = abi.encode(
C.x,
C.y,
verifierKey().delta.x2,
verifierKey().delta.x1,
verifierKey().delta.y2,
verifierKey().delta.y1,
K.x,
K.y,
verifierKey().gamma.x2,
verifierKey().gamma.x1,
verifierKey().gamma.y2,
verifierKey().gamma.y1
);
bytes memory points = abi.encodePacked(points1, points2);
return run(points);
}
}