Skip to content

Commit

Permalink
refactor: add type for permutation's trace
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Oct 12, 2023
1 parent d30e023 commit 94899d8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
29 changes: 9 additions & 20 deletions triton-vm/src/aet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::program::Program;
use crate::stark::StarkHasher;
use crate::table::hash_table;
use crate::table::hash_table::HashTable;
use crate::table::hash_table::PERMUTATION_TRACE_LENGTH;
use crate::table::hash_table::PermutationTrace;
use crate::table::processor_table;
use crate::table::table_column::HashBaseTableColumn::CI;
use crate::table::table_column::MasterBaseTableColumn;
Expand Down Expand Up @@ -182,12 +182,9 @@ impl AlgebraicExecutionTrace {
.unwrap()
}

pub fn append_hash_trace(
&mut self,
hash_permutation_trace: [[BFieldElement; tip5::STATE_SIZE]; PERMUTATION_TRACE_LENGTH],
) {
self.increase_lookup_multiplicities(hash_permutation_trace);
let mut hash_trace_addendum = HashTable::convert_to_hash_table_rows(hash_permutation_trace);
pub fn append_hash_trace(&mut self, trace: PermutationTrace) {
self.increase_lookup_multiplicities(trace);
let mut hash_trace_addendum = HashTable::convert_to_hash_table_rows(trace);
hash_trace_addendum
.slice_mut(s![.., CI.base_table_index()])
.fill(Instruction::Hash.opcode_b());
Expand Down Expand Up @@ -218,18 +215,13 @@ impl AlgebraicExecutionTrace {
}
}

fn append_sponge_trace(
&mut self,
instruction: Instruction,
hash_permutation_trace: [[BFieldElement; tip5::STATE_SIZE]; PERMUTATION_TRACE_LENGTH],
) {
fn append_sponge_trace(&mut self, instruction: Instruction, trace: PermutationTrace) {
assert!(matches!(
instruction,
Instruction::AbsorbInit | Instruction::Absorb | Instruction::Squeeze
));
self.increase_lookup_multiplicities(hash_permutation_trace);
let mut sponge_trace_addendum =
HashTable::convert_to_hash_table_rows(hash_permutation_trace);
self.increase_lookup_multiplicities(trace);
let mut sponge_trace_addendum = HashTable::convert_to_hash_table_rows(trace);
sponge_trace_addendum
.slice_mut(s![.., CI.base_table_index()])
.fill(instruction.opcode_b());
Expand All @@ -242,12 +234,9 @@ impl AlgebraicExecutionTrace {
/// - cascade table was looked up, and
/// - lookup table was looked up
/// and increases the multiplicities accordingly
fn increase_lookup_multiplicities(
&mut self,
hash_permutation_trace: [[BFieldElement; tip5::STATE_SIZE]; PERMUTATION_TRACE_LENGTH],
) {
fn increase_lookup_multiplicities(&mut self, trace: PermutationTrace) {
// The last row in the trace is the permutation's result: no lookups are performed for it.
let rows_for_which_lookups_are_performed = hash_permutation_trace.iter().dropping_back(1);
let rows_for_which_lookups_are_performed = trace.iter().dropping_back(1);
for row in rows_for_which_lookups_are_performed {
self.increase_lookup_multiplicities_for_row(row);
}
Expand Down
10 changes: 5 additions & 5 deletions triton-vm/src/table/hash_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ pub const FULL_WIDTH: usize = BASE_WIDTH + EXT_WIDTH;

pub const POWER_MAP_EXPONENT: u64 = 7;
pub const NUM_ROUND_CONSTANTS: usize = STATE_SIZE;
pub const PERMUTATION_TRACE_LENGTH: usize = NUM_ROUNDS + 1;

const PERMUTATION_TRACE_LENGTH: usize = NUM_ROUNDS + 1;
pub type PermutationTrace = [[BFieldElement; STATE_SIZE]; PERMUTATION_TRACE_LENGTH];

#[derive(Debug, Clone)]
pub struct HashTable {}
Expand Down Expand Up @@ -1394,12 +1396,10 @@ impl HashTable {
/// - adding the looked-up value for each limb.
///
/// The current instruction is not set.
pub fn convert_to_hash_table_rows(
hash_permutation_trace: [[BFieldElement; STATE_SIZE]; PERMUTATION_TRACE_LENGTH],
) -> Array2<BFieldElement> {
pub fn convert_to_hash_table_rows(trace: PermutationTrace) -> Array2<BFieldElement> {
let mut hash_trace_addendum = Array2::zeros([PERMUTATION_TRACE_LENGTH, BASE_WIDTH]);
for (round_number, mut row) in hash_trace_addendum.rows_mut().into_iter().enumerate() {
let trace_row = hash_permutation_trace[round_number];
let trace_row = trace[round_number];
row[RoundNumber.base_table_index()] = BFieldElement::from(round_number as u64);

let st_0_limbs = Self::base_field_element_into_16_bit_limbs(trace_row[0]);
Expand Down
7 changes: 2 additions & 5 deletions triton-vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::op_stack::OpStackElement::*;
use crate::op_stack::*;
use crate::program::*;
use crate::stark::StarkHasher;
use crate::table::hash_table::PERMUTATION_TRACE_LENGTH;
use crate::table::hash_table::PermutationTrace;
use crate::table::processor_table;
use crate::table::processor_table::ProcessorTraceRow;
use crate::table::table_column::*;
Expand Down Expand Up @@ -97,10 +97,7 @@ pub enum CoProcessorCall {
/// Trace of the state registers for hash coprocessor table when executing instruction `hash`
/// or any of the Sponge instructions `absorb_init`, `absorb`, `squeeze`.
/// One row per round in the Tip5 permutation.
Tip5Trace(
Instruction,
Box<[[BFieldElement; tip5::STATE_SIZE]; PERMUTATION_TRACE_LENGTH]>,
),
Tip5Trace(Instruction, Box<PermutationTrace>),

SingleU32TableEntry(U32TableEntry),

Expand Down

0 comments on commit 94899d8

Please sign in to comment.