Bitcoin Core  0.18.99
P2P Digital Currency
siphash.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2018 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 #include <crypto/siphash.h>
6 
7 #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
8 
9 #define SIPROUND do { \
10  v0 += v1; v1 = ROTL(v1, 13); v1 ^= v0; \
11  v0 = ROTL(v0, 32); \
12  v2 += v3; v3 = ROTL(v3, 16); v3 ^= v2; \
13  v0 += v3; v3 = ROTL(v3, 21); v3 ^= v0; \
14  v2 += v1; v1 = ROTL(v1, 17); v1 ^= v2; \
15  v2 = ROTL(v2, 32); \
16 } while (0)
17 
18 CSipHasher::CSipHasher(uint64_t k0, uint64_t k1)
19 {
20  v[0] = 0x736f6d6570736575ULL ^ k0;
21  v[1] = 0x646f72616e646f6dULL ^ k1;
22  v[2] = 0x6c7967656e657261ULL ^ k0;
23  v[3] = 0x7465646279746573ULL ^ k1;
24  count = 0;
25  tmp = 0;
26 }
27 
29 {
30  uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
31 
32  assert(count % 8 == 0);
33 
34  v3 ^= data;
35  SIPROUND;
36  SIPROUND;
37  v0 ^= data;
38 
39  v[0] = v0;
40  v[1] = v1;
41  v[2] = v2;
42  v[3] = v3;
43 
44  count += 8;
45  return *this;
46 }
47 
48 CSipHasher& CSipHasher::Write(const unsigned char* data, size_t size)
49 {
50  uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
51  uint64_t t = tmp;
52  int c = count;
53 
54  while (size--) {
55  t |= ((uint64_t)(*(data++))) << (8 * (c % 8));
56  c++;
57  if ((c & 7) == 0) {
58  v3 ^= t;
59  SIPROUND;
60  SIPROUND;
61  v0 ^= t;
62  t = 0;
63  }
64  }
65 
66  v[0] = v0;
67  v[1] = v1;
68  v[2] = v2;
69  v[3] = v3;
70  count = c;
71  tmp = t;
72 
73  return *this;
74 }
75 
76 uint64_t CSipHasher::Finalize() const
77 {
78  uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
79 
80  uint64_t t = tmp | (((uint64_t)count) << 56);
81 
82  v3 ^= t;
83  SIPROUND;
84  SIPROUND;
85  v0 ^= t;
86  v2 ^= 0xFF;
87  SIPROUND;
88  SIPROUND;
89  SIPROUND;
90  SIPROUND;
91  return v0 ^ v1 ^ v2 ^ v3;
92 }
93 
94 uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val)
95 {
96  /* Specialized implementation for efficiency */
97  uint64_t d = val.GetUint64(0);
98 
99  uint64_t v0 = 0x736f6d6570736575ULL ^ k0;
100  uint64_t v1 = 0x646f72616e646f6dULL ^ k1;
101  uint64_t v2 = 0x6c7967656e657261ULL ^ k0;
102  uint64_t v3 = 0x7465646279746573ULL ^ k1 ^ d;
103 
104  SIPROUND;
105  SIPROUND;
106  v0 ^= d;
107  d = val.GetUint64(1);
108  v3 ^= d;
109  SIPROUND;
110  SIPROUND;
111  v0 ^= d;
112  d = val.GetUint64(2);
113  v3 ^= d;
114  SIPROUND;
115  SIPROUND;
116  v0 ^= d;
117  d = val.GetUint64(3);
118  v3 ^= d;
119  SIPROUND;
120  SIPROUND;
121  v0 ^= d;
122  v3 ^= ((uint64_t)4) << 59;
123  SIPROUND;
124  SIPROUND;
125  v0 ^= ((uint64_t)4) << 59;
126  v2 ^= 0xFF;
127  SIPROUND;
128  SIPROUND;
129  SIPROUND;
130  SIPROUND;
131  return v0 ^ v1 ^ v2 ^ v3;
132 }
133 
134 uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint32_t extra)
135 {
136  /* Specialized implementation for efficiency */
137  uint64_t d = val.GetUint64(0);
138 
139  uint64_t v0 = 0x736f6d6570736575ULL ^ k0;
140  uint64_t v1 = 0x646f72616e646f6dULL ^ k1;
141  uint64_t v2 = 0x6c7967656e657261ULL ^ k0;
142  uint64_t v3 = 0x7465646279746573ULL ^ k1 ^ d;
143 
144  SIPROUND;
145  SIPROUND;
146  v0 ^= d;
147  d = val.GetUint64(1);
148  v3 ^= d;
149  SIPROUND;
150  SIPROUND;
151  v0 ^= d;
152  d = val.GetUint64(2);
153  v3 ^= d;
154  SIPROUND;
155  SIPROUND;
156  v0 ^= d;
157  d = val.GetUint64(3);
158  v3 ^= d;
159  SIPROUND;
160  SIPROUND;
161  v0 ^= d;
162  d = (((uint64_t)36) << 56) | extra;
163  v3 ^= d;
164  SIPROUND;
165  SIPROUND;
166  v0 ^= d;
167  v2 ^= 0xFF;
168  SIPROUND;
169  SIPROUND;
170  SIPROUND;
171  SIPROUND;
172  return v0 ^ v1 ^ v2 ^ v3;
173 }
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.cpp:94
uint64_t v[4]
Definition: siphash.h:16
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data It is treated as if this was the little-endian interpretation of ...
Definition: siphash.cpp:28
CSipHasher(uint64_t k0, uint64_t k1)
Construct a SipHash calculator initialized with 128-bit key (k0, k1)
Definition: siphash.cpp:18
#define SIPROUND
Definition: siphash.cpp:9
int count
Definition: siphash.h:18
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
Definition: siphash.cpp:76
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: siphash.cpp:134
uint64_t tmp
Definition: siphash.h:17
256-bit opaque blob.
Definition: uint256.h:121
SipHash-2-4.
Definition: siphash.h:13
uint64_t GetUint64(int pos) const
Definition: uint256.h:80