Skip to content

Commit 07c0716

Browse files
authored
fix: update ascon to 0.5 (#206)
ascon 0.5 removed the State struct's byte accessors (State::from(&[u8; 40]) and as_bytes); State is now a plain [u64; 5] and the permutation is exposed as free functions like permute12. The Ascon12 permutation now converts between bytes and words explicitly with u64::from_le_bytes/to_le_bytes, mirroring the KeccakF1600 module, and a known-answer test pins the little-endian state layout on any target. Supersedes #135 while preserving its little-endian byte-order handling, without the raw pointer copies.
1 parent 1e891be commit 07c0716

3 files changed

Lines changed: 78 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spongefish/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ark-ec = { workspace = true, optional = true }
5353
# arkworks-rs
5454
ark-ff = { workspace = true, optional = true }
5555
ark-serialize = { workspace = true, optional = true }
56-
ascon = { version = "^0.4.0", optional = true }
56+
ascon = { version = "^0.5.0", optional = true }
5757
blake2 = { workspace = true, optional = true }
5858
blake3 = { workspace = true, optional = true }
5959

spongefish/src/instantiations/permutations.rs

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,87 @@ pub use keccak::KeccakF1600;
55

66
#[cfg(feature = "ascon")]
77
mod ascon {
8+
use crate::duplex_sponge::Permutation;
9+
10+
const STATE_BYTES: usize = 40;
11+
const WORD_BYTES: usize = 8;
12+
const _: () = assert!(STATE_BYTES == core::mem::size_of::<ascon::State>());
813

14+
/// Ascon permutation internal state: 5 64-bit words,
15+
/// or equivalently 40 bytes in little-endian order.
916
#[derive(Clone, Debug, Default)]
1017
pub struct Ascon12;
11-
use crate::duplex_sponge::Permutation;
1218

13-
impl Permutation<40> for Ascon12 {
19+
impl Permutation<STATE_BYTES> for Ascon12 {
1420
type U = u8;
1521

16-
fn permute(&self, state: &[u8; 40]) -> [u8; 40] {
17-
let mut state = ascon::State::from(state);
18-
state.permute_12();
19-
state.as_bytes()
22+
fn permute(&self, state: &[u8; STATE_BYTES]) -> [u8; STATE_BYTES] {
23+
let mut new_state = *state;
24+
self.permute_mut(&mut new_state);
25+
new_state
26+
}
27+
28+
fn permute_mut(&self, state: &mut [u8; STATE_BYTES]) {
29+
let mut words = bytes_to_words(state);
30+
ascon::permute12(&mut words);
31+
words_to_bytes(&words, state);
32+
}
33+
}
34+
35+
fn bytes_to_words(state: &[u8; STATE_BYTES]) -> ascon::State {
36+
core::array::from_fn(|i| {
37+
let start = i * WORD_BYTES;
38+
let mut word = [0; WORD_BYTES];
39+
word.copy_from_slice(&state[start..start + WORD_BYTES]);
40+
u64::from_le_bytes(word)
41+
})
42+
}
43+
44+
fn words_to_bytes(words: &ascon::State, state: &mut [u8; STATE_BYTES]) {
45+
for (chunk, word) in state.as_chunks_mut::<WORD_BYTES>().0.iter_mut().zip(words) {
46+
chunk.copy_from_slice(&word.to_le_bytes());
47+
}
48+
}
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use super::*;
53+
54+
/// Serialize words in little-endian order, independently of the
55+
/// conversion functions under test.
56+
fn serialize_le(words: &ascon::State) -> [u8; STATE_BYTES] {
57+
let mut bytes = [0u8; STATE_BYTES];
58+
for (chunk, word) in bytes.as_chunks_mut::<WORD_BYTES>().0.iter_mut().zip(words) {
59+
chunk.copy_from_slice(&word.to_le_bytes());
60+
}
61+
bytes
62+
}
63+
64+
/// Known-answer test for the permutation, using the word-level vectors
65+
/// from the `ascon` crate's own test suite. Serializing the words with
66+
/// an explicit byte order pins the state layout to little-endian
67+
/// independently of the target's native endianness.
68+
#[test]
69+
fn ascon12_little_endian_known_answer() {
70+
let input_words: ascon::State = [
71+
0x0123_4567_89ab_cdef,
72+
0xef01_2345_6789_abcd,
73+
0xcdef_0123_4567_89ab,
74+
0xabcd_ef01_2345_6789,
75+
0x89ab_cdef_0123_4567,
76+
];
77+
let output_words: ascon::State = [
78+
0x2064_16df_c624_bb14,
79+
0x1b0c_47a6_0105_8aab,
80+
0x8934_cfc9_3814_cddd,
81+
0xa973_8d28_7a74_8e4b,
82+
0xddd9_34f0_58af_c7e1,
83+
];
84+
85+
let input = serialize_le(&input_words);
86+
let expected = serialize_le(&output_words);
87+
88+
assert_eq!(Ascon12.permute(&input), expected);
2089
}
2190
}
2291
}

0 commit comments

Comments
 (0)