miden_base_sys/bindings/
types.rs

1extern crate alloc;
2
3use alloc::vec::Vec;
4
5use miden_stdlib_sys::{Digest, Felt, Word, felt, hash_elements, intrinsics::crypto::merge};
6
7#[allow(unused)]
8#[derive(Copy, Clone, Debug, PartialEq, Eq)]
9pub struct AccountId {
10    pub prefix: Felt,
11    pub suffix: Felt,
12}
13
14impl AccountId {
15    /// Creates a new AccountId from prefix and suffix Felt values
16    pub fn from(prefix: Felt, suffix: Felt) -> Self {
17        Self { prefix, suffix }
18    }
19}
20
21#[derive(Copy, Clone, Debug, PartialEq, Eq)]
22#[repr(transparent)]
23pub struct Asset {
24    pub inner: Word,
25}
26
27impl Asset {
28    pub fn new(word: impl Into<Word>) -> Self {
29        Asset { inner: word.into() }
30    }
31
32    pub fn as_word(&self) -> &Word {
33        &self.inner
34    }
35
36    #[inline]
37    pub(crate) fn reverse(&self) -> Self {
38        Self {
39            inner: self.inner.reverse(),
40        }
41    }
42}
43
44impl From<Word> for Asset {
45    fn from(value: Word) -> Self {
46        Self::new(value)
47    }
48}
49
50impl From<[Felt; 4]> for Asset {
51    fn from(value: [Felt; 4]) -> Self {
52        Asset::new(Word::from(value))
53    }
54}
55
56impl From<Asset> for Word {
57    fn from(val: Asset) -> Self {
58        val.inner
59    }
60}
61
62impl AsRef<Word> for Asset {
63    fn as_ref(&self) -> &Word {
64        &self.inner
65    }
66}
67
68#[derive(Clone, Debug, PartialEq, Eq)]
69#[repr(transparent)]
70pub struct Recipient {
71    pub inner: Word,
72}
73
74impl Recipient {
75    /// Computes a recipient digest from the provided components.
76    ///
77    /// The `padded_inputs` must be padded with ZEROs to the next multiple of 8 (i.e. 2-word
78    /// aligned). For example, to pass two inputs `a` and `b`, use:
79    /// `vec![a, b, felt!(0), felt!(0), felt!(0), felt!(0), felt!(0), felt!(0)]`.
80    ///
81    /// # Panics
82    /// Panics if `padded_inputs.len()` is not a multiple of 8.
83    pub fn compute(serial_num: Word, script_digest: Digest, padded_inputs: Vec<Felt>) -> Self {
84        assert!(
85            padded_inputs.len().is_multiple_of(8),
86            "`padded_inputs` length must be a multiple of 8"
87        );
88
89        let empty_word = Word::new([felt!(0), felt!(0), felt!(0), felt!(0)]);
90
91        let serial_num_hash = merge([Digest::from_word(serial_num), Digest::from_word(empty_word)]);
92        let merge_script = merge([serial_num_hash, script_digest]);
93        let digest: Word = merge([merge_script, hash_elements(padded_inputs)]).into();
94
95        Self { inner: digest }
96    }
97}
98
99impl From<[Felt; 4]> for Recipient {
100    fn from(value: [Felt; 4]) -> Self {
101        Recipient {
102            inner: Word::from(value),
103        }
104    }
105}
106
107impl From<Word> for Recipient {
108    fn from(value: Word) -> Self {
109        Recipient { inner: value }
110    }
111}
112
113#[derive(Clone, Copy, Debug, PartialEq, Eq)]
114#[repr(transparent)]
115pub struct Tag {
116    pub inner: Felt,
117}
118
119impl From<Felt> for Tag {
120    fn from(value: Felt) -> Self {
121        Tag { inner: value }
122    }
123}
124
125#[derive(Clone, Copy, Debug, PartialEq, Eq)]
126#[repr(transparent)]
127pub struct NoteIdx {
128    pub inner: Felt,
129}
130
131#[derive(Clone, Copy, Debug, PartialEq, Eq)]
132#[repr(transparent)]
133pub struct NoteType {
134    pub inner: Felt,
135}
136
137impl From<Felt> for NoteType {
138    fn from(value: Felt) -> Self {
139        NoteType { inner: value }
140    }
141}
142
143#[derive(Clone, Debug, PartialEq, Eq)]
144#[repr(transparent)]
145pub struct StorageCommitmentRoot(Word);
146
147impl StorageCommitmentRoot {
148    #[inline]
149    pub(crate) fn reverse(&self) -> StorageCommitmentRoot {
150        Self(self.0.reverse())
151    }
152}