Browse Source

Add SHA256 dispatcher

0.15
Pieter Wuille 7 years ago
parent
commit
2991c91d88
  1. 2
      src/bench/bench_bitcoin.cpp
  2. 13
      src/crypto/sha256.cpp
  3. 6
      src/crypto/sha256.h
  4. 2
      src/init.cpp
  5. 2
      src/test/test_bitcoin.cpp

2
src/bench/bench_bitcoin.cpp

@ -4,6 +4,7 @@
#include "bench.h" #include "bench.h"
#include "crypto/sha256.h"
#include "key.h" #include "key.h"
#include "validation.h" #include "validation.h"
#include "util.h" #include "util.h"
@ -12,6 +13,7 @@
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
SHA256AutoDetect();
RandomInit(); RandomInit();
ECC_Start(); ECC_Start();
SetupEnvironment(); SetupEnvironment();

13
src/crypto/sha256.cpp

@ -8,6 +8,8 @@
#include <string.h> #include <string.h>
#include <atomic>
// Internal implementation code. // Internal implementation code.
namespace namespace
{ {
@ -131,8 +133,15 @@ void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks)
} }
} // namespace sha256 } // namespace sha256
void (*Transform)(uint32_t*, const unsigned char*, size_t) = sha256::Transform;
} // namespace } // namespace
std::string SHA256AutoDetect()
{
return "standard";
}
////// SHA-256 ////// SHA-256
@ -150,12 +159,12 @@ CSHA256& CSHA256::Write(const unsigned char* data, size_t len)
memcpy(buf + bufsize, data, 64 - bufsize); memcpy(buf + bufsize, data, 64 - bufsize);
bytes += 64 - bufsize; bytes += 64 - bufsize;
data += 64 - bufsize; data += 64 - bufsize;
sha256::Transform(s, buf, 1); Transform(s, buf, 1);
bufsize = 0; bufsize = 0;
} }
if (end - data >= 64) { if (end - data >= 64) {
size_t blocks = (end - data) / 64; size_t blocks = (end - data) / 64;
sha256::Transform(s, data, blocks); Transform(s, data, blocks);
data += 64 * blocks; data += 64 * blocks;
bytes += 64 * blocks; bytes += 64 * blocks;
} }

6
src/crypto/sha256.h

@ -7,6 +7,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
/** A hasher class for SHA-256. */ /** A hasher class for SHA-256. */
class CSHA256 class CSHA256
@ -25,4 +26,9 @@ public:
CSHA256& Reset(); CSHA256& Reset();
}; };
/** Autodetect the best available SHA256 implementation.
* Returns the name of the implementation.
*/
std::string SHA256AutoDetect();
#endif // BITCOIN_CRYPTO_SHA256_H #endif // BITCOIN_CRYPTO_SHA256_H

2
src/init.cpp

@ -1161,6 +1161,8 @@ bool AppInitSanityChecks()
// ********************************************************* Step 4: sanity checks // ********************************************************* Step 4: sanity checks
// Initialize elliptic curve code // Initialize elliptic curve code
std::string sha256_algo = SHA256AutoDetect();
LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo);
RandomInit(); RandomInit();
ECC_Start(); ECC_Start();
globalVerifyHandle.reset(new ECCVerifyHandle()); globalVerifyHandle.reset(new ECCVerifyHandle());

2
src/test/test_bitcoin.cpp

@ -7,6 +7,7 @@
#include "chainparams.h" #include "chainparams.h"
#include "consensus/consensus.h" #include "consensus/consensus.h"
#include "consensus/validation.h" #include "consensus/validation.h"
#include "crypto/sha256.h"
#include "fs.h" #include "fs.h"
#include "key.h" #include "key.h"
#include "validation.h" #include "validation.h"
@ -33,6 +34,7 @@ extern void noui_connect();
BasicTestingSetup::BasicTestingSetup(const std::string& chainName) BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
{ {
SHA256AutoDetect();
RandomInit(); RandomInit();
ECC_Start(); ECC_Start();
SetupEnvironment(); SetupEnvironment();

Loading…
Cancel
Save