1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#![no_std]
#![warn(missing_docs)]
use core::mem::size_of;
#[inline(always)]
pub const fn pad(n: usize) -> u64 {
0x80_u64 << (56 - 8 * n)
}
#[inline(always)]
pub const fn clear(word: u64, n: usize) -> u64 {
word & (0x00ffffffffffffff >> (n * 8 - 8))
}
#[derive(Clone, Copy, Debug)]
pub struct State {
x: [u64; 5],
}
impl State {
pub fn new(x0: u64, x1: u64, x2: u64, x3: u64, x4: u64) -> Self {
State {
x: [x0, x1, x2, x3, x4],
}
}
fn round(&mut self, c: u64) {
let x0 = self.x[0] ^ self.x[4];
let x2 = self.x[2] ^ self.x[1] ^ c;
let x4 = self.x[4] ^ self.x[3];
let tx0 = x0 ^ (!self.x[1] & x2);
let tx1 = self.x[1] ^ (!x2 & self.x[3]);
let tx2 = x2 ^ (!self.x[3] & x4);
let tx3 = self.x[3] ^ (!x4 & x0);
let tx4 = x4 ^ (!x0 & self.x[1]);
let tx1 = tx1 ^ tx0;
let tx3 = tx3 ^ tx2;
let tx0 = tx0 ^ tx4;
let x0 = tx0 ^ tx0.rotate_right(9);
let x1 = tx1 ^ tx1.rotate_right(22);
let x2 = tx2 ^ tx2.rotate_right(5);
let x3 = tx3 ^ tx3.rotate_right(7);
let x4 = tx4 ^ tx4.rotate_right(34);
self.x[0] = tx0 ^ x0.rotate_right(19);
self.x[1] = tx1 ^ x1.rotate_right(39);
self.x[2] = !(tx2 ^ x2.rotate_right(1));
self.x[3] = tx3 ^ x3.rotate_right(10);
self.x[4] = tx4 ^ x4.rotate_right(7);
}
pub fn permute_12(&mut self) {
self.round(0xf0);
self.round(0xe1);
self.round(0xd2);
self.round(0xc3);
self.round(0xb4);
self.round(0xa5);
self.round(0x96);
self.round(0x87);
self.round(0x78);
self.round(0x69);
self.round(0x5a);
self.round(0x4b);
}
pub fn permute_8(&mut self) {
self.round(0xb4);
self.round(0xa5);
self.round(0x96);
self.round(0x87);
self.round(0x78);
self.round(0x69);
self.round(0x5a);
self.round(0x4b);
}
pub fn permute_6(&mut self) {
self.round(0x96);
self.round(0x87);
self.round(0x78);
self.round(0x69);
self.round(0x5a);
self.round(0x4b);
}
pub fn as_bytes(&self) -> [u8; 40] {
let mut bytes = [0u8; size_of::<u64>() * 5];
for (dst, src) in bytes
.chunks_exact_mut(size_of::<u64>())
.zip(self.x.into_iter())
{
dst.copy_from_slice(&u64::to_be_bytes(src));
}
bytes
}
}
impl core::ops::Index<usize> for State {
type Output = u64;
#[inline(always)]
fn index(&self, index: usize) -> &Self::Output {
&self.x[index]
}
}
impl core::ops::IndexMut<usize> for State {
#[inline(always)]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.x[index]
}
}
impl TryFrom<&[u64]> for State {
type Error = ();
fn try_from(value: &[u64]) -> Result<Self, Self::Error> {
match value.len() {
5 => Ok(Self::new(value[0], value[1], value[2], value[3], value[4])),
_ => Err(()),
}
}
}
impl From<&[u64; 5]> for State {
fn from(value: &[u64; 5]) -> Self {
Self::new(value[0], value[1], value[2], value[3], value[4])
}
}
impl TryFrom<&[u8]> for State {
type Error = ();
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if value.len() != core::mem::size_of::<u64>() * 5 {
return Err(());
}
let mut x = [0u64; 5];
for (src, dst) in value
.chunks_exact(core::mem::size_of::<u64>())
.zip(x.iter_mut())
{
*dst = u64::from_be_bytes(src.try_into().unwrap());
}
Ok(Self { x })
}
}
impl From<&[u8; size_of::<u64>() * 5]> for State {
fn from(value: &[u8; size_of::<u64>() * 5]) -> Self {
let mut x = [0u64; 5];
for (src, dst) in value
.chunks_exact(core::mem::size_of::<u64>())
.zip(x.iter_mut())
{
*dst = u64::from_be_bytes(src.try_into().unwrap());
}
Self { x }
}
}
impl AsRef<[u64]> for State {
fn as_ref(&self) -> &[u64] {
&self.x
}
}
#[cfg(test)]
mod tests {
use super::{clear, pad, State};
#[test]
fn pad_0to7() {
assert_eq!(pad(0), 0x8000000000000000);
assert_eq!(pad(1), 0x80000000000000);
assert_eq!(pad(2), 0x800000000000);
assert_eq!(pad(3), 0x8000000000);
assert_eq!(pad(4), 0x80000000);
assert_eq!(pad(5), 0x800000);
assert_eq!(pad(6), 0x8000);
assert_eq!(pad(7), 0x80);
}
#[test]
fn clear_0to7() {
assert_eq!(clear(0x0123456789abcdef, 1), 0x23456789abcdef);
assert_eq!(clear(0x0123456789abcdef, 2), 0x456789abcdef);
assert_eq!(clear(0x0123456789abcdef, 3), 0x6789abcdef);
assert_eq!(clear(0x0123456789abcdef, 4), 0x89abcdef);
assert_eq!(clear(0x0123456789abcdef, 5), 0xabcdef);
assert_eq!(clear(0x0123456789abcdef, 6), 0xcdef);
assert_eq!(clear(0x0123456789abcdef, 7), 0xef);
}
#[test]
fn state_permute_12() {
let mut state = State::new(
0x0123456789abcdef,
0xef0123456789abcd,
0xcdef0123456789ab,
0xabcdef0123456789,
0x89abcdef01234567,
);
state.permute_12();
assert_eq!(state[0], 0x206416dfc624bb14);
assert_eq!(state[1], 0x1b0c47a601058aab);
assert_eq!(state[2], 0x8934cfc93814cddd);
assert_eq!(state[3], 0xa9738d287a748e4b);
assert_eq!(state[4], 0xddd934f058afc7e1);
}
#[test]
fn state_permute_6() {
let mut state = State::new(
0x0123456789abcdef,
0xef0123456789abcd,
0xcdef0123456789ab,
0xabcdef0123456789,
0x89abcdef01234567,
);
state.permute_6();
assert_eq!(state[0], 0xc27b505c635eb07f);
assert_eq!(state[1], 0xd388f5d2a72046fa);
assert_eq!(state[2], 0x9e415c204d7b15e7);
assert_eq!(state[3], 0xce0d71450fe44581);
assert_eq!(state[4], 0xdd7c5fef57befe48);
}
#[test]
fn state_permute_8() {
let mut state = State::new(
0x0123456789abcdef,
0xef0123456789abcd,
0xcdef0123456789ab,
0xabcdef0123456789,
0x89abcdef01234567,
);
state.permute_8();
assert_eq!(state[0], 0x67ed228272f46eee);
assert_eq!(state[1], 0x80bc0b097aad7944);
assert_eq!(state[2], 0x2fa599382c6db215);
assert_eq!(state[3], 0x368133fae2f7667a);
assert_eq!(state[4], 0x28cefb195a7c651c);
}
#[test]
fn state_convert_bytes() {
let state = State::new(
0x0123456789abcdef,
0xef0123456789abcd,
0xcdef0123456789ab,
0xabcdef0123456789,
0x89abcdef01234567,
);
let bytes = state.as_bytes();
let state2 = State::try_from(bytes.as_slice());
assert_eq!(state2.expect("try_from bytes").x, state.x);
let state2 = State::from(&bytes);
assert_eq!(state2.x, state.x);
}
}