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

Implement Range for OpStack #338

Closed
wants to merge 10 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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,8 @@ specification/cheatsheet.aux
specification/cheatsheet.log
specification/cheatsheet.synctex.gz
triton-vm/proofs/*

# Ignore proptest-regression files caused by bug
triton-air/proptest-regressions/table/*
triton-isa/proptest-regressions/*
triton-vm/proptest-regressions/*
162 changes: 138 additions & 24 deletions triton-isa/src/op_stack.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::collections::VecDeque;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::num::TryFromIntError;
use std::ops::Index;
use std::ops::IndexMut;
use std::ops::Range;
use std::ops::RangeInclusive;

use arbitrary::Arbitrary;
use get_size2::GetSize;
Expand Down Expand Up @@ -35,10 +38,9 @@ pub const NUM_OP_STACK_REGISTERS: usize = OpStackElement::COUNT;
/// remaining elements.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Arbitrary)]
pub struct OpStack {
/// The underlying, actual stack. When manually accessing, be aware of reversed indexing:
/// while `op_stack[0]` is the top of the stack, `op_stack.stack[0]` is the lowest element in
/// the stack.
pub stack: Vec<BFieldElement>,
///`op_stack[0]` is the top of the stack,
/// `op_stack.stack[0]` is the lowest element in the stack.
stack: VecDeque<BFieldElement>,
Copy link
Author

@cyberbono3 cyberbono3 Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jan-ferdinand would you like to change stack to queue ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No; conceptually, it is still a stack. This should probably be reflected in the doc comment. How about:

    /// The underlying, actual stack.
    ///
    /// Even though the type suggests a queue, it is actually used as a stack. The
    /// decision to use a [VecDeque] is motivated by indexing using ranges,
    /// allowing, for example:
    ///
    /// ```no_compile
    /// let my_slice = op_stack[3..=5];
    /// ```
    ///
    /// Note that only `*_front` methods should be called on this stack. Pushing to
    /// or popping from the back is almost certainly a logic error.


underflow_io_sequence: Vec<UnderflowIO>,
}
Expand All @@ -51,17 +53,18 @@ pub enum OpStackError {

#[error("failed to convert BFieldElement {0} into u32")]
FailedU32Conversion(BFieldElement),

#[error("index {0} is out of range for OpStack")]
IndexOutOfBounds(usize),
}

impl OpStack {
pub fn new(program_digest: Digest) -> Self {
let mut stack = bfe_vec![0; OpStackElement::COUNT];

let reverse_digest = program_digest.reversed().values();
stack[..Digest::LEN].copy_from_slice(&reverse_digest);
stack[..Digest::LEN].copy_from_slice(&program_digest.values());

Self {
stack,
stack: VecDeque::from(stack),
underflow_io_sequence: vec![],
}
}
Expand All @@ -74,26 +77,27 @@ impl OpStack {
}

pub fn push(&mut self, element: BFieldElement) {
self.stack.push(element);
self.stack.push_front(element);
self.record_underflow_io(UnderflowIO::Write);
}

pub fn pop(&mut self) -> Result<BFieldElement> {
self.record_underflow_io(UnderflowIO::Read);
self.stack.pop().ok_or(OpStackError::TooShallow)
self.stack.pop_front().ok_or(OpStackError::TooShallow)
}

pub fn insert(&mut self, index: OpStackElement, element: BFieldElement) {
let insertion_index = self.len() - usize::from(index);
let insertion_index = usize::from(index);
self.stack.insert(insertion_index, element);
self.record_underflow_io(UnderflowIO::Write);
}

pub fn remove(&mut self, index: OpStackElement) -> BFieldElement {
pub fn remove(&mut self, index: OpStackElement) -> Result<BFieldElement> {
self.record_underflow_io(UnderflowIO::Read);
let top_of_stack = self.len() - 1;
let index = top_of_stack - usize::from(index);
self.stack.remove(index)
let index = usize::from(index);
self.stack
.remove(index)
.ok_or(OpStackError::IndexOutOfBounds(index))
}

fn record_underflow_io(&mut self, io_type: fn(BFieldElement) -> UnderflowIO) {
Expand Down Expand Up @@ -178,15 +182,44 @@ impl Index<usize> for OpStack {
type Output = BFieldElement;

fn index(&self, index: usize) -> &Self::Output {
let top_of_stack = self.len() - 1;
&self.stack[top_of_stack - index]
&self.stack[index]
}
}

impl Index<Range<usize>> for OpStack {
type Output = [BFieldElement];

fn index(&self, range: Range<usize>) -> &Self::Output {
&self.stack.as_slices().0[range.start..range.end]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
&self.stack.as_slices().0[range.start..range.end]
let (front, back) = self.stack.as_slices();
if front.is_empty() {
&back[range]
} else if back.is_empty() {
&front[range]
} else {
panic!("the underlying op stack is not contiguous")
}

}
}

impl Index<RangeInclusive<usize>> for OpStack {
type Output = [BFieldElement];

fn index(&self, range: RangeInclusive<usize>) -> &Self::Output {
let (start, end) = (*range.start(), *range.end());
&self.stack.as_slices().0[start..=end]
}
}

impl IndexMut<usize> for OpStack {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
let top_of_stack = self.len() - 1;
&mut self.stack[top_of_stack - index]
&mut self.stack[index]
}
}

impl IndexMut<Range<usize>> for OpStack {
fn index_mut(&mut self, range: Range<usize>) -> &mut Self::Output {
let (start, end) = (range.start, range.end);
&mut self.stack.as_mut_slices().0[start..end]
}
}

impl IndexMut<RangeInclusive<usize>> for OpStack {
fn index_mut(&mut self, range: RangeInclusive<usize>) -> &mut Self::Output {
let (start, end) = (*range.start(), *range.end());
&mut self.stack.as_mut_slices().0[start..=end]
}
}

Expand All @@ -198,20 +231,50 @@ impl Index<OpStackElement> for OpStack {
}
}

impl Index<Range<OpStackElement>> for OpStack {
type Output = [BFieldElement];

fn index(&self, range: Range<OpStackElement>) -> &Self::Output {
let (start, end) = (usize::from(range.start), usize::from(range.end));
&self.stack.as_slices().0[start..end]
}
}

impl Index<RangeInclusive<OpStackElement>> for OpStack {
type Output = [BFieldElement];

fn index(&self, range: RangeInclusive<OpStackElement>) -> &Self::Output {
let (start, end) = (usize::from(range.start()), usize::from(range.end()));
&self.stack.as_slices().0[start..=end]
}
}

impl IndexMut<OpStackElement> for OpStack {
fn index_mut(&mut self, stack_element: OpStackElement) -> &mut Self::Output {
&mut self[usize::from(stack_element)]
}
}

impl IndexMut<Range<OpStackElement>> for OpStack {
fn index_mut(&mut self, range: Range<OpStackElement>) -> &mut Self::Output {
let (start, end) = (usize::from(range.start), usize::from(range.end));
&mut self.stack.as_mut_slices().0[start..end]
}
}

impl IndexMut<RangeInclusive<OpStackElement>> for OpStack {
fn index_mut(&mut self, range: RangeInclusive<OpStackElement>) -> &mut Self::Output {
let (start, end) = (usize::from(range.start()), usize::from(range.end()));
&mut self.stack.as_mut_slices().0[start..=end]
}
}

impl IntoIterator for OpStack {
type Item = BFieldElement;
type IntoIter = std::vec::IntoIter<Self::Item>;
type IntoIter = std::collections::vec_deque::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
let mut stack = self.stack;
stack.reverse();
stack.into_iter()
self.stack.into_iter()
}
}

Expand Down Expand Up @@ -1024,10 +1087,61 @@ mod tests {
}

let expected_element = BFieldElement::from(removal_index);
let removed_element = op_stack.remove(removal_index);
let removed_element = op_stack.remove(removal_index).unwrap();
prop_assert_eq!(expected_element, removed_element);

let expected_len = 2 * OpStackElement::COUNT - 1;
prop_assert_eq!(expected_len, op_stack.len());
}

fn setup_op_stack() -> OpStack {
OpStack {
stack: VecDeque::from(bfe_vec![1, 2, 3, 4, 5]),
underflow_io_sequence: vec![],
}
}

#[test]
fn test_opstack_index_range() {
let mut op_stack = setup_op_stack();

assert_eq!(op_stack[0], op_stack.stack[0]);
assert_eq!(
&op_stack[0..2],
&[BFieldElement::from(1), BFieldElement::from(2)]
);
assert_eq!(
&op_stack[0..=2],
&[
BFieldElement::from(1),
BFieldElement::from(2),
BFieldElement::from(3)
]
);

op_stack.push(BFieldElement::from(0));
assert_eq!(op_stack[0], BFieldElement::from(0));

assert!(op_stack.pop().is_ok());
assert_eq!(op_stack[0], BFieldElement::from(1));

assert_eq!(
Ok(BFieldElement::from(3)),
op_stack.remove(OpStackElement::ST2),
);

let mut op_stack = setup_op_stack();
assert_eq!(
&mut op_stack[0..2],
&mut [BFieldElement::from(1), BFieldElement::from(2)]
);
assert_eq!(
&mut op_stack[0..=2],
&mut [
BFieldElement::from(1),
BFieldElement::from(2),
BFieldElement::from(3)
]
);
}
}
16 changes: 8 additions & 8 deletions triton-isa/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::collections::HashSet;
use std::error::Error;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;

use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::take_while;
Expand All @@ -22,6 +14,13 @@ use nom::multi::many0;
use nom::multi::many1;
use nom::Finish;
use nom::IResult;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::collections::HashSet;
use std::error::Error;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use twenty_first::bfe;
use twenty_first::prelude::BFieldElement;

Expand All @@ -31,6 +30,7 @@ use crate::instruction::Instruction;
use crate::instruction::LabelledInstruction;
use crate::instruction::TypeHint;
use crate::instruction::ALL_INSTRUCTION_NAMES;

use crate::op_stack::NumberOfWords;
use crate::op_stack::OpStackElement;

Expand Down
8 changes: 8 additions & 0 deletions triton-vm/proptest-regressions/constraints.txt

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions triton-vm/proptest-regressions/lib.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 2743b74a372c974c49932a8db3db2be90fbf365c87cab10b86dee3eb57c5f179 # shrinks to input = _ProveVerifyKnowledgeOfHashPreimageArgs { hash_preimage: Digest([BFieldElement(0), BFieldElement(0), BFieldElement(0), BFieldElement(0), BFieldElement(0)]), some_tie_to_an_outer_context: Digest([BFieldElement(0), BFieldElement(0), BFieldElement(0), BFieldElement(0), BFieldElement(0)]) }
9 changes: 9 additions & 0 deletions triton-vm/proptest-regressions/stark.txt

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions triton-vm/proptest-regressions/vm.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc cc5e7c83023ce77adf24c14717b5fdb7bb5737c728576f7a5cef9a0dabeb23c1 # shrinks to input = _NegativePropertyIsU32Args { st0: BFieldElement(4109908372713024461) }
cc 3e1b845b7bb2e5d0b385404fd8181c39663c3dd8987060b369c7c955fb403f61 # shrinks to input = _PropertyBasedSpongeAndHashInstructionsProgramSanityCheckArgs { program: ProgramForSpongeAndHashInstructions { instructions: [SpongeInit], ram: {} } }
9 changes: 4 additions & 5 deletions triton-vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl VMState {
Instruction::Pop(n) => self.pop(n)?,
Instruction::Push(field_element) => self.push(field_element),
Instruction::Divine(n) => self.divine(n)?,
Instruction::Pick(stack_element) => self.pick(stack_element),
Instruction::Pick(stack_element) => self.pick(stack_element)?,
Instruction::Place(stack_element) => self.place(stack_element)?,
Instruction::Dup(stack_element) => self.dup(stack_element),
Instruction::Swap(stack_element) => self.swap(stack_element),
Expand Down Expand Up @@ -484,12 +484,11 @@ impl VMState {
Ok(vec![])
}

fn pick(&mut self, stack_register: OpStackElement) -> Vec<CoProcessorCall> {
let element = self.op_stack.remove(stack_register);
fn pick(&mut self, stack_register: OpStackElement) -> InstructionResult<Vec<CoProcessorCall>> {
let element = self.op_stack.remove(stack_register)?;
self.op_stack.push(element);

self.instruction_pointer += 2;
vec![]
Ok(vec![])
}

fn place(&mut self, stack_register: OpStackElement) -> InstructionResult<Vec<CoProcessorCall>> {
Expand Down