1111namespace
1212{
1313
14- /* Max length of Bech32 or Bech32m encoding is 90 chars, this includes:
14+ /* * Max length of Bech32 or Bech32m encoding is 90 chars, this includes:
1515 *
1616 * HRP: 1 - 83 human readable characters, 'bc' or 'tb' for a SegWit address
1717 * separator: always '1'
@@ -26,7 +26,19 @@ constexpr size_t max_hrp_len = 83; // Note: if we only support segwit addresses,
2626
2727using bech32_data = std::vector<uint8_t >;
2828
29- /* * Convert from one power-of-2 number base to another. */
29+ /* * Convert from one power-of-2 number base to another.
30+ *
31+ * Function will convert a input vector of <frombits>-bit data to an output vector of <tobit>-bit data,
32+ * padding the result if <pad> is true.
33+ *
34+ * Example:
35+ * Input: 10010110 11001011 (8-bit numbers)
36+ * Output: 10010 11011 00101 10000 (5-bit numbers)
37+ *
38+ * The last 4 "extra" 0s in the output are padding, they will only be added if <pad> is true.
39+ * If <pad> is false, no padding will be added and the function will return false if there are bits
40+ * left over
41+ */
3042template <int frombits, int tobits, bool pad>
3143bool convertbits (bech32_data & out, const bech32_data & in)
3244{
@@ -46,9 +58,10 @@ bool convertbits(bech32_data & out, const bech32_data & in)
4658 }
4759 if (pad)
4860 {
49- if (bits)
61+ if (bits) // pad leftover bits with 0s and push to 'out'
5062 out.push_back ((acc << (tobits - bits)) & maxv);
5163 }
64+ // if pad == false: sanity check, then check if there are significant (non-0) leftover bits
5265 else if (bits >= frombits || ((acc << (tobits - bits)) & maxv))
5366 {
5467 return false ;
0 commit comments