BLAKE3.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- BLAKE3.h - BLAKE3 C++ wrapper for LLVM ---------------------*- C++ -*-==//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This is a C++ wrapper of the BLAKE3 C interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_SUPPORT_BLAKE3_H
  18. #define LLVM_SUPPORT_BLAKE3_H
  19. #include "llvm-c/blake3.h"
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/StringRef.h"
  22. namespace llvm {
  23. /// The constant \p LLVM_BLAKE3_OUT_LEN provides the default output length,
  24. /// 32 bytes, which is recommended for most callers.
  25. ///
  26. /// Outputs shorter than the default length of 32 bytes (256 bits) provide
  27. /// less security. An N-bit BLAKE3 output is intended to provide N bits of
  28. /// first and second preimage resistance and N/2 bits of collision
  29. /// resistance, for any N up to 256. Longer outputs don't provide any
  30. /// additional security.
  31. ///
  32. /// Shorter BLAKE3 outputs are prefixes of longer ones. Explicitly
  33. /// requesting a short output is equivalent to truncating the default-length
  34. /// output.
  35. template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
  36. using BLAKE3Result = std::array<uint8_t, NumBytes>;
  37. /// A class that wraps the BLAKE3 algorithm.
  38. class BLAKE3 {
  39. public:
  40. BLAKE3() { init(); }
  41. /// Reinitialize the internal state
  42. void init() { llvm_blake3_hasher_init(&Hasher); }
  43. /// Digest more data.
  44. void update(ArrayRef<uint8_t> Data) {
  45. llvm_blake3_hasher_update(&Hasher, Data.data(), Data.size());
  46. }
  47. /// Digest more data.
  48. void update(StringRef Str) {
  49. llvm_blake3_hasher_update(&Hasher, Str.data(), Str.size());
  50. }
  51. /// Finalize the hasher and put the result in \p Result.
  52. /// This doesn't modify the hasher itself, and it's possible to finalize again
  53. /// after adding more input.
  54. template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
  55. void final(BLAKE3Result<NumBytes> &Result) {
  56. llvm_blake3_hasher_finalize(&Hasher, Result.data(), Result.size());
  57. }
  58. /// Finalize the hasher and return an output of any length, given in bytes.
  59. /// This doesn't modify the hasher itself, and it's possible to finalize again
  60. /// after adding more input.
  61. template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
  62. BLAKE3Result<NumBytes> final() {
  63. BLAKE3Result<NumBytes> Result;
  64. llvm_blake3_hasher_finalize(&Hasher, Result.data(), Result.size());
  65. return Result;
  66. }
  67. /// Return the current output for the digested data since the last call to
  68. /// init().
  69. ///
  70. /// Other hash functions distinguish between \p result() and \p final(), with
  71. /// \p result() allowing more calls into \p update(), but there's no
  72. // difference for the BLAKE3 hash function.
  73. template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
  74. BLAKE3Result<NumBytes> result() {
  75. return final<NumBytes>();
  76. }
  77. /// Returns a BLAKE3 hash for the given data.
  78. template <size_t NumBytes = LLVM_BLAKE3_OUT_LEN>
  79. static BLAKE3Result<NumBytes> hash(ArrayRef<uint8_t> Data) {
  80. BLAKE3 Hasher;
  81. Hasher.update(Data);
  82. return Hasher.final<NumBytes>();
  83. }
  84. private:
  85. llvm_blake3_hasher Hasher;
  86. };
  87. /// Like \p BLAKE3 but using a class-level template parameter for specifying the
  88. /// hash size of the \p final() and \p result() functions.
  89. ///
  90. /// This is useful for using BLAKE3 as the hasher type for \p HashBuilder with
  91. /// non-default hash sizes.
  92. template <size_t NumBytes> class TruncatedBLAKE3 : public BLAKE3 {
  93. public:
  94. /// Finalize the hasher and put the result in \p Result.
  95. /// This doesn't modify the hasher itself, and it's possible to finalize again
  96. /// after adding more input.
  97. void final(BLAKE3Result<NumBytes> &Result) { return BLAKE3::final(Result); }
  98. /// Finalize the hasher and return an output of any length, given in bytes.
  99. /// This doesn't modify the hasher itself, and it's possible to finalize again
  100. /// after adding more input.
  101. BLAKE3Result<NumBytes> final() { return BLAKE3::final<NumBytes>(); }
  102. /// Return the current output for the digested data since the last call to
  103. /// init().
  104. ///
  105. /// Other hash functions distinguish between \p result() and \p final(), with
  106. /// \p result() allowing more calls into \p update(), but there's no
  107. // difference for the BLAKE3 hash function.
  108. BLAKE3Result<NumBytes> result() { return BLAKE3::result<NumBytes>(); }
  109. };
  110. } // namespace llvm
  111. #endif
  112. #ifdef __GNUC__
  113. #pragma GCC diagnostic pop
  114. #endif