Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_alt_bn128.cpp File Reference
#include <boost/test/included/unit_test.hpp>
#include <fc/exception/exception.hpp>
#include <fc/crypto/hex.hpp>
#include <fc/crypto/alt_bn128.hpp>
#include <fc/utility.hpp>
#include "test_utils.hpp"
Include dependency graph for test_alt_bn128.cpp:

Go to the source code of this file.

Namespaces

namespace  std
 

Macros

#define BOOST_TEST_MODULE   altbn_128_tests
 

Functions

std::ostream & std::operator<< (std::ostream &st, const std::variant< fc::alt_bn128_error, bytes > &err)
 
std::ostream & std::operator<< (std::ostream &st, const std::variant< fc::alt_bn128_error, bool > &err)
 
 BOOST_AUTO_TEST_CASE (add)
 
 FC_LOG_AND_RETHROW ()
 
 BOOST_AUTO_TEST_CASE (mul)
 
 BOOST_AUTO_TEST_CASE (pair)
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   altbn_128_tests

Definition at line 1 of file test_alt_bn128.cpp.

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/3]

BOOST_AUTO_TEST_CASE ( add )

Definition at line 32 of file test_alt_bn128.cpp.

32 {
33
34 using test_add = std::tuple<std::string, std::string, std::variant<fc::alt_bn128_error, bytes>>;
35 const std::vector<test_add> tests = {
36 //test (2 valid points, both on curve)
37 {
38 "222480c9f95409bfa4ac6ae890b9c150bc88542b87b352e92950c340458b0c092976efd698cf23b414ea622b3f720dd9080d679042482ff3668cb2e32cad8ae2",
39 "1bd20beca3d8d28e536d2b5bd3bf36d76af68af5e6c96ca6e5519ba9ff8f53322a53edf6b48bcf5cb1c0b4ad1d36dfce06a79dcd6526f1c386a14d8ce4649844",
40 to_bytes("16c7c4042e3a725ddbacf197c519c3dcad2bc87dfd9ac7e1e1631154ee0b7d9c19cd640dd28c9811ebaaa095a16b16190d08d6906c4f926fce581985fe35be0e")
41 },
42
43 //test (2 valid points, P1 not on curve)
44 {
45 "222480c9f95409bfa4ac6ae890b9c150bc88542b87b352e92950c340458b0c092976efd698cf23b414ea622b3f720dd9080d679042482ff3668cb2e32cad8ae2",
46 "2a53edf6b48bcf5cb1c0b4ad1d36dfce06a79dcd6526f1c386a14d8ce46498441bd20beca3d8d28e536d2b5bd3bf36d76af68af5e6c96ca6e5519ba9ff8f5332",
47 alt_bn128_error::operand_not_in_curve
48 },
49
50 //test (invalid P1 length)
51 {
52 "2a",
53 "222480c9f95409bfa4ac6ae890b9c150bc88542b87b352e92950c340458b0c092976efd698cf23b414ea622b3f720dd9080d679042482ff3668cb2e32cad8ae2",
54 alt_bn128_error::input_len_error
55 },
56
57 //|Fp| = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47
58 //test (P1.x=|Fp|)
59 {
60 "30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd472976efd698cf23b414ea622b3f720dd9080d679042482ff3668cb2e32cad8ae2",
61 "1bd20beca3d8d28e536d2b5bd3bf36d76af68af5e6c96ca6e5519ba9ff8f53322a53edf6b48bcf5cb1c0b4ad1d36dfce06a79dcd6526f1c386a14d8ce4649844",
62 alt_bn128_error::operand_component_invalid
63 },
64
65 //test (P1=(0,0))
66 {
67 "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
68 "1bd20beca3d8d28e536d2b5bd3bf36d76af68af5e6c96ca6e5519ba9ff8f53322a53edf6b48bcf5cb1c0b4ad1d36dfce06a79dcd6526f1c386a14d8ce4649844",
69 to_bytes("1bd20beca3d8d28e536d2b5bd3bf36d76af68af5e6c96ca6e5519ba9ff8f53322a53edf6b48bcf5cb1c0b4ad1d36dfce06a79dcd6526f1c386a14d8ce4649844")
70 },
71 };
72
73 for(const auto& test : tests) {
74 auto op1 = to_bytes(std::get<0>(test));
75 auto op2 = to_bytes(std::get<1>(test));
76 auto expected_result = std::get<2>(test);
77
78 auto res = alt_bn128_add(op1, op2);
79 BOOST_CHECK_EQUAL(res, expected_result);
80 }
81
std::variant< alt_bn128_error, bytes > alt_bn128_add(const bytes &op1, const bytes &op2)
const unsigned char expected_result[]
Definition ssh.c:73
FC_LOG_AND_RETHROW()
bytes to_bytes(const std::string &source)
Definition test_utils.hpp:6
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/3]

BOOST_AUTO_TEST_CASE ( mul )

Definition at line 84 of file test_alt_bn128.cpp.

84 {
85
86 using test_mul = std::tuple<std::string, std::string, std::variant<fc::alt_bn128_error, bytes>>;
87 const std::vector<test_mul> tests = {
88 //test (valid point on curve, scalar size = 256 bits)
89 {
90 "007c43fcd125b2b13e2521e395a81727710a46b34fe279adbf1b94c72f7f91360db2f980370fb8962751c6ff064f4516a6a93d563388518bb77ab9a6b30755be",
91 "0312ed43559cf8ecbab5221256a56e567aac5035308e3f1d54954d8b97cd1c9b",
92 to_bytes("2d66cdeca5e1715896a5a924c50a149be87ddd2347b862150fbb0fd7d0b1833c11c76319ebefc5379f7aa6d85d40169a612597637242a4bbb39e5cd3b844becd")
93 },
94
95 //test (scalar size < 256 bits)
96 {
97 "007c43fcd125b2b13e2521e395a81727710a46b34fe279adbf1b94c72f7f91360db2f980370fb8962751c6ff064f4516a6a93d563388518bb77ab9a6b30755be",
98 "01",
99 alt_bn128_error::invalid_scalar_size,
100 },
101
102 //test (P1 not on curve)
103 {
104 "0db2f980370fb8962751c6ff064f4516a6a93d563388518bb77ab9a6b30755be007c43fcd125b2b13e2521e395a81727710a46b34fe279adbf1b94c72f7f9136",
105 "0312ed43559cf8ecbab5221256a56e567aac5035308e3f1d54954d8b97cd1c9b",
106 alt_bn128_error::operand_not_in_curve,
107 },
108
109 //test (invalid P1 length)
110 {
111 "222480c9f95409bfa4ac6ae890b9c150bc88542b87b352e92950c340458b0c092976efd698cf23b414ea622b3f720dd9080d679042482ff3668cb2e32cad8a",
112 "0312ed43559cf8ecbab5221256a56e567aac5035308e3f1d54954d8b97cd1c9b",
113 alt_bn128_error::input_len_error,
114 },
115
116 //|Fp| = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47
117 //test (P1.y=|Fp|)
118 {
119 "2976efd698cf23b414ea622b3f720dd9080d679042482ff3668cb2e32cad8ae230644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47",
120 "0100010001000100010001000100010001000100010001000100010001000100",
121 alt_bn128_error::operand_component_invalid,
122 },
123
124 //test (P1=(0,0))
125 {
126 "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
127 "0312ed43559cf8ecbab5221256a56e567aac5035308e3f1d54954d8b97cd1c9b",
128 to_bytes("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
129 },
130
131 };
132
133 for(const auto& test : tests) {
134 auto point = to_bytes(std::get<0>(test));
135 auto scalar = to_bytes(std::get<1>(test));
136 auto expected_result = std::get<2>(test);
137
138 auto res = alt_bn128_mul(point, scalar);
139 BOOST_CHECK_EQUAL(res, expected_result);
140 }
141
std::variant< alt_bn128_error, bytes > alt_bn128_mul(const bytes &g1_point, const bytes &scalar)
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [3/3]

BOOST_AUTO_TEST_CASE ( pair )

Definition at line 145 of file test_alt_bn128.cpp.

145 {
146
147 using g1g2_pair = std::vector<std::string>;
148 using pair_test = std::tuple<std::vector<g1g2_pair>, std::variant<fc::alt_bn128_error, bool>>;
149
150 const std::vector<pair_test> tests =
151 {
152 //test1: 2 pairs => (G1_a,G2_a),(G1_b,G2_b) ; alt_bn128_error::none ; true
153 {
154 {
155 { //G1_a G2_a
156 "0f25929bcb43d5a57391564615c9e70a992b10eafa4db109709649cf48c50dd2", //G1_a.x
157 "16da2f5cb6be7a0aa72c440c53c9bbdfec6c36c7d515536431b3a865468acbba", //G1_a.y
158 "2e89718ad33c8bed92e210e81d1853435399a271913a6520736a4729cf0d51eb", //G2_a.x
159 "01a9e2ffa2e92599b68e44de5bcf354fa2642bd4f26b259daa6f7ce3ed57aeb3",
160 "14a9a87b789a58af499b314e13c3d65bede56c07ea2d418d6874857b70763713", //G2_a.y
161 "178fb49a2d6cd347dc58973ff49613a20757d0fcc22079f9abd10c3baee24590",
162 },
163
164 { //G1_b G2_b
165 "1b9e027bd5cfc2cb5db82d4dc9677ac795ec500ecd47deee3b5da006d6d049b8", //G1_b.x
166 "11d7511c78158de484232fc68daf8a45cf217d1c2fae693ff5871e8752d73b21", //G1_b.y
167 "198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2", //G2_b.x
168 "1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed",
169 "090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b", //G2_b.y
170 "12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa",
171 }
172 },
173 true
174 },
175
176 //test2: 1 pair => (G1_a,G2_a) ; alt_bn128_error::none; false
177 {
178 {
179 { //G1_a G2_a
180 "0000000000000000000000000000000000000000000000000000000000000001", //G1_a.x
181 "0000000000000000000000000000000000000000000000000000000000000002", //G1_a.y
182 "198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2", //G2_b.x
183 "1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed",
184 "090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b", //G2_b.y
185 "12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa"
186 },
187
188 },
189 false
190 },
191
192 //test3: 1 pair => (G1_a,G2_a) ; alt_bn128_error::pairing_list_size_error; false
193 {
194 {
195 { //G1_a G2_a
196 "00000000000000000000000000000000000000000000000000000000000001", //G1_a.x
197 "0000000000000000000000000000000000000000000000000000000000000002", //G1_a.y
198 "198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2", //G2_b.x
199 "1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed",
200 "090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b", //G2_b.y
201 "12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa"
202 },
203
204 },
205 alt_bn128_error::pairing_list_size_error
206 },
207
208 //test5: 1 pair => (G1_a,G2_a) ; alt_bn128_error::operand_not_in_curve; false
209 {
210 {
211 { //G1_a G2_a
212 "0000000000000000000000000000000000000000000000000000000000000000", //G1_a.x
213 "0000000000000000000000000000000100000000000000000000000000000000", //G1_a.y
214 "198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2", //G2_b.x
215 "1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed",
216 "090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b", //G2_b.y
217 "12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa"
218 },
219
220 },
221 alt_bn128_error::operand_not_in_curve
222 },
223
224 //test6: 1 pair => (G1_a,G2_a) ; alt_bn128_error::operand_component_invalid; false
225 {
226 {
227 { //G1_a G2_a
228 "30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47", //G1_a.x == |Fp|
229 "0000000000000000000000000000000100000000000000000000000000000000", //G1_a.y
230 "198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2", //G2_b.x
231 "1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed",
232 "090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b", //G2_b.y
233 "12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa"
234 },
235
236 },
237 alt_bn128_error::operand_component_invalid
238 }
239 };
240
241 auto concat = [&](const std::string& s, bytes& buffer) {
242 auto res = to_bytes(s);
243 buffer.insert( buffer.end(), res.begin(), res.end());
244 };
245
246 yield_function_t yield = [](){};
247
248 for(const auto& test : tests) {
249 const auto& pairs = std::get<0>(test);
250 const auto& expected_result = std::get<1>(test);
251
252 bytes g1_g2_pairs;
253 for(const auto& pair : pairs) {
254 BOOST_REQUIRE(pair.size() == 6);
255 concat(pair[0], g1_g2_pairs);
256 concat(pair[1], g1_g2_pairs);
257 concat(pair[2], g1_g2_pairs);
258 concat(pair[3], g1_g2_pairs);
259 concat(pair[4], g1_g2_pairs);
260 concat(pair[5], g1_g2_pairs);
261 }
262
263 auto res = alt_bn128_pair(g1_g2_pairs, yield);
264 BOOST_CHECK_EQUAL(res, expected_result);
265 }
266
thread_local yield_t yield
Definition yield.hpp:52
std::vector< char > bytes
Definition alt_bn128.hpp:10
std::variant< alt_bn128_error, bool > alt_bn128_pair(const bytes &g1_g2_pairs, const yield_function_t &yield)
char * s
Here is the call graph for this function:

◆ FC_LOG_AND_RETHROW()

FC_LOG_AND_RETHROW ( )