Skip to content

Commit

Permalink
Add compile-time evaluated tests to ensure that the library is `const…
Browse files Browse the repository at this point in the history
…expr`

Signed-off-by: Anjan Roy <[email protected]>
  • Loading branch information
itzmeanjan committed Dec 4, 2024
1 parent ea12463 commit f154991
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 22 deletions.
32 changes: 32 additions & 0 deletions tests/prop_test_ascon_aead128.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
#include "ascon/aead/ascon_aead128.hpp"
#include "test_helper.hpp"
#include <array>
#include <gtest/gtest.h>

constexpr bool
eval_encrypt_decrypt()
{
constexpr size_t ASSOCIATED_DATA_BYTE_LEN = 32;
constexpr size_t PLAIN_TEXT_BYTE_LEN = 32;

std::array<uint8_t, ascon_aead128::KEY_BYTE_LEN> key;
std::array<uint8_t, ascon_aead128::NONCE_BYTE_LEN> nonce;
std::array<uint8_t, ASSOCIATED_DATA_BYTE_LEN> associated_data;
std::array<uint8_t, PLAIN_TEXT_BYTE_LEN> plain_text;

std::iota(key.begin(), key.end(), 0);
std::iota(nonce.begin(), nonce.end(), 0);
std::iota(associated_data.begin(), associated_data.end(), 0);
std::iota(plain_text.begin(), plain_text.end(), 0);

std::array<uint8_t, PLAIN_TEXT_BYTE_LEN> cipher_text;
std::array<uint8_t, PLAIN_TEXT_BYTE_LEN> deciphered_text;
std::array<uint8_t, ascon_aead128::TAG_BYTE_LEN> tag;

ascon_aead128::encrypt(key, nonce, associated_data, plain_text, cipher_text, tag);
const bool is_decrypted = ascon_aead128::decrypt(key, nonce, associated_data, cipher_text, deciphered_text, tag);

return is_decrypted;
}

TEST(AsconAEAD128, CompileTimeEncryptAndThenDecrypt)
{
static_assert(eval_encrypt_decrypt(), "Must be able to encrypt and then decrypt using Ascon-AEAD128 during program compilation time itself !");
}

TEST(AsconAEAD128, EncryptThenDecrypt)
{
for (size_t associated_data_len = MIN_AD_LEN; associated_data_len <= MAX_AD_LEN; associated_data_len++) {
Expand Down
23 changes: 12 additions & 11 deletions tests/prop_test_ascon_hash256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ constexpr std::array<char, 2 * ascon_hash256::DIGEST_BYTE_LEN>
eval_ascon_hash256()
{
// Statically defined input.
// Message = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
// Message = 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
std::array<uint8_t, 32> data{};
std::iota(data.begin(), data.end(), 0);

Expand All @@ -27,17 +27,18 @@ eval_ascon_hash256()

TEST(AsconHash256, CompileTimeComputeMessageDigest)
{
// AsconHash256("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f") =
// "2a4f6f2b6b3ec2a6c47ba08d18c8ea561b493c13ccb35803fa8b9fb00a0f1f35"
constexpr auto expected_md = std::array<char, ascon_hash256::DIGEST_BYTE_LEN * 2>{
'B', 'D', '9', 'D', '3', 'D', '6', '0', 'A', '6', '6', 'B', '5', '3', '8', '6', '8', 'E', 'A', 'B', '2', 'A',
'5', 'C', '7', '4', '5', '3', '9', 'A', '5', '1', '8', 'A', '1', 'F', '6', '0', 'F', '0', '1', 'E', 'B', '1',
'7', '6', 'C', '6', '0', 'E', '4', '3', 'D', 'E', 'E', '8', '1', '6', '8', '0', 'B', '3', '3', 'E',
};

// AsconHash256("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F") = "BD9D3D60A66B53868EAB2A5C74539A518A1F60F01EB176C60E43DEE81680B33E"
constexpr auto md = eval_ascon_hash256();
constexpr auto is_match =
md == std::array<char, ascon_hash256::DIGEST_BYTE_LEN * 2>{ '2', 'a', '4', 'f', '6', 'f', '2', 'b', '6', 'b', '3', 'e', 'c', '2', 'a', '6',
'c', '4', '7', 'b', 'a', '0', '8', 'd', '1', '8', 'c', '8', 'e', 'a', '5', '6',
'1', 'b', '4', '9', '3', 'c', '1', '3', 'c', 'c', 'b', '3', '5', '8', '0', '3',
'f', 'a', '8', 'b', '9', 'f', 'b', '0', '0', 'a', '0', 'f', '1', 'f', '3', '5' };

static_assert(!is_match, "Must not be able to evaluate Ascon-Hash256 correctly, as expected output is wrong. I'll update it !");
EXPECT_FALSE(is_match);
constexpr auto is_matching = md == expected_md;

static_assert(is_matching, "Must be able to evaluate Ascon-Hash256 during program compilation time itself !");
EXPECT_TRUE(is_matching);
}

TEST(AsconHash256, ForSameMessageOneshotHashingAndIncrementalHashingProducesSameDigest)
Expand Down
23 changes: 13 additions & 10 deletions tests/prop_test_ascon_xof128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ constexpr std::array<char, 2 * olen>
eval_ascon_xof128()
{
// Statically defined input.
// Message = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
// Message = 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
std::array<uint8_t, 32> data{};
std::iota(data.begin(), data.end(), 0);

Expand All @@ -28,15 +28,18 @@ eval_ascon_xof128()

TEST(AsconXof128, CompileTimeComputeXofOutput)
{
// AsconXof128("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f") =
// "0b8e325b9bbf1bb43e77aa1eed93bee62b4ea1e4b0c5a696b2f5c5b09c968918"
constexpr auto md = eval_ascon_xof128();
constexpr auto flg = md == std::array<char, 64>{ '0', 'b', '8', 'e', '3', '2', '5', 'b', '9', 'b', 'b', 'f', '1', 'b', 'b', '4', '3', 'e', '7', '7', 'a', 'a',
'1', 'e', 'e', 'd', '9', '3', 'b', 'e', 'e', '6', '2', 'b', '4', 'e', 'a', '1', 'e', '4', 'b', '0', 'c', '5',
'a', '6', '9', '6', 'b', '2', 'f', '5', 'c', '5', 'b', '0', '9', 'c', '9', '6', '8', '9', '1', '8' };

static_assert(!flg, "Must not be able to evaluate Ascon-Xof128 correctly, as expected output is wrong. I'll update it !");
EXPECT_FALSE(flg);
constexpr auto expected_output = std::array<char, 64>{
'2', 'E', '5', 'F', '3', '4', '0', '3', 'F', '4', '1', '7', '1', '4', '7', '1', 'C', 'C', '7', '9', '3', '4',
'B', '5', '1', '9', '8', '2', 'C', 'E', 'C', 'E', '8', 'D', '6', '6', '2', '8', '4', '3', '5', 'D', 'B', '7',
'0', 'E', '8', '9', '8', '8', '0', 'F', '3', 'B', 'E', '4', 'E', '0', 'B', '7', 'B', '0', '5', '2',
};

// AsconXof128("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F") = "2E5F3403F4171471CC7934B51982CECE8D6628435DB70E89880F3BE4E0B7B052"
constexpr auto output = eval_ascon_xof128();
constexpr auto is_matching = output == expected_output;

static_assert(is_matching, "Must be able to evaluate Ascon-Xof128 during program compilation time itself !");
EXPECT_TRUE(is_matching);
}

TEST(AsconXof128, ForSameMessageOneshotHashingAndIncrementalHashingProducesSameOutput)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ template<size_t L>
constexpr std::array<char, L * 2>
bytes_to_hex(std::array<uint8_t, L> bytes)
{
constexpr std::array<char, 16> table{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
constexpr std::array<char, 16> table{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

std::array<char, bytes.size() * 2> hex{};

Expand Down

0 comments on commit f154991

Please sign in to comment.