miden_protocol/note/
inputs.rs1use alloc::vec::Vec;
2
3use crate::errors::NoteError;
4use crate::utils::serde::{
5 ByteReader,
6 ByteWriter,
7 Deserializable,
8 DeserializationError,
9 Serializable,
10};
11use crate::{Felt, Hasher, MAX_INPUTS_PER_NOTE, Word};
12
13#[derive(Clone, Debug)]
24pub struct NoteInputs {
25 values: Vec<Felt>,
26 commitment: Word,
27}
28
29impl NoteInputs {
30 pub fn new(values: Vec<Felt>) -> Result<Self, NoteError> {
38 if values.len() > MAX_INPUTS_PER_NOTE {
39 return Err(NoteError::TooManyInputs(values.len()));
40 }
41
42 let commitment = Hasher::hash_elements(&values);
43
44 Ok(Self { values, commitment })
45 }
46
47 pub fn commitment(&self) -> Word {
52 self.commitment
53 }
54
55 pub fn num_values(&self) -> u16 {
59 const _: () = assert!(MAX_INPUTS_PER_NOTE <= u16::MAX as usize);
60 debug_assert!(
61 self.values.len() <= MAX_INPUTS_PER_NOTE,
62 "The constructor should have checked the number of inputs"
63 );
64 self.values.len() as u16
65 }
66
67 pub fn values(&self) -> &[Felt] {
69 &self.values
70 }
71
72 pub fn to_elements(&self) -> Vec<Felt> {
74 self.values.to_vec()
75 }
76}
77
78impl Default for NoteInputs {
79 fn default() -> Self {
80 Self::new(vec![]).expect("empty values should be valid")
81 }
82}
83
84impl PartialEq for NoteInputs {
85 fn eq(&self, other: &Self) -> bool {
86 let NoteInputs { values: inputs, commitment: _ } = self;
87 inputs == &other.values
88 }
89}
90
91impl Eq for NoteInputs {}
92
93impl From<NoteInputs> for Vec<Felt> {
97 fn from(value: NoteInputs) -> Self {
98 value.values
99 }
100}
101
102impl TryFrom<Vec<Felt>> for NoteInputs {
103 type Error = NoteError;
104
105 fn try_from(value: Vec<Felt>) -> Result<Self, Self::Error> {
106 NoteInputs::new(value)
107 }
108}
109
110impl Serializable for NoteInputs {
114 fn write_into<W: ByteWriter>(&self, target: &mut W) {
115 let NoteInputs { values, commitment: _commitment } = self;
116 target.write_u16(values.len().try_into().expect("inputs len is not a u16 value"));
117 target.write_many(values);
118 }
119}
120
121impl Deserializable for NoteInputs {
122 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
123 let num_values = source.read_u16()? as usize;
124 let values = source.read_many::<Felt>(num_values)?;
125 Self::new(values).map_err(|v| DeserializationError::InvalidValue(format!("{v}")))
126 }
127}
128
129#[cfg(test)]
133mod tests {
134 use miden_crypto::utils::Deserializable;
135
136 use super::{Felt, NoteInputs, Serializable};
137
138 #[test]
139 fn test_input_ordering() {
140 let inputs = vec![Felt::new(1), Felt::new(2), Felt::new(3)];
142 let expected_ordering = vec![Felt::new(1), Felt::new(2), Felt::new(3)];
144
145 let note_inputs = NoteInputs::new(inputs).expect("note created should succeed");
146 assert_eq!(&expected_ordering, ¬e_inputs.values);
147 }
148
149 #[test]
150 fn test_input_serialization() {
151 let inputs = vec![Felt::new(1), Felt::new(2), Felt::new(3)];
152 let note_inputs = NoteInputs::new(inputs).unwrap();
153
154 let bytes = note_inputs.to_bytes();
155 let parsed_note_inputs = NoteInputs::read_from_bytes(&bytes).unwrap();
156 assert_eq!(note_inputs, parsed_note_inputs);
157 }
158}