179TEST(stdlib_plookup, uint32_xor)
183 const size_t num_lookups = (32 + 5) / 6;
200 for (
size_t i = 0; i < left_slices.size(); ++i) {
201 out_expected[i] = left_slices[i] ^ right_slices[i];
202 left_expected[i] = left_slices[i];
203 right_expected[i] = right_slices[i];
206 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
207 out_expected[i] += out_expected[i + 1] * (1 << 6);
208 left_expected[i] += left_expected[i + 1] * (1 << 6);
209 right_expected[i] += right_expected[i + 1] * (1 << 6);
212 for (
size_t i = 0; i < num_lookups; ++i) {
213 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(),
bb::fr(left_expected[i]));
214 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(),
bb::fr(right_expected[i]));
215 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(),
bb::fr(out_expected[i]));
220 EXPECT_EQ(result,
true);
223TEST(stdlib_plookup, blake2s_xor_rotate_16)
227 const size_t num_lookups = 6;
240 std::vector<fr> out_expected(num_lookups);
241 std::vector<fr> left_expected(num_lookups);
242 std::vector<fr> right_expected(num_lookups);
244 for (
size_t i = 0; i < left_slices.size(); ++i) {
246 uint32_t
a =
static_cast<uint32_t
>(left_slices[i]);
247 uint32_t
b =
static_cast<uint32_t
>(right_slices[i]);
253 left_expected[i] = left_slices[i];
254 right_expected[i] = right_slices[i];
263 std::vector<fr> out_coefficients{ (1 << 6), (
bb::fr(1) /
bb::fr(1 << 22)), (1 << 2), (1 << 6), (1 << 6) };
265 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
266 out_expected[i] += out_expected[i + 1] * out_coefficients[i];
267 left_expected[i] += left_expected[i + 1] * (1 << 6);
268 right_expected[i] += right_expected[i + 1] * (1 << 6);
271 for (
size_t i = 0; i < num_lookups; ++i) {
272 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(), left_expected[i]);
273 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(), right_expected[i]);
274 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(), out_expected[i]);
282 auto mul_constant =
fr(1 << 16);
283 fr lookup_output = lookup[ColumnIdx::C3][0].get_value() * mul_constant;
284 uint32_t xor_rotate_output =
numeric::rotate32(uint32_t(left_value) ^ uint32_t(right_value), 16);
285 EXPECT_EQ(
fr(
uint256_t(xor_rotate_output)), lookup_output);
289 EXPECT_EQ(result,
true);
292TEST(stdlib_plookup, blake2s_xor_rotate_8)
296 const size_t num_lookups = 6;
309 std::vector<fr> out_expected(num_lookups);
310 std::vector<fr> left_expected(num_lookups);
311 std::vector<fr> right_expected(num_lookups);
313 for (
size_t i = 0; i < left_slices.size(); ++i) {
315 uint32_t
a =
static_cast<uint32_t
>(left_slices[i]);
316 uint32_t
b =
static_cast<uint32_t
>(right_slices[i]);
322 left_expected[i] = left_slices[i];
323 right_expected[i] = right_slices[i];
326 auto mul_constant =
fr(1 << 24);
327 std::vector<fr> out_coefficients{ (
bb::fr(1) / mul_constant), (1 << 4), (1 << 6), (1 << 6), (1 << 6) };
329 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
330 out_expected[i] += out_expected[i + 1] * out_coefficients[i];
331 left_expected[i] += left_expected[i + 1] * (1 << 6);
332 right_expected[i] += right_expected[i + 1] * (1 << 6);
335 for (
size_t i = 0; i < num_lookups; ++i) {
336 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(), left_expected[i]);
337 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(), right_expected[i]);
338 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(), out_expected[i]);
341 fr lookup_output = lookup[ColumnIdx::C3][0].get_value() * mul_constant;
342 uint32_t xor_rotate_output =
numeric::rotate32(uint32_t(left_value) ^ uint32_t(right_value), 8);
343 EXPECT_EQ(
fr(
uint256_t(xor_rotate_output)), lookup_output);
347 EXPECT_EQ(result,
true);
350TEST(stdlib_plookup, blake2s_xor_rotate_7)
354 const size_t num_lookups = 6;
367 std::vector<fr> out_expected(num_lookups);
368 std::vector<fr> left_expected(num_lookups);
369 std::vector<fr> right_expected(num_lookups);
371 for (
size_t i = 0; i < left_slices.size(); ++i) {
373 uint32_t
a =
static_cast<uint32_t
>(left_slices[i]);
374 uint32_t
b =
static_cast<uint32_t
>(right_slices[i]);
380 left_expected[i] = left_slices[i];
381 right_expected[i] = right_slices[i];
384 auto mul_constant =
fr(1 << 25);
385 std::vector<fr> out_coefficients{ (
bb::fr(1) / mul_constant), (1 << 5), (1 << 6), (1 << 6), (1 << 6) };
387 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
388 out_expected[i] += out_expected[i + 1] * out_coefficients[i];
389 left_expected[i] += left_expected[i + 1] * (1 << 6);
390 right_expected[i] += right_expected[i + 1] * (1 << 6);
393 for (
size_t i = 0; i < num_lookups; ++i) {
394 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(), left_expected[i]);
395 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(), right_expected[i]);
396 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(), out_expected[i]);
399 fr lookup_output = lookup[ColumnIdx::C3][0].get_value() * mul_constant;
400 uint32_t xor_rotate_output =
numeric::rotate32(uint32_t(left_value) ^ uint32_t(right_value), 7);
401 EXPECT_EQ(
fr(
uint256_t(xor_rotate_output)), lookup_output);
405 EXPECT_EQ(result,
true);
408TEST(stdlib_plookup, blake2s_xor)
412 const size_t num_lookups = 6;
429 for (
size_t i = 0; i < left_slices.size(); ++i) {
430 out_expected[i] = left_slices[i] ^ right_slices[i];
431 left_expected[i] = left_slices[i];
432 right_expected[i] = right_slices[i];
444 fr lookup_output = lookup[ColumnIdx::C3][2].get_value();
445 fr t2_term =
fr(1 << 12) * lookup[ColumnIdx::C3][2].get_value();
446 lookup_output +=
fr(1 << 20) * (lookup[ColumnIdx::C3][0].get_value() - t2_term);
448 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
449 out_expected[i] += out_expected[i + 1] * (1 << 6);
450 left_expected[i] += left_expected[i + 1] * (1 << 6);
451 right_expected[i] += right_expected[i + 1] * (1 << 6);
457 auto xor_rotate_output =
numeric::rotate32(uint32_t(left_value) ^ uint32_t(right_value), 12);
458 EXPECT_EQ(
fr(
uint256_t(xor_rotate_output)), lookup_output);
460 for (
size_t i = 0; i < num_lookups; ++i) {
461 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(),
bb::fr(left_expected[i]));
462 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(),
bb::fr(right_expected[i]));
463 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(),
bb::fr(out_expected[i]));
468 EXPECT_EQ(result,
true);
471TEST(stdlib_plookup, uint32_and)
475 const size_t num_lookups = (32 + 5) / 6;
490 for (
size_t i = 0; i < left_slices.size(); ++i) {
491 out_expected[i] = left_slices[i] & right_slices[i];
492 left_expected[i] = left_slices[i];
493 right_expected[i] = right_slices[i];
496 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
497 out_expected[i] += out_expected[i + 1] * (1 << 6);
498 left_expected[i] += left_expected[i + 1] * (1 << 6);
499 right_expected[i] += right_expected[i + 1] * (1 << 6);
502 for (
size_t i = 0; i < num_lookups; ++i) {
503 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(),
bb::fr(left_expected[i]));
504 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(),
bb::fr(right_expected[i]));
505 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(),
bb::fr(out_expected[i]));
510 EXPECT_EQ(result,
true);
513TEST(stdlib_plookup, secp256k1_generator)
520 uint64_t wnaf_entries[18] = { 0 };
522 wnaf::fixed_wnaf<129, 1, 8>(&input_value.
data[0], &wnaf_entries[0], skew, 0);
524 std::vector<uint64_t> naf_values;
525 for (
size_t i = 0; i < 17; ++i) {
526 bool predicate = bool((wnaf_entries[i] >> 31U) & 1U);
527 uint64_t offset_entry;
529 offset_entry = (127 - (wnaf_entries[i] & 0xffffff));
531 offset_entry = (128 + (wnaf_entries[i] & 0xffffff));
533 naf_values.emplace_back(offset_entry);
537 for (
size_t i = 0; i < naf_values.size(); ++i) {
542 for (
size_t i = 0; i < naf_values.size(); ++i) {
545 accumulators.emplace_back(t1 - t2);
550 for (
size_t i = 0; i < 256; ++i) {
556 curve::fq_ct x = curve::fq_ct::unsafe_construct_from_limbs(xlo.first, xlo.second, xhi.first, xhi.second);
557 curve::fq_ct y = curve::fq_ct::unsafe_construct_from_limbs(ylo.first, ylo.second, yhi.first, yhi.second);
559 const auto res = curve::g1_ct(x, y).get_value();
561 scalar = scalar + scalar;
562 scalar = scalar - 255;
565 EXPECT_EQ(res, expec);
567 curve::g1_ct accumulator;
574 curve::fq_ct x = curve::fq_ct::unsafe_construct_from_limbs(xlo.first, xlo.second, xhi.first, xhi.second);
575 curve::fq_ct y = curve::fq_ct::unsafe_construct_from_limbs(ylo.first, ylo.second, yhi.first, yhi.second);
576 accumulator = curve::g1_ct(x, y);
578 for (
size_t i = 1; i < circuit_naf_values.size(); ++i) {
579 accumulator = accumulator.dbl();
580 accumulator = accumulator.dbl();
581 accumulator = accumulator.dbl();
582 accumulator = accumulator.dbl();
583 accumulator = accumulator.dbl();
584 accumulator = accumulator.dbl();
585 accumulator = accumulator.dbl();
591 curve::fq_ct x = curve::fq_ct::unsafe_construct_from_limbs(xlo.first, xlo.second, xhi.first, xhi.second);
592 curve::fq_ct y = curve::fq_ct::unsafe_construct_from_limbs(ylo.first, ylo.second, yhi.first, yhi.second);
593 accumulator = accumulator.montgomery_ladder(curve::g1_ct(x, y));
597 accumulator = accumulator - curve::g1_ct::one(&
builder);
602 EXPECT_EQ(result, expected);
605 EXPECT_EQ(proof_result,
true);