Skip to content

Commit 8a4cf9b

Browse files
updating comments for convertbits
1 parent 5d08b35 commit 8a4cf9b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/Functions/FunctionsBech32Representation.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace
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

2727
using 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+
*/
3042
template <int frombits, int tobits, bool pad>
3143
bool 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

Comments
 (0)