Inside Bitcoin
Opening the selected file…
Inside Bitcoin
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) 2020-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#include <crypto/chacha20.h>6#include <test/fuzz/FuzzedDataProvider.h>7#include <test/fuzz/fuzz.h>8#include <test/fuzz/util.h>10#include <cstdint>11#include <vector>13/*14From https://cr.yp.to/chacha.html15chacha-merged.c version 2008011816D. J. Bernstein17Public domain.18*/20typedef unsigned int u32;21typedef unsigned char u8;23#define U8C(v) (v##U)24#define U32C(v) (v##U)26#define U8V(v) ((u8)(v)&U8C(0xFF))27#define U32V(v) ((u32)(v)&U32C(0xFFFFFFFF))29#define ROTL32(v, n) (U32V((v) << (n)) | ((v) >> (32 - (n))))31#define U8TO32_LITTLE(p) \32(((u32)((p)[0])) | ((u32)((p)[1]) << 8) | ((u32)((p)[2]) << 16) | \33((u32)((p)[3]) << 24))35#define U32TO8_LITTLE(p, v) \36do { \37(p)[0] = U8V((v)); \38(p)[1] = U8V((v) >> 8); \39(p)[2] = U8V((v) >> 16); \40(p)[3] = U8V((v) >> 24); \41} while (0)43/* ------------------------------------------------------------------------- */44/* Data structures */46typedef struct47{48u32 input[16];49} ECRYPT_ctx;51/* ------------------------------------------------------------------------- */52/* Mandatory functions */54void ECRYPT_keysetup(55ECRYPT_ctx* ctx,56const u8* key,57u32 keysize, /* Key size in bits. */58u32 ivsize); /* IV size in bits. */60void ECRYPT_ivsetup(61ECRYPT_ctx* ctx,62const u8* iv);64void ECRYPT_encrypt_bytes(65ECRYPT_ctx* ctx,66const u8* plaintext,67u8* ciphertext,68u32 msglen); /* Message length in bytes. */70/* ------------------------------------------------------------------------- */72/* Optional features */74void ECRYPT_keystream_bytes(75ECRYPT_ctx* ctx,76u8* keystream,77u32 length); /* Length of keystream in bytes. */79/* ------------------------------------------------------------------------- */81#define ROTATE(v, c) (ROTL32(v, c))82#define XOR(v, w) ((v) ^ (w))83#define PLUS(v, w) (U32V((v) + (w)))84#define PLUSONE(v) (PLUS((v), 1))86#define QUARTERROUND(a, b, c, d) \87a = PLUS(a, b); d = ROTATE(XOR(d, a), 16); \88c = PLUS(c, d); b = ROTATE(XOR(b, c), 12); \89a = PLUS(a, b); d = ROTATE(XOR(d, a), 8); \90c = PLUS(c, d); b = ROTATE(XOR(b, c), 7);92static const char sigma[] = "expand 32-byte k";93static const char tau[] = "expand 16-byte k";95void ECRYPT_keysetup(ECRYPT_ctx* x, const u8* k, u32 kbits, u32 ivbits)96{97const char* constants;99x->input[4] = U8TO32_LITTLE(k + 0);100x->input[5] = U8TO32_LITTLE(k + 4);101x->input[6] = U8TO32_LITTLE(k + 8);102x->input[7] = U8TO32_LITTLE(k + 12);103if (kbits == 256) { /* recommended */104k += 16;105constants = sigma;106} else { /* kbits == 128 */107constants = tau;108}109x->input[8] = U8TO32_LITTLE(k + 0);110x->input[9] = U8TO32_LITTLE(k + 4);111x->input[10] = U8TO32_LITTLE(k + 8);112x->input[11] = U8TO32_LITTLE(k + 12);113x->input[0] = U8TO32_LITTLE(constants + 0);114x->input[1] = U8TO32_LITTLE(constants + 4);115x->input[2] = U8TO32_LITTLE(constants + 8);116x->input[3] = U8TO32_LITTLE(constants + 12);117}119void ECRYPT_ivsetup(ECRYPT_ctx* x, const u8* iv)120{121x->input[12] = 0;122x->input[13] = 0;123x->input[14] = U8TO32_LITTLE(iv + 0);124x->input[15] = U8TO32_LITTLE(iv + 4);125}127void ECRYPT_encrypt_bytes(ECRYPT_ctx* x, const u8* m, u8* c, u32 bytes)128{129u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;130u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;131u8* ctarget = nullptr;132u8 tmp[64];133uint32_t i;135if (!bytes) return;137j0 = x->input[0];138j1 = x->input[1];139j2 = x->input[2];140j3 = x->input[3];141j4 = x->input[4];142j5 = x->input[5];143j6 = x->input[6];144j7 = x->input[7];145j8 = x->input[8];146j9 = x->input[9];147j10 = x->input[10];148j11 = x->input[11];149j12 = x->input[12];150j13 = x->input[13];151j14 = x->input[14];152j15 = x->input[15];154for (;;) {155if (bytes < 64) {156for (i = 0; i < bytes; ++i)157tmp[i] = m[i];158m = tmp;159ctarget = c;160c = tmp;161}162x0 = j0;163x1 = j1;164x2 = j2;165x3 = j3;166x4 = j4;167x5 = j5;168x6 = j6;169x7 = j7;170x8 = j8;171x9 = j9;172x10 = j10;173x11 = j11;174x12 = j12;175x13 = j13;176x14 = j14;177x15 = j15;178for (i = 20; i > 0; i -= 2) {179QUARTERROUND(x0, x4, x8, x12)180QUARTERROUND(x1, x5, x9, x13)181QUARTERROUND(x2, x6, x10, x14)182QUARTERROUND(x3, x7, x11, x15)183QUARTERROUND(x0, x5, x10, x15)184QUARTERROUND(x1, x6, x11, x12)185QUARTERROUND(x2, x7, x8, x13)186QUARTERROUND(x3, x4, x9, x14)187}188x0 = PLUS(x0, j0);189x1 = PLUS(x1, j1);190x2 = PLUS(x2, j2);191x3 = PLUS(x3, j3);192x4 = PLUS(x4, j4);193x5 = PLUS(x5, j5);194x6 = PLUS(x6, j6);195x7 = PLUS(x7, j7);196x8 = PLUS(x8, j8);197x9 = PLUS(x9, j9);198x10 = PLUS(x10, j10);199x11 = PLUS(x11, j11);200x12 = PLUS(x12, j12);201x13 = PLUS(x13, j13);202x14 = PLUS(x14, j14);203x15 = PLUS(x15, j15);205x0 = XOR(x0, U8TO32_LITTLE(m + 0));206x1 = XOR(x1, U8TO32_LITTLE(m + 4));207x2 = XOR(x2, U8TO32_LITTLE(m + 8));208x3 = XOR(x3, U8TO32_LITTLE(m + 12));209x4 = XOR(x4, U8TO32_LITTLE(m + 16));210x5 = XOR(x5, U8TO32_LITTLE(m + 20));211x6 = XOR(x6, U8TO32_LITTLE(m + 24));212x7 = XOR(x7, U8TO32_LITTLE(m + 28));213x8 = XOR(x8, U8TO32_LITTLE(m + 32));214x9 = XOR(x9, U8TO32_LITTLE(m + 36));215x10 = XOR(x10, U8TO32_LITTLE(m + 40));216x11 = XOR(x11, U8TO32_LITTLE(m + 44));217x12 = XOR(x12, U8TO32_LITTLE(m + 48));218x13 = XOR(x13, U8TO32_LITTLE(m + 52));219x14 = XOR(x14, U8TO32_LITTLE(m + 56));220x15 = XOR(x15, U8TO32_LITTLE(m + 60));222j12 = PLUSONE(j12);223if (!j12) {224j13 = PLUSONE(j13);225/* stopping at 2^70 bytes per nonce is user's responsibility */226}228U32TO8_LITTLE(c + 0, x0);229U32TO8_LITTLE(c + 4, x1);230U32TO8_LITTLE(c + 8, x2);231U32TO8_LITTLE(c + 12, x3);232U32TO8_LITTLE(c + 16, x4);233U32TO8_LITTLE(c + 20, x5);234U32TO8_LITTLE(c + 24, x6);235U32TO8_LITTLE(c + 28, x7);236U32TO8_LITTLE(c + 32, x8);237U32TO8_LITTLE(c + 36, x9);238U32TO8_LITTLE(c + 40, x10);239U32TO8_LITTLE(c + 44, x11);240U32TO8_LITTLE(c + 48, x12);241U32TO8_LITTLE(c + 52, x13);242U32TO8_LITTLE(c + 56, x14);243U32TO8_LITTLE(c + 60, x15);245if (bytes <= 64) {246if (bytes < 64) {247for (i = 0; i < bytes; ++i)248ctarget[i] = c[i];249}250x->input[12] = j12;
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.