0

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.

3

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.

Examples:

  • Can you give an example code? – kcalvinalvin Jun 4 at 10:36
  • @KCalvin edited. – MCCCS Jun 4 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.