Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ecall demo with Keccak-f #717

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

naure
Copy link
Collaborator

@naure naure commented Dec 9, 2024

  • Expand StepRecord to support multiple accesses to registers and memory (beyond standard instructions).
  • Introduce a module for syscall witness generation. See fn handle_syscall(…)
  • Witness generation for Keccak-f. Same format as sp1.
  • Runtime wrapper function syscall_keccak_permute.
  • Test program using Keccak-f and concrete values.

This was linked to issues Dec 10, 2024
@naure naure requested a review from lispc December 10, 2024 11:23
// Compute Keccak permutation.
let output = {
let mut state = [0_u64; KECCAK_CELLS];
izip!(state.iter_mut(), input.chunks_exact(2)).for_each(|(cell, chunk)| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff --git a/ceno_emul/src/syscalls/keccak_permute.rs b/ceno_emul/src/syscalls/keccak_permute.rs
index 05c04c87..c3ba4add 100644
--- a/ceno_emul/src/syscalls/keccak_permute.rs
+++ b/ceno_emul/src/syscalls/keccak_permute.rs
@@ -39,11 +39,9 @@ pub fn keccak_permute(vm: &VMState) -> SyscallEffects {
     // Compute Keccak permutation.
     let output = {
         let mut state = [0_u64; KECCAK_CELLS];
-        izip!(state.iter_mut(), input.chunks_exact(2)).for_each(|(cell, chunk)| {
-            let lo = chunk[0] as u64;
-            let hi = chunk[1] as u64;
-            *cell = lo | hi << 32;
-        });
+        for (cell, (&lo, &hi)) in izip!(&mut state, input.iter().tuples()) {
+            *cell = lo as u64 | (hi as u64) << 32;
+        }
         keccakf(&mut state);
         state.into_iter().flat_map(|c| [c as u32, (c >> 32) as u32])
     };

This might be slightly easier to read.

}
Err(err) if self.platform.unsafe_ecall_nop => {
tracing::warn!("ecall ignored with unsafe_ecall_nop: {:?}", err);
// TODO: remove this example.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a TODO for this PR, or for later?

);
let lo = write_ops[0].value.after as u64;
let hi = write_ops[1].value.after as u64;
lo | (hi << 32)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are doing the conversion from two u32 to one one u64 and back quite a few times in our code base. Perhaps we should write to helper functions?

@@ -0,0 +1,24 @@
// Based on https://github.com/succinctlabs/sp1/blob/013c24ea2fa15a0e7ed94f7d11a7ada4baa39ab9/crates/zkvm/entrypoint/src/syscalls/keccak_permute.rs

const KECCAK_PERMUTE: u32 = 0x00_01_01_09;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this magic number come from?

Copy link
Collaborator

@matthiasgoergens matthiasgoergens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, please see comments.


mod keccak_permute;

pub const KECCAK_PERMUTE: u32 = 0x00_01_01_09;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider setting up a syscall numbers table in a common crate that both guest and host code can import.

(There's a few more such common data and structures that we might want to put in this simple crate.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Precompile Trace Generation. Precompile Architecture
2 participants