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 24, 2024
1 parent 165fa8a commit e168e62
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 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 effectiveLength = input.size() - leadingZeros;
std::vector<int64_t> inputAsLongs((effectiveLength + (groupSize - 1)) / groupSize);
int groupOffset = ((groupSize - (effectiveLength % groupSize)) % groupSize) - leadingZeros;
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 + (GROUP_SIZE - 1)) / GROUP_SIZE);
int groupOffset = ((GROUP_SIZE - (effectiveLength % GROUP_SIZE)) % GROUP_SIZE) - leadingZeros;

for (auto i = leadingZeros; i < input.size(); ++i) {
auto index = (groupOffset + i) / groupSize;
for (size_t i = leadingZeros; i < input.size(); ++i) {
int index = (groupOffset + i) / GROUP_SIZE;
inputAsLongs[index] <<= 8;
inputAsLongs[index] |= input[i];
}
Expand All @@ -106,25 +106,25 @@ 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');

auto inputAsLongs = ConvertToLongs(input, leadingZeros);
for (auto i = 0U; i < inputAsLongs.size();) {
for (auto i{0U}; i < inputAsLongs.size();) {
int64_t remainder{0L};
for (auto j = 0U; j < inputAsLongs.size(); ++j) {
auto accumulator = (remainder << (groupSize * 8)) | inputAsLongs[j];
for (auto j{0U}; j < inputAsLongs.size(); ++j) {
auto accumulator = (remainder << (GROUP_SIZE * 8)) | inputAsLongs[j];
inputAsLongs[j] = accumulator / 58;
remainder = accumulator % 58;
}
result += pszBase58[remainder];
result += PSZ_BASE58[remainder];
if (inputAsLongs[i] == 0) ++i;
}
std::reverse(result.begin() + leadingZeros, result.end());
Expand Down

0 comments on commit e168e62

Please sign in to comment.