Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
logic.cpp
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#include "logic.hpp"
8#include "../circuit_builders/circuit_builders.hpp"
9#include "../plookup/plookup.hpp"
13#include <cstddef>
14
15namespace bb::stdlib {
16
31template <typename Builder>
33 field_pt& a,
34 field_pt& b,
35 size_t num_bits,
36 bool is_xor_gate,
37 const std::function<std::pair<uint256_t, uint256_t>(uint256_t, uint256_t, size_t)>& get_chunk)
38{
39 // ensure the number of bits doesn't exceed field size and is not negative
41 BB_ASSERT_GT(num_bits, 0U);
42
43 if (a.is_constant() && !b.is_constant()) {
44 Builder* ctx = b.get_context();
45 uint256_t a_native(a.get_value());
46 field_pt a_witness = field_pt::from_witness_index(ctx, ctx->put_constant_variable(a_native));
47 return create_logic_constraint(a_witness, b, num_bits, is_xor_gate, get_chunk);
48 }
49
50 if (!a.is_constant() && b.is_constant()) {
51 Builder* ctx = a.get_context();
52 uint256_t b_native(b.get_value());
53 field_pt b_witness = field_pt::from_witness_index(ctx, ctx->put_constant_variable(b_native));
54 return create_logic_constraint(a, b_witness, num_bits, is_xor_gate, get_chunk);
55 }
56
57 Builder* ctx = a.get_context();
58
59 // We slice the input values into 32-bit chunks, and then use a multi-table lookup to compute the AND or XOR
60 // of each chunk. Since we perform the lookup from 32-bit multi-tables, the lookup operation implicitly enforces a
61 // 32-bit range constraint on each chunk. However, if `num_bits` is not a multiple of 32, the last chunk will be
62 // smaller than 32 bits. Therefore, the last chunk needs to be explicitly range-constrained to ensure it is in the
63 // correct range. The result is then reconstructed from the chunks, and checked against the original value.
64 const size_t num_chunks = (num_bits / 32) + ((num_bits % 32 == 0) ? 0 : 1);
65 auto left((uint256_t)a.get_value());
66 auto right((uint256_t)b.get_value());
67
68 field_pt a_accumulator(bb::fr::zero());
69 field_pt b_accumulator(bb::fr::zero());
70
71 field_pt res(ctx, 0);
72 for (size_t i = 0; i < num_chunks; ++i) {
73 size_t chunk_size = (i != num_chunks - 1) ? 32 : num_bits - i * 32;
74 auto [left_chunk, right_chunk] = get_chunk(left, right, chunk_size);
75
76 field_pt a_chunk = witness_pt(ctx, left_chunk);
77 field_pt b_chunk = witness_pt(ctx, right_chunk);
78 const auto multi_table_id = is_xor_gate ? plookup::MultiTableId::UINT32_XOR : plookup::MultiTableId::UINT32_AND;
79 field_pt result_chunk = stdlib::plookup_read<Builder>::read_from_2_to_1_table(multi_table_id, a_chunk, b_chunk);
80
81 auto scaling_factor = uint256_t(1) << (32 * i);
82 a_accumulator += a_chunk * scaling_factor;
83 b_accumulator += b_chunk * scaling_factor;
84
85 if (chunk_size != 32) {
86 // If the chunk is smaller than 32 bits, we need to explicitly range constrain it.
87 ctx->create_range_constraint(
88 a_chunk.witness_index, chunk_size, "stdlib logic: bad range on final chunk of left operand");
89 ctx->create_range_constraint(
90 b_chunk.witness_index, chunk_size, "stdlib logic: bad range on final chunk of right operand");
91 }
92
93 res += result_chunk * scaling_factor;
94
95 left = left >> 32;
96 right = right >> 32;
97 }
98
99 field_pt a_slice_other = a.split_at(num_bits).first;
100 field_pt b_slice_other = b.split_at(num_bits).first;
101 a_slice_other.assert_equal(a_accumulator, "stdlib logic: failed to reconstruct left operand");
102 b_slice_other.assert_equal(b_accumulator, "stdlib logic: failed to reconstruct right operand");
103
104 return res;
105}
106template class logic<bb::UltraCircuitBuilder>;
107template class logic<bb::MegaCircuitBuilder>;
108} // namespace bb::stdlib
#define BB_ASSERT_GT(left, right,...)
Definition assert.hpp:87
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:129
void assert_equal(const field_t &rhs, std::string const &msg="field_t::assert_equal") const
Copy constraint: constrain that *this field is equal to rhs element.
Definition field.cpp:929
static field_t from_witness_index(Builder *ctx, uint32_t witness_index)
Definition field.cpp:59
uint32_t witness_index
Definition field.hpp:132
static field_pt create_logic_constraint(field_pt &a, field_pt &b, size_t num_bits, bool is_xor_gate, const std::function< std::pair< uint256_t, uint256_t >(uint256_t, uint256_t, size_t)> &get_chunk=[](uint256_t left, uint256_t right, size_t chunk_size) { uint256_t left_chunk=left &((uint256_t(1)<< chunk_size) - 1);uint256_t right_chunk=right &((uint256_t(1)<< chunk_size) - 1);return std::make_pair(left_chunk, right_chunk);})
A logical AND or XOR over a variable number of bits.
Definition logic.cpp:32
static field_pt read_from_2_to_1_table(const plookup::MultiTableId id, const field_pt &key_a, const field_pt &key_b)
Definition plookup.cpp:79
FF a
FF b
stdlib::witness_t< bb::UltraCircuitBuilder > witness_pt
constexpr size_t MAX_NO_WRAP_INTEGER_BIT_LENGTH
Definition grumpkin.hpp:15
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static constexpr field zero()