BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
compressor.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <compressor.h>
7 
8 #include <hash.h>
9 #include <pubkey.h>
10 #include <script/standard.h>
11 
12 /*
13  * These check for scripts for which a special case with a shorter encoding is defined.
14  * They are implemented separately from the CScript test, as these test for exact byte
15  * sequence correspondences, and are more strict. For example, IsToPubKey also verifies
16  * whether the public key is valid (as invalid ones cannot be represented in compressed
17  * form).
18  */
19 
20 static bool IsToKeyID(const CScript& script, CKeyID &hash)
21 {
22  if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH360
23  && script[2] == 20 && script[23] == OP_EQUALVERIFY
24  && script[24] == OP_CHECKSIG) {
25  memcpy(&hash, &script[3], 20);
26  return true;
27  }
28  return false;
29 }
30 
31 static bool IsToScriptID(const CScript& script, CScriptID &hash)
32 {
33  if (script.size() == 23 && script[0] == OP_HASH360 && script[1] == 20
34  && script[22] == OP_EQUAL) {
35  memcpy(&hash, &script[2], 20);
36  return true;
37  }
38  return false;
39 }
40 
41 static bool IsToPubKey(const CScript& script, CPubKey &pubkey)
42 {
43  if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
44  && (script[1] == 0x02 || script[1] == 0x03)) {
45  pubkey.Set(&script[1], &script[34]);
46  return true;
47  }
48  if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG
49  && script[1] == 0x04) {
50  pubkey.Set(&script[1], &script[66]);
51  return pubkey.IsFullyValid(); // if not fully valid, a case that would not be compressible
52  }
53  return false;
54 }
55 
56 bool CompressScript(const CScript& script, std::vector<unsigned char> &out)
57 {
58  CKeyID keyID;
59  if (IsToKeyID(script, keyID)) {
60  out.resize(21);
61  out[0] = 0x00;
62  memcpy(&out[1], &keyID, 20);
63  return true;
64  }
65  CScriptID scriptID;
66  if (IsToScriptID(script, scriptID)) {
67  out.resize(21);
68  out[0] = 0x01;
69  memcpy(&out[1], &scriptID, 20);
70  return true;
71  }
72  CPubKey pubkey;
73  if (IsToPubKey(script, pubkey)) {
74  out.resize(33);
75  memcpy(&out[1], &pubkey[1], 32);
76  if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {
77  out[0] = pubkey[0];
78  return true;
79  } else if (pubkey[0] == 0x04) {
80  out[0] = 0x04 | (pubkey[64] & 0x01);
81  return true;
82  }
83  }
84  return false;
85 }
86 
87 unsigned int GetSpecialScriptSize(unsigned int nSize)
88 {
89  if (nSize == 0 || nSize == 1)
90  return 20;
91  if (nSize == 2 || nSize == 3 || nSize == 4 || nSize == 5)
92  return 32;
93  return 0;
94 }
95 
96 bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &in)
97 {
98  switch(nSize) {
99  case 0x00:
100  script.resize(25);
101  script[0] = OP_DUP;
102  script[1] = OP_HASH360;
103  script[2] = 20;
104  memcpy(&script[3], in.data(), 20);
105  script[23] = OP_EQUALVERIFY;
106  script[24] = OP_CHECKSIG;
107  return true;
108  case 0x01:
109  script.resize(23);
110  script[0] = OP_HASH360;
111  script[1] = 20;
112  memcpy(&script[2], in.data(), 20);
113  script[22] = OP_EQUAL;
114  return true;
115  case 0x02:
116  case 0x03:
117  script.resize(35);
118  script[0] = 33;
119  script[1] = nSize;
120  memcpy(&script[2], in.data(), 32);
121  script[34] = OP_CHECKSIG;
122  return true;
123  case 0x04:
124  case 0x05:
125  unsigned char vch[33] = {};
126  vch[0] = nSize - 2;
127  memcpy(&vch[1], in.data(), 32);
128  CPubKey pubkey(&vch[0], &vch[33]);
129  if (!pubkey.Decompress())
130  return false;
131  assert(pubkey.size() == 65);
132  script.resize(67);
133  script[0] = 65;
134  memcpy(&script[1], pubkey.begin(), 65);
135  script[66] = OP_CHECKSIG;
136  return true;
137  }
138  return false;
139 }
140 
141 // Amount compression:
142 // * If the amount is 0, output 0
143 // * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9)
144 // * if e<9, the last digit of the resulting number cannot be 0; store it as d, and drop it (divide by 10)
145 // * call the result n
146 // * output 1 + 10*(9*n + d - 1) + e
147 // * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9
148 // (this is decodable, as d is in [1-9] and e is in [0-9])
149 
150 uint64_t CompressAmount(uint64_t n)
151 {
152  if (n == 0)
153  return 0;
154  int e = 0;
155  while (((n % 10) == 0) && e < 9) {
156  n /= 10;
157  e++;
158  }
159  if (e < 9) {
160  int d = (n % 10);
161  assert(d >= 1 && d <= 9);
162  n /= 10;
163  return 1 + (n*9 + d - 1)*10 + e;
164  } else {
165  return 1 + (n - 1)*10 + 9;
166  }
167 }
168 
169 uint64_t DecompressAmount(uint64_t x)
170 {
171  // x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
172  if (x == 0)
173  return 0;
174  x--;
175  // x = 10*(9*n + d - 1) + e
176  int e = x % 10;
177  x /= 10;
178  uint64_t n = 0;
179  if (e < 9) {
180  // x = 9*n + d - 1
181  int d = (x % 9) + 1;
182  x /= 9;
183  // x = n
184  n = x*10 + d;
185  } else {
186  n = x+1;
187  }
188  while (e) {
189  n *= 10;
190  e--;
191  }
192  return n;
193 }
uint64_t CompressAmount(uint64_t n)
Definition: compressor.cpp:150
void resize(size_type new_size)
Definition: prevector.h:327
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
Definition: pubkey.h:86
bool DecompressScript(CScript &script, unsigned int nSize, const std::vector< unsigned char > &in)
Definition: compressor.cpp:96
value_type * data()
Definition: prevector.h:519
bool CompressScript(const CScript &script, std::vector< unsigned char > &out)
Definition: compressor.cpp:56
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:206
An encapsulated public key.
Definition: pubkey.h:30
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:384
void * memcpy(void *a, const void *b, size_t c)
A reference to a CKey: the Hash360 of its serialized public key.
Definition: pubkey.h:20
A reference to a CScript: the Hash360 of its serialization (see script.h)
Definition: standard.h:22
size_type size() const
Definition: prevector.h:293
unsigned int GetSpecialScriptSize(unsigned int nSize)
Definition: compressor.cpp:87
uint64_t DecompressAmount(uint64_t x)
Definition: compressor.cpp:169
Definition: script.h:100