Skip to content

Commit f96fc4b

Browse files
authored
streebog: use const eval to generate SHUFFLED_LIN_TABLE (#541)
1 parent 4d7fb0e commit f96fc4b

4 files changed

Lines changed: 25 additions & 2111 deletions

File tree

streebog/src/consts.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
pub const BLOCK_SIZE: usize = 64;
77

88
/// Linear transformation matrix
9-
#[cfg(test)]
109
pub const A: [u64; BLOCK_SIZE] = [
1110
0x641c314b2b8ee083,
1211
0xc83862965601dd1b,
@@ -75,7 +74,6 @@ pub const A: [u64; BLOCK_SIZE] = [
7574
];
7675

7776
/// Substitution table
78-
#[cfg(test)]
7977
pub const P: [u8; 256] = [
8078
252, 238, 221, 17, 207, 110, 49, 22, 251, 196, 250, 218, 35, 197, 4, 77, 233, 119, 240, 219,
8179
147, 46, 153, 186, 23, 54, 241, 187, 20, 205, 95, 193, 249, 24, 101, 90, 226, 92, 239, 33, 129,
@@ -179,3 +177,27 @@ pub const C: [[u8; BLOCK_SIZE]; 12] = [
179177
0x67, 0xe7, 0x8e, 0x37,
180178
],
181179
];
180+
181+
/// Precomputed, pre-shuffled table for linear transformation using matrix
182+
/// `const::A` and shuffled using `const::P`
183+
pub const SHUFFLED_LIN_TABLE: [[u64; 256]; 8] = {
184+
let mut table = [[0u64; 256]; 8];
185+
let mut i = 0;
186+
while i < 8 {
187+
let mut j = 0;
188+
while j < 256 {
189+
let mut accum = 0u64;
190+
let mut k = 0;
191+
while k < 8 {
192+
if P[j] & (1u8 << k) != 0 {
193+
accum ^= A[8 * i + k];
194+
}
195+
k += 1;
196+
}
197+
table[i][j] = accum;
198+
j += 1;
199+
}
200+
i += 1;
201+
}
202+
table
203+
};

streebog/src/core_api.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use digest::{
99
HashMarker, InvalidOutputSize, Output,
1010
};
1111

12-
use crate::consts::{BLOCK_SIZE, C};
13-
use crate::table::SHUFFLED_LIN_TABLE;
12+
use crate::consts::{BLOCK_SIZE, C, SHUFFLED_LIN_TABLE};
1413

1514
type Block = [u8; 64];
1615

streebog/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use digest::{
2020

2121
mod consts;
2222
mod core_api;
23-
mod table;
2423

2524
pub use core_api::StreebogVarCore;
2625
pub use digest::{self, Digest};

0 commit comments

Comments
 (0)