Merge 5667b0d758 into merged_master (Bitcoin PR #16792)

This commit is contained in:
Andrew Poelstra 2020-11-09 21:20:21 +00:00
commit f8386055b5
2 changed files with 8 additions and 2 deletions

View file

@ -4,6 +4,8 @@
#include <bech32.h>
#include <assert.h>
namespace bech32
{
@ -58,7 +60,7 @@ uint32_t PolyMod(const data& v)
// During the course of the loop below, `c` contains the bitpacked coefficients of the
// polynomial constructed from just the values of v that were processed so far, mod g(x). In
// the above example, `c` initially corresponds to 1 mod (x), and after processing 2 inputs of
// the above example, `c` initially corresponds to 1 mod g(x), and after processing 2 inputs of
// v, it corresponds to x^2 + v0*x + v1 mod g(x). As 1 mod g(x) = 1, that is the starting value
// for `c`.
uint32_t c = 1;
@ -140,6 +142,10 @@ data CreateChecksum(const std::string& hrp, const data& values)
/** Encode a Bech32 string. */
std::string Encode(const std::string& hrp, const data& values) {
// First ensure that the HRP is all lowercase. BIP-173 requires an encoder
// to return a lowercase Bech32 string, but if given an uppercase HRP, the
// result will always be invalid.
for (const char& c : hrp) assert(c < 'A' || c > 'Z');
data checksum = CreateChecksum(hrp, values);
data combined = Cat(values, checksum);
std::string ret = hrp + '1';

View file

@ -19,7 +19,7 @@
namespace bech32
{
/** Encode a Bech32 string. Returns the empty string in case of failure. */
/** Encode a Bech32 string. If hrp contains uppercase characters, this will cause an assertion error. */
std::string Encode(const std::string& hrp, const std::vector<uint8_t>& values);
/** Decode a Bech32 string. Returns (hrp, data). Empty hrp means failure. */