1 //===- llvm/unittest/Support/BLAKE3Test.cpp - BLAKE3 tests ----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements unit tests for the BLAKE3 functions.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/BLAKE3.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/Support/HashBuilder.h"
16 #include "gtest/gtest.h"
22 /// Tests an arbitrary set of bytes passed as \p Input.
23 void TestBLAKE3Sum(ArrayRef
<uint8_t> Input
, StringRef Final
) {
26 auto hash
= Hash
.final();
27 auto hashStr
= toHex(hash
);
28 EXPECT_EQ(hashStr
, Final
);
31 using KV
= std::pair
<const char *, const char *>;
33 TEST(BLAKE3Test
, BLAKE3
) {
34 std::array
<KV
, 5> testvectors
{
36 "AF1349B9F5F9A1A6A0404DEA36DCC9499BCB25C9ADC112B7CC9A93CAE41F3262"},
38 "17762FDDD969A453925D65717AC3EEA21320B66B54342FDE15128D6CAF21215F"},
40 "6437B3AC38465133FFB63B75273A8DB548C558465D79DB03FD359C6CD5BD9D85"},
41 KV
{"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
42 "C19012CC2AAF0DC3D8E5C45A1B79114D2DF42ABB2A410BF54BE09E891AF06FF8"},
43 KV
{"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklm"
44 "nopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
45 "553E1AA2A477CB3166E6AB38C12D59F6C5017F0885AAF079F217DA00CFCA363F"}};
47 for (auto input_expected
: testvectors
) {
48 auto str
= std::get
<0>(input_expected
);
49 auto expected
= std::get
<1>(input_expected
);
50 TestBLAKE3Sum({reinterpret_cast<const uint8_t *>(str
), strlen(str
)},
54 std::string
rep(1000, 'a');
56 for (int i
= 0; i
< 1000; ++i
) {
57 Hash
.update({reinterpret_cast<const uint8_t *>(rep
.data()), rep
.size()});
59 auto hash
= Hash
.final();
60 auto hashStr
= toHex(hash
);
62 "616F575A1B58D4C9797D4217B9730AE5E6EB319D76EDEF6549B46F4EFE31FF8B");
64 // Using generic HashBuilder.
65 HashBuilder
<BLAKE3
, support::endianness::native
> HashBuilder
;
66 HashBuilder
.update(std::get
<0>(testvectors
[2]));
67 BLAKE3Result
<> HBHash1
= HashBuilder
.final();
68 BLAKE3Result
<> HBHash2
= HashBuilder
.result();
69 EXPECT_EQ(std::get
<1>(testvectors
[2]), toHex(HBHash1
));
70 EXPECT_EQ(std::get
<1>(testvectors
[2]), toHex(HBHash2
));
73 TEST(BLAKE3Test
, SmallerHashSize
) {
74 const char *InputStr
= "abc";
75 ArrayRef
<uint8_t> Input(reinterpret_cast<const uint8_t *>(InputStr
),
79 auto hash1
= Hash
.final
<16>();
80 auto hash2
= BLAKE3::hash
<16>(Input
);
81 auto hashStr1
= toHex(hash1
);
82 auto hashStr2
= toHex(hash2
);
83 EXPECT_EQ(hashStr1
, hashStr2
);
84 EXPECT_EQ(hashStr1
, "6437B3AC38465133FFB63B75273A8DB5");
86 // Using generic HashBuilder.
87 HashBuilder
<TruncatedBLAKE3
<16>, support::endianness::native
> HashBuilder
;
88 HashBuilder
.update(Input
);
89 BLAKE3Result
<16> hash3
= HashBuilder
.final();
90 BLAKE3Result
<16> hash4
= HashBuilder
.result();
91 EXPECT_EQ(hashStr1
, toHex(hash3
));
92 EXPECT_EQ(hashStr1
, toHex(hash4
));