perfomance improvements

This commit is contained in:
hal8174 2025-07-05 21:52:16 +02:00
parent de7b7536ce
commit 422e3018e2
Signed by: hal8174
SSH key fingerprint: SHA256:JwuqS+eVfISfKr+DkDQ6NWAbGd1jFAHkPpCM1yCnlTs

View file

@ -30,21 +30,21 @@ static RSBOX: [u8; 256] = [
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d, 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
]; ];
fn calculate_models(ciphertexts: &[[u8; 16]]) -> Vec<[[u8; 16]; 256]> { fn calculate_models(ciphertexts: &[[u8; 16]]) -> Vec<Vec<[u8; 16]>> {
ciphertexts let mut r = vec![Vec::with_capacity(ciphertexts.len()); 256];
.iter()
.map(|c| {
let mut row = [[0; 16]; 256];
for i in 0..256 { for (i, c) in ciphertexts.iter().enumerate() {
for j in 0..16 { for j in 0..256 {
row[i][j] = RSBOX[(c[j] ^ (i as u8)) as usize]; let mut row = [0; 16];
}
for k in 0..16 {
row[k] = RSBOX[(c[k] ^ (j as u8)) as usize];
} }
r[j].push(row);
}
}
row r
})
.collect()
} }
fn read_msgs(path: impl AsRef<Path>) -> Vec<[u8; 16]> { fn read_msgs(path: impl AsRef<Path>) -> Vec<[u8; 16]> {
@ -108,7 +108,7 @@ fn correlation(
bit: usize, bit: usize,
key_hypothesis: usize, key_hypothesis: usize,
trace_index: usize, trace_index: usize,
cyphtertext: &[[[u8; 16]; 256]], cyphtertext: &[Vec<[u8; 16]>],
traces: &[[u8; TRACES]], traces: &[[u8; TRACES]],
) -> f64 { ) -> f64 {
let mut x = 0i64; let mut x = 0i64;
@ -118,7 +118,7 @@ fn correlation(
let mut ysqr = 0i64; let mut ysqr = 0i64;
for i in 0..traces.len() { for i in 0..traces.len() {
let xi = (cyphtertext[i][key_hypothesis][bit / 8] & (1 << (bit % 8))) as i64; let xi = (cyphtertext[key_hypothesis][i][bit / 8] & (1 << (bit % 8))) as i64;
let yi = traces[i][trace_index] as i64; let yi = traces[i][trace_index] as i64;
x += xi; x += xi;