Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
aztec_types.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <stdexcept>
5#include <vector>
6
7#include "barretenberg/common/streams.hpp" // Derives operator<< from MSGPACK_FIELDS.
12#include "msgpack/adaptor/define_decl.hpp"
13
14namespace bb::avm2 {
15
17using BytecodeId = FF;
19using PC = uint32_t;
21// In typescript the EthAddress is a byte vector, but in our circuit implementation
22// it's represented as a field element for simplicity
23using EthAddress = FF;
24
25using FunctionSelector = FF; // really a 4-byte BE buffer in TS, but we use FF for simplicity
26
27// The Tx phases are executed in increasing order defined by these enum values.
28// Do not change the order of the enum values.
29// pil constraints rely on these constants being in consecutive order (increment by 1).
30enum class TransactionPhase : uint8_t {
34 SETUP = 3,
38 APP_LOGIC = 7,
39 TEARDOWN = 8,
41 TREE_PADDING = 10,
42 CLEANUP = 11,
43 LAST = CLEANUP,
44};
45
46// The three following constants are used in .pil files and need to match the enum counterpart.
47static_assert(static_cast<uint8_t>(TransactionPhase::SETUP) == AVM_TX_PHASE_VALUE_SETUP,
48 "TransactionPhase::LAST must match AVM_TX_PHASE_VALUE_SETUP");
49static_assert(static_cast<uint8_t>(TransactionPhase::NR_NULLIFIER_INSERTION) == AVM_TX_PHASE_VALUE_START,
50 "TransactionPhase::NR_NULLIFIER_INSERTION must match AVM_TX_PHASE_VALUE_START");
51static_assert(static_cast<uint8_t>(TransactionPhase::LAST) == AVM_TX_PHASE_VALUE_LAST,
52 "TransactionPhase::LAST must match AVM_TX_PHASE_VALUE_LAST");
53
54using InternalCallId = uint32_t;
55
60enum class EnvironmentVariable : uint8_t {
61 ADDRESS = 0,
62 SENDER = 1,
64 CHAINID = 3,
65 VERSION = 4,
66 BLOCKNUMBER = 5,
67 TIMESTAMP = 6,
70 ISSTATICCALL = 9,
71 L2GASLEFT = 10,
72 DAGASLEFT = 11,
73 MAX = DAGASLEFT,
74};
75
76enum class ContractInstanceMember : uint8_t {
77 DEPLOYER = 0,
78 CLASS_ID = 1,
79 INIT_HASH = 2,
80 MAX = INIT_HASH,
81};
82
84// Keys, Instances, Classes
86
87struct PublicKeys {
92
98
99 bool operator==(const PublicKeys& other) const = default;
100
101 // Custom msgpack with TS camelCase field names
102 // TODO(fcarreiro): solve with macro
103 void msgpack(auto pack_fn)
104 {
105 pack_fn("masterNullifierPublicKey",
107 "masterIncomingViewingPublicKey",
109 "masterOutgoingViewingPublicKey",
111 "masterTaggingPublicKey",
113 }
114};
115
117 FF salt = 0;
123
124 bool operator==(const ContractInstance& other) const = default;
125
126 // Custom msgpack with TS camelCase field names
127 // TODO(fcarreiro): solve with macro
128 void msgpack(auto pack_fn)
129 {
130 pack_fn(NVP(salt),
131 "deployer",
132 deployer,
133 "currentContractClassId",
135 "originalContractClassId",
137 "initializationHash",
139 "publicKeys",
141 }
142};
143
144// Similar to ContractClassPublicWithCommitment in TS but without:
145// - version
146// - privateFunctions[]
147// - utilityFunctions[]
149 FF id = 0;
152 std::vector<uint8_t> packed_bytecode;
154
155 bool operator==(const ContractClassWithCommitment& other) const = default;
156
157 // Custom msgpack with TS camelCase field names
158 // TODO(fcarreiro): solve with macro
159 void msgpack(auto pack_fn)
160 {
161 pack_fn(NVP(id),
162 "artifactHash",
164 "privateFunctionsRoot",
166 "packedBytecode",
168 "publicBytecodeCommitment",
170 }
171};
172
173// Similar to ContractClassPublic in TS but without:
174// - version
175// - privateFunctions[]
176// - utilityFunctions[]
178 FF id = 0;
181 std::vector<uint8_t> packed_bytecode;
182
183 bool operator==(const ContractClass& other) const = default;
184
185 // Custom msgpack with TS camelCase field names
186 // TODO(fcarreiro): solve with macro
187 void msgpack(auto pack_fn)
188 {
189 pack_fn(NVP(id),
190 "artifactHash",
192 "privateFunctionsRoot",
194 "packedBytecode",
196 }
197
198 ContractClassWithCommitment with_commitment(const FF& public_bytecode_commitment) const
199 {
200 return {
201 .id = id,
202 .artifact_hash = artifact_hash,
203 .private_functions_root = private_functions_root,
204 .packed_bytecode = packed_bytecode,
205 .public_bytecode_commitment = public_bytecode_commitment,
206 };
207 }
208};
209
211// Size Effect Types
213
217
218 bool operator==(const L2ToL1Message& other) const = default;
219
221};
222
231
232struct PublicLog {
233 std::vector<FF> fields;
235
236 bool operator==(const PublicLog& other) const = default;
237
239};
240
242 uint32_t length = 0;
243 std::array<FF, FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH> payload{};
244 bool operator==(const PublicLogs& other) const = default;
245
246 PublicLogs() = default;
247 PublicLogs(uint32_t length, const std::array<FF, FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH>& payload)
248 : length(length)
250 {}
252 : PublicLogs(from_logs(logs))
253 {}
254
255 void add_log(const PublicLog& log)
256 {
257 // Header
258 payload[length] = log.fields.size();
260 // Payload
261 for (size_t i = 0; i < log.fields.size(); ++i) {
263 }
264 length += log.fields.size() + PUBLIC_LOG_HEADER_LENGTH;
265 }
266
268 {
269 PublicLogs public_logs;
270 for (const auto& log : logs) {
271 public_logs.add_log(log);
272 }
273 return public_logs;
274 }
275
277 {
279 for (uint32_t i = 0; i < length;) {
280 uint32_t log_length = static_cast<uint32_t>(payload[i]);
282 std::vector<FF> fields;
283 for (uint32_t j = 0; j < log_length; ++j) {
284 fields.push_back(payload[i + 2 + j]);
285 }
286 logs.push_back(PublicLog{ .fields = fields, .contract_address = contract_address });
287 i += log_length + PUBLIC_LOG_HEADER_LENGTH;
288 }
289 return logs;
290 }
291
293};
294
298
299 bool operator==(const PublicDataWrite& other) const = default;
300
302};
303
305// Gas Types
307
316
317struct Gas {
318 uint32_t l2_gas = 0;
319 uint32_t da_gas = 0;
320
321 bool operator==(const Gas& other) const = default;
322
323 Gas operator+(const Gas& other) const { return { l2_gas + other.l2_gas, da_gas + other.da_gas }; }
324 Gas operator-(const Gas& other) const { return { l2_gas - other.l2_gas, da_gas - other.da_gas }; }
325
327};
328
338
349
351// Public Call Requests
353
364
374
385
387// Contract Deployment Data Types
389
391 std::vector<FF> fields;
392
393 bool operator==(const ContractClassLogFields& other) const = default;
394
396};
397
407
409 std::vector<FF> fields;
410 uint32_t emitted_length = 0;
411
412 bool operator==(const PrivateLog& other) const = default;
413
415};
416
425
427// Accumulated Data Types
429
439
441 std::array<FF, MAX_NOTE_HASHES_PER_TX> note_hashes{};
442 std::array<FF, MAX_NULLIFIERS_PER_TX> nullifiers{};
444
445 bool operator==(const PrivateToAvmAccumulatedData& other) const = default;
446
448};
449
461
463// Global Variables
465
481
483// Tree Snapshots
485
495
506
507struct TreeState {
509 uint32_t counter = 0;
510
511 bool operator==(const TreeState& other) const = default;
513};
514
524
526// Misc Types
528
529enum class RevertCode : uint8_t {
530 OK,
534};
535
536enum class DebugLogLevel : uint8_t {
537 SILENT = 0,
538 FATAL = 1,
539 ERROR = 2,
540 WARN = 3,
541 INFO = 4,
542 VERBOSE = 5,
543 DEBUG = 6,
544 TRACE = 7,
545 LAST = TRACE
546};
547
548inline bool is_valid_debug_log_level(uint8_t v)
549{
550 return v <= static_cast<uint8_t>(DebugLogLevel::LAST);
551}
552
554{
555 switch (lvl) {
557 return "silent";
559 return "fatal";
561 return "error";
563 return "warn";
565 return "info";
567 return "verbose";
569 return "debug";
571 return "trace";
572 }
573}
574
575struct DebugLog {
577 // Level is a string since on the TS side is a union type of strings
578 // We could make it a number but we'd need to/from validation and conversion on the TS side.
579 // Consider doing that if it becomes a performance problem.
580 std::string level;
581 std::string message;
582 std::vector<FF> fields;
583
584 bool operator==(const DebugLog& other) const = default;
586};
587
589 std::array<AztecAddress, MAX_PROTOCOL_CONTRACTS> derived_addresses{};
590
591 bool operator==(const ProtocolContracts& other) const = default;
592
594};
595
597{
598 return !address.is_zero() && static_cast<uint256_t>(address) <= MAX_PROTOCOL_CONTRACTS;
599}
600
602 const AztecAddress& canonical_address)
603{
604 assert(is_protocol_contract_address(canonical_address) && "Protocol contract canonical address out of bounds");
605 AztecAddress derived_address =
606 protocol_contracts.derived_addresses.at(static_cast<uint32_t>(canonical_address) - 1);
607 if (derived_address.is_zero()) {
608 return std::nullopt;
609 }
610 return derived_address;
611}
612
613} // namespace bb::avm2
614
615MSGPACK_ADD_ENUM(bb::avm2::RevertCode)
#define AVM_TX_PHASE_VALUE_LAST
#define PUBLIC_LOG_HEADER_LENGTH
#define AVM_TX_PHASE_VALUE_SETUP
#define AVM_TX_PHASE_VALUE_START
#define MAX_PROTOCOL_CONTRACTS
group_elements::affine_element< Fq, Fr, Params > affine_element
Definition group.hpp:42
#define NVP(...)
uint32_t PC
bool is_valid_debug_log_level(uint8_t v)
FF ContractClassId
uint32_t InternalCallId
std::optional< AztecAddress > get_derived_address(const ProtocolContracts &protocol_contracts, const AztecAddress &canonical_address)
bool is_protocol_contract_address(const AztecAddress &address)
std::string debug_log_level_to_string(DebugLogLevel lvl)
FF FunctionSelector
AvmFlavorSettings::FF FF
Definition field.hpp:10
size_t hash_as_tuple(const Ts &... ts)
Definition utils.hpp:22
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
unsigned __int128 uint128_t
Definition serialize.hpp:44
bool operator==(const AppendOnlyTreeSnapshot &other) const =default
MSGPACK_CAMEL_CASE_FIELDS(root, next_available_leaf_index)
std::size_t hash() const noexcept
bool operator==(const AvmAccumulatedDataArrayLengths &other) const =default
MSGPACK_CAMEL_CASE_FIELDS(note_hashes, nullifiers, l2_to_l1_msgs, public_data_writes)
std::array< FF, MAX_NULLIFIERS_PER_TX > nullifiers
std::array< PublicDataWrite, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX > public_data_writes
MSGPACK_CAMEL_CASE_FIELDS(note_hashes, nullifiers, l2_to_l1_msgs, public_logs, public_data_writes)
std::array< ScopedL2ToL1Message, MAX_L2_TO_L1_MSGS_PER_TX > l2_to_l1_msgs
std::array< FF, MAX_NOTE_HASHES_PER_TX > note_hashes
bool operator==(const AvmAccumulatedData &other) const =default
void msgpack(auto pack_fn)
std::vector< uint8_t > packed_bytecode
bool operator==(const ContractClass &other) const =default
ContractClassWithCommitment with_commitment(const FF &public_bytecode_commitment) const
bool operator==(const ContractClassLogFields &other) const =default
bool operator==(const ContractClassLog &other) const =default
MSGPACK_CAMEL_CASE_FIELDS(contract_address, fields, emitted_length)
ContractClassLogFields fields
bool operator==(const ContractClassWithCommitment &other) const =default
std::vector< uint8_t > packed_bytecode
bool operator==(const ContractDeploymentData &other) const =default
std::vector< PrivateLog > private_logs
std::vector< ContractClassLog > contract_class_logs
MSGPACK_CAMEL_CASE_FIELDS(contract_class_logs, private_logs)
bool operator==(const ContractInstance &other) const =default
ContractClassId current_contract_class_id
void msgpack(auto pack_fn)
ContractClassId original_contract_class_id
bool operator==(const DebugLog &other) const =default
AztecAddress contract_address
MSGPACK_CAMEL_CASE_FIELDS(contract_address, level, message, fields)
std::vector< FF > fields
uint128_t fee_per_l2_gas
uint128_t fee_per_da_gas
bool operator==(const GasFees &other) const =default
MSGPACK_CAMEL_CASE_FIELDS(fee_per_da_gas, fee_per_l2_gas)
Gas operator+(const Gas &other) const
MSGPACK_CAMEL_CASE_FIELDS(l2_gas, da_gas)
Gas operator-(const Gas &other) const
bool operator==(const Gas &other) const =default
MSGPACK_CAMEL_CASE_FIELDS(gas_limits, teardown_gas_limits, max_fees_per_gas, max_priority_fees_per_gas)
bool operator==(const GasSettings &other) const =default
bool operator==(const GasUsed &other) const =default
MSGPACK_CAMEL_CASE_FIELDS(total_gas, teardown_gas, public_gas, billed_gas)
MSGPACK_CAMEL_CASE_FIELDS(chain_id, version, block_number, slot_number, timestamp, coinbase, fee_recipient, gas_fees)
bool operator==(const GlobalVariables &other) const =default
MSGPACK_FIELDS(recipient, content)
bool operator==(const L2ToL1Message &other) const =default
bool operator==(const PrivateLog &other) const =default
std::vector< FF > fields
MSGPACK_CAMEL_CASE_FIELDS(fields, emitted_length)
MSGPACK_CAMEL_CASE_FIELDS(note_hashes, nullifiers, l2_to_l1_msgs)
bool operator==(const PrivateToAvmAccumulatedDataArrayLengths &other) const =default
std::array< FF, MAX_NOTE_HASHES_PER_TX > note_hashes
MSGPACK_CAMEL_CASE_FIELDS(note_hashes, nullifiers, l2_to_l1_msgs)
std::array< ScopedL2ToL1Message, MAX_L2_TO_L1_MSGS_PER_TX > l2_to_l1_msgs
std::array< FF, MAX_NULLIFIERS_PER_TX > nullifiers
bool operator==(const PrivateToAvmAccumulatedData &other) const =default
bool operator==(const ProtocolContracts &other) const =default
std::array< AztecAddress, MAX_PROTOCOL_CONTRACTS > derived_addresses
MSGPACK_CAMEL_CASE_FIELDS(derived_addresses)
MSGPACK_CAMEL_CASE_FIELDS(setup_calls, app_logic_calls, teardown_call)
bool operator==(const PublicCallRequestArrayLengths &other) const =default
MSGPACK_CAMEL_CASE_FIELDS(msg_sender, contract_address, is_static_call, calldata_hash)
bool operator==(const PublicCallRequest &other) const =default
MSGPACK_CAMEL_CASE_FIELDS(leaf_slot, value)
bool operator==(const PublicDataWrite &other) const =default
AffinePoint nullifier_key
void msgpack(auto pack_fn)
AffinePoint tagging_key
AffinePoint incoming_viewing_key
bool operator==(const PublicKeys &other) const =default
AffinePoint outgoing_viewing_key
std::vector< FF > to_fields() const
MSGPACK_CAMEL_CASE_FIELDS(fields, contract_address)
std::vector< FF > fields
bool operator==(const PublicLog &other) const =default
AztecAddress contract_address
std::array< FF, FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH > payload
static PublicLogs from_logs(const std::vector< PublicLog > &logs)
MSGPACK_FIELDS(length, payload)
PublicLogs(const std::vector< PublicLog > &logs)
PublicLogs(uint32_t length, const std::array< FF, FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH > &payload)
void add_log(const PublicLog &log)
bool operator==(const PublicLogs &other) const =default
std::vector< PublicLog > to_logs() const
bool operator==(const ScopedL2ToL1Message &other) const =default
MSGPACK_CAMEL_CASE_FIELDS(message, contract_address)
AppendOnlyTreeSnapshot public_data_tree
bool operator==(const TreeSnapshots &other) const =default
AppendOnlyTreeSnapshot l1_to_l2_message_tree
AppendOnlyTreeSnapshot nullifier_tree
MSGPACK_CAMEL_CASE_FIELDS(l1_to_l2_message_tree, note_hash_tree, nullifier_tree, public_data_tree)
AppendOnlyTreeSnapshot note_hash_tree
MSGPACK_FIELDS(tree, counter)
bool operator==(const TreeState &other) const =default
AppendOnlyTreeSnapshot tree
MSGPACK_CAMEL_CASE_FIELDS(note_hash_tree, nullifier_tree, l1_to_l2_message_tree, public_data_tree)
TreeState l1_to_l2_message_tree
bool operator==(const TreeStates &other) const =default