Skip to content

Commit

Permalink
Add crypto benchmarks for iroha-ed25519 and Ursa
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Lebedev <lebdron@gmail.com>
  • Loading branch information
lebdron committed Oct 11, 2019
1 parent 218cc8c commit 21fb23e
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ set(DEPS_CMAKE_ARGS
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS})
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-G${CMAKE_GENERATOR}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
)

SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules)

Expand Down
3 changes: 3 additions & 0 deletions cmake/Modules/Findbenchmark.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ if (NOT benchmark_FOUND)
externalproject_add(google_benchmark
GIT_REPOSITORY https://github.com/google/benchmark
GIT_TAG v1.2.0
CMAKE_ARGS
${DEPS_CMAKE_ARGS}
-DBENCHMARK_ENABLE_TESTING=OFF
INSTALL_COMMAND "" # remove install step
TEST_COMMAND "" # remove test step
UPDATE_COMMAND "" # remove update step
Expand Down
14 changes: 14 additions & 0 deletions test/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,17 @@ target_link_libraries(bm_pipeline
integration_framework
shared_model_stateless_validation
)

add_executable(bm_iroha_ed25519 bm_iroha_ed25519.cpp)
target_link_libraries(bm_iroha_ed25519
benchmark
ed25519
)

if(USE_LIBURSA)
add_executable(bm_ursa_ed25519 bm_ursa_ed25519.cpp)
target_link_libraries(bm_ursa_ed25519
benchmark
ursa
)
endif()
68 changes: 68 additions & 0 deletions test/benchmark/bm_iroha_ed25519.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include <ed25519/ed25519.h>

#include <cstdlib>
#include <vector>

#include <benchmark/benchmark.h>

auto ConstructRandomVector(size_t size) {
using T = unsigned char;
std::vector<T> v;
v.reserve(size);
for (size_t i = 0; i < size; ++i) {
v.push_back(static_cast<T>(std::rand() % size));
}
return v;
}

static void BM_CreateKeypair(benchmark::State &state) {
public_key_t pub{};
private_key_t priv{};

while (state.KeepRunning()) {
ed25519_create_keypair(&priv, &pub);
}
}
BENCHMARK(BM_CreateKeypair);

static void BM_Sign(benchmark::State &state) {
public_key_t pub{};
private_key_t priv{};
signature_t sig{};

ed25519_create_keypair(&priv, &pub);

while (state.KeepRunning()) {
state.PauseTiming();
auto data = ConstructRandomVector(state.range(0));
state.ResumeTiming();

ed25519_sign(&sig, data.data(), data.size(), &pub, &priv);
}
}
BENCHMARK(BM_Sign)->RangeMultiplier(2)->Range(1 << 10, 1 << 18);

static void BM_Verify(benchmark::State &state) {
public_key_t pub{};
private_key_t priv{};
signature_t sig{};

ed25519_create_keypair(&priv, &pub);

while (state.KeepRunning()) {
state.PauseTiming();
auto data = ConstructRandomVector(state.range(0));
ed25519_sign(&sig, data.data(), data.size(), &pub, &priv);
state.ResumeTiming();

ed25519_verify(&sig, data.data(), data.size(), &pub);
}
}
BENCHMARK(BM_Verify)->RangeMultiplier(2)->Range(1 << 10, 1 << 18);

BENCHMARK_MAIN();
86 changes: 86 additions & 0 deletions test/benchmark/bm_ursa_ed25519.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include "ursa_crypto.h"

#include <cstdlib>
#include <vector>

#include <benchmark/benchmark.h>

auto ConstructRandomVector(size_t size) {
using T = std::remove_pointer_t<decltype(ByteBuffer::data)>;
std::vector<T> v;
v.reserve(size);
for (size_t i = 0; i < size; ++i) {
v.push_back(static_cast<T>(std::rand() % size));
}
return v;
}

static void BM_KeypairNew(benchmark::State &state) {
ByteBuffer pub{}, priv{};
ExternError err{};

while (state.KeepRunning()) {
ursa_ed25519_keypair_new(&pub, &priv, &err);

ursa_ed25519_bytebuffer_free(pub);
ursa_ed25519_bytebuffer_free(priv);
ursa_ed25519_string_free(err.message);
}
}
BENCHMARK(BM_KeypairNew);

static void BM_Sign(benchmark::State &state) {
ByteBuffer pub{}, priv{}, sig{}, msg{};
ExternError err{};

ursa_ed25519_keypair_new(&pub, &priv, &err);

while (state.KeepRunning()) {
state.PauseTiming();
auto data = ConstructRandomVector(state.range(0));
msg.data = data.data();
msg.len = data.size();
state.ResumeTiming();

ursa_ed25519_sign(&msg, &priv, &sig, &err);

ursa_ed25519_bytebuffer_free(sig);
ursa_ed25519_string_free(err.message);
}

ursa_ed25519_bytebuffer_free(pub);
ursa_ed25519_bytebuffer_free(priv);
}
BENCHMARK(BM_Sign)->RangeMultiplier(2)->Range(1 << 10, 1 << 18);

static void BM_Verify(benchmark::State &state) {
ByteBuffer pub{}, priv{}, sig{}, msg{};
ExternError err{};

ursa_ed25519_keypair_new(&pub, &priv, &err);

while (state.KeepRunning()) {
state.PauseTiming();
auto data = ConstructRandomVector(state.range(0));
msg.data = data.data();
msg.len = data.size();
ursa_ed25519_sign(&msg, &priv, &sig, &err);
state.ResumeTiming();

ursa_ed25519_verify(&msg, &sig, &pub, &err);

ursa_ed25519_bytebuffer_free(sig);
ursa_ed25519_string_free(err.message);
}

ursa_ed25519_bytebuffer_free(pub);
ursa_ed25519_bytebuffer_free(priv);
}
BENCHMARK(BM_Verify)->RangeMultiplier(2)->Range(1 << 10, 1 << 18);

BENCHMARK_MAIN();

0 comments on commit 21fb23e

Please sign in to comment.