Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
straus_plookup_table.cpp
Go to the documentation of this file.
2#include "./cycle_group.hpp"
4
5namespace bb::stdlib {
6
19template <typename Builder>
21 const AffineElement& base_point, const AffineElement& offset_generator, size_t table_bits)
22{
23 const size_t table_size = 1UL << table_bits;
24
25 // Compute native table entries using projective coordinates, then batch-normalize
26 std::vector<Element> projective_points(table_size);
27 projective_points[0] = Element(offset_generator);
28 Element base_proj(base_point);
29 for (size_t i = 1; i < table_size; ++i) {
30 projective_points[i] = projective_points[i - 1] + base_proj;
31 }
32 Element::batch_normalize(projective_points.data(), table_size);
33
34 PrecomputedData result;
35 result.native_table.resize(table_size);
36 for (size_t i = 0; i < table_size; ++i) {
37 result.native_table[i] = AffineElement(projective_points[i].x, projective_points[i].y);
38 }
39
40 // Populate BasicTable columns (table_index is NOT set here — that requires the builder)
42 result.basic_table.use_twin_keys = false;
46 result.basic_table.get_values_from_key = nullptr;
47
48 result.basic_table.column_1.resize(table_size);
49 result.basic_table.column_2.resize(table_size);
50 result.basic_table.column_3.resize(table_size);
51 for (size_t i = 0; i < table_size; ++i) {
52 result.basic_table.column_1[i] = bb::fr(i);
53 result.basic_table.column_2[i] = result.native_table[i].x;
54 result.basic_table.column_3[i] = result.native_table[i].y;
55 }
56
57 return result;
58}
59
69template <typename Builder>
71 : _context(context)
72 , native_table(std::move(data.native_table))
73{
74 _table = context->register_basic_lookup_table(std::move(data.basic_table));
75
76 // This table is built entirely from native constants, so the tag is pure constant.
78}
79
94template <typename Builder>
96 const AffineElement& base_point,
97 const AffineElement& offset_generator,
98 size_t table_bits)
99 : straus_plookup_table(context, build_precomputed_data(base_point, offset_generator, table_bits))
100{}
101
111template <typename Builder> cycle_group<Builder> straus_plookup_table<Builder>::read(const field_t& _index)
112{
113 // A plookup gate key must be a witness; convert constants to a witness constrained to the constant value
114 // (mirrors the same pattern in straus_lookup_table::read and create_gates_from_plookup_accumulators).
115 field_t index(_index);
116 if (index.is_constant()) {
117 index = field_t::from_witness(_context, _index.get_value());
118 index.assert_equal(_index.get_value());
119 }
120
121 // Get native index value and look up the corresponding point
122 auto native_index = static_cast<size_t>(uint256_t(index.get_value()));
123 BB_ASSERT(native_index < native_table.size());
124 const auto& point = native_table[native_index];
125
126 // Create witnesses for x and y outputs
127 auto x_idx = _context->add_variable(point.x);
128 auto y_idx = _context->add_variable(point.y);
129
130 // Create a standalone lookup gate constraining (index, x, y) to a valid table row.
132 entry.key = { uint256_t(native_index), 0 };
133 entry.value = { point.x, point.y };
134 _context->create_lookup_gate(index.get_witness_index(), x_idx, y_idx, *_table, entry);
135
136 // Wrap output witnesses in field_t and propagate origin tag from the index
137 field_t x = field_t::from_witness_index(_context, x_idx);
138 field_t y = field_t::from_witness_index(_context, y_idx);
139 OriginTag merged_tag(tag, index.get_origin_tag());
140 x.set_origin_tag(merged_tag);
141 y.set_origin_tag(merged_tag);
142
143 // Result is never at infinity due to offset generator in every table entry
144 return cycle_group<Builder>(x, y, /*is_infinity=*/bool_t(_context, false), /*assert_on_curve=*/false);
145}
146
149
150} // namespace bb::stdlib
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
Implements boolean logic in-circuit.
Definition bool.hpp:60
cycle_group represents a group Element of the proving system's embedded curve, i.e....
static field_t from_witness_index(Builder *ctx, uint32_t witness_index)
Definition field.cpp:67
bb::fr get_value() const
Given a := *this, compute its value given by a.v * a.mul + a.add.
Definition field.cpp:838
static field_t from_witness(Builder *ctx, const bb::fr &input)
Definition field.hpp:467
void set_origin_tag(const OriginTag &new_tag) const
Definition field.hpp:358
straus_plookup_table computes a plookup-based lookup table of size 1 << table_bits
typename Curve::AffineElement AffineElement
cycle_group< Builder > read(const field_t &index)
Read from the plookup table at the given index.
static PrecomputedData build_precomputed_data(const AffineElement &base_point, const AffineElement &offset_generator, size_t table_bits)
Compute native table entries and BasicTable column data without touching the circuit builder.
const std::vector< MemoryValue > data
StrictMock< MockContext > context
bb::curve::BN254::Element Element
@ STRAUS_EC_POINT
Definition types.hpp:91
field< Bn254FrParams > fr
Definition fr.hpp:155
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static OriginTag constant()
Definition types.hpp:289
std::array< bb::fr, 2 > value
Definition types.hpp:294
std::array< uint256_t, 2 > key
Definition types.hpp:293
std::vector< bb::fr > column_3
Definition types.hpp:323
std::vector< bb::fr > column_2
Definition types.hpp:322
std::array< bb::fr, 2 >(* get_values_from_key)(const std::array< uint64_t, 2 >)
Definition types.hpp:331
std::vector< bb::fr > column_1
Definition types.hpp:321
Precomputed data for two-phase construction. Contains all data computed without builder access.