Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
emit_public_log.cpp
Go to the documentation of this file.
2
3#include <cstdint>
4#include <stdexcept>
5
6namespace bb::avm2::simulation {
7
19 const AztecAddress& contract_address,
20 MemoryAddress log_address,
21 uint32_t log_size)
22{
23 uint64_t end_log_address_upper_bound = static_cast<uint64_t>(log_address) + static_cast<uint64_t>(log_size);
24 bool error_memory_out_of_bounds = greater_than.gt(end_log_address_upper_bound, AVM_MEMORY_SIZE);
25
26 auto& side_effect_tracker = context.get_side_effect_tracker();
27 uint32_t prev_emitted_log_fields = side_effect_tracker.get_side_effects().get_num_public_log_fields();
28
29 uint64_t total_log_fields_size = PUBLIC_LOG_HEADER_LENGTH + log_size;
30 uint64_t expected_next_emitted_log_fields = prev_emitted_log_fields + total_log_fields_size;
31
32 bool error_too_many_log_fields = greater_than.gt(expected_next_emitted_log_fields, FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH);
33
34 bool error_is_static = context.get_is_static();
35
36 // Will be computed in the loop below
37 bool error_tag_mismatch = false;
38
40 if (!error_memory_out_of_bounds) {
41 values.reserve(log_size);
42 for (uint32_t i = 0; i < log_size; ++i) {
43 MemoryValue value = memory.get(log_address + i);
44 if (value.get_tag() != ValueTag::FF) {
45 error_tag_mismatch = true;
46 }
47 values.push_back(value);
48 }
49 }
50
51 bool error = error_memory_out_of_bounds || error_too_many_log_fields || error_tag_mismatch || error_is_static;
52
53 if (!error) {
54 side_effect_tracker.add_public_log(contract_address, std::vector<FF>(values.begin(), values.end()));
55 }
56
59 .contract_address = contract_address,
60 .space_id = memory.get_space_id(),
61 .log_address = log_address,
62 .log_size = log_size,
63 .prev_num_public_log_fields = prev_emitted_log_fields,
64 .next_num_public_log_fields = side_effect_tracker.get_side_effects().get_num_public_log_fields(),
65 .is_static = error_is_static,
66 .values = values,
67 .error_memory_out_of_bounds = error_memory_out_of_bounds,
68 .error_too_many_log_fields = error_too_many_log_fields,
69 .error_tag_mismatch = error_tag_mismatch,
70 });
71
72 if (error_memory_out_of_bounds) {
73 throw EmitPublicLogException("Memory out of bounds");
74 }
75 if (error_too_many_log_fields) {
76 throw EmitPublicLogException("Too many logs");
77 }
78 if (error_tag_mismatch) {
79 throw EmitPublicLogException("Tag mismatch");
80 }
81 if (error_is_static) {
82 throw EmitPublicLogException("Static call cannot update the state.");
83 }
84}
85
90
95
100
101} // namespace bb::avm2::simulation
#define FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH
#define PUBLIC_LOG_HEADER_LENGTH
#define AVM_MEMORY_SIZE
ExecutionIdManagerInterface & execution_id_manager
EventEmitterInterface< EmitPublicLogEvent > & events
void emit_public_log(MemoryInterface &memory, ContextInterface &context, const AztecAddress &contract_address, MemoryAddress log_offset, uint32_t log_size) override
Emit a public log from the current execution context.
virtual void emit(Event &&event)=0
virtual uint32_t get_execution_id() const =0
virtual bool gt(const FF &a, const FF &b)=0
AVM range check gadget for witness generation.
uint32_t MemoryAddress
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
SideEffectTracker side_effect_tracker