Skip to main content

miden_protocol/note/
metadata.rs

1use super::{
2    AccountId,
3    ByteReader,
4    ByteWriter,
5    Deserializable,
6    DeserializationError,
7    Felt,
8    NoteTag,
9    NoteType,
10    Serializable,
11    Word,
12};
13use crate::Hasher;
14use crate::note::{NoteAttachmentHeader, NoteAttachments};
15
16// PARTIAL NOTE METADATA
17// ================================================================================================
18
19/// The user-facing metadata associated with a note.
20///
21/// Contains the sender, note type, and tag. For the full protocol-level encoding (including
22/// attachment headers and commitment computation), see [`NoteMetadata`].
23#[derive(Debug, Clone, Copy, Eq, PartialEq)]
24pub struct PartialNoteMetadata {
25    /// The ID of the account which created the note.
26    sender: AccountId,
27
28    /// Defines how the note is to be stored (e.g. public or private).
29    note_type: NoteType,
30
31    /// A value which can be used by the recipient(s) to identify notes intended for them.
32    tag: NoteTag,
33}
34
35impl PartialNoteMetadata {
36    // CONSTRUCTORS
37    // --------------------------------------------------------------------------------------------
38
39    /// Returns a new [`PartialNoteMetadata`] instantiated with the specified parameters.
40    ///
41    /// The tag defaults to [`NoteTag::default()`]. Use [`PartialNoteMetadata::with_tag`] to set a
42    /// specific tag if needed.
43    pub fn new(sender: AccountId, note_type: NoteType) -> Self {
44        Self {
45            sender,
46            note_type,
47            tag: NoteTag::default(),
48        }
49    }
50
51    // ACCESSORS
52    // --------------------------------------------------------------------------------------------
53
54    /// Returns the account which created the note.
55    pub fn sender(&self) -> AccountId {
56        self.sender
57    }
58
59    /// Returns the note's type.
60    pub fn note_type(&self) -> NoteType {
61        self.note_type
62    }
63
64    /// Returns the tag associated with the note.
65    pub fn tag(&self) -> NoteTag {
66        self.tag
67    }
68
69    /// Returns `true` if the note is private, `false` otherwise.
70    pub fn is_private(&self) -> bool {
71        self.note_type == NoteType::Private
72    }
73
74    /// Returns `true` if the note is public, `false` otherwise.
75    pub fn is_public(&self) -> bool {
76        self.note_type == NoteType::Public
77    }
78
79    // MUTATORS
80    // --------------------------------------------------------------------------------------------
81
82    /// Mutates the note's tag by setting it to the provided value.
83    pub fn set_tag(&mut self, tag: NoteTag) {
84        self.tag = tag;
85    }
86
87    /// Returns a new [`PartialNoteMetadata`] with the tag set to the provided value.
88    ///
89    /// This is a builder method that consumes self and returns a new instance for method chaining.
90    pub fn with_tag(mut self, tag: NoteTag) -> Self {
91        self.tag = tag;
92        self
93    }
94}
95
96// SERIALIZATION
97// ================================================================================================
98
99impl Serializable for PartialNoteMetadata {
100    fn write_into<W: ByteWriter>(&self, target: &mut W) {
101        self.note_type().write_into(target);
102        self.sender().write_into(target);
103        self.tag().write_into(target);
104    }
105
106    fn get_size_hint(&self) -> usize {
107        self.note_type().get_size_hint()
108            + self.sender().get_size_hint()
109            + self.tag().get_size_hint()
110    }
111}
112
113impl Deserializable for PartialNoteMetadata {
114    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
115        let note_type = NoteType::read_from(source)?;
116        let sender = AccountId::read_from(source)?;
117        let tag = NoteTag::read_from(source)?;
118
119        Ok(PartialNoteMetadata::new(sender, note_type).with_tag(tag))
120    }
121}
122
123// NOTE METADATA
124// ================================================================================================
125
126/// Protocol-level note metadata that combines [`PartialNoteMetadata`] with attachment information.
127///
128/// This type wraps `PartialNoteMetadata` together with attachment headers and an attachment
129/// commitment, and knows how to encode them into a [`Word`] and compute commitments.
130///
131/// The metadata word is encoded as a single [`Word`] (4 felts) with the following layout:
132///
133/// ```text
134/// 0th felt: [sender_id_suffix (56 bits) | reserved (3 bits) | note_type (1 bit) | version (4 bits)]
135/// 1st felt: [sender_id_prefix (64 bits)]
136/// 2nd felt: [reserved (32 bits) | note_tag (32 bits)]
137/// 3rd felt: [attachment_3_scheme (16 bits) | attachment_2_scheme (16 bits) |
138///            attachment_1_scheme (16 bits) | attachment_0_scheme (16 bits)]
139/// ```
140///
141/// Felt validity is guaranteed:
142/// - 0th felt: The lower 8 bits of the account ID suffix are `0` by construction, so they can be
143///   overwritten. The suffix's MSB is zero so the felt stays valid when lower bits are set.
144/// - 1st felt: Equivalent to the account ID prefix, so it inherits its validity.
145/// - 2nd felt: The tag is a u32 and the reserved bits are _currently_ set to zero, however users
146///   shouldn't assume these are zero.
147/// - 3rd felt: Max value is `0xFFFEFFFE_FFFEFFFE` (schemes capped at 65534), which is less than
148///   `p`.
149///
150/// The version is hardcoded to 0 and is reserved for forward compatibility.
151#[derive(Debug, Clone, Copy, Eq, PartialEq)]
152pub struct NoteMetadata {
153    partial_metadata: PartialNoteMetadata,
154    attachment_headers: [NoteAttachmentHeader; NoteAttachments::MAX_COUNT],
155    attachments_commitment: Word,
156}
157
158impl NoteMetadata {
159    // CONSTANTS
160    // --------------------------------------------------------------------------------------------
161
162    /// The number of bits by which the note type is offset in the first felt of the metadata word.
163    const NOTE_TYPE_SHIFT: u64 = 4;
164
165    /// Version 1 of the note metadata encoding.
166    ///
167    /// If we make this public, we may want to instead consider introducing a `NoteMetadataVersion`
168    /// struct, similar to `AccountIdVersion`.
169    const VERSION_1: u8 = 1;
170
171    // CONSTRUCTORS
172    // --------------------------------------------------------------------------------------------
173
174    /// Returns a new [`NoteMetadata`] derived from the given partial metadata and attachments.
175    ///
176    /// The attachment headers and commitment are derived from the provided attachments.
177    pub fn new(partial_metadata: PartialNoteMetadata, attachments: &NoteAttachments) -> Self {
178        Self::from_parts(partial_metadata, attachments.to_headers(), attachments.to_commitment())
179    }
180
181    /// Creates a [`NoteMetadata`] from its raw parts.
182    ///
183    /// Prefer [`Self::new`] whenever possible.
184    pub fn from_parts(
185        partial_metadata: PartialNoteMetadata,
186        attachment_headers: [NoteAttachmentHeader; NoteAttachments::MAX_COUNT],
187        attachments_commitment: Word,
188    ) -> Self {
189        Self {
190            partial_metadata,
191            attachment_headers,
192            attachments_commitment,
193        }
194    }
195
196    // ACCESSORS
197    // --------------------------------------------------------------------------------------------
198
199    /// Returns the inner [`PartialNoteMetadata`].
200    pub fn partial_metadata(&self) -> &PartialNoteMetadata {
201        &self.partial_metadata
202    }
203
204    /// Returns the account which created the note.
205    pub fn sender(&self) -> AccountId {
206        self.partial_metadata.sender()
207    }
208
209    /// Returns the note's type.
210    pub fn note_type(&self) -> NoteType {
211        self.partial_metadata.note_type()
212    }
213
214    /// Returns the tag associated with the note.
215    pub fn tag(&self) -> NoteTag {
216        self.partial_metadata.tag()
217    }
218
219    /// Returns the attachment headers.
220    pub fn attachment_headers(&self) -> &[NoteAttachmentHeader; NoteAttachments::MAX_COUNT] {
221        &self.attachment_headers
222    }
223
224    /// Returns the attachments commitment.
225    pub fn attachments_commitment(&self) -> Word {
226        self.attachments_commitment
227    }
228
229    /// Returns `true` if the note is private, `false` otherwise.
230    pub fn is_private(&self) -> bool {
231        self.partial_metadata.is_private()
232    }
233
234    /// Returns `true` if the note is public, `false` otherwise.
235    pub fn is_public(&self) -> bool {
236        self.partial_metadata.is_public()
237    }
238
239    /// Returns the metadata encoded as a [`Word`].
240    ///
241    /// See [`NoteMetadata`] docs for the layout.
242    pub fn to_metadata_word(&self) -> Word {
243        let mut word = Word::empty();
244        word[0] = merge_sender_suffix_and_note_type(
245            self.partial_metadata.sender.suffix(),
246            self.partial_metadata.note_type,
247        );
248        word[1] = self.partial_metadata.sender.prefix().as_felt();
249        word[2] = self.partial_metadata.tag.into();
250        word[3] = merge_schemes(self.attachment_headers);
251        word
252    }
253
254    /// Returns the commitment to the note metadata, which is defined as:
255    ///
256    /// ```text
257    /// hash(NOTE_METADATA_WORD || ATTACHMENTS_COMMITMENT)
258    /// ```
259    pub fn to_commitment(&self) -> Word {
260        Hasher::merge(&[self.to_metadata_word(), self.attachments_commitment])
261    }
262
263    /// Consumes self and returns the inner [`PartialNoteMetadata`].
264    pub fn into_partial_metadata(self) -> PartialNoteMetadata {
265        self.partial_metadata
266    }
267}
268
269impl Serializable for NoteMetadata {
270    fn write_into<W: ByteWriter>(&self, target: &mut W) {
271        self.partial_metadata.write_into(target);
272
273        let present_headers_iter =
274            self.attachment_headers.iter().filter(|header| !header.is_absent());
275
276        let num_headers_present = u8::try_from(present_headers_iter.clone().count())
277            .expect("num attachments is validated to be at most 4");
278        num_headers_present.write_into(target);
279        target.write_many(present_headers_iter);
280
281        self.attachments_commitment.write_into(target);
282    }
283
284    fn get_size_hint(&self) -> usize {
285        self.partial_metadata.get_size_hint()
286            + core::mem::size_of::<u8>()
287            + self
288                .attachment_headers
289                .iter()
290                .filter(|header| !header.is_absent())
291                .map(NoteAttachmentHeader::get_size_hint)
292                .sum::<usize>()
293            + self.attachments_commitment.get_size_hint()
294    }
295}
296
297impl Deserializable for NoteMetadata {
298    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
299        let partial_metadata = PartialNoteMetadata::read_from(source)?;
300
301        let num_headers_present = u8::read_from(source)? as usize;
302        if num_headers_present > NoteAttachments::MAX_COUNT {
303            return Err(DeserializationError::InvalidValue(format!(
304                "number of attachment headers ({num_headers_present}) exceeds maximum ({})",
305                NoteAttachments::MAX_COUNT
306            )));
307        }
308
309        let mut attachment_headers = [NoteAttachmentHeader::absent(); NoteAttachments::MAX_COUNT];
310        for header in attachment_headers.iter_mut().take(num_headers_present) {
311            *header = NoteAttachmentHeader::read_from(source)?;
312        }
313
314        let attachment_commitment = Word::read_from(source)?;
315
316        Ok(Self::from_parts(partial_metadata, attachment_headers, attachment_commitment))
317    }
318}
319
320// HELPER FUNCTIONS
321// ================================================================================================
322
323/// Merges the suffix of an [`AccountId`] and note metadata into a single [`Felt`].
324///
325/// The layout is as follows:
326///
327/// ```text
328/// [sender_id_suffix (56 bits) | reserved (3 bits) | note_type (1 bit) | version (4 bits)]
329/// ```
330///
331/// The most significant bit of the suffix is guaranteed to be zero, so the felt retains its
332/// validity.
333///
334/// The `sender_id_suffix` is the suffix of the sender's account ID.
335fn merge_sender_suffix_and_note_type(sender_id_suffix: Felt, note_type: NoteType) -> Felt {
336    let mut merged = sender_id_suffix.as_canonical_u64();
337
338    let note_type_byte = note_type as u8;
339    debug_assert!(note_type_byte < 2, "note type must not contain values >= 2");
340    // note_type at bit 4, version at bits 0..=3 (hardcoded to NoteMetadata::VERSION_1)
341    merged |= (note_type_byte as u64) << NoteMetadata::NOTE_TYPE_SHIFT;
342    merged |= NoteMetadata::VERSION_1 as u64;
343
344    // SAFETY: The most significant bit of the suffix is zero by construction so the u64 will be a
345    // valid felt.
346    Felt::try_from(merged).expect("encoded value should be a valid felt")
347}
348
349/// Merges four attachment schemes into a single [`Felt`].
350///
351/// The layout is as follows:
352///
353/// ```text
354/// [attachment_3_scheme (16 bits) | attachment_2_scheme (16 bits) |
355///  attachment_1_scheme (16 bits) | attachment_0_scheme (16 bits)]
356/// ```
357///
358/// Max value: `0xFFFEFFFE_FFFEFFFE` < p. Schemes are capped at 65534.
359fn merge_schemes(headers: [NoteAttachmentHeader; NoteAttachments::MAX_COUNT]) -> Felt {
360    let mut merged: u64 = headers[0].as_u16() as u64;
361    merged |= (headers[1].as_u16() as u64) << 16;
362    merged |= (headers[2].as_u16() as u64) << 32;
363    merged |= (headers[3].as_u16() as u64) << 48;
364
365    Felt::try_from(merged).expect("encoded value should be a valid felt (schemes <= 65534)")
366}
367
368// TESTS
369// ================================================================================================
370
371#[cfg(test)]
372mod tests {
373
374    use super::*;
375    use crate::note::{NoteAttachment, NoteAttachmentScheme};
376    use crate::testing::account_id::ACCOUNT_ID_MAX_ONES;
377
378    #[test]
379    fn note_metadata_word_encodes_attachment_header() -> anyhow::Result<()> {
380        let sender = AccountId::try_from(ACCOUNT_ID_MAX_ONES).unwrap();
381        let partial_metadata =
382            PartialNoteMetadata::new(sender, NoteType::Public).with_tag(NoteTag::new(0xff));
383        let attachment0 = NoteAttachment::with_word(
384            NoteAttachmentScheme::new(1)?,
385            Word::from([10, 20, 30, 40u32]),
386        );
387        let attachment1 = NoteAttachment::with_words(
388            NoteAttachmentScheme::new(0xfffe)?,
389            vec![Word::from([10, 20, 30, 40u32]), Word::from([10, 20, 30, 40u32])],
390        )?;
391        let attachments = NoteAttachments::new(vec![attachment0, attachment1])?;
392        let metadata = NoteMetadata::new(partial_metadata, &attachments);
393
394        let encoded = metadata.to_metadata_word();
395
396        let tag = encoded[2].as_canonical_u64();
397        assert_eq!(tag, 0x0000_0000_0000_00ff);
398
399        let schemes = encoded[3].as_canonical_u64();
400        // scheme 3 and 4 are 0, 2 is 0xfffe, 1 is 0x1
401        assert_eq!(schemes, 0x0000_0000_fffe_0001);
402
403        Ok(())
404    }
405
406    #[rstest::rstest]
407    #[case::attachment_none([])]
408    #[case::attachment_two_words([
409      NoteAttachment::with_word(NoteAttachmentScheme::none(), Word::from([3, 4, 5, 6u32])),
410      NoteAttachment::with_word(NoteAttachmentScheme::none(), Word::from([3, 4, 5, 6u32])),
411    ])]
412    #[case::attachment_word_and_two_arrays([
413      NoteAttachment::with_word(NoteAttachmentScheme::none(), Word::from([3, 4, 5, 6u32])),
414      NoteAttachment::with_words(
415        NoteAttachmentScheme::MAX,
416        vec![Word::from([5, 5, 5, 5u32]); 2],
417      )?,
418      NoteAttachment::with_words(
419        NoteAttachmentScheme::MAX,
420        vec![Word::from([10, 10, 10, 10u32]); NoteAttachment::MAX_NUM_WORDS as usize],
421      )?,
422    ])]
423    #[test]
424    fn note_metadata_serde(
425        #[case] attachments: impl IntoIterator<Item = NoteAttachment>,
426    ) -> anyhow::Result<()> {
427        // Use the Account ID with the maximum one bits to test if the merge function always
428        // produces valid felts.
429        let sender = AccountId::try_from(ACCOUNT_ID_MAX_ONES).unwrap();
430        let note_type = NoteType::Public;
431        let tag = NoteTag::new(u32::MAX);
432        let partial_metadata = PartialNoteMetadata::new(sender, note_type).with_tag(tag);
433        let attachments = NoteAttachments::new(attachments.into_iter().collect())?;
434        let metadata = NoteMetadata::new(partial_metadata, &attachments);
435
436        // Partial Metadata Roundtrip
437        let deserialized = PartialNoteMetadata::read_from_bytes(&partial_metadata.to_bytes())?;
438        assert_eq!(deserialized, partial_metadata);
439
440        // Metadata Roundtrip
441        let roundtripped = NoteMetadata::read_from_bytes(&metadata.to_bytes())?;
442        assert_eq!(roundtripped, metadata);
443
444        Ok(())
445    }
446
447    #[test]
448    fn note_metadata_header_encodes_v1_as_one() {
449        let sender = AccountId::try_from(ACCOUNT_ID_MAX_ONES).unwrap();
450        let metadata = PartialNoteMetadata::new(sender, NoteType::Private);
451        let metadata = NoteMetadata::new(metadata, &NoteAttachments::default());
452
453        let metadata = metadata.to_metadata_word();
454        let version = metadata[0].as_canonical_u64() & 0b1111;
455
456        assert_eq!(version, NoteMetadata::VERSION_1 as u64);
457        assert_eq!(version, 1);
458    }
459}