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.
252{253std::vector<unsigned char> vch_sig;254const bool ok = key.Sign(random_uint256, vch_sig, false);255assert(ok);256assert(pubkey.Verify(random_uint256, vch_sig));257assert(CPubKey::CheckLowS(vch_sig));259const std::vector<unsigned char> vch_invalid_sig{vch_sig.begin(), vch_sig.begin() + vch_sig.size() - 1};260assert(!pubkey.Verify(random_uint256, vch_invalid_sig));261assert(!CPubKey::CheckLowS(vch_invalid_sig));262}264{265std::vector<unsigned char> vch_compact_sig;266const bool ok_sign_compact = key.SignCompact(random_uint256, vch_compact_sig);267assert(ok_sign_compact);269CPubKey recover_pubkey;270const bool ok_recover_compact = recover_pubkey.RecoverCompact(random_uint256, vch_compact_sig);271assert(ok_recover_compact);272assert(recover_pubkey == pubkey);273}275{276CPubKey child_pubkey;277ChainCode child_chaincode;278const bool ok = pubkey.Derive(child_pubkey, child_chaincode, 0, ChainCode{random_uint256});279assert(ok);280assert(child_pubkey != pubkey);281assert(child_pubkey.IsCompressed());282assert(child_pubkey.IsFullyValid());283assert(child_pubkey.IsValid());284assert(child_pubkey.size() == 33);285assert(child_chaincode != random_uint256);286}288const CPrivKey priv_key = key.GetPrivKey();290{291for (const bool skip_check : {true, false}) {292CKey loaded_key;293const bool ok = loaded_key.Load(priv_key, pubkey, skip_check);294assert(ok);295assert(key == loaded_key);296}297}298}300FUZZ_TARGET(ellswift_roundtrip, .init = initialize_key)301{302FuzzedDataProvider fdp{buffer.data(), buffer.size()};304CKey key = ConsumePrivateKey(fdp, /*compressed=*/true);305if (!key.IsValid()) return;307auto ent32 = fdp.ConsumeBytes<std::byte>(32);308ent32.resize(32);310auto encoded_ellswift = key.EllSwiftCreate(ent32);311auto decoded_pubkey = encoded_ellswift.Decode();313uint256 hash{ConsumeUInt256(fdp)};314std::vector<unsigned char> sig;315key.Sign(hash, sig);316assert(decoded_pubkey.Verify(hash, sig));317}319FUZZ_TARGET(bip324_ecdh, .init = initialize_key)320{321FuzzedDataProvider fdp{buffer.data(), buffer.size()};323// We generate private key, k1.324CKey k1 = ConsumePrivateKey(fdp, /*compressed=*/true);325if (!k1.IsValid()) return;327// They generate private key, k2.328CKey k2 = ConsumePrivateKey(fdp, /*compressed=*/true);329if (!k2.IsValid()) return;331// We construct an ellswift encoding for our key, k1_ellswift.332auto ent32_1 = fdp.ConsumeBytes<std::byte>(32);333ent32_1.resize(32);334auto k1_ellswift = k1.EllSwiftCreate(ent32_1);336// They construct an ellswift encoding for their key, k2_ellswift.337auto ent32_2 = fdp.ConsumeBytes<std::byte>(32);338ent32_2.resize(32);339auto k2_ellswift = k2.EllSwiftCreate(ent32_2);341// They construct another (possibly distinct) ellswift encoding for their key, k2_ellswift_bad.342auto ent32_2_bad = fdp.ConsumeBytes<std::byte>(32);343ent32_2_bad.resize(32);344auto k2_ellswift_bad = k2.EllSwiftCreate(ent32_2_bad);345assert((ent32_2_bad == ent32_2) == (k2_ellswift_bad == k2_ellswift));347// Determine who is who.348bool initiating = fdp.ConsumeBool();350// We compute our shared secret using our key and their public key.351auto ecdh_secret_1 = k1.ComputeBIP324ECDHSecret(k2_ellswift, k1_ellswift, initiating);352// They compute their shared secret using their key and our public key.353auto ecdh_secret_2 = k2.ComputeBIP324ECDHSecret(k1_ellswift, k2_ellswift, !initiating);354// Those must match, as everyone is behaving correctly.355assert(ecdh_secret_1 == ecdh_secret_2);357if (k1_ellswift != k2_ellswift) {358// Unless the two keys are exactly identical, acting as the wrong party breaks things.359auto ecdh_secret_bad = k1.ComputeBIP324ECDHSecret(k2_ellswift, k1_ellswift, !initiating);360assert(ecdh_secret_bad != ecdh_secret_1);361}363if (k2_ellswift_bad != k2_ellswift) {364// Unless both encodings created by them are identical, using the second one breaks things.365auto ecdh_secret_bad = k1.ComputeBIP324ECDHSecret(k2_ellswift_bad, k1_ellswift, initiating);366assert(ecdh_secret_bad != ecdh_secret_1);367}368}
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.