Skip to content

Commit 023bf36

Browse files
committed
vm core: simplify poseidon16 logic
1 parent f7be7f6 commit 023bf36

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

crates/leanVm/src/core.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use p3_field::{Field, PrimeCharacteristicRing, PrimeField64};
1+
use p3_field::{Field, PrimeField64};
22
use p3_symmetric::Permutation;
33

44
use crate::{
@@ -313,10 +313,12 @@ impl<PERM16, PERM24> VirtualMachine<PERM16, PERM24> {
313313

314314
/// Executes the `Poseidon2_16` precompile instruction.
315315
///
316-
/// This function orchestrates a Poseidon2 permutation over 16 field elements,
317-
/// as defined by the zkVM's ISA. It reads two 8-element input vectors and
318-
/// writes two 8-element output vectors using pointers stored relative to the
319-
/// frame pointer (`fp`).
316+
/// Reads four pointers from memory starting at `fp + shift`, representing:
317+
/// - two input vector addresses (`ptr_in_0`, `ptr_in_1`)
318+
/// - two output vector addresses (`ptr_out_0`, `ptr_out_1`)
319+
///
320+
/// Each input is an 8-element vector of `F`. The two inputs are concatenated,
321+
/// permuted using Poseidon2, and written back to the output locations.
320322
///
321323
/// The operation is: `Poseidon2(m_vec[ptr_0], m_vec[ptr_1]) -> (m_vec[ptr_2], m_vec[ptr_3])`
322324
fn execute_poseidon2_16(&mut self, shift: usize) -> Result<(), VirtualMachineError>
@@ -338,30 +340,30 @@ impl<PERM16, PERM24> VirtualMachine<PERM16, PERM24> {
338340
// Read Input Vectors
339341
//
340342
// Read the 8-element vectors from the locations pointed to by `ptr_arg_0` and `ptr_arg_1`.
341-
let arg0: [MemoryValue; DIMENSION] = self.memory_manager.get_array(ptr_arg_0)?;
342-
let arg1: [MemoryValue; DIMENSION] = self.memory_manager.get_array(ptr_arg_1)?;
343+
let arg0 = self
344+
.memory_manager
345+
.memory
346+
.get_array_as::<F, DIMENSION>(ptr_arg_0)?;
347+
let arg1 = self
348+
.memory_manager
349+
.memory
350+
.get_array_as::<F, DIMENSION>(ptr_arg_1)?;
343351

344352
// Perform Hashing
345353
//
346354
// Concatenate the two input vectors into a single 16-element array for the permutation.
347-
let mut state = [MemoryValue::default(); DIMENSION * 2];
355+
let mut state = [F::default(); DIMENSION * 2];
348356
state[..DIMENSION].copy_from_slice(&arg0);
349357
state[DIMENSION..].copy_from_slice(&arg1);
350358

351-
// Convert the state to an array of field
352-
let mut state_f: [F; DIMENSION * 2] = [F::ZERO; DIMENSION * 2];
353-
for i in 0..DIMENSION {
354-
state_f[i] = state[i].try_into()?;
355-
}
356-
357359
// Apply the Poseidon2 permutation to the state.
358-
self.poseidon2_16.permute_mut(&mut state_f);
360+
self.poseidon2_16.permute_mut(&mut state);
359361

360362
// Write Output Vectors
361363
//
362364
// Split the permuted state back into two 8-element output vectors.
363-
let res0: [F; DIMENSION] = state_f[..DIMENSION].try_into().unwrap();
364-
let res1: [F; DIMENSION] = state_f[DIMENSION..].try_into().unwrap();
365+
let res0: [F; DIMENSION] = state[..DIMENSION].try_into().unwrap();
366+
let res1: [F; DIMENSION] = state[DIMENSION..].try_into().unwrap();
365367

366368
// Write the output vectors to the memory locations pointed to by `ptr_res_0` and `ptr_res_1`.
367369
self.memory_manager.load_data(ptr_res_0, &res0)?;

0 commit comments

Comments
 (0)