7#ifndef SECP256K1_HASH_IMPL_H
8#define SECP256K1_HASH_IMPL_H
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))
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)); \
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;
46 memcpy(hash->
s, state,
sizeof(hash->
s));
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;
144 unsigned char out[32];
152 static const size_t msg_lens[] = {
155 64, 128, 192, 256, 320,
158 unsigned char msg[640];
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,
182 for (i = 0; i <
sizeof(
msg); i++) {
183 msg[i] = (
unsigned char)(i % 251);
188 for (i = 0; i < 64; i++) {
189 unsigned char *
m =
msg + i;
212 size_t bufsize = hash->
bytes & 0x3F;
219 chunk_len = 64 - bufsize;
220 if (bufsize && len >= chunk_len) {
221 memcpy(hash->
buf + bufsize,
data, chunk_len);
230 const size_t n_blocks = len / 64;
231 const size_t advance = n_blocks * 64;
239 memcpy(hash->
buf + bufsize,
data, len);
244 static const unsigned char pad[64] = {0x80};
245 unsigned char sizedesc[8];
253 for (i = 0; i < 8; i++) {
262 unsigned char buf[32];
278 unsigned char rkey[64];
279 if (keylen <=
sizeof(rkey)) {
280 memcpy(rkey, key, keylen);
281 memset(rkey + keylen, 0,
sizeof(rkey) - keylen);
287 memset(rkey + 32, 0, 32);
291 for (n = 0; n <
sizeof(rkey); n++) {
297 for (n = 0; n <
sizeof(rkey); n++) {
298 rkey[n] ^= 0x5c ^ 0x36;
309 unsigned char temp[32];
322 static const unsigned char zero[1] = {0x00};
323 static const unsigned char one[1] = {0x01};
325 memset(
rng->v, 0x01, 32);
326 memset(
rng->k, 0x00, 32);
352 static const unsigned char zero[1] = {0x00};
static const PrecomputedData data
Precomputed COutPoint and CCoins values.
static void secp256k1_hmac_sha256_finalize(const secp256k1_hash_ctx *hash_ctx, secp256k1_hmac_sha256 *hash, unsigned char *out32)
static void secp256k1_sha256_finalize(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, unsigned char *out32)
static int secp256k1_sha256_smoke_test(const secp256k1_sha256_compression_function fn_compression)
static void secp256k1_sha256_initialize(secp256k1_sha256 *hash)
static void secp256k1_sha256_transform(uint32_t *state, const unsigned char *blocks64, size_t n_blocks)
static void secp256k1_hmac_sha256_clear(secp256k1_hmac_sha256 *hash)
static void secp256k1_hash_ctx_init(secp256k1_hash_ctx *hash_ctx)
#define Round(a, b, c, d, e, f, g, h, k, w)
static void secp256k1_sha256_initialize_midstate(secp256k1_sha256 *hash, uint64_t bytes, const uint32_t state[8])
static void secp256k1_hmac_sha256_write(const secp256k1_hash_ctx *hash_ctx, secp256k1_hmac_sha256 *hash, const unsigned char *data, size_t size)
static void secp256k1_hmac_sha256_initialize(const secp256k1_hash_ctx *hash_ctx, secp256k1_hmac_sha256 *hash, const unsigned char *key, size_t keylen)
static void secp256k1_rfc6979_hmac_sha256_clear(secp256k1_rfc6979_hmac_sha256 *rng)
static void secp256k1_rfc6979_hmac_sha256_generate(const secp256k1_hash_ctx *hash_ctx, secp256k1_rfc6979_hmac_sha256 *rng, unsigned char *out, size_t outlen)
static void secp256k1_sha256_write(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, const unsigned char *data, size_t len)
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)
static void secp256k1_sha256_initialize_tagged(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, const unsigned char *tag, size_t taglen)
static void secp256k1_rfc6979_hmac_sha256_finalize(secp256k1_rfc6979_hmac_sha256 *rng)
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.
static void secp256k1_sha256_clear(secp256k1_sha256 *hash)
Internal SHA-256 implementation.
static SECP256K1_INLINE void secp256k1_memclear_explicit(void *ptr, size_t len)
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
static SECP256K1_INLINE uint32_t secp256k1_read_be32(const unsigned char *p)
static SECP256K1_INLINE void secp256k1_write_be32(unsigned char *p, uint32_t x)
#define VERIFY_CHECK(cond)
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.
secp256k1_sha256_compression_function fn_sha256_compression