Skip to content

Commit

Permalink
Apply code conventions to modified code areas
Browse files Browse the repository at this point in the history
  • Loading branch information
Lőrinc committed Feb 25, 2024
1 parent d0c9ba9 commit b930cd5
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include <vector>

/** All alphanumeric characters except for "0", "I", "O", and "l" */
static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
static const int8_t mapBase58[256] = {
static const char* PSZ_BASE58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
static const int8_t MAP_BASE58[256] = {
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
Expand All @@ -38,10 +38,10 @@ static const int8_t mapBase58[256] = {
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
};

static constexpr int baseScale{1000};
static const int log58_256Ratio = ceil((std::log(58) / std::log(256)) * baseScale); // 733
static const int log256_58Ratio = ceil((std::log(256) / std::log(58)) * baseScale); // 1366
static constexpr int groupSize = 7; // Group bytes into longs, considering that remainder has to fit in front
static constexpr int BASE_SCALE{1000};
static const int LOG_58_256_RATIO = ceil((std::log(58) / std::log(256)) * BASE_SCALE); // 733
static const int LOG_256_58_RATIO = ceil((std::log(256) / std::log(58)) * BASE_SCALE); // 1366
static constexpr int GROUP_SIZE = 7; // Group bytes into longs, considering that remainder has to fit in front

[[nodiscard]] static bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch, int max_ret_len)
{
Expand All @@ -56,13 +56,13 @@ static constexpr int groupSize = 7; // Group bytes into longs, considering that
if (zeroes > max_ret_len) return false;
psz++;
}
int size = 1 + strlen(psz) * log58_256Ratio / baseScale;
int size = 1 + strlen(psz) * LOG_58_256_RATIO / BASE_SCALE;
std::vector<unsigned char> b256(size);
// Process the characters.
static_assert(std::size(mapBase58) == 256, "mapBase58.size() should be 256"); // guarantee not out of range
static_assert(std::size(MAP_BASE58) == 256, "MAP_BASE58.size() should be 256"); // guarantee not out of range
while (*psz && !IsSpace(*psz)) {
// Decode base58 character
int carry = mapBase58[(uint8_t)*psz];
int carry = MAP_BASE58[(uint8_t)*psz];
if (carry == -1) // Invalid b58 character
return false;
int i = 0;
Expand Down Expand Up @@ -90,13 +90,13 @@ static constexpr int groupSize = 7; // Group bytes into longs, considering that
return true;
}

auto ConvertToLongs(const Span<const unsigned char>& input, size_t leadingZeros) -> std::vector<int64_t> {
auto ConvertToLongs(const Span<const unsigned char>& input, int leadingZeros) -> std::vector<int64_t> {
int effectiveLength = input.size() - leadingZeros;
std::vector<int64_t> inputAsLongs((effectiveLength + (groupSize - 1)) / groupSize);
int groupOffset = ((groupSize - (effectiveLength % groupSize)) % groupSize) - leadingZeros;
std::vector<int64_t> inputAsLongs((effectiveLength + (GROUP_SIZE - 1)) / GROUP_SIZE);
int groupOffset = ((GROUP_SIZE - (effectiveLength % GROUP_SIZE)) % GROUP_SIZE) - leadingZeros;

for (auto i = leadingZeros; i < input.size(); ++i) {
int index = (groupOffset + static_cast<int>(i)) / groupSize;
for (size_t i = leadingZeros; i < input.size(); ++i) {
int index = (groupOffset + static_cast<int>(i)) / GROUP_SIZE;
inputAsLongs[index] <<= 8;
inputAsLongs[index] |= input[i];
}
Expand All @@ -106,12 +106,12 @@ auto ConvertToLongs(const Span<const unsigned char>& input, size_t leadingZeros)

std::string EncodeBase58(Span<const unsigned char> input)
{
auto leadingZeros = 0U;
auto leadingZeros{0U};
while (leadingZeros < input.size() && input[leadingZeros] == 0) {
++leadingZeros;
}

auto size = 1 + input.size() * log256_58Ratio / baseScale;
auto size = 1 + input.size() * LOG_256_58_RATIO / BASE_SCALE;
std::string result;
result.reserve(leadingZeros + size);
result.assign(leadingZeros, '1');
Expand All @@ -124,7 +124,7 @@ std::string EncodeBase58(Span<const unsigned char> input)
inputAsLongs[j] = accumulator / 58;
remainder = accumulator % 58;
}
result += pszBase58[remainder];
result += PSZ_BASE58[remainder];
if (inputAsLongs[i] == 0) {
++i; // Skip new leading zeros
}
Expand Down

0 comments on commit b930cd5

Please sign in to comment.