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

Make Everything constexpr #20

Merged
merged 34 commits into from
Aug 6, 2023
Merged

Make Everything constexpr #20

merged 34 commits into from
Aug 6, 2023

Conversation

itzmeanjan
Copy link
Owner

Make whole implementation of SHA3 standard constexpr ( more @ https://en.cppreference.com/w/cpp/language/constexpr ) so that one can evaluate SHA3-{224, 256, 384, 512} and SHAKE-{128, 256} hash function/ xof on statically defined input data during compilation time itself.

#include "sha3_256.hpp"
#include <gtest/gtest.h>

constexpr std::array<uint8_t, sha3_256::DIGEST_LEN>
eval_sha3_256()
{
  // Statically defined input.
  std::array<uint8_t, sha3_256::DIGEST_LEN * 2> data{};
  std::iota(data.begin(), data.end(), 0);

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

  sha3_256::sha3_256_t hasher;
  hasher.absorb(data);
  hasher.finalize();
  hasher.digest(md);

  return md;
}

TEST(Sha3Hashing, CompileTimeEvalSha3_256)
{
  // Input  = 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f
  // Output = c8ad478f4e1dd9d47dfc3b985708d92db1f8db48fe9cddd459e63c321f490402

  constexpr auto md = eval_sha3_256();
  static_assert(md ==
                  std::array<uint8_t, sha3_256::DIGEST_LEN>{
                    200, 173, 71, 143, 78, 29,  217, 212, 125, 252, 59,
                    152, 87,  8,  217, 45, 177, 248, 219, 72,  254, 156,
                    221, 212, 89, 230, 60, 50,  31,  73,  4,   2 },
                "Must compute Sha3-256 hash during compile-time !");
}

@itzmeanjan itzmeanjan merged commit 2228e43 into master Aug 6, 2023
1 check passed
@itzmeanjan itzmeanjan deleted the make-constexpr branch August 6, 2023 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant