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) 2012-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 <compressor.h>6#include <script/script.h>7#include <test/util/random.h>8#include <test/util/setup_common.h>10#include <cstdint>12#include <boost/test/unit_test.hpp>14// amounts 0.00000001 .. 0.0010000015#define NUM_MULTIPLES_UNIT 10000017// amounts 0.01 .. 100.0018#define NUM_MULTIPLES_CENT 1000020// amounts 1 .. 1000021#define NUM_MULTIPLES_1BTC 1000023// amounts 50 .. 2100000024#define NUM_MULTIPLES_50BTC 42000026BOOST_FIXTURE_TEST_SUITE(compress_tests, BasicTestingSetup)28bool static TestEncode(uint64_t in) {29return in == DecompressAmount(CompressAmount(in));30}32bool static TestDecode(uint64_t in) {33return in == CompressAmount(DecompressAmount(in));34}36bool static TestPair(uint64_t dec, uint64_t enc) {37return CompressAmount(dec) == enc &&38DecompressAmount(enc) == dec;39}41BOOST_AUTO_TEST_CASE(compress_amounts)42{43BOOST_CHECK(TestPair( 0, 0x0));44BOOST_CHECK(TestPair( 1, 0x1));45BOOST_CHECK(TestPair( CENT, 0x7));46BOOST_CHECK(TestPair( COIN, 0x9));47BOOST_CHECK(TestPair( 50*COIN, 0x32));48BOOST_CHECK(TestPair(21000000*COIN, 0x1406f40));50for (uint64_t i = 1; i <= NUM_MULTIPLES_UNIT; i++)51BOOST_CHECK(TestEncode(i));53for (uint64_t i = 1; i <= NUM_MULTIPLES_CENT; i++)54BOOST_CHECK(TestEncode(i * CENT));56for (uint64_t i = 1; i <= NUM_MULTIPLES_1BTC; i++)57BOOST_CHECK(TestEncode(i * COIN));59for (uint64_t i = 1; i <= NUM_MULTIPLES_50BTC; i++)60BOOST_CHECK(TestEncode(i * 50 * COIN));62for (uint64_t i = 0; i < 100000; i++)63BOOST_CHECK(TestDecode(i));64}66BOOST_AUTO_TEST_CASE(compress_script_to_ckey_id)67{68// case CKeyID69CKey key = GenerateRandomKey();70CPubKey pubkey = key.GetPubKey();72CScript script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkey.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;73BOOST_CHECK_EQUAL(script.size(), 25U);75CompressedScript out;76bool done = CompressScript(script, out);77BOOST_CHECK_EQUAL(done, true);79// Check compressed script80BOOST_CHECK_EQUAL(out.size(), 21U);81BOOST_CHECK_EQUAL(out[0], 0x00);82BOOST_CHECK_EQUAL(memcmp(out.data() + 1, script.data() + 3, 20), 0); // compare the 20 relevant chars of the CKeyId in the script83}85BOOST_AUTO_TEST_CASE(compress_script_to_cscript_id)86{87// case CScriptID88CScript script, redeemScript;89script << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL;90BOOST_CHECK_EQUAL(script.size(), 23U);92CompressedScript out;93bool done = CompressScript(script, out);94BOOST_CHECK_EQUAL(done, true);96// Check compressed script97BOOST_CHECK_EQUAL(out.size(), 21U);98BOOST_CHECK_EQUAL(out[0], 0x01);99BOOST_CHECK_EQUAL(memcmp(out.data() + 1, script.data() + 2, 20), 0); // compare the 20 relevant chars of the CScriptId in the script100}102BOOST_AUTO_TEST_CASE(compress_script_to_compressed_pubkey_id)103{104CKey key = GenerateRandomKey(); // case compressed PubKeyID106CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG; // COMPRESSED_PUBLIC_KEY_SIZE (33)107BOOST_CHECK_EQUAL(script.size(), 35U);109CompressedScript out;110bool done = CompressScript(script, out);111BOOST_CHECK_EQUAL(done, true);113// Check compressed script114BOOST_CHECK_EQUAL(out.size(), 33U);115BOOST_CHECK_EQUAL(memcmp(out.data(), script.data() + 1, 1), 0);116BOOST_CHECK_EQUAL(memcmp(out.data() + 1, script.data() + 2, 32), 0); // compare the 32 chars of the compressed CPubKey117}119BOOST_AUTO_TEST_CASE(compress_script_to_uncompressed_pubkey_id)120{121CKey key = GenerateRandomKey(/*compressed=*/false); // case uncompressed PubKeyID122CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG; // PUBLIC_KEY_SIZE (65)123BOOST_CHECK_EQUAL(script.size(), 67U); // 1 char code + 65 char pubkey + OP_CHECKSIG125CompressedScript out;126bool done = CompressScript(script, out);127BOOST_CHECK_EQUAL(done, true);129// Check compressed script130BOOST_CHECK_EQUAL(out.size(), 33U);131BOOST_CHECK_EQUAL(memcmp(out.data() + 1, script.data() + 2, 32), 0); // first 32 chars of CPubKey are copied into out[1:]132BOOST_CHECK_EQUAL(out[0], 0x04 | (script[65] & 0x01)); // least significant bit (lsb) of last char of pubkey is mapped into out[0]133}135BOOST_AUTO_TEST_CASE(compress_p2pk_scripts_not_on_curve)136{137XOnlyPubKey x_not_on_curve;138do {139x_not_on_curve = XOnlyPubKey(m_rng.randbytes(32));140} while (x_not_on_curve.IsFullyValid());142// Check that P2PK script with uncompressed pubkey [=> OP_PUSH65 <0x04 .....> OP_CHECKSIG]143// which is not fully valid (i.e. point is not on curve) can't be compressed144std::vector<unsigned char> pubkey_raw(65, 0);145pubkey_raw[0] = 4;146std::copy(x_not_on_curve.begin(), x_not_on_curve.end(), &pubkey_raw[1]);147CPubKey pubkey_not_on_curve(pubkey_raw);148assert(pubkey_not_on_curve.IsValid());149assert(!pubkey_not_on_curve.IsFullyValid());150CScript script = CScript() << ToByteVector(pubkey_not_on_curve) << OP_CHECKSIG;151BOOST_CHECK_EQUAL(script.size(), 67U);153CompressedScript out;154bool done = CompressScript(script, out);155BOOST_CHECK_EQUAL(done, false);157// Check that compressed P2PK script with uncompressed pubkey that is not fully158// valid (i.e. x coordinate of the pubkey is not on curve) can't be decompressed159CompressedScript compressed_script(x_not_on_curve.begin(), x_not_on_curve.end());160for (unsigned int compression_id : {4, 5}) {161CScript uncompressed_script;162bool success = DecompressScript(uncompressed_script, compression_id, compressed_script);163BOOST_CHECK_EQUAL(success, false);164}165}167BOOST_AUTO_TEST_SUITE_END()
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.