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
8#include "barretenberg/common/streams.hpp" // Derives operator<< from SERIALIZATION_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,
82};
83
85// Keys, Instances, Classes
87
88// Only `incoming_viewing_key` is exposed as a point (since address derivation
89// needs the curve point in-circuit); the other five keys are exposed as their hashes.
90struct PublicKeys {
97
103
104 bool operator==(const PublicKeys& other) const = default;
105
106 // Custom msgpack with TS camelCase field names
107 // TODO(fcarreiro): solve with macro
108 void msgpack(auto pack_fn)
109 {
110 pack_fn("npkMHash",
112 "ivpkM",
114 "ovpkMHash",
116 "tpkMHash",
118 "mspkMHash",
120 "fbpkMHash",
122 }
123};
124
126 FF salt = 0;
133
134 bool operator==(const ContractInstance& other) const = default;
135
136 // Custom msgpack with TS camelCase field names
137 // TODO(fcarreiro): solve with macro
138 void msgpack(auto pack_fn)
139 {
140 pack_fn(NVP(salt),
141 "deployer",
142 deployer,
143 "currentContractClassId",
145 "originalContractClassId",
147 "initializationHash",
149 "immutablesHash",
151 "publicKeys",
153 }
154};
155
156// Similar to ContractClassPublicWithCommitment in TS but without:
157// - version
158// - privateFunctions[]
159// - utilityFunctions[]
161 FF id = 0;
164 std::vector<uint8_t> packed_bytecode;
166
167 bool operator==(const ContractClassWithCommitment& other) const = default;
168
169 // Custom msgpack with TS camelCase field names
170 // TODO(fcarreiro): solve with macro
171 void msgpack(auto pack_fn)
172 {
173 pack_fn(NVP(id),
174 "artifactHash",
176 "privateFunctionsRoot",
178 "packedBytecode",
180 "publicBytecodeCommitment",
182 }
183};
184
185// Similar to ContractClassPublic in TS but without:
186// - version
187// - privateFunctions[]
188// - utilityFunctions[]
190 FF id = 0;
193 std::vector<uint8_t> packed_bytecode;
194
195 bool operator==(const ContractClass& other) const = default;
196
197 // Custom msgpack with TS camelCase field names
198 // TODO(fcarreiro): solve with macro
199 void msgpack(auto pack_fn)
200 {
201 pack_fn(NVP(id),
202 "artifactHash",
204 "privateFunctionsRoot",
206 "packedBytecode",
208 }
209
210 ContractClassWithCommitment with_commitment(const FF& public_bytecode_commitment) const
211 {
212 return {
213 .id = id,
214 .artifact_hash = artifact_hash,
215 .private_functions_root = private_functions_root,
216 .packed_bytecode = packed_bytecode,
217 .public_bytecode_commitment = public_bytecode_commitment,
218 };
219 }
220};
221
223// Size Effect Types
225
229
230 bool operator==(const L2ToL1Message& other) const = default;
231
233};
234
243
244struct PublicLog {
245 std::vector<FF> fields;
247
248 bool operator==(const PublicLog& other) const = default;
249
251};
252
254 uint32_t length = 0;
255 std::array<FF, FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH> payload{};
256 bool operator==(const PublicLogs& other) const = default;
257
258 PublicLogs() = default;
259 PublicLogs(uint32_t length, const std::array<FF, FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH>& payload)
260 : length(length)
262 {}
264 : PublicLogs(from_logs(logs))
265 {}
266
267 void add_log(const PublicLog& log)
268 {
269 // Header
270 payload[length] = log.fields.size();
272 // Payload
273 for (size_t i = 0; i < log.fields.size(); ++i) {
275 }
276 length += static_cast<uint32_t>(log.fields.size()) + PUBLIC_LOG_HEADER_LENGTH;
277 }
278
280 {
281 PublicLogs public_logs;
282 for (const auto& log : logs) {
283 public_logs.add_log(log);
284 }
285 return public_logs;
286 }
287
289 {
291 for (uint32_t i = 0; i < length;) {
292 uint32_t log_length = static_cast<uint32_t>(payload[i]);
293 AztecAddress contract_address = payload[i + 1];
294 std::vector<FF> fields;
295 for (uint32_t j = 0; j < log_length; ++j) {
296 fields.push_back(payload[i + 2 + j]);
297 }
298 logs.push_back(PublicLog{ .fields = fields, .contract_address = contract_address });
299 i += log_length + PUBLIC_LOG_HEADER_LENGTH;
300 }
301 return logs;
302 }
303
305};
306
310
311 bool operator==(const PublicDataWrite& other) const = default;
312
314};
315
317// Gas Types
319
328
329struct Gas {
330 uint32_t l2_gas = 0;
331 uint32_t da_gas = 0;
332
333 bool operator==(const Gas& other) const = default;
334
335 Gas operator+(const Gas& other) const { return { l2_gas + other.l2_gas, da_gas + other.da_gas }; }
336 Gas operator-(const Gas& other) const { return { l2_gas - other.l2_gas, da_gas - other.da_gas }; }
337
339};
340
350
361
363// Public Call Requests
365
376
386
397
399// Contract Deployment Data Types
401
403 std::vector<FF> fields;
404
405 bool operator==(const ContractClassLogFields& other) const = default;
406
408};
409
419
421 std::vector<FF> fields;
422 uint32_t emitted_length = 0;
423
424 bool operator==(const PrivateLog& other) const = default;
425
427};
428
437
439// Accumulated Data Types
441
451
453 std::array<FF, MAX_NOTE_HASHES_PER_TX> note_hashes{};
454 std::array<FF, MAX_NULLIFIERS_PER_TX> nullifiers{};
456
457 bool operator==(const PrivateToAvmAccumulatedData& other) const = default;
458
460};
461
473
475// Global Variables
477
493
495// Tree Snapshots
497
507
518
519struct TreeState {
521 uint32_t counter = 0;
522
523 bool operator==(const TreeState& other) const = default;
525};
526
536
538// Misc Types
540
541enum class RevertCode : uint8_t {
542 OK,
546};
547
548enum class DebugLogLevel : uint8_t {
549 SILENT = 0,
550 FATAL = 1,
551 ERROR = 2,
552 WARN = 3,
553 INFO = 4,
554 VERBOSE = 5,
555 DEBUG = 6,
556 TRACE = 7,
557 LAST = TRACE
558};
559
560inline bool is_valid_debug_log_level(uint8_t v)
561{
562 return v <= static_cast<uint8_t>(DebugLogLevel::LAST);
563}
564
566{
567 switch (lvl) {
569 return "silent";
571 return "fatal";
573 return "error";
575 return "warn";
577 return "info";
579 return "verbose";
581 return "debug";
583 return "trace";
584 }
585
586 __builtin_unreachable();
587}
588
589struct DebugLog {
591 // Level is a string since on the TS side is a union type of strings
592 // We could make it a number but we'd need to/from validation and conversion on the TS side.
593 // Consider doing that if it becomes a performance problem.
594 std::string level;
595 std::string message;
596 std::vector<FF> fields;
597
598 bool operator==(const DebugLog& other) const = default;
600};
601
603 std::array<AztecAddress, MAX_PROTOCOL_CONTRACTS> derived_addresses{};
604
605 bool operator==(const ProtocolContracts& other) const = default;
606
608};
609
611{
612 return !address.is_zero() && static_cast<uint256_t>(address) <= MAX_PROTOCOL_CONTRACTS;
613}
614
616 const AztecAddress& canonical_address)
617{
618 BB_ASSERT(is_protocol_contract_address(canonical_address), "Protocol contract canonical address out of bounds");
619 AztecAddress derived_address =
620 protocol_contracts.derived_addresses.at(static_cast<uint32_t>(canonical_address) - 1);
621 if (derived_address.is_zero()) {
622 return std::nullopt;
623 }
624 return derived_address;
625}
626
627} // namespace bb::avm2
628
629MSGPACK_ADD_ENUM(bb::avm2::RevertCode)
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#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:44
#define NVP(...)
uint32_t PC
bool is_valid_debug_log_level(uint8_t v)
AvmFlavorSettings::FF FF
Definition field.hpp:10
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
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:45
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
bool operator==(const L2ToL1Message &other) const =default
SERIALIZATION_FIELDS(recipient, content)
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
void msgpack(auto pack_fn)
AffinePoint incoming_viewing_key
bool operator==(const PublicKeys &other) const =default
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
SERIALIZATION_FIELDS(length, payload)
std::array< FF, FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH > payload
static PublicLogs from_logs(const std::vector< PublicLog > &logs)
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
SERIALIZATION_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