|
| 1 | +//! The purpose of this binary is to perform a single run of the primitive under test so that |
| 2 | +//! its peak memory usage can be measured with: |
| 3 | +//! |
| 4 | +//! valgrind --tool=massif --heap=no --stacks=yes -- target/release/bench_sha2_mem_usage > /dev/null |
| 5 | +//! |
| 6 | +//! ms_print massif.out.835000 |
| 7 | +//! |
| 8 | +//! or, shoved all into one line: |
| 9 | +//! |
| 10 | +//! clear; clear; valgrind --tool=massif --heap=no --stacks=yes -- target/release/bench_sha2_mem_usage > /dev/null; ms_print massif.out.*; rm massif.out.* |
| 11 | +//! |
| 12 | +//! Make sure you build in release mode! |
| 13 | +//! |
| 14 | +//! Note: I'm using print!() to force the compiler not to optimize away the actual code. |
| 15 | +//! I'm printing the important stuff for benchmarking to stderr so that I can pipe the junk to /dev/null |
| 16 | +//! (I'm not doing it the other way because /usr/bin/time prints its useful stuff to stderr as well) |
| 17 | +//! |
| 18 | +//! Main is at the bottom, controls which this was actually run. |
| 19 | +
|
| 20 | +#![allow(dead_code)] |
| 21 | +#![allow(unused_imports)] |
| 22 | + |
| 23 | +use bouncycastle::core::traits::Hash; |
| 24 | +use bouncycastle::sha2::{SHA224, SHA256, SHA384, SHA512}; |
| 25 | + |
| 26 | +/// A fixed input larger than one block for every SHA-2 variant, so a single hash call |
| 27 | +/// exercises multiple compression rounds. |
| 28 | +const INPUT: [u8; 1024] = [0xAB; 1024]; |
| 29 | + |
| 30 | +/// Number of `do_update` calls in the streaming benches (total absorbed = 16 KiB). |
| 31 | +const STREAMING_ITERS: usize = 16; |
| 32 | + |
| 33 | +/// This exists so I can use /usr/bin/time to measure the base memory footprint of the harness. |
| 34 | +fn bench_do_nothing() { |
| 35 | + eprintln!("DoNothing"); |
| 36 | + |
| 37 | + print!("{}", 1 + 1); |
| 38 | +} |
| 39 | + |
| 40 | +/// This prints the in-memory size of all the SHA-2 state structs. |
| 41 | +fn print_struct_sizes() { |
| 42 | + use core::mem::size_of; |
| 43 | + |
| 44 | + println!("size_of<SHA224>: {}", size_of::<SHA224>()); |
| 45 | + println!("size_of<SHA256>: {}", size_of::<SHA256>()); |
| 46 | + println!("size_of<SHA384>: {}", size_of::<SHA384>()); |
| 47 | + println!("size_of<SHA512>: {}", size_of::<SHA512>()); |
| 48 | +} |
| 49 | + |
| 50 | +fn bench_sha224_oneshot() { |
| 51 | + eprintln!("SHA224/OneShot"); |
| 52 | + |
| 53 | + let mut digest = [0u8; 28]; |
| 54 | + SHA224::new().hash_out(&INPUT, &mut digest); |
| 55 | + print!("{:x?}", digest); |
| 56 | +} |
| 57 | + |
| 58 | +fn bench_sha224_streaming() { |
| 59 | + eprintln!("SHA224/Streaming"); |
| 60 | + |
| 61 | + let mut digest = [0u8; 28]; |
| 62 | + let mut md = SHA224::new(); |
| 63 | + for _ in 0..STREAMING_ITERS { |
| 64 | + md.do_update(&INPUT); |
| 65 | + } |
| 66 | + md.do_final_out(&mut digest); |
| 67 | + print!("{:x?}", digest); |
| 68 | +} |
| 69 | + |
| 70 | +fn bench_sha256_oneshot() { |
| 71 | + eprintln!("SHA256/OneShot"); |
| 72 | + |
| 73 | + let mut digest = [0u8; 32]; |
| 74 | + SHA256::new().hash_out(&INPUT, &mut digest); |
| 75 | + print!("{:x?}", digest); |
| 76 | +} |
| 77 | + |
| 78 | +fn bench_sha256_streaming() { |
| 79 | + eprintln!("SHA256/Streaming"); |
| 80 | + |
| 81 | + let mut digest = [0u8; 32]; |
| 82 | + let mut md = SHA256::new(); |
| 83 | + for _ in 0..STREAMING_ITERS { |
| 84 | + md.do_update(&INPUT); |
| 85 | + } |
| 86 | + md.do_final_out(&mut digest); |
| 87 | + print!("{:x?}", digest); |
| 88 | +} |
| 89 | + |
| 90 | +fn bench_sha384_oneshot() { |
| 91 | + eprintln!("SHA384/OneShot"); |
| 92 | + |
| 93 | + let mut digest = [0u8; 48]; |
| 94 | + SHA384::new().hash_out(&INPUT, &mut digest); |
| 95 | + print!("{:x?}", digest); |
| 96 | +} |
| 97 | + |
| 98 | +fn bench_sha384_streaming() { |
| 99 | + eprintln!("SHA384/Streaming"); |
| 100 | + |
| 101 | + let mut digest = [0u8; 48]; |
| 102 | + let mut md = SHA384::new(); |
| 103 | + for _ in 0..STREAMING_ITERS { |
| 104 | + md.do_update(&INPUT); |
| 105 | + } |
| 106 | + md.do_final_out(&mut digest); |
| 107 | + print!("{:x?}", digest); |
| 108 | +} |
| 109 | + |
| 110 | +fn bench_sha512_oneshot() { |
| 111 | + eprintln!("SHA512/OneShot"); |
| 112 | + |
| 113 | + let mut digest = [0u8; 64]; |
| 114 | + SHA512::new().hash_out(&INPUT, &mut digest); |
| 115 | + print!("{:x?}", digest); |
| 116 | +} |
| 117 | + |
| 118 | +fn bench_sha512_streaming() { |
| 119 | + eprintln!("SHA512/Streaming"); |
| 120 | + |
| 121 | + let mut digest = [0u8; 64]; |
| 122 | + let mut md = SHA512::new(); |
| 123 | + for _ in 0..STREAMING_ITERS { |
| 124 | + md.do_update(&INPUT); |
| 125 | + } |
| 126 | + md.do_final_out(&mut digest); |
| 127 | + print!("{:x?}", digest); |
| 128 | +} |
| 129 | + |
| 130 | +fn main() { |
| 131 | + // print_struct_sizes() |
| 132 | + // bench_do_nothing() |
| 133 | + // bench_sha224_oneshot() |
| 134 | + // bench_sha224_streaming() |
| 135 | + // bench_sha256_oneshot() |
| 136 | + // bench_sha256_streaming() |
| 137 | + // bench_sha384_oneshot() |
| 138 | + // bench_sha384_streaming() |
| 139 | + bench_sha512_oneshot() |
| 140 | + // bench_sha512_streaming() |
| 141 | +} |
0 commit comments