Skip to content

Commit 027d92e

Browse files
authored
docs: add MASM named storage slot examples and word() syntax docs (#158)
1 parent a4337d7 commit 027d92e

File tree

3 files changed

+94
-11
lines changed

3 files changed

+94
-11
lines changed

versioned_docs/version-0.13/builder/migration/02-account-changes.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,37 @@ Replace index-based storage access with `StorageSlotName` identifiers.
4242
+ account.storage_mut().set_slot(&slot_name, new_value)?;
4343
```
4444

45-
### MASM Updates
45+
### MASM
46+
47+
In MASM, use the `word("...")` syntax to define named storage slot constants. The `word()` function hashes the slot name to produce a `Word`, and you extract the slot ID using `[0..2]` slice notation:
48+
49+
```diff title="src/contract.masm"
50+
- # Before (v0.12): index-based storage access
51+
- use.miden::account
52+
- const.MY_SLOT=0
53+
- push.MY_SLOT
54+
- exec.account::get_item
55+
56+
+ # After (v0.13): name-based storage access
57+
+ use miden::protocol::active_account
58+
+ const MY_SLOT = word("my_project::my_component::my_slot")
59+
+ push.MY_SLOT[0..2]
60+
+ exec.active_account::get_item
61+
+ # => [VALUE]
62+
```
63+
64+
Writing to storage follows the same pattern:
65+
66+
```masm title="src/contract.masm"
67+
use miden::protocol::native_account
68+
69+
const MY_SLOT = word("my_project::my_component::my_slot")
70+
71+
# Set a value in the slot
72+
push.MY_SLOT[0..2]
73+
exec.native_account::set_item
74+
# => [OLD_VALUE]
75+
```
4676

4777
The `set_map_item` procedure now takes slot IDs and returns only old values:
4878

@@ -52,14 +82,16 @@ The `set_map_item` procedure now takes slot IDs and returns only old values:
5282
- # Returns: [OLD_MAP_ROOT, OLD_VALUE, ...]
5383

5484
+ # After
55-
+ exec.account::set_map_item
85+
+ exec.native_account::set_map_item
5686
+ # Returns: [OLD_VALUE, ...]
5787
```
5888

5989
:::info Good to know
6090
The return value change means you may need to update stack manipulation after calling `set_map_item`.
6191
:::
6292

93+
For details on the `word("...")` syntax, see the [MASM Changes](06-masm-changes.md#named-storage-slots) migration page. For the full list of storage procedures, see the [Protocol Library Reference](../../design/miden-base/protocol_library.md).
94+
6395
---
6496

6597
## Keystore Changes

versioned_docs/version-0.13/builder/migration/06-masm-changes.md

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,37 @@ sed -i 's/use\./use /g' *.masm
8080

8181
---
8282

83+
## Named Storage Slots
84+
85+
:::warning Breaking Change
86+
Storage access is now name-based, not index-based. Use `word("...")` to derive slot IDs from names.
87+
:::
88+
89+
In v0.13, account storage slots are identified by name rather than numeric index. In MASM, the `word("...")` syntax computes a deterministic slot ID from a slot name string:
90+
91+
```diff title="src/contract.masm"
92+
- # Before (v0.12): index-based storage access
93+
- const.BALANCE_SLOT=0
94+
- push.BALANCE_SLOT
95+
- exec.account::get_item
96+
97+
+ # After (v0.13): name-based storage access
98+
+ use miden::protocol::active_account
99+
+ const BALANCE_SLOT = word("my_project::my_component::balance")
100+
+ push.BALANCE_SLOT[0..2]
101+
+ exec.active_account::get_item
102+
```
103+
104+
Key changes:
105+
- Define slot constants using `word("slot_name")` instead of integer indices
106+
- The `word()` function hashes the name string to produce a `Word` (4 field elements)
107+
- Use `[0..2]` slice notation to extract the slot ID (first 2 elements) for kernel procedures like `get_item` and `set_item`
108+
- Slot names should follow the `project::component::slot` naming convention to avoid collisions
109+
110+
For more details on storage slot naming conventions, see [Account Storage](../../design/miden-base/account/storage.md). For the full list of storage procedures, see the [Protocol Library Reference](../../design/miden-base/protocol_library.md).
111+
112+
---
113+
83114
## Cryptography Updates
84115

85116
### Falcon Signature Rename
@@ -136,18 +167,18 @@ end
136167

137168
After:
138169
```masm title="src/contract.masm (after)"
139-
use miden::account
140-
use miden::core::crypto::hashes::rpo
170+
use miden::protocol::active_account
171+
use miden::core::crypto::hashes::rpo256
141172
142-
const BALANCE_SLOT=0
173+
const BALANCE_SLOT = word("my_project::my_component::balance")
143174
144175
export get_balance
145-
push.BALANCE_SLOT
146-
exec.account::get_item
176+
push.BALANCE_SLOT[0..2]
177+
exec.active_account::get_item
147178
end
148179
149180
export transfer
150-
exec.rpo::hash_words
181+
exec.rpo256::hash_words
151182
# ... rest of procedure
152183
end
153184
```
@@ -162,9 +193,11 @@ end
162193
4. Replace `use.` with `use ` (space instead of dot)
163194
5. Replace `export.<path>` re-exports with `pub use <path>`
164195
6. Update `std::` namespace to `miden::core::`
165-
7. Rename `RpoFalcon512` to `Falcon512Rpo`
166-
8. Update ECDSA procedure paths
167-
9. Rename `hash_memory_words` to `hash_words`
196+
7. Replace numeric storage slot constants with `word("...")` named slots
197+
8. Update `get_item`/`set_item` calls to use `[0..2]` slice for slot IDs
198+
9. Rename `RpoFalcon512` to `Falcon512Rpo`
199+
10. Update ECDSA procedure paths
200+
11. Rename `hash_memory_words` to `hash_words`
168201

169202
---
170203

@@ -176,3 +209,4 @@ end
176209
| `module 'std' not found` | Namespace changed | Use `miden::core::` |
177210
| `procedure 'auth_tx_rpo_falcon512' not found` | Renamed | Use `auth_tx_falcon512_rpo` |
178211
| `procedure 'hash_memory_words' not found` | Renamed | Use `hash_words` |
212+
| `get_item` returns unexpected values | Using integer index instead of slot ID | Use `word("...")` and `[0..2]` slice |

versioned_docs/version-0.13/design/miden-vm/user_docs/assembly/code_organization.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,23 @@ begin
270270
end
271271
```
272272

273+
#### Word constants from strings
274+
275+
A word constant can also be derived from a string using the `word("...")` syntax. This computes a deterministic `Word` by hashing the provided string with Blake3 and mapping the result to four field elements:
276+
277+
```
278+
const MY_IDENTIFIER = word("my_project::my_module::my_name")
279+
280+
begin
281+
push.MY_IDENTIFIER # pushes all 4 elements of the derived word
282+
push.MY_IDENTIFIER[0..2] # pushes only the first 2 elements (slice notation)
283+
end
284+
```
285+
286+
The `word("...")` constructor can be combined with [slice notation](#constant-slices) just like array-based word constants. This is commonly used for named storage slot IDs in Miden account programs.
287+
288+
Similarly, the `event("...")` syntax derives a single field element from a string, for use with `emit` instructions.
289+
273290
### Types
274291

275292
Miden Assembly supports types for the purpose of specifying the _type signature_ of a procedure. This is used by other tooling in the Miden toolchain to bind against procedures written in Miden Assembly from higher-level languages, e.g. Rust. The type system is low-level and structural, but some conveniences are provided in Miden Assembly to improve ergonomics and aid in the construction of future static analyses.

0 commit comments

Comments
 (0)