24TEST(stdlib_plookup, uint32_xor)
28 const size_t num_lookups = (32 + 5) / 6;
45 for (
size_t i = 0; i < left_slices.size(); ++i) {
46 out_expected[i] = left_slices[i] ^ right_slices[i];
47 left_expected[i] = left_slices[i];
48 right_expected[i] = right_slices[i];
51 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
52 out_expected[i] += out_expected[i + 1] * (1 << 6);
53 left_expected[i] += left_expected[i + 1] * (1 << 6);
54 right_expected[i] += right_expected[i + 1] * (1 << 6);
57 for (
size_t i = 0; i < num_lookups; ++i) {
58 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(),
bb::fr(left_expected[i]));
59 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(),
bb::fr(right_expected[i]));
60 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(),
bb::fr(out_expected[i]));
65 EXPECT_EQ(result,
true);
68TEST(stdlib_plookup, blake2s_xor_rotate_16)
72 const size_t num_lookups = 6;
85 std::vector<fr> out_expected(num_lookups);
86 std::vector<fr> left_expected(num_lookups);
87 std::vector<fr> right_expected(num_lookups);
89 for (
size_t i = 0; i < left_slices.size(); ++i) {
91 uint32_t
a =
static_cast<uint32_t
>(left_slices[i]);
92 uint32_t
b =
static_cast<uint32_t
>(right_slices[i]);
98 left_expected[i] = left_slices[i];
99 right_expected[i] = right_slices[i];
108 std::vector<fr> out_coefficients{ (1 << 6), (
bb::fr(1) /
bb::fr(1 << 22)), (1 << 2), (1 << 6), (1 << 6) };
110 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
111 out_expected[i] += out_expected[i + 1] * out_coefficients[i];
112 left_expected[i] += left_expected[i + 1] * (1 << 6);
113 right_expected[i] += right_expected[i + 1] * (1 << 6);
116 for (
size_t i = 0; i < num_lookups; ++i) {
117 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(), left_expected[i]);
118 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(), right_expected[i]);
119 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(), out_expected[i]);
127 auto mul_constant =
fr(1 << 16);
128 fr lookup_output = lookup[ColumnIdx::C3][0].get_value() * mul_constant;
129 uint32_t xor_rotate_output =
numeric::rotate32(uint32_t(left_value) ^ uint32_t(right_value), 16);
130 EXPECT_EQ(
fr(
uint256_t(xor_rotate_output)), lookup_output);
134 EXPECT_EQ(result,
true);
137TEST(stdlib_plookup, blake2s_xor_rotate_8)
141 const size_t num_lookups = 6;
154 std::vector<fr> out_expected(num_lookups);
155 std::vector<fr> left_expected(num_lookups);
156 std::vector<fr> right_expected(num_lookups);
158 for (
size_t i = 0; i < left_slices.size(); ++i) {
160 uint32_t
a =
static_cast<uint32_t
>(left_slices[i]);
161 uint32_t
b =
static_cast<uint32_t
>(right_slices[i]);
167 left_expected[i] = left_slices[i];
168 right_expected[i] = right_slices[i];
171 auto mul_constant =
fr(1 << 24);
172 std::vector<fr> out_coefficients{ (
bb::fr(1) / mul_constant), (1 << 4), (1 << 6), (1 << 6), (1 << 6) };
174 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
175 out_expected[i] += out_expected[i + 1] * out_coefficients[i];
176 left_expected[i] += left_expected[i + 1] * (1 << 6);
177 right_expected[i] += right_expected[i + 1] * (1 << 6);
180 for (
size_t i = 0; i < num_lookups; ++i) {
181 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(), left_expected[i]);
182 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(), right_expected[i]);
183 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(), out_expected[i]);
186 fr lookup_output = lookup[ColumnIdx::C3][0].get_value() * mul_constant;
187 uint32_t xor_rotate_output =
numeric::rotate32(uint32_t(left_value) ^ uint32_t(right_value), 8);
188 EXPECT_EQ(
fr(
uint256_t(xor_rotate_output)), lookup_output);
192 EXPECT_EQ(result,
true);
195TEST(stdlib_plookup, blake2s_xor_rotate_7)
199 const size_t num_lookups = 6;
212 std::vector<fr> out_expected(num_lookups);
213 std::vector<fr> left_expected(num_lookups);
214 std::vector<fr> right_expected(num_lookups);
216 for (
size_t i = 0; i < left_slices.size(); ++i) {
218 uint32_t
a =
static_cast<uint32_t
>(left_slices[i]);
219 uint32_t
b =
static_cast<uint32_t
>(right_slices[i]);
225 left_expected[i] = left_slices[i];
226 right_expected[i] = right_slices[i];
229 auto mul_constant =
fr(1 << 25);
230 std::vector<fr> out_coefficients{ (
bb::fr(1) / mul_constant), (1 << 5), (1 << 6), (1 << 6), (1 << 6) };
232 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
233 out_expected[i] += out_expected[i + 1] * out_coefficients[i];
234 left_expected[i] += left_expected[i + 1] * (1 << 6);
235 right_expected[i] += right_expected[i + 1] * (1 << 6);
238 for (
size_t i = 0; i < num_lookups; ++i) {
239 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(), left_expected[i]);
240 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(), right_expected[i]);
241 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(), out_expected[i]);
244 fr lookup_output = lookup[ColumnIdx::C3][0].get_value() * mul_constant;
245 uint32_t xor_rotate_output =
numeric::rotate32(uint32_t(left_value) ^ uint32_t(right_value), 7);
246 EXPECT_EQ(
fr(
uint256_t(xor_rotate_output)), lookup_output);
250 EXPECT_EQ(result,
true);
253TEST(stdlib_plookup, blake2s_xor)
257 const size_t num_lookups = 6;
274 for (
size_t i = 0; i < left_slices.size(); ++i) {
275 out_expected[i] = left_slices[i] ^ right_slices[i];
276 left_expected[i] = left_slices[i];
277 right_expected[i] = right_slices[i];
289 fr lookup_output = lookup[ColumnIdx::C3][2].get_value();
290 fr t2_term =
fr(1 << 12) * lookup[ColumnIdx::C3][2].get_value();
291 lookup_output +=
fr(1 << 20) * (lookup[ColumnIdx::C3][0].get_value() - t2_term);
293 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
294 out_expected[i] += out_expected[i + 1] * (1 << 6);
295 left_expected[i] += left_expected[i + 1] * (1 << 6);
296 right_expected[i] += right_expected[i + 1] * (1 << 6);
302 auto xor_rotate_output =
numeric::rotate32(uint32_t(left_value) ^ uint32_t(right_value), 12);
303 EXPECT_EQ(
fr(
uint256_t(xor_rotate_output)), lookup_output);
305 for (
size_t i = 0; i < num_lookups; ++i) {
306 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(),
bb::fr(left_expected[i]));
307 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(),
bb::fr(right_expected[i]));
308 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(),
bb::fr(out_expected[i]));
313 EXPECT_EQ(result,
true);
316TEST(stdlib_plookup, uint32_and)
320 const size_t num_lookups = (32 + 5) / 6;
335 for (
size_t i = 0; i < left_slices.size(); ++i) {
336 out_expected[i] = left_slices[i] & right_slices[i];
337 left_expected[i] = left_slices[i];
338 right_expected[i] = right_slices[i];
341 for (
size_t i = num_lookups - 2; i < num_lookups; --i) {
342 out_expected[i] += out_expected[i + 1] * (1 << 6);
343 left_expected[i] += left_expected[i + 1] * (1 << 6);
344 right_expected[i] += right_expected[i + 1] * (1 << 6);
347 for (
size_t i = 0; i < num_lookups; ++i) {
348 EXPECT_EQ(lookup[ColumnIdx::C1][i].get_value(),
bb::fr(left_expected[i]));
349 EXPECT_EQ(lookup[ColumnIdx::C2][i].get_value(),
bb::fr(right_expected[i]));
350 EXPECT_EQ(lookup[ColumnIdx::C3][i].get_value(),
bb::fr(out_expected[i]));
355 EXPECT_EQ(result,
true);
358TEST(stdlib_plookup, secp256k1_generator)
365 uint64_t wnaf_entries[18] = { 0 };
367 wnaf::fixed_wnaf<129, 1, 8>(&input_value.
data[0], &wnaf_entries[0], skew, 0);
369 std::vector<uint64_t> naf_values;
370 for (
size_t i = 0; i < 17; ++i) {
371 bool predicate = bool((wnaf_entries[i] >> 31U) & 1U);
372 uint64_t offset_entry;
374 offset_entry = (127 - (wnaf_entries[i] & 0xffffff));
376 offset_entry = (128 + (wnaf_entries[i] & 0xffffff));
378 naf_values.emplace_back(offset_entry);
382 for (
size_t i = 0; i < naf_values.size(); ++i) {
387 for (
size_t i = 0; i < naf_values.size(); ++i) {
390 accumulators.emplace_back(t1 - t2);
395 for (
size_t i = 0; i < 256; ++i) {
401 curve::fq_ct x = curve::fq_ct::unsafe_construct_from_limbs(xlo.first, xlo.second, xhi.first, xhi.second);
402 curve::fq_ct y = curve::fq_ct::unsafe_construct_from_limbs(ylo.first, ylo.second, yhi.first, yhi.second);
404 const auto res = curve::g1_ct(x, y).get_value();
406 scalar = scalar + scalar;
407 scalar = scalar - 255;
410 EXPECT_EQ(res, expec);
412 curve::g1_ct accumulator;
419 curve::fq_ct x = curve::fq_ct::unsafe_construct_from_limbs(xlo.first, xlo.second, xhi.first, xhi.second);
420 curve::fq_ct y = curve::fq_ct::unsafe_construct_from_limbs(ylo.first, ylo.second, yhi.first, yhi.second);
421 accumulator = curve::g1_ct(x, y);
423 for (
size_t i = 1; i < circuit_naf_values.size(); ++i) {
424 accumulator = accumulator.dbl();
425 accumulator = accumulator.dbl();
426 accumulator = accumulator.dbl();
427 accumulator = accumulator.dbl();
428 accumulator = accumulator.dbl();
429 accumulator = accumulator.dbl();
430 accumulator = accumulator.dbl();
436 curve::fq_ct x = curve::fq_ct::unsafe_construct_from_limbs(xlo.first, xlo.second, xhi.first, xhi.second);
437 curve::fq_ct y = curve::fq_ct::unsafe_construct_from_limbs(ylo.first, ylo.second, yhi.first, yhi.second);
438 accumulator = accumulator.dbl() + curve::g1_ct(x, y);
442 accumulator = accumulator - curve::g1_ct::one(&
builder);
447 EXPECT_EQ(result, expected);
450 EXPECT_EQ(proof_result,
true);