Skip to main content

miden_protocol/note/
note_details_commitment.rs

1use alloc::string::String;
2
3use miden_crypto_derive::WordWrapper;
4
5use super::{Felt, Hasher, Word};
6use crate::note::{NoteAssets, NoteRecipient};
7use crate::utils::serde::{
8    ByteReader,
9    ByteWriter,
10    Deserializable,
11    DeserializationError,
12    Serializable,
13};
14
15// NOTE DETAILS COMMITMENT
16// ================================================================================================
17
18/// A commitment to a note's details, without note metadata.
19///
20/// This commitment is computed as:
21/// > hash(NOTE_RECIPIENT_DIGEST || NOTE_ASSETS_COMMITMENT)
22///
23/// Together with the note metadata commitment it is used to derive the note's
24/// [`NoteId`](super::NoteId).
25#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, WordWrapper)]
26pub struct NoteDetailsCommitment(Word);
27
28impl NoteDetailsCommitment {
29    /// Returns a new [`NoteDetailsCommitment`] instantiated from the provided note components.
30    pub fn new(recipient: &NoteRecipient, assets: &NoteAssets) -> Self {
31        Self::from_raw_commitments(recipient.digest(), assets.commitment())
32    }
33
34    /// Returns a new [`NoteDetailsCommitment`] by merging the provided recipient and asset
35    /// commitments.
36    pub fn from_raw_commitments(recipient: Word, asset_commitment: Word) -> Self {
37        Self(Hasher::merge(&[recipient, asset_commitment]))
38    }
39}
40
41// SERIALIZATION
42// ================================================================================================
43
44impl Serializable for NoteDetailsCommitment {
45    fn write_into<W: ByteWriter>(&self, target: &mut W) {
46        target.write_bytes(&self.0.to_bytes());
47    }
48
49    fn get_size_hint(&self) -> usize {
50        Word::SERIALIZED_SIZE
51    }
52}
53
54impl Deserializable for NoteDetailsCommitment {
55    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
56        let commitment = Word::read_from(source)?;
57        Ok(Self(commitment))
58    }
59}