Skip to content

Commit

Permalink
add tests for ensuring that ascon-{hash, hashA, xof, xofA} can be eva…
Browse files Browse the repository at this point in the history
…luated during compile-time itself, given static input message

Signed-off-by: Anjan Roy <[email protected]>
  • Loading branch information
itzmeanjan committed Oct 3, 2023
1 parent 0f19f85 commit 20053cd
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 8 deletions.
45 changes: 43 additions & 2 deletions tests/test_ascon_hash.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
#include "hashing/ascon_hash.hpp"
#include "test_common.hpp"
#include <array>
#include <fstream>
#include <gtest/gtest.h>
#include <span>

// Given a statically known input message, computes Ascon-Hash digest on it, returning
// hex-encoded character array as output, during program compilation time.
constexpr std::array<char, 2 * ascon_hash::DIGEST_LEN>
eval_ascon_hash()
{
// Statically defined input.
// Message = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
std::array<uint8_t, 32> data{};
std::iota(data.begin(), data.end(), 0);

// To be computed digest.
std::array<uint8_t, ascon_hash::DIGEST_LEN> md{};

ascon_hash::ascon_hash_t hasher;
hasher.absorb(data);
hasher.finalize();
hasher.digest(md);

// Returns hex-encoded digest.
return bytes_to_hex(md);
}

TEST(AsconHashing, CompileTimeEvalAsconHash)
{
// AsconHash("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f") =
// "2a4f6f2b6b3ec2a6c47ba08d18c8ea561b493c13ccb35803fa8b9fb00a0f1f35"
constexpr auto md = eval_ascon_hash();
constexpr auto flg = md == std::array<char, ascon_hash::DIGEST_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(
flg,
"Must be able to evaluate Ascon-Hash during program compilation time itself !");
EXPECT_TRUE(flg);
}

// Ensure that both oneshot and incremental way of absorbing same message produces same
// digest for Ascon-Hash.
inline void
Expand Down Expand Up @@ -47,7 +88,7 @@ test_ascon_hash(const size_t mlen)
hasher.digest(_dig_incremental);
}

ASSERT_EQ(dig_oneshot, dig_incremental);
EXPECT_EQ(dig_oneshot, dig_incremental);
}

TEST(AsconHashing, IncrementalMessageAbsorptionAsconHash)
Expand Down Expand Up @@ -96,7 +137,7 @@ kat_ascon_hash()
hasher.finalize();
hasher.digest(_digest);

ASSERT_EQ(digest, md);
EXPECT_EQ(digest, md);

std::string empty_line;
std::getline(file, empty_line);
Expand Down
45 changes: 43 additions & 2 deletions tests/test_ascon_hasha.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
#include "hashing/ascon_hasha.hpp"
#include "test_common.hpp"
#include <array>
#include <fstream>
#include <gtest/gtest.h>
#include <span>

// Given a statically known input message, computes Ascon-HashA digest on it, returning
// hex-encoded character array as output, during program compilation time.
constexpr std::array<char, 2 * ascon_hasha::DIGEST_LEN>
eval_ascon_hasha()
{
// Statically defined input.
// Message = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
std::array<uint8_t, 32> data{};
std::iota(data.begin(), data.end(), 0);

// To be computed digest.
std::array<uint8_t, ascon_hasha::DIGEST_LEN> md{};

ascon_hasha::ascon_hasha_t hasher;
hasher.absorb(data);
hasher.finalize();
hasher.digest(md);

// Returns hex-encoded digest.
return bytes_to_hex(md);
}

TEST(AsconHashing, CompileTimeEvalAsconHashA)
{
// AsconHashA("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f") =
// "3237cbcc617a2550583a50e8bad3dacda82562e06220150448c109008fa054a2"
constexpr auto md = eval_ascon_hasha();
constexpr auto flg = md == std::array<char, ascon_hasha::DIGEST_LEN * 2>{
'3', '2', '3', '7', 'c', 'b', 'c', 'c', '6', '1', '7', 'a', '2', '5', '5', '0',
'5', '8', '3', 'a', '5', '0', 'e', '8', 'b', 'a', 'd', '3', 'd', 'a', 'c', 'd',
'a', '8', '2', '5', '6', '2', 'e', '0', '6', '2', '2', '0', '1', '5', '0', '4',
'4', '8', 'c', '1', '0', '9', '0', '0', '8', 'f', 'a', '0', '5', '4', 'a', '2'
};

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

// Ensure that both oneshot and incremental way of absorbing same message produces same
// digest for Ascon-HashA.
inline void
Expand Down Expand Up @@ -47,7 +88,7 @@ test_ascon_hasha(const size_t mlen)
hasher.digest(_dig_incremental);
}

ASSERT_EQ(dig_oneshot, dig_incremental);
EXPECT_EQ(dig_oneshot, dig_incremental);
}

TEST(AsconHashing, IncrementalMessageAbsorptionAsconHashA)
Expand Down Expand Up @@ -96,7 +137,7 @@ kat_ascon_hasha()
hasher.finalize();
hasher.digest(_digest);

ASSERT_EQ(digest, md);
EXPECT_EQ(digest, md);

std::string empty_line;
std::getline(file, empty_line);
Expand Down
45 changes: 43 additions & 2 deletions tests/test_ascon_xof.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
#include "hashing/ascon_xof.hpp"
#include "test_common.hpp"
#include <array>
#include <fstream>
#include <gtest/gtest.h>
#include <span>

// Given a statically known input message, computes olen -bytes Ascon-Xof digest on it,
// returning hex-encoded character array as output, during program compilation time.
template<size_t olen = 32>
constexpr std::array<char, 2 * olen>
eval_ascon_xof()
{
// Statically defined input.
// Message = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
std::array<uint8_t, 32> data{};
std::iota(data.begin(), data.end(), 0);

// To be computed digest.
std::array<uint8_t, olen> md{};

ascon_xof::ascon_xof_t hasher;
hasher.absorb(data);
hasher.finalize();
hasher.squeeze(md);

// Returns hex-encoded digest.
return bytes_to_hex(md);
}

TEST(AsconHashing, CompileTimeEvalAsconXof)
{
// AsconXof("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f") =
// "0b8e325b9bbf1bb43e77aa1eed93bee62b4ea1e4b0c5a696b2f5c5b09c968918"
constexpr auto md = eval_ascon_xof();
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 be able to evaluate Ascon-Xof during program compilation time itself !");
EXPECT_TRUE(flg);
}

// Ensure that both oneshot and incremental way of absorbing same message and squeezing
// same length output, produces same digest for Ascon-Xof.
inline void
Expand Down Expand Up @@ -58,7 +99,7 @@ test_ascon_xof(const size_t mlen, const size_t dlen)
}
}

ASSERT_EQ(dig_oneshot, dig_incremental);
EXPECT_EQ(dig_oneshot, dig_incremental);
}

TEST(AsconHashing, IncrementalMessageAbsorptionSqueezingAsconXof)
Expand Down Expand Up @@ -109,7 +150,7 @@ kat_ascon_xof()
hasher.finalize();
hasher.squeeze(_digest);

ASSERT_EQ(digest, md);
EXPECT_EQ(digest, md);

std::string empty_line;
std::getline(file, empty_line);
Expand Down
45 changes: 43 additions & 2 deletions tests/test_ascon_xofa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,47 @@
#include <gtest/gtest.h>
#include <span>

// Given a statically known input message, computes olen -bytes Ascon-XofA digest on it,
// returning hex-encoded character array as output, during program compilation time.
template<size_t olen = 32>
constexpr std::array<char, 2 * olen>
eval_ascon_xofa()
{
// Statically defined input.
// Message = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
std::array<uint8_t, 32> data{};
std::iota(data.begin(), data.end(), 0);

// To be computed digest.
std::array<uint8_t, olen> md{};

ascon_xofa::ascon_xofa_t hasher;
hasher.absorb(data);
hasher.finalize();
hasher.squeeze(md);

// Returns hex-encoded digest.
return bytes_to_hex(md);
}

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

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

// Ensure that both oneshot and incremental way of absorbing same message and squeezing
// same length output, produces same digest for Ascon-XofA.
inline void
Expand Down Expand Up @@ -58,7 +99,7 @@ test_ascon_xofa(const size_t mlen, const size_t dlen)
}
}

ASSERT_EQ(dig_oneshot, dig_incremental);
EXPECT_EQ(dig_oneshot, dig_incremental);
}

TEST(AsconHashing, IncrementalMessageAbsorptionSqueezingAsconXofA)
Expand Down Expand Up @@ -109,7 +150,7 @@ kat_ascon_xofa()
hasher.finalize();
hasher.squeeze(_digest);

ASSERT_EQ(digest, md);
EXPECT_EQ(digest, md);

std::string empty_line;
std::getline(file, empty_line);
Expand Down

0 comments on commit 20053cd

Please sign in to comment.