You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compressor.cpp 5.0KB

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