Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StackOutputs: introduce pop_*() methods #1146

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions core/src/stack/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use super::{
STACK_TOP_SIZE,
};

use miden_crypto::hash::rpo::RpoDigest;

// STACK OUTPUTS
// ================================================================================================

Expand Down Expand Up @@ -140,6 +142,10 @@ impl StackOutputs {
overflow
}

pub fn iter(&self) -> impl Iterator<Item = Felt> + '_ {
self.stack.iter().cloned().map(Felt::from)
}

// PUBLIC MUTATORS
// --------------------------------------------------------------------------------------------

Expand All @@ -149,6 +155,53 @@ impl StackOutputs {
pub fn stack_mut(&mut self) -> &mut [u64] {
&mut self.stack
}

/// Pops a field element off the stack. Returns `None` if the stack is empty.
pub fn pop_felt(&mut self) -> Option<Felt> {
self.stack.pop().map(Into::into)
}

/// Pops a RPO digest off the stack. Returns `None` if not enough elements are on the stack.
pub fn pop_digest(&mut self) -> Option<RpoDigest> {
let digest_elements: [Felt; 4] = {
let digest_elements: Vec<Felt> = (0..4)
.map(|_| self.pop_felt())
// Elements need to be reversed, since a word `[a, b, c, d]` will be stored on the
// stack as `[d, c, b, a]`
.rev()
.collect::<Option<_>>()?;

digest_elements.try_into().expect("digest_elements contains 4 elements")
};

Some(digest_elements.into())
}
}

// ITERATOR
// ================================================================================================
pub trait StackOutputsIteratorDigest: Iterator<Item = Felt> {
fn next_digest(&mut self) -> Option<RpoDigest>;
}

impl<I> StackOutputsIteratorDigest for I
where
I: Iterator<Item = Felt>,
{
fn next_digest(&mut self) -> Option<RpoDigest> {
let digest_elements: [Felt; 4] = {
let digest_elements: Vec<Felt> = (0..4)
.map(|_| self.next())
// Elements need to be reversed, since a word `[a, b, c, d]` will be stored on the
// stack as `[d, c, b, a]`
.rev()
.collect::<Option<_>>()?;

digest_elements.try_into().expect("digest_elements contains 4 elements")
};

Some(digest_elements.into())
}
}

// HELPER FUNCTIONS
Expand Down
4 changes: 4 additions & 0 deletions processor/src/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ impl ExecutionTrace {
&self.stack_outputs
}

pub fn stack_outputs_mut(&mut self) -> &mut StackOutputs {
&mut self.stack_outputs
}

/// Returns the initial state of the top 16 stack registers.
pub fn init_stack_state(&self) -> StackTopState {
let mut result = [ZERO; STACK_TOP_SIZE];
Expand Down
Loading