Skip to content

Commit

Permalink
streebog: use const eval to generate SHUFFLED_LIN_TABLE (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored Jan 10, 2024
1 parent 4d7fb0e commit f96fc4b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2,111 deletions.
26 changes: 24 additions & 2 deletions streebog/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
pub const BLOCK_SIZE: usize = 64;

/// Linear transformation matrix
#[cfg(test)]
pub const A: [u64; BLOCK_SIZE] = [
0x641c314b2b8ee083,
0xc83862965601dd1b,
Expand Down Expand Up @@ -75,7 +74,6 @@ pub const A: [u64; BLOCK_SIZE] = [
];

/// Substitution table
#[cfg(test)]
pub const P: [u8; 256] = [
252, 238, 221, 17, 207, 110, 49, 22, 251, 196, 250, 218, 35, 197, 4, 77, 233, 119, 240, 219,
147, 46, 153, 186, 23, 54, 241, 187, 20, 205, 95, 193, 249, 24, 101, 90, 226, 92, 239, 33, 129,
Expand Down Expand Up @@ -179,3 +177,27 @@ pub const C: [[u8; BLOCK_SIZE]; 12] = [
0x67, 0xe7, 0x8e, 0x37,
],
];

/// Precomputed, pre-shuffled table for linear transformation using matrix
/// `const::A` and shuffled using `const::P`
pub const SHUFFLED_LIN_TABLE: [[u64; 256]; 8] = {
let mut table = [[0u64; 256]; 8];
let mut i = 0;
while i < 8 {
let mut j = 0;
while j < 256 {
let mut accum = 0u64;
let mut k = 0;
while k < 8 {
if P[j] & (1u8 << k) != 0 {
accum ^= A[8 * i + k];
}
k += 1;
}
table[i][j] = accum;
j += 1;
}
i += 1;
}
table
};
3 changes: 1 addition & 2 deletions streebog/src/core_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use digest::{
HashMarker, InvalidOutputSize, Output,
};

use crate::consts::{BLOCK_SIZE, C};
use crate::table::SHUFFLED_LIN_TABLE;
use crate::consts::{BLOCK_SIZE, C, SHUFFLED_LIN_TABLE};

type Block = [u8; 64];

Expand Down
1 change: 0 additions & 1 deletion streebog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use digest::{

mod consts;
mod core_api;
mod table;

pub use core_api::StreebogVarCore;
pub use digest::{self, Digest};
Expand Down
Loading

0 comments on commit f96fc4b

Please sign in to comment.