Skip to content

Instantly share code, notes, and snippets.

@MarekKnapek
Created August 14, 2023 02:36
Show Gist options
  • Save MarekKnapek/80fc270efabff19125047c867563936a to your computer and use it in GitHub Desktop.
Save MarekKnapek/80fc270efabff19125047c867563936a to your computer and use it in GitHub Desktop.
BLAKE3 constexpr
#include "mk_lib_crypto_hash_stream_blake3.h"
#include <array> /* std::array */
#include <string_view> /* std::string_view */
constexpr auto blake3_constexpr(std::string_view const& str_a, char const& char_a, std::string_view const& str_b, char const& char_b)
{
mk_lib_crypto_hash_stream_blake3_t hash{};
std::size_t i{};
std::array<unsigned char, 80> helper_arr{};
mk_lib_crypto_hash_block_blake3_digest_t digest{};
std::array<unsigned char, mk_lib_crypto_hash_block_blake3_digest_len> ret{};
mk_lib_crypto_hash_stream_blake3_init(&hash);
for(i = 0; i != str_a.size(); ++i){ helper_arr[i] = ((unsigned char)(str_a[i])); }
mk_lib_crypto_hash_stream_blake3_append(&hash, helper_arr.data(), str_a.size());
helper_arr[0] = ((unsigned char)(char_a));
mk_lib_crypto_hash_stream_blake3_append(&hash, helper_arr.data(), 1);
for(i = 0; i != str_b.size(); ++i){ helper_arr[i] = ((unsigned char)(str_b[i])); }
mk_lib_crypto_hash_stream_blake3_append(&hash, helper_arr.data(), str_b.size());
helper_arr[0] = ((unsigned char)(char_b));
mk_lib_crypto_hash_stream_blake3_append(&hash, helper_arr.data(), 1);
mk_lib_crypto_hash_stream_blake3_finish(&hash, &digest);
for(i = 0; i != mk_lib_crypto_hash_block_blake3_digest_len; ++i){ mk_sl_cui_uint8_to_bi_uchar(&digest.m_uint8s[i], &ret[i]); }
return ret;
}
int main(void)
{
constexpr static std::string_view const str_a = "abcdef";
constexpr static char const char_a = 'g';
constexpr static std::string_view const str_b = "hijklmnopqrstuvwxy";
constexpr static char const char_b = 'z';
constexpr static auto const blake3_digest = blake3_constexpr(str_a, char_a, str_b, char_b);
static_assert(blake3_digest[0] == 0x24, "");
static_assert(blake3_digest[1] == 0x68, "");
static_assert(blake3_digest[2] == 0xee, "");
static_assert(blake3_digest[3] == 0xc8, "");
static_assert(blake3_digest[4] == 0x89, "");
static_assert(blake3_digest[5] == 0x4a, "");
static_assert(blake3_digest[6] == 0xcf, "");
static_assert(blake3_digest[7] == 0xb4, "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment