miden_core/advice/
stack.rs1use alloc::{collections::VecDeque, vec::Vec};
2
3use crate::{Felt, Word, field::QuotientMap, program::InputError};
4
5#[derive(Clone, Debug, Default, PartialEq, Eq)]
12pub struct AdviceStack {
13 stack: VecDeque<Felt>,
14}
15
16impl AdviceStack {
17 pub fn new() -> Self {
19 Self::default()
20 }
21
22 pub fn try_from_values<I>(values: I) -> Result<Self, InputError>
28 where
29 I: IntoIterator<Item = u64>,
30 {
31 values
32 .into_iter()
33 .map(|value| {
34 Felt::from_canonical_checked(value).ok_or(InputError::InvalidStackElement(value))
35 })
36 .collect()
37 }
38
39 pub fn len(&self) -> usize {
41 self.stack.len()
42 }
43
44 pub fn is_empty(&self) -> bool {
46 self.stack.is_empty()
47 }
48
49 pub fn iter(&self) -> impl Iterator<Item = &Felt> {
51 self.stack.iter()
52 }
53
54 pub fn append_element(&mut self, value: Felt) -> &mut Self {
58 self.stack.push_back(value);
59 self
60 }
61
62 pub fn append_elements<I>(&mut self, values: I) -> &mut Self
66 where
67 I: IntoIterator<Item = Felt>,
68 {
69 self.stack.extend(values);
70 self
71 }
72
73 pub fn prepend_elements<I>(&mut self, values: I) -> &mut Self
77 where
78 I: IntoIterator<Item = Felt>,
79 {
80 let values: Vec<Felt> = values.into_iter().collect();
81 for value in values.into_iter().rev() {
82 self.stack.push_front(value);
83 }
84 self
85 }
86
87 pub fn push_element(&mut self, value: Felt) -> &mut Self {
89 self.stack.push_front(value);
90 self
91 }
92
93 pub fn prepend_word(&mut self, word: Word) -> &mut Self {
95 self.prepend_elements(word.iter().copied())
96 }
97
98 pub fn prepend_stack(&mut self, stack: AdviceStack) -> &mut Self {
100 self.prepend_elements(stack.into_elements())
101 }
102
103 pub fn append_for_adv_push(&mut self, slice: &[Felt]) -> &mut Self {
107 for elem in slice.iter().rev() {
108 self.stack.push_back(*elem);
109 }
110 self
111 }
112
113 pub fn append_word(&mut self, word: Word) -> &mut Self {
115 self.stack.extend(word.iter().copied());
116 self
117 }
118
119 pub fn append_dword(&mut self, words: [Word; 2]) -> &mut Self {
121 for word in words {
122 self.append_word(word);
123 }
124 self
125 }
126
127 pub fn append_for_adv_pipe(&mut self, slice: &[Felt]) -> &mut Self {
133 assert!(
134 slice.len().is_multiple_of(8),
135 "append_for_adv_pipe requires slice length to be a multiple of 8, got {}",
136 slice.len()
137 );
138
139 self.stack.extend(slice.iter().copied());
140 self
141 }
142
143 pub fn consume_element(&mut self) -> Option<Felt> {
145 self.stack.pop_front()
146 }
147
148 pub fn consume_word(&mut self) -> Option<Word> {
150 if self.stack.len() < 4 {
151 return None;
152 }
153
154 Some(Word::new([
155 self.consume_element().expect("checked len"),
156 self.consume_element().expect("checked len"),
157 self.consume_element().expect("checked len"),
158 self.consume_element().expect("checked len"),
159 ]))
160 }
161
162 pub fn consume_dword(&mut self) -> Option<[Word; 2]> {
164 if self.stack.len() < 8 {
165 return None;
166 }
167
168 Some([self.consume_word()?, self.consume_word()?])
169 }
170
171 pub fn into_elements(self) -> Vec<Felt> {
173 self.stack.into_iter().collect()
174 }
175}
176
177impl From<Vec<Felt>> for AdviceStack {
178 fn from(stack: Vec<Felt>) -> Self {
179 Self { stack: stack.into() }
180 }
181}
182
183impl From<VecDeque<Felt>> for AdviceStack {
184 fn from(stack: VecDeque<Felt>) -> Self {
185 Self { stack }
186 }
187}
188
189impl From<AdviceStack> for Vec<Felt> {
190 fn from(stack: AdviceStack) -> Self {
191 stack.into_elements()
192 }
193}
194
195impl FromIterator<Felt> for AdviceStack {
196 fn from_iter<T: IntoIterator<Item = Felt>>(iter: T) -> Self {
197 Self { stack: iter.into_iter().collect() }
198 }
199}