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.
751"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0",752{0, 0x200000000000000}, 0,753"965e3bc6f9ec7ed9560808f4d229f94b137ff275ca9b3fcbdd59deaad23310ae");755// test encryption756TestChaCha20("4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756"757"c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e"758"20776f756c642062652069742e",759"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", {0, 0x4a000000UL}, 1,760"6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f593dabcd62b3571639d"761"624e65152ab8f530c359f0861d807ca0dbf500d6a6156a38e088a22b65e52bc514d16ccf806818ce91ab77937365af90bbf74"762"a35be6b40b8eedf2785e42874d"763);765// test keystream output766TestChaCha20("", "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", {0, 0x4a000000UL}, 1,767"224f51f3401bd9e12fde276fb8631ded8c131f823d2c06e27e4fcaec9ef3cf788a3b0aa372600a92b57974cded2b9334794cb"768"a40c63e34cdea212c4cf07d41b769a6749f3f630f4122cafe28ec4dc47e26d4346d70b98c73f3e9c53ac40c5945398b6eda1a"769"832c89c167eacd901d7e2bf363");771// Test vectors from https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7772// The first one is identical to the above one from the RFC8439 A.1 vectors, but repeated here773// for completeness.774TestChaCha20("",775"0000000000000000000000000000000000000000000000000000000000000000",776{0, 0}, 0,777"76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7"778"da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586");779TestChaCha20("",780"0000000000000000000000000000000000000000000000000000000000000001",781{0, 0}, 0,782"4540f05a9f1fb296d7736e7b208e3c96eb4fe1834688d2604f450952ed432d41"783"bbe2a0b6ea7566d2a5d1e7e20d42af2c53d792b1c43fea817e9ad275ae546963");784TestChaCha20("",785"0000000000000000000000000000000000000000000000000000000000000000",786{0, 0x0100000000000000ULL}, 0,787"de9cba7bf3d69ef5e786dc63973f653a0b49e015adbff7134fcb7df137821031"788"e85a050278a7084527214f73efc7fa5b5277062eb7a0433e445f41e3");789TestChaCha20("",790"0000000000000000000000000000000000000000000000000000000000000000",791{0, 1}, 0,792"ef3fdfd6c61578fbf5cf35bd3dd33b8009631634d21e42ac33960bd138e50d32"793"111e4caf237ee53ca8ad6426194a88545ddc497a0b466e7d6bbdb0041b2f586b");794TestChaCha20("",795"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",796{0, 0x0706050403020100ULL}, 0,797"f798a189f195e66982105ffb640bb7757f579da31602fc93ec01ac56f85ac3c1"798"34a4547b733b46413042c9440049176905d3be59ea1c53f15916155c2be8241a"799"38008b9a26bc35941e2444177c8ade6689de95264986d95889fb60e84629c9bd"800"9a5acb1cc118be563eb9b3a4a472f82e09a7e778492b562ef7130e88dfe031c7"801"9db9d4f7c7a899151b9a475032b63fc385245fe054e3dd5a97a5f576fe064025"802"d3ce042c566ab2c507b138db853e3d6959660996546cc9c4a6eafdc777c040d7"803"0eaf46f76dad3979e5c5360c3317166a1c894c94a371876a94df7628fe4eaaf2"804"ccb27d5aaae0ad7ad0f9d4b6ad3b54098746d4524d38407a6deb3ab78fab78c9");806// Test overflow of 32-bit block counter, should increment the first 32-bit807// part of the nonce to retain compatibility with >256 GiB output.808// The test data was generated with an implementation that uses a 64-bit809// counter and a 64-bit initialization vector (PyCryptodome's ChaCha20 class810// with 8 bytes nonce length).811TestChaCha20("",812"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",813{0, 0xdeadbeef12345678}, 0xffffffff,814"2d292c880513397b91221c3a647cfb0765a4815894715f411e3df5e0dd0ba9df"815"fd565dea5addbdb914208fde7950f23e0385f9a727143f6a6ac51d84b1c0fb3e"816"2e3b00b63d6841a1cc6d1538b1d3a74bef1eb2f54c7b7281e36e484dba89b351"817"c8f572617e61e342879f211b0e4c515df50ea9d0771518fad96cd0baee62deb6");819// Forward secure ChaCha20820TestFSChaCha20("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",821"0000000000000000000000000000000000000000000000000000000000000000",822256,823"a93df4ef03011f3db95f60d996e1785df5de38fc39bfcb663a47bb5561928349");824TestFSChaCha20("01",825"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",8265,827"ea");828TestFSChaCha20("e93fdb5c762804b9a706816aca31e35b11d2aa3080108ef46a5b1f1508819c0a",829"8ec4c3ccdaea336bdeb245636970be01266509b33f3d2642504eaf412206207a",8304096,831"8bfaa4eacff308fdb4a94a5ff25bd9d0c1f84b77f81239f67ff39d6e1ac280c9");832}834BOOST_AUTO_TEST_CASE(chacha20_midblock)835{836auto key = "0000000000000000000000000000000000000000000000000000000000000000"_hex;837ChaCha20 c20{key};838// get one block of keystream839std::byte block[64];840c20.Keystream(block);841std::byte b1[5], b2[7], b3[52];842c20 = ChaCha20{key};843c20.Keystream(b1);844c20.Keystream(b2);845c20.Keystream(b3);847BOOST_CHECK(std::ranges::equal(std::span{block}.first(5), b1));848BOOST_CHECK(std::ranges::equal(std::span{block}.subspan(5, 7), b2));849BOOST_CHECK(std::ranges::equal(std::span{block}.last(52), b3));850}852BOOST_AUTO_TEST_CASE(poly1305_testvector)853{854// RFC 7539, section 2.5.2.855TestPoly1305("43727970746f6772617068696320466f72756d2052657365617263682047726f7570",856"85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b",857"a8061dc1305136c6c22b8baf0c0127a9");859// RFC 7539, section A.3.860TestPoly1305("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"861"000000000000000000000000000",862"0000000000000000000000000000000000000000000000000000000000000000",863"00000000000000000000000000000000");865TestPoly1305("416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e747269627"866"5746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465"867"726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686"868"520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e2022494554"869"4620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c20737461746"870"56d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c65"871"6374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207"872"768696368206172652061646472657373656420746f",873"0000000000000000000000000000000036e5f6b5c5e06070f0efca96227a863e",874"36e5f6b5c5e06070f0efca96227a863e");876TestPoly1305("416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e747269627"877"5746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465"878"726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686"879"520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e2022494554"880"4620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c20737461746"881"56d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c65"882"6374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207"883"768696368206172652061646472657373656420746f",884"36e5f6b5c5e06070f0efca96227a863e00000000000000000000000000000000",885"f3477e7cd95417af89a6b8794c310cf0");887TestPoly1305("2754776173206272696c6c69672c20616e642074686520736c6974687920746f7665730a446964206779726520616e6420676"888"96d626c6520696e2074686520776162653a0a416c6c206d696d737920776572652074686520626f726f676f7665732c0a416e"889"6420746865206d6f6d65207261746873206f757467726162652e",890"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0",891"4541669a7eaaee61e708dc7cbcc5eb62");893TestPoly1305("ffffffffffffffffffffffffffffffff",894"0200000000000000000000000000000000000000000000000000000000000000",895"03000000000000000000000000000000");897TestPoly1305("02000000000000000000000000000000",898"02000000000000000000000000000000ffffffffffffffffffffffffffffffff",899"03000000000000000000000000000000");901TestPoly1305("fffffffffffffffffffffffffffffffff0ffffffffffffffffffffffffffffff11000000000000000000000000000000",902"0100000000000000000000000000000000000000000000000000000000000000",903"05000000000000000000000000000000");905TestPoly1305("fffffffffffffffffffffffffffffffffbfefefefefefefefefefefefefefefe01010101010101010101010101010101",906"0100000000000000000000000000000000000000000000000000000000000000",907"00000000000000000000000000000000");909TestPoly1305("fdffffffffffffffffffffffffffffff",910"0200000000000000000000000000000000000000000000000000000000000000",911"faffffffffffffffffffffffffffffff");913TestPoly1305("e33594d7505e43b900000000000000003394d7505e4379cd01000000000000000000000000000000000000000000000001000000000000000000000000000000",914"0100000000000000040000000000000000000000000000000000000000000000",915"14000000000000005500000000000000");917TestPoly1305("e33594d7505e43b900000000000000003394d7505e4379cd010000000000000000000000000000000000000000000000",918"0100000000000000040000000000000000000000000000000000000000000000",919"13000000000000000000000000000000");921// Tests from https://github.com/floodyberry/poly1305-donna/blob/master/poly1305-donna.c922TestPoly1305("8e993b9f48681273c29650ba32fc76ce48332ea7164d96a4476fb8c531a1186a"923"c0dfc17c98dce87b4da7f011ec48c97271d2c20f9b928fe2270d6fb863d51738"924"b48eeee314a7cc8ab932164548e526ae90224368517acfeabd6bb3732bc0e9da"925"99832b61ca01b6de56244a9e88d5f9b37973f622a43d14a6599b1f654cb45a74"926"e355a5",927"eea6a7251c1e72916d11c2cb214d3c252539121d8e234e652d651fa4c8cff880",928"f3ffc7703f9400e52a7dfb4b3d3305d9");929{930// mac of the macs of messages of length 0 to 256, where the key and messages have all931// their values set to the length.932auto total_key = "01020304050607fffefdfcfbfaf9ffffffffffffffffffffffffffff00000000"_hex;933Poly1305 total_ctx(total_key);934for (unsigned i = 0; i < 256; ++i) {935std::vector<std::byte> key(32, std::byte{uint8_t(i)});936std::vector<std::byte> msg(i, std::byte{uint8_t(i)});937std::array<std::byte, Poly1305::TAGLEN> tag;938Poly1305{key}.Update(msg).Finalize(tag);939total_ctx.Update(tag);940}941std::vector<std::byte> total_tag(Poly1305::TAGLEN);942total_ctx.Finalize(total_tag);943BOOST_CHECK_EQUAL(HexStr(total_tag), "64afe2e8d6ad7bbdd287f97c44623d39");944}946// Tests with sparse messages and random keys.947TestPoly1305("000000000000000000000094000000000000b07c4300000000002c002600d500"948"00000000000000000000000000bc58000000000000000000c9000000dd000000"949"00000000000000d34c000000000000000000000000f9009100000000000000c2"950"4b0000e900000000000000000000000000000000000e00000027000074000000"951"0000000003000000000000f1000000000000dce2000000000000003900000000"952"0000000000000000000000000000000000000000000000520000000000000000"953"000000000000000000000000009500000000000000000000000000cf00826700"954"000000a900000000000000000000000000000000000000000079000000000000"955"0000de0000004c000000000033000000000000000000000000002800aa000000"956"00003300860000e000000000",957"6e543496db3cf677592989891ab021f58390feb84fb419fbc7bb516a60bfa302",958"7ea80968354d40d9d790b45310caf7f3");959TestPoly1305("0000005900000000c40000002f00000000000000000000000000000029690000"960"0000e8000037000000000000000000000000000b000000000000000000000000"961"000000000000000000000000001800006e0000000000a4000000000000000000"962"00000000000000004d00000000000000b0000000000000000000005a00000000"963"0000000000b7c300000000000000540000000000000000000000000a00000000"964"00005b0000000000000000000000000000000000002d00e70000000000000000"965"000000000000003400006800d700000000000000000000360000000000000000"966"00eb000000000000000000000000000000000000000000000000000028000000"967"37000000000000000000000000000000000000000000000000000000008f0000"968"000000000000000000000000",969"f0b659a4f3143d8a1e1dacb9a409fe7e7cd501dfb58b16a2623046c5d337922a",970"0e410fa9d7a40ac582e77546be9a72bb");971}973BOOST_AUTO_TEST_CASE(chacha20poly1305_testvectors)974{975// Note that in our implementation, the authentication is suffixed to the ciphertext.976// The RFC test vectors specify them separately.978// RFC 8439 Example from section 2.8.2979TestChaCha20Poly1305("4c616469657320616e642047656e746c656d656e206f662074686520636c6173"980"73206f66202739393a204966204920636f756c64206f6666657220796f75206f"981"6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73"982"637265656e20776f756c642062652069742e",983"50515253c0c1c2c3c4c5c6c7",984"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f",985{7, 0x4746454443424140},986"d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d6"987"3dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b36"988"92ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc"989"3ff4def08e4b7a9de576d26586cec64b61161ae10b594f09e26a7e902ecbd060"990"0691");992// RFC 8439 Test vector A.5993TestChaCha20Poly1305("496e7465726e65742d4472616674732061726520647261667420646f63756d65"994"6e74732076616c696420666f722061206d6178696d756d206f6620736978206d"995"6f6e74687320616e64206d617920626520757064617465642c207265706c6163"996"65642c206f72206f62736f6c65746564206279206f7468657220646f63756d65"997"6e747320617420616e792074696d652e20497420697320696e617070726f7072"998"6961746520746f2075736520496e7465726e65742d4472616674732061732072"999"65666572656e6365206d6174657269616c206f7220746f206369746520746865"1000"6d206f74686572207468616e206173202fe2809c776f726b20696e2070726f67"
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.