/ref/c/segwit_addr.c

https://github.com/sipa/bech32 · C · 209 lines · 178 code · 11 blank · 20 comment · 90 complexity · 3449b12f10f06f8437614bada3107703 MD5 · raw file

  1. /* Copyright (c) 2017, 2021 Pieter Wuille
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #include <assert.h>
  22. #include <stdlib.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include "segwit_addr.h"
  26. static uint32_t bech32_polymod_step(uint32_t pre) {
  27. uint8_t b = pre >> 25;
  28. return ((pre & 0x1FFFFFF) << 5) ^
  29. (-((b >> 0) & 1) & 0x3b6a57b2UL) ^
  30. (-((b >> 1) & 1) & 0x26508e6dUL) ^
  31. (-((b >> 2) & 1) & 0x1ea119faUL) ^
  32. (-((b >> 3) & 1) & 0x3d4233ddUL) ^
  33. (-((b >> 4) & 1) & 0x2a1462b3UL);
  34. }
  35. static uint32_t bech32_final_constant(bech32_encoding enc) {
  36. if (enc == BECH32_ENCODING_BECH32) return 1;
  37. if (enc == BECH32_ENCODING_BECH32M) return 0x2bc830a3;
  38. assert(0);
  39. }
  40. static const char* charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
  41. static const int8_t charset_rev[128] = {
  42. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  43. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  44. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  45. 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1,
  46. -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
  47. 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1,
  48. -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
  49. 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1
  50. };
  51. int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len, bech32_encoding enc) {
  52. uint32_t chk = 1;
  53. size_t i = 0;
  54. while (hrp[i] != 0) {
  55. int ch = hrp[i];
  56. if (ch < 33 || ch > 126) {
  57. return 0;
  58. }
  59. if (ch >= 'A' && ch <= 'Z') return 0;
  60. chk = bech32_polymod_step(chk) ^ (ch >> 5);
  61. ++i;
  62. }
  63. if (i + 7 + data_len > 90) return 0;
  64. chk = bech32_polymod_step(chk);
  65. while (*hrp != 0) {
  66. chk = bech32_polymod_step(chk) ^ (*hrp & 0x1f);
  67. *(output++) = *(hrp++);
  68. }
  69. *(output++) = '1';
  70. for (i = 0; i < data_len; ++i) {
  71. if (*data >> 5) return 0;
  72. chk = bech32_polymod_step(chk) ^ (*data);
  73. *(output++) = charset[*(data++)];
  74. }
  75. for (i = 0; i < 6; ++i) {
  76. chk = bech32_polymod_step(chk);
  77. }
  78. chk ^= bech32_final_constant(enc);
  79. for (i = 0; i < 6; ++i) {
  80. *(output++) = charset[(chk >> ((5 - i) * 5)) & 0x1f];
  81. }
  82. *output = 0;
  83. return 1;
  84. }
  85. bech32_encoding bech32_decode(char* hrp, uint8_t *data, size_t *data_len, const char *input) {
  86. uint32_t chk = 1;
  87. size_t i;
  88. size_t input_len = strlen(input);
  89. size_t hrp_len;
  90. int have_lower = 0, have_upper = 0;
  91. if (input_len < 8 || input_len > 90) {
  92. return BECH32_ENCODING_NONE;
  93. }
  94. *data_len = 0;
  95. while (*data_len < input_len && input[(input_len - 1) - *data_len] != '1') {
  96. ++(*data_len);
  97. }
  98. hrp_len = input_len - (1 + *data_len);
  99. if (1 + *data_len >= input_len || *data_len < 6) {
  100. return BECH32_ENCODING_NONE;
  101. }
  102. *(data_len) -= 6;
  103. for (i = 0; i < hrp_len; ++i) {
  104. int ch = input[i];
  105. if (ch < 33 || ch > 126) {
  106. return BECH32_ENCODING_NONE;
  107. }
  108. if (ch >= 'a' && ch <= 'z') {
  109. have_lower = 1;
  110. } else if (ch >= 'A' && ch <= 'Z') {
  111. have_upper = 1;
  112. ch = (ch - 'A') + 'a';
  113. }
  114. hrp[i] = ch;
  115. chk = bech32_polymod_step(chk) ^ (ch >> 5);
  116. }
  117. hrp[i] = 0;
  118. chk = bech32_polymod_step(chk);
  119. for (i = 0; i < hrp_len; ++i) {
  120. chk = bech32_polymod_step(chk) ^ (input[i] & 0x1f);
  121. }
  122. ++i;
  123. while (i < input_len) {
  124. int v = (input[i] & 0x80) ? -1 : charset_rev[(int)input[i]];
  125. if (input[i] >= 'a' && input[i] <= 'z') have_lower = 1;
  126. if (input[i] >= 'A' && input[i] <= 'Z') have_upper = 1;
  127. if (v == -1) {
  128. return BECH32_ENCODING_NONE;
  129. }
  130. chk = bech32_polymod_step(chk) ^ v;
  131. if (i + 6 < input_len) {
  132. data[i - (1 + hrp_len)] = v;
  133. }
  134. ++i;
  135. }
  136. if (have_lower && have_upper) {
  137. return BECH32_ENCODING_NONE;
  138. }
  139. if (chk == bech32_final_constant(BECH32_ENCODING_BECH32)) {
  140. return BECH32_ENCODING_BECH32;
  141. } else if (chk == bech32_final_constant(BECH32_ENCODING_BECH32M)) {
  142. return BECH32_ENCODING_BECH32M;
  143. } else {
  144. return BECH32_ENCODING_NONE;
  145. }
  146. }
  147. static int convert_bits(uint8_t* out, size_t* outlen, int outbits, const uint8_t* in, size_t inlen, int inbits, int pad) {
  148. uint32_t val = 0;
  149. int bits = 0;
  150. uint32_t maxv = (((uint32_t)1) << outbits) - 1;
  151. while (inlen--) {
  152. val = (val << inbits) | *(in++);
  153. bits += inbits;
  154. while (bits >= outbits) {
  155. bits -= outbits;
  156. out[(*outlen)++] = (val >> bits) & maxv;
  157. }
  158. }
  159. if (pad) {
  160. if (bits) {
  161. out[(*outlen)++] = (val << (outbits - bits)) & maxv;
  162. }
  163. } else if (((val << (outbits - bits)) & maxv) || bits >= inbits) {
  164. return 0;
  165. }
  166. return 1;
  167. }
  168. int segwit_addr_encode(char *output, const char *hrp, int witver, const uint8_t *witprog, size_t witprog_len) {
  169. uint8_t data[65];
  170. size_t datalen = 0;
  171. bech32_encoding enc = BECH32_ENCODING_BECH32;
  172. if (witver > 16) return 0;
  173. if (witver == 0 && witprog_len != 20 && witprog_len != 32) return 0;
  174. if (witprog_len < 2 || witprog_len > 40) return 0;
  175. if (witver > 0) enc = BECH32_ENCODING_BECH32M;
  176. data[0] = witver;
  177. convert_bits(data + 1, &datalen, 5, witprog, witprog_len, 8, 1);
  178. ++datalen;
  179. return bech32_encode(output, hrp, data, datalen, enc);
  180. }
  181. int segwit_addr_decode(int* witver, uint8_t* witdata, size_t* witdata_len, const char* hrp, const char* addr) {
  182. uint8_t data[84];
  183. char hrp_actual[84];
  184. size_t data_len;
  185. bech32_encoding enc = bech32_decode(hrp_actual, data, &data_len, addr);
  186. if (enc == BECH32_ENCODING_NONE) return 0;
  187. if (data_len == 0 || data_len > 65) return 0;
  188. if (strncmp(hrp, hrp_actual, 84) != 0) return 0;
  189. if (data[0] > 16) return 0;
  190. if (data[0] == 0 && enc != BECH32_ENCODING_BECH32) return 0;
  191. if (data[0] > 0 && enc != BECH32_ENCODING_BECH32M) return 0;
  192. *witdata_len = 0;
  193. if (!convert_bits(witdata, witdata_len, 8, data + 1, data_len - 1, 5, 0)) return 0;
  194. if (*witdata_len < 2 || *witdata_len > 40) return 0;
  195. if (data[0] == 0 && *witdata_len != 20 && *witdata_len != 32) return 0;
  196. *witver = data[0];
  197. return 1;
  198. }