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

Keccak Rust Wrappers #1337

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open

Keccak Rust Wrappers #1337

wants to merge 25 commits into from

Conversation

qwang98
Copy link
Collaborator

@qwang98 qwang98 commented Apr 30, 2024

Input for syscall is one memory pointer to the state array of 25 gl fe. Output is calculated in place.

All functions outside of keccakf are executed in Rust. Might need to delete everything except keccakf from keccakf.asm (including the padding, updating, and byte <-> u64 conversions).

Not tested. Should I wait till all infrastructure needed are merged? Technically can also do the following for the machine and I think I can test it already?

pol commit input_state[25];
pol commit output_state[25];
operation keccakf_permutation<0> input_state[0], input_state[1], ..., input_state[24] -> output_state[0], output_state[1], ..., output_state[24]
array::zip(input_state, output_state, |input_state, output_state| (keccakf(input_state) - output_state = 0);

@qwang98 qwang98 requested a review from chriseth April 30, 2024 21:40
riscv/src/runtime.rs Outdated Show resolved Hide resolved
test_data/asm/keccakf.asm Outdated Show resolved Hide resolved
riscv/src/runtime.rs Outdated Show resolved Hide resolved
test_data/asm/keccakf.asm Outdated Show resolved Hide resolved
riscv/src/runtime.rs Outdated Show resolved Hide resolved
riscv/src/runtime.rs Outdated Show resolved Hide resolved
riscv/src/runtime.rs Outdated Show resolved Hide resolved
riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
riscv-runtime/src/hash.rs Show resolved Hide resolved
riscv/src/runtime.rs Outdated Show resolved Hide resolved
@qwang98
Copy link
Collaborator Author

qwang98 commented May 6, 2024

Ready for review again.

riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
riscv/src/runtime.rs Outdated Show resolved Hide resolved
riscv/src/runtime.rs Outdated Show resolved Hide resolved
std/machines/hash/keccakf.asm Outdated Show resolved Hide resolved
let x;
let y;
operation keccakf x -> y;
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Copy link
Collaborator

@georgwiese georgwiese left a comment

Choose a reason for hiding this comment

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

Very cool! :)

riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
std/machines/hash/keccakf.asm Outdated Show resolved Hide resolved
riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
@qwang98
Copy link
Collaborator Author

qwang98 commented May 14, 2024

Implement all comments. Ready for a final review.

Changes:

  1. Rust wrapper takes two memory pointers and uses reinterpret casting.
  2. Rust wrapper tested correct using tiny keccak instead of unfinished syscall, which can't be done yet.

Tested using: cargo run -r --bin powdr-rs compile riscv/tests/riscv_data/keccak -o output && RUST_BACKTRACE=full cargo test --release --package powdr-riscv --test riscv -- keccak --exact --show-output --ignored

test_data/asm/keccakf.asm Outdated Show resolved Hide resolved
riscv/src/runtime.rs Show resolved Hide resolved
std/machines/hash/keccakf.asm Outdated Show resolved Hide resolved
test_data/asm/keccakf.asm Outdated Show resolved Hide resolved
riscv/tests/riscv_data/keccak/src/lib.rs Outdated Show resolved Hide resolved
riscv/tests/riscv_data/keccak/src/lib.rs Outdated Show resolved Hide resolved
@@ -11,6 +11,7 @@ repository = "https://github.com/powdr-labs/powdr"
serde = { version = "1.0", default-features = false, features = ["alloc", "derive", "rc"] }
serde_cbor = { version = "0.11.2", default-features = false, features = ["alloc"] }
powdr-riscv-syscalls = { path = "../riscv-syscalls" }
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
Copy link
Collaborator

Choose a reason for hiding this comment

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

not sure we want this here... i think the suggestion for tiny-keccak was mostly so it was possible to check things were proper without having the asm side. @leonardoalt?

Copy link
Collaborator Author

@qwang98 qwang98 May 15, 2024

Choose a reason for hiding this comment

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

Exactly. Still prefer to have it here or we need to remove the test coverage in riscv.rs entirely.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this should be here. Why do we need this dependency here for tests?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is because currently the keccak.asm isn't ready and depends on Chris's circuit compiler #1218 to be merged first, so we put a placeholder keccak.asm. To test the Rust wrapper, we instead use the tiny-keccak.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm removing this now and leaving the keccakf Rust wrapper as unimplemented.

// asm!("ecall", in("a0") input, in("a1") output, in("t0") u32::from(Syscall::KeccakF));
// }

// TODO: delete the following testing only chunk which uses tiny_keccak once syscall implemented
Copy link
Collaborator

Choose a reason for hiding this comment

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

and this would be just unimplemented for now

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

and this would be just unimplemented for now

I think the tiny keccak part is still needed just for the test in riscv.rs to pass.

Copy link
Member

Choose a reason for hiding this comment

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

which test is that? The keccak test already imports tiny keccak itself, we shouldn't ship tiny keccak with the riscv-runtime crate

Copy link
Collaborator Author

@qwang98 qwang98 May 29, 2024

Choose a reason for hiding this comment

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

It's the test using our Rust wrapper (rather than tiny keccak) here https://github.com/powdr-labs/powdr/blob/main/riscv/tests/riscv_data/keccak/src/lib.rs, which I'm now moving to a separate draft PR. I'm also leaving this as unimplemented! for now, and the contents here are also moved to the same draft PR.

riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
@qwang98
Copy link
Collaborator Author

qwang98 commented May 15, 2024

All comments addressed and ready for review again. @pacheco

@qwang98 qwang98 requested a review from pacheco May 15, 2024 15:13
.gitignore Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
@@ -11,6 +11,7 @@ repository = "https://github.com/powdr-labs/powdr"
serde = { version = "1.0", default-features = false, features = ["alloc", "derive", "rc"] }
serde_cbor = { version = "0.11.2", default-features = false, features = ["alloc"] }
powdr-riscv-syscalls = { path = "../riscv-syscalls" }
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this should be here. Why do we need this dependency here for tests?

riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
// asm!("ecall", in("a0") input, in("a1") output, in("t0") u32::from(Syscall::KeccakF));
// }

// TODO: delete the following testing only chunk which uses tiny_keccak once syscall implemented
Copy link
Member

Choose a reason for hiding this comment

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

which test is that? The keccak test already imports tiny keccak itself, we shouldn't ship tiny keccak with the riscv-runtime crate

riscv-runtime/src/hash.rs Outdated Show resolved Hide resolved
riscv/src/runtime.rs Show resolved Hide resolved
@@ -1,19 +1,32 @@
#![no_std]

use tiny_keccak::{Hasher, Keccak};
extern crate alloc;
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this test should be modified like this, I think we should add a new test instead. It's still good to have a test that uses native tiny keccak.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Alrighty. I'm restoring this file entirely and removed all tests using our Rust wrapper because it still depends on keccak.asm which is road blocked by Chris' circuit compiler.

I'm moving the tests for Rust wrapper to another draft PR, so that we can merge after all road blocks are cleared.

@qwang98
Copy link
Collaborator Author

qwang98 commented May 29, 2024

Split roadblocked contents to #1408. This PR is ready for a final review. @leonardoalt

@qwang98 qwang98 requested a review from leonardoalt May 30, 2024 00:38
@@ -8,5 +8,6 @@ edition = "2021"
[dependencies]
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
powdr-riscv-runtime = { path = "../../../../riscv-runtime" }
hex-literal = "0.3.1"
Copy link
Member

Choose a reason for hiding this comment

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

I think this can also go?

Copy link
Member

@leonardoalt leonardoalt left a comment

Choose a reason for hiding this comment

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

Sorry for being pedantic, but I think we could still add another riscv test (like a sibling of riscv/tests/riscv_data/keccak) that does call the new syscall but has a dummy assert, just so that's "ready to go"

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.

None yet

5 participants