forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuint256.h
78 lines (66 loc) · 2.31 KB
/
uint256.h
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
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
#pragma once
#include "Data.h"
#include <boost/lexical_cast.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace TW {
using uint128_t = boost::multiprecision::uint128_t;
using int256_t = boost::multiprecision::int256_t;
using uint256_t = boost::multiprecision::uint256_t;
/// Loads a `uint256_t` from a collection of bytes.
/// The rightmost bytes are taken from data
inline uint256_t load(const Data& data) {
using boost::multiprecision::cpp_int;
if (data.empty()) {
return uint256_t(0);
}
uint256_t result;
import_bits(result, data.begin(), data.end());
return result;
}
/// Loads a `uint256_t` from Protobuf bytes (which are wrongly represented as
/// std::string).
inline uint256_t load(const std::string& data) {
using boost::multiprecision::cpp_int;
if (data.empty()) {
return uint256_t(0);
}
uint256_t result;
import_bits(result, reinterpret_cast<const byte*>(data.data()),
reinterpret_cast<const byte*>(data.data() + data.size()));
return result;
}
/// Stores a `uint256_t` as a collection of bytes, with optional padding (typically to 32 bytes).
/// If minLen is given (non-zero), and result is shorter, it is padded (with zeroes, on the left, big endian)
inline Data store(const uint256_t& v, byte minLen = 0) {
using boost::multiprecision::cpp_int;
Data bytes;
bytes.reserve(32);
export_bits(v, std::back_inserter(bytes), 8);
if (minLen && bytes.size() < minLen) {
Data padded(minLen - bytes.size());
append(padded, bytes);
return padded;
}
return bytes;
}
// Append a uint256_t value as a big-endian byte array into the provided buffer, and limit
// the array size by digit/8.
inline void encode256BE(Data& data, const uint256_t& value, uint32_t digit) {
Data bytes = store(value);
Data buff(digit / 8);
for (int i = 0; i < (int)bytes.size(); ++i) {
int start = (int)buff.size() - (int)bytes.size() + i;
if (start >= 0) {
buff[start] = bytes[i];
}
}
data.insert(data.end(), buff.begin(), buff.end());
}
/// Return string representation of uint256_t
inline std::string toString(uint256_t value) {
return boost::lexical_cast<std::string>(value);
}
} // namespace TW