From 7559110b1644099e355dc5cdf39c0e7fc480a6c6 Mon Sep 17 00:00:00 2001 From: Anjan Roy Date: Tue, 29 Oct 2024 20:38:57 +0400 Subject: [PATCH] Refactor examples; make them easy to run from CLI Signed-off-by: Anjan Roy --- Makefile | 3 ++- examples/example.mk | 15 +++++++++++++++ examples/example_helper.hpp | 37 +++++++++++++++++++++++++++++++++++++ examples/sha3_224.cpp | 10 +++++----- examples/sha3_256.cpp | 10 +++++----- examples/sha3_384.cpp | 10 +++++----- examples/sha3_512.cpp | 10 +++++----- examples/shake128.cpp | 10 +++++----- examples/shake256.cpp | 10 +++++----- 9 files changed, 84 insertions(+), 31 deletions(-) create mode 100644 examples/example.mk create mode 100644 examples/example_helper.hpp diff --git a/Makefile b/Makefile index 4daddee..6512728 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ all: test include tests/test.mk include benches/bench.mk +include examples/example.mk $(GTEST_PARALLEL): git submodule update --init gtest-parallel @@ -26,5 +27,5 @@ $(GTEST_PARALLEL): clean: rm -rf $(BUILD_DIR) -format: $(SHA3_SOURCES) $(TEST_SOURCES) $(TEST_HEADERS) $(BENCHMARK_SOURCES) $(BENCHMARK_HEADERS) +format: $(SHA3_SOURCES) $(TEST_SOURCES) $(TEST_HEADERS) $(BENCHMARK_SOURCES) $(BENCHMARK_HEADERS) $(EXAMPLE_SOURCES) $(EXAMPLE_HEADERS) clang-format -i $^ diff --git a/examples/example.mk b/examples/example.mk new file mode 100644 index 0000000..0c595d6 --- /dev/null +++ b/examples/example.mk @@ -0,0 +1,15 @@ +EXAMPLE_BUILD_DIR := $(BUILD_DIR)/example + +EXAMPLE_DIR := examples +EXAMPLE_SOURCES := $(wildcard $(EXAMPLE_DIR)/*.cpp) +EXAMPLE_HEADERS := $(wildcard $(EXAMPLE_DIR)/*.hpp) +EXAMPLE_EXECS := $(addprefix $(EXAMPLE_BUILD_DIR)/, $(notdir $(patsubst %.cpp,%.exe,$(EXAMPLE_SOURCES)))) + +$(EXAMPLE_BUILD_DIR): + mkdir -p $@ + +$(EXAMPLE_BUILD_DIR)/%.exe: $(EXAMPLE_DIR)/%.cpp $(EXAMPLE_BUILD_DIR) + $(CXX) $(CXX_DEFS) $(CXX_FLAGS) $(WARN_FLAGS) $(RELEASE_FLAGS) $(I_FLAGS) $< -o $@ + +example: $(EXAMPLE_EXECS) + $(foreach exec,$^,./$(exec); echo "--- --- ---";) diff --git a/examples/example_helper.hpp b/examples/example_helper.hpp new file mode 100644 index 0000000..2503714 --- /dev/null +++ b/examples/example_helper.hpp @@ -0,0 +1,37 @@ +#pragma once +#include +#include +#include +#include +#include + +// Generates N -many random values of type T | N >= 0 +template +static inline void +random_data(std::span data) + requires(std::is_unsigned_v) +{ + std::random_device rd; + std::mt19937_64 gen(rd()); + std::uniform_int_distribution dis; + + const size_t len = data.size(); + for (size_t i = 0; i < len; i++) { + data[i] = dis(gen); + } +} + +// Given a bytearray of length N, this function converts it to human readable +// hex string of length N << 1 | N >= 0 +static inline std::string +to_hex(std::span bytes) +{ + std::stringstream ss; + ss << std::hex; + + for (size_t i = 0; i < bytes.size(); i++) { + ss << std::setw(2) << std::setfill('0') << static_cast(bytes[i]); + } + + return ss.str(); +} diff --git a/examples/sha3_224.cpp b/examples/sha3_224.cpp index d15a6d7..2e2faf0 100644 --- a/examples/sha3_224.cpp +++ b/examples/sha3_224.cpp @@ -1,5 +1,5 @@ -#include "sha3_224.hpp" -#include "utils.hpp" +#include "sha3/sha3_224.hpp" +#include "example_helper.hpp" #include #include @@ -16,7 +16,7 @@ main() std::vector dig(olen, 0); auto _dig = std::span(dig); - sha3_utils::random_data(msg); + random_data(msg); sha3_224::sha3_224_t hasher; hasher.absorb(msg); @@ -24,8 +24,8 @@ main() hasher.digest(_dig); std::cout << "SHA3-224" << std::endl << std::endl; - std::cout << "Input : " << sha3_utils::to_hex(msg) << "\n"; - std::cout << "Output : " << sha3_utils::to_hex(dig) << "\n"; + std::cout << "Input : " << to_hex(msg) << "\n"; + std::cout << "Output : " << to_hex(dig) << "\n"; return EXIT_SUCCESS; } diff --git a/examples/sha3_256.cpp b/examples/sha3_256.cpp index 1ddfb3f..b687c14 100644 --- a/examples/sha3_256.cpp +++ b/examples/sha3_256.cpp @@ -1,5 +1,5 @@ -#include "sha3_256.hpp" -#include "utils.hpp" +#include "sha3/sha3_256.hpp" +#include "example_helper.hpp" #include #include @@ -16,7 +16,7 @@ main() std::vector dig(olen, 0); auto _dig = std::span(dig); - sha3_utils::random_data(msg); + random_data(msg); sha3_256::sha3_256_t hasher; hasher.absorb(msg); @@ -24,8 +24,8 @@ main() hasher.digest(_dig); std::cout << "SHA3-256" << std::endl << std::endl; - std::cout << "Input : " << sha3_utils::to_hex(msg) << "\n"; - std::cout << "Output : " << sha3_utils::to_hex(dig) << "\n"; + std::cout << "Input : " << to_hex(msg) << "\n"; + std::cout << "Output : " << to_hex(dig) << "\n"; return EXIT_SUCCESS; } diff --git a/examples/sha3_384.cpp b/examples/sha3_384.cpp index 42246bf..29c1c91 100644 --- a/examples/sha3_384.cpp +++ b/examples/sha3_384.cpp @@ -1,5 +1,5 @@ -#include "sha3_384.hpp" -#include "utils.hpp" +#include "sha3/sha3_384.hpp" +#include "example_helper.hpp" #include #include @@ -16,7 +16,7 @@ main() std::vector dig(olen, 0); auto _dig = std::span(dig); - sha3_utils::random_data(msg); + random_data(msg); sha3_384::sha3_384_t hasher; hasher.absorb(msg); @@ -24,8 +24,8 @@ main() hasher.digest(_dig); std::cout << "SHA3-384" << std::endl << std::endl; - std::cout << "Input : " << sha3_utils::to_hex(msg) << "\n"; - std::cout << "Output : " << sha3_utils::to_hex(dig) << "\n"; + std::cout << "Input : " << to_hex(msg) << "\n"; + std::cout << "Output : " << to_hex(dig) << "\n"; return EXIT_SUCCESS; } diff --git a/examples/sha3_512.cpp b/examples/sha3_512.cpp index 8d2f848..d5fd15c 100644 --- a/examples/sha3_512.cpp +++ b/examples/sha3_512.cpp @@ -1,5 +1,5 @@ -#include "sha3_512.hpp" -#include "utils.hpp" +#include "sha3/sha3_512.hpp" +#include "example_helper.hpp" #include #include @@ -16,7 +16,7 @@ main() std::vector dig(olen, 0); auto _dig = std::span(dig); - sha3_utils::random_data(msg); + random_data(msg); sha3_512::sha3_512_t hasher; hasher.absorb(msg); @@ -24,8 +24,8 @@ main() hasher.digest(_dig); std::cout << "SHA3-512" << std::endl << std::endl; - std::cout << "Input : " << sha3_utils::to_hex(msg) << "\n"; - std::cout << "Output : " << sha3_utils::to_hex(dig) << "\n"; + std::cout << "Input : " << to_hex(msg) << "\n"; + std::cout << "Output : " << to_hex(dig) << "\n"; return EXIT_SUCCESS; } diff --git a/examples/shake128.cpp b/examples/shake128.cpp index 0d9ef01..ebdfd04 100644 --- a/examples/shake128.cpp +++ b/examples/shake128.cpp @@ -1,5 +1,5 @@ -#include "shake128.hpp" -#include "utils.hpp" +#include "sha3/shake128.hpp" +#include "example_helper.hpp" #include #include @@ -16,7 +16,7 @@ main() std::vector dig(olen, 0); auto _dig = std::span(dig); - sha3_utils::random_data(msg); + random_data(msg); // Create shake128 hasher shake128::shake128_t hasher; @@ -34,8 +34,8 @@ main() } std::cout << "SHAKE-128" << std::endl << std::endl; - std::cout << "Input : " << sha3_utils::to_hex(msg) << "\n"; - std::cout << "Output : " << sha3_utils::to_hex(dig) << "\n"; + std::cout << "Input : " << to_hex(msg) << "\n"; + std::cout << "Output : " << to_hex(dig) << "\n"; return EXIT_SUCCESS; } diff --git a/examples/shake256.cpp b/examples/shake256.cpp index a534128..c97786d 100644 --- a/examples/shake256.cpp +++ b/examples/shake256.cpp @@ -1,5 +1,5 @@ -#include "shake256.hpp" -#include "utils.hpp" +#include "sha3/shake256.hpp" +#include "example_helper.hpp" #include #include @@ -16,7 +16,7 @@ main() std::vector dig(olen, 0); auto _dig = std::span(dig); - sha3_utils::random_data(msg); + random_data(msg); // Create shake256 hasher shake256::shake256_t hasher; @@ -34,8 +34,8 @@ main() } std::cout << "SHAKE-256" << std::endl << std::endl; - std::cout << "Input : " << sha3_utils::to_hex(msg) << "\n"; - std::cout << "Output : " << sha3_utils::to_hex(dig) << "\n"; + std::cout << "Input : " << to_hex(msg) << "\n"; + std::cout << "Output : " << to_hex(dig) << "\n"; return EXIT_SUCCESS; }