diff --git a/.github/actions/mach_benchmark/action.yml b/.github/actions/mach_benchmark/action.yml index fe1ff62b..1afd46ab 100644 --- a/.github/actions/mach_benchmark/action.yml +++ b/.github/actions/mach_benchmark/action.yml @@ -25,6 +25,7 @@ runs: - run: | echo "BITS_FLAG=-m32" >> $GITHUB_ENV echo "OPENSSL_FLAG=--no-openssl" >> $GITHUB_ENV + echo "LIBB2_FLAG=--no-libb2" >> $GITHUB_ENV shell: bash if: ${{ inputs.bits == '32' }} - run: echo "BITS_FLAG=" > $GITHUB_ENV @@ -54,4 +55,4 @@ runs: shell: bash run: | echo OPENSSL_HOME = $OPENSSL_HOME - ./mach build -v $BENCHMARK_COMMAND --release $BITS_FLAG $EDITION_FLAG $TARGET_FLAG $OPENSSL_FLAG ${{ inputs.args }} + ./mach build -v $BENCHMARK_COMMAND --release $BITS_FLAG $EDITION_FLAG $TARGET_FLAG $OPENSSL_FLAG $LIBB2_FLAG ${{ inputs.args }} diff --git a/.github/workflows/benchmark_pull_request.yml b/.github/workflows/benchmark_pull_request.yml index 45e1d3ba..f09327fe 100644 --- a/.github/workflows/benchmark_pull_request.yml +++ b/.github/workflows/benchmark_pull_request.yml @@ -20,6 +20,7 @@ jobs: sudo apt-get install clang echo "CC=clang" >> $GITHUB_ENV echo "CXX=clang++" >> $GITHUB_ENV + sudo apt-get install libb2-dev - name: Checkout hacl-packages (${{ github.ref }}) uses: actions/checkout@v3 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 15a9ecdd..082d06c7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -71,6 +71,9 @@ jobs: if: ${{ matrix.compiler == 'clang' && matrix.bits == 32 }} run: sudo apt-get install gcc-multilib g++-multilib + - name: Setup | Install libb2 + run: sudo apt-get install libb2-dev + - name: Checkout uses: actions/checkout@v3 @@ -135,6 +138,9 @@ jobs: - name: Setup | Install OpenSSL3 run: brew install openssl@3 + - name: Setup | Install libb2 + run: brew install libb2 + - name: Checkout uses: actions/checkout@v3 diff --git a/.github/workflows/build_pull_request.yml b/.github/workflows/build_pull_request.yml index afd44829..06ddc948 100644 --- a/.github/workflows/build_pull_request.yml +++ b/.github/workflows/build_pull_request.yml @@ -53,6 +53,10 @@ jobs: echo "CC=clang" >> $GITHUB_ENV echo "CXX=clang++" >> $GITHUB_ENV + - name: Setup libb2 + if: ${{ matrix.bits == 64 }} + run: sudo apt-get install libb2-dev + - name: Checkout uses: actions/checkout@v3 @@ -100,6 +104,9 @@ jobs: - name: Setup | Install OpenSSL3 run: brew install openssl@3 + - name: Setup | Install libb2 + run: brew install libb2 + - name: Checkout uses: actions/checkout@v3 diff --git a/CMakeLists.txt b/CMakeLists.txt index dcdd2f68..5ab416bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,9 @@ include(${PROJECT_SOURCE_DIR}/build/config.cmake) # Constants used throughout hacl and the build. include(config/constants.cmake) +# for libb2 +include(FindPkgConfig) + # Set system processor to 32-bit. # Note that this only works on intel for now. if(CMAKE_C_FLAGS MATCHES ".*-m32.*") @@ -412,13 +415,6 @@ if(ENABLE_BENCHMARKS) add_library(ecckiila OBJECT ${SOURCES_ecckiila}) endif(ENABLE_BENCHMARKS) -# Add blake2 for benchmarks -if(ENABLE_BENCHMARKS) - include(${PROJECT_SOURCE_DIR}/third-party/blake2/config.cmake) - add_library(blake2 OBJECT ${SOURCES_blake2_ref}) - target_include_directories(blake2 PUBLIC ${PROJECT_SOURCE_DIR}/third-party/blake2/ref) -endif(ENABLE_BENCHMARKS) - # Add digestif for benchmarks if(ENABLE_BENCHMARKS) include(${PROJECT_SOURCE_DIR}/third-party/digestif/config.cmake) @@ -525,6 +521,16 @@ if(ENABLE_BENCHMARKS) target_compile_definitions(${BENCH_NAME} PUBLIC NO_OPENSSL) endif(ENABLE_OPENSSL_BENCHMARKS) + if(ENABLE_LIBB2_BENCHMARKS) + pkg_check_modules (LIBB2 REQUIRED libb2) + target_link_libraries(${BENCH_NAME} PRIVATE ${LIBB2_LIBRARIES}) + target_link_directories(${BENCH_NAME} PRIVATE ${LIBB2_LIBRARY_DIRS}) + target_include_directories(${BENCH_NAME} PUBLIC ${LIBB2_INCLUDE_DIRS}) + target_compile_options(${BENCH_NAME} PUBLIC ${LIBB2_CFLAGS_OTHER}) + else() + target_compile_definitions(${BENCH_NAME} PUBLIC NO_LIBB2) + endif(ENABLE_LIBB2_BENCHMARKS) + if(ENABLE_LIBTOMCRYPT_BENCHMARKS) if(DEFINED ENV{LIBTOMCRYPT_HOME}) target_include_directories(${BENCH_NAME} PUBLIC $ENV{LIBTOMCRYPT_HOME}/include/) @@ -546,7 +552,6 @@ if(ENABLE_BENCHMARKS) target_link_libraries(${BENCH_NAME} PRIVATE hacl_static ecckiila - blake2 digestif hacl_cpu_features benchmark::benchmark diff --git a/benchmarks/blake.cc b/benchmarks/blake.cc index 19f7ed8a..aabcc45f 100644 --- a/benchmarks/blake.cc +++ b/benchmarks/blake.cc @@ -20,8 +20,6 @@ #include "Hacl_Hash_Blake2b_Simd256.h" #endif -#include "blake2.h" - #define HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX 64 #define HACL_HASH_BLAKE2S_DIGEST_LENGTH_MAX 32 @@ -97,6 +95,22 @@ BENCHMARK_CAPTURE(OpenSSL_hash_oneshot, ->Setup(DoSetup); #endif +#ifndef NO_LIBB2 +#include + +static void +libb2_blake2b_oneshot(benchmark::State& state) +{ + bytes input(state.range(0), 0xAB); + + for (auto _ : state) { + blake2b(digest2b.data(), (const void*)input.data(), NULL, digest2b.size(), input.size(), 0); + } +} + +BENCHMARK(libb2_blake2b_oneshot)->Setup(DoSetup)->Apply(Range); +#endif + // ----------------------------------------------------------------------------- static void @@ -150,6 +164,19 @@ OpenSSL_blake2b_oneshot_keyed(benchmark::State& state) BENCHMARK(OpenSSL_blake2b_oneshot_keyed)->Setup(DoSetup); #endif +#ifndef NO_LIBB2 +#include + +static void +libb2_blake2b_oneshot_keyed(benchmark::State& state) +{ + for (auto _ : state) + blake2b(digest2b.data(), (const void*)input.data(), (const void*)key.data(), digest2b.size(), input.size(), key.size()); +} + +BENCHMARK(libb2_blake2b_oneshot_keyed)->Setup(DoSetup); +#endif + // ----------------------------------------------------------------------------- @@ -209,6 +236,21 @@ BENCHMARK_CAPTURE(OpenSSL_hash_oneshot, ->Setup(DoSetup); #endif +#ifndef NO_LIBB2 +#include + +static void +libb2_blake2s_oneshot(benchmark::State& state) +{ + bytes input(state.range(0), 0xAB); + + for (auto _ : state) + blake2s(digest2s.data(), (const void*)input.data(), NULL, digest2s.size(), input.size(), 0); +} + +BENCHMARK(libb2_blake2s_oneshot)->Setup(DoSetup)->Apply(Range); +#endif + // ----------------------------------------------------------------------------- static void @@ -262,6 +304,20 @@ OpenSSL_blake2s_oneshot_keyed(benchmark::State& state) BENCHMARK(OpenSSL_blake2s_oneshot_keyed)->Setup(DoSetup); #endif +#ifndef NO_LIBB2 +#include + +static void +libb2_blake2s_oneshot_keyed(benchmark::State& state) +{ + for (auto _ : state) + blake2s(digest2s.data(), (const void*)input.data(), (const void*)key.data(), digest2s.size(), input.size(), key.size()); +} + +BENCHMARK(libb2_blake2s_oneshot_keyed)->Setup(DoSetup); +#endif + + // ----------------------------------------------------------------------------- static void @@ -288,28 +344,6 @@ HACL_blake2b_32_streaming(benchmark::State& state) BENCHMARK(HACL_blake2b_32_streaming)->Setup(DoSetup); -static void -BLAKE2_blake2b_ref_streaming(benchmark::State& state) -{ - for (auto _ : state) { - uint8_t digest[64]; - - // Init - blake2b_state s; - blake2b_init(&s,64); - - // Update - for (auto chunk : chunk(input, chunk_len)) { - blake2b_update(&s, (uint8_t*)chunk.data(), chunk.size()); - } - - // Finish - blake2b_final(&s,digest,64); - } -} - -BENCHMARK(BLAKE2_blake2b_ref_streaming)->Setup(DoSetup); - #ifdef HACL_CAN_COMPILE_VEC256 static void HACL_blake2b_vec256_streaming(benchmark::State& state) @@ -399,28 +433,6 @@ HACL_blake2s_32_streaming(benchmark::State& state) BENCHMARK(HACL_blake2s_32_streaming)->Setup(DoSetup); -static void -BLAKE2_blake2s_ref_streaming(benchmark::State& state) -{ - for (auto _ : state) { - uint8_t digest[32]; - - // Init - blake2s_state s; - blake2s_init(&s,32); - - // Update - for (auto chunk : chunk(input, chunk_len)) { - blake2s_update(&s, (uint8_t*)chunk.data(), chunk.size()); - } - - // Finish - blake2s_final(&s,digest,32); - } -} - -BENCHMARK(BLAKE2_blake2s_ref_streaming)->Setup(DoSetup); - #ifdef HACL_CAN_COMPILE_VEC128 static void HACL_blake2s_vec128_streaming(benchmark::State& state) diff --git a/mach b/mach index 005c26ba..98ecda4a 100755 --- a/mach +++ b/mach @@ -95,6 +95,11 @@ def install(args): help="Don't build and run OpenSSL benchmarks.", action="store_true", ), + argument( + "--no-libb2", + help="Don't build and run libb2 benchmarks.", + action="store_true", + ), argument( "--libtomcrypt", help="Build and run LibTomCrypt benchmarks.", @@ -340,6 +345,8 @@ def build(args): ) exit(1) cmake_args.append("-DENABLE_BENCHMARKS=ON") + if not args.no_libb2: + cmake_args.append("-DENABLE_LIBB2_BENCHMARKS=ON") if not args.no_openssl: cmake_args.append("-DENABLE_OPENSSL_BENCHMARKS=ON") if platform.system() == "Darwin": diff --git a/tools/openssl.py b/tools/openssl.py index 7a2cdb74..a513062d 100644 --- a/tools/openssl.py +++ b/tools/openssl.py @@ -9,7 +9,7 @@ def find_openssl_home(): mprint(f"Probing for openssl with brew ... ") try: result = subprocess.run( - ["brew info --quiet openssl | egrep '^/' | cut -f 1 -d ' '"], shell=True, capture_output=True, text=True + ["brew info --quiet openssl | egrep '^/' | cut -f 1 -d ' ' | tail -n 1"], shell=True, capture_output=True, text=True ).stdout.rstrip() mprint("Found OpenSSL at", result) return result