28
28
(h) = t1 + t2; \
29
29
} while(0)
30
30
31
- #if defined(SECP256K1_BIG_ENDIAN )
32
- #define BE32 (x ) (x)
33
- #elif defined(SECP256K1_LITTLE_ENDIAN )
34
- #define BE32 (p ) ((((p) & 0xFF) << 24) | (((p) & 0xFF00) << 8) | (((p) & 0xFF0000) >> 8) | (((p) & 0xFF000000) >> 24))
35
- #endif
36
-
37
31
static void secp256k1_sha256_initialize (secp256k1_sha256 * hash ) {
38
32
hash -> s [0 ] = 0x6a09e667ul ;
39
33
hash -> s [1 ] = 0xbb67ae85ul ;
@@ -47,26 +41,26 @@ static void secp256k1_sha256_initialize(secp256k1_sha256 *hash) {
47
41
}
48
42
49
43
/** Perform one SHA-256 transformation, processing 16 big endian 32-bit words. */
50
- static void secp256k1_sha256_transform (uint32_t * s , const uint32_t * chunk ) {
44
+ static void secp256k1_sha256_transform (uint32_t * s , const unsigned char * buf ) {
51
45
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 ];
52
46
uint32_t w0 , w1 , w2 , w3 , w4 , w5 , w6 , w7 , w8 , w9 , w10 , w11 , w12 , w13 , w14 , w15 ;
53
47
54
- Round (a , b , c , d , e , f , g , h , 0x428a2f98 , w0 = BE32 ( chunk [0 ]));
55
- Round (h , a , b , c , d , e , f , g , 0x71374491 , w1 = BE32 ( chunk [ 1 ]));
56
- Round (g , h , a , b , c , d , e , f , 0xb5c0fbcf , w2 = BE32 ( chunk [ 2 ]));
57
- Round (f , g , h , a , b , c , d , e , 0xe9b5dba5 , w3 = BE32 ( chunk [ 3 ]));
58
- Round (e , f , g , h , a , b , c , d , 0x3956c25b , w4 = BE32 ( chunk [ 4 ]));
59
- Round (d , e , f , g , h , a , b , c , 0x59f111f1 , w5 = BE32 ( chunk [ 5 ]));
60
- Round (c , d , e , f , g , h , a , b , 0x923f82a4 , w6 = BE32 ( chunk [ 6 ]));
61
- Round (b , c , d , e , f , g , h , a , 0xab1c5ed5 , w7 = BE32 ( chunk [ 7 ]));
62
- Round (a , b , c , d , e , f , g , h , 0xd807aa98 , w8 = BE32 ( chunk [ 8 ]));
63
- Round (h , a , b , c , d , e , f , g , 0x12835b01 , w9 = BE32 ( chunk [ 9 ]));
64
- Round (g , h , a , b , c , d , e , f , 0x243185be , w10 = BE32 ( chunk [ 10 ]));
65
- Round (f , g , h , a , b , c , d , e , 0x550c7dc3 , w11 = BE32 ( chunk [ 11 ]));
66
- Round (e , f , g , h , a , b , c , d , 0x72be5d74 , w12 = BE32 ( chunk [ 12 ]));
67
- Round (d , e , f , g , h , a , b , c , 0x80deb1fe , w13 = BE32 ( chunk [ 13 ]));
68
- Round (c , d , e , f , g , h , a , b , 0x9bdc06a7 , w14 = BE32 ( chunk [ 14 ]));
69
- Round (b , c , d , e , f , g , h , a , 0xc19bf174 , w15 = BE32 ( chunk [ 15 ]));
48
+ Round (a , b , c , d , e , f , g , h , 0x428a2f98 , w0 = secp256k1_read_be32 ( & buf [0 ]));
49
+ Round (h , a , b , c , d , e , f , g , 0x71374491 , w1 = secp256k1_read_be32 ( & buf [ 4 ]));
50
+ Round (g , h , a , b , c , d , e , f , 0xb5c0fbcf , w2 = secp256k1_read_be32 ( & buf [ 8 ]));
51
+ Round (f , g , h , a , b , c , d , e , 0xe9b5dba5 , w3 = secp256k1_read_be32 ( & buf [ 12 ]));
52
+ Round (e , f , g , h , a , b , c , d , 0x3956c25b , w4 = secp256k1_read_be32 ( & buf [ 16 ]));
53
+ Round (d , e , f , g , h , a , b , c , 0x59f111f1 , w5 = secp256k1_read_be32 ( & buf [ 20 ]));
54
+ Round (c , d , e , f , g , h , a , b , 0x923f82a4 , w6 = secp256k1_read_be32 ( & buf [ 24 ]));
55
+ Round (b , c , d , e , f , g , h , a , 0xab1c5ed5 , w7 = secp256k1_read_be32 ( & buf [ 28 ]));
56
+ Round (a , b , c , d , e , f , g , h , 0xd807aa98 , w8 = secp256k1_read_be32 ( & buf [ 32 ]));
57
+ Round (h , a , b , c , d , e , f , g , 0x12835b01 , w9 = secp256k1_read_be32 ( & buf [ 36 ]));
58
+ Round (g , h , a , b , c , d , e , f , 0x243185be , w10 = secp256k1_read_be32 ( & buf [ 40 ]));
59
+ Round (f , g , h , a , b , c , d , e , 0x550c7dc3 , w11 = secp256k1_read_be32 ( & buf [ 44 ]));
60
+ Round (e , f , g , h , a , b , c , d , 0x72be5d74 , w12 = secp256k1_read_be32 ( & buf [ 48 ]));
61
+ Round (d , e , f , g , h , a , b , c , 0x80deb1fe , w13 = secp256k1_read_be32 ( & buf [ 52 ]));
62
+ Round (c , d , e , f , g , h , a , b , 0x9bdc06a7 , w14 = secp256k1_read_be32 ( & buf [ 56 ]));
63
+ Round (b , c , d , e , f , g , h , a , 0xc19bf174 , w15 = secp256k1_read_be32 ( & buf [ 60 ]));
70
64
71
65
Round (a , b , c , d , e , f , g , h , 0xe49b69c1 , w0 += sigma1 (w14 ) + w9 + sigma0 (w1 ));
72
66
Round (h , a , b , c , d , e , f , g , 0xefbe4786 , w1 += sigma1 (w15 ) + w10 + sigma0 (w2 ));
@@ -136,7 +130,7 @@ static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *
136
130
while (len >= 64 - bufsize ) {
137
131
/* Fill the buffer, and process it. */
138
132
size_t chunk_len = 64 - bufsize ;
139
- memcpy ((( unsigned char * ) hash -> buf ) + bufsize , data , chunk_len );
133
+ memcpy (hash -> buf + bufsize , data , chunk_len );
140
134
data += chunk_len ;
141
135
len -= chunk_len ;
142
136
secp256k1_sha256_transform (hash -> s , hash -> buf );
@@ -150,20 +144,18 @@ static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *
150
144
151
145
static void secp256k1_sha256_finalize (secp256k1_sha256 * hash , unsigned char * out32 ) {
152
146
static const unsigned char pad [64 ] = {0x80 };
153
- uint32_t sizedesc [2 ];
154
- uint32_t out [8 ];
155
- int i = 0 ;
147
+ unsigned char sizedesc [8 ];
148
+ int i ;
156
149
/* The maximum message size of SHA256 is 2^64-1 bits. */
157
150
VERIFY_CHECK (hash -> bytes < ((uint64_t )1 << 61 ));
158
- sizedesc [0 ] = BE32 ( hash -> bytes >> 29 );
159
- sizedesc [1 ] = BE32 ( hash -> bytes << 3 );
151
+ secp256k1_write_be32 ( & sizedesc [0 ], hash -> bytes >> 29 );
152
+ secp256k1_write_be32 ( & sizedesc [4 ], hash -> bytes << 3 );
160
153
secp256k1_sha256_write (hash , pad , 1 + ((119 - (hash -> bytes % 64 )) % 64 ));
161
- secp256k1_sha256_write (hash , ( const unsigned char * ) sizedesc , 8 );
154
+ secp256k1_sha256_write (hash , sizedesc , 8 );
162
155
for (i = 0 ; i < 8 ; i ++ ) {
163
- out [ i ] = BE32 ( hash -> s [i ]);
156
+ secp256k1_write_be32 ( & out32 [ 4 * i ], hash -> s [i ]);
164
157
hash -> s [i ] = 0 ;
165
158
}
166
- memcpy (out32 , (const unsigned char * )out , 32 );
167
159
}
168
160
169
161
/* Initializes a sha256 struct and writes the 64 byte string
@@ -287,7 +279,6 @@ static void secp256k1_rfc6979_hmac_sha256_finalize(secp256k1_rfc6979_hmac_sha256
287
279
rng -> retry = 0 ;
288
280
}
289
281
290
- #undef BE32
291
282
#undef Round
292
283
#undef sigma1
293
284
#undef sigma0
0 commit comments