├── README.md ├── bech32.h ├── bech32.c └── tests.c /README.md: -------------------------------------------------------------------------------- 1 | # Reference C code for Bech32 encoded keys 2 | 3 | https://gist.github.com/jonasschnelli/68a2a5a5a5b796dc9992f432e794d719 4 | 5 | 6 | ## Compile 7 | 8 | gcc -O0 -g bech32.c tests.c -o test 9 | 10 | -------------------------------------------------------------------------------- /bech32.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2017 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 | 22 | #ifndef _BECH32_H_ 23 | #define _BECH32_H_ 1 24 | 25 | #include 26 | 27 | /** Encode a Bech32 string 28 | * 29 | * Out: output: Pointer to a buffer of size strlen(hrp) + data_len + 8 that 30 | * will be updated to contain the null-terminated Bech32 string. 31 | * In: hrp : Pointer to the null-terminated human readable part. 32 | * data : Pointer to an array of 5-bit values. 33 | * data_len: Length of the data array. 34 | * Returns 1 if successful. 35 | */ 36 | int bech32_encode( 37 | char *output, 38 | const char *hrp, 39 | const uint8_t *data, 40 | size_t data_len 41 | ); 42 | 43 | /** Decode a Bech32 string 44 | * 45 | * Out: hrp: Pointer to a buffer of size strlen(input) - 6. Will be 46 | * updated to contain the null-terminated human readable part. 47 | * data: Pointer to a buffer of size strlen(input) - 8 that will 48 | * hold the encoded 5-bit data values. 49 | * data_len: Pointer to a size_t that will be updated to be the number 50 | * of entries in data. 51 | * In: input: Pointer to a null-terminated Bech32 string. 52 | * Returns 1 if succesful. 53 | */ 54 | int bech32_decode( 55 | char *hrp, 56 | uint8_t *data, 57 | size_t *data_len, 58 | const char *input 59 | ); 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /bech32.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2017 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 22 | #include 23 | #include 24 | 25 | #include "bech32.h" 26 | 27 | uint32_t bech32_polymod_step(uint32_t pre) { 28 | uint8_t b = pre >> 25; 29 | return ((pre & 0x1FFFFFF) << 5) ^ 30 | (-((b >> 0) & 1) & 0x3b6a57b2UL) ^ 31 | (-((b >> 1) & 1) & 0x26508e6dUL) ^ 32 | (-((b >> 2) & 1) & 0x1ea119faUL) ^ 33 | (-((b >> 3) & 1) & 0x3d4233ddUL) ^ 34 | (-((b >> 4) & 1) & 0x2a1462b3UL); 35 | } 36 | 37 | static const char* charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; 38 | 39 | static const int8_t charset_rev[128] = { 40 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 41 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 42 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 43 | 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, 44 | -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, 45 | 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -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 | }; 49 | 50 | int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len) { 51 | uint32_t chk = 1; 52 | size_t i = 0; 53 | while (hrp[i] != 0) { 54 | int ch = hrp[i]; 55 | if (ch < 33 || ch > 126) { 56 | return 0; 57 | } 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 ^= 1; 79 | for (i = 0; i < 6; ++i) { 80 | *(output++) = charset[(chk >> ((5 - i) * 5)) & 0x1f]; 81 | } 82 | *output = 0; 83 | return 1; 84 | } 85 | 86 | int bech32_decode(char* hrp, uint8_t *data, size_t *data_len, const char *input) { 87 | uint32_t chk = 1; 88 | size_t i; 89 | size_t input_len = strlen(input); 90 | size_t hrp_len; 91 | int have_lower = 0, have_upper = 0; 92 | if (input_len < 8) { 93 | return 0; 94 | } 95 | *data_len = 0; 96 | while (*data_len < input_len && input[(input_len - 1) - *data_len] != '1') { 97 | ++(*data_len); 98 | } 99 | hrp_len = input_len - (1 + *data_len); 100 | if (hrp_len < 1 || *data_len < 6) { 101 | return 0; 102 | } 103 | *(data_len) -= 6; 104 | for (i = 0; i < hrp_len; ++i) { 105 | int ch = input[i]; 106 | if (ch < 33 || ch > 126) { 107 | return 0; 108 | } 109 | if (ch >= 'a' && ch <= 'z') { 110 | have_lower = 1; 111 | } else if (ch >= 'A' && ch <= 'Z') { 112 | have_upper = 1; 113 | ch = (ch - 'A') + 'a'; 114 | } 115 | hrp[i] = ch; 116 | chk = bech32_polymod_step(chk) ^ (ch >> 5); 117 | } 118 | hrp[i] = 0; 119 | chk = bech32_polymod_step(chk); 120 | for (i = 0; i < hrp_len; ++i) { 121 | chk = bech32_polymod_step(chk) ^ (input[i] & 0x1f); 122 | } 123 | ++i; 124 | while (i < input_len) { 125 | int v = (input[i] & 0x80) ? -1 : charset_rev[(int)input[i]]; 126 | if (input[i] >= 'a' && input[i] <= 'z') have_lower = 1; 127 | if (input[i] >= 'A' && input[i] <= 'Z') have_upper = 1; 128 | if (v == -1) { 129 | return 0; 130 | } 131 | chk = bech32_polymod_step(chk) ^ v; 132 | if (i + 6 < input_len) { 133 | data[i - (1 + hrp_len)] = v; 134 | } 135 | ++i; 136 | } 137 | if (have_lower && have_upper) { 138 | return 0; 139 | } 140 | return chk == 1; 141 | } 142 | 143 | -------------------------------------------------------------------------------- /tests.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "bech32.h" 7 | 8 | static int convert_bits(uint8_t* out, size_t* outlen, int outbits, const uint8_t* in, size_t inlen, int inbits, int pad) { 9 | uint32_t val = 0; 10 | int bits = 0; 11 | uint32_t maxv = (((uint32_t)1) << outbits) - 1; 12 | while (inlen--) { 13 | val = (val << inbits) | *(in++); 14 | bits += inbits; 15 | while (bits >= outbits) { 16 | bits -= outbits; 17 | out[(*outlen)++] = (val >> bits) & maxv; 18 | } 19 | } 20 | if (pad) { 21 | if (bits) { 22 | out[(*outlen)++] = (val << (outbits - bits)) & maxv; 23 | } 24 | } else if (((val << (outbits - bits)) & maxv) || bits >= inbits) { 25 | return 0; 26 | } 27 | return 1; 28 | } 29 | 30 | static void append_bits(uint8_t* data_out, size_t* pos_in_bit_in_out, int32_t data_in, int in_bits) { 31 | uint32_t mask = (((uint32_t)1) << in_bits) - 1; 32 | int in_pos = 0; 33 | while (in_pos < in_bits) { 34 | data_out[*pos_in_bit_in_out / 8] |= ((data_in & mask) >> in_pos << (*pos_in_bit_in_out % 8)); 35 | int written = (in_bits - in_pos) > (8-(*pos_in_bit_in_out % 8)) ? (8-(*pos_in_bit_in_out % 8)) : (in_bits - in_pos); 36 | in_pos += written; 37 | *pos_in_bit_in_out+= written; 38 | } 39 | } 40 | 41 | int xpriv(uint8_t *privkey, uint8_t *chaincode, uint32_t birthday, uint32_t gap_limit_multiplier, uint8_t script_type) { 42 | char hrp[32] = "xp"; 43 | uint8_t data_in[128] = {}; 44 | size_t data_in_len = 128; 45 | size_t pos = 0; 46 | append_bits(data_in, &pos, 0, 1); 47 | append_bits(data_in, &pos, birthday, 15); 48 | append_bits(data_in, &pos, gap_limit_multiplier, 9); 49 | append_bits(data_in, &pos, script_type, 8); 50 | for(int i = 0; i<32;i++) { 51 | append_bits(data_in, &pos, privkey[i], 8); 52 | } 53 | for(int i = 0; i<32;i++) { 54 | append_bits(data_in, &pos, chaincode[i], 8); 55 | } 56 | uint8_t data5[128] = {}; 57 | size_t data5_len = 0; 58 | convert_bits(data5, &data5_len, 5, data_in, (int)ceil(pos/8.0), 8, 1); 59 | 60 | char bech32_str[128]; 61 | if (!bech32_encode(bech32_str, hrp, data5, data5_len)) { 62 | printf("Encode failed\n"); 63 | } 64 | printf("Bech32 encoded string: %s\n", bech32_str); 65 | 66 | uint8_t dblcheck5[100] = {}; 67 | size_t dblcheck5_len = 0; 68 | if (!bech32_decode(hrp, dblcheck5, &dblcheck5_len, bech32_str)) { 69 | printf("bech32_decode fails: '%s'\n", bech32_str); 70 | } 71 | uint8_t dblcheck8[100] = {}; 72 | size_t dblcheck8_len = 0; 73 | convert_bits(dblcheck8, &dblcheck8_len, 8, dblcheck5, dblcheck5_len, 5, 1); 74 | if (memcmp(dblcheck8, data_in, (int)ceil(pos/8.0)) != 0) { 75 | printf("Failed\n"); 76 | return 1; 77 | } 78 | int c_version = (dblcheck8[0] & 1); 79 | uint32_t c_birthday = (dblcheck8[0] & 0xFE) >> 1; 80 | c_birthday |= (dblcheck8[1] & 0xFF) << 7; 81 | uint32_t c_gap_limit = dblcheck8[2]; 82 | c_gap_limit |= (dblcheck8[3] & 0x1) << 8; 83 | uint32_t c_script_type = (dblcheck8[3] & 0xFE) >> 1; 84 | c_script_type |= (dblcheck8[4] & 1) << 7; 85 | printf("Version: %d\n", c_version); 86 | printf("Birthday: %d\n", c_birthday); 87 | printf("Gap-limit-multiplier: %d\n", c_gap_limit); 88 | printf("Resulting Gap-limit: %d\n", (c_gap_limit+1)*100); 89 | printf("script_type: %d\n", c_script_type); 90 | printf("===================\n"); 91 | return 0; 92 | } 93 | 94 | int wif(uint8_t *privkey, uint32_t birthday, uint8_t script_type) { 95 | char hrp[32] = "pk"; 96 | uint8_t data_in[128] = {}; 97 | size_t data_in_len = 128; 98 | size_t pos = 0; 99 | append_bits(data_in, &pos, 0, 1); 100 | append_bits(data_in, &pos, birthday, 15); 101 | append_bits(data_in, &pos, script_type, 8); 102 | for(int i = 0; i<32;i++) { 103 | append_bits(data_in, &pos, privkey[i], 8); 104 | } 105 | uint8_t data5[128] = {}; 106 | size_t data5_len = 0; 107 | convert_bits(data5, &data5_len, 5, data_in, (int)ceil(pos/8.0), 8, 1); 108 | 109 | char bech32_str[128] = {0}; 110 | if (!bech32_encode(bech32_str, hrp, data5, data5_len)) { 111 | printf("Encode failed\n"); 112 | } 113 | printf("Bech32 encoded string: %s\n", bech32_str); 114 | 115 | uint8_t dblcheck5[100] = {}; 116 | size_t dblcheck5_len = 0; 117 | if (!bech32_decode(hrp, dblcheck5, &dblcheck5_len, bech32_str)) { 118 | printf("bech32_decode fails: '%s'\n", bech32_str); 119 | } 120 | uint8_t dblcheck8[100] = {}; 121 | size_t dblcheck8_len = 0; 122 | convert_bits(dblcheck8, &dblcheck8_len, 8, dblcheck5, dblcheck5_len, 5, 1); 123 | if (memcmp(dblcheck8, data_in, (int)ceil(pos/8.0)) != 0) { 124 | printf("Failed\n"); 125 | return 1; 126 | } 127 | int c_version = (dblcheck8[0] & 1); 128 | uint32_t c_birthday = (dblcheck8[0] & 0xFE) >> 1; 129 | c_birthday |= (dblcheck8[1] & 0xFF) << 7; 130 | uint32_t c_script_type = dblcheck8[2]; 131 | printf("Version: %d\n", c_version); 132 | printf("Birthday: %d\n", c_birthday); 133 | printf("script_type: %d\n", c_script_type); 134 | return 0; 135 | } 136 | 137 | int main(void) { 138 | uint8_t privkey[32] = {0x71, 0x54, 0x70, 0x43, 0x29, 0xd1, 0x17, 0x25, 0xd1, 0xbf, 0x5a, 0x6d, 139 | 0x44, 0x9c, 0x80, 0xdf, 0xb9, 0x3f, 0xf2, 0x27, 0xa0, 0x7d, 0xac, 0x75, 140 | 0xb9, 0x78, 0x88, 0xe8, 0x56, 0x84, 0x7e, 0xb5}; 141 | uint8_t chaincode[32] = {0x50, 0x1a, 0x4f, 0x15, 0x2d, 0x1d, 0xb6, 0xb8, 0x48, 0xdb, 0x6e, 0xe2, 142 | 0x05, 0xfa, 0x18, 0xee, 0x5c, 0x6d, 0x25, 0x02, 0x3d, 0x7c, 0xec, 0x47, 143 | 0x59, 0x87, 0x8f, 0x1a, 0x46, 0x57, 0x82, 0x8c}; 144 | 145 | //birthday: monday, 7. July 2014 (2011 days since january 3th 2009) 146 | //gap-limit-mp 10 = (10 + 1) * 100 == gaplimit of 1100 147 | //script type restriction to P2PKH compressed 148 | xpriv(privkey, chaincode, 2011, 10, 1); 149 | 150 | xpriv(privkey, chaincode, 0, 0, 0); //3rd Jan 2009, glm of 0, no script type restrictions 151 | xpriv(privkey, chaincode, 32767, 511, 255); // Saturday, September 2098 152 | wif(privkey, 32767, 255); 153 | } 154 | --------------------------------------------------------------------------------