Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
bech32.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <bech32.h>
6#include <test/fuzz/fuzz.h>
8#include <test/util/str.h>
9#include <util/strencodings.h>
10
11#include <cassert>
12#include <cstdint>
13#include <string>
14#include <vector>
15
17{
19 FuzzedDataProvider fdp(buffer.data(), buffer.size());
20 auto random_string = fdp.ConsumeRandomLengthString(limit + 1);
22
23 if (decoded.hrp.empty()) {
25 assert(decoded.data.empty());
26 } else {
28 auto reencoded = bech32::Encode(decoded.encoding, decoded.hrp, decoded.data);
30 }
31}
32
33// https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki and https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki
35{
36 std::string hrp;
37 size_t length = fdp.ConsumeIntegralInRange<size_t>(1, 83);
38 for (size_t i = 0; i < length; ++i) {
39 // Generate lowercase ASCII characters in ([33-126] - ['A'-'Z']) range
40 char c = fdp.ConsumeBool()
41 ? fdp.ConsumeIntegralInRange<char>(33, 'A' - 1)
42 : fdp.ConsumeIntegralInRange<char>('Z' + 1, 126);
43 hrp += c;
44 }
45 return hrp;
46}
47
49{
50 FuzzedDataProvider fdp(buffer.data(), buffer.size());
51 auto hrp = GenerateRandomHRP(fdp);
52
53 auto input_chars = fdp.ConsumeBytes<unsigned char>(fdp.ConsumeIntegralInRange<size_t>(0, 82));
54 std::vector<uint8_t> converted_input;
55 ConvertBits<8, 5, true>([&](auto c) { converted_input.push_back(c); }, input_chars.begin(), input_chars.end());
56
57 auto size = converted_input.size() + hrp.length() + std::string({bech32::SEPARATOR}).size() + bech32::CHECKSUM_SIZE;
58 if (size <= bech32::CharLimit::BECH32) {
60 auto encoded = bech32::Encode(encoding, hrp, converted_input);
61 assert(!encoded.empty());
62
63 const auto decoded = bech32::Decode(encoded);
64 assert(decoded.encoding == encoding);
65 assert(decoded.hrp == hrp);
67 }
68 }
69}
#define FUZZ_TARGET(...)
Definition fuzz.h:35
@ INVALID
Failed decoding.
@ BECH32
Bech32 encoding as defined in BIP173.
@ BECH32M
Bech32m encoding as defined in BIP350.
@ BECH32
BIP173/350 imposed character limit for Bech32(m) encoded addresses. This guarantees finding up to 4 e...
Definition bech32.h:39
DecodeResult Decode(const std::string &str, CharLimit limit)
Decode a Bech32 or Bech32m string.
Definition bech32.cpp:374
std::string Encode(Encoding encoding, const std::string &hrp, const data &values)
Encode a Bech32 or Bech32m string.
Definition bech32.cpp:358
static constexpr size_t CHECKSUM_SIZE
Definition bech32.h:24
static constexpr char SEPARATOR
Definition bech32.h:25
bool CaseInsensitiveEqual(const std::string &s1, const std::string &s2)
Definition str.cpp:10
std::string GenerateRandomHRP(FuzzedDataProvider &fdp)
Definition bech32.cpp:34
std::string random_string(uint32_t length)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73
assert(!tx.IsCoinBase())