Use simd to improve performance and add clap

This commit is contained in:
hal8174 2025-07-06 00:38:53 +02:00
parent fbd946c7ec
commit fdbc206b53
Signed by: hal8174
SSH key fingerprint: SHA256:JwuqS+eVfISfKr+DkDQ6NWAbGd1jFAHkPpCM1yCnlTs
4 changed files with 315 additions and 25 deletions

3
.cargo/config.toml Normal file
View file

@ -0,0 +1,3 @@
[build]
rustflags = ["-C", "target-cpu=native"]

235
Cargo.lock generated
View file

@ -18,9 +18,60 @@ name = "alan-side-channel"
version = "0.1.0"
dependencies = [
"aes",
"clap",
"rayon",
]
[[package]]
name = "anstream"
version = "0.6.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd"
[[package]]
name = "anstyle-parse"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "cfg-if"
version = "1.0.1"
@ -37,6 +88,52 @@ dependencies = [
"inout",
]
[[package]]
name = "clap"
version = "4.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40b6887a1d8685cebccf115538db5c0efe625ccac9696ad45c409d96566e910f"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0c66c08ce9f0c698cbce5c0279d0bb6ac936d8674174fe48f736533b964f59e"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2c7947ae4cc3d851207c1adb5b5e260ff0cca11446b1d6d1423788e442257ce"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675"
[[package]]
name = "colorchoice"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "cpufeatures"
version = "0.2.17"
@ -97,6 +194,12 @@ dependencies = [
"version_check",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "inout"
version = "0.1.4"
@ -106,12 +209,42 @@ dependencies = [
"generic-array",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "once_cell_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad"
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.10.0"
@ -132,14 +265,116 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "typenum"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

View file

@ -5,4 +5,5 @@ edition = "2024"
[dependencies]
aes = "0.8.4"
clap = { version = "4.5.40", features = ["derive"] }
rayon = "1.10.0"

View file

@ -1,15 +1,17 @@
#![feature(int_from_ascii)]
use std::{
io::{BufRead, Read},
path::Path,
};
#![feature(portable_simd)]
use aes::{
Aes128,
cipher::{self, BlockEncrypt, KeyInit, generic_array::GenericArray},
cipher::{BlockEncrypt, KeyInit, generic_array::GenericArray},
};
use clap::Parser;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use std::{
io::{BufRead, Read},
path::{Path, PathBuf},
simd::{Simd, num::SimdUint},
};
static RSBOX: [u8; 256] = [
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
@ -30,17 +32,35 @@ 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<Vec<[u8; 16]>> {
let mut r = vec![Vec::with_capacity(ciphertexts.len()); 256];
fn calculate_models(ciphertexts: &[[u8; 16]]) -> Vec<[Vec<u8>; 16]> {
let mut r: Vec<[Vec<u8>; 16]> = (0..256)
.map(|_| {
[
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
Vec::with_capacity(ciphertexts.len()),
]
})
.collect();
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][k].push(RSBOX[(c[k] ^ (j as u8)) as usize]);
}
r[j].push(row);
}
}
@ -101,21 +121,43 @@ fn read_traces(path: impl AsRef<Path>, capacity: usize) -> Vec<Vec<u8>> {
r
}
const LANES: usize = 16;
fn correlation(
bit: usize,
key_hypothesis: usize,
trace_index: usize,
cyphtertext: &[Vec<[u8; 16]>],
cyphtertext: &[[Vec<u8>; 16]],
traces: &[Vec<u8>],
) -> f64 {
let mut x = 0i64;
let mut y = 0i64;
let mut xy = 0i64;
let mut xsqr = 0i64;
let mut ysqr = 0i64;
let mut x = Simd::<u64, LANES>::default();
let mut y = Simd::<u64, LANES>::default();
let mut xy = Simd::<u64, LANES>::default();
let mut xsqr = Simd::<u64, LANES>::default();
let mut ysqr = Simd::<u64, LANES>::default();
for i in 0..traces[0].len() {
let xi = (cyphtertext[key_hypothesis][i][bit / 8] & (1 << (bit % 8))) as i64;
let mask = Simd::<u8, LANES>::splat(1 << (bit % 8));
for i in 0..traces[0].len() / LANES {
let xi = Simd::<u8, LANES>::from_slice(&cyphtertext[key_hypothesis][bit / 8][i * LANES..]);
let xi = (xi & mask).cast::<u64>();
let yi = Simd::<u8, LANES>::from_slice(&traces[trace_index][i * LANES..]).cast::<u64>();
x += xi;
y += yi;
xy += xi * yi;
xsqr += xi * xi;
ysqr += yi * yi;
}
let mut x = x.reduce_sum() as i64;
let mut y = y.reduce_sum() as i64;
let mut xy = xy.reduce_sum() as i64;
let mut xsqr = xsqr.reduce_sum() as i64;
let mut ysqr = ysqr.reduce_sum() as i64;
for i in (traces[0].len() / LANES) * LANES..traces[0].len() {
let xi = (cyphtertext[key_hypothesis][bit / 8][i] & (1 << (bit % 8))) as i64;
let yi = traces[trace_index][i] as i64;
x += xi;
@ -132,9 +174,20 @@ fn correlation(
num / denom
}
#[derive(Debug, Parser)]
struct Args {
#[arg(default_value = "./alan/Task-3-example_traces/test_msgs.csv")]
msgs: PathBuf,
#[arg(default_value = "./alan/Task-3-example_traces/test_traces.csv")]
traces: PathBuf,
}
fn main() {
let start_total = std::time::Instant::now();
let args = Args::parse();
let start = std::time::Instant::now();
let cyphertext = read_msgs("./alan/Task-3-example_traces/test_msgs.csv");
let cyphertext = read_msgs(args.msgs);
println!("read msgs: {:?}", start.elapsed());
let start = std::time::Instant::now();
@ -142,10 +195,7 @@ fn main() {
println!("calculate models: {:?}", start.elapsed());
let start = std::time::Instant::now();
let traces = read_traces(
"./alan/Task-3-example_traces/test_traces.csv",
cyphertext.len(),
);
let traces = read_traces(args.traces, cyphertext.len());
println!("read traces: {:?}", start.elapsed());
let start = std::time::Instant::now();
@ -170,4 +220,5 @@ fn main() {
println!("bit: {bit}, key_hypothesis: {max_index}, max: {max}");
}
println!("calculate correlations: {:?}", start.elapsed());
println!("total: {:?}", start_total.elapsed());
}