Skip to content

Commit 661c9ef

Browse files
committed
add tests
1 parent 075f14c commit 661c9ef

5 files changed

Lines changed: 157 additions & 3 deletions

File tree

Cargo.lock

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

sponge-cursor/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ description = "Cursor for sponge-based absorption and squeezing"
1616
zeroize = { version = "1.4", optional = true, default-features = false }
1717

1818
[dev-dependencies]
19-
hex-literal = "1"
19+
keccak = "0.2"
2020

2121
[lints]
2222
workspace = true

sponge-cursor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod u64_le_utils;
1313
///
1414
/// `RATE` MUST be smaller than `256`, trying to initialize cursor with an invalid rate will
1515
/// result in a compilation error.
16-
#[derive(Debug, Clone)]
16+
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
1717
pub struct SpongeCursor<const RATE: usize> {
1818
pos: u8,
1919
}
500 Bytes
Binary file not shown.

sponge-cursor/tests/mod.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,136 @@
11
//! Basic tests
2+
use keccak::{Keccak, State1600};
3+
use sponge_cursor::SpongeCursor;
4+
5+
const DATA_LEN: usize = 500;
6+
const CHUNK_SIZES: &[usize] = &[1, 17, 133, 139, 203, 300];
7+
8+
static SHORT_DATA: [u8; 12] = *b"hello world!";
9+
10+
#[allow(clippy::cast_possible_truncation)]
11+
static DATA: [u8; DATA_LEN] = {
12+
let mut buf = [0u8; DATA_LEN];
13+
let mut i = 0;
14+
while i < buf.len() {
15+
buf[i] = (i % 217) as u8;
16+
i += 1;
17+
}
18+
buf
19+
};
20+
21+
static CONCAT_DATA: [u8; SHORT_DATA.len() + DATA_LEN] = {
22+
let mut buf = [0u8; SHORT_DATA.len() + DATA_LEN];
23+
// TODO(MSRV-1.87): use `copy_from_slice`
24+
let mut i = 0;
25+
while i < buf.len() {
26+
buf[i] = if i < SHORT_DATA.len() {
27+
SHORT_DATA[i]
28+
} else {
29+
DATA[i - SHORT_DATA.len()]
30+
};
31+
i += 1;
32+
}
33+
buf
34+
};
35+
36+
static STATE1: State1600 = [
37+
0x9C40D618E6055156,
38+
0x44755CBBEAEBC6F9,
39+
0xF903B381412808D8,
40+
0x0339BEC23E8A90AB,
41+
0xF32F96B8456CB8AF,
42+
0xCE6349D647F4BE3C,
43+
0x1AA37FEB280A570F,
44+
0x99233F523925282C,
45+
0x835AA18F6EEA40E5,
46+
0xAEEFA72F28932C4C,
47+
0x8A602BA780C75936,
48+
0x16E389AAA33064F0,
49+
0xB2889A3A30E2DCC3,
50+
0x052546C55F62FF34,
51+
0xD7146414F4796631,
52+
0x77EE6E84441B567A,
53+
0xE2C698D4357E57AF,
54+
0x744AE4F10636A122,
55+
0x020E03048CD9CD5E,
56+
0x43C4B8081CF9C548,
57+
0x81A3AED00DD27357,
58+
0x54159D6C3AF969DE,
59+
0xD73C4B34CAB22195,
60+
0xEDD07D1D23162C0D,
61+
0x87CBF66555849682,
62+
];
63+
64+
static SQUEEZE_DATA: &[u8; DATA_LEN] = include_bytes!("data/squeeze.bin");
65+
66+
#[test]
67+
fn keccak_test() {
68+
Keccak::new().with_f1600(|f1600| {
69+
let mut cursor = SpongeCursor::<136>::default();
70+
let mut state = State1600::default();
71+
72+
cursor.absorb_u64_le(&mut state, f1600, &SHORT_DATA);
73+
assert_eq!(state[0], 0x6F77_206F_6C6C_6568);
74+
assert_eq!(state[1], 0x0000_0000_2164_6C72);
75+
assert!(state[2..].iter().all(|v| *v == 0));
76+
assert_eq!(cursor.pos(), SHORT_DATA.len());
77+
assert_eq!(usize::from(cursor.raw_pos()), SHORT_DATA.len());
78+
let short_len = u8::try_from(SHORT_DATA.len()).unwrap();
79+
assert_eq!(cursor, SpongeCursor::new(short_len).unwrap());
80+
81+
let expected_pos = (SHORT_DATA.len() + DATA.len()) % 136;
82+
for &chunk_size in CHUNK_SIZES {
83+
let mut state_cpy = state;
84+
let mut cursor_cpy = cursor.clone();
85+
86+
for chunk in DATA.chunks(chunk_size) {
87+
cursor_cpy.absorb_u64_le(&mut state_cpy, f1600, chunk);
88+
}
89+
assert_eq!(state_cpy, STATE1);
90+
assert_eq!(cursor_cpy.pos(), expected_pos);
91+
assert_eq!(usize::from(cursor_cpy.raw_pos()), expected_pos);
92+
}
93+
94+
{
95+
let mut cursor = SpongeCursor::<136>::default();
96+
let mut state = State1600::default();
97+
98+
cursor.absorb_u64_le(&mut state, f1600, &CONCAT_DATA);
99+
assert_eq!(state, STATE1);
100+
assert_eq!(cursor.pos(), expected_pos);
101+
assert_eq!(usize::from(cursor.raw_pos()), expected_pos);
102+
}
103+
104+
cursor.absorb_u64_le(&mut state, f1600, &DATA);
105+
assert_eq!(state, STATE1);
106+
assert_eq!(cursor.pos(), expected_pos);
107+
assert_eq!(usize::from(cursor.raw_pos()), expected_pos);
108+
109+
f1600(&mut state);
110+
111+
for &chunk_size in CHUNK_SIZES {
112+
let mut buf = [0u8; DATA_LEN];
113+
cursor = Default::default();
114+
let mut state_cpy = state;
115+
116+
for chunk in buf.chunks_mut(chunk_size) {
117+
cursor.squeeze_read_u64_le(&mut state_cpy, f1600, chunk);
118+
}
119+
120+
assert_eq!(&buf, SQUEEZE_DATA);
121+
}
122+
123+
let expected: [u8; DATA_LEN] = core::array::from_fn(|i| DATA[i] ^ SQUEEZE_DATA[i]);
124+
for &chunk_size in CHUNK_SIZES {
125+
let mut buf = DATA;
126+
cursor = Default::default();
127+
let mut state_cpy = state;
128+
129+
for chunk in buf.chunks_mut(chunk_size) {
130+
cursor.squeeze_xor_u64_le(&mut state_cpy, f1600, chunk);
131+
}
132+
133+
assert_eq!(buf, expected);
134+
}
135+
});
136+
}

0 commit comments

Comments
 (0)