Repository navigation
Expand file tree
/
Copy pathECOps.t.sol
More file actions
165 lines (148 loc) · 4.25 KB
/
Copy pathECOps.t.sol
File metadata and controls
165 lines (148 loc) · 4.25 KB
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;
import {Test, console} from "forge-std/Test.sol";
contract ECOpts is Test {
// BN254 curve prime
uint256 private constant P =
21888242871839275222246405745257275088696311157297823662689037894645226208583;
struct G1 {
uint256 x;
uint256 y;
}
// y^2 = x^3 + 3 / (9 + i)
// x = [imaginary, real]
// y = [imaginary, real]
struct G2 {
uint256[2] x;
uint256[2] y;
}
function add(G1 memory p, G1 memory q) internal view returns (G1 memory r) {
// Call fails if x or y >= P
// Call fails if point is not on the curve
// y^2 = (x^3 + 3) % P
uint256[4] memory input;
input[0] = p.x;
input[1] = p.y;
input[2] = q.x;
input[3] = q.y;
bool ok;
assembly {
ok := staticcall(
gas(),
// EC add
6,
input,
// input length = 128 bytes
0x80,
r,
0x40
)
}
require(ok, "add failed");
}
function mul(G1 memory p, uint256 s) internal view returns (G1 memory r) {
uint256[3] memory input;
input[0] = p.x;
input[1] = p.y;
input[2] = s;
bool ok;
assembly {
ok := staticcall(
gas(),
// EC scalar mul
7,
input,
// input length = 128 bytes
0x80,
r,
0x40
)
}
require(ok, "mul failed");
}
function pair(G1 memory a, G2 memory b, G1 memory c, G2 memory d)
internal
view
returns (uint256)
{
// All x, y coordinates must be < P
// All G1 and G2 points must be valid points on their elliptic curve
uint256[12] memory input = [
a.x,
a.y,
b.x[0],
b.x[1],
b.y[0],
b.y[1],
c.x,
c.y,
d.x[0],
d.x[1],
d.y[0],
d.y[1]
];
uint256[1] memory out;
bool ok;
assembly {
ok := staticcall(
gas(),
// EC pairing address
8,
input,
// Calldata size in bytes (12 * 32)
mul(12, 32),
// Writes 32 bytes to 0th element in out
out,
0x20
)
}
require(ok, "pair failed");
// Returns 1 if e(a, b) * e(c, d) = 1
// 0 otherwise
return out[0];
}
function test_ec_add() public view {
// y^2 = (x^3 + 3) % P or x, y = 0, 0
G1 memory p = G1({x: 1, y: 2});
G1 memory q = G1({x: 1, y: 2});
G1 memory r = add(p, q);
console.log("r.x", r.x);
console.log("r.y", r.y);
}
function test_ec_mul() public view {
// y^2 = (x^3 + 3) % P or x, y = 0, 0
G1 memory p = G1({x: 1, y: 2});
uint256 s = 2;
G1 memory r = mul(p, s);
console.log("r.x", r.x);
console.log("r.y", r.y);
}
function test_ec_pair() public view {
G1 memory a = G1({x: 1, y: 2});
// Standard G2 generator
G2 memory b = G2({
x: [
uint256(
11559732032986387107991004021392285783925812861821192530917403151452391805634
),
uint256(
10857046999023057135944570762232829481370756359578518086990519993285655852781
)
],
y: [
uint256(
4082367875863433681332203403145435568316851327593401208105741076214120093531
),
uint256(
8495653923123431417604973247489272438418190587263600148770280649306958101930
)
]
});
G1 memory c = G1({x: 1, y: 2});
G2 memory d = G2({x: [b.x[0], b.x[1]], y: [P - b.y[0], P - b.y[1]]});
// e(a, b) * e(c, d) = 1 iff e(a, b) = e(c, d)^(-1)
// e(a, b) * e(a, -b) = 1
uint256 r = pair(a, b, c, d);
console.log("r", r);
}
}