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