Skip to content

Commit

Permalink
feat: extension field instructions change stack size
Browse files Browse the repository at this point in the history
Both instructions `xx_add` and `xx_mul` pop both their operands,
shrinking the stack by 3 elements.
  • Loading branch information
jan-ferdinand committed Nov 6, 2023
1 parent 0fac3fc commit f0b3ab8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
16 changes: 8 additions & 8 deletions triton-vm/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ impl<Dest: PartialEq + Default> AnInstruction<Dest> {
Pow => 30,
DivMod => 20,
PopCount => 28,
XxAdd => 80,
XxMul => 88,
XInvert => 96,
XbMul => 66,
ReadIo => 104,
WriteIo => 74,
XxAdd => 66,
XxMul => 74,
XInvert => 80,
XbMul => 82,
ReadIo => 88,
WriteIo => 90,
}
}

Expand Down Expand Up @@ -355,8 +355,8 @@ impl<Dest: PartialEq + Default> AnInstruction<Dest> {
Pow => -1,
DivMod => 0,
PopCount => 0,
XxAdd => 0,
XxMul => 0,
XxAdd => -3,
XxMul => -3,
XInvert => 0,
XbMul => -1,
ReadIo => 1,
Expand Down
5 changes: 0 additions & 5 deletions triton-vm/src/op_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ impl OpStack {
self.stack[top_of_stack_index - stack_element_index]
}

pub(crate) fn peek_at_top_extension_field_element(&self) -> XFieldElement {
let coefficients = [self.peek_at(ST0), self.peek_at(ST1), self.peek_at(ST2)];
XFieldElement::new(coefficients)
}

pub(crate) fn swap_top_with(&mut self, stack_element: OpStackElement) {
let stack_element_index = usize::from(stack_element);
let top_of_stack_index = self.stack.len() - 1;
Expand Down
46 changes: 33 additions & 13 deletions triton-vm/src/table/processor_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2058,23 +2058,18 @@ impl ExtProcessorTable {
circuit_builder.input(NextBaseRow(col.master_base_table_index()))
};

let st0_becomes_st0_plus_st3 =
next_base_row(ST0) - (curr_base_row(ST0) + curr_base_row(ST3));
let st1_becomes_st1_plus_st4 =
next_base_row(ST1) - (curr_base_row(ST1) + curr_base_row(ST4));
let st2_becomes_st2_plus_st5 =
next_base_row(ST2) - (curr_base_row(ST2) + curr_base_row(ST5));

let st0_becomes_st0_plus_st3 = next_base_row(ST0) - curr_base_row(ST0) - curr_base_row(ST3);
let st1_becomes_st1_plus_st4 = next_base_row(ST1) - curr_base_row(ST1) - curr_base_row(ST4);
let st2_becomes_st2_plus_st5 = next_base_row(ST2) - curr_base_row(ST2) - curr_base_row(ST5);
let specific_constraints = vec![
st0_becomes_st0_plus_st3,
st1_becomes_st1_plus_st4,
st2_becomes_st2_plus_st5,
];

[
specific_constraints,
Self::instruction_group_op_stack_remains_and_top_three_elements_unconstrained(
circuit_builder,
),
Self::constraints_for_shrinking_stack_by_3_and_top_3_unconstrained(circuit_builder),
Self::instruction_group_step_1(circuit_builder),
Self::instruction_group_keep_ram(circuit_builder),
]
Expand Down Expand Up @@ -2113,9 +2108,7 @@ impl ExtProcessorTable {
];
[
specific_constraints,
Self::instruction_group_op_stack_remains_and_top_three_elements_unconstrained(
circuit_builder,
),
Self::constraints_for_shrinking_stack_by_3_and_top_3_unconstrained(circuit_builder),
Self::instruction_group_step_1(circuit_builder),
Self::instruction_group_keep_ram(circuit_builder),
]
Expand Down Expand Up @@ -2378,6 +2371,33 @@ impl ExtProcessorTable {
+ write_io_deselector * running_evaluation_updates
}

fn constraints_for_shrinking_stack_by_3_and_top_3_unconstrained(
circuit_builder: &ConstraintCircuitBuilder<DualRowIndicator>,
) -> Vec<ConstraintCircuitMonad<DualRowIndicator>> {
let constant = |c: u64| circuit_builder.b_constant(c.into());
let curr_base_row = |col: ProcessorBaseTableColumn| {
circuit_builder.input(CurrentBaseRow(col.master_base_table_index()))
};
let next_base_row = |col: ProcessorBaseTableColumn| {
circuit_builder.input(NextBaseRow(col.master_base_table_index()))
};

vec![
next_base_row(ST3) - curr_base_row(ST6),
next_base_row(ST4) - curr_base_row(ST7),
next_base_row(ST5) - curr_base_row(ST8),
next_base_row(ST6) - curr_base_row(ST9),
next_base_row(ST7) - curr_base_row(ST10),
next_base_row(ST8) - curr_base_row(ST11),
next_base_row(ST9) - curr_base_row(ST12),
next_base_row(ST10) - curr_base_row(ST13),
next_base_row(ST11) - curr_base_row(ST14),
next_base_row(ST12) - curr_base_row(ST15),
next_base_row(OpStackPointer) - curr_base_row(OpStackPointer) + constant(3),
Self::running_product_op_stack_accounts_for_shrinking_stack_by(circuit_builder, 3),
]
}

fn stack_shrinks_by_any_of(
circuit_builder: &ConstraintCircuitBuilder<DualRowIndicator>,
shrinkages: &[usize],
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,15 +664,15 @@ impl<'pgm> VMState<'pgm> {

fn xx_add(&mut self) -> Result<Vec<CoProcessorCall>> {
let lhs = self.op_stack.pop_extension_field_element()?;
let rhs = self.op_stack.peek_at_top_extension_field_element();
let rhs = self.op_stack.pop_extension_field_element()?;
self.op_stack.push_extension_field_element(lhs + rhs);
self.instruction_pointer += 1;
Ok(vec![])
}

fn xx_mul(&mut self) -> Result<Vec<CoProcessorCall>> {
let lhs = self.op_stack.pop_extension_field_element()?;
let rhs = self.op_stack.peek_at_top_extension_field_element();
let rhs = self.op_stack.pop_extension_field_element()?;
self.op_stack.push_extension_field_element(lhs * rhs);
self.instruction_pointer += 1;
Ok(vec![])
Expand Down

0 comments on commit f0b3ab8

Please sign in to comment.