Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bench against libb2 #458

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/actions/mach_benchmark/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
1 change: 1 addition & 0 deletions .github/workflows/benchmark_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/build_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
21 changes: 13 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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/)
Expand All @@ -546,7 +552,6 @@ if(ENABLE_BENCHMARKS)
target_link_libraries(${BENCH_NAME} PRIVATE
hacl_static
ecckiila
blake2
digestif
hacl_cpu_features
benchmark::benchmark
Expand Down
104 changes: 58 additions & 46 deletions benchmarks/blake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -97,6 +95,22 @@ BENCHMARK_CAPTURE(OpenSSL_hash_oneshot,
->Setup(DoSetup);
#endif

#ifndef NO_LIBB2
#include <blake2.h>

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
Expand Down Expand Up @@ -150,6 +164,19 @@ OpenSSL_blake2b_oneshot_keyed(benchmark::State& state)
BENCHMARK(OpenSSL_blake2b_oneshot_keyed)->Setup(DoSetup);
#endif

#ifndef NO_LIBB2
#include <blake2.h>

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


// -----------------------------------------------------------------------------

Expand Down Expand Up @@ -209,6 +236,21 @@ BENCHMARK_CAPTURE(OpenSSL_hash_oneshot,
->Setup(DoSetup);
#endif

#ifndef NO_LIBB2
#include <blake2.h>

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
Expand Down Expand Up @@ -262,6 +304,20 @@ OpenSSL_blake2s_oneshot_keyed(benchmark::State& state)
BENCHMARK(OpenSSL_blake2s_oneshot_keyed)->Setup(DoSetup);
#endif

#ifndef NO_LIBB2
#include <blake2.h>

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
Expand All @@ -288,28 +344,6 @@ HACL_blake2b_32_streaming(benchmark::State& state)

BENCHMARK(HACL_blake2b_32_streaming)->Setup(DoSetup);

static void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to drop these two?
@karthikbhargavan looks like you did this change.

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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions mach
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion tools/openssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading