Skip to content

Commit

Permalink
ecall-keccak: test memory records
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien Nicolas committed Dec 9, 2024
1 parent 44972e1 commit d854fa5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ceno_emul/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ impl StepRecord {
pub fn is_busy_loop(&self) -> bool {
self.pc.before == self.pc.after
}

pub fn syscall(&self) -> Option<&SyscallWitness> {
self.syscall.as_ref()
}
}

#[derive(Debug)]
Expand Down
32 changes: 28 additions & 4 deletions ceno_emul/tests/test_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,43 @@ fn test_ceno_rt_io() -> Result<()> {
fn test_ceno_rt_keccak() -> Result<()> {
let program_elf = ceno_examples::ceno_rt_keccak;
let mut state = VMState::new_from_elf(unsafe_platform(), program_elf)?;
let _steps = run(&mut state)?;
let steps = run(&mut state)?;

// Expect the program to have written successive states between Keccak permutations.
const ITERATIONS: usize = 3;
let keccak_outs = sample_keccak_f(ITERATIONS);

let all_messages = read_all_messages(&state);
for (got, expect) in izip!(&all_messages, keccak_outs) {
assert_eq!(all_messages.len(), ITERATIONS);
for (got, expect) in izip!(&all_messages, &keccak_outs) {
let got = got
.chunks_exact(8)
.map(|chunk| u64::from_le_bytes(chunk.try_into().unwrap()))
.collect_vec();
assert_eq!(&got, expect);
}

// Find the syscall records.
let syscalls = steps.iter().filter_map(|step| step.syscall()).collect_vec();
assert_eq!(syscalls.len(), ITERATIONS);

// Check the syscall effects.
for (witness, expect) in izip!(syscalls, keccak_outs) {
assert_eq!(witness.mem_writes.len(), expect.len() * 2);

let got = witness
.mem_writes
.chunks_exact(2)
.map(|write_ops| {
assert_eq!(
write_ops[1].addr.baddr(),
write_ops[0].addr.baddr() + WORD_SIZE as u32
);
let lo = write_ops[0].value.after as u64;
let hi = write_ops[1].value.after as u64;
lo | (hi << 32)
})
.collect_vec();
assert_eq!(got, expect);
}

Expand All @@ -103,8 +128,7 @@ fn unsafe_platform() -> Platform {
}

fn sample_keccak_f(count: usize) -> Vec<Vec<u64>> {
let input = [0_u64; 25];
let mut state = input.clone();
let mut state = [0_u64; 25];

(0..count)
.map(|_| {
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/ceno_rt_keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#![no_std]
extern crate ceno_rt;
use ceno_rt::{info_out, syscall_keccak_permute};
use core::{ptr::read_volatile, slice};
use core::slice;

const ITERATIONS: usize = 3;

Expand All @@ -22,7 +22,7 @@ fn main() {

fn log_state(state: &[u64; 25]) {
let out = unsafe {
slice::from_raw_parts_mut(state.as_ptr() as *mut u8, state.len() * size_of::<u64>())
slice::from_raw_parts(state.as_ptr() as *const u8, state.len() * size_of::<u64>())
};
info_out().write_frame(out);
}

0 comments on commit d854fa5

Please sign in to comment.