LLVM  16.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 
20 namespace 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.
34 template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
35 using BLAKE3Result = std::array<uint8_t, NumBytes>;
36 
37 /// A class that wraps the BLAKE3 algorithm.
38 class BLAKE3 {
39 public:
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 
92 private:
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.
101 template <size_t NumBytes> class TruncatedBLAKE3 : public BLAKE3 {
102 public:
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
llvm_blake3_hasher
Definition: blake3.h:45
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
StringRef.h
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm_blake3_hasher_finalize
void llvm_blake3_hasher_finalize(const llvm_blake3_hasher *self, uint8_t *out, size_t out_len)
llvm::BLAKE3::result
BLAKE3Result< NumBytes > result()
Return the current output for the digested data since the last call to init().
Definition: BLAKE3.h:80
llvm::BLAKE3::init
void init()
Reinitialize the internal state.
Definition: BLAKE3.h:43
llvm::TruncatedBLAKE3
Like BLAKE3 but using a class-level template parameter for specifying the hash size of the final() an...
Definition: BLAKE3.h:101
llvm::BLAKE3::final
void final(BLAKE3Result< NumBytes > &Result)
Finalize the hasher and put the result in Result.
Definition: BLAKE3.h:59
llvm::BLAKE3::final
BLAKE3Result< NumBytes > final()
Finalize the hasher and return an output of any length, given in bytes.
Definition: BLAKE3.h:67
llvm::BLAKE3::BLAKE3
BLAKE3()
Definition: BLAKE3.h:40
llvm::BLAKE3
A class that wraps the BLAKE3 algorithm.
Definition: BLAKE3.h:38
ArrayRef.h
llvm::TruncatedBLAKE3::result
BLAKE3Result< NumBytes > result()
Return the current output for the digested data since the last call to init().
Definition: BLAKE3.h:119
llvm::ArrayRef< uint8_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::BLAKE3::update
void update(StringRef Str)
Digest more data.
Definition: BLAKE3.h:51
llvm_blake3_hasher_update
void llvm_blake3_hasher_update(llvm_blake3_hasher *self, const void *input, size_t input_len)
llvm::BLAKE3Result
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
blake3.h
llvm::BLAKE3::update
void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition: BLAKE3.h:46
llvm::BLAKE3::hash
static BLAKE3Result< NumBytes > hash(ArrayRef< uint8_t > Data)
Returns a BLAKE3 hash for the given data.
Definition: BLAKE3.h:86
llvm_blake3_hasher_init
void llvm_blake3_hasher_init(llvm_blake3_hasher *self)