diff --git a/CMakeLists.txt b/CMakeLists.txt index 68b8800..52c87aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,79 +8,111 @@ set(CMAKE_CXX_EXTENSIONS OFF) # Options option(BUILD_TESTS "Build test suite" ON) # Enable tests +option(BUILD_DOCS "Build docs" OFF) option(BUILD_SHARED_LIBS "Build shared libraries" OFF) -# Find required packages -find_package(OpenSSL REQUIRED) -find_package(GTest REQUIRED) - -# Add nlohmann/json -include(FetchContent) -FetchContent_Declare( - json - URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz +# Preprocessing +get_filename_component(RESOURCES_FOLDER_PATH ${CMAKE_CURRENT_SOURCE_DIR}/res ABSOLUTE) +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/include/hashsigs-cpp/resources.hpp.in + ${CMAKE_CURRENT_SOURCE_DIR}/include/hashsigs-cpp/resources.hpp ) -FetchContent_MakeAvailable(json) # Set output directories set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) -# Include directories -include_directories( - ${CMAKE_CURRENT_SOURCE_DIR}/include - ${OPENSSL_INCLUDE_DIR} -) +############## +# Dependencies +############## +include(FetchContent) +include(FindPackageMessage) + +# Add OpenSSL +find_package(OpenSSL REQUIRED) + +# Add fmt +FetchContent_Declare( + fmt + GIT_REPOSITORY https://github.com/fmtlib/fmt + GIT_TAG 407c905e45ad75fc29bf0f9bb7c5c2fd3475976f) # 12.1.0 +FetchContent_MakeAvailable(fmt) +################ # Library target +################ add_library(hashsigs - src/wotsplus.cpp - src/keccak.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/wotsplus.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/keccak.cpp ) -target_link_libraries(hashsigs - PRIVATE +target_include_directories(hashsigs PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/include +) + +target_link_libraries(hashsigs PRIVATE + fmt::fmt OpenSSL::SSL OpenSSL::Crypto ) +########## # CLI tool +########## add_executable(hashsigs_cli - src/hashsigs_cli.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/hashsigs_cli.cpp ) -target_link_libraries(hashsigs_cli - PRIVATE +target_link_libraries(hashsigs_cli PRIVATE hashsigs ) +############### # Install rules -install(TARGETS hashsigs - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib - RUNTIME DESTINATION bin +############### +include(GNUInstallDirs) + +install( + TARGETS hashsigs + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) -install(DIRECTORY include/ DESTINATION include) +install( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h" +) + +####### # Tests +####### if(BUILD_TESTS) enable_testing() add_subdirectory(tests) endif() +############### # Documentation -find_package(Doxygen) -if(DOXYGEN_FOUND) - set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in) - set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile) - - configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY) - - add_custom_target(docs - COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT} - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - COMMENT "Generating API documentation with Doxygen" - VERBATIM - ) -endif() \ No newline at end of file +############### +if(BUILD_DOCS) + message(STATUS "Building docs...") + find_package(Doxygen) + if(Doxygen_FOUND) + set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in) + set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile) + + configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY) + + add_custom_target(docs + COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Generating API documentation with Doxygen..." + VERBATIM + ) + else() + message(STATUS "Doxygen not found.") + endif() +endif() diff --git a/README.md b/README.md index 34ff2bf..74613db 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,15 @@ make ## Testing +Tests are built by default. To build the library without tests: + +```bash +mkdir build +cd build +cmake -DBUILD_TESTS=OFF .. +make +``` + Run all tests: ```bash @@ -34,13 +43,13 @@ make test Or run the test executable directly: ```bash -./tests/wotsplus_test +./build/bin/hashsigs_tests ``` For test output and backtrace: ```bash -GTEST_COLOR=1 ./tests/wotsplus_test --gtest_color=yes +GTEST_COLOR=1 ./build/bin/hashsigs_tests --gtest_color=yes ``` ## Development Requirements @@ -48,7 +57,6 @@ GTEST_COLOR=1 ./tests/wotsplus_test --gtest_color=yes - CMake 3.10 or higher - C++17 or higher - Google Test -- nlohmann/json ## Project Structure @@ -58,12 +66,13 @@ GTEST_COLOR=1 ./tests/wotsplus_test --gtest_color=yes │ ├── constants.hpp │ ├── public_key.hpp │ └── wotsplus.hpp +├── res/ # Resource files + └── vectors/ ├── src/ # Implementation files │ ├── keccak.cpp │ └── wotsplus.cpp -└── tests/ # Test vectors and unit tests +└── tests/ # Unit tests ├── wotsplus_test.cpp - └── test_vectors/ ``` ## License diff --git a/docs/Doxyfile.in b/docs/Doxyfile.in new file mode 100644 index 0000000..e69de29 diff --git a/include/constants.hpp b/include/hashsigs-cpp/constants.hpp similarity index 100% rename from include/constants.hpp rename to include/hashsigs-cpp/constants.hpp diff --git a/include/keccak.h b/include/hashsigs-cpp/keccak.h similarity index 100% rename from include/keccak.h rename to include/hashsigs-cpp/keccak.h diff --git a/include/hashsigs-cpp/resources.hpp.in b/include/hashsigs-cpp/resources.hpp.in new file mode 100644 index 0000000..a15c797 --- /dev/null +++ b/include/hashsigs-cpp/resources.hpp.in @@ -0,0 +1,7 @@ +#include + +namespace res { + +static const std::filesystem::path resources_folder_path = "@RESOURCES_FOLDER_PATH@"; + +} diff --git a/include/wotsplus.hpp b/include/hashsigs-cpp/wotsplus.hpp similarity index 100% rename from include/wotsplus.hpp rename to include/hashsigs-cpp/wotsplus.hpp diff --git a/tests/test_vectors/wotsplus_keccak256.json b/res/vectors/wotsplus_keccak256.json similarity index 100% rename from tests/test_vectors/wotsplus_keccak256.json rename to res/vectors/wotsplus_keccak256.json diff --git a/src/hashsigs_cli.cpp b/src/hashsigs_cli.cpp index 52a21ef..bb51355 100644 --- a/src/hashsigs_cli.cpp +++ b/src/hashsigs_cli.cpp @@ -1,5 +1,7 @@ -#include "../include/keccak.h" -#include "../include/wotsplus.hpp" +#include "hashsigs-cpp/keccak.h" +#include "hashsigs-cpp/wotsplus.hpp" + +#include #include #include #include diff --git a/src/keccak.cpp b/src/keccak.cpp index 7751037..2558404 100644 --- a/src/keccak.cpp +++ b/src/keccak.cpp @@ -4,7 +4,7 @@ // see http://create.stephan-brumme.com/disclaimer.html // -#include "keccak.h" +#include "hashsigs-cpp/keccak.h" // Cross-platform endianness detection #if defined(__APPLE__) diff --git a/src/wotsplus.cpp b/src/wotsplus.cpp index 29795d9..1b44e76 100644 --- a/src/wotsplus.cpp +++ b/src/wotsplus.cpp @@ -1,5 +1,6 @@ -#include "wotsplus.hpp" -#include "constants.hpp" +#include "hashsigs-cpp/wotsplus.hpp" +#include "hashsigs-cpp/constants.hpp" + #include #include #include diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cafe84b..6aacade 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,16 +1,24 @@ -# Add test executable -add_executable(hashsigs_tests - wotsplus_test.cpp +############## +# Dependencies +############## + +# Add GTest +find_package(GTest REQUIRED) + +# Add nlohmann/json +FetchContent_Declare(json + URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz + DOWNLOAD_EXTRACT_TIMESTAMP TRUE ) +FetchContent_MakeAvailable(json) -# Copy test vectors to build directory -configure_file( - ${CMAKE_CURRENT_SOURCE_DIR}/test_vectors/wotsplus_keccak256.json - ${CMAKE_CURRENT_BINARY_DIR}/wotsplus_keccak256.json - COPYONLY +################# +# Test executable +################# +add_executable(hashsigs_tests + wotsplus_test.cpp ) -# Link against the library and GTest target_link_libraries(hashsigs_tests PRIVATE hashsigs @@ -25,4 +33,4 @@ add_test(NAME hashsigs_tests COMMAND hashsigs_tests) # Set test properties set_tests_properties(hashsigs_tests PROPERTIES ENVIRONMENT "GTEST_COLOR=1" -) \ No newline at end of file +) diff --git a/tests/wotsplus_test.cpp b/tests/wotsplus_test.cpp index 973795d..b0e63fa 100644 --- a/tests/wotsplus_test.cpp +++ b/tests/wotsplus_test.cpp @@ -1,5 +1,8 @@ -#include "keccak.h" -#include "wotsplus.hpp" +#include "hashsigs-cpp/keccak.h" +#include "hashsigs-cpp/resources.hpp" +#include "hashsigs-cpp/wotsplus.hpp" + +#include #include #include #include @@ -145,7 +148,9 @@ TEST(WOTSPlusTest, VerifyValidSignatureWithRandomizationElements) { } TEST(WOTSPlusTest, Keccak256TestVectors) { - std::ifstream f("wotsplus_keccak256.json"); + std::filesystem::path vectors_file_path{ + res::resources_folder_path / "vectors" / "wotsplus_keccak256.json" }; + std::ifstream f{ vectors_file_path }; json data = json::parse(f); WOTSPlus wots(keccak256);