miden-base-sys 0.13.0

Miden rollup Rust bingings and MASM library
Documentation
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use miden_stdlib_sys::{Felt, Word};

use super::types::{AccountId, Asset, RawAccountId};

#[allow(improper_ctypes)]
unsafe extern "C" {
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_id"]
    fn extern_active_account_get_id(ptr: *mut RawAccountId);
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_nonce"]
    fn extern_active_account_get_nonce() -> Felt;
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_initial_commitment"]
    fn extern_active_account_get_initial_commitment(ptr: *mut Word);
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::compute_commitment"]
    fn extern_active_account_compute_commitment(ptr: *mut Word);
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_code_commitment"]
    fn extern_active_account_get_code_commitment(ptr: *mut Word);
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_initial_storage_commitment"]
    fn extern_active_account_get_initial_storage_commitment(ptr: *mut Word);
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::compute_storage_commitment"]
    fn extern_active_account_compute_storage_commitment(ptr: *mut Word);
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_asset"]
    fn extern_active_account_get_asset(
        asset_key_0: Felt,
        asset_key_1: Felt,
        asset_key_2: Felt,
        asset_key_3: Felt,
        ptr: *mut Word,
    );
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_initial_asset"]
    fn extern_active_account_get_initial_asset(
        asset_key_0: Felt,
        asset_key_1: Felt,
        asset_key_2: Felt,
        asset_key_3: Felt,
        ptr: *mut Word,
    );
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_balance"]
    fn extern_active_account_get_balance(
        asset_key_0: Felt,
        asset_key_1: Felt,
        asset_key_2: Felt,
        asset_key_3: Felt,
    ) -> Felt;
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_initial_balance"]
    fn extern_active_account_get_initial_balance(
        asset_key_0: Felt,
        asset_key_1: Felt,
        asset_key_2: Felt,
        asset_key_3: Felt,
    ) -> Felt;
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::has_non_fungible_asset"]
    fn extern_active_account_has_non_fungible_asset(
        asset_0: Felt,
        asset_1: Felt,
        asset_2: Felt,
        asset_3: Felt,
    ) -> Felt;
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_initial_vault_root"]
    fn extern_active_account_get_initial_vault_root(ptr: *mut Word);
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_vault_root"]
    fn extern_active_account_get_vault_root(ptr: *mut Word);
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_num_procedures"]
    fn extern_active_account_get_num_procedures() -> Felt;
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::get_procedure_root"]
    fn extern_active_account_get_procedure_root(index: Felt, ptr: *mut Word);
    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
    #[link_name = "miden::protocol::active_account::has_procedure"]
    fn extern_active_account_has_procedure(
        proc_root_0: Felt,
        proc_root_1: Felt,
        proc_root_2: Felt,
        proc_root_3: Felt,
    ) -> Felt;
}

/// Returns the account ID of the active account.
pub fn get_id() -> AccountId {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<RawAccountId>::uninit();
        extern_active_account_get_id(ret_area.as_mut_ptr());
        ret_area.assume_init().into_account_id()
    }
}

/// Returns the nonce of the active account.
#[inline]
pub fn get_nonce() -> Felt {
    unsafe { extern_active_account_get_nonce() }
}

/// Returns the active account commitment at the beginning of the transaction.
#[inline]
pub fn get_initial_commitment() -> Word {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
        extern_active_account_get_initial_commitment(ret_area.as_mut_ptr());
        ret_area.assume_init()
    }
}

/// Computes and returns the commitment of the current account data.
#[inline]
pub fn compute_commitment() -> Word {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
        extern_active_account_compute_commitment(ret_area.as_mut_ptr());
        ret_area.assume_init()
    }
}

/// Returns the code commitment of the active account.
#[inline]
pub fn get_code_commitment() -> Word {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
        extern_active_account_get_code_commitment(ret_area.as_mut_ptr());
        ret_area.assume_init()
    }
}

/// Returns the initial storage commitment of the active account.
#[inline]
pub fn get_initial_storage_commitment() -> Word {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
        extern_active_account_get_initial_storage_commitment(ret_area.as_mut_ptr());
        ret_area.assume_init()
    }
}

/// Computes the latest storage commitment of the active account.
#[inline]
pub fn compute_storage_commitment() -> Word {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
        extern_active_account_compute_storage_commitment(ret_area.as_mut_ptr());
        ret_area.assume_init()
    }
}

/// Returns the current value stored under the specified `asset_key` in the active account vault.
pub fn get_asset(asset_key: Word) -> Word {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
        extern_active_account_get_asset(
            asset_key[0],
            asset_key[1],
            asset_key[2],
            asset_key[3],
            ret_area.as_mut_ptr(),
        );
        ret_area.assume_init()
    }
}

/// Returns the initial value stored under the specified `asset_key` in the active account vault.
pub fn get_initial_asset(asset_key: Word) -> Word {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
        extern_active_account_get_initial_asset(
            asset_key[0],
            asset_key[1],
            asset_key[2],
            asset_key[3],
            ret_area.as_mut_ptr(),
        );
        ret_area.assume_init()
    }
}

/// Returns the balance of the fungible asset identified by `asset_key`.
///
/// # Panics
///
/// Propagates kernel errors if the referenced asset is non-fungible or the
/// account vault invariants are violated.
pub fn get_balance(asset_key: Word) -> Felt {
    unsafe {
        extern_active_account_get_balance(asset_key[0], asset_key[1], asset_key[2], asset_key[3])
    }
}

/// Returns the initial balance of the fungible asset identified by `asset_key`.
#[inline]
pub fn get_initial_balance(asset_key: Word) -> Felt {
    unsafe {
        extern_active_account_get_initial_balance(
            asset_key[0],
            asset_key[1],
            asset_key[2],
            asset_key[3],
        )
    }
}

/// Returns `true` if the active account vault currently contains the specified non-fungible asset.
#[inline]
pub fn has_non_fungible_asset(asset: Asset) -> bool {
    unsafe {
        extern_active_account_has_non_fungible_asset(
            asset.key[0],
            asset.key[1],
            asset.key[2],
            asset.key[3],
        ) != Felt::new(0).unwrap()
    }
}

/// Returns the vault root of the active account at the beginning of the transaction.
#[inline]
pub fn get_initial_vault_root() -> Word {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
        extern_active_account_get_initial_vault_root(ret_area.as_mut_ptr());
        ret_area.assume_init()
    }
}

/// Returns the current vault root of the active account.
#[inline]
pub fn get_vault_root() -> Word {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
        extern_active_account_get_vault_root(ret_area.as_mut_ptr());
        ret_area.assume_init()
    }
}

/// Returns the number of procedures exported by the active account.
#[inline]
pub fn get_num_procedures() -> Felt {
    unsafe { extern_active_account_get_num_procedures() }
}

/// Returns the procedure root for the procedure at `index`.
#[inline]
pub fn get_procedure_root(index: u8) -> Word {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
        extern_active_account_get_procedure_root(
            Felt::new(index as u64).unwrap(),
            ret_area.as_mut_ptr(),
        );
        ret_area.assume_init()
    }
}

/// Returns `true` if the procedure identified by `proc_root` exists on the active account.
#[inline]
pub fn has_procedure(proc_root: Word) -> bool {
    unsafe {
        extern_active_account_has_procedure(proc_root[0], proc_root[1], proc_root[2], proc_root[3])
            != Felt::new(0).unwrap()
    }
}

/// Trait that provides active account operations for components.
///
/// This trait is automatically implemented for the storage struct marked with the
/// `#[component_storage]` macro.
///
/// NOTE: when adding methods, also update `ACTIVE_ACCOUNT_METHODS` in
/// `sdk/base-macros/src/fpi.rs`, which rejects dependency functions that would shadow them.
pub trait ActiveAccount {
    /// Guard hook invoked by every active-account operation before it runs.
    ///
    /// The default implementation is a no-op. Types that can also represent a *foreign* account
    /// (for example the struct generated by the `#[account(...)]` macro) override this to reject
    /// calls made on a foreign binding, because the active-account operations always target the
    /// transaction's active account rather than the foreign one.
    #[doc(hidden)]
    #[inline]
    fn __assert_active_account(&self) {}

    /// Returns the account ID of the active account.
    #[inline]
    fn get_id(&self) -> AccountId {
        self.__assert_active_account();
        get_id()
    }

    /// Returns the nonce of the active account.
    #[inline]
    fn get_nonce(&self) -> Felt {
        self.__assert_active_account();
        get_nonce()
    }

    /// Returns the active account commitment at the beginning of the transaction.
    #[inline]
    fn get_initial_commitment(&self) -> Word {
        self.__assert_active_account();
        get_initial_commitment()
    }

    /// Computes and returns the commitment of the current account data.
    #[inline]
    fn compute_commitment(&self) -> Word {
        self.__assert_active_account();
        compute_commitment()
    }

    /// Returns the code commitment of the active account.
    #[inline]
    fn get_code_commitment(&self) -> Word {
        self.__assert_active_account();
        get_code_commitment()
    }

    /// Returns the initial storage commitment of the active account.
    #[inline]
    fn get_initial_storage_commitment(&self) -> Word {
        self.__assert_active_account();
        get_initial_storage_commitment()
    }

    /// Computes the latest storage commitment of the active account.
    #[inline]
    fn compute_storage_commitment(&self) -> Word {
        self.__assert_active_account();
        compute_storage_commitment()
    }

    /// Returns the current value stored under the specified `asset_key` in the active account
    /// vault.
    #[inline]
    fn get_asset(&self, asset_key: Word) -> Word {
        self.__assert_active_account();
        get_asset(asset_key)
    }

    /// Returns the initial value stored under the specified `asset_key` in the active account
    /// vault.
    #[inline]
    fn get_initial_asset(&self, asset_key: Word) -> Word {
        self.__assert_active_account();
        get_initial_asset(asset_key)
    }

    /// Returns the balance of the fungible asset identified by `asset_key`.
    ///
    /// # Panics
    ///
    /// Propagates kernel errors if the referenced asset is non-fungible or the
    /// account vault invariants are violated.
    #[inline]
    fn get_balance(&self, asset_key: Word) -> Felt {
        self.__assert_active_account();
        get_balance(asset_key)
    }

    /// Returns the initial balance of the fungible asset identified by `asset_key`.
    #[inline]
    fn get_initial_balance(&self, asset_key: Word) -> Felt {
        self.__assert_active_account();
        get_initial_balance(asset_key)
    }

    /// Returns `true` if the active account vault currently contains the specified non-fungible asset.
    #[inline]
    fn has_non_fungible_asset(&self, asset: Asset) -> bool {
        self.__assert_active_account();
        has_non_fungible_asset(asset)
    }

    /// Returns the vault root of the active account at the beginning of the transaction.
    #[inline]
    fn get_initial_vault_root(&self) -> Word {
        self.__assert_active_account();
        get_initial_vault_root()
    }

    /// Returns the current vault root of the active account.
    #[inline]
    fn get_vault_root(&self) -> Word {
        self.__assert_active_account();
        get_vault_root()
    }

    /// Returns the number of procedures exported by the active account.
    #[inline]
    fn get_num_procedures(&self) -> Felt {
        self.__assert_active_account();
        get_num_procedures()
    }

    /// Returns the procedure root for the procedure at `index`.
    #[inline]
    fn get_procedure_root(&self, index: u8) -> Word {
        self.__assert_active_account();
        get_procedure_root(index)
    }

    /// Returns `true` if the procedure identified by `proc_root` exists on the active account.
    #[inline]
    fn has_procedure(&self, proc_root: Word) -> bool {
        self.__assert_active_account();
        has_procedure(proc_root)
    }
}

/// Marker trait for account API wrapper types.
///
/// The `#[note]` and `#[tx_script]` macros instantiate their entrypoint account parameter
/// through this trait, so that parameter must be a type implementing it. The `ActiveAccount`
/// supertrait guarantees that parameter is usable as the transaction's active account and keeps
/// unrelated `Default` types from satisfying the bound. The `#[account(...)]` macro implements
/// both automatically; it is not a sealed capability boundary, so manual implementations are
/// possible but normally unnecessary.
#[diagnostic::on_unimplemented(
    message = "`{Self}` is not an account wrapper generated by `#[account(...)]`",
    note = "define a struct with `#[account(...)]` and use it as the entrypoint account parameter"
)]
pub trait AccountWrapper: ActiveAccount + Default {
    /// Creates a binding to the transaction's active account.
    ///
    /// This is the account the transaction executes against, as opposed to a foreign account
    /// reached through FPI (created with `new`).
    #[inline(always)]
    fn active() -> Self {
        Self::default()
    }
}