Bitcoin Core  29.1.0
P2P Digital Currency
base_encode_decode.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2022 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 <test/fuzz/fuzz.h>
6 
7 #include <base58.h>
8 #include <psbt.h>
9 #include <span.h>
11 #include <util/strencodings.h>
12 #include <util/string.h>
13 
14 #include <cassert>
15 #include <string>
16 #include <vector>
17 #include <ranges>
18 
20 
21 FUZZ_TARGET(base58_encode_decode)
22 {
23  FuzzedDataProvider provider{buffer.data(), buffer.size()};
24  const auto random_string{provider.ConsumeRandomLengthString(100)};
25 
26  const auto encoded{EncodeBase58(MakeUCharSpan(random_string))};
27  const auto decode_input{provider.ConsumeBool() ? random_string : encoded};
28  const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, decode_input.size() + 1)};
29  if (std::vector<unsigned char> decoded; DecodeBase58(decode_input, decoded, max_ret_len)) {
30  const auto encoded_string{EncodeBase58(decoded)};
31  assert(encoded_string == TrimStringView(decode_input));
32  if (decoded.size() > 0) {
33  assert(max_ret_len > 0);
34  assert(decoded.size() <= static_cast<size_t>(max_ret_len));
35  assert(!DecodeBase58(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
36  }
37  }
38 }
39 
40 FUZZ_TARGET(base58check_encode_decode)
41 {
42  FuzzedDataProvider provider{buffer.data(), buffer.size()};
43  const auto random_string{provider.ConsumeRandomLengthString(100)};
44 
45  const auto encoded{EncodeBase58Check(MakeUCharSpan(random_string))};
46  const auto decode_input{provider.ConsumeBool() ? random_string : encoded};
47  const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, decode_input.size() + 1)};
48  if (std::vector<unsigned char> decoded; DecodeBase58Check(decode_input, decoded, max_ret_len)) {
49  const auto encoded_string{EncodeBase58Check(decoded)};
50  assert(encoded_string == TrimStringView(decode_input));
51  if (decoded.size() > 0) {
52  assert(max_ret_len > 0);
53  assert(decoded.size() <= static_cast<size_t>(max_ret_len));
54  assert(!DecodeBase58Check(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
55  }
56  }
57 }
58 
59 FUZZ_TARGET(base32_encode_decode)
60 {
61  const std::string random_string{buffer.begin(), buffer.end()};
62 
63  // Decode/Encode roundtrip
64  if (auto result{DecodeBase32(random_string)}) {
65  const auto encoded_string{EncodeBase32(*result)};
66  assert(encoded_string == ToLower(TrimStringView(random_string)));
67  }
68  // Encode/Decode roundtrip
69  const auto encoded{EncodeBase32(buffer)};
70  const auto decoded{DecodeBase32(encoded)};
71  assert(decoded && std::ranges::equal(*decoded, buffer));
72 }
73 
74 FUZZ_TARGET(base64_encode_decode)
75 {
76  const std::string random_string{buffer.begin(), buffer.end()};
77 
78  // Decode/Encode roundtrip
79  if (auto result{DecodeBase64(random_string)}) {
80  const auto encoded_string{EncodeBase64(*result)};
81  assert(encoded_string == TrimStringView(random_string));
82  }
83  // Encode/Decode roundtrip
84  const auto encoded{EncodeBase64(buffer)};
85  const auto decoded{DecodeBase64(encoded)};
86  assert(decoded && std::ranges::equal(*decoded, buffer));
87 }
88 
89 FUZZ_TARGET(psbt_base64_decode)
90 {
91  const std::string random_string{buffer.begin(), buffer.end()};
92 
94  std::string error;
95  const bool ok{DecodeBase64PSBT(psbt, random_string, error)};
96  assert(ok == error.empty());
97 }
assert(!tx.IsCoinBase())
std::string EncodeBase64(Span< const unsigned char > input)
std::string EncodeBase58(Span< const unsigned char > input)
Why base-58 instead of standard base-64 encoding?
Definition: base58.cpp:89
A version of CTransaction with the PSBT format.
Definition: psbt.h:950
FUZZ_TARGET(base58_encode_decode)
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \\\)
Definition: string.h:146
bool DecodeBase64PSBT(PartiallySignedTransaction &psbt, const std::string &base64_tx, std::string &error)
Decode a base64ed PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:536
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)
std::string ConsumeRandomLengthString(size_t max_length)
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
std::string EncodeBase58Check(Span< const unsigned char > input)
Encode a byte span into a base58-encoded string, including checksum.
Definition: base58.cpp:137
auto result
Definition: common-types.h:74
static bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
Definition: base58.cpp:40
std::optional< std::vector< unsigned char > > DecodeBase32(std::string_view str)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:296
static bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
Definition: base58.cpp:146
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.