Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
biggroup_edgecase_handling.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
12
14
24template <typename C, class Fq, class Fr, class G>
26{
27 constexpr typename G::affine_element offset_generator =
28 get_precomputed_generators<G, "biggroup table offset generator", 1>()[0];
29
30 return offset_generator;
31}
32
40template <typename C, class Fq, class Fr, class G>
42 const std::vector<element>& _points, const std::vector<Fr>& _scalars)
43{
45 std::vector<Fr> scalars;
46 BB_ASSERT_EQ(_points.size(), _scalars.size());
47 using NativeFr = typename Fr::native;
48 auto running_scalar = NativeFr::one();
49 // Get the offset generator G_offset in native and in-circuit form
50 auto native_offset_generator = element::compute_table_offset_generator();
51 Fr last_scalar = Fr(0);
52 NativeFr generator_coefficient = NativeFr(2).pow(_points.size());
53 auto generator_coefficient_inverse = generator_coefficient.invert();
54 // For each point and scalar
55 for (size_t i = 0; i < _points.size(); i++) {
56 scalars.push_back(_scalars[i]);
57 // Convert point into point + 2ⁱ⋅G_offset
58 points.push_back(_points[i] + (native_offset_generator * running_scalar));
59 // Add \frac{2ⁱ⋅scalar}{2ⁿ} to the last scalar
60 last_scalar += _scalars[i] * (running_scalar * generator_coefficient_inverse);
61 // Double the running scalar
62 running_scalar += running_scalar;
63 }
64
65 // Add a scalar -(<(1,2,4,...,2ⁿ⁻¹ ),(scalar₀,...,scalarₙ₋₁)> / 2ⁿ)
66 scalars.push_back(-last_scalar);
67 if constexpr (Fr::is_composite) {
68 scalars.back().self_reduce();
69 }
70 // Add in-circuit G_offset to points
71 points.push_back(element(native_offset_generator * generator_coefficient));
72
73 return { points, scalars };
74}
75
80template <typename C, class Fq, class Fr, class G>
82 const std::vector<element>& _points, const std::vector<Fr>& _scalars)
83{
84 auto builder = _points[0].get_context();
86 std::vector<Fr> scalars;
87 element one = element::one(builder);
88
89 for (auto [_point, _scalar] : zip_view(_points, _scalars)) {
90 bool_ct is_point_at_infinity = _point.is_point_at_infinity();
91 if (is_point_at_infinity.get_value() && static_cast<bool>(is_point_at_infinity.is_constant())) {
92 // if point is at infinity and a circuit constant we can just skip.
93 continue;
94 }
95 if (_scalar.get_value() == 0 && _scalar.is_constant()) {
96 // if scalar multiplier is 0 and also a constant, we can skip
97 continue;
98 }
99 Fq updated_x = Fq::conditional_assign(is_point_at_infinity, one.x, _point.x);
100 Fq updated_y = Fq::conditional_assign(is_point_at_infinity, one.y, _point.y);
101 element point(updated_x, updated_y);
102 Fr scalar = Fr::conditional_assign(is_point_at_infinity, 0, _scalar);
103
104 points.push_back(point);
105 scalars.push_back(scalar);
106 // TODO(https://github.com/AztecProtocol/barretenberg/issues/1002): if both point and scalar are constant,
107 // don't bother adding constraints
108 }
109
110 return { points, scalars };
111}
112} // namespace bb::stdlib::element_default
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:59
Implements boolean logic in-circuit.
Definition bool.hpp:59
bool get_value() const
Definition bool.hpp:109
bool is_constant() const
Definition bool.hpp:111
AluTraceBuilder builder
Definition alu.test.cpp:123
#define G(r, i, a, b, c, d)
Definition blake2s.cpp:116
constexpr std::span< const typename Group::affine_element > get_precomputed_generators()
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Curve::ScalarField Fr