Bitcoin Core  22.99.0
P2P Digital Currency
compress_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <compressor.h>
6 #include <script/standard.h>
8 
9 #include <stdint.h>
10 
11 #include <boost/test/unit_test.hpp>
12 
13 // amounts 0.00000001 .. 0.00100000
14 #define NUM_MULTIPLES_UNIT 100000
15 
16 // amounts 0.01 .. 100.00
17 #define NUM_MULTIPLES_CENT 10000
18 
19 // amounts 1 .. 10000
20 #define NUM_MULTIPLES_1BTC 10000
21 
22 // amounts 50 .. 21000000
23 #define NUM_MULTIPLES_50BTC 420000
24 
26 
27 bool static TestEncode(uint64_t in) {
28  return in == DecompressAmount(CompressAmount(in));
29 }
30 
31 bool static TestDecode(uint64_t in) {
32  return in == CompressAmount(DecompressAmount(in));
33 }
34 
35 bool static TestPair(uint64_t dec, uint64_t enc) {
36  return CompressAmount(dec) == enc &&
37  DecompressAmount(enc) == dec;
38 }
39 
40 BOOST_AUTO_TEST_CASE(compress_amounts)
41 {
42  BOOST_CHECK(TestPair( 0, 0x0));
43  BOOST_CHECK(TestPair( 1, 0x1));
44  BOOST_CHECK(TestPair( CENT, 0x7));
45  BOOST_CHECK(TestPair( COIN, 0x9));
46  BOOST_CHECK(TestPair( 50*COIN, 0x32));
47  BOOST_CHECK(TestPair(21000000*COIN, 0x1406f40));
48 
49  for (uint64_t i = 1; i <= NUM_MULTIPLES_UNIT; i++)
51 
52  for (uint64_t i = 1; i <= NUM_MULTIPLES_CENT; i++)
54 
55  for (uint64_t i = 1; i <= NUM_MULTIPLES_1BTC; i++)
57 
58  for (uint64_t i = 1; i <= NUM_MULTIPLES_50BTC; i++)
59  BOOST_CHECK(TestEncode(i * 50 * COIN));
60 
61  for (uint64_t i = 0; i < 100000; i++)
63 }
64 
65 BOOST_AUTO_TEST_CASE(compress_script_to_ckey_id)
66 {
67  // case CKeyID
68  CKey key;
69  key.MakeNewKey(true);
70  CPubKey pubkey = key.GetPubKey();
71 
72  CScript script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkey.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
73  BOOST_CHECK_EQUAL(script.size(), 25U);
74 
75  CompressedScript out;
76  bool done = CompressScript(script, out);
77  BOOST_CHECK_EQUAL(done, true);
78 
79  // Check compressed script
80  BOOST_CHECK_EQUAL(out.size(), 21U);
81  BOOST_CHECK_EQUAL(out[0], 0x00);
82  BOOST_CHECK_EQUAL(memcmp(out.data() + 1, script.data() + 3, 20), 0); // compare the 20 relevant chars of the CKeyId in the script
83 }
84 
85 BOOST_AUTO_TEST_CASE(compress_script_to_cscript_id)
86 {
87  // case CScriptID
88  CScript script, redeemScript;
89  script << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL;
90  BOOST_CHECK_EQUAL(script.size(), 23U);
91 
92  CompressedScript out;
93  bool done = CompressScript(script, out);
94  BOOST_CHECK_EQUAL(done, true);
95 
96  // Check compressed script
97  BOOST_CHECK_EQUAL(out.size(), 21U);
98  BOOST_CHECK_EQUAL(out[0], 0x01);
99  BOOST_CHECK_EQUAL(memcmp(out.data() + 1, script.data() + 2, 20), 0); // compare the 20 relevant chars of the CScriptId in the script
100 }
101 
102 BOOST_AUTO_TEST_CASE(compress_script_to_compressed_pubkey_id)
103 {
104  CKey key;
105  key.MakeNewKey(true); // case compressed PubKeyID
106 
107  CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG; // COMPRESSED_PUBLIC_KEY_SIZE (33)
108  BOOST_CHECK_EQUAL(script.size(), 35U);
109 
110  CompressedScript out;
111  bool done = CompressScript(script, out);
112  BOOST_CHECK_EQUAL(done, true);
113 
114  // Check compressed script
115  BOOST_CHECK_EQUAL(out.size(), 33U);
116  BOOST_CHECK_EQUAL(memcmp(out.data(), script.data() + 1, 1), 0);
117  BOOST_CHECK_EQUAL(memcmp(out.data() + 1, script.data() + 2, 32), 0); // compare the 32 chars of the compressed CPubKey
118 }
119 
120 BOOST_AUTO_TEST_CASE(compress_script_to_uncompressed_pubkey_id)
121 {
122  CKey key;
123  key.MakeNewKey(false); // case uncompressed PubKeyID
124  CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG; // PUBLIC_KEY_SIZE (65)
125  BOOST_CHECK_EQUAL(script.size(), 67U); // 1 char code + 65 char pubkey + OP_CHECKSIG
126 
127  CompressedScript out;
128  bool done = CompressScript(script, out);
129  BOOST_CHECK_EQUAL(done, true);
130 
131  // Check compressed script
132  BOOST_CHECK_EQUAL(out.size(), 33U);
133  BOOST_CHECK_EQUAL(memcmp(out.data() + 1, script.data() + 2, 32), 0); // first 32 chars of CPubKey are copied into out[1:]
134  BOOST_CHECK_EQUAL(out[0], 0x04 | (script[65] & 0x01)); // least significant bit (lsb) of last char of pubkey is mapped into out[0]
135 }
136 
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(compress_amounts)
Definition: compress_tests.cpp:40
CKey::MakeNewKey
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:160
setup_common.h
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
compressor.h
ToByteVector
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:60
CompressScript
bool CompressScript(const CScript &script, CompressedScript &out)
Definition: compressor.cpp:55
TestEncode
static bool TestEncode(uint64_t in)
Definition: compress_tests.cpp:27
OP_EQUAL
@ OP_EQUAL
Definition: script.h:139
CENT
static constexpr CAmount CENT
Definition: setup_common.h:71
NUM_MULTIPLES_1BTC
#define NUM_MULTIPLES_1BTC
Definition: compress_tests.cpp:20
TestPair
static bool TestPair(uint64_t dec, uint64_t enc)
Definition: compress_tests.cpp:35
standard.h
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
TestDecode
static bool TestDecode(uint64_t in)
Definition: compress_tests.cpp:31
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
OP_HASH160
@ OP_HASH160
Definition: script.h:180
prevector
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
CPubKey
An encapsulated public key.
Definition: pubkey.h:32
CKey
An encapsulated private key.
Definition: key.h:26
prevector::data
value_type * data()
Definition: prevector.h:528
DecompressAmount
uint64_t DecompressAmount(uint64_t x)
Definition: compressor.cpp:168
prevector::size
size_type size() const
Definition: prevector.h:282
OP_EQUALVERIFY
@ OP_EQUALVERIFY
Definition: script.h:140
COIN
static const CAmount COIN
Definition: amount.h:14
NUM_MULTIPLES_UNIT
#define NUM_MULTIPLES_UNIT
Definition: compress_tests.cpp:14
CScriptID
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:25
OP_CHECKSIG
@ OP_CHECKSIG
Definition: script.h:183
NUM_MULTIPLES_50BTC
#define NUM_MULTIPLES_50BTC
Definition: compress_tests.cpp:23
OP_DUP
@ OP_DUP
Definition: script.h:118
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
CompressAmount
uint64_t CompressAmount(uint64_t n)
Compress amount.
Definition: compressor.cpp:149
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:160
NUM_MULTIPLES_CENT
#define NUM_MULTIPLES_CENT
Definition: compress_tests.cpp:17