Skip to content

Commit

Permalink
refactor: cleanup block hash table (#1333)
Browse files Browse the repository at this point in the history
  • Loading branch information
plafer committed May 28, 2024
1 parent 7e901ad commit ac9b07c
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 249 deletions.
21 changes: 18 additions & 3 deletions air/src/trace/main_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ use super::{
CHIPLETS_OFFSET, CLK_COL_IDX, CTX_COL_IDX, DECODER_TRACE_OFFSET, FMP_COL_IDX, FN_HASH_OFFSET,
STACK_TRACE_OFFSET,
};
use alloc::vec::Vec;
use core::ops::{Deref, Range};
use vm_core::{utils::range, Felt, ONE, ZERO};
use vm_core::{utils::range, Felt, Word, ONE, ZERO};

#[cfg(any(test, feature = "internals"))]
use alloc::vec::Vec;

// CONSTANTS
// ================================================================================================
Expand Down Expand Up @@ -107,14 +109,27 @@ impl MainTrace {
}

/// Returns the first half of the hasher state at row i.
pub fn decoder_hasher_state_first_half(&self, i: usize) -> [Felt; DIGEST_LEN] {
pub fn decoder_hasher_state_first_half(&self, i: usize) -> Word {
let mut state = [ZERO; DIGEST_LEN];
for (col, s) in state.iter_mut().enumerate() {
*s = self.columns.get_column(DECODER_TRACE_OFFSET + HASHER_STATE_OFFSET + col)[i];
}
state
}

/// Returns the second half of the hasher state at row i.
pub fn decoder_hasher_state_second_half(&self, i: usize) -> Word {
const SECOND_WORD_OFFSET: usize = 4;
let mut state = [ZERO; DIGEST_LEN];
for (col, s) in state.iter_mut().enumerate() {
*s = self
.columns
.get_column(DECODER_TRACE_OFFSET + HASHER_STATE_OFFSET + SECOND_WORD_OFFSET + col)
[i];
}
state
}

/// Returns a specific element from the hasher state at row i.
pub fn decoder_hasher_state_element(&self, element: usize, i: usize) -> Felt {
self.columns.get_column(DECODER_TRACE_OFFSET + HASHER_STATE_OFFSET + element)[i + 1]
Expand Down
18 changes: 9 additions & 9 deletions assembly/src/ast/instruction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ pub enum Instruction {
Gte,
IsOdd,

// ----- ext2 operations ----------------------------------------------------------------------
// ----- ext2 operations ---------------------------------------------------------------------
Ext2Add,
Ext2Sub,
Ext2Mul,
Ext2Div,
Ext2Neg,
Ext2Inv,

// ----- u32 manipulation ---------------------------------------------------------------------
// ----- u32 manipulation --------------------------------------------------------------------
U32Test,
U32TestW,
U32Assert,
Expand Down Expand Up @@ -125,7 +125,7 @@ pub enum Instruction {
U32Min,
U32Max,

// ----- stack manipulation -------------------------------------------------------------------
// ----- stack manipulation ------------------------------------------------------------------
Drop,
DropW,
PadW,
Expand Down Expand Up @@ -205,7 +205,7 @@ pub enum Instruction {
CDrop,
CDropW,

// ----- input / output operations ------------------------------------------------------------
// ----- input / output operations -----------------------------------------------------------
Push(ImmFelt),
PushU8(u8),
PushU16(u16),
Expand Down Expand Up @@ -243,7 +243,7 @@ pub enum Instruction {

AdvInject(AdviceInjectorNode),

// ----- cryptographic operations -------------------------------------------------------------
// ----- cryptographic operations ------------------------------------------------------------
Hash,
HMerge,
HPerm,
Expand All @@ -252,23 +252,23 @@ pub enum Instruction {
MTreeMerge,
MTreeVerify,

// ----- STARK proof verification -------------------------------------------------------------
// ----- STARK proof verification ------------------------------------------------------------
FriExt2Fold4,
RCombBase,

// ----- exec / call --------------------------------------------------------------------------
// ----- exec / call -------------------------------------------------------------------------
Exec(InvocationTarget),
Call(InvocationTarget),
SysCall(InvocationTarget),
DynExec,
DynCall,
ProcRef(InvocationTarget),

// ----- debug decorators ---------------------------------------------------------------------
// ----- debug decorators --------------------------------------------------------------------
Breakpoint,
Debug(DebugOptions),

// ----- event decorators ---------------------------------------------------------------------
// ----- event decorators --------------------------------------------------------------------
Emit(ImmU32),
Trace(ImmU32),
}
Expand Down
20 changes: 10 additions & 10 deletions assembly/src/ast/instruction/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ pub enum OpCode {
Gte,
IsOdd,

// ----- ext2 operations ----------------------------------------------------------------------
// ----- ext2 operations ---------------------------------------------------------------------
Ext2Add,
Ext2Sub,
Ext2Mul,
Ext2Div,
Ext2Neg,
Ext2Inv,

// ----- u32 manipulation ---------------------------------------------------------------------
// ----- u32 manipulation -------------------------------------------------------------------
U32Test,
U32TestW,
U32Assert,
Expand Down Expand Up @@ -116,7 +116,7 @@ pub enum OpCode {
U32Min,
U32Max,

// ----- stack manipulation -------------------------------------------------------------------
// ----- stack manipulation ------------------------------------------------------------------
Drop,
DropW,
PadW,
Expand Down Expand Up @@ -196,7 +196,7 @@ pub enum OpCode {
CDrop,
CDropW,

// ----- input / output operations ------------------------------------------------------------
// ----- input / output operations -----------------------------------------------------------
PushU8,
PushU16,
PushU32,
Expand Down Expand Up @@ -233,7 +233,7 @@ pub enum OpCode {

AdvInject,

// ----- cryptographic operations -------------------------------------------------------------
// ----- cryptographic operations ------------------------------------------------------------
Hash,
HMerge,
HPerm,
Expand All @@ -242,26 +242,26 @@ pub enum OpCode {
MTreeMerge,
MTreeVerify,

// ----- STARK proof verification -------------------------------------------------------------
// ----- STARK proof verification ------------------------------------------------------------
FriExt2Fold4,
RCombBase,

// ----- exec / call --------------------------------------------------------------------------
// ----- exec / call -------------------------------------------------------------------------
Exec,
Call,
SysCall,
DynExec,
DynCall,
ProcRef,

// ----- debugging ----------------------------------------------------------------------------
// ----- debugging ---------------------------------------------------------------------------
Debug,

// ----- event decorators ---------------------------------------------------------------------
// ----- event decorators --------------------------------------------------------------------
Emit,
Trace,

// ----- control flow -------------------------------------------------------------------------
// ----- control flow ------------------------------------------------------------------------
IfElse,
Repeat,
While,
Expand Down
18 changes: 9 additions & 9 deletions core/src/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use decorators::{
/// These operations take exactly one cycle to execute.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Operation {
// ----- system operations --------------------------------------------------------------------
// ----- system operations -------------------------------------------------------------------
/// Advances cycle counter, but does not change the state of user stack.
Noop,

Expand Down Expand Up @@ -43,7 +43,7 @@ pub enum Operation {
/// instruction.
Clk,

// ----- flow control operations --------------------------------------------------------------
// ----- flow control operations -------------------------------------------------------------
/// Marks the beginning of a join block.
Join,

Expand Down Expand Up @@ -79,7 +79,7 @@ pub enum Operation {
/// by the VM (HALT operation itself excepted).
Halt,

// ----- field operations ---------------------------------------------------------------------
// ----- field operations --------------------------------------------------------------------
/// Pops two elements off the stack, adds them, and pushes the result back onto the stack.
Add,

Expand Down Expand Up @@ -129,7 +129,7 @@ pub enum Operation {
///
/// The top 4 elements of the stack are expected to be arranged as follows (form the top):
/// - least significant bit of the exponent in the previous trace if there's an expacc call,
/// otherwise ZERO
/// otherwise ZERO
/// - exponent of base number `a` for this turn
/// - accumulated power of base number `a` so far
/// - number which needs to be shifted to the right
Expand All @@ -139,13 +139,13 @@ pub enum Operation {
/// shifted to the right by one bit.
Expacc,

// ----- ext2 operations ----------------------------------------------------------------------
// ----- ext2 operations ---------------------------------------------------------------------
/// Computes the product of two elements in the extension field of degree 2 and pushes the
/// result back onto the stack as the third and fourth elements. Pushes 0 onto the stack as
/// the first and second elements.
Ext2Mul,

// ----- u32 operations -----------------------------------------------------------------------
// ----- u32 operations ----------------------------------------------------------------------
/// Pops an element off the stack, splits it into upper and lower 32-bit values, and pushes
/// these values back onto the stack.
U32split,
Expand Down Expand Up @@ -210,7 +210,7 @@ pub enum Operation {
/// If either of the elements is greater than or equal to 2^32, execution fails.
U32xor,

// ----- stack manipulation -------------------------------------------------------------------
// ----- stack manipulation ------------------------------------------------------------------
/// Pushes 0 onto the stack.
Pad,

Expand Down Expand Up @@ -326,7 +326,7 @@ pub enum Operation {
/// If the popped element is neither 0 nor 1, execution fails.
CSwapW,

// ----- input / output -----------------------------------------------------------------------
// ----- input / output ----------------------------------------------------------------------
/// Pushes the immediate value onto the stack.
Push(Felt),

Expand Down Expand Up @@ -380,7 +380,7 @@ pub enum Operation {
/// - All other stack elements remain the same.
Pipe,

// ----- cryptographic operations -------------------------------------------------------------
// ----- cryptographic operations ------------------------------------------------------------
/// Performs a Rescue Prime Optimized permutation on the top 3 words of the operand stack,
/// where the top 2 words are the rate (words C and B), the deepest word is the capacity (word
/// A), and the digest output is the middle word E.
Expand Down
Loading

0 comments on commit ac9b07c

Please sign in to comment.