Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2017-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifdef ENABLE_AVX27#include <cstdint>8#include <immintrin.h>10#include <attributes.h>11#include <crypto/common.h>13namespace sha256d64_avx2 {14namespace {16__m256i inline K(uint32_t x) { return _mm256_set1_epi32(x); }18__m256i inline Add(__m256i x, __m256i y) { return _mm256_add_epi32(x, y); }19__m256i inline Add(__m256i x, __m256i y, __m256i z) { return Add(Add(x, y), z); }20__m256i inline Add(__m256i x, __m256i y, __m256i z, __m256i w) { return Add(Add(x, y), Add(z, w)); }21__m256i inline Add(__m256i x, __m256i y, __m256i z, __m256i w, __m256i v) { return Add(Add(x, y, z), Add(w, v)); }22__m256i inline Inc(__m256i& x, __m256i y) { x = Add(x, y); return x; }23__m256i inline Inc(__m256i& x, __m256i y, __m256i z) { x = Add(x, y, z); return x; }24__m256i inline Inc(__m256i& x, __m256i y, __m256i z, __m256i w) { x = Add(x, y, z, w); return x; }25__m256i inline Xor(__m256i x, __m256i y) { return _mm256_xor_si256(x, y); }26__m256i inline Xor(__m256i x, __m256i y, __m256i z) { return Xor(Xor(x, y), z); }27__m256i inline Or(__m256i x, __m256i y) { return _mm256_or_si256(x, y); }28__m256i inline And(__m256i x, __m256i y) { return _mm256_and_si256(x, y); }29__m256i inline ShR(__m256i x, int n) { return _mm256_srli_epi32(x, n); }30__m256i inline ShL(__m256i x, int n) { return _mm256_slli_epi32(x, n); }32__m256i inline Ch(__m256i x, __m256i y, __m256i z) { return Xor(z, And(x, Xor(y, z))); }33__m256i inline Maj(__m256i x, __m256i y, __m256i z) { return Or(And(x, y), And(z, Or(x, y))); }34__m256i inline Sigma0(__m256i x) { return Xor(Or(ShR(x, 2), ShL(x, 30)), Or(ShR(x, 13), ShL(x, 19)), Or(ShR(x, 22), ShL(x, 10))); }35__m256i inline Sigma1(__m256i x) { return Xor(Or(ShR(x, 6), ShL(x, 26)), Or(ShR(x, 11), ShL(x, 21)), Or(ShR(x, 25), ShL(x, 7))); }36__m256i inline sigma0(__m256i x) { return Xor(Or(ShR(x, 7), ShL(x, 25)), Or(ShR(x, 18), ShL(x, 14)), ShR(x, 3)); }37__m256i inline sigma1(__m256i x) { return Xor(Or(ShR(x, 17), ShL(x, 15)), Or(ShR(x, 19), ShL(x, 13)), ShR(x, 10)); }39/** One round of SHA-256. */40void ALWAYS_INLINE Round(__m256i a, __m256i b, __m256i c, __m256i& d, __m256i e, __m256i f, __m256i g, __m256i& h, __m256i k)41{42__m256i t1 = Add(h, Sigma1(e), Ch(e, f, g), k);43__m256i t2 = Add(Sigma0(a), Maj(a, b, c));44d = Add(d, t1);45h = Add(t1, t2);46}48__m256i inline Read8(const unsigned char* chunk, int offset) {49__m256i ret = _mm256_set_epi32(50ReadLE32(chunk + 0 + offset),51ReadLE32(chunk + 64 + offset),52ReadLE32(chunk + 128 + offset),53ReadLE32(chunk + 192 + offset),54ReadLE32(chunk + 256 + offset),55ReadLE32(chunk + 320 + offset),56ReadLE32(chunk + 384 + offset),57ReadLE32(chunk + 448 + offset)58);59return _mm256_shuffle_epi8(ret, _mm256_set_epi32(0x0C0D0E0FUL, 0x08090A0BUL, 0x04050607UL, 0x00010203UL, 0x0C0D0E0FUL, 0x08090A0BUL, 0x04050607UL, 0x00010203UL));60}62void inline Write8(unsigned char* out, int offset, __m256i v) {63v = _mm256_shuffle_epi8(v, _mm256_set_epi32(0x0C0D0E0FUL, 0x08090A0BUL, 0x04050607UL, 0x00010203UL, 0x0C0D0E0FUL, 0x08090A0BUL, 0x04050607UL, 0x00010203UL));64WriteLE32(out + 0 + offset, _mm256_extract_epi32(v, 7));65WriteLE32(out + 32 + offset, _mm256_extract_epi32(v, 6));66WriteLE32(out + 64 + offset, _mm256_extract_epi32(v, 5));67WriteLE32(out + 96 + offset, _mm256_extract_epi32(v, 4));68WriteLE32(out + 128 + offset, _mm256_extract_epi32(v, 3));69WriteLE32(out + 160 + offset, _mm256_extract_epi32(v, 2));70WriteLE32(out + 192 + offset, _mm256_extract_epi32(v, 1));71WriteLE32(out + 224 + offset, _mm256_extract_epi32(v, 0));72}74}76void Transform_8way(unsigned char* out, const unsigned char* in)77{78// Transform 179__m256i a = K(0x6a09e667ul);80__m256i b = K(0xbb67ae85ul);81__m256i c = K(0x3c6ef372ul);82__m256i d = K(0xa54ff53aul);83__m256i e = K(0x510e527ful);84__m256i f = K(0x9b05688cul);85__m256i g = K(0x1f83d9abul);86__m256i h = K(0x5be0cd19ul);88__m256i w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15;90Round(a, b, c, d, e, f, g, h, Add(K(0x428a2f98ul), w0 = Read8(in, 0)));91Round(h, a, b, c, d, e, f, g, Add(K(0x71374491ul), w1 = Read8(in, 4)));92Round(g, h, a, b, c, d, e, f, Add(K(0xb5c0fbcful), w2 = Read8(in, 8)));93Round(f, g, h, a, b, c, d, e, Add(K(0xe9b5dba5ul), w3 = Read8(in, 12)));94Round(e, f, g, h, a, b, c, d, Add(K(0x3956c25bul), w4 = Read8(in, 16)));95Round(d, e, f, g, h, a, b, c, Add(K(0x59f111f1ul), w5 = Read8(in, 20)));96Round(c, d, e, f, g, h, a, b, Add(K(0x923f82a4ul), w6 = Read8(in, 24)));97Round(b, c, d, e, f, g, h, a, Add(K(0xab1c5ed5ul), w7 = Read8(in, 28)));98Round(a, b, c, d, e, f, g, h, Add(K(0xd807aa98ul), w8 = Read8(in, 32)));99Round(h, a, b, c, d, e, f, g, Add(K(0x12835b01ul), w9 = Read8(in, 36)));100Round(g, h, a, b, c, d, e, f, Add(K(0x243185beul), w10 = Read8(in, 40)));101Round(f, g, h, a, b, c, d, e, Add(K(0x550c7dc3ul), w11 = Read8(in, 44)));102Round(e, f, g, h, a, b, c, d, Add(K(0x72be5d74ul), w12 = Read8(in, 48)));103Round(d, e, f, g, h, a, b, c, Add(K(0x80deb1feul), w13 = Read8(in, 52)));104Round(c, d, e, f, g, h, a, b, Add(K(0x9bdc06a7ul), w14 = Read8(in, 56)));105Round(b, c, d, e, f, g, h, a, Add(K(0xc19bf174ul), w15 = Read8(in, 60)));106Round(a, b, c, d, e, f, g, h, Add(K(0xe49b69c1ul), Inc(w0, sigma1(w14), w9, sigma0(w1))));107Round(h, a, b, c, d, e, f, g, Add(K(0xefbe4786ul), Inc(w1, sigma1(w15), w10, sigma0(w2))));108Round(g, h, a, b, c, d, e, f, Add(K(0x0fc19dc6ul), Inc(w2, sigma1(w0), w11, sigma0(w3))));109Round(f, g, h, a, b, c, d, e, Add(K(0x240ca1ccul), Inc(w3, sigma1(w1), w12, sigma0(w4))));110Round(e, f, g, h, a, b, c, d, Add(K(0x2de92c6ful), Inc(w4, sigma1(w2), w13, sigma0(w5))));111Round(d, e, f, g, h, a, b, c, Add(K(0x4a7484aaul), Inc(w5, sigma1(w3), w14, sigma0(w6))));112Round(c, d, e, f, g, h, a, b, Add(K(0x5cb0a9dcul), Inc(w6, sigma1(w4), w15, sigma0(w7))));113Round(b, c, d, e, f, g, h, a, Add(K(0x76f988daul), Inc(w7, sigma1(w5), w0, sigma0(w8))));114Round(a, b, c, d, e, f, g, h, Add(K(0x983e5152ul), Inc(w8, sigma1(w6), w1, sigma0(w9))));115Round(h, a, b, c, d, e, f, g, Add(K(0xa831c66dul), Inc(w9, sigma1(w7), w2, sigma0(w10))));116Round(g, h, a, b, c, d, e, f, Add(K(0xb00327c8ul), Inc(w10, sigma1(w8), w3, sigma0(w11))));117Round(f, g, h, a, b, c, d, e, Add(K(0xbf597fc7ul), Inc(w11, sigma1(w9), w4, sigma0(w12))));118Round(e, f, g, h, a, b, c, d, Add(K(0xc6e00bf3ul), Inc(w12, sigma1(w10), w5, sigma0(w13))));119Round(d, e, f, g, h, a, b, c, Add(K(0xd5a79147ul), Inc(w13, sigma1(w11), w6, sigma0(w14))));120Round(c, d, e, f, g, h, a, b, Add(K(0x06ca6351ul), Inc(w14, sigma1(w12), w7, sigma0(w15))));121Round(b, c, d, e, f, g, h, a, Add(K(0x14292967ul), Inc(w15, sigma1(w13), w8, sigma0(w0))));122Round(a, b, c, d, e, f, g, h, Add(K(0x27b70a85ul), Inc(w0, sigma1(w14), w9, sigma0(w1))));123Round(h, a, b, c, d, e, f, g, Add(K(0x2e1b2138ul), Inc(w1, sigma1(w15), w10, sigma0(w2))));124Round(g, h, a, b, c, d, e, f, Add(K(0x4d2c6dfcul), Inc(w2, sigma1(w0), w11, sigma0(w3))));125Round(f, g, h, a, b, c, d, e, Add(K(0x53380d13ul), Inc(w3, sigma1(w1), w12, sigma0(w4))));126Round(e, f, g, h, a, b, c, d, Add(K(0x650a7354ul), Inc(w4, sigma1(w2), w13, sigma0(w5))));127Round(d, e, f, g, h, a, b, c, Add(K(0x766a0abbul), Inc(w5, sigma1(w3), w14, sigma0(w6))));128Round(c, d, e, f, g, h, a, b, Add(K(0x81c2c92eul), Inc(w6, sigma1(w4), w15, sigma0(w7))));129Round(b, c, d, e, f, g, h, a, Add(K(0x92722c85ul), Inc(w7, sigma1(w5), w0, sigma0(w8))));130Round(a, b, c, d, e, f, g, h, Add(K(0xa2bfe8a1ul), Inc(w8, sigma1(w6), w1, sigma0(w9))));131Round(h, a, b, c, d, e, f, g, Add(K(0xa81a664bul), Inc(w9, sigma1(w7), w2, sigma0(w10))));132Round(g, h, a, b, c, d, e, f, Add(K(0xc24b8b70ul), Inc(w10, sigma1(w8), w3, sigma0(w11))));133Round(f, g, h, a, b, c, d, e, Add(K(0xc76c51a3ul), Inc(w11, sigma1(w9), w4, sigma0(w12))));134Round(e, f, g, h, a, b, c, d, Add(K(0xd192e819ul), Inc(w12, sigma1(w10), w5, sigma0(w13))));135Round(d, e, f, g, h, a, b, c, Add(K(0xd6990624ul), Inc(w13, sigma1(w11), w6, sigma0(w14))));136Round(c, d, e, f, g, h, a, b, Add(K(0xf40e3585ul), Inc(w14, sigma1(w12), w7, sigma0(w15))));137Round(b, c, d, e, f, g, h, a, Add(K(0x106aa070ul), Inc(w15, sigma1(w13), w8, sigma0(w0))));138Round(a, b, c, d, e, f, g, h, Add(K(0x19a4c116ul), Inc(w0, sigma1(w14), w9, sigma0(w1))));139Round(h, a, b, c, d, e, f, g, Add(K(0x1e376c08ul), Inc(w1, sigma1(w15), w10, sigma0(w2))));140Round(g, h, a, b, c, d, e, f, Add(K(0x2748774cul), Inc(w2, sigma1(w0), w11, sigma0(w3))));141Round(f, g, h, a, b, c, d, e, Add(K(0x34b0bcb5ul), Inc(w3, sigma1(w1), w12, sigma0(w4))));142Round(e, f, g, h, a, b, c, d, Add(K(0x391c0cb3ul), Inc(w4, sigma1(w2), w13, sigma0(w5))));143Round(d, e, f, g, h, a, b, c, Add(K(0x4ed8aa4aul), Inc(w5, sigma1(w3), w14, sigma0(w6))));144Round(c, d, e, f, g, h, a, b, Add(K(0x5b9cca4ful), Inc(w6, sigma1(w4), w15, sigma0(w7))));145Round(b, c, d, e, f, g, h, a, Add(K(0x682e6ff3ul), Inc(w7, sigma1(w5), w0, sigma0(w8))));146Round(a, b, c, d, e, f, g, h, Add(K(0x748f82eeul), Inc(w8, sigma1(w6), w1, sigma0(w9))));147Round(h, a, b, c, d, e, f, g, Add(K(0x78a5636ful), Inc(w9, sigma1(w7), w2, sigma0(w10))));148Round(g, h, a, b, c, d, e, f, Add(K(0x84c87814ul), Inc(w10, sigma1(w8), w3, sigma0(w11))));149Round(f, g, h, a, b, c, d, e, Add(K(0x8cc70208ul), Inc(w11, sigma1(w9), w4, sigma0(w12))));150Round(e, f, g, h, a, b, c, d, Add(K(0x90befffaul), Inc(w12, sigma1(w10), w5, sigma0(w13))));151Round(d, e, f, g, h, a, b, c, Add(K(0xa4506cebul), Inc(w13, sigma1(w11), w6, sigma0(w14))));152Round(c, d, e, f, g, h, a, b, Add(K(0xbef9a3f7ul), Inc(w14, sigma1(w12), w7, sigma0(w15))));153Round(b, c, d, e, f, g, h, a, Add(K(0xc67178f2ul), Inc(w15, sigma1(w13), w8, sigma0(w0))));155a = Add(a, K(0x6a09e667ul));156b = Add(b, K(0xbb67ae85ul));157c = Add(c, K(0x3c6ef372ul));158d = Add(d, K(0xa54ff53aul));159e = Add(e, K(0x510e527ful));160f = Add(f, K(0x9b05688cul));161g = Add(g, K(0x1f83d9abul));162h = Add(h, K(0x5be0cd19ul));164__m256i t0 = a, t1 = b, t2 = c, t3 = d, t4 = e, t5 = f, t6 = g, t7 = h;166// Transform 2167Round(a, b, c, d, e, f, g, h, K(0xc28a2f98ul));168Round(h, a, b, c, d, e, f, g, K(0x71374491ul));169Round(g, h, a, b, c, d, e, f, K(0xb5c0fbcful));170Round(f, g, h, a, b, c, d, e, K(0xe9b5dba5ul));171Round(e, f, g, h, a, b, c, d, K(0x3956c25bul));172Round(d, e, f, g, h, a, b, c, K(0x59f111f1ul));173Round(c, d, e, f, g, h, a, b, K(0x923f82a4ul));174Round(b, c, d, e, f, g, h, a, K(0xab1c5ed5ul));175Round(a, b, c, d, e, f, g, h, K(0xd807aa98ul));176Round(h, a, b, c, d, e, f, g, K(0x12835b01ul));177Round(g, h, a, b, c, d, e, f, K(0x243185beul));178Round(f, g, h, a, b, c, d, e, K(0x550c7dc3ul));179Round(e, f, g, h, a, b, c, d, K(0x72be5d74ul));180Round(d, e, f, g, h, a, b, c, K(0x80deb1feul));181Round(c, d, e, f, g, h, a, b, K(0x9bdc06a7ul));182Round(b, c, d, e, f, g, h, a, K(0xc19bf374ul));183Round(a, b, c, d, e, f, g, h, K(0x649b69c1ul));184Round(h, a, b, c, d, e, f, g, K(0xf0fe4786ul));185Round(g, h, a, b, c, d, e, f, K(0x0fe1edc6ul));186Round(f, g, h, a, b, c, d, e, K(0x240cf254ul));187Round(e, f, g, h, a, b, c, d, K(0x4fe9346ful));188Round(d, e, f, g, h, a, b, c, K(0x6cc984beul));189Round(c, d, e, f, g, h, a, b, K(0x61b9411eul));190Round(b, c, d, e, f, g, h, a, K(0x16f988faul));191Round(a, b, c, d, e, f, g, h, K(0xf2c65152ul));192Round(h, a, b, c, d, e, f, g, K(0xa88e5a6dul));193Round(g, h, a, b, c, d, e, f, K(0xb019fc65ul));194Round(f, g, h, a, b, c, d, e, K(0xb9d99ec7ul));195Round(e, f, g, h, a, b, c, d, K(0x9a1231c3ul));196Round(d, e, f, g, h, a, b, c, K(0xe70eeaa0ul));197Round(c, d, e, f, g, h, a, b, K(0xfdb1232bul));198Round(b, c, d, e, f, g, h, a, K(0xc7353eb0ul));199Round(a, b, c, d, e, f, g, h, K(0x3069bad5ul));200Round(h, a, b, c, d, e, f, g, K(0xcb976d5ful));201Round(g, h, a, b, c, d, e, f, K(0x5a0f118ful));202Round(f, g, h, a, b, c, d, e, K(0xdc1eeefdul));203Round(e, f, g, h, a, b, c, d, K(0x0a35b689ul));204Round(d, e, f, g, h, a, b, c, K(0xde0b7a04ul));205Round(c, d, e, f, g, h, a, b, K(0x58f4ca9dul));206Round(b, c, d, e, f, g, h, a, K(0xe15d5b16ul));207Round(a, b, c, d, e, f, g, h, K(0x007f3e86ul));208Round(h, a, b, c, d, e, f, g, K(0x37088980ul));209Round(g, h, a, b, c, d, e, f, K(0xa507ea32ul));210Round(f, g, h, a, b, c, d, e, K(0x6fab9537ul));211Round(e, f, g, h, a, b, c, d, K(0x17406110ul));212Round(d, e, f, g, h, a, b, c, K(0x0d8cd6f1ul));213Round(c, d, e, f, g, h, a, b, K(0xcdaa3b6dul));214Round(b, c, d, e, f, g, h, a, K(0xc0bbbe37ul));215Round(a, b, c, d, e, f, g, h, K(0x83613bdaul));216Round(h, a, b, c, d, e, f, g, K(0xdb48a363ul));217Round(g, h, a, b, c, d, e, f, K(0x0b02e931ul));218Round(f, g, h, a, b, c, d, e, K(0x6fd15ca7ul));219Round(e, f, g, h, a, b, c, d, K(0x521afacaul));220Round(d, e, f, g, h, a, b, c, K(0x31338431ul));221Round(c, d, e, f, g, h, a, b, K(0x6ed41a95ul));222Round(b, c, d, e, f, g, h, a, K(0x6d437890ul));223Round(a, b, c, d, e, f, g, h, K(0xc39c91f2ul));224Round(h, a, b, c, d, e, f, g, K(0x9eccabbdul));225Round(g, h, a, b, c, d, e, f, K(0xb5c9a0e6ul));226Round(f, g, h, a, b, c, d, e, K(0x532fb63cul));227Round(e, f, g, h, a, b, c, d, K(0xd2c741c6ul));228Round(d, e, f, g, h, a, b, c, K(0x07237ea3ul));229Round(c, d, e, f, g, h, a, b, K(0xa4954b68ul));230Round(b, c, d, e, f, g, h, a, K(0x4c191d76ul));232w0 = Add(t0, a);233w1 = Add(t1, b);234w2 = Add(t2, c);235w3 = Add(t3, d);236w4 = Add(t4, e);237w5 = Add(t5, f);238w6 = Add(t6, g);239w7 = Add(t7, h);241// Transform 3242a = K(0x6a09e667ul);243b = K(0xbb67ae85ul);244c = K(0x3c6ef372ul);245d = K(0xa54ff53aul);246e = K(0x510e527ful);247f = K(0x9b05688cul);248g = K(0x1f83d9abul);249h = K(0x5be0cd19ul);
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.