Confused on using SHA256 code from Bitcoin Core


I was looking at Bitcoin Core code to use its SHA256 code but am confused on where I should be looking at.

In bitcoin/src/crypto/sha256.h there is CSHA256

and in bitcoin/src/crypto/sha256.cpp there are namespaces named sha256_sse4, sha256_shani.

And then there is bitcoin/src/hash.h

Explanations and guidance would be greatly appreciated.


Posted 2019-06-04T09:35:02.260

Reputation: 348



sha256.h has the exported (visible from outside) methods and functions. To compute SHA256, you should #include that.

  • sha256_sse4.cpp
  • sha256_sse41.cpp
  • sha256_avx2.cpp
  • sha256_shani.cpp

contain SHA256 transform functions (used in SHA256.Write), each specialized for different processors with different instruction sets. Above is the list in order, most of the computers support SSE4, but only a few support SHA-NI instruction set, which is the fastest way a new processor can calculate SHA256.

sha256.cpp contains other methods that aren't a bottleneck, functions that check which instruction sets are supported, tests, as well as a pure-C++ transform function for older and non-x86 computers.

hash.h favors polymorphism via C++ templates.



Posted 2019-06-04T09:35:02.260

Reputation: 8 689

Can you give an example code? – kcalvinalvin – 2019-06-04T10:36:53.827

@KCalvin edited. – MCCCS – 2019-06-04T12:45:45.063