Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spongefish/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ark-ec = { workspace = true, optional = true }
# arkworks-rs
ark-ff = { workspace = true, optional = true }
ark-serialize = { workspace = true, optional = true }
ascon = { version = "^0.4.0", optional = true }
ascon = { version = "^0.5.0", optional = true }
blake2 = { workspace = true, optional = true }
blake3 = { workspace = true, optional = true }

Expand Down
68 changes: 44 additions & 24 deletions spongefish/src/instantiations/permutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,41 @@ pub use ascon::Ascon12;
#[cfg(feature = "keccak")]
pub use keccak::KeccakF1600;

#[cfg(any(feature = "ascon", feature = "keccak"))]
const _: () = assert!(cfg!(target_endian = "little"));

#[cfg(feature = "ascon")]
mod ascon {
use crate::duplex_sponge::Permutation;
use core::ptr;

#[derive(Clone, Debug, Default)]
pub struct Ascon12;
use crate::duplex_sponge::Permutation;

impl Permutation<40> for Ascon12 {
type U = u8;

fn permute(&self, state: &[u8; 40]) -> [u8; 40] {
let mut state = ascon::State::from(state);
state.permute_12();
state.as_bytes()
let mut words = [0u64; 5];
unsafe {
ptr::copy_nonoverlapping(
state.as_ptr(),
words.as_mut_ptr().cast::<u8>(),
state.len(),
);
}

ascon::permute12(&mut words);

let mut new_state = [0u8; 40];
unsafe {
ptr::copy_nonoverlapping(
words.as_ptr().cast::<u8>(),
new_state.as_mut_ptr(),
new_state.len(),
);
}
new_state
}
}
}
Expand Down Expand Up @@ -46,29 +67,28 @@ mod keccak {
new_state
}

fn permute_mut(&self, state: &mut [u8; STATE_BYTES]) {
let mut words = bytes_to_words(state);
fn permute_mut(&self, state: &mut [u8; 200]) {
let mut words = [0u64; 25];
unsafe {
ptr::copy_nonoverlapping(
state.as_ptr(),
words.as_mut_ptr().cast::<u8>(),
state.len(),
);
}

f1600(&mut words);
words_to_bytes(&words, state);
}
}

unsafe {
ptr::copy_nonoverlapping(
words.as_ptr().cast::<u8>(),
state.as_mut_ptr(),
state.len(),
);
}
}

fn f1600(state: &mut State1600) {
Keccak::new().with_f1600(|f1600| f1600(state));
}

fn bytes_to_words(state: &[u8; STATE_BYTES]) -> State1600 {
core::array::from_fn(|i| {
let start = i * WORD_BYTES;
let mut word = [0; WORD_BYTES];
word.copy_from_slice(&state[start..start + WORD_BYTES]);
u64::from_le_bytes(word)
})
}

fn words_to_bytes(words: &State1600, state: &mut [u8; STATE_BYTES]) {
for (chunk, word) in state.chunks_exact_mut(WORD_BYTES).zip(words) {
chunk.copy_from_slice(&word.to_le_bytes());
}
}
}
Loading