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.
251}252}253}255void TestChaCha20Poly1305(const std::string& plain_hex, const std::string& aad_hex, const std::string& key_hex, ChaCha20::Nonce96 nonce, const std::string& cipher_hex)256{257auto plain = ParseHex<std::byte>(plain_hex);258auto aad = ParseHex<std::byte>(aad_hex);259auto key = ParseHex<std::byte>(key_hex);260auto expected_cipher = ParseHex<std::byte>(cipher_hex);262for (int i = 0; i < 10; ++i) {263// During i=0, use single-plain Encrypt/Decrypt; others use a split at prefix.264size_t prefix = i ? m_rng.randrange(plain.size() + 1) : plain.size();265// Encrypt.266std::vector<std::byte> cipher(plain.size() + AEADChaCha20Poly1305::EXPANSION);267AEADChaCha20Poly1305 aead{key};268if (i == 0) {269aead.Encrypt(plain, aad, nonce, cipher);270} else {271aead.Encrypt(std::span{plain}.first(prefix), std::span{plain}.subspan(prefix), aad, nonce, cipher);272}273BOOST_CHECK(cipher == expected_cipher);275// Decrypt.276std::vector<std::byte> decipher(cipher.size() - AEADChaCha20Poly1305::EXPANSION);277bool ret{false};278if (i == 0) {279ret = aead.Decrypt(cipher, aad, nonce, decipher);280} else {281ret = aead.Decrypt(cipher, aad, nonce, std::span{decipher}.first(prefix), std::span{decipher}.subspan(prefix));282}283BOOST_CHECK(ret);284BOOST_CHECK(decipher == plain);285}287// Test Keystream output.288std::vector<std::byte> keystream(plain.size());289AEADChaCha20Poly1305 aead{key};290aead.Keystream(nonce, keystream);291for (size_t i = 0; i < plain.size(); ++i) {292BOOST_CHECK_EQUAL(plain[i] ^ keystream[i], expected_cipher[i]);293}294}296void TestFSChaCha20Poly1305(const std::string& plain_hex, const std::string& aad_hex, const std::string& key_hex, uint64_t msg_idx, const std::string& cipher_hex)297{298auto plain = ParseHex<std::byte>(plain_hex);299auto aad = ParseHex<std::byte>(aad_hex);300auto key = ParseHex<std::byte>(key_hex);301auto expected_cipher = ParseHex<std::byte>(cipher_hex);302std::vector<std::byte> cipher(plain.size() + FSChaCha20Poly1305::EXPANSION);304for (int it = 0; it < 10; ++it) {305// During it==0 we use the single-plain Encrypt/Decrypt; others use a split at prefix.306size_t prefix = it ? m_rng.randrange(plain.size() + 1) : plain.size();307std::byte dummy_tag[FSChaCha20Poly1305::EXPANSION] = {{}};309// Do msg_idx dummy encryptions to seek to the correct packet.310FSChaCha20Poly1305 enc_aead{key, 224};311for (uint64_t i = 0; i < msg_idx; ++i) {312enc_aead.Encrypt(std::span{dummy_tag}.first(0), std::span{dummy_tag}.first(0), dummy_tag);313}315// Invoke single-plain or plain1/plain2 Encrypt.316if (it == 0) {317enc_aead.Encrypt(plain, aad, cipher);318} else {319enc_aead.Encrypt(std::span{plain}.first(prefix), std::span{plain}.subspan(prefix), aad, cipher);320}321BOOST_CHECK(cipher == expected_cipher);323// Do msg_idx dummy decryptions to seek to the correct packet.324FSChaCha20Poly1305 dec_aead{key, 224};325for (uint64_t i = 0; i < msg_idx; ++i) {326dec_aead.Decrypt(dummy_tag, std::span{dummy_tag}.first(0), std::span{dummy_tag}.first(0));327}329// Invoke single-plain or plain1/plain2 Decrypt.330std::vector<std::byte> decipher(cipher.size() - AEADChaCha20Poly1305::EXPANSION);331bool ret{false};332if (it == 0) {333ret = dec_aead.Decrypt(cipher, aad, decipher);334} else {335ret = dec_aead.Decrypt(cipher, aad, std::span{decipher}.first(prefix), std::span{decipher}.subspan(prefix));336}337BOOST_CHECK(ret);338BOOST_CHECK(decipher == plain);339}340}342void TestHKDF_SHA256_32(const std::string &ikm_hex, const std::string &salt_hex, const std::string &info_hex, const std::string &okm_check_hex) {343std::vector<unsigned char> initial_key_material = ParseHex(ikm_hex);344std::vector<unsigned char> salt = ParseHex(salt_hex);345std::vector<unsigned char> info = ParseHex(info_hex);348// our implementation only supports strings for the "info" and "salt", stringify them349std::string salt_stringified(reinterpret_cast<char*>(salt.data()), salt.size());350std::string info_stringified(reinterpret_cast<char*>(info.data()), info.size());352CHKDF_HMAC_SHA256_L32 hkdf32(initial_key_material.data(), initial_key_material.size(), salt_stringified);353unsigned char out[32];354hkdf32.Expand32(info_stringified, out);355BOOST_CHECK(HexStr(out) == okm_check_hex);356}358void TestSHA3_256(const std::string& input, const std::string& output);359}; // struct CryptoTests360} // namespace crypto_tests362static std::string LongTestString()363{364std::string ret;365for (int i = 0; i < 200000; i++) {366ret += (char)(i);367ret += (char)(i >> 4);368ret += (char)(i >> 8);369ret += (char)(i >> 12);370ret += (char)(i >> 16);371}372return ret;373}375const std::string test1 = LongTestString();377BOOST_FIXTURE_TEST_SUITE(crypto_tests, CryptoTest)379BOOST_AUTO_TEST_CASE(ripemd160_testvectors) {380TestRIPEMD160("", "9c1185a5c5e9fc54612808977ee8f548b2258d31");381TestRIPEMD160("abc", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc");382TestRIPEMD160("message digest", "5d0689ef49d2fae572b881b123a85ffa21595f36");383TestRIPEMD160("secure hash algorithm", "20397528223b6a5f4cbc2808aba0464e645544f9");384TestRIPEMD160("RIPEMD160 is considered to be safe", "a7d78608c7af8a8e728778e81576870734122b66");385TestRIPEMD160("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",386"12a053384a9c0c88e405a06c27dcf49ada62eb2b");387TestRIPEMD160("For this sample, this 63-byte string will be used as input data",388"de90dbfee14b63fb5abf27c2ad4a82aaa5f27a11");389TestRIPEMD160("This is exactly 64 bytes long, not counting the terminating byte",390"eda31d51d3a623b81e19eb02e24ff65d27d67b37");391TestRIPEMD160(std::string(1000000, 'a'), "52783243c1697bdbe16d37f97f68f08325dc1528");392TestRIPEMD160(test1, "464243587bd146ea835cdf57bdae582f25ec45f1");393}395BOOST_AUTO_TEST_CASE(sha1_testvectors) {396TestSHA1("", "da39a3ee5e6b4b0d3255bfef95601890afd80709");397TestSHA1("abc", "a9993e364706816aba3e25717850c26c9cd0d89d");398TestSHA1("message digest", "c12252ceda8be8994d5fa0290a47231c1d16aae3");399TestSHA1("secure hash algorithm", "d4d6d2f0ebe317513bbd8d967d89bac5819c2f60");400TestSHA1("SHA1 is considered to be safe", "f2b6650569ad3a8720348dd6ea6c497dee3a842a");401TestSHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",402"84983e441c3bd26ebaae4aa1f95129e5e54670f1");403TestSHA1("For this sample, this 63-byte string will be used as input data",404"4f0ea5cd0585a23d028abdc1a6684e5a8094dc49");405TestSHA1("This is exactly 64 bytes long, not counting the terminating byte",406"fb679f23e7d1ce053313e66e127ab1b444397057");407TestSHA1(std::string(1000000, 'a'), "34aa973cd4c4daa4f61eeb2bdbad27316534016f");408TestSHA1(test1, "b7755760681cbfd971451668f32af5774f4656b5");409}411BOOST_AUTO_TEST_CASE(sha256_testvectors) {412TestSHA256("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");413TestSHA256("abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");414TestSHA256("message digest",415"f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650");416TestSHA256("secure hash algorithm",417"f30ceb2bb2829e79e4ca9753d35a8ecc00262d164cc077080295381cbd643f0d");418TestSHA256("SHA256 is considered to be safe",419"6819d915c73f4d1e77e4e1b52d1fa0f9cf9beaead3939f15874bd988e2a23630");420TestSHA256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",421"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");422TestSHA256("For this sample, this 63-byte string will be used as input data",423"f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342");424TestSHA256("This is exactly 64 bytes long, not counting the terminating byte",425"ab64eff7e88e2e46165e29f2bce41826bd4c7b3552f6b382a9e7d3af47c245f8");426TestSHA256("As Bitcoin relies on 80 byte header hashes, we want to have an example for that.",427"7406e8de7d6e4fffc573daef05aefb8806e7790f55eab5576f31349743cca743");428TestSHA256(std::string(1000000, 'a'),429"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");430TestSHA256(test1, "a316d55510b49662420f49d145d42fb83f31ef8dc016aa4e32df049991a91e26");431}433BOOST_AUTO_TEST_CASE(sha512_testvectors) {434TestSHA512("",435"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"436"47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");437TestSHA512("abc",438"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"439"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");440TestSHA512("message digest",441"107dbf389d9e9f71a3a95f6c055b9251bc5268c2be16d6c13492ea45b0199f33"442"09e16455ab1e96118e8a905d5597b72038ddb372a89826046de66687bb420e7c");443TestSHA512("secure hash algorithm",444"7746d91f3de30c68cec0dd693120a7e8b04d8073cb699bdce1a3f64127bca7a3"445"d5db502e814bb63c063a7a5043b2df87c61133395f4ad1edca7fcf4b30c3236e");446TestSHA512("SHA512 is considered to be safe",447"099e6468d889e1c79092a89ae925a9499b5408e01b66cb5b0a3bd0dfa51a9964"448"6b4a3901caab1318189f74cd8cf2e941829012f2449df52067d3dd5b978456c2");449TestSHA512("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",450"204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c335"451"96fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445");452TestSHA512("For this sample, this 63-byte string will be used as input data",453"b3de4afbc516d2478fe9b518d063bda6c8dd65fc38402dd81d1eb7364e72fb6e"454"6663cf6d2771c8f5a6da09601712fb3d2a36c6ffea3e28b0818b05b0a8660766");455TestSHA512("This is exactly 64 bytes long, not counting the terminating byte",456"70aefeaa0e7ac4f8fe17532d7185a289bee3b428d950c14fa8b713ca09814a38"457"7d245870e007a80ad97c369d193e41701aa07f3221d15f0e65a1ff970cedf030");458TestSHA512("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"459"ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",460"8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"461"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909");462TestSHA512(std::string(1000000, 'a'),463"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"464"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b");465TestSHA512(test1,466"40cac46c147e6131c5193dd5f34e9d8bb4951395f27b08c558c65ff4ba2de594"467"37de8c3ef5459d76a52cedc02dc499a3c9ed9dedbfb3281afd9653b8a112fafc");468}470BOOST_AUTO_TEST_CASE(hmac_sha256_testvectors) {471// test cases 1, 2, 3, 4, 6 and 7 of RFC 4231472TestHMACSHA256("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",473"4869205468657265",474"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7");475TestHMACSHA256("4a656665",476"7768617420646f2079612077616e7420666f72206e6f7468696e673f",477"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843");478TestHMACSHA256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",479"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"480"dddddddddddddddddddddddddddddddddddd",481"773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe");482TestHMACSHA256("0102030405060708090a0b0c0d0e0f10111213141516171819",483"cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"484"cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",485"82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b");486TestHMACSHA256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"487"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"488"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"489"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"490"aaaaaa",491"54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a"492"65204b6579202d2048617368204b6579204669727374",493"60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54");494TestHMACSHA256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"495"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"496"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"497"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"498"aaaaaa",499"5468697320697320612074657374207573696e672061206c6172676572207468"500"616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074"
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.