Bitcoin Core  31.0.0
P2P Digital Currency
siphash.h
Go to the documentation of this file.
1 // Copyright (c) 2016-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_CRYPTO_SIPHASH_H
6 #define BITCOIN_CRYPTO_SIPHASH_H
7 
8 #include <array>
9 #include <cstdint>
10 #include <span>
11 
12 class uint256;
13 
16 {
17  static constexpr uint64_t C0{0x736f6d6570736575ULL}, C1{0x646f72616e646f6dULL}, C2{0x6c7967656e657261ULL}, C3{0x7465646279746573ULL};
18 
19 public:
20  explicit SipHashState(uint64_t k0, uint64_t k1) noexcept : v{C0 ^ k0, C1 ^ k1, C2 ^ k0, C3 ^ k1} {}
21 
22  std::array<uint64_t, 4> v{};
23 };
24 
27 {
29  uint64_t m_tmp{0};
30  uint8_t m_count{0};
31 
32 public:
34  CSipHasher(uint64_t k0, uint64_t k1);
39  CSipHasher& Write(uint64_t data);
41  CSipHasher& Write(std::span<const unsigned char> data);
43  uint64_t Finalize() const;
44 };
45 
56 {
58 
59 public:
60  explicit PresaltedSipHasher(uint64_t k0, uint64_t k1) noexcept : m_state{k0, k1} {}
61 
63  uint64_t operator()(const uint256& val) const noexcept;
64 
69  uint64_t operator()(const uint256& val, uint32_t extra) const noexcept;
70 };
71 
72 #endif // BITCOIN_CRYPTO_SIPHASH_H
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.h:55
static constexpr uint64_t C2
Definition: siphash.h:17
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data.
Definition: siphash.cpp:24
std::array< uint64_t, 4 > v
Definition: siphash.h:22
PresaltedSipHasher(uint64_t k0, uint64_t k1) noexcept
Definition: siphash.h:60
Shared SipHash internal state v[0..3], initialized from (k0, k1).
Definition: siphash.h:15
CSipHasher(uint64_t k0, uint64_t k1)
Construct a SipHash calculator initialized with 128-bit key (k0, k1).
Definition: siphash.cpp:22
static constexpr uint64_t C1
Definition: siphash.h:17
SipHashState m_state
Definition: siphash.h:28
uint64_t m_tmp
Definition: siphash.h:29
SipHashState(uint64_t k0, uint64_t k1) noexcept
Definition: siphash.h:20
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
Definition: siphash.cpp:73
uint64_t operator()(const uint256 &val) const noexcept
Equivalent to CSipHasher(k0, k1).Write(val).Finalize().
Definition: siphash.cpp:91
static constexpr uint64_t C3
Definition: siphash.h:17
256-bit opaque blob.
Definition: uint256.h:195
General SipHash-2-4 implementation.
Definition: siphash.h:26
const SipHashState m_state
Definition: siphash.h:57
uint8_t m_count
Only the low 8 bits of the input size matter.
Definition: siphash.h:30
static constexpr uint64_t C0
Definition: siphash.h:17