Skip to main content

miden_protocol/note/
note_details_commitment.rs

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