Skip to content

Commit

Permalink
StackOutputsPop
Browse files Browse the repository at this point in the history
  • Loading branch information
plafer committed Nov 13, 2023
1 parent ed0e870 commit fd79ceb
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 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 @@ -195,3 +197,33 @@ impl ToElements<Felt> for StackOutputs {
.collect()
}
}

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

pub trait StackOutputsPop<T> {
fn pop(&mut self) -> Option<T>;
}

impl StackOutputsPop<Felt> for StackOutputs {
fn pop(&mut self) -> Option<Felt> {
self.stack.pop().map(Into::into)
}
}

impl StackOutputsPop<RpoDigest> for StackOutputs {
fn pop(&mut self) -> Option<RpoDigest> {
let digest_elements: [Felt; 4] = {
let digest_elements: Vec<Felt> = (0..4)
.map(|_| <Self as StackOutputsPop<Felt>>::pop(self))
// 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())
}
}

0 comments on commit fd79ceb

Please sign in to comment.