Skip to content

Commit 2991c91

Browse files
committed
Add SHA256 dispatcher
1 parent 4d50f38 commit 2991c91

File tree

5 files changed

+23
-2
lines changed

5 files changed

+23
-2
lines changed

src/bench/bench_bitcoin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "bench.h"
66

7+
#include "crypto/sha256.h"
78
#include "key.h"
89
#include "validation.h"
910
#include "util.h"
@@ -12,6 +13,7 @@
1213
int
1314
main(int argc, char** argv)
1415
{
16+
SHA256AutoDetect();
1517
RandomInit();
1618
ECC_Start();
1719
SetupEnvironment();

src/crypto/sha256.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <string.h>
1010

11+
#include <atomic>
12+
1113
// Internal implementation code.
1214
namespace
1315
{
@@ -131,8 +133,15 @@ void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks)
131133
}
132134

133135
} // namespace sha256
136+
137+
void (*Transform)(uint32_t*, const unsigned char*, size_t) = sha256::Transform;
138+
134139
} // namespace
135140

141+
std::string SHA256AutoDetect()
142+
{
143+
return "standard";
144+
}
136145

137146
////// SHA-256
138147

@@ -150,12 +159,12 @@ CSHA256& CSHA256::Write(const unsigned char* data, size_t len)
150159
memcpy(buf + bufsize, data, 64 - bufsize);
151160
bytes += 64 - bufsize;
152161
data += 64 - bufsize;
153-
sha256::Transform(s, buf, 1);
162+
Transform(s, buf, 1);
154163
bufsize = 0;
155164
}
156165
if (end - data >= 64) {
157166
size_t blocks = (end - data) / 64;
158-
sha256::Transform(s, data, blocks);
167+
Transform(s, data, blocks);
159168
data += 64 * blocks;
160169
bytes += 64 * blocks;
161170
}

src/crypto/sha256.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <stdint.h>
99
#include <stdlib.h>
10+
#include <string>
1011

1112
/** A hasher class for SHA-256. */
1213
class CSHA256
@@ -25,4 +26,9 @@ class CSHA256
2526
CSHA256& Reset();
2627
};
2728

29+
/** Autodetect the best available SHA256 implementation.
30+
* Returns the name of the implementation.
31+
*/
32+
std::string SHA256AutoDetect();
33+
2834
#endif // BITCOIN_CRYPTO_SHA256_H

src/init.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,8 @@ bool AppInitSanityChecks()
11611161
// ********************************************************* Step 4: sanity checks
11621162

11631163
// Initialize elliptic curve code
1164+
std::string sha256_algo = SHA256AutoDetect();
1165+
LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo);
11641166
RandomInit();
11651167
ECC_Start();
11661168
globalVerifyHandle.reset(new ECCVerifyHandle());

src/test/test_bitcoin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "chainparams.h"
88
#include "consensus/consensus.h"
99
#include "consensus/validation.h"
10+
#include "crypto/sha256.h"
1011
#include "fs.h"
1112
#include "key.h"
1213
#include "validation.h"
@@ -33,6 +34,7 @@ extern void noui_connect();
3334

3435
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
3536
{
37+
SHA256AutoDetect();
3638
RandomInit();
3739
ECC_Start();
3840
SetupEnvironment();

0 commit comments

Comments
 (0)