segwit_addr.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <stdlib.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include "segwit_addr.h"
  25. static uint32_t bech32_polymod_step(uint32_t pre) {
  26. uint8_t b = pre >> 25;
  27. return ((pre & 0x1FFFFFF) << 5) ^ (-((b >> 0) & 1) & 0x3b6a57b2UL) ^
  28. (-((b >> 1) & 1) & 0x26508e6dUL) ^ (-((b >> 2) & 1) & 0x1ea119faUL) ^
  29. (-((b >> 3) & 1) & 0x3d4233ddUL) ^ (-((b >> 4) & 1) & 0x2a1462b3UL);
  30. }
  31. static uint32_t bech32_final_constant(bech32_encoding enc) {
  32. if(enc == BECH32_ENCODING_BECH32) return 1;
  33. if(enc == BECH32_ENCODING_BECH32M) return 0x2bc830a3;
  34. return 0;
  35. }
  36. static const char* charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
  37. static const int8_t charset_rev[128] = {
  38. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  39. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  40. -1, -1, -1, -1, 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, -1, 29,
  41. -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, 1, 0, 3, 16, 11, 28, 12, 14,
  42. 6, 4, 2, -1, -1, -1, -1, -1, -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27,
  43. 19, -1, 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1};
  44. int bech32_encode(
  45. char* output,
  46. const char* hrp,
  47. const uint8_t* data,
  48. size_t data_len,
  49. bech32_encoding enc) {
  50. uint32_t chk = 1;
  51. size_t i = 0;
  52. while(hrp[i] != 0) {
  53. int ch = hrp[i];
  54. if(ch < 33 || ch > 126) {
  55. return 0;
  56. }
  57. if(ch >= 'A' && ch <= 'Z') return 0;
  58. chk = bech32_polymod_step(chk) ^ (ch >> 5);
  59. ++i;
  60. }
  61. if(i + 7 + data_len > 90) return 0;
  62. chk = bech32_polymod_step(chk);
  63. while(*hrp != 0) {
  64. chk = bech32_polymod_step(chk) ^ (*hrp & 0x1f);
  65. *(output++) = *(hrp++);
  66. }
  67. *(output++) = '1';
  68. for(i = 0; i < data_len; ++i) {
  69. if(*data >> 5) return 0;
  70. chk = bech32_polymod_step(chk) ^ (*data);
  71. *(output++) = charset[*(data++)];
  72. }
  73. for(i = 0; i < 6; ++i) {
  74. chk = bech32_polymod_step(chk);
  75. }
  76. chk ^= bech32_final_constant(enc);
  77. for(i = 0; i < 6; ++i) {
  78. *(output++) = charset[(chk >> ((5 - i) * 5)) & 0x1f];
  79. }
  80. *output = 0;
  81. return 1;
  82. }
  83. bech32_encoding bech32_decode(char* hrp, uint8_t* data, size_t* data_len, const char* input) {
  84. uint32_t chk = 1;
  85. size_t i = 0;
  86. size_t input_len = strlen(input);
  87. size_t hrp_len = 0;
  88. int have_lower = 0, have_upper = 0;
  89. if(input_len < 8) {
  90. return BECH32_ENCODING_NONE;
  91. }
  92. *data_len = 0;
  93. while(*data_len < input_len && input[(input_len - 1) - *data_len] != '1') {
  94. ++(*data_len);
  95. }
  96. hrp_len = input_len - (1 + *data_len);
  97. if(1 + *data_len >= input_len || *data_len < 6 || hrp_len > BECH32_MAX_HRP_LEN) {
  98. return BECH32_ENCODING_NONE;
  99. }
  100. *(data_len) -= 6;
  101. for(i = 0; i < hrp_len; ++i) {
  102. int ch = input[i];
  103. if(ch < 33 || ch > 126) {
  104. return BECH32_ENCODING_NONE;
  105. }
  106. if(ch >= 'a' && ch <= 'z') {
  107. have_lower = 1;
  108. } else if(ch >= 'A' && ch <= 'Z') {
  109. have_upper = 1;
  110. ch = (ch - 'A') + 'a';
  111. }
  112. hrp[i] = ch;
  113. chk = bech32_polymod_step(chk) ^ (ch >> 5);
  114. }
  115. hrp[i] = 0;
  116. chk = bech32_polymod_step(chk);
  117. for(i = 0; i < hrp_len; ++i) {
  118. chk = bech32_polymod_step(chk) ^ (input[i] & 0x1f);
  119. }
  120. ++i;
  121. while(i < input_len) {
  122. int v = (input[i] & 0x80) ? -1 : charset_rev[(int)input[i]];
  123. if(input[i] >= 'a' && input[i] <= 'z') have_lower = 1;
  124. if(input[i] >= 'A' && input[i] <= 'Z') have_upper = 1;
  125. if(v == -1) {
  126. return BECH32_ENCODING_NONE;
  127. }
  128. chk = bech32_polymod_step(chk) ^ v;
  129. if(i + 6 < input_len) {
  130. data[i - (1 + hrp_len)] = v;
  131. }
  132. ++i;
  133. }
  134. if(have_lower && have_upper) {
  135. return BECH32_ENCODING_NONE;
  136. }
  137. if(chk == bech32_final_constant(BECH32_ENCODING_BECH32)) {
  138. return BECH32_ENCODING_BECH32;
  139. } else if(chk == bech32_final_constant(BECH32_ENCODING_BECH32M)) {
  140. return BECH32_ENCODING_BECH32M;
  141. } else {
  142. return BECH32_ENCODING_NONE;
  143. }
  144. }
  145. static int convert_bits(
  146. uint8_t* out,
  147. size_t* outlen,
  148. int outbits,
  149. const uint8_t* in,
  150. size_t inlen,
  151. int inbits,
  152. int pad) {
  153. uint32_t val = 0;
  154. int bits = 0;
  155. uint32_t maxv = (((uint32_t)1) << outbits) - 1;
  156. while(inlen--) {
  157. val = (val << inbits) | *(in++);
  158. bits += inbits;
  159. while(bits >= outbits) {
  160. bits -= outbits;
  161. out[(*outlen)++] = (val >> bits) & maxv;
  162. }
  163. }
  164. if(pad) {
  165. if(bits) {
  166. out[(*outlen)++] = (val << (outbits - bits)) & maxv;
  167. }
  168. } else if(((val << (outbits - bits)) & maxv) || bits >= inbits) {
  169. return 0;
  170. }
  171. return 1;
  172. }
  173. int segwit_addr_encode(
  174. char* output,
  175. const char* hrp,
  176. int witver,
  177. const uint8_t* witprog,
  178. size_t witprog_len) {
  179. uint8_t data[65] = {0};
  180. size_t datalen = 0;
  181. bech32_encoding enc = BECH32_ENCODING_BECH32;
  182. if(witver > 16) return 0;
  183. if(witver == 0 && witprog_len != 20 && witprog_len != 32) return 0;
  184. if(witprog_len < 2 || witprog_len > 40) return 0;
  185. if(witver > 0) enc = BECH32_ENCODING_BECH32M;
  186. data[0] = witver;
  187. convert_bits(data + 1, &datalen, 5, witprog, witprog_len, 8, 1);
  188. ++datalen;
  189. return bech32_encode(output, hrp, data, datalen, enc);
  190. }
  191. int segwit_addr_decode(
  192. int* witver,
  193. uint8_t* witdata,
  194. size_t* witdata_len,
  195. const char* hrp,
  196. const char* addr) {
  197. uint8_t data[84] = {0};
  198. char hrp_actual[84] = {0};
  199. size_t data_len = 0;
  200. if(strlen(addr) > 90) return 0;
  201. bech32_encoding enc = bech32_decode(hrp_actual, data, &data_len, addr);
  202. if(enc == BECH32_ENCODING_NONE) return 0;
  203. if(data_len == 0 || data_len > 65) return 0;
  204. if(strncmp(hrp, hrp_actual, 84) != 0) return 0;
  205. if(data[0] > 16) return 0;
  206. if(data[0] == 0 && enc != BECH32_ENCODING_BECH32) return 0;
  207. if(data[0] > 0 && enc != BECH32_ENCODING_BECH32M) return 0;
  208. *witdata_len = 0;
  209. if(!convert_bits(witdata, witdata_len, 8, data + 1, data_len - 1, 5, 0)) return 0;
  210. if(*witdata_len < 2 || *witdata_len > 40) return 0;
  211. if(data[0] == 0 && *witdata_len != 20 && *witdata_len != 32) return 0;
  212. *witver = data[0];
  213. return 1;
  214. }