Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
calldata_trace.cpp
Go to the documentation of this file.
2
3#include <cstddef>
4#include <cstdint>
5#include <vector>
6
11
12namespace bb::avm2::tracegen {
13
25{
26 using C = Column;
27
28 uint32_t row = 1; // Has shifted columns
29
30 for (const auto& event : events) {
31 const auto& calldata = event.calldata;
32 const auto context_id = event.context_id;
33
34 for (size_t i = 0; i < calldata.size(); i++) {
35 bool is_end = i == calldata.size() - 1;
36 trace.set(row,
37 { {
38 { C::calldata_sel, 1 },
39 { C::calldata_context_id, context_id },
40 { C::calldata_value, calldata[i] },
41 { C::calldata_index, i + 1 },
42 { C::calldata_end, is_end ? 1 : 0 },
43 } });
44 row++;
45 }
46 }
47}
48
61{
62 using C = Column;
63 uint32_t row = 1;
64
65 for (const auto& event : events) {
66 std::vector<FF> calldata_with_sep = { DOM_SEP__PUBLIC_CALLDATA };
67 const size_t input_len = event.calldata.size() + 1; // +1 for the separator
68
69 // We must pad up to the next multiple of 3:
70 // n % 3 == 0 => padding_amount = 0 = 2n % 3
71 // n % 3 == 1 => padding_amount = 2 = 2n % 3
72 // n % 3 == 2 => padding_amount = 1 = 2n % 3
73 const auto padding_amount = (2 * input_len) % 3;
74 const auto padded_len = input_len + padding_amount;
75
76 calldata_with_sep.reserve(padded_len);
77 calldata_with_sep.insert(calldata_with_sep.end(), event.calldata.begin(), event.calldata.end());
78 // We add padding values (FF(0)) to ensure each array access in loop below is within bounds.
79 calldata_with_sep.insert(calldata_with_sep.end(), padding_amount, FF(0));
80
81 auto num_rounds_rem = padded_len / 3;
82 uint32_t index = 0;
83 while (num_rounds_rem > 0) {
84 bool start = index == 0;
85 bool end = num_rounds_rem == 1;
86 trace.set(row,
87 { {
88 { C::calldata_hashing_sel, 1 },
89 { C::calldata_hashing_start, start ? 1 : 0 },
90 { C::calldata_hashing_sel_not_start, !start ? 1 : 0 },
91 { C::calldata_hashing_context_id, event.context_id },
92 { C::calldata_hashing_calldata_size, event.calldata.size() },
93 { C::calldata_hashing_input_len, input_len },
94 { C::calldata_hashing_rounds_rem, num_rounds_rem },
95 { C::calldata_hashing_index_0_, index },
96 { C::calldata_hashing_index_1_, index + 1 },
97 { C::calldata_hashing_index_2_, index + 2 },
98 { C::calldata_hashing_input_0_, calldata_with_sep[index] },
99 { C::calldata_hashing_input_1_, calldata_with_sep[index + 1] },
100 { C::calldata_hashing_input_2_, calldata_with_sep[index + 2] },
101 { C::calldata_hashing_output_hash, event.calldata_hash },
102 { C::calldata_hashing_sel_not_padding_1, end && (padding_amount == 2) ? 0 : 1 },
103 { C::calldata_hashing_sel_not_padding_2, end && (padding_amount > 0) ? 0 : 1 },
104 { C::calldata_hashing_end, end ? 1 : 0 },
105 { C::calldata_hashing_sel_end_not_empty, end && !event.calldata.empty() ? 1 : 0 },
106 } });
107 index += 3;
108 row++;
109 num_rounds_rem--;
110 }
111 }
112}
113
117 .add<InteractionType::LookupSequential, lookup_calldata_hashing_get_calldata_field_1_settings>()
119 .add<InteractionType::Permutation, perm_calldata_hashing_check_final_size_settings>()
121
122} // namespace bb::avm2::tracegen
#define DOM_SEP__PUBLIC_CALLDATA
void process_hashing(const simulation::EventEmitterInterface< simulation::CalldataEvent >::Container &events, TraceContainer &trace)
Populate the calldata hashing trace (calldata_hashing.pil) from calldata events.
static const InteractionDefinition interactions
void process_retrieval(const simulation::EventEmitterInterface< simulation::CalldataEvent >::Container &events, TraceContainer &trace)
Populate the calldata retrieval trace (calldata.pil) from calldata events.
InteractionDefinition & add(auto &&... args)
TestTraceContainer trace
AvmFlavorSettings::FF FF
Definition field.hpp:10
simulation::PublicDataTreeReadWriteEvent event
uint32_t context_id
Settings to be passed ot GenericLookupRelationImpl.