Skip to content

Commit

Permalink
fix: fail Sponge instructions if Sponge state is uninitialized
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Dec 1, 2023
1 parent d7fbb3f commit 881b6c0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
3 changes: 3 additions & 0 deletions triton-vm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ pub enum InstructionError {
#[error("division by 0 is impossible")]
DivisionByZero,

#[error("the Sponge state must be initialized before it can be used")]
SpongeNotInitialized,

#[error("the logarithm of 0 does not exist")]
LogarithmOfZero,

Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ mod tests {
) -> (Program, usize) {
let num_push_instructions = 10;
let push_instructions = triton_asm![push 1; num_push_instructions];
let program = triton_program!({&push_instructions} {test_instruction} nop halt);
let program = triton_program!(sponge_init {&push_instructions} {test_instruction} nop halt);

let stack_size_when_reaching_test_instruction =
NUM_OP_STACK_REGISTERS + num_push_instructions;
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/table/processor_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3396,7 +3396,7 @@ pub(crate) mod tests {
#[test]
/// helps identifying whether the printing causes an infinite loop
fn print_simple_processor_table_row() {
let program = triton_program!(push 2 assert halt);
let program = triton_program!(push 2 sponge_init assert halt);
let err = program.run([].into(), [].into()).unwrap_err();
println!("\n{}", err.vm_state);
}
Expand Down
32 changes: 19 additions & 13 deletions triton-vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct VMState {
/// Sponge state.
/// Note that this is the _full_ state, including capacity. The capacity should never be
/// exposed outside of the VM.
pub sponge_state: [BFieldElement; tip5::STATE_SIZE],
pub sponge_state: Option<[BFieldElement; tip5::STATE_SIZE]>,

/// Indicates whether the terminating instruction `halt` has been executed.
pub halting: bool,
Expand Down Expand Up @@ -453,18 +453,19 @@ impl VMState {
}

fn sponge_init(&mut self) -> Vec<CoProcessorCall> {
self.sponge_state = Tip5::init().state;
self.sponge_state = Some(Tip5::init().state);
self.instruction_pointer += 1;
vec![SpongeStateReset]
}

fn sponge_absorb(&mut self) -> Result<Vec<CoProcessorCall>> {
let Some(mut state) = self.sponge_state else {
return Err(SpongeNotInitialized);
};
let to_absorb = self.op_stack.pop_multiple::<{ tip5::RATE }>()?;
self.sponge_state[..tip5::RATE].copy_from_slice(&to_absorb);
let tip5_trace = Tip5::trace(&mut Tip5State {
state: self.sponge_state,
});
self.sponge_state = tip5_trace.last().unwrap().to_owned();
state[..tip5::RATE].copy_from_slice(&to_absorb);
let tip5_trace = Tip5::trace(&mut Tip5State { state });
self.sponge_state = Some(tip5_trace.last().unwrap().to_owned());

let co_processor_calls = vec![Tip5Trace(SpongeAbsorb, Box::new(tip5_trace))];

Expand All @@ -473,13 +474,14 @@ impl VMState {
}

fn sponge_squeeze(&mut self) -> Result<Vec<CoProcessorCall>> {
let Some(state) = self.sponge_state else {
return Err(SpongeNotInitialized);
};
for i in (0..tip5::RATE).rev() {
self.op_stack.push(self.sponge_state[i]);
self.op_stack.push(state[i]);
}
let tip5_trace = Tip5::trace(&mut Tip5State {
state: self.sponge_state,
});
self.sponge_state = tip5_trace.last().unwrap().to_owned();
let tip5_trace = Tip5::trace(&mut Tip5State { state });
self.sponge_state = Some(tip5_trace.last().unwrap().to_owned());

let co_processor_calls = vec![Tip5Trace(SpongeSqueeze, Box::new(tip5_trace))];

Expand Down Expand Up @@ -973,7 +975,11 @@ impl Display for VMState {
.join(" | ");
print_row(f, format!("ib6-0: [ {ib_registers} ]",))?;

let sponge_state_register = |i: usize| self.sponge_state[i].value();
let Some(sponge_state) = self.sponge_state else {
return writeln!(f, "╰─{:─<total_width$}─╯", "");
};

let sponge_state_register = |i: usize| sponge_state[i].value();
let sponge_state_slice = |idxs: [usize; 4]| {
idxs.map(sponge_state_register)
.map(|ss| format!("{ss:>register_width$}"))
Expand Down

0 comments on commit 881b6c0

Please sign in to comment.