-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patheip2537.bats
More file actions
234 lines (195 loc) · 8.68 KB
/
eip2537.bats
File metadata and controls
234 lines (195 loc) · 8.68 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env bats
# bats file_tags=pectra,eip-2537
# This file implements tests for EIP-2537: Precompile for BLS12-381 curve operations
# https://eips.ethereum.org/EIPS/eip-2537
setup() {
true
}
setup_file() {
export kurtosis_enclave_name=${KURTOSIS_ENCLAVE_NAME:-"pectra"}
if [[ -n "$L2_RPC_URL" ]]; then
export l2_rpc_url="$L2_RPC_URL"
elif l2_rpc_url=$(kurtosis port print "$kurtosis_enclave_name" op-el-1-op-geth-op-node-001 rpc 2>/dev/null); then
export l2_rpc_url
elif l2_rpc_url=$(kurtosis port print "$kurtosis_enclave_name" cdk-erigon-rpc-001 rpc 2>/dev/null); then
export l2_rpc_url
else
echo "❌ Failed to determine L2 RPC URL. Please set L2_RPC_URL" >&2
exit 1
fi
test_vectors_dir="./eip2537_test_vectors"
# G1ADD
export BLS12_G1ADD_PRECOMPILE_ADDR="0x000000000000000000000000000000000000000b"
export g1add_test_vectors_ok="$test_vectors_dir/add_G1_bls.json"
export g1add_test_vectors_ko="$test_vectors_dir/fail-add_G1_bls.json"
# G2ADD
export BLS12_G2ADD_PRECOMPILE_ADDR="0x000000000000000000000000000000000000000d"
export g2add_test_vectors_ok="$test_vectors_dir/add_G2_bls.json"
export g2add_test_vectors_ko="$test_vectors_dir/fail-add_G2_bls.json"
# G1MUL
export BLS12_G1MUL_PRECOMPILE_ADDR="0x000000000000000000000000000000000000000c"
export g1mul_test_vectors_ok="$test_vectors_dir/mul_G1_bls.json"
export g1mul_test_vectors_ko="$test_vectors_dir/fail-mul_G1_bls.json"
# G2MUL
export BLS12_G2MUL_PRECOMPILE_ADDR="0x000000000000000000000000000000000000000e"
export g2mul_test_vectors_ok="$test_vectors_dir/mul_G2_bls.json"
export g2mul_test_vectors_ko="$test_vectors_dir/fail-mul_G2_bls.json"
# G1MSM
export BLS12_G1MSM_PRECOMPILE_ADDR="0x000000000000000000000000000000000000000c"
export g1msm_test_vectors_ok="$test_vectors_dir/msm_G1_bls.json"
export g1msm_test_vectors_ko="$test_vectors_dir/fail-msm_G1_bls.json"
# G2MSM
export BLS12_G2MSM_PRECOMPILE_ADDR="0x000000000000000000000000000000000000000e"
export g2msm_test_vectors_ok="$test_vectors_dir/msm_G2_bls.json"
export g2msm_test_vectors_ko="$test_vectors_dir/fail-msm_G2_bls.json"
# PAIRING_CHECK
export BLS12_PAIRING_CHECK_PRECOMPILE_ADDR="0x000000000000000000000000000000000000000f"
export pairing_check_test_vectors_ok="$test_vectors_dir/pairing_check_bls.json"
export pairing_check_test_vectors_ko="$test_vectors_dir/fail-pairing_check_bls.json"
# MAP_FP_TO_G1
export BLS12_MAP_FP_TO_G1_PRECOMPILE_ADDR="0x0000000000000000000000000000000000000010"
export map_fp_to_g1_test_vectors_ok="$test_vectors_dir/map_fp_to_G1_bls.json"
export map_fp_to_g1_test_vectors_ko="$test_vectors_dir/fail-map_fp_to_G1_bls.json"
# MAP_FP2_TO_G2
export BLS12_MAP_FP2_TO_G2_PRECOMPILE_ADDR="0x0000000000000000000000000000000000000011"
export map_fp2_to_g2_test_vectors_ok="$test_vectors_dir/map_fp2_to_G2_bls.json"
export map_fp2_to_g2_test_vectors_ko="$test_vectors_dir/fail-map_fp2_to_G2_bls.json"
}
# These function is for tests that are expected to be working. Output is also checked against expected result.
function eip2537_test_ok() {
test_name=$1
test_vectors_ok=$2
bls12_precompile_addr=$3
echo "Running EIP-2537 test vectors from path $BATS_TEST_DIRNAME/$test_vectors_ok for $test_name"
count=$(jq length "$BATS_TEST_DIRNAME/$test_vectors_ok")
echo "Running $count EIP-2537 $test_name test vectors..."
for i in $(seq 0 $((count - 1))); do
input=$(jq -r ".[$i].Input" "$BATS_TEST_DIRNAME/$test_vectors_ok")
expected_output=$(jq -r ".[$i].Expected" "$BATS_TEST_DIRNAME/$test_vectors_ok")
name=$(jq -r ".[$i].Name" "$BATS_TEST_DIRNAME/$test_vectors_ok")
# strip 0x if present
input=${input#0x}
expected_output=${expected_output#0x}
run cast call "$bls12_precompile_addr" "0x$input" --rpc-url "$l2_rpc_url"
# strip 0x from output
output=${output#0x}
if [ "$status" -ne 0 ]; then
echo "❌ Test #$i ($name) failed to execute"
echo "Input: $input"
echo "Error: $output"
false
elif [ "$output" != "$expected_output" ]; then
echo "❌ Test #$i ($name) failed"
echo "Input: $input"
echo "Expected: $expected_output"
echo "Got: $output"
false
else
echo "✅ Test #$i ($name) passed"
fi
done
}
# These functions are for tests that are expected to fail. Output is also checked against expected error.
function eip2537_test_ko() {
test_name=$1
test_vectors_ko=$2
bls12_precompile_addr=$3
echo "Running EIP-2537 test vectors from path $BATS_TEST_DIRNAME/$test_vectors_ko for $test_name"
count=$(jq length "$BATS_TEST_DIRNAME/$test_vectors_ko")
echo "Running $count $test_name test vectors..."
for i in $(seq 0 $((count - 1))); do
input=$(jq -r ".[$i].Input" "$BATS_TEST_DIRNAME/$test_vectors_ko")
expected_error=$(jq -r ".[$i].ExpectedError" "$BATS_TEST_DIRNAME/$test_vectors_ko")
name=$(jq -r ".[$i].Name" "$BATS_TEST_DIRNAME/$test_vectors_ko")
# strip 0x if present
input=${input#0x}
run cast call "$bls12_precompile_addr" "0x$input" --rpc-url "$l2_rpc_url"
# strip 0x from output
output=${output#0x}
if [ "$status" -ne 1 ]; then
echo "❌ Test #$i ($name) was expected to fail, but it did not"
echo "Input: $input"
echo "Output: $output"
false
elif [[ "$output" != *"$expected_error"* ]]; then
echo "❌ Test #$i ($name) failed"
echo "Input: $input"
echo "Expected: $expected_error"
echo "Got: $output"
false
else
echo "✅ Test #$i ($name) passed"
fi
done
}
# bats file_tags=pectra,eip-2537
@test "G1ADD test vectors OK" {
eip2537_test_ok "G1ADD" "$g1add_test_vectors_ok" "$BLS12_G1ADD_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "G1ADD test vectors KO" {
eip2537_test_ko "G1ADD" "$g1add_test_vectors_ko" "$BLS12_G1ADD_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "G2ADD test vectors OK" {
eip2537_test_ok "G2ADD" "$g2add_test_vectors_ok" "$BLS12_G2ADD_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "G2ADD test vectors KO" {
eip2537_test_ko "G2ADD" "$g2add_test_vectors_ko" "$BLS12_G2ADD_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "G1MUL test vectors OK" {
eip2537_test_ok "G1MUL" "$g1mul_test_vectors_ok" "$BLS12_G1MUL_PRECOMPILE_ADDR"
}
@test "G1MUL test vectors KO" {
eip2537_test_ko "G1MUL" "$g1mul_test_vectors_ko" "$BLS12_G1MUL_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "G2MUL test vectors OK" {
eip2537_test_ok "G2MUL" "$g2mul_test_vectors_ok" "$BLS12_G2MUL_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "G2MUL test vectors KO" {
eip2537_test_ko "G2MUL" "$g2mul_test_vectors_ko" "$BLS12_G2MUL_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "G1MSM test vectors OK (long test)" {
eip2537_test_ok "G1MSM" "$g1msm_test_vectors_ok" "$BLS12_G1MSM_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "G1MSM test vectors KO" {
eip2537_test_ko "G1MSM" "$g1msm_test_vectors_ko" "$BLS12_G1MSM_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "G2MSM test vectors OK (long test)" {
eip2537_test_ok "G2MSM" "$g2msm_test_vectors_ok" "$BLS12_G2MSM_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "G2MSM test vectors KO" {
eip2537_test_ko "G2MSM" "$g2msm_test_vectors_ko" "$BLS12_G2MSM_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "PAIRING_CHECK test vectors OK" {
eip2537_test_ok "PAIRING_CHECK" "$pairing_check_test_vectors_ok" "$BLS12_PAIRING_CHECK_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "PAIRING_CHECK test vectors KO" {
eip2537_test_ko "PAIRING_CHECK" "$pairing_check_test_vectors_ko" "$BLS12_PAIRING_CHECK_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "MAP_FP_TO_G1 test vectors OK" {
eip2537_test_ok "MAP_FP_TO_G1" "$map_fp_to_g1_test_vectors_ok" "$BLS12_MAP_FP_TO_G1_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "MAP_FP_TO_G1 test vectors KO" {
eip2537_test_ko "MAP_FP_TO_G1" "$map_fp_to_g1_test_vectors_ko" "$BLS12_MAP_FP_TO_G1_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "MAP_FP2_TO_G2 test vectors OK" {
eip2537_test_ok "MAP_FP2_TO_G2" "$map_fp2_to_g2_test_vectors_ok" "$BLS12_MAP_FP2_TO_G2_PRECOMPILE_ADDR"
}
# bats file_tags=pectra,eip-2537
@test "MAP_FP2_TO_G2 test vectors KO" {
eip2537_test_ko "MAP_FP2_TO_G2" "$map_fp2_to_g2_test_vectors_ko" "$BLS12_MAP_FP2_TO_G2_PRECOMPILE_ADDR"
}