perfomance improvements
This commit is contained in:
parent
de7b7536ce
commit
422e3018e2
1 changed files with 14 additions and 14 deletions
28
src/main.rs
28
src/main.rs
|
|
@ -30,21 +30,21 @@ static RSBOX: [u8; 256] = [
|
|||
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]> {
|
||||
ciphertexts
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let mut row = [[0; 16]; 256];
|
||||
fn calculate_models(ciphertexts: &[[u8; 16]]) -> Vec<Vec<[u8; 16]>> {
|
||||
let mut r = vec![Vec::with_capacity(ciphertexts.len()); 256];
|
||||
|
||||
for i in 0..256 {
|
||||
for j in 0..16 {
|
||||
row[i][j] = RSBOX[(c[j] ^ (i as u8)) as usize];
|
||||
}
|
||||
for (i, c) in ciphertexts.iter().enumerate() {
|
||||
for j in 0..256 {
|
||||
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
|
||||
})
|
||||
.collect()
|
||||
r
|
||||
}
|
||||
|
||||
fn read_msgs(path: impl AsRef<Path>) -> Vec<[u8; 16]> {
|
||||
|
|
@ -108,7 +108,7 @@ fn correlation(
|
|||
bit: usize,
|
||||
key_hypothesis: usize,
|
||||
trace_index: usize,
|
||||
cyphtertext: &[[[u8; 16]; 256]],
|
||||
cyphtertext: &[Vec<[u8; 16]>],
|
||||
traces: &[[u8; TRACES]],
|
||||
) -> f64 {
|
||||
let mut x = 0i64;
|
||||
|
|
@ -118,7 +118,7 @@ fn correlation(
|
|||
let mut ysqr = 0i64;
|
||||
|
||||
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;
|
||||
|
||||
x += xi;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue