Bitcoin ABC  0.24.7
P2P Digital Currency
sha512.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014 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/sha512.h>
6 
7 #include <crypto/common.h>
8 
9 #include <cstring>
10 
11 // Internal implementation code.
12 namespace {
14 namespace sha512 {
15  inline uint64_t Ch(uint64_t x, uint64_t y, uint64_t z) {
16  return z ^ (x & (y ^ z));
17  }
18  inline uint64_t Maj(uint64_t x, uint64_t y, uint64_t z) {
19  return (x & y) | (z & (x | y));
20  }
21  inline uint64_t Sigma0(uint64_t x) {
22  return (x >> 28 | x << 36) ^ (x >> 34 | x << 30) ^ (x >> 39 | x << 25);
23  }
24  inline uint64_t Sigma1(uint64_t x) {
25  return (x >> 14 | x << 50) ^ (x >> 18 | x << 46) ^ (x >> 41 | x << 23);
26  }
27  inline uint64_t sigma0(uint64_t x) {
28  return (x >> 1 | x << 63) ^ (x >> 8 | x << 56) ^ (x >> 7);
29  }
30  inline uint64_t sigma1(uint64_t x) {
31  return (x >> 19 | x << 45) ^ (x >> 61 | x << 3) ^ (x >> 6);
32  }
33 
35  inline void Round(uint64_t a, uint64_t b, uint64_t c, uint64_t &d,
36  uint64_t e, uint64_t f, uint64_t g, uint64_t &h,
37  uint64_t k, uint64_t w) {
38  uint64_t t1 = h + Sigma1(e) + Ch(e, f, g) + k + w;
39  uint64_t t2 = Sigma0(a) + Maj(a, b, c);
40  d += t1;
41  h = t1 + t2;
42  }
43 
45  inline void Initialize(uint64_t *s) {
46  s[0] = 0x6a09e667f3bcc908ull;
47  s[1] = 0xbb67ae8584caa73bull;
48  s[2] = 0x3c6ef372fe94f82bull;
49  s[3] = 0xa54ff53a5f1d36f1ull;
50  s[4] = 0x510e527fade682d1ull;
51  s[5] = 0x9b05688c2b3e6c1full;
52  s[6] = 0x1f83d9abfb41bd6bull;
53  s[7] = 0x5be0cd19137e2179ull;
54  }
55 
57  void Transform(uint64_t *s, const uint8_t *chunk) {
58  uint64_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5],
59  g = s[6], h = s[7];
60  uint64_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13,
61  w14, w15;
62 
63  Round(a, b, c, d, e, f, g, h, 0x428a2f98d728ae22ull,
64  w0 = ReadBE64(chunk + 0));
65  Round(h, a, b, c, d, e, f, g, 0x7137449123ef65cdull,
66  w1 = ReadBE64(chunk + 8));
67  Round(g, h, a, b, c, d, e, f, 0xb5c0fbcfec4d3b2full,
68  w2 = ReadBE64(chunk + 16));
69  Round(f, g, h, a, b, c, d, e, 0xe9b5dba58189dbbcull,
70  w3 = ReadBE64(chunk + 24));
71  Round(e, f, g, h, a, b, c, d, 0x3956c25bf348b538ull,
72  w4 = ReadBE64(chunk + 32));
73  Round(d, e, f, g, h, a, b, c, 0x59f111f1b605d019ull,
74  w5 = ReadBE64(chunk + 40));
75  Round(c, d, e, f, g, h, a, b, 0x923f82a4af194f9bull,
76  w6 = ReadBE64(chunk + 48));
77  Round(b, c, d, e, f, g, h, a, 0xab1c5ed5da6d8118ull,
78  w7 = ReadBE64(chunk + 56));
79  Round(a, b, c, d, e, f, g, h, 0xd807aa98a3030242ull,
80  w8 = ReadBE64(chunk + 64));
81  Round(h, a, b, c, d, e, f, g, 0x12835b0145706fbeull,
82  w9 = ReadBE64(chunk + 72));
83  Round(g, h, a, b, c, d, e, f, 0x243185be4ee4b28cull,
84  w10 = ReadBE64(chunk + 80));
85  Round(f, g, h, a, b, c, d, e, 0x550c7dc3d5ffb4e2ull,
86  w11 = ReadBE64(chunk + 88));
87  Round(e, f, g, h, a, b, c, d, 0x72be5d74f27b896full,
88  w12 = ReadBE64(chunk + 96));
89  Round(d, e, f, g, h, a, b, c, 0x80deb1fe3b1696b1ull,
90  w13 = ReadBE64(chunk + 104));
91  Round(c, d, e, f, g, h, a, b, 0x9bdc06a725c71235ull,
92  w14 = ReadBE64(chunk + 112));
93  Round(b, c, d, e, f, g, h, a, 0xc19bf174cf692694ull,
94  w15 = ReadBE64(chunk + 120));
95 
96  Round(a, b, c, d, e, f, g, h, 0xe49b69c19ef14ad2ull,
97  w0 += sigma1(w14) + w9 + sigma0(w1));
98  Round(h, a, b, c, d, e, f, g, 0xefbe4786384f25e3ull,
99  w1 += sigma1(w15) + w10 + sigma0(w2));
100  Round(g, h, a, b, c, d, e, f, 0x0fc19dc68b8cd5b5ull,
101  w2 += sigma1(w0) + w11 + sigma0(w3));
102  Round(f, g, h, a, b, c, d, e, 0x240ca1cc77ac9c65ull,
103  w3 += sigma1(w1) + w12 + sigma0(w4));
104  Round(e, f, g, h, a, b, c, d, 0x2de92c6f592b0275ull,
105  w4 += sigma1(w2) + w13 + sigma0(w5));
106  Round(d, e, f, g, h, a, b, c, 0x4a7484aa6ea6e483ull,
107  w5 += sigma1(w3) + w14 + sigma0(w6));
108  Round(c, d, e, f, g, h, a, b, 0x5cb0a9dcbd41fbd4ull,
109  w6 += sigma1(w4) + w15 + sigma0(w7));
110  Round(b, c, d, e, f, g, h, a, 0x76f988da831153b5ull,
111  w7 += sigma1(w5) + w0 + sigma0(w8));
112  Round(a, b, c, d, e, f, g, h, 0x983e5152ee66dfabull,
113  w8 += sigma1(w6) + w1 + sigma0(w9));
114  Round(h, a, b, c, d, e, f, g, 0xa831c66d2db43210ull,
115  w9 += sigma1(w7) + w2 + sigma0(w10));
116  Round(g, h, a, b, c, d, e, f, 0xb00327c898fb213full,
117  w10 += sigma1(w8) + w3 + sigma0(w11));
118  Round(f, g, h, a, b, c, d, e, 0xbf597fc7beef0ee4ull,
119  w11 += sigma1(w9) + w4 + sigma0(w12));
120  Round(e, f, g, h, a, b, c, d, 0xc6e00bf33da88fc2ull,
121  w12 += sigma1(w10) + w5 + sigma0(w13));
122  Round(d, e, f, g, h, a, b, c, 0xd5a79147930aa725ull,
123  w13 += sigma1(w11) + w6 + sigma0(w14));
124  Round(c, d, e, f, g, h, a, b, 0x06ca6351e003826full,
125  w14 += sigma1(w12) + w7 + sigma0(w15));
126  Round(b, c, d, e, f, g, h, a, 0x142929670a0e6e70ull,
127  w15 += sigma1(w13) + w8 + sigma0(w0));
128 
129  Round(a, b, c, d, e, f, g, h, 0x27b70a8546d22ffcull,
130  w0 += sigma1(w14) + w9 + sigma0(w1));
131  Round(h, a, b, c, d, e, f, g, 0x2e1b21385c26c926ull,
132  w1 += sigma1(w15) + w10 + sigma0(w2));
133  Round(g, h, a, b, c, d, e, f, 0x4d2c6dfc5ac42aedull,
134  w2 += sigma1(w0) + w11 + sigma0(w3));
135  Round(f, g, h, a, b, c, d, e, 0x53380d139d95b3dfull,
136  w3 += sigma1(w1) + w12 + sigma0(w4));
137  Round(e, f, g, h, a, b, c, d, 0x650a73548baf63deull,
138  w4 += sigma1(w2) + w13 + sigma0(w5));
139  Round(d, e, f, g, h, a, b, c, 0x766a0abb3c77b2a8ull,
140  w5 += sigma1(w3) + w14 + sigma0(w6));
141  Round(c, d, e, f, g, h, a, b, 0x81c2c92e47edaee6ull,
142  w6 += sigma1(w4) + w15 + sigma0(w7));
143  Round(b, c, d, e, f, g, h, a, 0x92722c851482353bull,
144  w7 += sigma1(w5) + w0 + sigma0(w8));
145  Round(a, b, c, d, e, f, g, h, 0xa2bfe8a14cf10364ull,
146  w8 += sigma1(w6) + w1 + sigma0(w9));
147  Round(h, a, b, c, d, e, f, g, 0xa81a664bbc423001ull,
148  w9 += sigma1(w7) + w2 + sigma0(w10));
149  Round(g, h, a, b, c, d, e, f, 0xc24b8b70d0f89791ull,
150  w10 += sigma1(w8) + w3 + sigma0(w11));
151  Round(f, g, h, a, b, c, d, e, 0xc76c51a30654be30ull,
152  w11 += sigma1(w9) + w4 + sigma0(w12));
153  Round(e, f, g, h, a, b, c, d, 0xd192e819d6ef5218ull,
154  w12 += sigma1(w10) + w5 + sigma0(w13));
155  Round(d, e, f, g, h, a, b, c, 0xd69906245565a910ull,
156  w13 += sigma1(w11) + w6 + sigma0(w14));
157  Round(c, d, e, f, g, h, a, b, 0xf40e35855771202aull,
158  w14 += sigma1(w12) + w7 + sigma0(w15));
159  Round(b, c, d, e, f, g, h, a, 0x106aa07032bbd1b8ull,
160  w15 += sigma1(w13) + w8 + sigma0(w0));
161 
162  Round(a, b, c, d, e, f, g, h, 0x19a4c116b8d2d0c8ull,
163  w0 += sigma1(w14) + w9 + sigma0(w1));
164  Round(h, a, b, c, d, e, f, g, 0x1e376c085141ab53ull,
165  w1 += sigma1(w15) + w10 + sigma0(w2));
166  Round(g, h, a, b, c, d, e, f, 0x2748774cdf8eeb99ull,
167  w2 += sigma1(w0) + w11 + sigma0(w3));
168  Round(f, g, h, a, b, c, d, e, 0x34b0bcb5e19b48a8ull,
169  w3 += sigma1(w1) + w12 + sigma0(w4));
170  Round(e, f, g, h, a, b, c, d, 0x391c0cb3c5c95a63ull,
171  w4 += sigma1(w2) + w13 + sigma0(w5));
172  Round(d, e, f, g, h, a, b, c, 0x4ed8aa4ae3418acbull,
173  w5 += sigma1(w3) + w14 + sigma0(w6));
174  Round(c, d, e, f, g, h, a, b, 0x5b9cca4f7763e373ull,
175  w6 += sigma1(w4) + w15 + sigma0(w7));
176  Round(b, c, d, e, f, g, h, a, 0x682e6ff3d6b2b8a3ull,
177  w7 += sigma1(w5) + w0 + sigma0(w8));
178  Round(a, b, c, d, e, f, g, h, 0x748f82ee5defb2fcull,
179  w8 += sigma1(w6) + w1 + sigma0(w9));
180  Round(h, a, b, c, d, e, f, g, 0x78a5636f43172f60ull,
181  w9 += sigma1(w7) + w2 + sigma0(w10));
182  Round(g, h, a, b, c, d, e, f, 0x84c87814a1f0ab72ull,
183  w10 += sigma1(w8) + w3 + sigma0(w11));
184  Round(f, g, h, a, b, c, d, e, 0x8cc702081a6439ecull,
185  w11 += sigma1(w9) + w4 + sigma0(w12));
186  Round(e, f, g, h, a, b, c, d, 0x90befffa23631e28ull,
187  w12 += sigma1(w10) + w5 + sigma0(w13));
188  Round(d, e, f, g, h, a, b, c, 0xa4506cebde82bde9ull,
189  w13 += sigma1(w11) + w6 + sigma0(w14));
190  Round(c, d, e, f, g, h, a, b, 0xbef9a3f7b2c67915ull,
191  w14 += sigma1(w12) + w7 + sigma0(w15));
192  Round(b, c, d, e, f, g, h, a, 0xc67178f2e372532bull,
193  w15 += sigma1(w13) + w8 + sigma0(w0));
194 
195  Round(a, b, c, d, e, f, g, h, 0xca273eceea26619cull,
196  w0 += sigma1(w14) + w9 + sigma0(w1));
197  Round(h, a, b, c, d, e, f, g, 0xd186b8c721c0c207ull,
198  w1 += sigma1(w15) + w10 + sigma0(w2));
199  Round(g, h, a, b, c, d, e, f, 0xeada7dd6cde0eb1eull,
200  w2 += sigma1(w0) + w11 + sigma0(w3));
201  Round(f, g, h, a, b, c, d, e, 0xf57d4f7fee6ed178ull,
202  w3 += sigma1(w1) + w12 + sigma0(w4));
203  Round(e, f, g, h, a, b, c, d, 0x06f067aa72176fbaull,
204  w4 += sigma1(w2) + w13 + sigma0(w5));
205  Round(d, e, f, g, h, a, b, c, 0x0a637dc5a2c898a6ull,
206  w5 += sigma1(w3) + w14 + sigma0(w6));
207  Round(c, d, e, f, g, h, a, b, 0x113f9804bef90daeull,
208  w6 += sigma1(w4) + w15 + sigma0(w7));
209  Round(b, c, d, e, f, g, h, a, 0x1b710b35131c471bull,
210  w7 += sigma1(w5) + w0 + sigma0(w8));
211  Round(a, b, c, d, e, f, g, h, 0x28db77f523047d84ull,
212  w8 += sigma1(w6) + w1 + sigma0(w9));
213  Round(h, a, b, c, d, e, f, g, 0x32caab7b40c72493ull,
214  w9 += sigma1(w7) + w2 + sigma0(w10));
215  Round(g, h, a, b, c, d, e, f, 0x3c9ebe0a15c9bebcull,
216  w10 += sigma1(w8) + w3 + sigma0(w11));
217  Round(f, g, h, a, b, c, d, e, 0x431d67c49c100d4cull,
218  w11 += sigma1(w9) + w4 + sigma0(w12));
219  Round(e, f, g, h, a, b, c, d, 0x4cc5d4becb3e42b6ull,
220  w12 += sigma1(w10) + w5 + sigma0(w13));
221  Round(d, e, f, g, h, a, b, c, 0x597f299cfc657e2aull,
222  w13 += sigma1(w11) + w6 + sigma0(w14));
223  Round(c, d, e, f, g, h, a, b, 0x5fcb6fab3ad6faecull,
224  w14 + sigma1(w12) + w7 + sigma0(w15));
225  Round(b, c, d, e, f, g, h, a, 0x6c44198c4a475817ull,
226  w15 + sigma1(w13) + w8 + sigma0(w0));
227 
228  s[0] += a;
229  s[1] += b;
230  s[2] += c;
231  s[3] += d;
232  s[4] += e;
233  s[5] += f;
234  s[6] += g;
235  s[7] += h;
236  }
237 
238 } // namespace sha512
239 
240 } // namespace
241 
243 
244 CSHA512::CSHA512() : bytes(0) {
245  sha512::Initialize(s);
246 }
247 
248 CSHA512 &CSHA512::Write(const uint8_t *data, size_t len) {
249  const uint8_t *end = data + len;
250  size_t bufsize = bytes % 128;
251  if (bufsize && bufsize + len >= 128) {
252  // Fill the buffer, and process it.
253  memcpy(buf + bufsize, data, 128 - bufsize);
254  bytes += 128 - bufsize;
255  data += 128 - bufsize;
257  bufsize = 0;
258  }
259  while (end - data >= 128) {
260  // Process full chunks directly from the source.
261  sha512::Transform(s, data);
262  data += 128;
263  bytes += 128;
264  }
265  if (end > data) {
266  // Fill the buffer with what remains.
267  memcpy(buf + bufsize, data, end - data);
268  bytes += end - data;
269  }
270  return *this;
271 }
272 
273 void CSHA512::Finalize(uint8_t hash[OUTPUT_SIZE]) {
274  static const uint8_t pad[128] = {0x80};
275  uint8_t sizedesc[16] = {0x00};
276  WriteBE64(sizedesc + 8, bytes << 3);
277  Write(pad, 1 + ((239 - (bytes % 128)) % 128));
278  Write(sizedesc, 16);
279  WriteBE64(hash, s[0]);
280  WriteBE64(hash + 8, s[1]);
281  WriteBE64(hash + 16, s[2]);
282  WriteBE64(hash + 24, s[3]);
283  WriteBE64(hash + 32, s[4]);
284  WriteBE64(hash + 40, s[5]);
285  WriteBE64(hash + 48, s[6]);
286  WriteBE64(hash + 56, s[7]);
287 }
288 
290  bytes = 0;
291  sha512::Initialize(s);
292  return *this;
293 }
Maj
#define Maj(x, y, z)
Definition: hash_impl.h:18
CSHA512::s
uint64_t s[8]
Definition: sha512.h:14
sha512
Internal SHA-512 implementation.
Definition: sha512.cpp:14
Sigma0
#define Sigma0(x)
Definition: hash_impl.h:19
sha512.h
Round
#define Round(a, b, c, d, e, f, g, h, k, w)
Definition: hash_impl.h:24
ReadBE64
static uint64_t ReadBE64(const uint8_t *ptr)
Definition: common.h:62
CSHA512::Reset
CSHA512 & Reset()
Definition: sha512.cpp:289
sigma1
#define sigma1(x)
Definition: hash_impl.h:22
CSHA512::Finalize
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha512.cpp:273
WriteBE64
static void WriteBE64(uint8_t *ptr, uint64_t x)
Definition: common.h:73
CSHA512::bytes
uint64_t bytes
Definition: sha512.h:16
common.h
sigma0
#define sigma0(x)
Definition: hash_impl.h:21
CSHA512::CSHA512
CSHA512()
Definition: sha512.cpp:244
CSHA512
A hasher class for SHA-512.
Definition: sha512.h:12
Ch
#define Ch(x, y, z)
Definition: hash_impl.h:17
sha256_shani::Transform
void Transform(uint32_t *s, const uint8_t *chunk, size_t blocks)
CSHA512::buf
uint8_t buf[128]
Definition: sha512.h:15
Sigma1
#define Sigma1(x)
Definition: hash_impl.h:20
CSHA512::Write
CSHA512 & Write(const uint8_t *data, size_t len)
Definition: sha512.cpp:248