miden_objects/note/
execution_hint.rs

1// NOTE EXECUTION HINT
2// ================================================================================================
3
4use crate::block::BlockNumber;
5use crate::{Felt, NoteError};
6
7/// Specifies the conditions under which a note is ready to be consumed.
8/// These conditions are meant to be encoded in the note script as well.
9///
10/// This struct can be represented as the combination of a tag, and a payload.
11/// The tag specifies the variant of the hint, and the payload encodes the hint data.
12///
13/// # Felt layout
14///
15/// [`NoteExecutionHint`] can be encoded into a [`Felt`] with the following layout:
16///
17/// ```text
18/// [26 zero bits | payload (32 bits) | tag (6 bits)]
19/// ```
20///
21/// This way, hints such as [NoteExecutionHint::Always], are represented by `Felt::new(1)`.
22#[derive(Clone, Copy, Debug, Eq, PartialEq)]
23pub enum NoteExecutionHint {
24    /// Unspecified note execution hint. Implies it is not known under which conditions the note
25    /// is consumable.
26    None,
27    /// The note's script can be executed at any time.
28    Always,
29    /// The note's script can be executed after the specified block number.
30    ///
31    /// The block number cannot be [`u32::MAX`] which is enforced through the [`AfterBlockNumber`]
32    /// type.
33    AfterBlock { block_num: AfterBlockNumber },
34    /// The note's script can be executed in the specified slot within the specified round.
35    ///
36    /// The slot is defined as follows:
37    /// - First we define the length of the round in powers of 2. For example, round_len = 10 is a
38    ///   round of 1024 blocks.
39    /// - Then we define the length of a slot within the round also using powers of 2. For example,
40    ///   slot_len = 7 is a slot of 128 blocks.
41    /// - Lastly, the offset specifies the index of the slot within the round - i.e., 0 is the first
42    ///   slot, 1 is the second slot etc.
43    ///
44    /// For example: { round_len: 10, slot_len: 7, slot_offset: 1 } means that the note can
45    /// be executed in any second 128 block slot of a 1024 block round. These would be blocks
46    /// 128..255, 1152..1279, 2176..2303 etc.
47    OnBlockSlot {
48        round_len: u8,
49        slot_len: u8,
50        slot_offset: u8,
51    },
52}
53
54impl NoteExecutionHint {
55    // CONSTANTS
56    // ------------------------------------------------------------------------------------------------
57
58    pub(crate) const NONE_TAG: u8 = 0;
59    pub(crate) const ALWAYS_TAG: u8 = 1;
60    pub(crate) const AFTER_BLOCK_TAG: u8 = 2;
61    pub(crate) const ON_BLOCK_SLOT_TAG: u8 = 3;
62
63    // CONSTRUCTORS
64    // ------------------------------------------------------------------------------------------------
65
66    /// Creates a [NoteExecutionHint::None] variant
67    pub fn none() -> Self {
68        NoteExecutionHint::None
69    }
70
71    /// Creates a [NoteExecutionHint::Always] variant
72    pub fn always() -> Self {
73        NoteExecutionHint::Always
74    }
75
76    /// Creates a [NoteExecutionHint::AfterBlock] variant based on the given `block_num`
77    ///
78    /// # Errors
79    ///
80    /// Returns an error if `block_num` is equal to [`u32::MAX`].
81    pub fn after_block(block_num: BlockNumber) -> Result<Self, NoteError> {
82        AfterBlockNumber::new(block_num)
83            .map(|block_number| NoteExecutionHint::AfterBlock { block_num: block_number })
84    }
85
86    /// Creates a [NoteExecutionHint::OnBlockSlot] for the given parameters. See the variants
87    /// documentation for details on the parameters.
88    pub fn on_block_slot(round_len: u8, slot_len: u8, slot_offset: u8) -> Self {
89        NoteExecutionHint::OnBlockSlot { round_len, slot_len, slot_offset }
90    }
91
92    pub fn from_parts(tag: u8, payload: u32) -> Result<NoteExecutionHint, NoteError> {
93        match tag {
94            Self::NONE_TAG => {
95                if payload != 0 {
96                    return Err(NoteError::InvalidNoteExecutionHintPayload(tag, payload));
97                }
98                Ok(NoteExecutionHint::None)
99            },
100            Self::ALWAYS_TAG => {
101                if payload != 0 {
102                    return Err(NoteError::InvalidNoteExecutionHintPayload(tag, payload));
103                }
104                Ok(NoteExecutionHint::Always)
105            },
106            Self::AFTER_BLOCK_TAG => NoteExecutionHint::after_block(payload.into()),
107            Self::ON_BLOCK_SLOT_TAG => {
108                let remainder = ((payload >> 24) & 0xff) as u8;
109                if remainder != 0 {
110                    return Err(NoteError::InvalidNoteExecutionHintPayload(tag, payload));
111                }
112
113                let round_len = ((payload >> 16) & 0xff) as u8;
114                let slot_len = ((payload >> 8) & 0xff) as u8;
115                let slot_offset = (payload & 0xff) as u8;
116                let hint = NoteExecutionHint::OnBlockSlot { round_len, slot_len, slot_offset };
117
118                Ok(hint)
119            },
120            _ => Err(NoteError::NoteExecutionHintTagOutOfRange(tag)),
121        }
122    }
123
124    /// Returns whether the note execution conditions validate for the given `block_num`
125    ///
126    /// # Returns
127    /// - `None` if we don't know whether the note can be consumed.
128    /// - `Some(true)` if the note is consumable for the given `block_num`
129    /// - `Some(false)` if the note is not consumable for the given `block_num`
130    pub fn can_be_consumed(&self, block_num: BlockNumber) -> Option<bool> {
131        let block_num = block_num.as_u32();
132        match self {
133            NoteExecutionHint::None => None,
134            NoteExecutionHint::Always => Some(true),
135            NoteExecutionHint::AfterBlock { block_num: hint_block_num } => {
136                Some(block_num >= hint_block_num.as_u32())
137            },
138            NoteExecutionHint::OnBlockSlot { round_len, slot_len, slot_offset } => {
139                let round_len_blocks: u32 = 1 << round_len;
140                let slot_len_blocks: u32 = 1 << slot_len;
141
142                let block_round_index = block_num / round_len_blocks;
143
144                let slot_start_block =
145                    block_round_index * round_len_blocks + (*slot_offset as u32) * slot_len_blocks;
146                let slot_end_block = slot_start_block + slot_len_blocks;
147
148                let can_be_consumed = block_num >= slot_start_block && block_num < slot_end_block;
149                Some(can_be_consumed)
150            },
151        }
152    }
153
154    /// Encodes the [`NoteExecutionHint`] into a 6-bit tag and a 32-bit payload.
155    ///
156    /// # Guarantees
157    ///
158    /// Since the tag has at most 6 bits, the returned byte is guaranteed to have its two most
159    /// significant bits set to `0`.
160    ///
161    /// The payload is guaranteed to contain at least one `0` bit to make encoding it into
162    /// [`NoteMetadata`](crate::note::NoteMetadata) safely possible.
163    pub fn into_parts(&self) -> (u8, u32) {
164        match self {
165            NoteExecutionHint::None => (Self::NONE_TAG, 0),
166            NoteExecutionHint::Always => (Self::ALWAYS_TAG, 0),
167            NoteExecutionHint::AfterBlock { block_num } => {
168                (Self::AFTER_BLOCK_TAG, block_num.as_u32())
169            },
170            NoteExecutionHint::OnBlockSlot { round_len, slot_len, slot_offset } => {
171                let payload: u32 =
172                    ((*round_len as u32) << 16) | ((*slot_len as u32) << 8) | (*slot_offset as u32);
173                (Self::ON_BLOCK_SLOT_TAG, payload)
174            },
175        }
176    }
177}
178
179/// Converts a [`NoteExecutionHint`] into a [`Felt`] with the layout documented on the type.
180impl From<NoteExecutionHint> for Felt {
181    fn from(value: NoteExecutionHint) -> Self {
182        let int_representation: u64 = value.into();
183        Felt::new(int_representation)
184    }
185}
186
187/// Tries to convert a `u64` into a [`NoteExecutionHint`] with the expected layout documented on the
188/// type.
189///
190/// Note: The upper 26 bits are not enforced to be zero.
191impl TryFrom<u64> for NoteExecutionHint {
192    type Error = NoteError;
193    fn try_from(value: u64) -> Result<Self, Self::Error> {
194        let tag = (value & 0b111111) as u8;
195        let payload = ((value >> 6) & 0xffffffff) as u32;
196
197        Self::from_parts(tag, payload)
198    }
199}
200
201/// Converts a [`NoteExecutionHint`] into a `u64` with the layout documented on the type.
202impl From<NoteExecutionHint> for u64 {
203    fn from(value: NoteExecutionHint) -> Self {
204        let (tag, payload) = value.into_parts();
205        ((payload as u64) << 6) | (tag as u64)
206    }
207}
208
209// AFTER BLOCK NUMBER
210// ================================================================================================
211
212/// A wrapper around a block number which enforces that it is not `u32::MAX`.
213///
214/// Used for the [`NoteExecutionHint::AfterBlock`] variant where this constraint is needed.
215#[derive(Debug, Clone, Copy, PartialEq, Eq)]
216pub struct AfterBlockNumber(BlockNumber);
217
218impl AfterBlockNumber {
219    /// Creates a new [`AfterBlockNumber`] from the given `block_number`.
220    ///
221    /// # Errors
222    ///
223    /// Returns an error if:
224    /// - `block_number` is equal to `u32::MAX`.
225    pub fn new(block_number: BlockNumber) -> Result<Self, NoteError> {
226        if block_number.as_u32() == u32::MAX {
227            Err(NoteError::NoteExecutionHintAfterBlockCannotBeU32Max)
228        } else {
229            Ok(Self(block_number))
230        }
231    }
232
233    /// Returns the block number as a `u32`.
234    pub fn as_u32(&self) -> u32 {
235        self.0.as_u32()
236    }
237}
238
239impl From<AfterBlockNumber> for u32 {
240    fn from(block_number: AfterBlockNumber) -> Self {
241        block_number.0.as_u32()
242    }
243}
244
245impl TryFrom<u32> for AfterBlockNumber {
246    type Error = NoteError;
247
248    fn try_from(block_number: u32) -> Result<Self, Self::Error> {
249        Self::new(block_number.into())
250    }
251}
252
253// TESTS
254// ================================================================================================
255
256#[cfg(test)]
257mod tests {
258    use assert_matches::assert_matches;
259
260    use super::*;
261
262    fn assert_hint_serde(note_execution_hint: NoteExecutionHint) {
263        let (tag, payload) = note_execution_hint.into_parts();
264        let deserialized = NoteExecutionHint::from_parts(tag, payload).unwrap();
265        assert_eq!(deserialized, note_execution_hint);
266    }
267
268    #[test]
269    fn test_serialization_round_trip() {
270        assert_hint_serde(NoteExecutionHint::None);
271        assert_hint_serde(NoteExecutionHint::Always);
272        assert_hint_serde(NoteExecutionHint::after_block(15.into()).unwrap());
273        assert_hint_serde(NoteExecutionHint::OnBlockSlot {
274            round_len: 9,
275            slot_len: 12,
276            slot_offset: 18,
277        });
278    }
279
280    #[test]
281    fn test_encode_round_trip() {
282        let hint = NoteExecutionHint::after_block(15.into()).unwrap();
283        let hint_int: u64 = hint.into();
284        let decoded_hint: NoteExecutionHint = hint_int.try_into().unwrap();
285        assert_eq!(hint, decoded_hint);
286
287        let hint = NoteExecutionHint::OnBlockSlot {
288            round_len: 22,
289            slot_len: 33,
290            slot_offset: 44,
291        };
292        let hint_int: u64 = hint.into();
293        let decoded_hint: NoteExecutionHint = hint_int.try_into().unwrap();
294        assert_eq!(hint, decoded_hint);
295
296        let always_int: u64 = NoteExecutionHint::always().into();
297        assert_eq!(always_int, 1u64);
298    }
299
300    #[test]
301    fn test_can_be_consumed() {
302        let none = NoteExecutionHint::none();
303        assert!(none.can_be_consumed(100.into()).is_none());
304
305        let always = NoteExecutionHint::always();
306        assert!(always.can_be_consumed(100.into()).unwrap());
307
308        let after_block = NoteExecutionHint::after_block(12345.into()).unwrap();
309        assert!(!after_block.can_be_consumed(12344.into()).unwrap());
310        assert!(after_block.can_be_consumed(12345.into()).unwrap());
311
312        let on_block_slot = NoteExecutionHint::on_block_slot(10, 7, 1);
313        assert!(!on_block_slot.can_be_consumed(127.into()).unwrap()); // Block 127 is not in the slot 128..255
314        assert!(on_block_slot.can_be_consumed(128.into()).unwrap()); // Block 128 is in the slot 128..255
315        assert!(on_block_slot.can_be_consumed(255.into()).unwrap()); // Block 255 is in the slot 128..255
316        assert!(!on_block_slot.can_be_consumed(256.into()).unwrap()); // Block 256 is not in the slot 128..255
317        assert!(on_block_slot.can_be_consumed(1152.into()).unwrap()); // Block 1152 is in the slot 1152..1279
318        assert!(on_block_slot.can_be_consumed(1279.into()).unwrap()); // Block 1279 is in the slot 1152..1279
319        assert!(on_block_slot.can_be_consumed(2176.into()).unwrap()); // Block 2176 is in the slot 2176..2303
320        assert!(!on_block_slot.can_be_consumed(2175.into()).unwrap()); // Block 1279 is in the slot
321        // 2176..2303
322    }
323
324    #[test]
325    fn test_parts_validity() {
326        NoteExecutionHint::from_parts(NoteExecutionHint::NONE_TAG, 1).unwrap_err();
327        NoteExecutionHint::from_parts(NoteExecutionHint::ALWAYS_TAG, 12).unwrap_err();
328        // 4th byte should be blank for tag 3 (OnBlockSlot)
329        NoteExecutionHint::from_parts(NoteExecutionHint::ON_BLOCK_SLOT_TAG, 1 << 24).unwrap_err();
330        NoteExecutionHint::from_parts(NoteExecutionHint::ON_BLOCK_SLOT_TAG, 0).unwrap();
331
332        NoteExecutionHint::from_parts(10, 1).unwrap_err();
333    }
334
335    #[test]
336    fn test_after_block_fails_on_u32_max() {
337        assert_matches!(
338            NoteExecutionHint::after_block(u32::MAX.into()).unwrap_err(),
339            NoteError::NoteExecutionHintAfterBlockCannotBeU32Max
340        );
341    }
342}