Skip to content

Commit

Permalink
succinct patch sha3
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstam committed Jul 11, 2024
1 parent 026b0e8 commit fbb02da
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
7 changes: 7 additions & 0 deletions sha3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ use digest::{
mod macros;
mod state;

#[cfg(all(
target_os = "zkvm",
target_vendor = "succinct",
target_arch = "riscv32"
))]
pub mod succinct;

use crate::state::Sha3State;

// Paddings
Expand Down
27 changes: 25 additions & 2 deletions sha3/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
const PLEN: usize = 25;
const DEFAULT_ROUND_COUNT: usize = 24;

#[cfg(all(
target_os = "zkvm",
target_vendor = "succinct",
target_arch = "riscv32"
))]
use crate::succinct;

#[derive(Clone)]
pub(crate) struct Sha3State {
pub state: [u64; PLEN],
Expand Down Expand Up @@ -46,7 +53,7 @@ impl Sha3State {
*s ^= u64::from_le_bytes(b.try_into().unwrap());
}

keccak::p1600(&mut self.state, self.round_count);
self.permute();
}

#[inline(always)]
Expand All @@ -58,6 +65,22 @@ impl Sha3State {

#[inline(always)]
pub(crate) fn permute(&mut self) {
keccak::p1600(&mut self.state, self.round_count);
#[cfg(all(
target_os = "zkvm",
target_vendor = "succinct",
target_arch = "riscv32"
))]
{
succinct::keccak_permute(&mut self.state);
}

#[cfg(not(all(
target_os = "zkvm",
target_vendor = "succinct",
target_arch = "riscv32"
)))]
{
keccak::p1600(&mut self.state, self.round_count);
}
}
}
10 changes: 10 additions & 0 deletions sha3/src/succinct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extern "C" {
fn syscall_keccak_permute(state: *mut u64);
}

#[inline]
pub(crate) fn keccak_permute(state: &mut [u64; 25]) {
unsafe {
syscall_keccak_permute(state.as_mut_ptr());
}
}

0 comments on commit fbb02da

Please sign in to comment.