miden_objects/note/
header.rs

1use alloc::vec::Vec;
2
3use super::{
4    ByteReader, ByteWriter, Deserializable, DeserializationError, Digest, Felt, NoteId,
5    NoteMetadata, Serializable, Word,
6};
7use crate::Hasher;
8
9// NOTE HEADER
10// ================================================================================================
11
12/// Holds the strictly required, public information of a note.
13///
14/// See [NoteId] and [NoteMetadata] for additional details.
15#[derive(Debug, Copy, Clone, PartialEq, Eq)]
16pub struct NoteHeader {
17    note_id: NoteId,
18    note_metadata: NoteMetadata,
19}
20
21impl NoteHeader {
22    /// Returns a new [NoteHeader] instantiated from the specified note ID and metadata.
23    pub fn new(note_id: NoteId, note_metadata: NoteMetadata) -> Self {
24        Self { note_id, note_metadata }
25    }
26
27    /// Returns the note's identifier.
28    ///
29    /// The [NoteId] value is both an unique identifier and a commitment to the note.
30    pub fn id(&self) -> NoteId {
31        self.note_id
32    }
33
34    /// Returns the note's metadata.
35    pub fn metadata(&self) -> &NoteMetadata {
36        &self.note_metadata
37    }
38
39    /// Returns a commitment to the note and its metadata.
40    ///
41    /// > hash(NOTE_ID || NOTE_METADATA)
42    ///
43    /// This value is used primarily for authenticating notes consumed when they are consumed
44    /// in a transaction.
45    pub fn commitment(&self) -> Digest {
46        compute_note_commitment(self.id(), self.metadata())
47    }
48}
49
50// UTILITIES
51// ================================================================================================
52
53/// Returns a commitment to the note and its metadata.
54///
55/// > hash(NOTE_ID || NOTE_METADATA)
56///
57/// This value is used primarily for authenticating notes consumed when they are consumed
58/// in a transaction.
59pub fn compute_note_commitment(id: NoteId, metadata: &NoteMetadata) -> Digest {
60    Hasher::merge(&[id.inner(), Word::from(metadata).into()])
61}
62
63// CONVERSIONS FROM NOTE HEADER
64// ================================================================================================
65
66impl From<NoteHeader> for [Felt; 8] {
67    fn from(note_header: NoteHeader) -> Self {
68        (&note_header).into()
69    }
70}
71
72impl From<NoteHeader> for [Word; 2] {
73    fn from(note_header: NoteHeader) -> Self {
74        (&note_header).into()
75    }
76}
77
78impl From<NoteHeader> for [u8; 64] {
79    fn from(note_header: NoteHeader) -> Self {
80        (&note_header).into()
81    }
82}
83
84impl From<&NoteHeader> for [Felt; 8] {
85    fn from(note_header: &NoteHeader) -> Self {
86        let mut elements: [Felt; 8] = Default::default();
87        elements[..4].copy_from_slice(note_header.note_id.as_elements());
88        elements[4..].copy_from_slice(&Word::from(note_header.metadata()));
89        elements
90    }
91}
92
93impl From<&NoteHeader> for [Word; 2] {
94    fn from(note_header: &NoteHeader) -> Self {
95        let mut elements: [Word; 2] = Default::default();
96        elements[0].copy_from_slice(note_header.note_id.as_elements());
97        elements[1].copy_from_slice(&Word::from(note_header.metadata()));
98        elements
99    }
100}
101
102impl From<&NoteHeader> for [u8; 64] {
103    fn from(note_header: &NoteHeader) -> Self {
104        let mut elements: [u8; 64] = [0; 64];
105        let note_metadata_bytes = Word::from(note_header.metadata())
106            .iter()
107            .flat_map(|x| x.as_int().to_le_bytes())
108            .collect::<Vec<u8>>();
109        elements[..32].copy_from_slice(&note_header.note_id.as_bytes());
110        elements[32..].copy_from_slice(&note_metadata_bytes);
111        elements
112    }
113}
114
115// SERIALIZATION
116// ================================================================================================
117
118impl Serializable for NoteHeader {
119    fn write_into<W: ByteWriter>(&self, target: &mut W) {
120        self.note_id.write_into(target);
121        self.note_metadata.write_into(target);
122    }
123}
124
125impl Deserializable for NoteHeader {
126    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
127        let note_id = NoteId::read_from(source)?;
128        let note_metadata = NoteMetadata::read_from(source)?;
129
130        Ok(Self { note_id, note_metadata })
131    }
132}