Skip to content

Commit

Permalink
tests: start separating tests by cipher suite
Browse files Browse the repository at this point in the history
  • Loading branch information
schanzen committed Dec 6, 2024
1 parent 325a722 commit 516ce79
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 213 deletions.
16 changes: 13 additions & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ 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-fixtures-suite-bls12-381-shake-256 ${fixture-tests} fixtures.c)
target_compile_definitions(bbs-test-fixtures-suite-bls12-381-shake-256 PUBLIC LIBBBS_TEST_SUITE_SHAKE256)
add_executable(bbs-test-fixtures-suite-bls12-381-sha-256 ${fixture-tests} fixtures.c)
target_compile_definitions(bbs-test-fixtures-suite-bls12-381-sha-256 PUBLIC LIBBBS_TEST_SUITE_SHA256)
#add_executable(bbs-test-fixtures ${fixture-tests} fixtures.c)
target_link_libraries(bbs-test-fixtures-suite-bls12-381-sha-256 PRIVATE bbs)
target_link_libraries(bbs-test-fixtures-suite-bls12-381-shake-256 PRIVATE bbs)

add_executable(bbs-test-e2e ${e2e-tests})
target_link_libraries(bbs-test-e2e PRIVATE bbs)
Expand All @@ -29,8 +34,13 @@ add_custom_target(bench COMMAND bbs-test-bench)

foreach(test ${BBS_FIX_TESTS})
get_filename_component(TName ${test} NAME_WE)
add_test(NAME ${TName} COMMAND bbs-test-fixtures ${TName})
add_test(NAME ${TName}-suite-bls12-381-shake-256 COMMAND bbs-test-fixtures-suite-bls12-381-shake-256 ${TName})
endforeach()
foreach(test ${BBS_FIX_TESTS})
get_filename_component(TName ${test} NAME_WE)
add_test(NAME ${TName}-suite-bls12-381-sha-256 COMMAND bbs-test-fixtures-suite-bls12-381-sha-256 ${TName})
endforeach()


foreach(test ${BBS_E2E_TESTS})
get_filename_component(TName ${test} NAME_WE)
Expand Down
135 changes: 64 additions & 71 deletions test/bbs_fix_generators.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,97 +10,90 @@ typedef struct
int
bbs_fix_generators ()
{
bbs_cipher_suite_t *cipher_suites[] = {
bbs_sha256_cipher_suite, bbs_shake256_cipher_suite
};

bbs_fix_generators_fixture_t test_cases[] = {
{
.q_1 = fixture_bls12_381_sha_256_Q_1,
.hs = {
fixture_bls12_381_sha_256_H_1, fixture_bls12_381_sha_256_H_2,
fixture_bls12_381_sha_256_H_3, fixture_bls12_381_sha_256_H_4,
fixture_bls12_381_sha_256_H_5, fixture_bls12_381_sha_256_H_6,
fixture_bls12_381_sha_256_H_7, fixture_bls12_381_sha_256_H_8,
fixture_bls12_381_sha_256_H_9, fixture_bls12_381_sha_256_H_10
},
#ifdef LIBBBS_TEST_SUITE_SHAKE256
bbs_cipher_suite_t *cipher_suite = bbs_shake256_cipher_suite;
bbs_fix_generators_fixture_t fixture = {
.q_1 = fixture_bls12_381_shake_256_Q_1,
.hs = {
fixture_bls12_381_shake_256_H_1, fixture_bls12_381_shake_256_H_2,
fixture_bls12_381_shake_256_H_3, fixture_bls12_381_shake_256_H_4,
fixture_bls12_381_shake_256_H_5, fixture_bls12_381_shake_256_H_6,
fixture_bls12_381_shake_256_H_7, fixture_bls12_381_shake_256_H_8,
fixture_bls12_381_shake_256_H_9, fixture_bls12_381_shake_256_H_10
},
{
.q_1 = fixture_bls12_381_shake_256_Q_1,
.hs = {
fixture_bls12_381_shake_256_H_1, fixture_bls12_381_shake_256_H_2,
fixture_bls12_381_shake_256_H_3, fixture_bls12_381_shake_256_H_4,
fixture_bls12_381_shake_256_H_5, fixture_bls12_381_shake_256_H_6,
fixture_bls12_381_shake_256_H_7, fixture_bls12_381_shake_256_H_8,
fixture_bls12_381_shake_256_H_9, fixture_bls12_381_shake_256_H_10
},
};
#elif LIBBBS_TEST_SUITE_SHA256
bbs_cipher_suite_t *cipher_suite = bbs_sha256_cipher_suite;
bbs_fix_generators_fixture_t fixture = {
.q_1 = fixture_bls12_381_sha_256_Q_1,
.hs = {
fixture_bls12_381_sha_256_H_1, fixture_bls12_381_sha_256_H_2,
fixture_bls12_381_sha_256_H_3, fixture_bls12_381_sha_256_H_4,
fixture_bls12_381_sha_256_H_5, fixture_bls12_381_sha_256_H_6,
fixture_bls12_381_sha_256_H_7, fixture_bls12_381_sha_256_H_8,
fixture_bls12_381_sha_256_H_9, fixture_bls12_381_sha_256_H_10
},
};
#endif


printf("Testing %s\n", cipher_suite->cipher_suite_id);

for (int i = 0; i < 2; i++)
if (core_init () != RLC_OK)
{
core_clean ();
return 1;
}
if (pc_param_set_any () != RLC_OK)
{
bbs_cipher_suite_t *cipher_suite = cipher_suites[i];
bbs_fix_generators_fixture_t fixture = test_cases[i];
core_clean ();
return 1;
}

printf("Testing %s\n", cipher_suite->cipher_suite_id);
uint8_t state[48 + 8];
uint8_t bin[BBS_G1_ELEM_LEN];
ep_t generator;
ep_null (generator);
RLC_TRY {
ep_new (generator); // Yes, this might leak. This is a test and thus
// short lived
}
RLC_CATCH_ANY { puts ("Internal Error"); return 1; }

if (core_init () != RLC_OK)
{
core_clean ();
return 1;
}
if (pc_param_set_any () != RLC_OK)
{
core_clean ();
return 1;
}
const uint8_t *api_id = (uint8_t *) cipher_suite->api_id;
const uint8_t api_id_len = cipher_suite->api_id_len;

uint8_t state[48 + 8];
uint8_t bin[BBS_G1_ELEM_LEN];
ep_t generator;
ep_null (generator);
RLC_TRY {
ep_new (generator); // Yes, this might leak. This is a test and thus
// short lived
}
RLC_CATCH_ANY { puts ("Internal Error"); return 1; }
if (BBS_OK != create_generator_init (cipher_suite, state, api_id, api_id_len))
{
puts ("Error during generator initialization");
return 1;
}

const uint8_t *api_id = (uint8_t *) cipher_suite->api_id;
const uint8_t api_id_len = cipher_suite->api_id_len;
DEBUG("TEST", state, 56);

if (BBS_OK != create_generator_init (cipher_suite, state, api_id, api_id_len))
{
puts ("Error during generator initialization");
return 1;
}
if (BBS_OK != create_generator_next (cipher_suite, state, generator, api_id,
api_id_len))
{
puts ("Error during generator Q_1 creation");
return 1;
}
RLC_TRY {
ep_write_bbs (bin, generator);
} RLC_CATCH_ANY { puts ("Internal Error"); return 1; }

DEBUG("TEST", state, 56);
ASSERT_EQ_PTR ("generator Q_1 creation", bin, fixture.q_1, BBS_G1_ELEM_LEN);

for (int j = 0; j < 10; j++) {
if (BBS_OK != create_generator_next (cipher_suite, state, generator, api_id,
api_id_len))
api_id_len))
{
puts ("Error during generator Q_1 creation");
printf ("Error during generator %d creation", j + 1);
return 1;
}
RLC_TRY {
ep_write_bbs (bin, generator);
} RLC_CATCH_ANY { puts ("Internal Error"); return 1; }

ASSERT_EQ_PTR ("generator Q_1 creation", bin, fixture.q_1, BBS_G1_ELEM_LEN);

for (int j = 0; j < 10; j++) {
if (BBS_OK != create_generator_next (cipher_suite, state, generator, api_id,
api_id_len))
{
printf ("Error during generator %d creation", j + 1);
return 1;
}
RLC_TRY {
ep_write_bbs (bin, generator);
} RLC_CATCH_ANY { puts ("Internal Error"); return 1; }
ASSERT_EQ_PTR ("generator creation", bin, fixture.hs[j], BBS_G1_ELEM_LEN);
}
ASSERT_EQ_PTR ("generator creation", bin, fixture.hs[j], BBS_G1_ELEM_LEN);
}
return 0;
}
88 changes: 42 additions & 46 deletions test/bbs_fix_keygen.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,43 @@ typedef struct
{
bbs_cipher_suite_t *cipher_suite;
uint8_t *key_material;
uint16_t key_material_len;
uint16_t key_material_len;
uint8_t *key_info;
uint16_t key_info_len;
uint16_t key_info_len;
uint8_t *key_dst;
uint16_t key_dst_len;
uint16_t key_dst_len;
uint8_t *expected_SK;
uint8_t *expected_PK;
} bbs_fix_keygen_fixture_t;

int
bbs_fix_keygen ()
{
bbs_fix_keygen_fixture_t test_cases[] = {
{
.cipher_suite = bbs_sha256_cipher_suite,
.key_material = fixture_bls12_381_sha_256_key_material,
.key_material_len = sizeof(fixture_bls12_381_sha_256_key_material),
.key_info = fixture_bls12_381_sha_256_key_info,
.key_info_len = sizeof(fixture_bls12_381_sha_256_key_info),
.key_dst = fixture_bls12_381_sha_256_key_dst,
.key_dst_len = sizeof(fixture_bls12_381_sha_256_key_dst),
.expected_SK = fixture_bls12_381_sha_256_SK,
.expected_PK = fixture_bls12_381_sha_256_PK,
},{
.cipher_suite = bbs_shake256_cipher_suite,
.key_material = fixture_bls12_381_shake_256_key_material,
.key_material_len = sizeof(fixture_bls12_381_shake_256_key_material),
.key_info = fixture_bls12_381_shake_256_key_info,
.key_info_len = sizeof(fixture_bls12_381_shake_256_key_info),
.key_dst = fixture_bls12_381_shake_256_key_dst,
.key_dst_len = sizeof(fixture_bls12_381_shake_256_key_dst),
.expected_SK = fixture_bls12_381_shake_256_SK,
.expected_PK = fixture_bls12_381_shake_256_PK,
}
#ifdef LIBBBS_TEST_SUITE_SHAKE256
bbs_fix_keygen_fixture_t fixture = {
.cipher_suite = bbs_shake256_cipher_suite,
.key_material = fixture_bls12_381_shake_256_key_material,
.key_material_len = sizeof(fixture_bls12_381_shake_256_key_material),
.key_info = fixture_bls12_381_shake_256_key_info,
.key_info_len = sizeof(fixture_bls12_381_shake_256_key_info),
.key_dst = fixture_bls12_381_shake_256_key_dst,
.key_dst_len = sizeof(fixture_bls12_381_shake_256_key_dst),
.expected_SK = fixture_bls12_381_shake_256_SK,
.expected_PK = fixture_bls12_381_shake_256_PK,
};

#elif LIBBBS_TEST_SUITE_SHA256
bbs_fix_keygen_fixture_t fixture = {
.cipher_suite = bbs_sha256_cipher_suite,
.key_material = fixture_bls12_381_sha_256_key_material,
.key_material_len = sizeof(fixture_bls12_381_sha_256_key_material),
.key_info = fixture_bls12_381_sha_256_key_info,
.key_info_len = sizeof(fixture_bls12_381_sha_256_key_info),
.key_dst = fixture_bls12_381_sha_256_key_dst,
.key_dst_len = sizeof(fixture_bls12_381_sha_256_key_dst),
.expected_SK = fixture_bls12_381_sha_256_SK,
.expected_PK = fixture_bls12_381_sha_256_PK,
};
#endif
if (core_init () != RLC_OK)
{
core_clean ();
Expand All @@ -52,28 +53,23 @@ bbs_fix_keygen ()
return 1;
}

for (int i = 0; i < 2; i++)
bbs_secret_key sk;
if (BBS_OK != bbs_keygen(fixture.cipher_suite, sk, fixture.key_material, fixture.key_material_len
, fixture.key_info,
fixture.key_info_len, fixture.key_dst,
fixture.key_dst_len))
{
bbs_fix_keygen_fixture_t fixture = test_cases[i];

bbs_secret_key sk;
if (BBS_OK != bbs_keygen(fixture.cipher_suite, sk, fixture.key_material, fixture.key_material_len
, fixture.key_info,
fixture.key_info_len, fixture.key_dst,
fixture.key_dst_len))
{
puts ("Error during secret key generation");
return 1;
}
ASSERT_EQ_PTR ("secret key generation", sk, fixture.expected_SK, BBS_SK_LEN);
puts ("Error during secret key generation");
return 1;
}
ASSERT_EQ_PTR ("secret key generation", sk, fixture.expected_SK, BBS_SK_LEN);

bbs_public_key pk;
if (BBS_OK != bbs_sk_to_pk (fixture.cipher_suite, fixture.expected_SK, pk))
{
puts ("Error during public key generation");
return 1;
}
ASSERT_EQ_PTR ("public key generation", pk, fixture.expected_PK, BBS_PK_LEN);
bbs_public_key pk;
if (BBS_OK != bbs_sk_to_pk (fixture.cipher_suite, fixture.expected_SK, pk))
{
puts ("Error during public key generation");
return 1;
}
ASSERT_EQ_PTR ("public key generation", pk, fixture.expected_PK, BBS_PK_LEN);
return 0;
}
22 changes: 8 additions & 14 deletions test/bbs_fix_msg_scalars.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ typedef struct
int
bbs_fix_msg_scalars ()
{
fixture_msg_scalar test_cases[] = {
{
.cipher_suite = bbs_sha256_cipher_suite,
fixture_msg_scalar fixture = {
#ifdef LIBBBS_TEST_SUITE_SHA256
.cipher_suite = bbs_sha256_cipher_suite,
.msg = {
fixture_bls12_381_sha_256_msg_scalar_1,
fixture_bls12_381_sha_256_msg_scalar_2,
Expand All @@ -25,11 +25,10 @@ bbs_fix_msg_scalars ()
fixture_bls12_381_sha_256_msg_scalar_8,
fixture_bls12_381_sha_256_msg_scalar_9,
fixture_bls12_381_sha_256_msg_scalar_10,
},
},
.msg_len = sizeof(fixture_bls12_381_sha_256_msg_scalar_1)
},
{
.cipher_suite = bbs_shake256_cipher_suite,
#elif LIBBBS_TEST_SUITE_SHAKE256
.cipher_suite = bbs_shake256_cipher_suite,
.msg = {
fixture_bls12_381_shake_256_msg_scalar_1,
fixture_bls12_381_shake_256_msg_scalar_2,
Expand All @@ -41,9 +40,9 @@ bbs_fix_msg_scalars ()
fixture_bls12_381_shake_256_msg_scalar_8,
fixture_bls12_381_shake_256_msg_scalar_9,
fixture_bls12_381_shake_256_msg_scalar_10,
},
},
.msg_len = sizeof(fixture_bls12_381_shake_256_msg_scalar_1)
}
#endif
};

uint8_t *fixture_ms[10] = {
Expand All @@ -56,9 +55,6 @@ bbs_fix_msg_scalars ()
sizeof(fixture_m_9), sizeof(fixture_m_10)
};

for (int cipher_suite_index = 0; cipher_suite_index < 2; cipher_suite_index++)
{
fixture_msg_scalar fixture = test_cases[cipher_suite_index];
if (core_init () != RLC_OK)
{
core_clean ();
Expand Down Expand Up @@ -96,8 +92,6 @@ bbs_fix_msg_scalars ()
} RLC_CATCH_ANY { puts ("Internal Error"); return 1; }
ASSERT_EQ_PTR ("scalar 1 generation", bin, fixture.msg[i], fixture.msg_len);
}
}

bn_free (scalar);
return 0;
}
Loading

0 comments on commit 516ce79

Please sign in to comment.