nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

bech32.c (8062B)


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