Inside Bitcoin
Opening the selected file…
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) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <compressor.h>8#include <pubkey.h>9#include <script/script.h>11/*12* These check for scripts for which a special case with a shorter encoding is defined.13* They are implemented separately from the CScript test, as these test for exact byte14* sequence correspondences, and are more strict. For example, IsToPubKey also verifies15* whether the public key is valid (as invalid ones cannot be represented in compressed16* form).17*/19static bool IsToKeyID(const CScript& script, CKeyID &hash)20{21if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH16022&& script[2] == 20 && script[23] == OP_EQUALVERIFY23&& script[24] == OP_CHECKSIG) {24memcpy(&hash, &script[3], 20);25return true;26}27return false;28}30static bool IsToScriptID(const CScript& script, CScriptID &hash)31{32if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 2033&& script[22] == OP_EQUAL) {34memcpy(&hash, &script[2], 20);35return true;36}37return false;38}40static bool IsToPubKey(const CScript& script, CPubKey &pubkey)41{42if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG43&& (script[1] == 0x02 || script[1] == 0x03)) {44pubkey.Set(&script[1], &script[34]);45return true;46}47if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG48&& script[1] == 0x04) {49pubkey.Set(&script[1], &script[66]);50return pubkey.IsFullyValid(); // if not fully valid, a case that would not be compressible51}52return false;53}55bool CompressScript(const CScript& script, CompressedScript& out)56{57CKeyID keyID;58if (IsToKeyID(script, keyID)) {59out.resize(21);60out[0] = 0x00;61memcpy(&out[1], &keyID, 20);62return true;63}64CScriptID scriptID;65if (IsToScriptID(script, scriptID)) {66out.resize(21);67out[0] = 0x01;68memcpy(&out[1], &scriptID, 20);69return true;70}71CPubKey pubkey;72if (IsToPubKey(script, pubkey)) {73out.resize(33);74memcpy(&out[1], &pubkey[1], 32);75if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {76out[0] = pubkey[0];77return true;78} else if (pubkey[0] == 0x04) {79out[0] = 0x04 | (pubkey[64] & 0x01);80return true;81}82}83return false;84}86unsigned int GetSpecialScriptSize(unsigned int nSize)87{88if (nSize == 0 || nSize == 1)89return 20;90if (nSize == 2 || nSize == 3 || nSize == 4 || nSize == 5)91return 32;92return 0;93}95bool DecompressScript(CScript& script, unsigned int nSize, const CompressedScript& in)96{97switch(nSize) {98case 0x00:99script.resize(25);100script[0] = OP_DUP;101script[1] = OP_HASH160;102script[2] = 20;103memcpy(&script[3], in.data(), 20);104script[23] = OP_EQUALVERIFY;105script[24] = OP_CHECKSIG;106return true;107case 0x01:108script.resize(23);109script[0] = OP_HASH160;110script[1] = 20;111memcpy(&script[2], in.data(), 20);112script[22] = OP_EQUAL;113return true;114case 0x02:115case 0x03:116script.resize(35);117script[0] = 33;118script[1] = nSize;119memcpy(&script[2], in.data(), 32);120script[34] = OP_CHECKSIG;121return true;122case 0x04:123case 0x05:124unsigned char vch[33] = {};125vch[0] = nSize - 2;126memcpy(&vch[1], in.data(), 32);127CPubKey pubkey{vch};128if (!pubkey.Decompress())129return false;130assert(pubkey.size() == 65);131script.resize(67);132script[0] = 65;133memcpy(&script[1], pubkey.begin(), 65);134script[66] = OP_CHECKSIG;135return true;136}137return false;138}140// Amount compression:141// * If the amount is 0, output 0142// * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9)143// * if e<9, the last digit of the resulting number cannot be 0; store it as d, and drop it (divide by 10)144// * call the result n145// * output 1 + 10*(9*n + d - 1) + e146// * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9147// (this is decodable, as d is in [1-9] and e is in [0-9])149uint64_t CompressAmount(uint64_t n)150{151if (n == 0)152return 0;153int e = 0;154while (((n % 10) == 0) && e < 9) {155n /= 10;156e++;157}158if (e < 9) {159int d = (n % 10);160assert(d >= 1 && d <= 9);161n /= 10;162return 1 + (n*9 + d - 1)*10 + e;163} else {164return 1 + (n - 1)*10 + 9;165}166}168uint64_t DecompressAmount(uint64_t x)169{170// x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9171if (x == 0)172return 0;173x--;174// x = 10*(9*n + d - 1) + e175int e = x % 10;176x /= 10;177uint64_t n = 0;178if (e < 9) {179// x = 9*n + d - 1180int d = (x % 9) + 1;181x /= 9;182// x = n183n = x*10 + d;184} else {185n = x+1;186}187while (e) {188n *= 10;189e--;190}191return n;192}