LLVM 23.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 /// Reinitialize the internal state with the given key.
47 // TODO: maybe assert on the size of the key?
48 llvm_blake3_hasher_init_keyed(&Hasher, Key.data());
49 }
50
51 /// Digest more data.
53 llvm_blake3_hasher_update(&Hasher, Data.data(), Data.size());
54 }
55
56 /// Digest more data.
57 void update(StringRef Str) {
58 llvm_blake3_hasher_update(&Hasher, Str.data(), Str.size());
59 }
60
61 /// Finalize the hasher and put the result in \p Result.
62 /// This doesn't modify the hasher itself, and it's possible to finalize again
63 /// after adding more input.
64 template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
65 void final(BLAKE3Result<NumBytes> &Result) {
66 llvm_blake3_hasher_finalize(&Hasher, Result.data(), Result.size());
67 }
68
69 /// Finalize the hasher and return an output of any length, given in bytes.
70 /// This doesn't modify the hasher itself, and it's possible to finalize again
71 /// after adding more input.
72 template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
75 llvm_blake3_hasher_finalize(&Hasher, Result.data(), Result.size());
76 return Result;
77 }
78
79 /// Return the current output for the digested data since the last call to
80 /// init().
81 ///
82 /// Other hash functions distinguish between \p result() and \p final(), with
83 /// \p result() allowing more calls into \p update(), but there's no
84 // difference for the BLAKE3 hash function.
85 template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
89
90 /// Returns a BLAKE3 hash for the given data.
91 template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
93 BLAKE3 Hasher;
94 Hasher.update(Data);
95 return Hasher.final<NumBytes>();
96 }
97
98private:
99 llvm_blake3_hasher Hasher;
100};
101
102/// Like \p BLAKE3 but using a class-level template parameter for specifying the
103/// hash size of the \p final() and \p result() functions.
104///
105/// This is useful for using BLAKE3 as the hasher type for \p HashBuilder with
106/// non-default hash sizes.
107template <size_t NumBytes> class TruncatedBLAKE3 : public BLAKE3 {
108public:
109 /// Finalize the hasher and put the result in \p Result.
110 /// This doesn't modify the hasher itself, and it's possible to finalize again
111 /// after adding more input.
112 void final(BLAKE3Result<NumBytes> &Result) { return BLAKE3::final(Result); }
113
114 /// Finalize the hasher and return an output of any length, given in bytes.
115 /// This doesn't modify the hasher itself, and it's possible to finalize again
116 /// after adding more input.
118
119 /// Return the current output for the digested data since the last call to
120 /// init().
121 ///
122 /// Other hash functions distinguish between \p result() and \p final(), with
123 /// \p result() allowing more calls into \p update(), but there's no
124 // difference for the BLAKE3 hash function.
126};
127
128} // namespace llvm
129
130#endif
LLVM_C_ABI void llvm_blake3_hasher_finalize(const llvm_blake3_hasher *self, uint8_t *out, size_t out_len)
LLVM_C_ABI void llvm_blake3_hasher_init(llvm_blake3_hasher *self)
LLVM_C_ABI void llvm_blake3_hasher_init_keyed(llvm_blake3_hasher *self, const uint8_t key[LLVM_BLAKE3_KEY_LEN])
LLVM_C_ABI void llvm_blake3_hasher_update(llvm_blake3_hasher *self, const void *input, size_t input_len)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
BLAKE3Result< NumBytes > final()
Finalize the hasher and return an output of any length, given in bytes.
Definition BLAKE3.h:73
void update(StringRef Str)
Digest more data.
Definition BLAKE3.h:57
void init_keyed(ArrayRef< uint8_t > Key)
Reinitialize the internal state with the given key.
Definition BLAKE3.h:46
static BLAKE3Result< NumBytes > hash(ArrayRef< uint8_t > Data)
Returns a BLAKE3 hash for the given data.
Definition BLAKE3.h:92
BLAKE3Result< NumBytes > result()
Return the current output for the digested data since the last call to init().
Definition BLAKE3.h:86
void init()
Reinitialize the internal state.
Definition BLAKE3.h:43
void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition BLAKE3.h:52
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Like BLAKE3 but using a class-level template parameter for specifying the hash size of the final() an...
Definition BLAKE3.h:107
BLAKE3Result< NumBytes > result()
Return the current output for the digested data since the last call to init().
Definition BLAKE3.h:125
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
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
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189