Skip to main content

miden_protocol/transaction/kernel/
memory.rs

1use miden_core::program::KernelDescriptor;
2
3use crate::WORD_SIZE;
4// TYPE ALIASES
5// ================================================================================================
6use crate::account::AccountHeader;
7
8pub type MemoryAddress = u32;
9pub type MemoryOffset = u32;
10pub type DataIndex = usize;
11pub type MemSize = usize;
12pub type StorageSlot = u8;
13
14// PUBLIC CONSTANTS
15// ================================================================================================
16
17// General layout
18//
19// | Section            | Start address | Size in elements | Comment                                     |
20// | ------------------ | ------------- | ---------------- | ------------------------------------------- |
21// | Kernel procs       | 0             | 1_024            | 255 procedures, 4 elements each + num_procs |
22// | Bookkeeping        | 1_200         | 85               |                                             |
23// | Global inputs      | 1_600         | 40               |                                             |
24// | Block header       | 2_000         | 44               |                                             |
25// | Protocol config    | 2_200         | 24               |                                             |
26// | Partial blockchain | 2_400         | 132              |                                             |
27// | Accounts data      | 8_192         | 524_288          | 64 accounts max, 8192 elements each         |
28// | Account delta      | 532_480       | 264              | fungible + non-fungible ptr + 256 patches   |
29// | Account upgrade    | 532_744       | 8                | code + storage upgrade commitment           |
30// | Input notes        | 4_194_304     | 1_114_112        | nullifiers data segment (2^16 elements)     |
31// |                    |               |                  | + 1024 input notes max, 1024 elements each  |
32// | Output notes       | 16_777_216    | 1_048_576        | 1024 output notes max, 1024 elements each   |
33// | Link Map Memory    | 33_554_432    | 33_554_432       | Enough for 2_097_151 key-value pairs        |
34
35// Relative layout of one account
36//
37// | Section            | Start address | Size in elements | Comment                                |
38// | ------------------ | ------------- | ---------------- | -------------------------------------- |
39// | Metadata           | 0             | 4                | version + nonce + account ID           |
40// | Vault root         | 4             | 4                |                                        |
41// | Storage commitment | 8             | 4                |                                        |
42// | Code commitment    | 12            | 4                |                                        |
43// | Padding            | 16            | 12               |                                        |
44// | Num procedures     | 28            | 4                |                                        |
45// | Procedures roots   | 32            | 1_024            | 256 procedures max, 4 elements each    |
46// | Padding            | 1_056         | 4                |                                        |
47// | Proc tracking      | 1_060         | 256              | 256 procedures max, 1 element each     |
48// | Num storage slots  | 1_316         | 4                |                                        |
49// | Initial slot info  | 1_320         | 2_040            | Only initialized on the native account |
50// | Active slot info   | 3_360         | 2_040            | 255 slots max, 8 elements each         |
51// | Padding            | 5_400         | 2_792            |                                        |
52//
53// Storage slots are laid out as [[0, slot_type, slot_id_suffix, slot_id_prefix], SLOT_VALUE].
54
55// Relative layout of the native account's delta.
56//
57// For now each Storage Map pointer (a link map ptr) occupies a single element.
58//
59// | Section                      | Start address | Size in elements | Comment                      |
60// | ---------------------------- | ------------- | ---------------- | ---------------------------- |
61// | Fungible Asset Delta Ptr     | 0             | 4                |                              |
62// | Non-Fungible Asset Delta Ptr | 4             | 4                |                              |
63// | Storage Map Patch Ptrs       | 8             | 256              | Max 255 storage map patches  |
64
65// KERNEL DATA
66// ------------------------------------------------------------------------------------------------
67
68/// The procedure roots of the kernel procedures.
69///
70/// Placed at address 0 so that multiple kernels can share the same dispatcher procedure.
71pub const KERNEL_PROCEDURES_PTR: MemoryAddress = 0;
72
73/// The maximum number of procedures which the transaction kernel can have.
74pub const MAX_KERNEL_PROCEDURES: usize = KernelDescriptor::MAX_NUM_PROCEDURES;
75
76/// The memory address at which the number of the kernel procedures is stored.
77pub const NUM_KERNEL_PROCEDURES_PTR: MemoryAddress =
78    MAX_KERNEL_PROCEDURES as MemoryAddress * WORD_SIZE as MemoryAddress;
79
80// BOOKKEEPING
81// ------------------------------------------------------------------------------------------------
82
83/// The memory address at which a pointer to the currently active input note is stored.
84pub const ACTIVE_INPUT_NOTE_PTR: MemoryAddress = 1200;
85
86/// The memory address at which the number of output notes is stored.
87pub const NUM_OUTPUT_NOTES_PTR: MemoryAddress = 1201;
88
89/// The memory address at which the transaction expiration block number is stored.
90pub const TX_EXPIRATION_BLOCK_NUM_PTR: MemoryAddress = 1202;
91
92/// The memory address at which the dirty flag of the storage commitment of the native account is
93/// stored.
94///
95/// This binary flag specifies whether the commitment is outdated: it holds 1 if some changes were
96/// made to the account storage since the last re-computation, and 0 otherwise.
97pub const NATIVE_ACCT_STORAGE_COMMITMENT_DIRTY_FLAG_PTR: MemoryAddress = 1203;
98
99/// The memory address at which the input vault root is stored.
100pub const INPUT_VAULT_ROOT_PTR: MemoryAddress = 1204;
101
102/// The memory address at which the output vault root is stored.
103pub const OUTPUT_VAULT_ROOT_PTR: MemoryAddress = 1208;
104
105// Pointer to the suffix and prefix of the ID of the foreign account which will be loaded during the
106// upcoming FPI call. This ID is updated during the `prepare_fpi_call` kernel procedure.
107pub const UPCOMING_FOREIGN_ACCOUNT_PREFIX_PTR: MemoryAddress = 1212;
108pub const UPCOMING_FOREIGN_ACCOUNT_SUFFIX_PTR: MemoryAddress =
109    UPCOMING_FOREIGN_ACCOUNT_PREFIX_PTR + 1;
110
111// Pointer to the 16th input value of the foreign procedure which will be loaded during the upcoming
112// FPI call. This "buffer" value helps to work around the 15 value limitation of the
113// `exec_kernel_proc` kernel procedure, so that any account procedure, even if it has 16 input
114// values, could be executed as foreign.
115pub const UPCOMING_FOREIGN_PROC_INPUT_VALUE_15_PTR: MemoryAddress = 1214;
116
117// The memory address at which the flag indicating that the epilogue is running the account's
118// authentication procedure is stored.
119pub const EPILOGUE_AUTH_IN_PROGRESS_FLAG_PTR: MemoryAddress = 1215;
120
121// Pointer to the root of the foreign procedure which will be executed during the upcoming FPI call.
122// This root is updated during the `prepare_fpi_call` kernel procedure.
123pub const UPCOMING_FOREIGN_PROCEDURE_PTR: MemoryAddress = 1216;
124
125/// The memory address at which the pointer to the stack element containing the pointer to the
126/// active account data is stored.
127///
128/// The stack starts at the address `1221`. Stack has a length of `64` elements meaning that the
129/// maximum depth of FPI calls is `63` — the first slot is always occupied by the native account
130/// data pointer.
131///
132/// ```text
133/// ┌───────────────┬────────────────┬───────────────────┬─────┬────────────────────┐
134/// │ STACK TOP PTR │ NATIVE ACCOUNT │ FOREIGN ACCOUNT 1 │ ... │ FOREIGN ACCOUNT 63 │
135/// ├───────────────┼────────────────┼───────────────────┼─────┼────────────────────┤
136///       1220             1221              1222                      1284
137/// ```
138pub const ACCOUNT_STACK_TOP_PTR: MemoryAddress = 1220;
139
140// GLOBAL INPUTS
141// ------------------------------------------------------------------------------------------------
142
143/// The memory address at which the global inputs section begins.
144pub const GLOBAL_INPUTS_SECTION_OFFSET: MemoryOffset = 1600;
145
146/// The memory address at which the commitment of the transaction's reference block is stored.
147pub const BLOCK_COMMITMENT_PTR: MemoryAddress = 1600;
148
149/// The memory address at which the native account ID suffix provided as a global transaction input
150/// is stored.
151pub const GLOBAL_ACCOUNT_ID_SUFFIX_PTR: MemoryAddress = 1604;
152/// The memory address at which the native account ID prefix provided as a global transaction input
153/// is stored.
154pub const GLOBAL_ACCOUNT_ID_PREFIX_PTR: MemoryAddress = GLOBAL_ACCOUNT_ID_SUFFIX_PTR + 1;
155
156/// The memory address at which the initial account commitment is stored.
157pub const INIT_ACCT_COMMITMENT_PTR: MemoryAddress = 1608;
158
159/// The memory address at which the initial nonce is stored.
160pub const INIT_NONCE_PTR: MemoryAddress = 1612;
161
162/// The memory address at which the initial vault root of the native account is stored.
163pub const INIT_NATIVE_ACCT_VAULT_ROOT_PTR: MemoryAddress = 1616;
164
165/// The memory address at which the initial storage commitment of the native account is stored.
166pub const INIT_NATIVE_ACCT_STORAGE_COMMITMENT_PTR: MemoryAddress = 1620;
167
168/// The memory address at which the input notes commitment is stored.
169pub const INPUT_NOTES_COMMITMENT_PTR: MemoryAddress = 1624;
170
171/// The memory address at which the transaction script mast root is store
172pub const TX_SCRIPT_ROOT_PTR: MemoryAddress = 1628;
173
174/// The memory address at which the transaction script arguments are stored.
175pub const TX_SCRIPT_ARGS: MemoryAddress = 1632;
176
177/// The memory address at which the key of the auth procedure arguments is stored.
178pub const AUTH_ARGS_PTR: MemoryAddress = 1636;
179
180// BLOCK DATA
181// ------------------------------------------------------------------------------------------------
182
183/// The memory address at which the block data section begins, holding the block metadata.
184///
185/// The section holds the reference block header, see
186/// [`BlockHeader::to_elements`](crate::block::BlockHeader::to_elements) for its layout.
187pub const BLOCK_METADATA_PTR: MemoryAddress = 2000;
188
189/// The index of the block header version within the block metadata.
190pub const BLOCK_VERSION_IDX: DataIndex = 0;
191
192/// The index of the block number within the block metadata.
193pub const BLOCK_NUMBER_IDX: DataIndex = 1;
194
195/// The index of the timestamp within the block metadata.
196pub const TIMESTAMP_IDX: DataIndex = 2;
197
198/// The memory address at which the previous block commitment is stored.
199pub const PREV_BLOCK_COMMITMENT_PTR: MemoryAddress = 2004;
200
201/// The memory address at which the chain commitment is stored.
202pub const CHAIN_COMMITMENT_PTR: MemoryAddress = 2008;
203
204/// The memory address at which the state root is stored.
205pub const ACCT_DB_ROOT_PTR: MemoryAddress = 2012;
206
207/// The memory address at which the nullifier db root is store.
208pub const NULLIFIER_DB_ROOT_PTR: MemoryAddress = 2016;
209
210/// The memory address at which the TX commitment is stored.
211pub const TX_COMMITMENT_PTR: MemoryAddress = 2020;
212
213/// The memory address at which the protocol config commitment is stored.
214pub const PROTOCOL_CONFIG_COMMITMENT_PTR: MemoryAddress = 2024;
215
216/// The memory address at which the validator config commitment is stored.
217pub const VALIDATOR_CONFIG_COMMITMENT_PTR: MemoryAddress = 2028;
218
219/// The memory address at which the next protocol config commitment is stored.
220pub const NEXT_PROTOCOL_CONFIG_COMMITMENT_PTR: MemoryAddress = 2032;
221
222/// The memory address at which the fee parameters are stored.
223pub const FEE_PARAMETERS_PTR: MemoryAddress = 2036;
224
225/// The index of the verification base fee within the block fee parameters.
226pub const VERIFICATION_BASE_FEE_IDX: DataIndex = 0;
227
228/// The memory address at which the note root is stored.
229pub const NOTE_ROOT_PTR: MemoryAddress = 2040;
230
231// PROTOCOL CONFIG
232// ------------------------------------------------------------------------------------------------
233
234/// The memory address at which the protocol config section begins.
235pub const PROTOCOL_CONFIG_SECTION_OFFSET: MemoryOffset = 2200;
236
237/// The memory address at which the fee asset ID is stored.
238pub const FEE_ASSET_ID_PTR: MemoryAddress = 2200;
239
240/// The memory address at which the transaction kernel config commitment is stored.
241pub const TX_KERNEL_CONFIG_COMMITMENT_PTR: MemoryAddress = 2204;
242
243/// The memory address at which the batch kernel config commitment is stored.
244pub const BATCH_KERNEL_CONFIG_COMMITMENT_PTR: MemoryAddress = 2208;
245
246/// The memory address at which the block kernel config commitment is stored.
247pub const BLOCK_KERNEL_CONFIG_COMMITMENT_PTR: MemoryAddress = 2212;
248
249/// The memory address at which the proof verification config commitment is stored.
250pub const PROOF_VERIFICATION_COMMITMENT_PTR: MemoryAddress = 2216;
251
252// CHAIN DATA
253// ------------------------------------------------------------------------------------------------
254
255/// The memory address at which the chain data section begins.
256pub const PARTIAL_BLOCKCHAIN_PTR: MemoryAddress = 2400;
257
258/// The memory address at which the total number of leaves in the partial blockchain is stored.
259pub const PARTIAL_BLOCKCHAIN_NUM_LEAVES_PTR: MemoryAddress = 2400;
260
261/// The memory address at which the partial blockchain peaks are stored.
262pub const PARTIAL_BLOCKCHAIN_PEAKS_PTR: MemoryAddress = 2404;
263
264// ACCOUNT DATA
265// ------------------------------------------------------------------------------------------------
266
267/// The memory address at which the native account is stored.
268pub const NATIVE_ACCOUNT_DATA_PTR: MemoryAddress = 8192;
269
270/// The length of the memory interval that the account data occupies.
271pub const ACCOUNT_DATA_LENGTH: MemSize = 8192;
272
273/// The offset at which the account metadata word is stored relative to the start of the account
274/// data segment.
275pub const ACCT_METADATA_OFFSET: MemoryOffset = 0;
276
277/// The memory address at which the account metadata word is stored in the native account.
278pub const NATIVE_ACCT_METADATA_PTR: MemoryAddress = NATIVE_ACCOUNT_DATA_PTR + ACCT_METADATA_OFFSET;
279
280/// The index of the account version within the account metadata word.
281pub const ACCT_VERSION_IDX: DataIndex = AccountHeader::VERSION_IDX;
282
283/// The index of the account nonce within the account metadata word.
284pub const ACCT_NONCE_IDX: DataIndex = AccountHeader::NONCE_IDX;
285
286/// The index of the account ID within the account metadata word.
287pub const ACCT_ID_SUFFIX_IDX: DataIndex = AccountHeader::ID_SUFFIX_IDX;
288pub const ACCT_ID_PREFIX_IDX: DataIndex = AccountHeader::ID_PREFIX_IDX;
289
290/// The offset at which the account vault root is stored relative to the start of the account
291/// data segment.
292pub const ACCT_VAULT_ROOT_OFFSET: MemoryOffset = 4;
293
294/// The memory address at which the account vault root is stored in the native account.
295pub const NATIVE_ACCT_VAULT_ROOT_PTR: MemoryAddress =
296    NATIVE_ACCOUNT_DATA_PTR + ACCT_VAULT_ROOT_OFFSET;
297
298/// The offset at which the account storage commitment is stored relative to the start of the
299/// account data segment.
300pub const ACCT_STORAGE_COMMITMENT_OFFSET: MemoryOffset = 8;
301
302/// The memory address at which the account storage commitment is stored in the native account.
303pub const NATIVE_ACCT_STORAGE_COMMITMENT_PTR: MemoryAddress =
304    NATIVE_ACCOUNT_DATA_PTR + ACCT_STORAGE_COMMITMENT_OFFSET;
305
306/// The offset at which the account code commitment is stored relative to the start of the account
307/// data segment.
308pub const ACCT_CODE_COMMITMENT_OFFSET: MemoryOffset = 12;
309
310/// The memory address at which the account code commitment is stored in the native account.
311pub const NATIVE_ACCT_CODE_COMMITMENT_PTR: MemoryAddress =
312    NATIVE_ACCOUNT_DATA_PTR + ACCT_CODE_COMMITMENT_OFFSET;
313
314/// The offset at which the number of procedures contained in the account code is stored relative to
315/// the start of the account data segment.
316pub const ACCT_NUM_PROCEDURES_OFFSET: MemoryAddress = 28;
317
318/// The memory address at which the number of procedures contained in the account code is stored in
319/// the native account.
320pub const NATIVE_NUM_ACCT_PROCEDURES_PTR: MemoryAddress =
321    NATIVE_ACCOUNT_DATA_PTR + ACCT_NUM_PROCEDURES_OFFSET;
322
323/// The offset at which the account procedures section begins relative to the start of the account
324/// data segment.
325pub const ACCT_PROCEDURES_SECTION_OFFSET: MemoryAddress = 32;
326
327/// The memory address at which the account procedures section begins in the native account.
328pub const NATIVE_ACCT_PROCEDURES_SECTION_PTR: MemoryAddress =
329    NATIVE_ACCOUNT_DATA_PTR + ACCT_PROCEDURES_SECTION_OFFSET;
330
331/// The offset at which the account procedures call tracking section begins relative to the start of
332/// the account data segment.
333pub const ACCT_PROCEDURES_CALL_TRACKING_OFFSET: MemoryAddress = 1060;
334
335/// The memory address at which the account procedures call tracking section begins in the native
336/// account.
337pub const NATIVE_ACCT_PROCEDURES_CALL_TRACKING_PTR: MemoryAddress =
338    NATIVE_ACCOUNT_DATA_PTR + ACCT_PROCEDURES_CALL_TRACKING_OFFSET;
339
340/// The offset at which the number of storage slots contained in the account storage is stored
341/// relative to the start of the account data segment.
342pub const ACCT_NUM_STORAGE_SLOTS_OFFSET: MemoryAddress = 1316;
343
344/// The memory address at which number of storage slots contained in the account storage is stored
345/// in the native account.
346pub const NATIVE_NUM_ACCT_STORAGE_SLOTS_PTR: MemoryAddress =
347    NATIVE_ACCOUNT_DATA_PTR + ACCT_NUM_STORAGE_SLOTS_OFFSET;
348
349/// The number of elements that each storage slot takes up in memory.
350pub const ACCT_STORAGE_SLOT_NUM_ELEMENTS: u8 = 8;
351
352/// The offset of the slot type in the storage slot.
353pub const ACCT_STORAGE_SLOT_TYPE_OFFSET: u8 = 1;
354
355/// The offset of the slot's ID suffix in the storage slot.
356pub const ACCT_STORAGE_SLOT_ID_SUFFIX_OFFSET: u8 = 2;
357
358/// The offset of the slot's ID prefix in the storage slot.
359pub const ACCT_STORAGE_SLOT_ID_PREFIX_OFFSET: u8 = 3;
360
361/// The offset of the slot value in the storage slot.
362pub const ACCT_STORAGE_SLOT_VALUE_OFFSET: u8 = 4;
363
364/// The offset at which the account's active storage slots section begins relative to the start of
365/// the account data segment.
366///
367/// This section contains the current values of the account storage slots.
368pub const ACCT_ACTIVE_STORAGE_SLOTS_SECTION_OFFSET: MemoryAddress = 3360;
369
370/// The memory address at which the account's active storage slots section begins in the native
371/// account.
372pub const NATIVE_ACCT_STORAGE_SLOTS_SECTION_PTR: MemoryAddress =
373    NATIVE_ACCOUNT_DATA_PTR + ACCT_ACTIVE_STORAGE_SLOTS_SECTION_OFFSET;
374
375// UPGRADE COMMITMENTS
376// ------------------------------------------------------------------------------------------------
377
378/// The memory address at which the native account code upgrade commitment is stored.
379pub const CODE_UPGRADE_COMMITMENT_PTR: MemoryAddress = 532_744;
380
381/// The memory address at which the native account storage upgrade commitment is stored.
382pub const STORAGE_UPGRADE_COMMITMENT_PTR: MemoryAddress = 532_748;
383
384// NOTES DATA
385// ================================================================================================
386
387/// The size of the memory segment allocated to each note.
388pub const NOTE_MEM_SIZE: MemoryAddress = 1024;
389
390#[allow(clippy::empty_line_after_outer_attr)]
391#[rustfmt::skip]
392// INPUT NOTES DATA
393// ------------------------------------------------------------------------------------------------
394// Inputs note section contains data of all notes consumed by a transaction. The section starts at
395// memory offset 4_194_304 with a word containing the total number of input notes and is followed
396// by note nullifiers and note data like so:
397//
398// ┌──────────┬───────────┬───────────┬─────┬────────────────┬─────────┬──────────┬────────┬───────┬────────┐
399// │    NUM   │  NOTE 0   │  NOTE 1   │ ... │     NOTE n     │ PADDING │  NOTE 0  │ NOTE 1 │  ...  │ NOTE n │
400// │   NOTES  │ NULLIFIER │ NULLIFIER │     │    NULLIFIER   │         │   DATA   │  DATA  │       │  DATA  │
401// ├──────────┼───────────┼───────────┼─────┼────────────────┼─────────┼──────────┼────────┼───────┼────────┤
402// 4_194_304  4_194_308   4_194_312         4_194_304+4(n+1)           4_259_840  +1024    +2048   +1024n
403//
404// Here `n` represents number of input notes.
405//
406// Each nullifier occupies a single word. A data section for each note consists of exactly 1024
407// elements and is laid out like so:
408//
409// ┌──────────────┬────────┬────────┬────────────┬────────────┬──────────┬─────────────┬───────────┬──────┬
410// │ NOTE DETAILS │ SERIAL │ SCRIPT │  STORAGE   │   ASSETS   │ METADATA │ ATTACHMENTS │ RECIPIENT │ NOTE │
411// │  COMMITMENT  │  NUM   │  ROOT  │ COMMITMENT │ COMMITMENT │          │  COMMITMENT │           │  ID  │
412// ├──────────────┼────────┼────────┼────────────┼────────────┼──────────┼─────────────┼───────────┼──────┼
413// 0              4        8        12           16           20         24            28          32
414//
415// ┬───────┬─────────┬────────┬───────┬─────────┬─────┬────────┬─────────┬─────────┐
416// │ NOTE  │ STORAGE │  NUM   │ ASSET │  ASSET  │ ... │ ASSET  │  ASSET  │ PADDING │
417// │ ARGS  │ LENGTH  │ ASSETS │ KEY 0 │ VALUE 0 │     │ KEY n  │ VALUE n │         │
418// ┼───────┼─────────┼────────┼───────┼─────────┼─────┼────────┼─────────┼─────────┘
419// 36      40        44       48      52              48 + 8n  52 + 8n
420//
421// - NUM_STORAGE_ITEMS is encoded as [num_storage_items, 0, 0, 0].
422// - NUM_ASSETS is encoded as [num_assets, 0, 0, 0].
423// - NOTE ID is computed and cached by the transaction prologue; asset removals only mutate the
424//   assets region and do not invalidate it.
425// - STORAGE_COMMITMENT is the key to look up note storage in the advice map.
426// - ASSETS_COMMITMENT is the key to look up note assets in the advice map.
427//
428// Notice that note storage item are not loaded to the memory, only their length. In order to obtain
429// the storage values the advice map should be used: they are stored there as
430// `STORAGE_COMMITMENT -> STORAGE`.
431//
432// As opposed to the asset values, storage items are never used in kernel memory, so their presence
433// there is unnecessary.
434
435/// The memory address at which the input note section begins.
436pub const INPUT_NOTE_SECTION_PTR: MemoryAddress = 4_194_304;
437
438/// The memory address at which the nullifier section of the input notes begins.
439pub const INPUT_NOTE_NULLIFIER_SECTION_PTR: MemoryAddress = 4_194_308;
440
441/// The memory address at which the input note data section begins.
442pub const INPUT_NOTE_DATA_SECTION_OFFSET: MemoryAddress = 4_259_840;
443
444/// The memory address at which the number of input notes is stored.
445pub const NUM_INPUT_NOTES_PTR: MemoryAddress = INPUT_NOTE_SECTION_PTR;
446
447/// The offsets at which data of an input note is stored relative to the start of its data segment.
448pub const INPUT_NOTE_DETAILS_COMMITMENT_OFFSET: MemoryOffset = 0;
449pub const INPUT_NOTE_SERIAL_NUM_OFFSET: MemoryOffset = 4;
450pub const INPUT_NOTE_SCRIPT_ROOT_OFFSET: MemoryOffset = 8;
451pub const INPUT_NOTE_STORAGE_COMMITMENT_OFFSET: MemoryOffset = 12;
452pub const INPUT_NOTE_ASSETS_COMMITMENT_OFFSET: MemoryOffset = 16;
453pub const INPUT_NOTE_METADATA_OFFSET: MemoryOffset = 20;
454pub const INPUT_NOTE_ATTACHMENTS_COMMITMENT_OFFSET: MemoryOffset = 24;
455pub const INPUT_NOTE_RECIPIENT_OFFSET: MemoryOffset = 28;
456pub const INPUT_NOTE_ID_OFFSET: MemoryOffset = 32;
457pub const INPUT_NOTE_ARGS_OFFSET: MemoryOffset = 36;
458pub const INPUT_NOTE_NUM_STORAGE_ITEMS_OFFSET: MemoryOffset = 40;
459pub const INPUT_NOTE_NUM_ASSETS_OFFSET: MemoryOffset = 44;
460pub const INPUT_NOTE_ASSETS_OFFSET: MemoryOffset = 48;
461
462#[allow(clippy::empty_line_after_outer_attr)]
463#[rustfmt::skip]
464// OUTPUT NOTES DATA
465// ------------------------------------------------------------------------------------------------
466// Output notes section contains data of all notes produced by a transaction. The section starts at
467// memory offset 16_777_216 with each note data laid out one after another in 1024 elements chunks.
468//
469//     ┌─────────────┬─────────────┬───────────────┬─────────────┐
470//     │ NOTE 0 DATA │ NOTE 1 DATA │      ...      │ NOTE n DATA │
471//     └─────────────┴─────────────┴───────────────┴─────────────┘
472// 16_777_216      +1024         +2048           +1024n
473//
474// The total number of output notes for a transaction is stored in the bookkeeping section of the
475// memory. Data section of each note is laid out like so:
476//
477// | Offset  | Section                                                  |
478// | ------- | -------------------------------------------------------- |
479// | 0       | NOTE DETAILS COMMITMENT                                  |
480// | 4       | METADATA                                                 |
481// | 8       | RECIPIENT                                                |
482// | 12      | [dirty_flag, sealed_flag, 0, 0]                          |
483// | 16      | [num_assets, num_attachments, total_attachment_words, 0] |
484// | 20      | ATTACHMENT 0                                             |
485// | 24      | ATTACHMENT 1                                             |
486// | 28      | ATTACHMENT 2                                             |
487// | 32      | ATTACHMENT 3                                             |
488// | 36      | ASSETS COMMITMENT                                        |
489// | 40 + 8i | ASSET KEY i                                              |
490// | 44 + 8i | ASSET VALUE i                                            |
491// | 40 + 8n | PADDING                                                  |
492//
493// Offsets are measured in field elements, where n = num_assets and 0 <= i < n.
494//
495// The sealed flag starts at zero. Once set to one, asset and attachment mutations are rejected; it is
496// excluded from note commitments and serialization.
497//
498// The DIRTY_FLAG is the binary flag which specifies whether the assets commitment stored in this
499// note is outdated. It holds 1 if some changes were made to the note assets since the last
500// re-computation, and 0 otherwise.
501// It is set to 0 after every recomputation of the assets commitment in the
502// `miden::tx_kernel_core::note::compute_output_note_assets_commitment` procedure. It is set to 1 in the
503// `miden::tx_kernel_core::output_note::add_asset` procedure after any change was made to the assets data.
504
505/// The memory address at which the output notes section begins.
506pub const OUTPUT_NOTE_SECTION_OFFSET: MemoryOffset = 16_777_216;
507
508/// The offsets at which data of an output note is stored relative to the start of its data segment.
509pub const OUTPUT_NOTE_DETAILS_COMMITMENT_OFFSET: MemoryOffset = 0;
510pub const OUTPUT_NOTE_METADATA_OFFSET: MemoryOffset = 4;
511pub const OUTPUT_NOTE_RECIPIENT_OFFSET: MemoryOffset = 8;
512pub const OUTPUT_NOTE_DIRTY_FLAG_OFFSET: MemoryOffset = 12;
513pub const OUTPUT_NOTE_SEALED_FLAG_OFFSET: MemoryOffset = 13;
514pub const OUTPUT_NOTE_NUM_ASSETS_OFFSET: MemoryOffset = 16;
515pub const OUTPUT_NOTE_NUM_ATTACHMENTS_OFFSET: MemoryOffset = 17;
516pub const OUTPUT_NOTE_TOTAL_ATTACHMENT_WORDS_OFFSET: MemoryOffset = 18;
517pub const OUTPUT_NOTE_ATTACHMENT_0_OFFSET: MemoryOffset = 20;
518pub const OUTPUT_NOTE_ATTACHMENT_1_OFFSET: MemoryOffset = 24;
519pub const OUTPUT_NOTE_ATTACHMENT_2_OFFSET: MemoryOffset = 28;
520pub const OUTPUT_NOTE_ATTACHMENT_3_OFFSET: MemoryOffset = 32;
521pub const OUTPUT_NOTE_ASSETS_COMMITMENT_OFFSET: MemoryOffset = 36;
522pub const OUTPUT_NOTE_ASSETS_OFFSET: MemoryOffset = 40;
523
524// ASSETS
525// ------------------------------------------------------------------------------------------------
526
527/// The size of an asset's memory representation.
528#[cfg(any(feature = "testing", test))]
529pub const ASSET_SIZE: MemoryOffset = 8;
530
531/// The offset of the asset value in an asset's memory representation.
532#[cfg(any(feature = "testing", test))]
533pub const ASSET_VALUE_OFFSET: MemoryOffset = 4;
534
535// LINK MAP
536// ------------------------------------------------------------------------------------------------
537
538/// The inclusive start of the link map dynamic memory region.
539pub const LINK_MAP_REGION_START_PTR: MemoryAddress = 33_554_448;
540
541/// The non-inclusive end of the link map dynamic memory region.
542pub const LINK_MAP_REGION_END_PTR: MemoryAddress = 67_108_864;
543
544/// [`LINK_MAP_REGION_START_PTR`] + the currently used size stored at this pointer defines the next
545/// entry pointer that will be allocated.
546pub const LINK_MAP_USED_MEMORY_SIZE: MemoryAddress = 33_554_432;
547
548/// The size of each map entry, i.e. four words.
549pub const LINK_MAP_ENTRY_SIZE: MemoryOffset = 16;
550
551const _: () = assert!(
552    LINK_MAP_REGION_START_PTR.is_multiple_of(LINK_MAP_ENTRY_SIZE),
553    "link map region start ptr should be aligned to entry size"
554);
555
556const _: () = assert!(
557    (LINK_MAP_REGION_END_PTR - LINK_MAP_REGION_START_PTR).is_multiple_of(LINK_MAP_ENTRY_SIZE),
558    "the link map memory range should cleanly contain a multiple of the entry size"
559);