From 9d9f67692bf52d6747ca530809145a41b107900a Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 29 Apr 2020 16:51:03 +0000 Subject: [PATCH] rust: Add benchmark comparing to software implementation --- rust/cryptest/Cargo.toml | 1 + rust/cryptest/src/main.rs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/rust/cryptest/Cargo.toml b/rust/cryptest/Cargo.toml index f7613d3..5dce981 100644 --- a/rust/cryptest/Cargo.toml +++ b/rust/cryptest/Cargo.toml @@ -10,3 +10,4 @@ k210-shared = { path = "../k210-shared" } riscv = "0.5" riscv-rt = "0.6" hex-literal = "0.2" +sha2 = { version = "0.8", default-features = false } diff --git a/rust/cryptest/src/main.rs b/rust/cryptest/src/main.rs index c12ee75..fb4e382 100644 --- a/rust/cryptest/src/main.rs +++ b/rust/cryptest/src/main.rs @@ -16,6 +16,7 @@ use k210_shared::soc::sha256::SHA256Ctx; use k210_shared::timing::clock; use riscv::asm; use riscv_rt::entry; +use sha2::{Sha256, Digest}; struct AESTestVec { cipher_mode: cipher_mode, @@ -567,6 +568,22 @@ fn main() -> ! { } write!(stdout, " ({} kB/s)", (size as u64) * 1_000 / (time_end - time_start)).unwrap(); writeln!(stdout).unwrap(); + + // Software + write!(stdout, "SHA256 sw ({} bytes): ", size).unwrap(); + let time_start = clock(); + let mut hasher = Sha256::new(); + for _ in 0..65_535 { + hasher.input(&s[..]); + } + let time_end = clock(); + if hasher.result()[..] == expected { + write!(stdout, "MATCH").unwrap(); + } else { + write!(stdout, "MISMATCH").unwrap(); + } + write!(stdout, " ({} kB/s)", (size as u64) * 1_000 / (time_end - time_start)).unwrap(); + writeln!(stdout).unwrap(); } loop {