Skip to content

Commit

Permalink
Add e2e bench testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanSchmiedmayer committed Apr 29, 2024
1 parent f9df59b commit 2982606
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 115 deletions.
19 changes: 12 additions & 7 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ set(BBS_FIX_TESTS
bbs_fix_hash_to_scalar.c
bbs_fix_expand_message.c)

create_test_sourcelist(fixture-tests bbs-test-fixtures.c ${BBS_FIX_TESTS})

set(BBS_E2E_TESTS bbs_e2e_sign_n_proof.c)
set(BBS_BENCH_TESTS bbs_e2e_sign_n_proof.c, bbs_e2e_bench.c)
set(BBS_BENCH_TESTS bbs_bench_e2e.c bbs_bench_individual.c)

create_test_sourcelist(fixture-tests bbs-test-fixtures.c ${BBS_FIX_TESTS})
create_test_sourcelist(e2e-tests bbs-test-e2e.c ${BBS_E2E_TESTS})
create_test_sourcelist(bench-tests bbs-test-bench.c ${BBS_BENCH_TESTS})

add_executable(bbs-test-fixtures ${fixture-tests} fixtures.c)
target_link_libraries(bbs-test-fixtures PRIVATE bbs)

add_executable(bbs-test-e2e ${e2e-tests})
target_link_libraries(bbs-test-e2e PRIVATE bbs)

add_executable(bbs-test-e2e-bench ${e2e-tests})
target_link_libraries(bbs-test-e2e-bench PRIVATE bbs)
target_compile_definitions(bbs-test-e2e-bench PUBLIC ENABLE_BENCHMARK)
add_custom_target(bench COMMAND bbs-test-e2e-bench)
add_executable(bbs-test-bench ${bench-tests})
target_link_libraries(bbs-test-bench PRIVATE bbs)
target_compile_definitions(bbs-test-bench PUBLIC ENABLE_BENCHMARK)
add_custom_target(bench COMMAND bbs-test-bench)

foreach(test ${BBS_FIX_TESTS})
get_filename_component(TName ${test} NAME_WE)
Expand All @@ -36,3 +36,8 @@ foreach(test ${BBS_E2E_TESTS})
get_filename_component(TName ${test} NAME_WE)
add_test(NAME ${TName} COMMAND bbs-test-e2e ${TName})
endforeach()

foreach(test ${BBS_BENCH_TESTS})
get_filename_component(TName ${test} NAME_WE)
add_test(NAME ${TName} COMMAND bbs-test-bench ${TName})
endforeach()
114 changes: 114 additions & 0 deletions test/bbs_bench_e2e.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "fixtures.h"
#include "test_util.h"
#include <string.h>

int
bbs_bench_e2e ()
{
#define ITERATIONS_START 100
#define ITERATIONS_END 110
#define ITERATIONS_STEP 10
#define MSG_LEN_START 1024
#define MSG_LEN_END 135168
#define MSG_LEN_STEP 1024
#define USE_HEADER 0
char msg1[ITERATIONS_END][MSG_LEN_END];
char msg2[ITERATIONS_END][MSG_LEN_END];
for (int i = 0; i < ITERATIONS_END; i++)
{
for (int j = 0; j < MSG_LEN_END; j++)
{
msg1[i][j] = (char) rand ();
msg2[i][j] = (char) rand ();
}
}
for (int iterations_count = ITERATIONS_START;
iterations_count < ITERATIONS_END;
iterations_count += ITERATIONS_STEP)
{
for (int msg_len = MSG_LEN_START; msg_len < MSG_LEN_END; msg_len += MSG_LEN_STEP)
{
printf (
"%d iterations BBS e2e sign and proof (2 messages, each %d bytes, include header %d, reveal message 0 (1/2).",
iterations_count,
msg_len,
USE_HEADER);
BBS_BENCH_START (e2e)

if (core_init () != RLC_OK)
{
core_clean ();
return 1;
}
if (pc_param_set_any () != RLC_OK)
{
core_clean ();
return 1;
}

for (int i = 0; i < iterations_count; i++)
{
bbs_secret_key sk;
bbs_public_key pk;

if (BBS_OK != bbs_sha256_keygen_full (sk, pk))
{
puts ("Error during key generation");
return 1;
}

bbs_signature sig;
#if USE_HEADER
static char header[] = "But I am a header!";
#else
static char header[] = "";
#endif

if (BBS_OK != bbs_sha256_sign (sk, pk, sig, (uint8_t*) header,
strlen (header), 2, msg1[i], msg_len,
msg2[i], msg_len))
{
puts ("Error during signing");
return 1;
}

if (BBS_OK != bbs_sha256_verify (pk, sig, (uint8_t*) header,
strlen (header), 2, msg1[i],
msg_len, msg2[i], msg_len))
{
puts ("Error during signature verification");
return 1;
}

uint8_t proof[BBS_PROOF_LEN (1)];
uint64_t disclosed_indexes[] = {0};
static char ph[] = "I am a challenge nonce!";

if (BBS_OK != bbs_sha256_proof_gen (pk, sig, proof,
(uint8_t*) header,
strlen (header), (uint8_t*) ph,
strlen (ph), disclosed_indexes,
1, 2, msg1[i], msg_len, msg2[i],
msg_len))
{
puts ("Error during proof generation");
return 1;
}

if (BBS_OK != bbs_sha256_proof_verify (pk, proof, BBS_PROOF_LEN (1),
(uint8_t*) header,
strlen (header),
(uint8_t*) ph, strlen (ph),
disclosed_indexes, 1, 2,
msg1[i], msg_len))
{
puts ("Error during proof verification");
return 1;
}
}
BBS_BENCH_END (e2e, "bbs_e2e_sign_n_proof")
}
}

return 0;
}
176 changes: 176 additions & 0 deletions test/bbs_bench_individual.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#include "fixtures.h"
#include "test_util.h"
#include <string.h>

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

int
bbs_bench_individual ()
{
#define CIPHERSUITE sha256
// Use an additional macro to ensure full expansion before concatenation
#define CONCAT_INTERNAL(a, b, c) a##_##b##_##c
#define BBS_EXECUTE(cipher_suite, function) CONCAT_INTERNAL(bbs, cipher_suite, function)

#define USE_HEADER 0

printf("Benchmarking %s \n", TOSTRING(CIPHERSUITE));
printf ("Include header: %d \n", USE_HEADER);

#define ITERATIONS 100

if (core_init () != RLC_OK)
{
core_clean ();
return 1;
}
if (pc_param_set_any () != RLC_OK)
{
core_clean ();
return 1;
}

// - MARK: Key generation
bbs_secret_key sk[ITERATIONS];
bbs_public_key pk[ITERATIONS];

printf ("%s key generation %d iterations.\n", TOSTRING(CIPHERSUITE), ITERATIONS);

BBS_BENCH_START (key_gen)
for (int i = 0; i < ITERATIONS; i++)
{
if (BBS_OK != BBS_EXECUTE(CIPHERSUITE, keygen_full) (sk[i], pk[i]))
{
puts ("Error during key generation");
return 1;
}
}
BBS_BENCH_END (key_gen, "Key generation (SK & PK)")

// - MARK: Signing

#define MSG_LEN 64

char msg1[ITERATIONS][MSG_LEN];
char msg2[ITERATIONS][MSG_LEN];
bbs_signature sig[ITERATIONS];

for (int i = 0; i < ITERATIONS; i++)
{
for (int j = 0; j < MSG_LEN; j++)
{
msg1[i][j] = (char) rand ();
msg2[i][j] = (char) rand ();
}
}
#if USE_HEADER
static char header[] = "But I am a header!";
#else
static char header[] = "";
#endif

printf ("%s signing %d iterations of %d messages each of size %d bytes.\n",
TOSTRING(CIPHERSUITE), ITERATIONS,
2,
MSG_LEN);

BBS_BENCH_START (sign)
for (int i = 0; i < ITERATIONS; i++)
{
if (BBS_OK != BBS_EXECUTE(CIPHERSUITE, sign) (sk[i], pk[i], sig[i], (uint8_t*) header, strlen (header), 2,
msg1[i], MSG_LEN, msg2[i], MSG_LEN))
{
puts ("Error during signing");
return 1;
}
}
BBS_BENCH_END (sign, "Signing")

// - MARK: Verification
printf ("%s verification %d iterations of %d messages each of size %d bytes.\n",
TOSTRING(CIPHERSUITE), ITERATIONS,
2,
MSG_LEN);
BBS_BENCH_START (verify)
for (int i = 0; i < ITERATIONS; i++)
{
if (BBS_OK != bbs_sha256_verify (pk[i], sig[i], (uint8_t*) header, strlen (header), 2,
msg1[i], MSG_LEN, msg2[i], MSG_LEN))
{
puts ("Error during signature verification");
return 1;
}
}
BBS_BENCH_END (verify, "Verification")

// - MARK: Proof generation
uint8_t proof[ITERATIONS][BBS_PROOF_LEN (1)];
uint64_t disclosed_indexes[] = {0};
#define RANDOM_NONCE_SIZE 23
static uint8_t random_nonces[ITERATIONS][RANDOM_NONCE_SIZE];
for (int i = 0; i < ITERATIONS; i++)
{
for (int j = 0; j < 23; j++)
{
random_nonces[i][j] = (uint8_t) rand ();
}
}

printf ("%s proof generation %d iterations of %d messages each of size %d bytes disclosing first message only.\n",
TOSTRING(CIPHERSUITE), ITERATIONS,
2,
MSG_LEN);
BBS_BENCH_START (proof_gen)
for (int i = 0; i < ITERATIONS; i++)
{
if (BBS_OK != BBS_EXECUTE(CIPHERSUITE, proof_gen) (pk[i],
sig[i],
proof[i],
(uint8_t*) header,
strlen (header),
(uint8_t*) random_nonces[i],
RANDOM_NONCE_SIZE,
disclosed_indexes,
1,
2,
msg1[i],
MSG_LEN,
msg2[i],
MSG_LEN))
{
puts ("Error during proof generation");
return 1;
}
}
BBS_BENCH_END (proof_gen, "Proof generation")

// - MARK: Proof verification
printf ("%s proof verification %d iterations of %d messages each of size %d bytes disclosing first message only.\n",
TOSTRING(CIPHERSUITE), ITERATIONS,
2,
MSG_LEN);
BBS_BENCH_START (proof_verify)
for (int i = 0; i < ITERATIONS; i++)
{
if (BBS_OK != bbs_sha256_proof_verify (pk[i],
proof[i],
BBS_PROOF_LEN (1),
(uint8_t*) header,
strlen (header),
(uint8_t*) random_nonces[i],
RANDOM_NONCE_SIZE,
disclosed_indexes,
1,
2,
msg1[i],
MSG_LEN))
{
puts ("Error during proof verification");
return 1;
}
}
BBS_BENCH_END (proof_verify, "Proof verification")

return 0;
}
Loading

0 comments on commit 2982606

Please sign in to comment.