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

benchmarks: Make benchmarks more robust and cleanup. #348

Merged
merged 20 commits into from
Jan 24, 2023
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
316 changes: 191 additions & 125 deletions benchmarks/blake.cc

Large diffs are not rendered by default.

40 changes: 21 additions & 19 deletions benchmarks/chacha20.cc
Original file line number Diff line number Diff line change
@@ -104,11 +104,10 @@ static bytes expected_ciphertext = {
0x94, 0xa9, 0x33, 0x6a, 0x08, 0x06, 0xce, 0x78, 0x88, 0x10, 0x0a, 0x2e
};

// Chacha non-vectorized
static void
Chacha20_32_encrypt(benchmark::State& state)
HACL_Chacha20_32_encrypt(benchmark::State& state)
{
while (state.KeepRunning()) {
for (auto _ : state) {
Hacl_Chacha20_chacha20_encrypt(INPUT_LEN,
ciphertext.data(),
plaintext.data(),
@@ -121,20 +120,19 @@ Chacha20_32_encrypt(benchmark::State& state)
}
}
}
BENCHMARK(Chacha20_32_encrypt);

// Chacha Vec128
BENCHMARK(HACL_Chacha20_32_encrypt)->Setup(DoSetup);

#ifdef HACL_CAN_COMPILE_VEC128
static void
Chacha20_Vec128_encrypt(benchmark::State& state)
HACL_Chacha20_Vec128_encrypt(benchmark::State& state)
{
cpu_init();
if (!vec128_support()) {
state.SkipWithError("No vec128 support");
return;
}

while (state.KeepRunning()) {
for (auto _ : state) {
Hacl_Chacha20_Vec128_chacha20_encrypt_128(INPUT_LEN,
ciphertext.data(),
plaintext.data(),
@@ -147,21 +145,20 @@ Chacha20_Vec128_encrypt(benchmark::State& state)
}
}
}
BENCHMARK(Chacha20_Vec128_encrypt);
#endif // HACL_CAN_COMPILE_VEC128

// Chacha Vec256
BENCHMARK(HACL_Chacha20_Vec128_encrypt)->Setup(DoSetup);
#endif

#ifdef HACL_CAN_COMPILE_VEC256
static void
Chacha20_Vec256_encrypt(benchmark::State& state)
HACL_Chacha20_Vec256_encrypt(benchmark::State& state)
{
cpu_init();
if (!vec256_support()) {
state.SkipWithError("No vec256 support");
return;
}

while (state.KeepRunning()) {
for (auto _ : state) {
Hacl_Chacha20_Vec256_chacha20_encrypt_256(INPUT_LEN,
ciphertext.data(),
plaintext.data(),
@@ -174,20 +171,24 @@ Chacha20_Vec256_encrypt(benchmark::State& state)
}
}
}
BENCHMARK(Chacha20_Vec256_encrypt);
#endif // HACL_CAN_COMPILE_VEC256

BENCHMARK(HACL_Chacha20_Vec256_encrypt)->Setup(DoSetup);
#endif

// EverCrypt_Chacha20_encrypt
// Not supported in EverCrypt.

#ifndef NO_OPENSSL
static void
Openssl_Chacha20(benchmark::State& state)
OpenSSL_Chacha20_encrypt(benchmark::State& state)
{
// For OpenSSL we need to prepend the counter to the nonce.
openssl_nonce[0] = 0;
openssl_nonce[1] = 0;
openssl_nonce[2] = 0;
openssl_nonce[3] = 0;

while (state.KeepRunning()) {
for (auto _ : state) {
int out_len, unused_len;
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
int result = EVP_EncryptInit_ex2(
@@ -217,7 +218,8 @@ Openssl_Chacha20(benchmark::State& state)
}
}
}
BENCHMARK(Openssl_Chacha20);

BENCHMARK(OpenSSL_Chacha20_encrypt)->Setup(DoSetup);
#endif

// TODO: decrypt (even though it should be the same we should measure it)
103 changes: 69 additions & 34 deletions benchmarks/chacha20poly1305.cc
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@
* - http://www.apache.org/licenses/LICENSE-2.0
* - http://opensource.org/licenses/MIT
*/
#include "util.h"

#include "Hacl_Chacha20Poly1305_32.h"
#ifdef HACL_CAN_COMPILE_VEC128
@@ -15,6 +14,10 @@
#include "Hacl_Chacha20Poly1305_256.h"
#endif

#include "EverCrypt_AEAD.h"

#include "util.h"

const int INPUT_LEN = 1000;

static bytes plaintext(INPUT_LEN, 3);
@@ -105,11 +108,10 @@ static bytes expected_ciphertext = {
0xab, 0x46, 0xe8, 0x6e, 0x6a, 0x03, 0xc6, 0x19, 0x2e, 0x47, 0x1e, 0xf5
};

// ChachaPoly non-vectorized
static void
Chacha20Poly1305_32_encrypt(benchmark::State& state)
HACL_Chacha20Poly1305_32_encrypt(benchmark::State& state)
{
while (state.KeepRunning()) {
for (auto _ : state) {
Hacl_Chacha20Poly1305_32_aead_encrypt(key.data(),
nonce.data(),
aad.size(),
@@ -118,26 +120,25 @@ Chacha20Poly1305_32_encrypt(benchmark::State& state)
plaintext.data(),
ciphertext.data(),
mac.data());
if (ciphertext != expected_ciphertext) {
state.SkipWithError("Wrong ciphertext");
break;
}
}

if (ciphertext != expected_ciphertext) {
state.SkipWithError("Wrong ciphertext");
}
}
BENCHMARK(Chacha20Poly1305_32_encrypt);

// ChachaPoly Vec128
BENCHMARK(HACL_Chacha20Poly1305_32_encrypt)->Setup(DoSetup);

#ifdef HACL_CAN_COMPILE_VEC128
static void
Chacha20Poly1305_Vec128_encrypt(benchmark::State& state)
HACL_Chacha20Poly1305_Vec128_encrypt(benchmark::State& state)
{
cpu_init();
if (!vec128_support()) {
state.SkipWithError("No vec128 support");
return;
}

while (state.KeepRunning()) {
for (auto _ : state) {
Hacl_Chacha20Poly1305_128_aead_decrypt(key.data(),
nonce.data(),
aad.size(),
@@ -146,27 +147,26 @@ Chacha20Poly1305_Vec128_encrypt(benchmark::State& state)
plaintext.data(),
ciphertext.data(),
mac.data());
if (ciphertext != expected_ciphertext) {
state.SkipWithError("Wrong ciphertext");
break;
}
}

if (ciphertext != expected_ciphertext) {
state.SkipWithError("Wrong ciphertext");
}
}
BENCHMARK(Chacha20Poly1305_Vec128_encrypt);
#endif // HACL_CAN_COMPILE_VEC128

// ChachaPoly Vec256
BENCHMARK(HACL_Chacha20Poly1305_Vec128_encrypt)->Setup(DoSetup);
#endif

#ifdef HACL_CAN_COMPILE_VEC256
static void
Chacha20Poly1305_Vec256_encrypt(benchmark::State& state)
HACL_Chacha20Poly1305_Vec256_encrypt(benchmark::State& state)
{
cpu_init();
if (!vec256_support()) {
state.SkipWithError("No vec256 support");
return;
}

while (state.KeepRunning()) {
for (auto _ : state) {
Hacl_Chacha20Poly1305_256_aead_encrypt(key.data(),
nonce.data(),
aad.size(),
@@ -175,26 +175,60 @@ Chacha20Poly1305_Vec256_encrypt(benchmark::State& state)
plaintext.data(),
ciphertext.data(),
mac.data());
if (ciphertext != expected_ciphertext) {
state.SkipWithError("Wrong ciphertext");
}

if (ciphertext != expected_ciphertext) {
state.SkipWithError("Wrong ciphertext");
}
}

BENCHMARK(HACL_Chacha20Poly1305_Vec256_encrypt)->Setup(DoSetup);
#endif

static void
EverCrypt_Chacha20Poly1305_encrypt(benchmark::State& state)
{
for (auto _ : state) {
EverCrypt_AEAD_state_s* ctx;
EverCrypt_Error_error_code res = EverCrypt_AEAD_create_in(
Spec_Agile_AEAD_CHACHA20_POLY1305, &ctx, key.data());

if (res != EverCrypt_Error_Success) {
state.SkipWithError("Could not allocate AEAD state.");
break;
}

EverCrypt_AEAD_encrypt(ctx,
nonce.data(),
nonce.size(),
aad.data(),
aad.size(),
plaintext.data(),
INPUT_LEN,
ciphertext.data(),
mac.data());

EverCrypt_AEAD_free(ctx);
}

if (ciphertext != expected_ciphertext) {
state.SkipWithError("Wrong ciphertext");
}
}
BENCHMARK(Chacha20Poly1305_Vec256_encrypt);
#endif // HACL_CAN_COMPILE_VEC256

BENCHMARK(EverCrypt_Chacha20Poly1305_encrypt)->Setup(DoSetup);

#ifndef NO_OPENSSL
static void
Openssl_Chacha20Poly1305(benchmark::State& state)
OpenSSL_Chacha20Poly1305_encrypt(benchmark::State& state)
{
// For OpenSSL we need to prepend the counter to the nonce.
openssl_nonce[0] = 0;
openssl_nonce[1] = 0;
openssl_nonce[2] = 0;
openssl_nonce[3] = 0;

while (state.KeepRunning()) {
for (auto _ : state) {
int out_len, unused_len;
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
int result = EVP_EncryptInit_ex2(
@@ -224,13 +258,14 @@ Openssl_Chacha20Poly1305(benchmark::State& state)
break;
}
EVP_CIPHER_CTX_free(ctx);
if (ciphertext != expected_ciphertext) {
state.SkipWithError("Wrong ciphertext");
break;
}
}

if (ciphertext != expected_ciphertext) {
state.SkipWithError("Wrong ciphertext");
}
}
BENCHMARK(Openssl_Chacha20Poly1305);

BENCHMARK(OpenSSL_Chacha20Poly1305_encrypt)->Setup(DoSetup);
#endif

// TODO: decrypt (even though it should be the same we should measure it)
53 changes: 15 additions & 38 deletions benchmarks/drbg.cc
Original file line number Diff line number Diff line change
@@ -17,9 +17,13 @@ static bytes personalization_string =
static bytes additional_input = from_hex("1CA8F61C");
static bytes output(128);

inline void
Drbg_complete(benchmark::State& state, Spec_Hash_Definitions_hash_alg algorithm)
template<class... Args>
void
HACL_Drbg_complete(benchmark::State& state, Args&&... args)
{
auto args_tuple = std::make_tuple(std::move(args)...);
Spec_Hash_Definitions_hash_alg algorithm = std::get<0>(args_tuple);

for (auto _ : state) {
Hacl_HMAC_DRBG_state state = Hacl_HMAC_DRBG_create_in(algorithm);
Hacl_HMAC_DRBG_instantiate(algorithm,
@@ -38,44 +42,17 @@ Drbg_complete(benchmark::State& state, Spec_Hash_Definitions_hash_alg algorithm)
additional_input.size(),
additional_input.data());

Hacl_HMAC_DRBG_free(Spec_Hash_Definitions_SHA2_256, state);
Hacl_HMAC_DRBG_free(algorithm, state);
}
}

// ----- SHA-2 256 -------------------------------------------------------------

static void Drbg_SHA2_256_complete(benchmark::State& state)
{
Drbg_complete(state, Spec_Hash_Definitions_SHA2_256);
}

BENCHMARK(Drbg_SHA2_256_complete);

// ----- SHA-2 384 -------------------------------------------------------------

static void Drbg_SHA2_384_complete(benchmark::State& state)
{
Drbg_complete(state, Spec_Hash_Definitions_SHA2_384);
}

BENCHMARK(Drbg_SHA2_384_complete);

// ----- SHA-2 512 -------------------------------------------------------------

static void Drbg_SHA2_512_complete(benchmark::State& state)
{
Drbg_complete(state, Spec_Hash_Definitions_SHA2_512);
}

BENCHMARK(Drbg_SHA2_512_complete);

// ----- SHA-1 -----------------------------------------------------------------

static void Drbg_SHA1_complete(benchmark::State& state)
{
Drbg_complete(state, Spec_Hash_Definitions_SHA1);
}

BENCHMARK(Drbg_SHA1_complete);
BENCHMARK_CAPTURE(HACL_Drbg_complete, sha2_256, Spec_Hash_Definitions_SHA2_256)
->Setup(DoSetup);
BENCHMARK_CAPTURE(HACL_Drbg_complete, sha2_384, Spec_Hash_Definitions_SHA2_384)
->Setup(DoSetup);
BENCHMARK_CAPTURE(HACL_Drbg_complete, sha2_512, Spec_Hash_Definitions_SHA2_512)
->Setup(DoSetup);
BENCHMARK_CAPTURE(HACL_Drbg_complete, sha1, Spec_Hash_Definitions_SHA1)
->Setup(DoSetup);

BENCHMARK_MAIN();
Loading