Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
range_check_trace.cpp
Go to the documentation of this file.
2
3#include <cstddef>
4#include <cstdint>
5#include <memory>
6#include <ranges>
7#include <stdexcept>
8
13
14namespace bb::avm2::tracegen {
15
18{
19 using C = Column;
20
21 uint32_t row = 0;
22 for (const auto& event : events) {
23 // store off event entries to be used directly in row
24 const uint256_t original_num_bits = event.num_bits;
25 const uint256_t original_value = uint256_t::from_uint128(event.value);
26
27 // these will be mutated below
28 uint8_t num_bits = event.num_bits;
30
31 std::array<uint16_t, 7> fixed_slice_registers; // u16_r0...6
32 size_t index_of_most_sig_16b_chunk = 0;
33 uint16_t dynamic_slice_register = 0; // same as u16_r7
34 uint8_t dynamic_bits = 0;
35
36 // Split the value into 16-bit chunks
37 for (size_t i = 0; i < 8; i++) {
38 // The most significant 16-bits have to be placed in the dynamic slice register
39 if (num_bits <= 16) {
40 dynamic_slice_register = static_cast<uint16_t>(value);
41 index_of_most_sig_16b_chunk = i;
42 dynamic_bits = num_bits;
43 break;
44 }
45 // We have more chunks of 16-bits to operate on, so set the ith fixed register
46 fixed_slice_registers[i] = static_cast<uint16_t>(value);
47 num_bits -= 16;
48 value >>= 16;
49 }
50
51 auto dynamic_diff = static_cast<uint16_t>((1 << dynamic_bits) - dynamic_slice_register - 1);
52
53 trace.set(row,
54 { {
55 { C::range_check_sel, 1 },
56 // value to range check
57 { C::range_check_value, original_value },
58 // number of bits to check the value against
59 { C::range_check_rng_chk_bits, original_num_bits },
60 // flag indicating which bit size range is active
61 { C::range_check_is_lte_u16, index_of_most_sig_16b_chunk == 0 ? 1 : 0 },
62 { C::range_check_is_lte_u32, index_of_most_sig_16b_chunk == 1 ? 1 : 0 },
63 { C::range_check_is_lte_u48, index_of_most_sig_16b_chunk == 2 ? 1 : 0 },
64 { C::range_check_is_lte_u64, index_of_most_sig_16b_chunk == 3 ? 1 : 0 },
65 { C::range_check_is_lte_u80, index_of_most_sig_16b_chunk == 4 ? 1 : 0 },
66 { C::range_check_is_lte_u96, index_of_most_sig_16b_chunk == 5 ? 1 : 0 },
67 { C::range_check_is_lte_u112, index_of_most_sig_16b_chunk == 6 ? 1 : 0 },
68 { C::range_check_is_lte_u128, index_of_most_sig_16b_chunk == 7 ? 1 : 0 },
69 // slice registers
70 { C::range_check_u16_r0, fixed_slice_registers[0] },
71 { C::range_check_u16_r1, fixed_slice_registers[1] },
72 { C::range_check_u16_r2, fixed_slice_registers[2] },
73 { C::range_check_u16_r3, fixed_slice_registers[3] },
74 { C::range_check_u16_r4, fixed_slice_registers[4] },
75 { C::range_check_u16_r5, fixed_slice_registers[5] },
76 { C::range_check_u16_r6, fixed_slice_registers[6] },
77 { C::range_check_u16_r7, dynamic_slice_register },
78 // computations on dynamic slice register
79 { C::range_check_dyn_rng_chk_bits, dynamic_bits },
80 { C::range_check_dyn_rng_chk_pow_2, 1 << dynamic_bits },
81 { C::range_check_dyn_diff, dynamic_diff },
82 // Lookup selectors
83 { C::range_check_sel_r0_16_bit_rng_lookup, index_of_most_sig_16b_chunk > 0 ? 1 : 0 },
84 { C::range_check_sel_r1_16_bit_rng_lookup, index_of_most_sig_16b_chunk > 1 ? 1 : 0 },
85 { C::range_check_sel_r2_16_bit_rng_lookup, index_of_most_sig_16b_chunk > 2 ? 1 : 0 },
86 { C::range_check_sel_r3_16_bit_rng_lookup, index_of_most_sig_16b_chunk > 3 ? 1 : 0 },
87 { C::range_check_sel_r4_16_bit_rng_lookup, index_of_most_sig_16b_chunk > 4 ? 1 : 0 },
88 { C::range_check_sel_r5_16_bit_rng_lookup, index_of_most_sig_16b_chunk > 5 ? 1 : 0 },
89 { C::range_check_sel_r6_16_bit_rng_lookup, index_of_most_sig_16b_chunk > 6 ? 1 : 0 },
90 } });
91
92 row++;
93 }
94}
95
99 .add<lookup_range_check_dyn_rng_chk_pow_2_settings, InteractionType::LookupIntoIndexedByClk>()
101 .add<lookup_range_check_r1_is_u16_settings, InteractionType::LookupIntoIndexedByClk>()
103 .add<lookup_range_check_r3_is_u16_settings, InteractionType::LookupIntoIndexedByClk>()
105 .add<lookup_range_check_r5_is_u16_settings, InteractionType::LookupIntoIndexedByClk>()
107 .add<lookup_range_check_r7_is_u16_settings, InteractionType::LookupIntoIndexedByClk>();
108
109} // namespace bb::avm2::tracegen
InteractionDefinition & add(auto &&... args)
void process(const simulation::EventEmitterInterface< simulation::RangeCheckEvent >::Container &events, TraceContainer &trace)
static const InteractionDefinition interactions
static constexpr uint256_t from_uint128(const uint128_t a) noexcept
Definition uint256.hpp:94
TestTraceContainer trace
lookup_settings< lookup_range_check_r4_is_u16_settings_ > lookup_range_check_r4_is_u16_settings
lookup_settings< lookup_range_check_dyn_diff_is_u16_settings_ > lookup_range_check_dyn_diff_is_u16_settings
lookup_settings< lookup_range_check_r2_is_u16_settings_ > lookup_range_check_r2_is_u16_settings
lookup_settings< lookup_range_check_r6_is_u16_settings_ > lookup_range_check_r6_is_u16_settings
lookup_settings< lookup_range_check_r0_is_u16_settings_ > lookup_range_check_r0_is_u16_settings
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
simulation::PublicDataTreeReadWriteEvent event