LLVM 19.0.0git
BLAKE3.h
Go to the documentation of this file.
1//==- BLAKE3.h - BLAKE3 C++ wrapper for LLVM ---------------------*- C++ -*-==//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This is a C++ wrapper of the BLAKE3 C interface.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_BLAKE3_H
14#define LLVM_SUPPORT_BLAKE3_H
15
16#include "llvm-c/blake3.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/StringRef.h"
19
20namespace llvm {
21
22/// The constant \p LLVM_BLAKE3_OUT_LEN provides the default output length,
23/// 32 bytes, which is recommended for most callers.
24///
25/// Outputs shorter than the default length of 32 bytes (256 bits) provide
26/// less security. An N-bit BLAKE3 output is intended to provide N bits of
27/// first and second preimage resistance and N/2 bits of collision
28/// resistance, for any N up to 256. Longer outputs don't provide any
29/// additional security.
30///
31/// Shorter BLAKE3 outputs are prefixes of longer ones. Explicitly
32/// requesting a short output is equivalent to truncating the default-length
33/// output.
34template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
35using BLAKE3Result = std::array<uint8_t, NumBytes>;
36
37/// A class that wraps the BLAKE3 algorithm.
38class BLAKE3 {
39public:
40 BLAKE3() { init(); }
41
42 /// Reinitialize the internal state
43 void init() { llvm_blake3_hasher_init(&Hasher); }
44
45 /// Digest more data.
47 llvm_blake3_hasher_update(&Hasher, Data.data(), Data.size());
48 }
49
50 /// Digest more data.
51 void update(StringRef Str) {
52 llvm_blake3_hasher_update(&Hasher, Str.data(), Str.size());
53 }
54
55 /// Finalize the hasher and put the result in \p Result.
56 /// This doesn't modify the hasher itself, and it's possible to finalize again
57 /// after adding more input.
58 template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
59 void final(BLAKE3Result<NumBytes> &Result) {
60 llvm_blake3_hasher_finalize(&Hasher, Result.data(), Result.size());
61 }
62
63 /// Finalize the hasher and return an output of any length, given in bytes.
64 /// This doesn't modify the hasher itself, and it's possible to finalize again
65 /// after adding more input.
66 template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
69 llvm_blake3_hasher_finalize(&Hasher, Result.data(), Result.size());
70 return Result;
71 }
72
73 /// Return the current output for the digested data since the last call to
74 /// init().
75 ///
76 /// Other hash functions distinguish between \p result() and \p final(), with
77 /// \p result() allowing more calls into \p update(), but there's no
78 // difference for the BLAKE3 hash function.
79 template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
81 return final<NumBytes>();
82 }
83
84 /// Returns a BLAKE3 hash for the given data.
85 template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
87 BLAKE3 Hasher;
88 Hasher.update(Data);
89 return Hasher.final<NumBytes>();
90 }
91
92private:
93 llvm_blake3_hasher Hasher;
94};
95
96/// Like \p BLAKE3 but using a class-level template parameter for specifying the
97/// hash size of the \p final() and \p result() functions.
98///
99/// This is useful for using BLAKE3 as the hasher type for \p HashBuilder with
100/// non-default hash sizes.
101template <size_t NumBytes> class TruncatedBLAKE3 : public BLAKE3 {
102public:
103 /// Finalize the hasher and put the result in \p Result.
104 /// This doesn't modify the hasher itself, and it's possible to finalize again
105 /// after adding more input.
106 void final(BLAKE3Result<NumBytes> &Result) { return BLAKE3::final(Result); }
107
108 /// Finalize the hasher and return an output of any length, given in bytes.
109 /// This doesn't modify the hasher itself, and it's possible to finalize again
110 /// after adding more input.
111 BLAKE3Result<NumBytes> final() { return BLAKE3::final<NumBytes>(); }
112
113 /// Return the current output for the digested data since the last call to
114 /// init().
115 ///
116 /// Other hash functions distinguish between \p result() and \p final(), with
117 /// \p result() allowing more calls into \p update(), but there's no
118 // difference for the BLAKE3 hash function.
119 BLAKE3Result<NumBytes> result() { return BLAKE3::result<NumBytes>(); }
120};
121
122} // namespace llvm
123
124#endif
void llvm_blake3_hasher_finalize(const llvm_blake3_hasher *self, uint8_t *out, size_t out_len)
void llvm_blake3_hasher_update(llvm_blake3_hasher *self, const void *input, size_t input_len)
void llvm_blake3_hasher_init(llvm_blake3_hasher *self)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
A class that wraps the BLAKE3 algorithm.
Definition: BLAKE3.h:38
void final(BLAKE3Result< NumBytes > &Result)
Finalize the hasher and put the result in Result.
Definition: BLAKE3.h:59
BLAKE3Result< NumBytes > final()
Finalize the hasher and return an output of any length, given in bytes.
Definition: BLAKE3.h:67
void update(StringRef Str)
Digest more data.
Definition: BLAKE3.h:51
static BLAKE3Result< NumBytes > hash(ArrayRef< uint8_t > Data)
Returns a BLAKE3 hash for the given data.
Definition: BLAKE3.h:86
BLAKE3Result< NumBytes > result()
Return the current output for the digested data since the last call to init().
Definition: BLAKE3.h:80
void init()
Reinitialize the internal state.
Definition: BLAKE3.h:43
void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition: BLAKE3.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Like BLAKE3 but using a class-level template parameter for specifying the hash size of the final() an...
Definition: BLAKE3.h:101
BLAKE3Result< NumBytes > result()
Return the current output for the digested data since the last call to init().
Definition: BLAKE3.h:119
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::array< uint8_t, NumBytes > BLAKE3Result
The constant LLVM_BLAKE3_OUT_LEN provides the default output length, 32 bytes, which is recommended f...
Definition: BLAKE3.h:35