-
Notifications
You must be signed in to change notification settings - Fork 0
/
numbers.hpp
369 lines (333 loc) · 11 KB
/
numbers.hpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#pragma once
#include <boost/multiprecision/cpp_int.hpp>
namespace vxldollar
{
using uint128_t = boost::multiprecision::uint128_t;
using uint256_t = boost::multiprecision::uint256_t;
using uint512_t = boost::multiprecision::uint512_t;
// SI dividers
//
vxldollar::uint128_t const Gxrb_ratio = vxldollar::uint128_t ("1000000000000000000000000000000"); // 10^30
vxldollar::uint128_t const Mxrb_ratio = vxldollar::uint128_t ("1000000000000000000000000000"); // 10^27
vxldollar::uint128_t const kxrb_ratio = vxldollar::uint128_t ("1000000000000000000000000"); // 10^24
vxldollar::uint128_t const xrb_ratio = vxldollar::uint128_t ("1000000000000000000000"); // 10^21
vxldollar::uint128_t const raw_ratio = vxldollar::uint128_t ("1"); // 10^0
class uint128_union
{
public:
uint128_union () = default;
/**
* Decode from hex string
* @warning Aborts at runtime if the input is invalid
*/
uint128_union (std::string const &);
uint128_union (uint64_t);
uint128_union (vxldollar::uint128_t const &);
bool operator== (vxldollar::uint128_union const &) const;
bool operator!= (vxldollar::uint128_union const &) const;
bool operator< (vxldollar::uint128_union const &) const;
bool operator> (vxldollar::uint128_union const &) const;
void encode_hex (std::string &) const;
bool decode_hex (std::string const &);
void encode_dec (std::string &) const;
bool decode_dec (std::string const &, bool = false);
bool decode_dec (std::string const &, vxldollar::uint128_t);
std::string format_balance (vxldollar::uint128_t scale, int precision, bool group_digits) const;
std::string format_balance (vxldollar::uint128_t scale, int precision, bool group_digits, std::locale const & locale) const;
vxldollar::uint128_t number () const;
void clear ();
bool is_zero () const;
std::string to_string () const;
std::string to_string_dec () const;
union
{
std::array<uint8_t, 16> bytes;
std::array<char, 16> chars;
std::array<uint32_t, 4> dwords;
std::array<uint64_t, 2> qwords;
};
};
static_assert (std::is_nothrow_move_constructible<uint128_union>::value, "uint128_union should be noexcept MoveConstructible");
// Balances are 128 bit.
class amount : public uint128_union
{
public:
using uint128_union::uint128_union;
};
class raw_key;
class uint256_union
{
public:
uint256_union () = default;
/**
* Decode from hex string
* @warning Aborts at runtime if the input is invalid
*/
explicit uint256_union (std::string const &);
uint256_union (uint64_t);
uint256_union (vxldollar::uint256_t const &);
void encrypt (vxldollar::raw_key const &, vxldollar::raw_key const &, uint128_union const &);
uint256_union & operator^= (vxldollar::uint256_union const &);
uint256_union operator^ (vxldollar::uint256_union const &) const;
bool operator== (vxldollar::uint256_union const &) const;
bool operator!= (vxldollar::uint256_union const &) const;
bool operator< (vxldollar::uint256_union const &) const;
void encode_hex (std::string &) const;
bool decode_hex (std::string const &);
void encode_dec (std::string &) const;
bool decode_dec (std::string const &);
void clear ();
bool is_zero () const;
std::string to_string () const;
vxldollar::uint256_t number () const;
union
{
std::array<uint8_t, 32> bytes;
std::array<char, 32> chars;
std::array<uint32_t, 8> dwords;
std::array<uint64_t, 4> qwords;
std::array<uint128_union, 2> owords;
};
};
static_assert (std::is_nothrow_move_constructible<uint256_union>::value, "uint256_union should be noexcept MoveConstructible");
class link;
class root;
class hash_or_account;
// All keys and hashes are 256 bit.
class block_hash final : public uint256_union
{
public:
using uint256_union::uint256_union;
operator vxldollar::link const & () const;
operator vxldollar::root const & () const;
operator vxldollar::hash_or_account const & () const;
};
class public_key final : public uint256_union
{
public:
using uint256_union::uint256_union;
public_key ();
static const public_key & null ();
std::string to_node_id () const;
bool decode_node_id (std::string const & source_a);
void encode_account (std::string &) const;
std::string to_account () const;
bool decode_account (std::string const &);
operator vxldollar::link const & () const;
operator vxldollar::root const & () const;
operator vxldollar::hash_or_account const & () const;
bool operator== (std::nullptr_t) const;
bool operator!= (std::nullptr_t) const;
using uint256_union::operator==;
using uint256_union::operator!=;
};
class wallet_id : public uint256_union
{
using uint256_union::uint256_union;
};
// These are synonymous
using account = public_key;
class hash_or_account
{
public:
hash_or_account ();
hash_or_account (uint64_t value_a);
bool is_zero () const;
void clear ();
std::string to_string () const;
bool decode_hex (std::string const &);
bool decode_account (std::string const &);
std::string to_account () const;
vxldollar::account const & as_account () const;
vxldollar::block_hash const & as_block_hash () const;
operator vxldollar::uint256_union const & () const;
bool operator== (vxldollar::hash_or_account const &) const;
bool operator!= (vxldollar::hash_or_account const &) const;
union
{
std::array<uint8_t, 32> bytes;
vxldollar::uint256_union raw; // This can be used when you don't want to explicitly mention either of the types
vxldollar::account account;
vxldollar::block_hash hash;
};
};
// A link can either be a destination account or source hash
class link final : public hash_or_account
{
public:
using hash_or_account::hash_or_account;
};
// A root can either be an open block hash or a previous hash
class root final : public hash_or_account
{
public:
using hash_or_account::hash_or_account;
vxldollar::block_hash const & previous () const;
};
// The seed or private key
class raw_key final : public uint256_union
{
public:
using uint256_union::uint256_union;
~raw_key ();
void decrypt (vxldollar::uint256_union const &, vxldollar::raw_key const &, uint128_union const &);
};
class uint512_union
{
public:
uint512_union () = default;
uint512_union (vxldollar::uint256_union const &, vxldollar::uint256_union const &);
uint512_union (vxldollar::uint512_t const &);
bool operator== (vxldollar::uint512_union const &) const;
bool operator!= (vxldollar::uint512_union const &) const;
vxldollar::uint512_union & operator^= (vxldollar::uint512_union const &);
void encode_hex (std::string &) const;
bool decode_hex (std::string const &);
void clear ();
bool is_zero () const;
vxldollar::uint512_t number () const;
std::string to_string () const;
union
{
std::array<uint8_t, 64> bytes;
std::array<uint32_t, 16> dwords;
std::array<uint64_t, 8> qwords;
std::array<uint256_union, 2> uint256s;
};
};
static_assert (std::is_nothrow_move_constructible<uint512_union>::value, "uint512_union should be noexcept MoveConstructible");
class signature : public uint512_union
{
public:
using uint512_union::uint512_union;
};
class qualified_root : public uint512_union
{
public:
using uint512_union::uint512_union;
vxldollar::root const & root () const
{
return reinterpret_cast<vxldollar::root const &> (uint256s[0]);
}
vxldollar::block_hash const & previous () const
{
return reinterpret_cast<vxldollar::block_hash const &> (uint256s[1]);
}
};
vxldollar::signature sign_message (vxldollar::raw_key const &, vxldollar::public_key const &, vxldollar::uint256_union const &);
vxldollar::signature sign_message (vxldollar::raw_key const &, vxldollar::public_key const &, uint8_t const *, size_t);
bool validate_message (vxldollar::public_key const &, vxldollar::uint256_union const &, vxldollar::signature const &);
bool validate_message (vxldollar::public_key const &, uint8_t const *, size_t, vxldollar::signature const &);
bool validate_message_batch (unsigned char const **, size_t *, unsigned char const **, unsigned char const **, size_t, int *);
vxldollar::raw_key deterministic_key (vxldollar::raw_key const &, uint32_t);
vxldollar::public_key pub_key (vxldollar::raw_key const &);
/* Conversion methods */
std::string to_string_hex (uint64_t const);
std::string to_string_hex (uint16_t const);
bool from_string_hex (std::string const &, uint64_t &);
/**
* Convert a double to string in fixed format
* @param precision_a (optional) use a specific precision (default is the maximum)
*/
std::string to_string (double const, int const precision_a = std::numeric_limits<double>::digits10);
namespace difficulty
{
uint64_t from_multiplier (double const, uint64_t const);
double to_multiplier (uint64_t const, uint64_t const);
}
}
namespace std
{
template <>
struct hash<::vxldollar::uint256_union>
{
size_t operator() (::vxldollar::uint256_union const & data_a) const
{
return data_a.qwords[0] + data_a.qwords[1] + data_a.qwords[2] + data_a.qwords[3];
}
};
template <>
struct hash<::vxldollar::account>
{
size_t operator() (::vxldollar::account const & data_a) const
{
return hash<::vxldollar::uint256_union> () (data_a);
}
};
template <>
struct hash<::vxldollar::block_hash>
{
size_t operator() (::vxldollar::block_hash const & data_a) const
{
return hash<::vxldollar::uint256_union> () (data_a);
}
};
template <>
struct hash<::vxldollar::raw_key>
{
size_t operator() (::vxldollar::raw_key const & data_a) const
{
return hash<::vxldollar::uint256_union> () (data_a);
}
};
template <>
struct hash<::vxldollar::root>
{
size_t operator() (::vxldollar::root const & data_a) const
{
return hash<::vxldollar::uint256_union> () (data_a);
}
};
template <>
struct hash<::vxldollar::wallet_id>
{
size_t operator() (::vxldollar::wallet_id const & data_a) const
{
return hash<::vxldollar::uint256_union> () (data_a);
}
};
template <>
struct hash<::vxldollar::uint256_t>
{
size_t operator() (::vxldollar::uint256_t const & number_a) const
{
return number_a.convert_to<size_t> ();
}
};
template <>
struct hash<::vxldollar::uint512_union>
{
size_t operator() (::vxldollar::uint512_union const & data_a) const
{
return hash<::vxldollar::uint256_union> () (data_a.uint256s[0]) + hash<::vxldollar::uint256_union> () (data_a.uint256s[1]);
}
};
template <>
struct hash<::vxldollar::qualified_root>
{
size_t operator() (::vxldollar::qualified_root const & data_a) const
{
return hash<::vxldollar::uint512_union> () (data_a);
}
};
template <>
struct equal_to<std::reference_wrapper<::vxldollar::block_hash const>>
{
bool operator() (std::reference_wrapper<::vxldollar::block_hash const> const & lhs, std::reference_wrapper<::vxldollar::block_hash const> const & rhs) const
{
return lhs.get () == rhs.get ();
}
};
}
namespace boost
{
template <>
struct hash<std::reference_wrapper<::vxldollar::block_hash const>>
{
size_t operator() (std::reference_wrapper<::vxldollar::block_hash const> const & hash_a) const
{
std::hash<::vxldollar::block_hash> hash;
return hash (hash_a);
}
};
}