Skip to content

Commit

Permalink
Merge bitcoin#30596: fuzz: replace hardcoded numbers for bech32 limits
Browse files Browse the repository at this point in the history
59c0ece fuzz: replace hardcoded numbers for bech32 limits (josibake)

Pull request description:

  Follow-up to bitcoin#30047 to replace a hardcoded value that was missed in the original PR

ACKs for top commit:
  paplorinc:
    ACK 59c0ece
  dergoegge:
    utACK 59c0ece
  marcofleon:
    ACK 59c0ece. Ran the test a bit to be sure, lgtm.
  brunoerg:
    utACK 59c0ece

Tree-SHA512: 89799928feb6752a533259117340b087ff7299f9bf204b165dd87708e15b99a338521f2ac9f9e1fd91dc48b93be839059768d9e68b172e36328232174d1dfa3f
  • Loading branch information
glozow committed Aug 6, 2024
2 parents 43740f4 + 59c0ece commit 31a3ff5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
3 changes: 0 additions & 3 deletions src/bech32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ namespace

typedef std::vector<uint8_t> data;

/** The Bech32 and Bech32m checksum size */
constexpr size_t CHECKSUM_SIZE = 6;

/** The Bech32 and Bech32m character set for encoding. */
const char* CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";

Expand Down
3 changes: 3 additions & 0 deletions src/bech32.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
namespace bech32
{

/** The Bech32 and Bech32m checksum size */
constexpr size_t CHECKSUM_SIZE = 6;

enum class Encoding {
INVALID, //!< Failed decoding

Expand Down
5 changes: 3 additions & 2 deletions src/test/fuzz/bech32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ FUZZ_TARGET(bech32)
std::vector<unsigned char> input;
ConvertBits<8, 5, true>([&](unsigned char c) { input.push_back(c); }, buffer.begin(), buffer.end());

if (input.size() + 3 + 6 <= 90) {
// If it's possible to encode input in Bech32(m) without exceeding the 90-character limit:
// Input data part + 3 characters for the HRP and separator (bc1) + the checksum characters
if (input.size() + 3 + bech32::CHECKSUM_SIZE <= bech32::CharLimit::BECH32) {
// If it's possible to encode input in Bech32(m) without exceeding the bech32-character limit:
for (auto encoding : {bech32::Encoding::BECH32, bech32::Encoding::BECH32M}) {
const std::string encoded = bech32::Encode(encoding, "bc", input);
assert(!encoded.empty());
Expand Down

0 comments on commit 31a3ff5

Please sign in to comment.