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

<ul>
<li><code>sha256_sse4.cpp</code></li>
<li><code>sha256_sse41.cpp</code></li>
<li><code>sha256_avx2.cpp</code></li>
<li><code>sha256_shani.cpp</code></li>
</ul>

<p>contain SHA256 transform functions (used in <code>SHA256.Write</code>), 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.</p>

<p><code>sha256.cpp</code> 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.</p>

<p><code>hash.h</code> favors polymorphism via C++ templates.</p>

<p>Examples:</p>

<ul>
<li><p><a href="https://github.com/bitcoin/bitcoin/blob/78dae8caccd82cfbfd76557f1fb7d7557c7b5edb/src/script/interpreter.cpp#L894" rel="nofollow noreferrer">hash <code>std::vector&lt;something&gt; vch</code> to <code>std::vector&lt;something&gt; vchHash</code></a></p></li>
<li><p><a href="https://github.com/bitcoin/bitcoin/blob/de5af41e3567dd9660b3e9734113dccbce394207/src/script/standard.cpp#L28" rel="nofollow noreferrer">hash <code>std::vector&lt;something&gt; in</code></a> to <a href="https://github.com/bitcoin/bitcoin/blob/de5af41e3567dd9660b3e9734113dccbce394207/src/script/standard.h#L92" rel="nofollow noreferrer"><code>std::vector&lt;something&gt; WitnessV0ScriptHash</code></a></p></li>
<li><p><a href="https://github.com/bitcoin/bitcoin/blob/bccb4d29a8080bf1ecda1fc235415a11d903a680/src/script/sigcache.cpp#L41" rel="nofollow noreferrer">hash <code>uint8_t * nonce.begin()</code>, <code>uint8_t *  hash.begin()</code>, <code>unsigned char pubkey[]</code>, <code>std::vector&lt;unsigned char&gt; vchSig</code> to <code>std::vector&lt;unsigned char&gt; entry</code></a></p></li>
<li><p><a href="https://github.com/bitcoin/bitcoin/blob/78295e97b8d38ecf6628b5d8ce1efd3900a5c345/src/bench/block_assemble.cpp#L23" rel="nofollow noreferrer">hash <code>op_true</code> to <code>witness_program</code></a></p></li>
</ul>

CC BY-SA 4.0
2019-06-04T10:06:28.253Z
2019-06-04T12:43:18.930Z
2019-06-04T12:43:18.930Z
MCCCS
<p>I was looking at Bitcoin Core code to use its SHA256 code but am confused on where I should be looking at.</p>

<p>In bitcoin/src/crypto/sha256.h there is CSHA256 </p>

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

<p>And then there is bitcoin/src/hash.h</p>

<p>Explanations and guidance would be greatly appreciated. </p>

<bitcoin-core><cryptography><sha256>
Confused on using SHA256 code from Bitcoin Core
kcalvinalvin
# Confused on using SHA256 code from Bitcoin Core

(Asked by: username_0 on 2019-06-04)

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.

**Tags:** bitcoin-core, cryptography, sha256


---

## 1 answers

### Answer by username_1 on 2019-06-04. Score: 4 | Accepted answer

`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:

* [hash `std::vector vch` to `std::vector vchHash`](https://github.com/bitcoin/bitcoin/blob/78dae8caccd82cfbfd76557f1fb7d7557c7b5edb/src/script/interpreter.cpp#L894)
* [hash `std::vector in`](https://github.com/bitcoin/bitcoin/blob/de5af41e3567dd9660b3e9734113dccbce394207/src/script/standard.cpp#L28) to [`std::vector WitnessV0ScriptHash`](https://github.com/bitcoin/bitcoin/blob/de5af41e3567dd9660b3e9734113dccbce394207/src/script/standard.h#L92)
* [hash `uint8_t * nonce.begin()`, `uint8_t * hash.begin()`, `unsigned char pubkey[]`, `std::vector vchSig` to `std::vector entry`](https://github.com/bitcoin/bitcoin/blob/bccb4d29a8080bf1ecda1fc235415a11d903a680/src/script/sigcache.cpp#L41)
* [hash `op_true` to `witness_program`](https://github.com/bitcoin/bitcoin/blob/78295e97b8d38ecf6628b5d8ce1efd3900a5c345/src/bench/block_assemble.cpp#L23)


---

