Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
secp256k1
src
hash_impl.h
Go to the documentation of this file.
1
/***********************************************************************
2
* Copyright (c) 2014 Pieter Wuille *
3
* Distributed under the MIT software license, see the accompanying *
4
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5
***********************************************************************/
6
7
#ifndef SECP256K1_HASH_IMPL_H
8
#define SECP256K1_HASH_IMPL_H
9
10
#include "
hash.h
"
11
#include "
util.h
"
12
13
#include <stdlib.h>
14
#include <stdint.h>
15
#include <
string.h
>
16
17
#define Ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
18
#define Maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
19
#define Sigma0(x) (((x) >> 2 | (x) << 30) ^ ((x) >> 13 | (x) << 19) ^ ((x) >> 22 | (x) << 10))
20
#define Sigma1(x) (((x) >> 6 | (x) << 26) ^ ((x) >> 11 | (x) << 21) ^ ((x) >> 25 | (x) << 7))
21
#define sigma0(x) (((x) >> 7 | (x) << 25) ^ ((x) >> 18 | (x) << 14) ^ ((x) >> 3))
22
#define sigma1(x) (((x) >> 17 | (x) << 15) ^ ((x) >> 19 | (x) << 13) ^ ((x) >> 10))
23
24
#define Round(a,b,c,d,e,f,g,h,k,w) do { \
25
uint32_t t1 = (h) + Sigma1(e) + Ch((e), (f), (g)) + (k) + (w); \
26
uint32_t t2 = Sigma0(a) + Maj((a), (b), (c)); \
27
(d) += t1; \
28
(h) = t1 + t2; \
29
} while(0)
30
31
static
void
secp256k1_sha256_initialize
(
secp256k1_sha256
*hash) {
32
hash->
s
[0] = 0x6a09e667ul;
33
hash->
s
[1] = 0xbb67ae85ul;
34
hash->
s
[2] = 0x3c6ef372ul;
35
hash->
s
[3] = 0xa54ff53aul;
36
hash->
s
[4] = 0x510e527ful;
37
hash->
s
[5] = 0x9b05688cul;
38
hash->
s
[6] = 0x1f83d9abul;
39
hash->
s
[7] = 0x5be0cd19ul;
40
hash->
bytes
= 0;
41
}
42
43
static
void
secp256k1_sha256_initialize_midstate
(
secp256k1_sha256
*hash, uint64_t bytes,
const
uint32_t state[8]) {
44
VERIFY_CHECK
((bytes & 0x3F) == 0);
45
VERIFY_CHECK
(state != NULL);
46
memcpy(hash->
s
, state,
sizeof
(hash->
s
));
47
hash->
bytes
= bytes;
48
}
49
51
static
void
secp256k1_sha256_transform_impl
(uint32_t* s,
const
unsigned
char
* buf) {
52
uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5],
g
= s[6], h = s[7];
53
uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15;
54
55
Round
(a, b, c, d, e, f,
g
, h, 0x428a2f98, w0 =
secp256k1_read_be32
(&buf[0]));
56
Round
(h, a, b, c, d, e, f,
g
, 0x71374491, w1 =
secp256k1_read_be32
(&buf[4]));
57
Round
(
g
, h, a, b, c, d, e, f, 0xb5c0fbcf, w2 =
secp256k1_read_be32
(&buf[8]));
58
Round
(f,
g
, h, a, b, c, d, e, 0xe9b5dba5, w3 =
secp256k1_read_be32
(&buf[12]));
59
Round
(e, f,
g
, h, a, b, c, d, 0x3956c25b, w4 =
secp256k1_read_be32
(&buf[16]));
60
Round
(d, e, f,
g
, h, a, b, c, 0x59f111f1, w5 =
secp256k1_read_be32
(&buf[20]));
61
Round
(c, d, e, f,
g
, h, a, b, 0x923f82a4, w6 =
secp256k1_read_be32
(&buf[24]));
62
Round
(b, c, d, e, f,
g
, h, a, 0xab1c5ed5, w7 =
secp256k1_read_be32
(&buf[28]));
63
Round
(a, b, c, d, e, f,
g
, h, 0xd807aa98, w8 =
secp256k1_read_be32
(&buf[32]));
64
Round
(h, a, b, c, d, e, f,
g
, 0x12835b01, w9 =
secp256k1_read_be32
(&buf[36]));
65
Round
(
g
, h, a, b, c, d, e, f, 0x243185be, w10 =
secp256k1_read_be32
(&buf[40]));
66
Round
(f,
g
, h, a, b, c, d, e, 0x550c7dc3, w11 =
secp256k1_read_be32
(&buf[44]));
67
Round
(e, f,
g
, h, a, b, c, d, 0x72be5d74, w12 =
secp256k1_read_be32
(&buf[48]));
68
Round
(d, e, f,
g
, h, a, b, c, 0x80deb1fe, w13 =
secp256k1_read_be32
(&buf[52]));
69
Round
(c, d, e, f,
g
, h, a, b, 0x9bdc06a7, w14 =
secp256k1_read_be32
(&buf[56]));
70
Round
(b, c, d, e, f,
g
, h, a, 0xc19bf174, w15 =
secp256k1_read_be32
(&buf[60]));
71
72
Round
(a, b, c, d, e, f,
g
, h, 0xe49b69c1, w0 +=
sigma1
(w14) + w9 +
sigma0
(w1));
73
Round
(h, a, b, c, d, e, f,
g
, 0xefbe4786, w1 +=
sigma1
(w15) + w10 +
sigma0
(w2));
74
Round
(
g
, h, a, b, c, d, e, f, 0x0fc19dc6, w2 +=
sigma1
(w0) + w11 +
sigma0
(w3));
75
Round
(f,
g
, h, a, b, c, d, e, 0x240ca1cc, w3 +=
sigma1
(w1) + w12 +
sigma0
(w4));
76
Round
(e, f,
g
, h, a, b, c, d, 0x2de92c6f, w4 +=
sigma1
(w2) + w13 +
sigma0
(w5));
77
Round
(d, e, f,
g
, h, a, b, c, 0x4a7484aa, w5 +=
sigma1
(w3) + w14 +
sigma0
(w6));
78
Round
(c, d, e, f,
g
, h, a, b, 0x5cb0a9dc, w6 +=
sigma1
(w4) + w15 +
sigma0
(w7));
79
Round
(b, c, d, e, f,
g
, h, a, 0x76f988da, w7 +=
sigma1
(w5) + w0 +
sigma0
(w8));
80
Round
(a, b, c, d, e, f,
g
, h, 0x983e5152, w8 +=
sigma1
(w6) + w1 +
sigma0
(w9));
81
Round
(h, a, b, c, d, e, f,
g
, 0xa831c66d, w9 +=
sigma1
(w7) + w2 +
sigma0
(w10));
82
Round
(
g
, h, a, b, c, d, e, f, 0xb00327c8, w10 +=
sigma1
(w8) + w3 +
sigma0
(w11));
83
Round
(f,
g
, h, a, b, c, d, e, 0xbf597fc7, w11 +=
sigma1
(w9) + w4 +
sigma0
(w12));
84
Round
(e, f,
g
, h, a, b, c, d, 0xc6e00bf3, w12 +=
sigma1
(w10) + w5 +
sigma0
(w13));
85
Round
(d, e, f,
g
, h, a, b, c, 0xd5a79147, w13 +=
sigma1
(w11) + w6 +
sigma0
(w14));
86
Round
(c, d, e, f,
g
, h, a, b, 0x06ca6351, w14 +=
sigma1
(w12) + w7 +
sigma0
(w15));
87
Round
(b, c, d, e, f,
g
, h, a, 0x14292967, w15 +=
sigma1
(w13) + w8 +
sigma0
(w0));
88
89
Round
(a, b, c, d, e, f,
g
, h, 0x27b70a85, w0 +=
sigma1
(w14) + w9 +
sigma0
(w1));
90
Round
(h, a, b, c, d, e, f,
g
, 0x2e1b2138, w1 +=
sigma1
(w15) + w10 +
sigma0
(w2));
91
Round
(
g
, h, a, b, c, d, e, f, 0x4d2c6dfc, w2 +=
sigma1
(w0) + w11 +
sigma0
(w3));
92
Round
(f,
g
, h, a, b, c, d, e, 0x53380d13, w3 +=
sigma1
(w1) + w12 +
sigma0
(w4));
93
Round
(e, f,
g
, h, a, b, c, d, 0x650a7354, w4 +=
sigma1
(w2) + w13 +
sigma0
(w5));
94
Round
(d, e, f,
g
, h, a, b, c, 0x766a0abb, w5 +=
sigma1
(w3) + w14 +
sigma0
(w6));
95
Round
(c, d, e, f,
g
, h, a, b, 0x81c2c92e, w6 +=
sigma1
(w4) + w15 +
sigma0
(w7));
96
Round
(b, c, d, e, f,
g
, h, a, 0x92722c85, w7 +=
sigma1
(w5) + w0 +
sigma0
(w8));
97
Round
(a, b, c, d, e, f,
g
, h, 0xa2bfe8a1, w8 +=
sigma1
(w6) + w1 +
sigma0
(w9));
98
Round
(h, a, b, c, d, e, f,
g
, 0xa81a664b, w9 +=
sigma1
(w7) + w2 +
sigma0
(w10));
99
Round
(
g
, h, a, b, c, d, e, f, 0xc24b8b70, w10 +=
sigma1
(w8) + w3 +
sigma0
(w11));
100
Round
(f,
g
, h, a, b, c, d, e, 0xc76c51a3, w11 +=
sigma1
(w9) + w4 +
sigma0
(w12));
101
Round
(e, f,
g
, h, a, b, c, d, 0xd192e819, w12 +=
sigma1
(w10) + w5 +
sigma0
(w13));
102
Round
(d, e, f,
g
, h, a, b, c, 0xd6990624, w13 +=
sigma1
(w11) + w6 +
sigma0
(w14));
103
Round
(c, d, e, f,
g
, h, a, b, 0xf40e3585, w14 +=
sigma1
(w12) + w7 +
sigma0
(w15));
104
Round
(b, c, d, e, f,
g
, h, a, 0x106aa070, w15 +=
sigma1
(w13) + w8 +
sigma0
(w0));
105
106
Round
(a, b, c, d, e, f,
g
, h, 0x19a4c116, w0 +=
sigma1
(w14) + w9 +
sigma0
(w1));
107
Round
(h, a, b, c, d, e, f,
g
, 0x1e376c08, w1 +=
sigma1
(w15) + w10 +
sigma0
(w2));
108
Round
(
g
, h, a, b, c, d, e, f, 0x2748774c, w2 +=
sigma1
(w0) + w11 +
sigma0
(w3));
109
Round
(f,
g
, h, a, b, c, d, e, 0x34b0bcb5, w3 +=
sigma1
(w1) + w12 +
sigma0
(w4));
110
Round
(e, f,
g
, h, a, b, c, d, 0x391c0cb3, w4 +=
sigma1
(w2) + w13 +
sigma0
(w5));
111
Round
(d, e, f,
g
, h, a, b, c, 0x4ed8aa4a, w5 +=
sigma1
(w3) + w14 +
sigma0
(w6));
112
Round
(c, d, e, f,
g
, h, a, b, 0x5b9cca4f, w6 +=
sigma1
(w4) + w15 +
sigma0
(w7));
113
Round
(b, c, d, e, f,
g
, h, a, 0x682e6ff3, w7 +=
sigma1
(w5) + w0 +
sigma0
(w8));
114
Round
(a, b, c, d, e, f,
g
, h, 0x748f82ee, w8 +=
sigma1
(w6) + w1 +
sigma0
(w9));
115
Round
(h, a, b, c, d, e, f,
g
, 0x78a5636f, w9 +=
sigma1
(w7) + w2 +
sigma0
(w10));
116
Round
(
g
, h, a, b, c, d, e, f, 0x84c87814, w10 +=
sigma1
(w8) + w3 +
sigma0
(w11));
117
Round
(f,
g
, h, a, b, c, d, e, 0x8cc70208, w11 +=
sigma1
(w9) + w4 +
sigma0
(w12));
118
Round
(e, f,
g
, h, a, b, c, d, 0x90befffa, w12 +=
sigma1
(w10) + w5 +
sigma0
(w13));
119
Round
(d, e, f,
g
, h, a, b, c, 0xa4506ceb, w13 +=
sigma1
(w11) + w6 +
sigma0
(w14));
120
Round
(c, d, e, f,
g
, h, a, b, 0xbef9a3f7, w14 +
sigma1
(w12) + w7 +
sigma0
(w15));
121
Round
(b, c, d, e, f,
g
, h, a, 0xc67178f2, w15 +
sigma1
(w13) + w8 +
sigma0
(w0));
122
123
s[0] += a;
124
s[1] += b;
125
s[2] += c;
126
s[3] += d;
127
s[4] += e;
128
s[5] += f;
129
s[6] +=
g
;
130
s[7] += h;
131
}
132
133
static
void
secp256k1_sha256_transform
(uint32_t *state,
const
unsigned
char
*blocks64,
size_t
n_blocks) {
134
while
(n_blocks--) {
135
secp256k1_sha256_transform_impl
(state, blocks64);
136
blocks64 += 64;
137
}
138
}
139
140
/* Perform a smoke test on a supplied SHA256 compression function. */
141
static
int
secp256k1_sha256_smoke_test
(
const
secp256k1_sha256_compression_function
fn_compression) {
142
secp256k1_hash_ctx
ctx;
143
secp256k1_sha256
sha_msg, sha_accum;
144
unsigned
char
out[32];
145
size_t
i, j;
146
147
/* SHA256 works on 64 byte blocks, secp256k1_sha256_write gives as many blocks
148
* at once to compression, so the count is what varies here. A SIMD implementation
149
* typically hashes four or eight at a time, then any left over one by one.
150
* These lengths cover every number from 1 to 9, which includes counts that
151
* divide evenly and counts leaving one, two or three over. */
152
static
const
size_t
msg_lens[] = {
153
0, 1, 28,
/* Shorter than a block, so padding makes up the rest */
154
55, 56,
/* Final 0x80 and 8 byte length fit in the last block, and don't */
155
64, 128, 192, 256, 320,
/* 1 to 5 blocks */
156
384, 448, 512, 576
/* 6 to 9 blocks */
157
};
158
unsigned
char
msg[640];
/* Longest message, plus 64 for the shifted start */
159
160
/* Accumulated digest of every message, hashed with the built-in secp256k1_sha256_transform.
161
* Note: To regenerate set 'ctx.fn_sha256_compression = secp256k1_sha256_transform' below
162
* and print the sha_accum digest. */
163
static
const
unsigned
char
accum_expected[32] = {
164
0x22, 0x8E, 0x6A, 0x1F, 0x78, 0x02, 0x1B, 0xCF,
165
0x4E, 0xF4, 0xEA, 0xB3, 0x8A, 0x40, 0x69, 0xBF,
166
0x8D, 0xF3, 0x72, 0xAB, 0xE2, 0x11, 0x93, 0xA6,
167
0xE6, 0x46, 0x98, 0xBF, 0xD4, 0x3D, 0x19, 0x84,
168
};
169
/* The purpose of this VERIFY_CHECK is to make anyone aware that they
170
* should also change the size of msg_buf when changing the length of the
171
* longest message. */
172
VERIFY_CHECK
(msg_lens[
ARRAY_SIZE
(msg_lens) - 1] == 576);
173
174
VERIFY_CHECK
(fn_compression != NULL);
175
secp256k1_hash_ctx_init
(&ctx);
176
ctx.
fn_sha256_compression
= fn_compression;
177
secp256k1_sha256_initialize
(&sha_accum);
178
179
/* No two blocks of a message are equal, so a function that doesn't advance the block
180
* pointer gives a different digest. 251 is prime, so its repeats only line up with
181
* block starts every 251 blocks; wrapping at 256 would do so every 4. */
182
for
(i = 0; i <
sizeof
(msg); i++) {
183
msg[i] = (
unsigned
char)(i % 251);
184
}
185
186
/* Each round starts one byte further along to check different alignments,
187
* secp256k1_sha256_write invokes compression directly on input >= 64 bytes */
188
for
(i = 0; i < 64; i++) {
189
unsigned
char
*
m
= msg + i;
190
m
[0] ^= 0xff;
/* Changes the first byte, so every state after it changes too */
191
for
(j = 0; j <
ARRAY_SIZE
(msg_lens); j++) {
192
secp256k1_sha256_initialize
(&sha_msg);
193
secp256k1_sha256_write
(&ctx, &sha_msg,
m
, msg_lens[j]);
194
secp256k1_sha256_finalize
(&ctx, &sha_msg, out);
195
secp256k1_sha256_write
(&ctx, &sha_accum, out, 32);
196
}
197
m
[0] ^= 0xff;
/* Reset first byte */
198
}
199
200
/* Compare against pre-computed accumulated digest */
201
secp256k1_sha256_finalize
(&ctx, &sha_accum, out);
202
return
secp256k1_memcmp_var
(accum_expected, out, 32) == 0;
203
}
204
205
static
void
secp256k1_hash_ctx_init
(
secp256k1_hash_ctx
*hash_ctx) {
206
VERIFY_CHECK
(hash_ctx != NULL);
207
hash_ctx->
fn_sha256_compression
=
secp256k1_sha256_transform
;
208
}
209
210
static
void
secp256k1_sha256_write
(
const
secp256k1_hash_ctx
*hash_ctx,
secp256k1_sha256
*hash,
const
unsigned
char
*data,
size_t
len) {
211
size_t
chunk_len;
212
size_t
bufsize = hash->
bytes
& 0x3F;
213
hash->
bytes
+= len;
214
VERIFY_CHECK
(hash->
bytes
>= len);
215
VERIFY_CHECK
(hash_ctx != NULL);
216
VERIFY_CHECK
(hash_ctx->
fn_sha256_compression
!= NULL);
217
218
/* If we exceed the 64-byte block size with this input, process it and wipe the buffer */
219
chunk_len = 64 - bufsize;
220
if
(bufsize && len >= chunk_len) {
221
memcpy(hash->
buf
+ bufsize, data, chunk_len);
222
data += chunk_len;
223
len -= chunk_len;
224
hash_ctx->
fn_sha256_compression
(hash->
s
, hash->
buf
, 1);
225
bufsize = 0;
226
}
227
228
/* If we still have data to process, invoke compression directly on the input */
229
if
(len >= 64) {
230
const
size_t
n_blocks = len / 64;
231
const
size_t
advance = n_blocks * 64;
232
hash_ctx->
fn_sha256_compression
(hash->
s
, data, n_blocks);
233
data += advance;
234
len -= advance;
235
}
236
237
/* Fill the buffer with what remains */
238
if
(len) {
239
memcpy(hash->
buf
+ bufsize, data, len);
240
}
241
}
242
243
static
void
secp256k1_sha256_finalize
(
const
secp256k1_hash_ctx
*hash_ctx,
secp256k1_sha256
*hash,
unsigned
char
*out32) {
244
static
const
unsigned
char
pad[64] = {0x80};
245
unsigned
char
sizedesc[8];
246
int
i;
247
/* The maximum message size of SHA256 is 2^64-1 bits. */
248
VERIFY_CHECK
(hash->
bytes
< ((uint64_t)1 << 61));
249
secp256k1_write_be32
(&sizedesc[0], hash->
bytes
>> 29);
250
secp256k1_write_be32
(&sizedesc[4], hash->
bytes
<< 3);
251
secp256k1_sha256_write
(hash_ctx, hash, pad, 1 + ((119 - (hash->
bytes
% 64)) % 64));
252
secp256k1_sha256_write
(hash_ctx, hash, sizedesc, 8);
253
for
(i = 0; i < 8; i++) {
254
secp256k1_write_be32
(&out32[4*i], hash->
s
[i]);
255
hash->
s
[i] = 0;
256
}
257
}
258
259
/* Initializes a sha256 struct and writes the 64 byte string
260
* SHA256(tag)||SHA256(tag) into it. */
261
static
void
secp256k1_sha256_initialize_tagged
(
const
secp256k1_hash_ctx
*hash_ctx,
secp256k1_sha256
*hash,
const
unsigned
char
*tag,
size_t
taglen) {
262
unsigned
char
buf[32];
263
secp256k1_sha256_initialize
(hash);
264
secp256k1_sha256_write
(hash_ctx, hash, tag, taglen);
265
secp256k1_sha256_finalize
(hash_ctx, hash, buf);
266
267
secp256k1_sha256_initialize
(hash);
268
secp256k1_sha256_write
(hash_ctx, hash, buf, 32);
269
secp256k1_sha256_write
(hash_ctx, hash, buf, 32);
270
}
271
272
static
void
secp256k1_sha256_clear
(
secp256k1_sha256
*hash) {
273
secp256k1_memclear_explicit
(hash,
sizeof
(*hash));
274
}
275
276
static
void
secp256k1_hmac_sha256_initialize
(
const
secp256k1_hash_ctx
*hash_ctx,
secp256k1_hmac_sha256
*hash,
const
unsigned
char
*key,
size_t
keylen) {
277
size_t
n;
278
unsigned
char
rkey[64];
279
if
(keylen <=
sizeof
(rkey)) {
280
memcpy(rkey, key, keylen);
281
memset(rkey + keylen, 0,
sizeof
(rkey) - keylen);
282
}
else
{
283
secp256k1_sha256
sha256
;
284
secp256k1_sha256_initialize
(&
sha256
);
285
secp256k1_sha256_write
(hash_ctx, &
sha256
, key, keylen);
286
secp256k1_sha256_finalize
(hash_ctx, &
sha256
, rkey);
287
memset(rkey + 32, 0, 32);
288
}
289
290
secp256k1_sha256_initialize
(&hash->
outer
);
291
for
(n = 0; n <
sizeof
(rkey); n++) {
292
rkey[n] ^= 0x5c;
293
}
294
secp256k1_sha256_write
(hash_ctx, &hash->
outer
, rkey,
sizeof
(rkey));
295
296
secp256k1_sha256_initialize
(&hash->
inner
);
297
for
(n = 0; n <
sizeof
(rkey); n++) {
298
rkey[n] ^= 0x5c ^ 0x36;
299
}
300
secp256k1_sha256_write
(hash_ctx, &hash->
inner
, rkey,
sizeof
(rkey));
301
secp256k1_memclear_explicit
(rkey,
sizeof
(rkey));
302
}
303
304
static
void
secp256k1_hmac_sha256_write
(
const
secp256k1_hash_ctx
*hash_ctx,
secp256k1_hmac_sha256
*hash,
const
unsigned
char
*data,
size_t
size) {
305
secp256k1_sha256_write
(hash_ctx, &hash->
inner
, data, size);
306
}
307
308
static
void
secp256k1_hmac_sha256_finalize
(
const
secp256k1_hash_ctx
*hash_ctx,
secp256k1_hmac_sha256
*hash,
unsigned
char
*out32) {
309
unsigned
char
temp[32];
310
secp256k1_sha256_finalize
(hash_ctx, &hash->
inner
, temp);
311
secp256k1_sha256_write
(hash_ctx, &hash->
outer
, temp, 32);
312
secp256k1_memclear_explicit
(temp,
sizeof
(temp));
313
secp256k1_sha256_finalize
(hash_ctx, &hash->
outer
, out32);
314
}
315
316
static
void
secp256k1_hmac_sha256_clear
(
secp256k1_hmac_sha256
*hash) {
317
secp256k1_memclear_explicit
(hash,
sizeof
(*hash));
318
}
319
320
static
void
secp256k1_rfc6979_hmac_sha256_initialize
(
const
secp256k1_hash_ctx
*hash_ctx,
secp256k1_rfc6979_hmac_sha256
*
rng
,
const
unsigned
char
*key,
size_t
keylen) {
321
secp256k1_hmac_sha256
hmac;
322
static
const
unsigned
char
zero[1] = {0x00};
323
static
const
unsigned
char
one[1] = {0x01};
324
325
memset(
rng
->v, 0x01, 32);
/* RFC6979 3.2.b. */
326
memset(
rng
->k, 0x00, 32);
/* RFC6979 3.2.c. */
327
328
/* RFC6979 3.2.d. */
329
secp256k1_hmac_sha256_initialize
(hash_ctx, &hmac,
rng
->k, 32);
330
secp256k1_hmac_sha256_write
(hash_ctx, &hmac,
rng
->v, 32);
331
secp256k1_hmac_sha256_write
(hash_ctx, &hmac, zero, 1);
332
secp256k1_hmac_sha256_write
(hash_ctx, &hmac, key, keylen);
333
secp256k1_hmac_sha256_finalize
(hash_ctx, &hmac,
rng
->k);
334
secp256k1_hmac_sha256_initialize
(hash_ctx, &hmac,
rng
->k, 32);
335
secp256k1_hmac_sha256_write
(hash_ctx, &hmac,
rng
->v, 32);
336
secp256k1_hmac_sha256_finalize
(hash_ctx, &hmac,
rng
->v);
337
338
/* RFC6979 3.2.f. */
339
secp256k1_hmac_sha256_initialize
(hash_ctx, &hmac,
rng
->k, 32);
340
secp256k1_hmac_sha256_write
(hash_ctx, &hmac,
rng
->v, 32);
341
secp256k1_hmac_sha256_write
(hash_ctx, &hmac, one, 1);
342
secp256k1_hmac_sha256_write
(hash_ctx, &hmac, key, keylen);
343
secp256k1_hmac_sha256_finalize
(hash_ctx, &hmac,
rng
->k);
344
secp256k1_hmac_sha256_initialize
(hash_ctx, &hmac,
rng
->k, 32);
345
secp256k1_hmac_sha256_write
(hash_ctx, &hmac,
rng
->v, 32);
346
secp256k1_hmac_sha256_finalize
(hash_ctx, &hmac,
rng
->v);
347
rng
->retry = 0;
348
}
349
350
static
void
secp256k1_rfc6979_hmac_sha256_generate
(
const
secp256k1_hash_ctx
*hash_ctx,
secp256k1_rfc6979_hmac_sha256
*
rng
,
unsigned
char
*out,
size_t
outlen) {
351
/* RFC6979 3.2.h. */
352
static
const
unsigned
char
zero[1] = {0x00};
353
if
(
rng
->retry) {
354
secp256k1_hmac_sha256
hmac;
355
secp256k1_hmac_sha256_initialize
(hash_ctx, &hmac,
rng
->k, 32);
356
secp256k1_hmac_sha256_write
(hash_ctx, &hmac,
rng
->v, 32);
357
secp256k1_hmac_sha256_write
(hash_ctx, &hmac, zero, 1);
358
secp256k1_hmac_sha256_finalize
(hash_ctx, &hmac,
rng
->k);
359
secp256k1_hmac_sha256_initialize
(hash_ctx, &hmac,
rng
->k, 32);
360
secp256k1_hmac_sha256_write
(hash_ctx, &hmac,
rng
->v, 32);
361
secp256k1_hmac_sha256_finalize
(hash_ctx, &hmac,
rng
->v);
362
}
363
364
while
(outlen > 0) {
365
secp256k1_hmac_sha256
hmac;
366
size_t
now = outlen;
367
secp256k1_hmac_sha256_initialize
(hash_ctx, &hmac,
rng
->k, 32);
368
secp256k1_hmac_sha256_write
(hash_ctx, &hmac,
rng
->v, 32);
369
secp256k1_hmac_sha256_finalize
(hash_ctx, &hmac,
rng
->v);
370
if
(now > 32) {
371
now = 32;
372
}
373
memcpy(out,
rng
->v, now);
374
out += now;
375
outlen -= now;
376
}
377
378
rng
->retry = 1;
379
}
380
381
static
void
secp256k1_rfc6979_hmac_sha256_finalize
(
secp256k1_rfc6979_hmac_sha256
*
rng
) {
382
(void)
rng
;
383
}
384
385
static
void
secp256k1_rfc6979_hmac_sha256_clear
(
secp256k1_rfc6979_hmac_sha256
*
rng
) {
386
secp256k1_memclear_explicit
(
rng
,
sizeof
(*
rng
));
387
}
388
389
#undef Round
390
#undef sigma1
391
#undef sigma0
392
#undef Sigma1
393
#undef Sigma0
394
#undef Maj
395
#undef Ch
396
397
#endif
/* SECP256K1_HASH_IMPL_H */
sigma1
#define sigma1(x)
Definition
hash_impl.h:22
secp256k1_sha256_smoke_test
static int secp256k1_sha256_smoke_test(const secp256k1_sha256_compression_function fn_compression)
Definition
hash_impl.h:141
secp256k1_sha256_transform
static void secp256k1_sha256_transform(uint32_t *state, const unsigned char *blocks64, size_t n_blocks)
Definition
hash_impl.h:133
Round
#define Round(a, b, c, d, e, f, g, h, k, w)
Definition
hash_impl.h:24
sigma0
#define sigma0(x)
Definition
hash_impl.h:21
secp256k1_sha256_initialize_tagged
static void secp256k1_sha256_initialize_tagged(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, const unsigned char *tag, size_t taglen)
Definition
hash_impl.h:261
secp256k1_sha256_transform_impl
static void secp256k1_sha256_transform_impl(uint32_t *s, const unsigned char *buf)
Perform one SHA-256 transformation, processing 16 big endian 32-bit words.
Definition
hash_impl.h:51
sha256
Internal SHA-256 implementation.
Definition
sha256.cpp:68
hash.h
secp256k1_hmac_sha256_finalize
static void secp256k1_hmac_sha256_finalize(const secp256k1_hash_ctx *hash_ctx, secp256k1_hmac_sha256 *hash, unsigned char *out32)
secp256k1_sha256_finalize
static void secp256k1_sha256_finalize(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, unsigned char *out32)
secp256k1_sha256_initialize
static void secp256k1_sha256_initialize(secp256k1_sha256 *hash)
secp256k1_hmac_sha256_clear
static void secp256k1_hmac_sha256_clear(secp256k1_hmac_sha256 *hash)
secp256k1_hash_ctx_init
static void secp256k1_hash_ctx_init(secp256k1_hash_ctx *hash_ctx)
secp256k1_sha256_initialize_midstate
static void secp256k1_sha256_initialize_midstate(secp256k1_sha256 *hash, uint64_t bytes, const uint32_t state[8])
secp256k1_hmac_sha256_write
static void secp256k1_hmac_sha256_write(const secp256k1_hash_ctx *hash_ctx, secp256k1_hmac_sha256 *hash, const unsigned char *data, size_t size)
secp256k1_rfc6979_hmac_sha256_clear
static void secp256k1_rfc6979_hmac_sha256_clear(secp256k1_rfc6979_hmac_sha256 *rng)
secp256k1_rfc6979_hmac_sha256_generate
static void secp256k1_rfc6979_hmac_sha256_generate(const secp256k1_hash_ctx *hash_ctx, secp256k1_rfc6979_hmac_sha256 *rng, unsigned char *out, size_t outlen)
secp256k1_rfc6979_hmac_sha256_initialize
static void secp256k1_rfc6979_hmac_sha256_initialize(const secp256k1_hash_ctx *hash_ctx, secp256k1_rfc6979_hmac_sha256 *rng, const unsigned char *key, size_t keylen)
secp256k1_rfc6979_hmac_sha256_finalize
static void secp256k1_rfc6979_hmac_sha256_finalize(secp256k1_rfc6979_hmac_sha256 *rng)
secp256k1_sha256_write
static void secp256k1_sha256_write(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, const unsigned char *data, size_t size)
secp256k1_sha256_clear
static void secp256k1_sha256_clear(secp256k1_sha256 *hash)
secp256k1_hmac_sha256_initialize
static void secp256k1_hmac_sha256_initialize(const secp256k1_hash_ctx *hash_ctx, secp256k1_hmac_sha256 *hash, const unsigned char *key, size_t size)
util.h
secp256k1_memclear_explicit
static SECP256K1_INLINE void secp256k1_memclear_explicit(void *ptr, size_t len)
Definition
util.h:268
secp256k1_memcmp_var
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
Definition
util.h:281
ARRAY_SIZE
#define ARRAY_SIZE(arr)
Definition
util.h:194
secp256k1_read_be32
static SECP256K1_INLINE uint32_t secp256k1_read_be32(const unsigned char *p)
Definition
util.h:428
secp256k1_write_be32
static SECP256K1_INLINE void secp256k1_write_be32(unsigned char *p, uint32_t x)
Definition
util.h:436
VERIFY_CHECK
#define VERIFY_CHECK(cond)
Definition
util.h:169
secp256k1_sha256_compression_function
void(*) secp256k1_sha256_compression_function(uint32_t *state, const unsigned char *blocks64, size_t n_blocks)
A pointer to a function implementing SHA256's internal compression function.
Definition
secp256k1.h:407
ByteUnit::m
@ m
Definition
strencodings.h:48
ByteUnit::g
@ g
Definition
strencodings.h:50
string.h
secp256k1_hash_ctx
Definition
hash.h:15
secp256k1_hash_ctx::fn_sha256_compression
secp256k1_sha256_compression_function fn_sha256_compression
Definition
hash.h:16
secp256k1_hmac_sha256
Definition
hash.h:36
secp256k1_hmac_sha256::outer
secp256k1_sha256 outer
Definition
hash.h:37
secp256k1_hmac_sha256::inner
secp256k1_sha256 inner
Definition
hash.h:37
secp256k1_rfc6979_hmac_sha256
Definition
hash.h:45
secp256k1_sha256
Definition
hash.h:21
secp256k1_sha256::bytes
uint64_t bytes
Definition
hash.h:24
secp256k1_sha256::buf
unsigned char buf[64]
Definition
hash.h:23
secp256k1_sha256::s
uint32_t s[8]
Definition
hash.h:22
rng
FastRandomContext rng
Definition
dbwrapper.cpp:413
Generated on
for Bitcoin Core by
1.18.0