CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
127 lines
4.1 KiB
C++
127 lines
4.1 KiB
C++
//===- llvm/unittest/Support/BLAKE3Test.cpp - BLAKE3 tests ----------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements unit tests for the BLAKE3 functions.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Support/BLAKE3.h"
|
|
#include "llvm/ADT/StringExtras.h"
|
|
#include "llvm/Support/HashBuilder.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
|
|
/// Tests an arbitrary set of bytes passed as \p Input.
|
|
void TestBLAKE3Sum(ArrayRef<uint8_t> Input, StringRef Final) {
|
|
BLAKE3 Hash;
|
|
Hash.update(Input);
|
|
auto hash = Hash.final();
|
|
auto hashStr = toHex(hash);
|
|
EXPECT_EQ(hashStr, Final);
|
|
}
|
|
|
|
using KV = std::pair<const char *, const char *>;
|
|
|
|
TEST(BLAKE3Test, BLAKE3) {
|
|
std::array<KV, 5> testvectors{
|
|
KV{"",
|
|
"AF1349B9F5F9A1A6A0404DEA36DCC9499BCB25C9ADC112B7CC9A93CAE41F3262"},
|
|
KV{"a",
|
|
"17762FDDD969A453925D65717AC3EEA21320B66B54342FDE15128D6CAF21215F"},
|
|
KV{"abc",
|
|
"6437B3AC38465133FFB63B75273A8DB548C558465D79DB03FD359C6CD5BD9D85"},
|
|
KV{"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
|
"C19012CC2AAF0DC3D8E5C45A1B79114D2DF42ABB2A410BF54BE09E891AF06FF8"},
|
|
KV{"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklm"
|
|
"nopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
|
|
"553E1AA2A477CB3166E6AB38C12D59F6C5017F0885AAF079F217DA00CFCA363F"}};
|
|
|
|
for (auto input_expected : testvectors) {
|
|
auto str = std::get<0>(input_expected);
|
|
auto expected = std::get<1>(input_expected);
|
|
TestBLAKE3Sum({reinterpret_cast<const uint8_t *>(str), strlen(str)},
|
|
expected);
|
|
}
|
|
|
|
std::string rep(1000, 'a');
|
|
BLAKE3 Hash;
|
|
for (int i = 0; i < 1000; ++i) {
|
|
Hash.update({reinterpret_cast<const uint8_t *>(rep.data()), rep.size()});
|
|
}
|
|
auto hash = Hash.final();
|
|
auto hashStr = toHex(hash);
|
|
EXPECT_EQ(hashStr,
|
|
"616F575A1B58D4C9797D4217B9730AE5E6EB319D76EDEF6549B46F4EFE31FF8B");
|
|
|
|
// Using generic HashBuilder.
|
|
HashBuilder<BLAKE3, llvm::endianness::native> HashBuilder;
|
|
HashBuilder.update(std::get<0>(testvectors[2]));
|
|
BLAKE3Result<> HBHash1 = HashBuilder.final();
|
|
BLAKE3Result<> HBHash2 = HashBuilder.result();
|
|
EXPECT_EQ(std::get<1>(testvectors[2]), toHex(HBHash1));
|
|
EXPECT_EQ(std::get<1>(testvectors[2]), toHex(HBHash2));
|
|
}
|
|
|
|
TEST(BLAKE3Test, SmallerHashSize) {
|
|
const char *InputStr = "abc";
|
|
ArrayRef<uint8_t> Input(reinterpret_cast<const uint8_t *>(InputStr),
|
|
strlen(InputStr));
|
|
BLAKE3 Hash;
|
|
Hash.update(Input);
|
|
auto hash1 = Hash.final<16>();
|
|
auto hash2 = BLAKE3::hash<16>(Input);
|
|
auto hashStr1 = toHex(hash1);
|
|
auto hashStr2 = toHex(hash2);
|
|
EXPECT_EQ(hashStr1, hashStr2);
|
|
EXPECT_EQ(hashStr1, "6437B3AC38465133FFB63B75273A8DB5");
|
|
|
|
// Using generic HashBuilder.
|
|
HashBuilder<TruncatedBLAKE3<16>, llvm::endianness::native> HashBuilder;
|
|
HashBuilder.update(Input);
|
|
BLAKE3Result<16> hash3 = HashBuilder.final();
|
|
BLAKE3Result<16> hash4 = HashBuilder.result();
|
|
EXPECT_EQ(hashStr1, toHex(hash3));
|
|
EXPECT_EQ(hashStr1, toHex(hash4));
|
|
}
|
|
|
|
TEST(BLAKE3Test, InitKeyed) {
|
|
const char *InputStr = "abc";
|
|
ArrayRef<uint8_t> Input(reinterpret_cast<const uint8_t *>(InputStr),
|
|
strlen(InputStr));
|
|
BLAKE3 Hash1;
|
|
uint8_t key1[32] = {0};
|
|
key1[0] = 'a';
|
|
key1[1] = 'b';
|
|
key1[2] = 'c';
|
|
key1[3] = 'd';
|
|
Hash1.init_keyed(key1);
|
|
Hash1.update(Input);
|
|
auto hash1 = Hash1.final<16>();
|
|
auto hashStr1 = toHex(hash1);
|
|
|
|
BLAKE3 Hash2;
|
|
uint8_t key2[32] = {0};
|
|
key2[0] = 'x';
|
|
key2[1] = 'y';
|
|
key2[2] = 'z';
|
|
key2[3] = 't';
|
|
Hash2.init_keyed(key2);
|
|
Hash2.update(Input);
|
|
auto hash2 = Hash2.final<16>();
|
|
auto hashStr2 = toHex(hash2);
|
|
|
|
ASSERT_NE(hashStr1, hashStr2);
|
|
EXPECT_EQ(hashStr1, "667DB3821E1AAABA03ADFE1F4F803DF7");
|
|
EXPECT_EQ(hashStr2, "B683D7EB0B441AC11FCAC1199B911053");
|
|
}
|
|
|
|
} // namespace
|