|
| 1 | +// evmone: Fast Ethereum Virtual Machine implementation |
| 2 | +// Copyright 2023 The evmone Authors. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#include "evmone_precompiles/bn254.hpp" |
| 6 | +#include <gtest/gtest.h> |
| 7 | + |
| 8 | +using namespace evmmax::bn254; |
| 9 | +using namespace intx; |
| 10 | + |
| 11 | + |
| 12 | +TEST(evmmax, bn254_pairing) |
| 13 | +{ |
| 14 | + { |
| 15 | + // vG1[0] == -vG1[1] |
| 16 | + const std::vector<Point> vG1{ |
| 17 | + { |
| 18 | + 0x1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59_u256, |
| 19 | + 0x3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41_u256, |
| 20 | + }, |
| 21 | + { |
| 22 | + 0x1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59_u256, |
| 23 | + 0x2f7149c03b2c47b35163356519d1179affcf3b9487f7f857c3f11331120e06_u256, |
| 24 | + }, |
| 25 | + }; |
| 26 | + |
| 27 | + const std::vector<ExtPoint> vG2{ |
| 28 | + { |
| 29 | + { |
| 30 | + 0x04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678_u256, |
| 31 | + 0x209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7_u256, |
| 32 | + }, |
| 33 | + { |
| 34 | + 0x120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550_u256, |
| 35 | + 0x2bb8324af6cfc93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d_u256, |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + { |
| 40 | + 0x04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678_u256, |
| 41 | + 0x209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7_u256, |
| 42 | + }, |
| 43 | + { |
| 44 | + 0x120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550_u256, |
| 45 | + 0x2bb8324af6cfc93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d_u256, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }; |
| 49 | + |
| 50 | + EXPECT_EQ(pairing(vG2, vG1), true); |
| 51 | + } |
| 52 | + |
| 53 | + { |
| 54 | + // vG2[0] == -vG2[1] |
| 55 | + const std::vector<Point> vG1{ |
| 56 | + { |
| 57 | + 0x1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59_u256, |
| 58 | + 0x3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41_u256, |
| 59 | + }, |
| 60 | + { |
| 61 | + 0x1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59_u256, |
| 62 | + 0x3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41_u256, |
| 63 | + }, |
| 64 | + }; |
| 65 | + |
| 66 | + const std::vector<ExtPoint> vG2{ |
| 67 | + { |
| 68 | + { |
| 69 | + 0x04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678_u256, |
| 70 | + 0x209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7_u256, |
| 71 | + }, |
| 72 | + { |
| 73 | + 0x120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550_u256, |
| 74 | + 0x2bb8324af6cfc93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d_u256, |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + { |
| 79 | + 0x04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678_u256, |
| 80 | + 0x209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7_u256, |
| 81 | + }, |
| 82 | + { |
| 83 | + 0x1e5a2425ee25843033f124ef834777def4b484725bd61a4525c0a631f9f587f7_u256, |
| 84 | + 0x4ac1c27ea61d6f480ad989c3d245b50f4da4fc3edadaadf7c8d4fec86bec8fa_u256, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }; |
| 88 | + |
| 89 | + EXPECT_EQ(pairing(vG2, vG1), true); |
| 90 | + } |
| 91 | + |
| 92 | + { |
| 93 | + // vG1[0] = vG1[0] * 17 |
| 94 | + const std::vector<Point> vG1{ |
| 95 | + { |
| 96 | + 0x22980b2e458ec77e258b19ca3a7b46181f63c6536307acae03eea236f6919eeb_u256, |
| 97 | + 0x4eab993e2ba2cca2b08c216645e3fbcf80ae67515b2c49806c17b90c9d3cad3_u256, |
| 98 | + }, |
| 99 | + { |
| 100 | + 0x1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59_u256, |
| 101 | + 0x3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41_u256, |
| 102 | + }, |
| 103 | + }; |
| 104 | + |
| 105 | + // vG2[1] = -vG2[1] * 16 |
| 106 | + const std::vector<ExtPoint> vG2{ |
| 107 | + { |
| 108 | + { |
| 109 | + 0x04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678_u256, |
| 110 | + 0x209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7_u256, |
| 111 | + }, |
| 112 | + { |
| 113 | + 0x120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550_u256, |
| 114 | + 0x2bb8324af6cfc93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d_u256, |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + { |
| 119 | + 0x14191bd65f51663a1d4ad71d8480c3c3260d598aab6ed95681f773abade7fd7a_u256, |
| 120 | + 0x299c79589dfb51fd6925fce3a7fc15c441fdafaa24f0d09b7c443befdddde4e5_u256, |
| 121 | + }, |
| 122 | + { |
| 123 | + 0x1d710ac19a995c6395f33be7f3dcd75e0632a006d196da6b4c9ba78708b6bb78_u256, |
| 124 | + 0xcae1001513ae5ddf742aa6dc2f52457d9b14e17765dd74fc098ad06045d434e_u256, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }; |
| 128 | + |
| 129 | + EXPECT_EQ(pairing(vG2, vG1), false); |
| 130 | + } |
| 131 | + |
| 132 | + { |
| 133 | + // vG1[0] = vG1[0] * 17 |
| 134 | + const std::vector<Point> vG1{ |
| 135 | + { |
| 136 | + 0x22980b2e458ec77e258b19ca3a7b46181f63c6536307acae03eea236f6919eeb_u256, |
| 137 | + 0x4eab993e2ba2cca2b08c216645e3fbcf80ae67515b2c49806c17b90c9d3cad3_u256, |
| 138 | + }, |
| 139 | + { |
| 140 | + 0x1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59_u256, |
| 141 | + 0x3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41_u256, |
| 142 | + }, |
| 143 | + }; |
| 144 | + |
| 145 | + // vG2[1] = -vG2[1] * 17 |
| 146 | + const std::vector<ExtPoint> vG2{ |
| 147 | + { |
| 148 | + { |
| 149 | + 0x04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678_u256, |
| 150 | + 0x209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7_u256, |
| 151 | + }, |
| 152 | + { |
| 153 | + 0x120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550_u256, |
| 154 | + 0x2bb8324af6cfc93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d_u256, |
| 155 | + }, |
| 156 | + }, |
| 157 | + { |
| 158 | + { |
| 159 | + 0x11eeb08db4fe0df9d7617f11f5f8f488d643510f825f3730ffb038c84c9260fd_u256, |
| 160 | + 0x12bf46039aa40a61762bf97b1bb028cebc6d42e46bbbe67f715eda54808b74c4_u256, |
| 161 | + }, |
| 162 | + { |
| 163 | + 0x42b65e62de1fd24534db81fd72e7ee832637948c1c466ccb08171e503f23e72_u256, |
| 164 | + 0x197a5efb333448885788690df5af2211c1697dd8b7b1f8845b4e30a909d2b0f5_u256, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }; |
| 168 | + |
| 169 | + EXPECT_EQ(pairing(vG2, vG1), true); |
| 170 | + } |
| 171 | + |
| 172 | + // Empty input |
| 173 | + { |
| 174 | + const std::vector<Point> t_vG1 = {}; |
| 175 | + const std::vector<ExtPoint> t_vG2 = {}; |
| 176 | + |
| 177 | + EXPECT_EQ(pairing(t_vG2, t_vG1), true); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +TEST(evmmax, bn254_pairing_invalid_input) |
| 182 | +{ |
| 183 | + // Valid input |
| 184 | + const std::vector<Point> vG1{ |
| 185 | + { |
| 186 | + 0x22980b2e458ec77e258b19ca3a7b46181f63c6536307acae03eea236f6919eeb_u256, |
| 187 | + 0x4eab993e2ba2cca2b08c216645e3fbcf80ae67515b2c49806c17b90c9d3cad3_u256, |
| 188 | + }, |
| 189 | + }; |
| 190 | + |
| 191 | + const std::vector<ExtPoint> vG2{ |
| 192 | + { |
| 193 | + { |
| 194 | + 0x04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678_u256, |
| 195 | + 0x209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7_u256, |
| 196 | + }, |
| 197 | + { |
| 198 | + 0x120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550_u256, |
| 199 | + 0x2bb8324af6cfc93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d_u256, |
| 200 | + }, |
| 201 | + }, |
| 202 | + }; |
| 203 | + |
| 204 | + EXPECT_EQ(pairing(vG2, vG1), false); |
| 205 | + |
| 206 | + // Invalid input size |
| 207 | + { |
| 208 | + const std::vector<Point> t_vG1 = {}; |
| 209 | + |
| 210 | + EXPECT_EQ(pairing(vG2, t_vG1), std::nullopt); |
| 211 | + } |
| 212 | + |
| 213 | + // Coordinare not the field element |
| 214 | + { |
| 215 | + auto t_vG1 = vG1; |
| 216 | + t_vG1[0].x = FieldPrime; |
| 217 | + |
| 218 | + EXPECT_EQ(pairing(vG2, t_vG1), std::nullopt); |
| 219 | + } |
| 220 | + |
| 221 | + // Coordinare not the field element |
| 222 | + { |
| 223 | + auto t_vG2 = vG2; |
| 224 | + t_vG2[0].x.second = FieldPrime; |
| 225 | + |
| 226 | + EXPECT_EQ(pairing(t_vG2, vG1), std::nullopt); |
| 227 | + } |
| 228 | + |
| 229 | + // P not on curve |
| 230 | + { |
| 231 | + auto t_vG1 = vG1; |
| 232 | + t_vG1[0].x += 1; |
| 233 | + |
| 234 | + EXPECT_EQ(pairing(vG2, t_vG1), std::nullopt); |
| 235 | + } |
| 236 | + |
| 237 | + // Q not on curve |
| 238 | + { |
| 239 | + auto t_vG2 = vG2; |
| 240 | + t_vG2[0].x.first += 1; |
| 241 | + |
| 242 | + EXPECT_EQ(pairing(t_vG2, vG1), std::nullopt); |
| 243 | + } |
| 244 | + |
| 245 | + // Q not in proper group. Q id a member of small subgroup on twisted curve over Fq^2 |
| 246 | + { |
| 247 | + const std::vector<ExtPoint> t_vG2{ |
| 248 | + { |
| 249 | + { |
| 250 | + 0x13d841ba7ff3c6efd6870c3fea13a3ecab0423af5e4db9c5d28a6b46a05cd57b_u256, |
| 251 | + 0x1a2b1eaa7b20faae36d26eff4db6e336c34434b66eded3cc5303d51ae353f478_u256, |
| 252 | + }, |
| 253 | + { |
| 254 | + 0x2d3e8808aa7a7fffa8f871f10df8d59c6dd725889c46e9136e01cb2465b20723_u256, |
| 255 | + 0x1d5224817b8714531fc77e20b975178b1b3044f4b729fa3230db03dc0088ebdb_u256, |
| 256 | + }, |
| 257 | + }, |
| 258 | + }; |
| 259 | + |
| 260 | + |
| 261 | + EXPECT_EQ(pairing(t_vG2, vG1), std::nullopt); |
| 262 | + } |
| 263 | +} |
0 commit comments