Skip to content

Commit

Permalink
rust: use black_box to prevent compiler optimisations
Browse files Browse the repository at this point in the history
we don't actually use the result of decryption, so compiler may
have some "ideas", use `black_box` to stop it from doing that
  • Loading branch information
tomato42 committed Nov 29, 2023
1 parent 3aeaeb2 commit a26abdb
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion example/rust-crypto/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fs;
use std::io::{Read, Write};
use std::path::PathBuf;
use std::time::Instant;
use std::hint::black_box;

#[derive(Parser, Debug)]
#[command(about = "Measure timing of PKCS#1 v1.5 RSA decryption using RustCrypto rsa crate")]
Expand Down Expand Up @@ -58,7 +59,7 @@ fn main() -> Result<()> {
assert!(n == buffer.len());

let now = Instant::now();
let _ = privkey.decrypt(Pkcs1v15Encrypt, &buffer);
let _ = black_box(privkey.decrypt(Pkcs1v15Encrypt, black_box(&buffer)));
writeln!(outfile, "{}", now.elapsed().as_nanos())?;
}

Expand Down

0 comments on commit a26abdb

Please sign in to comment.