diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4e3c78b..bd95cde 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,12 +9,12 @@ 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) @@ -22,10 +22,10 @@ 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) @@ -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() diff --git a/test/bbs_bench_e2e.c b/test/bbs_bench_e2e.c new file mode 100644 index 0000000..fe1e309 --- /dev/null +++ b/test/bbs_bench_e2e.c @@ -0,0 +1,114 @@ +#include "fixtures.h" +#include "test_util.h" +#include + +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; +} \ No newline at end of file diff --git a/test/bbs_bench_individual.c b/test/bbs_bench_individual.c new file mode 100644 index 0000000..a4fb84a --- /dev/null +++ b/test/bbs_bench_individual.c @@ -0,0 +1,176 @@ +#include "fixtures.h" +#include "test_util.h" +#include + +#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; +} \ No newline at end of file diff --git a/test/bbs_e2e_bench.c b/test/bbs_e2e_bench.c deleted file mode 100644 index 62bd9be..0000000 --- a/test/bbs_e2e_bench.c +++ /dev/null @@ -1,108 +0,0 @@ -#include "fixtures.h" -#include "test_util.h" -#include - -int -bbs_e2e_bench () -{ - BBS_BENCH_START(e2e) - - if (core_init () != RLC_OK) - { - core_clean (); - return 1; - } - if (pc_param_set_any () != RLC_OK) - { - core_clean (); - return 1; - } - - 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; - static char msg1[] = "I am a message"; - static char msg2[] = "And so am I. Crazy..."; - // static char header[] = "But I am a header!"; - static char header[] = ""; - - if (BBS_OK != bbs_sha256_sign (sk, - pk, - sig, - (uint8_t*) header, - strlen (header), - 2, - msg1, - strlen (msg1), - msg2, - strlen (msg2))) - { - puts ("Error during signing"); - return 1; - } - - if (BBS_OK != bbs_sha256_verify (pk, - sig, - (uint8_t*) header, - strlen (header), - 2, - msg1, - strlen (msg1), - msg2, - strlen (msg2))) - { - 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, - strlen (msg1), - msg2, - strlen (msg2))) - { - 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, - strlen (msg1))) - { - puts ("Error during proof verification"); - return 1; - } - - BBS_BENCH_END(e2e, "bbs_e2e_sign_n_proof") - - return 0; -} \ No newline at end of file