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

Benchmark comparisons #10

Merged
Merged
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
39 changes: 33 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,50 @@ Provides a library `libbbs` implementing the `BLS12381-SHA-256` and `BLS12-381-S
Dependencies:

- `gmp`
- `xsltproc` (indirect dependency of XKCP Keccak hash library)
- `cmake` (build only)

### Installation

```zsh
mkdir build
cd build
cmake ..
```

### Installation

Within `build`:

```zsh
make install
```

### Test

Within `build`:

```zsh
mkdir build
cd build
cmake ..
make -j
make
make test
```

### Benchmark

Within `build`:

```zsh
make bench
```

Benchmark on Apple M1 Pro 2021 16GB:

- 2 messages each of size 64 bytes
- Disclosing first message only
- Runtime averaged over 1000 iterations without warmup

| Function | SHA256 (ms) | SHAKE256 (ms) |
| ------------------------ | ----------- | ------------- |
| key generation (SK & PK) | 0,174 | 0,172 |
| sign | 1,995 | 1,888 |
| verify | 3,877 | 3,829 |
| proof generation | 3,289 | 3,220 |
| proof verification | 4,532 | 4,482 |
18 changes: 12 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +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_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 @@ -35,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()
111 changes: 111 additions & 0 deletions test/bbs_bench_e2e.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#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[MSG_LEN_END];
char msg2[MSG_LEN_END];
for (int j = 0; j < MSG_LEN_END; j++)
{
msg1[j] = (char) rand ();
msg2[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, msg_len,
msg2, msg_len))
{
puts ("Error during signing");
return 1;
}

if (BBS_OK != bbs_sha256_verify (pk, sig, (uint8_t*) header,
strlen (header), 2, msg1,
msg_len, msg2, 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, msg_len, msg2,
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, msg_len))
{
puts ("Error during proof verification");
return 1;
}
}
BBS_BENCH_END (e2e, "bbs_e2e_sign_n_proof")
}
}

return 0;
}
165 changes: 165 additions & 0 deletions test/bbs_bench_individual.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#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 1000

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_EXECUTE (CIPHERSUITE, 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_EXECUTE(CIPHERSUITE, 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