Skip to content

Commit

Permalink
Update benchmarks to use new directory structure of sha3 headers
Browse files Browse the repository at this point in the history
Signed-off-by: Anjan Roy <[email protected]>
  • Loading branch information
itzmeanjan committed Oct 29, 2024
1 parent 2dc4e4d commit b66c67f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
19 changes: 18 additions & 1 deletion benches/bench_common.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <algorithm>
#include <random>
#include <span>
#include <vector>

const auto compute_min = [](const std::vector<double>& v) -> double {
Expand All @@ -10,3 +11,19 @@ const auto compute_min = [](const std::vector<double>& v) -> double {
const auto compute_max = [](const std::vector<double>& v) -> double {
return *std::max_element(v.begin(), v.end());
};

// Generates N -many random values of type T | N >= 0
template<typename T>
static inline void
random_data(std::span<T> data)
requires(std::is_unsigned_v<T>)
{
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<T> dis;

const size_t len = data.size();
for (size_t i = 0; i < len; i++) {
data[i] = dis(gen);
}
}
16 changes: 8 additions & 8 deletions benches/bench_hashing.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "bench_common.hpp"
#include "sha3_224.hpp"
#include "sha3_256.hpp"
#include "sha3_384.hpp"
#include "sha3_512.hpp"
#include "sha3/sha3_224.hpp"
#include "sha3/sha3_256.hpp"
#include "sha3/sha3_384.hpp"
#include "sha3/sha3_512.hpp"
#include <benchmark/benchmark.h>

// Benchmarks SHA3-224 hash function with variable length input message.
Expand All @@ -15,7 +15,7 @@ bench_sha3_224(benchmark::State& state)
std::vector<uint8_t> md(sha3_224::DIGEST_LEN);
auto _md = std::span<uint8_t, sha3_224::DIGEST_LEN>(md);

sha3_utils::random_data<uint8_t>(msg);
random_data<uint8_t>(msg);

for (auto _ : state) {
sha3_224::sha3_224_t hasher;
Expand Down Expand Up @@ -47,7 +47,7 @@ bench_sha3_256(benchmark::State& state)
std::vector<uint8_t> md(sha3_256::DIGEST_LEN);
auto _md = std::span<uint8_t, sha3_256::DIGEST_LEN>(md);

sha3_utils::random_data<uint8_t>(msg);
random_data<uint8_t>(msg);

for (auto _ : state) {
sha3_256::sha3_256_t hasher;
Expand Down Expand Up @@ -79,7 +79,7 @@ bench_sha3_384(benchmark::State& state)
std::vector<uint8_t> md(sha3_384::DIGEST_LEN);
auto _md = std::span<uint8_t, sha3_384::DIGEST_LEN>(md);

sha3_utils::random_data<uint8_t>(msg);
random_data<uint8_t>(msg);

for (auto _ : state) {
sha3_384::sha3_384_t hasher;
Expand Down Expand Up @@ -111,7 +111,7 @@ bench_sha3_512(benchmark::State& state)
std::vector<uint8_t> md(sha3_512::DIGEST_LEN);
auto _md = std::span<uint8_t, sha3_512::DIGEST_LEN>(md);

sha3_utils::random_data<uint8_t>(msg);
random_data<uint8_t>(msg);

for (auto _ : state) {
sha3_512::sha3_512_t hasher;
Expand Down
5 changes: 2 additions & 3 deletions benches/bench_keccak.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include "bench_common.hpp"
#include "keccak.hpp"
#include "utils.hpp"
#include "sha3/internals/keccak.hpp"
#include <benchmark/benchmark.h>

// Benchmarks Keccak-p[1600, 24] permutation.
void
bench_keccak_permutation(benchmark::State& state)
{
uint64_t st[keccak::LANE_CNT]{};
sha3_utils::random_data<uint64_t>(st);
random_data<uint64_t>(st);

for (auto _ : state) {
keccak::permute(st);
Expand Down
8 changes: 4 additions & 4 deletions benches/bench_xof.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "bench_common.hpp"
#include "shake128.hpp"
#include "shake256.hpp"
#include "sha3/shake128.hpp"
#include "sha3/shake256.hpp"
#include <benchmark/benchmark.h>

// Benchmarks SHAKE-128 extendable output function with variable length input
Expand All @@ -17,7 +17,7 @@ bench_shake128(benchmark::State& state)
std::vector<uint8_t> msg(mlen);
std::vector<uint8_t> out(olen);

sha3_utils::random_data<uint8_t>(msg);
random_data<uint8_t>(msg);

for (auto _ : state) {
shake128::shake128_t hasher;
Expand Down Expand Up @@ -53,7 +53,7 @@ bench_shake256(benchmark::State& state)
std::vector<uint8_t> msg(mlen);
std::vector<uint8_t> out(olen);

sha3_utils::random_data<uint8_t>(msg);
random_data<uint8_t>(msg);

for (auto _ : state) {
shake256::shake256_t hasher;
Expand Down

0 comments on commit b66c67f

Please sign in to comment.