Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
emit_unencrypted_log.cpp
Go to the documentation of this file.
2
3#include <cassert>
4#include <cstdint>
5#include <stdexcept>
6
7namespace bb::avm2::simulation {
8
12 MemoryAddress log_address,
13 uint32_t log_size)
14{
15 uint64_t end_log_address = static_cast<uint64_t>(log_address) + static_cast<uint64_t>(log_size) - 1;
16 bool error_memory_out_of_bounds = greater_than.gt(end_log_address, AVM_HIGHEST_MEM_ADDRESS);
17
18 auto& side_effect_tracker = context.get_side_effect_tracker();
19 uint32_t prev_emitted_log_fields = side_effect_tracker.get_side_effects().get_num_unencrypted_log_fields();
20
21 uint32_t total_log_fields_size = PUBLIC_LOG_HEADER_LENGTH + log_size;
22 uint32_t expected_next_emitted_log_fields = prev_emitted_log_fields + total_log_fields_size;
23
24 bool error_too_many_log_fields = greater_than.gt(expected_next_emitted_log_fields, FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH);
25
26 bool error_is_static = context.get_is_static();
27
28 // Will be computed in the loop below
29 bool error_tag_mismatch = false;
30
32 values.reserve(log_size);
33 if (!error_memory_out_of_bounds) {
34 for (uint32_t i = 0; i < log_size; ++i) {
35 MemoryValue value = memory.get(log_address + i);
36 if (value.get_tag() != ValueTag::FF) {
37 error_tag_mismatch = true;
38 }
39 values.push_back(value);
40 }
41 }
42
43 bool error = error_memory_out_of_bounds || error_too_many_log_fields || error_tag_mismatch || error_is_static;
44
45 if (!error) {
46 side_effect_tracker.add_public_log(contract_address, std::vector<FF>(values.begin(), values.end()));
47 }
48
51 .contract_address = contract_address,
52 .space_id = memory.get_space_id(),
53 .log_address = log_address,
54 .log_size = log_size,
55 .prev_num_unencrypted_log_fields = prev_emitted_log_fields,
56 .next_num_unencrypted_log_fields = side_effect_tracker.get_side_effects().get_num_unencrypted_log_fields(),
57 .is_static = error_is_static,
58 .values = values,
59 .error_memory_out_of_bounds = error_memory_out_of_bounds,
60 .error_too_many_log_fields = error_too_many_log_fields,
61 .error_tag_mismatch = error_tag_mismatch,
62 });
63
64 if (error_memory_out_of_bounds) {
65 throw EmitUnencryptedLogException("Memory out of bounds");
66 }
67 if (error_too_many_log_fields) {
68 throw EmitUnencryptedLogException("Too many logs");
69 }
70 if (error_tag_mismatch) {
71 throw EmitUnencryptedLogException("Tag mismatch");
72 }
73 if (error_is_static) {
74 throw EmitUnencryptedLogException("Static call cannot update the state.");
75 }
76}
77
82
87
92
93} // namespace bb::avm2::simulation
#define FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH
#define PUBLIC_LOG_HEADER_LENGTH
#define AVM_HIGHEST_MEM_ADDRESS
ExecutionIdManagerInterface & execution_id_manager
EventEmitterInterface< EmitUnencryptedLogEvent > & events
void emit_unencrypted_log(MemoryInterface &memory, ContextInterface &context, const AztecAddress &contract_address, MemoryAddress log_offset, uint32_t log_size) override
virtual void emit(Event &&event)=0
virtual uint32_t get_execution_id() const =0
virtual bool gt(const FF &a, const FF &b)=0
uint32_t MemoryAddress
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
SideEffectTracker side_effect_tracker