Skip to main content

miden_protocol/note/
nullifier.rs

1use alloc::string::String;
2use core::fmt::{Debug, Display, Formatter};
3
4use miden_crypto::WordError;
5use miden_protocol_macros::WordWrapper;
6
7use super::{
8    ByteReader,
9    ByteWriter,
10    Deserializable,
11    DeserializationError,
12    Felt,
13    Hasher,
14    NoteDetails,
15    Serializable,
16    WORD_SIZE,
17    Word,
18    ZERO,
19};
20
21// CONSTANTS
22// ================================================================================================
23
24const NULLIFIER_PREFIX_SHIFT: u8 = 48;
25
26// NULLIFIER
27// ================================================================================================
28
29/// A note's nullifier.
30///
31/// A note's nullifier is computed as:
32///
33/// > hash(serial_num, script_root, input_commitment, asset_commitment).
34///
35/// This achieves the following properties:
36/// - Every note can be reduced to a single unique nullifier.
37/// - We cannot derive a note's commitment from its nullifier, or a note's nullifier from its hash.
38/// - To compute the nullifier we must know all components of the note: serial_num, script_root,
39///   input_commitment and asset_commitment.
40#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, WordWrapper)]
41pub struct Nullifier(Word);
42
43impl Nullifier {
44    /// Returns a new note [Nullifier] instantiated from the provided digest.
45    pub fn new(
46        script_root: Word,
47        inputs_commitment: Word,
48        asset_commitment: Word,
49        serial_num: Word,
50    ) -> Self {
51        let mut elements = [ZERO; 4 * WORD_SIZE];
52        elements[..4].copy_from_slice(serial_num.as_elements());
53        elements[4..8].copy_from_slice(script_root.as_elements());
54        elements[8..12].copy_from_slice(inputs_commitment.as_elements());
55        elements[12..].copy_from_slice(asset_commitment.as_elements());
56        Self(Hasher::hash_elements(&elements))
57    }
58
59    /// Returns the most significant felt (the last element in array)
60    pub fn most_significant_felt(&self) -> Felt {
61        self.as_elements()[3]
62    }
63
64    /// Returns the prefix of this nullifier.
65    ///
66    /// Nullifier prefix is defined as the 16 most significant bits of the nullifier value.
67    pub fn prefix(&self) -> u16 {
68        (self.as_word()[3].as_int() >> NULLIFIER_PREFIX_SHIFT) as u16
69    }
70
71    /// Creates a Nullifier from a hex string. Assumes that the string starts with "0x" and
72    /// that the hexadecimal characters are big-endian encoded.
73    ///
74    /// Callers must ensure the provided value is an actual [`Nullifier`].
75    pub fn from_hex(hex_value: &str) -> Result<Self, WordError> {
76        Word::try_from(hex_value).map(Self::from_raw)
77    }
78
79    #[cfg(any(feature = "testing", test))]
80    pub fn dummy(n: u64) -> Self {
81        use miden_core::FieldElement;
82
83        Self(Word::new([Felt::ZERO, Felt::ZERO, Felt::ZERO, Felt::new(n)]))
84    }
85}
86
87impl Display for Nullifier {
88    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
89        f.write_str(&self.to_hex())
90    }
91}
92
93impl Debug for Nullifier {
94    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
95        Display::fmt(self, f)
96    }
97}
98
99// CONVERSIONS INTO NULLIFIER
100// ================================================================================================
101
102impl From<&NoteDetails> for Nullifier {
103    fn from(note: &NoteDetails) -> Self {
104        Self::new(
105            note.script().root(),
106            note.inputs().commitment(),
107            note.assets().commitment(),
108            note.serial_num(),
109        )
110    }
111}
112
113// SERIALIZATION
114// ================================================================================================
115
116impl Serializable for Nullifier {
117    fn write_into<W: ByteWriter>(&self, target: &mut W) {
118        target.write_bytes(&self.0.to_bytes());
119    }
120}
121
122impl Deserializable for Nullifier {
123    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
124        let nullifier = Word::read_from(source)?;
125        Ok(Self(nullifier))
126    }
127}
128
129// TESTS
130// ================================================================================================
131
132#[cfg(test)]
133mod tests {
134    use crate::note::Nullifier;
135
136    #[test]
137    fn test_from_hex_and_back() {
138        let nullifier_hex = "0x41e7dbbc8ce63ec25cf2d76d76162f16ef8fd1195288171f5e5a3e178222f6d2";
139        let nullifier = Nullifier::from_hex(nullifier_hex).unwrap();
140
141        assert_eq!(nullifier_hex, nullifier.to_hex());
142    }
143}