1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
::
# ERRORS
# =================================================================================================
const ERR_VAULT_ASSET_METADATA_NOT_U32 =
const ERR_VAULT_ASSET_METADATA_UNKNOWN_VERSION =
const ERR_VAULT_ASSET_METADATA_NON_ZERO_RESERVED_BITS =
const ERR_VAULT_ASSET_METADATA_UNKNOWN_COMPOSITION =
# CONSTANTS
# =================================================================================================
# Specifies the maximum amount a fungible asset can represent.
#
# This is 2^63 - 2^31. See account_update.masm for more details.
pub const FUNGIBLE_ASSET_MAX_AMOUNT =
# The number of elements in an asset, i.e. asset ID and value.
pub const ASSET_SIZE =
# The offset of the asset value in an asset stored in memory.
pub const ASSET_VALUE_MEMORY_OFFSET =
# Version 1 of the asset ID encoding.
const ASSET_VERSION_1 =
# The mask for the version bits in the asset metadata.
const VERSION_MASK = # 0b1111
# The number of bits by which the composition is shifted in the asset metadata.
const COMPOSITION_SHIFT =
#! The mask for the composition bits in the asset metadata.
const COMPOSITION_MASK = # 0b0011_0000
# The flag representing the AssetComposition::None composition.
pub const COMPOSITION_NONE =
# The flag representing the AssetComposition::Fungible composition.
pub const COMPOSITION_FUNGIBLE =
# The flag representing the AssetComposition::Custom composition.
pub const COMPOSITION_CUSTOM =
#! The composition value that is not valid.
const COMPOSITION_INVALID =
#! The u32 mask for the reserved bits in the asset metadata.
:
# PROCEDURES
# =================================================================================================
#! Stores an asset ID and value into memory at the given pointer.
#!
#! The memory range pointer..pointer+8 will be overwritten.
#!
#! Inputs: [ptr, ASSET_ID, ASSET_VALUE]
#! Outputs: []
#!
#! Where:
#! - ptr is the memory address where the asset will be stored.
#! - ASSET_ID is the 4-element word representing the asset ID.
#! - ASSET_VALUE is the 4-element word representing the asset value.
pub proc store(: end
#! Loads an asset ID and value from memory given a pointer to the asset.
#!
#! Inputs: [ptr]
#! Outputs: [ASSET_ID, ASSET_VALUE]
#!
#! Where:
#! - ptr is the memory address of the asset.
#! - ASSET_ID is the 4-element word representing the asset ID.
#! - ASSET_VALUE is the 4-element word representing the asset value.
pub proc load(: end
#! Returns the faucet ID from an asset ID.
#!
#! WARNING: The faucet ID is not validated.
#!
#! Inputs: [ASSET_ID]
#! Outputs: [faucet_id_suffix, faucet_id_prefix, ASSET_ID]
#!
#! Where:
#! - faucet_id is the account ID in the asset ID.
#! - ASSET_ID is the asset ID from which to extract the faucet ID.
pub proc id_to_faucet_id(: end
#! Returns the faucet ID from an asset ID and consumes it.
#!
#! WARNING: The faucet ID is not validated.
#!
#! Inputs: [ASSET_ID]
#! Outputs: [faucet_id_suffix, faucet_id_prefix]
#!
#! Where:
#! - faucet_id is the account ID in the asset ID.
#! - ASSET_ID is the asset ID from which to extract the faucet ID.
pub proc id_into_faucet_id(: end
#! Returns the asset class from an asset ID.
#!
#! Inputs: [ASSET_ID]
#! Outputs: [asset_class_suffix, asset_class_prefix, ASSET_ID]
#!
#! Where:
#! - asset_class is the asset class in the asset ID.
#! - ASSET_ID is the asset ID from which to extract the asset class.
pub proc id_to_asset_class(: end
#! Returns the asset class from an asset ID and consumes it.
#!
#! Inputs: [ASSET_ID]
#! Outputs: [asset_class_suffix, asset_class_prefix]
#!
#! Where:
#! - asset_class is the asset class in the asset ID.
#! - ASSET_ID is the asset ID from which to extract the asset class.
pub proc id_into_asset_class(: end
#! Splits the merged faucet ID suffix and the asset metadata.
#!
#! Inputs: [faucet_id_suffix_and_metadata]
#! Outputs: [asset_metadata, faucet_id_suffix]
#!
#! Where:
#! - faucet_id_suffix_and_metadata is the faucet ID suffix merged with the asset metadata.
#! - faucet_id_suffix is the suffix of the account ID.
#! - asset_metadata is the asset metadata.
pub proc split_suffix_and_metadata(: end
#! Validates that asset metadata is well formed and consumes it.
#!
#! WARNING: This procedure should not be exposed to or called from user code (e.g. miden::protocol
#! or miden::standards) as this would make the calling code only accept asset version 1.
#!
#! Inputs: [asset_metadata]
#! Outputs: []
#!
#! Panics if:
#! - asset_metadata is not a valid u32
#! - encodes an unknown asset ID version.
#! - has reserved bits 6 or 7 set.
#! - encodes an unknown asset composition.
pub proc validate_metadata(: end
#! Extracts the asset composition from asset metadata.
#!
#! WARNING: asset_metadata is assumed to be a byte (in particular a valid u32)
#!
#! Inputs: [asset_metadata]
#! Outputs: [asset_composition]
#!
#! Where:
#! - asset_metadata is the asset metadata.
#! - asset_composition is the composition value (see COMPOSITION_* constants).
proc metadata_into_composition(: end
#! Returns the asset composition from an asset ID.
#!
#! Inputs: [ASSET_ID]
#! Outputs: [asset_composition, ASSET_ID]
#!
#! Where:
#! - ASSET_ID is the asset ID from which to extract the composition.
#! - asset_composition is the composition value (see COMPOSITION_* constants).
pub proc id_to_composition(: end
#! Returns the asset composition from an asset ID and consumes it.
#!
#! Inputs: [ASSET_ID]
#! Outputs: [asset_composition]
#!
#! Where:
#! - ASSET_ID is the asset ID from which to extract the composition.
#! - asset_composition is the composition value (see COMPOSITION_* constants).
pub proc id_into_composition(: end
#! Returns the balance of the given fungible asset and consumes it.
#!
#! WARNING: Assumes that the given asset value is fungible and does NOT validate it.
#!
#! Inputs: [ASSET_VALUE]
#! Outputs: [balance]
#!
#! Where:
#! - ASSET_VALUE is the fungible asset from which to extract the balance.
#! - balance is the amount of the fungible asset.
pub proc fungible_value_into_amount_unchecked(: end