PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/bech32.cpp

https://github.com/denis2342/bitcoin
C++ | 230 lines | 115 code | 27 blank | 88 comment | 43 complexity | ba5491df124c7f801dff6ce5fd4bfd4b MD5 | raw file
  1. // Copyright (c) 2017, 2021 Pieter Wuille
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include <bech32.h>
  5. #include <util/vector.h>
  6. #include <assert.h>
  7. namespace bech32
  8. {
  9. namespace
  10. {
  11. typedef std::vector<uint8_t> data;
  12. /** The Bech32 and Bech32m character set for encoding. */
  13. const char* CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
  14. /** The Bech32 and Bech32m character set for decoding. */
  15. const int8_t CHARSET_REV[128] = {
  16. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  17. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  18. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  19. 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1,
  20. -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
  21. 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1,
  22. -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
  23. 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1
  24. };
  25. /* Determine the final constant to use for the specified encoding. */
  26. uint32_t EncodingConstant(Encoding encoding) {
  27. assert(encoding == Encoding::BECH32 || encoding == Encoding::BECH32M);
  28. return encoding == Encoding::BECH32 ? 1 : 0x2bc830a3;
  29. }
  30. /** This function will compute what 6 5-bit values to XOR into the last 6 input values, in order to
  31. * make the checksum 0. These 6 values are packed together in a single 30-bit integer. The higher
  32. * bits correspond to earlier values. */
  33. uint32_t PolyMod(const data& v)
  34. {
  35. // The input is interpreted as a list of coefficients of a polynomial over F = GF(32), with an
  36. // implicit 1 in front. If the input is [v0,v1,v2,v3,v4], that polynomial is v(x) =
  37. // 1*x^5 + v0*x^4 + v1*x^3 + v2*x^2 + v3*x + v4. The implicit 1 guarantees that
  38. // [v0,v1,v2,...] has a distinct checksum from [0,v0,v1,v2,...].
  39. // The output is a 30-bit integer whose 5-bit groups are the coefficients of the remainder of
  40. // v(x) mod g(x), where g(x) is the Bech32 generator,
  41. // x^6 + {29}x^5 + {22}x^4 + {20}x^3 + {21}x^2 + {29}x + {18}. g(x) is chosen in such a way
  42. // that the resulting code is a BCH code, guaranteeing detection of up to 3 errors within a
  43. // window of 1023 characters. Among the various possible BCH codes, one was selected to in
  44. // fact guarantee detection of up to 4 errors within a window of 89 characters.
  45. // Note that the coefficients are elements of GF(32), here represented as decimal numbers
  46. // between {}. In this finite field, addition is just XOR of the corresponding numbers. For
  47. // example, {27} + {13} = {27 ^ 13} = {22}. Multiplication is more complicated, and requires
  48. // treating the bits of values themselves as coefficients of a polynomial over a smaller field,
  49. // GF(2), and multiplying those polynomials mod a^5 + a^3 + 1. For example, {5} * {26} =
  50. // (a^2 + 1) * (a^4 + a^3 + a) = (a^4 + a^3 + a) * a^2 + (a^4 + a^3 + a) = a^6 + a^5 + a^4 + a
  51. // = a^3 + 1 (mod a^5 + a^3 + 1) = {9}.
  52. // During the course of the loop below, `c` contains the bitpacked coefficients of the
  53. // polynomial constructed from just the values of v that were processed so far, mod g(x). In
  54. // the above example, `c` initially corresponds to 1 mod g(x), and after processing 2 inputs of
  55. // v, it corresponds to x^2 + v0*x + v1 mod g(x). As 1 mod g(x) = 1, that is the starting value
  56. // for `c`.
  57. // The following Sage code constructs the generator used:
  58. //
  59. // B = GF(2) # Binary field
  60. // BP.<b> = B[] # Polynomials over the binary field
  61. // F_mod = b**5 + b**3 + 1
  62. // F.<f> = GF(32, modulus=F_mod, repr='int') # GF(32) definition
  63. // FP.<x> = F[] # Polynomials over GF(32)
  64. // E_mod = x**2 + F.fetch_int(9)*x + F.fetch_int(23)
  65. // E.<e> = F.extension(E_mod) # GF(1024) extension field definition
  66. // for p in divisors(E.order() - 1): # Verify e has order 1023.
  67. // assert((e**p == 1) == (p % 1023 == 0))
  68. // G = lcm([(e**i).minpoly() for i in range(997,1000)])
  69. // print(G) # Print out the generator
  70. //
  71. // It demonstrates that g(x) is the least common multiple of the minimal polynomials
  72. // of 3 consecutive powers (997,998,999) of a primitive element (e) of GF(1024).
  73. // That guarantees it is, in fact, the generator of a primitive BCH code with cycle
  74. // length 1023 and distance 4. See https://en.wikipedia.org/wiki/BCH_code for more details.
  75. uint32_t c = 1;
  76. for (const auto v_i : v) {
  77. // We want to update `c` to correspond to a polynomial with one extra term. If the initial
  78. // value of `c` consists of the coefficients of c(x) = f(x) mod g(x), we modify it to
  79. // correspond to c'(x) = (f(x) * x + v_i) mod g(x), where v_i is the next input to
  80. // process. Simplifying:
  81. // c'(x) = (f(x) * x + v_i) mod g(x)
  82. // ((f(x) mod g(x)) * x + v_i) mod g(x)
  83. // (c(x) * x + v_i) mod g(x)
  84. // If c(x) = c0*x^5 + c1*x^4 + c2*x^3 + c3*x^2 + c4*x + c5, we want to compute
  85. // c'(x) = (c0*x^5 + c1*x^4 + c2*x^3 + c3*x^2 + c4*x + c5) * x + v_i mod g(x)
  86. // = c0*x^6 + c1*x^5 + c2*x^4 + c3*x^3 + c4*x^2 + c5*x + v_i mod g(x)
  87. // = c0*(x^6 mod g(x)) + c1*x^5 + c2*x^4 + c3*x^3 + c4*x^2 + c5*x + v_i
  88. // If we call (x^6 mod g(x)) = k(x), this can be written as
  89. // c'(x) = (c1*x^5 + c2*x^4 + c3*x^3 + c4*x^2 + c5*x + v_i) + c0*k(x)
  90. // First, determine the value of c0:
  91. uint8_t c0 = c >> 25;
  92. // Then compute c1*x^5 + c2*x^4 + c3*x^3 + c4*x^2 + c5*x + v_i:
  93. c = ((c & 0x1ffffff) << 5) ^ v_i;
  94. // Finally, for each set bit n in c0, conditionally add {2^n}k(x). These constants can be
  95. // computed using the following Sage code (continuing the code above):
  96. //
  97. // for i in [1,2,4,8,16]: # Print out {1,2,4,8,16}*(g(x) mod x^6), packed in hex integers.
  98. // v = 0
  99. // for coef in reversed((F.fetch_int(i)*(G % x**6)).coefficients(sparse=True)):
  100. // v = v*32 + coef.integer_representation()
  101. // print("0x%x" % v)
  102. //
  103. if (c0 & 1) c ^= 0x3b6a57b2; // k(x) = {29}x^5 + {22}x^4 + {20}x^3 + {21}x^2 + {29}x + {18}
  104. if (c0 & 2) c ^= 0x26508e6d; // {2}k(x) = {19}x^5 + {5}x^4 + x^3 + {3}x^2 + {19}x + {13}
  105. if (c0 & 4) c ^= 0x1ea119fa; // {4}k(x) = {15}x^5 + {10}x^4 + {2}x^3 + {6}x^2 + {15}x + {26}
  106. if (c0 & 8) c ^= 0x3d4233dd; // {8}k(x) = {30}x^5 + {20}x^4 + {4}x^3 + {12}x^2 + {30}x + {29}
  107. if (c0 & 16) c ^= 0x2a1462b3; // {16}k(x) = {21}x^5 + x^4 + {8}x^3 + {24}x^2 + {21}x + {19}
  108. }
  109. return c;
  110. }
  111. /** Convert to lower case. */
  112. inline unsigned char LowerCase(unsigned char c)
  113. {
  114. return (c >= 'A' && c <= 'Z') ? (c - 'A') + 'a' : c;
  115. }
  116. /** Expand a HRP for use in checksum computation. */
  117. data ExpandHRP(const std::string& hrp)
  118. {
  119. data ret;
  120. ret.reserve(hrp.size() + 90);
  121. ret.resize(hrp.size() * 2 + 1);
  122. for (size_t i = 0; i < hrp.size(); ++i) {
  123. unsigned char c = hrp[i];
  124. ret[i] = c >> 5;
  125. ret[i + hrp.size() + 1] = c & 0x1f;
  126. }
  127. ret[hrp.size()] = 0;
  128. return ret;
  129. }
  130. /** Verify a checksum. */
  131. Encoding VerifyChecksum(const std::string& hrp, const data& values)
  132. {
  133. // PolyMod computes what value to xor into the final values to make the checksum 0. However,
  134. // if we required that the checksum was 0, it would be the case that appending a 0 to a valid
  135. // list of values would result in a new valid list. For that reason, Bech32 requires the
  136. // resulting checksum to be 1 instead. In Bech32m, this constant was amended. See
  137. // https://gist.github.com/sipa/14c248c288c3880a3b191f978a34508e for details.
  138. const uint32_t check = PolyMod(Cat(ExpandHRP(hrp), values));
  139. if (check == EncodingConstant(Encoding::BECH32)) return Encoding::BECH32;
  140. if (check == EncodingConstant(Encoding::BECH32M)) return Encoding::BECH32M;
  141. return Encoding::INVALID;
  142. }
  143. /** Create a checksum. */
  144. data CreateChecksum(Encoding encoding, const std::string& hrp, const data& values)
  145. {
  146. data enc = Cat(ExpandHRP(hrp), values);
  147. enc.resize(enc.size() + 6); // Append 6 zeroes
  148. uint32_t mod = PolyMod(enc) ^ EncodingConstant(encoding); // Determine what to XOR into those 6 zeroes.
  149. data ret(6);
  150. for (size_t i = 0; i < 6; ++i) {
  151. // Convert the 5-bit groups in mod to checksum values.
  152. ret[i] = (mod >> (5 * (5 - i))) & 31;
  153. }
  154. return ret;
  155. }
  156. } // namespace
  157. /** Encode a Bech32 or Bech32m string. */
  158. std::string Encode(Encoding encoding, const std::string& hrp, const data& values) {
  159. // First ensure that the HRP is all lowercase. BIP-173 and BIP350 require an encoder
  160. // to return a lowercase Bech32/Bech32m string, but if given an uppercase HRP, the
  161. // result will always be invalid.
  162. for (const char& c : hrp) assert(c < 'A' || c > 'Z');
  163. data checksum = CreateChecksum(encoding, hrp, values);
  164. data combined = Cat(values, checksum);
  165. std::string ret = hrp + '1';
  166. ret.reserve(ret.size() + combined.size());
  167. for (const auto c : combined) {
  168. ret += CHARSET[c];
  169. }
  170. return ret;
  171. }
  172. /** Decode a Bech32 or Bech32m string. */
  173. DecodeResult Decode(const std::string& str) {
  174. bool lower = false, upper = false;
  175. for (size_t i = 0; i < str.size(); ++i) {
  176. unsigned char c = str[i];
  177. if (c >= 'a' && c <= 'z') lower = true;
  178. else if (c >= 'A' && c <= 'Z') upper = true;
  179. else if (c < 33 || c > 126) return {};
  180. }
  181. if (lower && upper) return {};
  182. size_t pos = str.rfind('1');
  183. if (str.size() > 90 || pos == str.npos || pos == 0 || pos + 7 > str.size()) {
  184. return {};
  185. }
  186. data values(str.size() - 1 - pos);
  187. for (size_t i = 0; i < str.size() - 1 - pos; ++i) {
  188. unsigned char c = str[i + pos + 1];
  189. int8_t rev = CHARSET_REV[c];
  190. if (rev == -1) {
  191. return {};
  192. }
  193. values[i] = rev;
  194. }
  195. std::string hrp;
  196. for (size_t i = 0; i < pos; ++i) {
  197. hrp += LowerCase(str[i]);
  198. }
  199. Encoding result = VerifyChecksum(hrp, values);
  200. if (result == Encoding::INVALID) return {};
  201. return {result, std::move(hrp), data(values.begin(), values.end() - 6)};
  202. }
  203. } // namespace bech32