-
Notifications
You must be signed in to change notification settings - Fork 161
/
inputs.rs
115 lines (95 loc) · 3.5 KB
/
inputs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use alloc::vec::Vec;
use core::{ops::Deref, slice};
use super::{
super::ZERO, get_num_stack_values, ByteWriter, Felt, InputError, Serializable, MIN_STACK_DEPTH,
};
use crate::utils::{ByteReader, Deserializable, DeserializationError};
// STACK INPUTS
// ================================================================================================
/// Defines the initial state of the VM's operand stack.
///
/// The values in the struct are stored in the "stack order" - i.e., the last input is at the top
/// of the stack (in position 0).
#[derive(Clone, Debug, Default)]
pub struct StackInputs {
elements: [Felt; MIN_STACK_DEPTH],
}
impl StackInputs {
// CONSTRUCTORS
// --------------------------------------------------------------------------------------------
/// Returns [StackInputs] from a list of values, reversing them into a stack.
///
/// # Errors
/// Returns an error if the number of input values exceeds the allowed maximum.
pub fn new(mut values: Vec<Felt>) -> Result<Self, InputError> {
if values.len() > MIN_STACK_DEPTH {
return Err(InputError::InputLengthExceeded(MIN_STACK_DEPTH, values.len()));
}
values.reverse();
values.resize(MIN_STACK_DEPTH, ZERO);
Ok(Self { elements: values.try_into().unwrap() })
}
/// Attempts to create stack inputs from an iterator of integers.
///
/// # Errors
/// Returns an error if:
/// - The values do not represent a valid field element.
/// - Number of values in the iterator exceeds the allowed maximum number of input values.
pub fn try_from_ints<I>(iter: I) -> Result<Self, InputError>
where
I: IntoIterator<Item = u64>,
{
let values = iter
.into_iter()
.map(|v| Felt::try_from(v).map_err(|e| InputError::NotFieldElement(v, e)))
.collect::<Result<Vec<_>, _>>()?;
Self::new(values)
}
}
impl Deref for StackInputs {
type Target = [Felt; MIN_STACK_DEPTH];
fn deref(&self) -> &Self::Target {
&self.elements
}
}
impl From<[Felt; MIN_STACK_DEPTH]> for StackInputs {
fn from(value: [Felt; MIN_STACK_DEPTH]) -> Self {
Self { elements: value }
}
}
impl<'a> IntoIterator for &'a StackInputs {
type Item = &'a Felt;
type IntoIter = slice::Iter<'a, Felt>;
fn into_iter(self) -> Self::IntoIter {
self.elements.iter()
}
}
impl IntoIterator for StackInputs {
type Item = Felt;
type IntoIter = core::array::IntoIter<Felt, 16>;
fn into_iter(self) -> Self::IntoIter {
self.elements.into_iter()
}
}
// SERIALIZATION
// ================================================================================================
impl Serializable for StackInputs {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
let num_stack_values = get_num_stack_values(self);
target.write_u8(num_stack_values);
target.write_many(&self.elements[..num_stack_values as usize]);
}
}
impl Deserializable for StackInputs {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let num_elements = source.read_u8()?;
let mut elements = source.read_many::<Felt>(num_elements.into())?;
elements.reverse();
StackInputs::new(elements).map_err(|_| {
DeserializationError::InvalidValue(format!(
"number of stack elements should not be greater than {}, but {} was found",
MIN_STACK_DEPTH, num_elements
))
})
}
}