/ src / crypto / siphash.h
siphash.h
 1  // Copyright (c) 2016-2020 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 <stdint.h>
 9  
10  #include <span.h>
11  #include <uint256.h>
12  
13  /** SipHash-2-4 */
14  class CSipHasher
15  {
16  private:
17      uint64_t v[4];
18      uint64_t tmp;
19      uint8_t count; // Only the low 8 bits of the input size matter.
20  
21  public:
22      /** Construct a SipHash calculator initialized with 128-bit key (k0, k1) */
23      CSipHasher(uint64_t k0, uint64_t k1);
24      /** Hash a 64-bit integer worth of data
25       *  It is treated as if this was the little-endian interpretation of 8 bytes.
26       *  This function can only be used when a multiple of 8 bytes have been written so far.
27       */
28      CSipHasher& Write(uint64_t data);
29      /** Hash arbitrary bytes. */
30      CSipHasher& Write(Span<const unsigned char> data);
31      /** Compute the 64-bit SipHash-2-4 of the data written so far. The object remains untouched. */
32      uint64_t Finalize() const;
33  };
34  
35  /** Optimized SipHash-2-4 implementation for uint256.
36   *
37   *  It is identical to:
38   *    SipHasher(k0, k1)
39   *      .Write(val.GetUint64(0))
40   *      .Write(val.GetUint64(1))
41   *      .Write(val.GetUint64(2))
42   *      .Write(val.GetUint64(3))
43   *      .Finalize()
44   */
45  uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val);
46  uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint32_t extra);
47  
48  #endif // BITCOIN_CRYPTO_SIPHASH_H