UltrafastSecp256k1 3.50.0
Ultra high-performance secp256k1 elliptic curve cryptography library
Loading...
Searching...
No Matches
sha512.hpp
Go to the documentation of this file.
1#ifndef SECP256K1_SHA512_HPP
2#define SECP256K1_SHA512_HPP
3#pragma once
4
5// ============================================================================
6// Minimal SHA-512 implementation for BIP-32 (HMAC-SHA512)
7// ============================================================================
8// Self-contained, no external dependencies. Used only for HD key derivation.
9// ============================================================================
10
11#include <array>
12#include <cstdint>
13#include <cstddef>
14#include <cstring>
15
16namespace secp256k1 {
17
18class SHA512 {
19public:
20 using digest_type = std::array<std::uint8_t, 64>;
21
22 SHA512() noexcept { reset(); }
23
24 void reset() noexcept {
25 state_[0] = 0x6a09e667f3bcc908ULL;
26 state_[1] = 0xbb67ae8584caa73bULL;
27 state_[2] = 0x3c6ef372fe94f82bULL;
28 state_[3] = 0xa54ff53a5f1d36f1ULL;
29 state_[4] = 0x510e527fade682d1ULL;
30 state_[5] = 0x9b05688c2b3e6c1fULL;
31 state_[6] = 0x1f83d9abfb41bd6bULL;
32 state_[7] = 0x5be0cd19137e2179ULL;
33 total_ = 0;
34 buf_len_ = 0;
35 }
36
37 void update(const void* data, std::size_t len) noexcept {
38 auto ptr = static_cast<const std::uint8_t*>(data);
39 total_ += len;
40
41 if (buf_len_ > 0) {
42 std::size_t const fill = 128 - buf_len_;
43 if (len < fill) {
44 std::memcpy(buf_ + buf_len_, ptr, len);
45 buf_len_ += len;
46 return;
47 }
48 std::memcpy(buf_ + buf_len_, ptr, fill);
49 compress(buf_);
50 ptr += fill;
51 len -= fill;
52 buf_len_ = 0;
53 }
54
55 while (len >= 128) {
56 compress(ptr);
57 ptr += 128;
58 len -= 128;
59 }
60
61 if (len > 0) {
62 std::memcpy(buf_, ptr, len);
63 buf_len_ = len;
64 }
65 }
66
67 digest_type finalize() noexcept {
68 std::uint64_t const bit_len = total_ * 8;
69
70 // -- Direct in-place padding (no per-byte update() calls) ---------
71 // buf_len_ is invariantly [0,127] after update() processes full blocks.
72 // Explicit bounds check satisfies static analysis (Sonar cpp:S3519).
73 if (buf_len_ >= 128) buf_len_ = 0;
74 std::size_t const pos = buf_len_;
75 buf_len_ = pos + 1;
76 buf_[pos] = 0x80;
77
78 if (buf_len_ > 112) {
79 // No room for 16-byte length -- pad, compress, start fresh block
80 if (buf_len_ < 128) {
81 std::memset(buf_ + buf_len_, 0, 128 - buf_len_);
82 }
83 compress(buf_);
84 buf_len_ = 0;
85 }
86
87 // Zero-pad to byte 112
88 std::memset(buf_ + buf_len_, 0, 112 - buf_len_);
89
90 // Append 128-bit length big-endian at bytes 112..127
91 // Upper 64 bits are zero (we only track lower 64 bits of length)
92 std::memset(buf_ + 112, 0, 8);
93 for (std::size_t i = 0; i < 8; ++i) {
94 buf_[120 + 7 - i] = static_cast<std::uint8_t>(bit_len >> (i * 8));
95 }
96 compress(buf_);
97
98 digest_type d{};
99 for (std::size_t i = 0; i < 8; ++i) {
100 d[i * 8 + 0] = static_cast<std::uint8_t>(state_[i] >> 56);
101 d[i * 8 + 1] = static_cast<std::uint8_t>(state_[i] >> 48);
102 d[i * 8 + 2] = static_cast<std::uint8_t>(state_[i] >> 40);
103 d[i * 8 + 3] = static_cast<std::uint8_t>(state_[i] >> 32);
104 d[i * 8 + 4] = static_cast<std::uint8_t>(state_[i] >> 24);
105 d[i * 8 + 5] = static_cast<std::uint8_t>(state_[i] >> 16);
106 d[i * 8 + 6] = static_cast<std::uint8_t>(state_[i] >> 8);
107 d[i * 8 + 7] = static_cast<std::uint8_t>(state_[i]);
108 }
109 return d;
110 }
111
112 // One-shot convenience
113 static digest_type hash(const void* data, std::size_t len) noexcept {
114 SHA512 ctx;
115 ctx.update(data, len);
116 return ctx.finalize();
117 }
118
119private:
120 static std::uint64_t rotr64(std::uint64_t x, unsigned n) noexcept {
121 return (x >> n) | (x << (64u - n));
122 }
123 static std::uint64_t ch64(std::uint64_t x, std::uint64_t y, std::uint64_t z) noexcept {
124 return (x & y) ^ (~x & z);
125 }
126 static std::uint64_t maj64(std::uint64_t x, std::uint64_t y, std::uint64_t z) noexcept {
127 return (x & y) ^ (x & z) ^ (y & z);
128 }
129 static std::uint64_t Sigma0(std::uint64_t x) noexcept {
130 return rotr64(x, 28) ^ rotr64(x, 34) ^ rotr64(x, 39);
131 }
132 static std::uint64_t Sigma1(std::uint64_t x) noexcept {
133 return rotr64(x, 14) ^ rotr64(x, 18) ^ rotr64(x, 41);
134 }
135 static std::uint64_t sigma0(std::uint64_t x) noexcept {
136 return rotr64(x, 1) ^ rotr64(x, 8) ^ (x >> 7);
137 }
138 static std::uint64_t sigma1(std::uint64_t x) noexcept {
139 return rotr64(x, 19) ^ rotr64(x, 61) ^ (x >> 6);
140 }
141
142 void compress(const std::uint8_t* block) noexcept {
143 static constexpr std::uint64_t K[80] = {
144 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
145 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
146 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
147 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
148 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
149 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
150 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
151 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
152 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
153 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
154 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
155 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
156 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
157 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
158 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
159 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
160 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
161 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
162 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
163 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
164 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
165 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
166 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
167 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
168 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
169 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
170 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
171 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
172 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
173 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
174 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
175 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
176 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
177 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
178 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
179 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
180 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
181 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
182 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
183 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
184 };
185
186 std::uint64_t W[80];
187 for (int i = 0; i < 16; ++i) {
188 W[i] = (static_cast<std::uint64_t>(block[i * 8 + 0]) << 56) |
189 (static_cast<std::uint64_t>(block[i * 8 + 1]) << 48) |
190 (static_cast<std::uint64_t>(block[i * 8 + 2]) << 40) |
191 (static_cast<std::uint64_t>(block[i * 8 + 3]) << 32) |
192 (static_cast<std::uint64_t>(block[i * 8 + 4]) << 24) |
193 (static_cast<std::uint64_t>(block[i * 8 + 5]) << 16) |
194 (static_cast<std::uint64_t>(block[i * 8 + 6]) << 8) |
195 (static_cast<std::uint64_t>(block[i * 8 + 7]));
196 }
197 for (int i = 16; i < 80; ++i) {
198 W[i] = sigma1(W[i - 2]) + W[i - 7] + sigma0(W[i - 15]) + W[i - 16];
199 }
200
201 std::uint64_t a = state_[0], b = state_[1], c = state_[2], d = state_[3];
202 std::uint64_t e = state_[4], f = state_[5], g = state_[6], h = state_[7];
203
204 for (int i = 0; i < 80; ++i) {
205 std::uint64_t const t1 = h + Sigma1(e) + ch64(e, f, g) + K[i] + W[i];
206 std::uint64_t const t2 = Sigma0(a) + maj64(a, b, c);
207 h = g; g = f; f = e; e = d + t1;
208 d = c; c = b; b = a; a = t1 + t2;
209 }
210
211 state_[0] += a; state_[1] += b; state_[2] += c; state_[3] += d;
212 state_[4] += e; state_[5] += f; state_[6] += g; state_[7] += h;
213 }
214
215 std::uint64_t state_[8]{};
216 std::uint64_t total_{};
217 std::uint8_t buf_[128]{};
218 std::size_t buf_len_{};
219};
220
221} // namespace secp256k1
222
223#endif // SECP256K1_SHA512_HPP
digest_type finalize() noexcept
Definition sha512.hpp:67
SHA512() noexcept
Definition sha512.hpp:22
static digest_type hash(const void *data, std::size_t len) noexcept
Definition sha512.hpp:113
void update(const void *data, std::size_t len) noexcept
Definition sha512.hpp:37
void reset() noexcept
Definition sha512.hpp:24
std::array< std::uint8_t, 64 > digest_type
Definition sha512.hpp:20