aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/codec/base58/base58.cpp
blob: ba974f96a381f88fab19896b5702fa3f74426b4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* (C) 2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/base58.h>
#include <botan/exceptn.h>
#include <botan/bigint.h>
#include <botan/divide.h>
#include <botan/loadstor.h>
#include <botan/hash.h>

namespace Botan {

namespace {

uint32_t sha256_d_checksum(const uint8_t input[], size_t input_length)
   {
   std::unique_ptr<HashFunction> sha256 = HashFunction::create_or_throw("SHA-256");

   std::vector<uint8_t> checksum(32);

   sha256->update(input, input_length);
   sha256->final(checksum);

   sha256->update(checksum);
   sha256->final(checksum);

   checksum.resize(4);
   uint32_t c = load_be<uint32_t>(checksum.data(), 0);
   return c;
   }

class Character_Table
   {
   public:
      // This must be a literal constant
      Character_Table(const char* alphabet) :
         m_alphabet(alphabet)
         {
         const size_t alpha_len = std::strlen(alphabet);

         // 128 or up would flow into 0x80 invalid bit
         if(alpha_len == 0 || alpha_len >= 128)
            throw Invalid_Argument("Bad Character_Table string");

         m_alphabet_len = static_cast<uint8_t>(alpha_len);

         set_mem(m_tab, 256, 0x80);

         for(size_t i = 0; m_alphabet[i]; ++i)
            {
            const uint8_t b = static_cast<uint8_t>(m_alphabet[i]);
            BOTAN_ASSERT(m_tab[b] == 0x80, "No duplicate chars");
            m_tab[b] = static_cast<uint8_t>(i);
            }
         }

      uint8_t radix() const { return m_alphabet_len; }

      char operator[](size_t i) const
         {
         BOTAN_ASSERT(i < m_alphabet_len, "Character in range");
         return m_alphabet[i];
         }

      uint8_t code_for(char c) const
         {
         return m_tab[static_cast<uint8_t>(c)];
         }

   private:
      const char* m_alphabet;
      uint8_t m_alphabet_len;
      uint8_t m_tab[256];
   };

static const Character_Table& BASE58_ALPHA()
   {
   static const Character_Table base58_alpha("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz");
   return base58_alpha;
   }

std::string base58_encode(BigInt v, size_t leading_zeros)
   {
   const auto base58 = BASE58_ALPHA();

   std::string result;
   BigInt q;
   uint8_t r;

   while(v.is_nonzero())
      {
      ct_divide_u8(v, base58.radix(), q, r);
      result.push_back(base58[r]);
      v.swap(q);
      }

   for(size_t i = 0; i != leading_zeros; ++i)
      result.push_back(base58[0]);

   return std::string(result.rbegin(), result.rend());
   }

template<typename T, typename Z>
size_t count_leading_zeros(const T input[], size_t input_length, Z zero)
   {
   size_t leading_zeros = 0;

   while(leading_zeros < input_length && input[leading_zeros] == zero)
      leading_zeros += 1;

   return leading_zeros;
   }

}

std::string base58_encode(const uint8_t input[], size_t input_length)
   {
   BigInt v(input, input_length);
   return base58_encode(v, count_leading_zeros(input, input_length, 0));
   }

std::string base58_check_encode(const uint8_t input[], size_t input_length)
   {
   BigInt v(input, input_length);
   v <<= 32;
   v += sha256_d_checksum(input, input_length);
   return base58_encode(v, count_leading_zeros(input, input_length, 0));
   }

std::vector<uint8_t> base58_decode(const char input[], size_t input_length)
   {
   const auto base58 = BASE58_ALPHA();

   const size_t leading_zeros = count_leading_zeros(input, input_length, base58[0]);

   BigInt v;

   for(size_t i = leading_zeros; i != input_length; ++i)
      {
      size_t idx = base58.code_for(input[i]);

      if(idx == 0x80)
         throw Decoding_Error("Invalid base58");

      v *= base58.radix();
      v += idx;
      }

   std::vector<uint8_t> output(v.bytes() + leading_zeros);
   v.binary_encode(output.data() + leading_zeros);
   return output;
   }

std::vector<uint8_t> base58_check_decode(const char input[], size_t input_length)
   {
   std::vector<uint8_t> dec = base58_decode(input, input_length);

   if(dec.size() < 4)
      throw Decoding_Error("Invalid base58 too short for checksum");

   const uint32_t computed_checksum = sha256_d_checksum(dec.data(), dec.size() - 4);
   const uint32_t checksum = load_be<uint32_t>(&dec[dec.size()-4], 0);

   if(checksum != computed_checksum)
      throw Decoding_Error("Invalid base58 checksum");

   dec.resize(dec.size() - 4);

   return dec;
   }

}