-
Notifications
You must be signed in to change notification settings - Fork 2
/
num256.cpp
341 lines (301 loc) · 11.2 KB
/
num256.cpp
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
#include <string.h>
#include <set>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
using namespace boost::multiprecision;
using namespace std;
#define STRING_LEN 67 // 2 (for "0x") + 1 (for '\0') + 64 bytes for
// the 32 bytes of data encoded in hex
extern "C" {
// Output always normalized to have:
// - "0x" as prefix
// - lower case letters for the hex digits (a-f)
// - no leading zeros (i.e., "0xff", not "0x000ff")
// REVIEW: Lots of repetition, factor out in the future
const char* number_to_hex(int32_t x) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t result(x);
string str_result = result.str(32, std::ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
int32_t hex_to_number(const char *x) {
return (int32_t) uint256_t(x).template convert_to<int>();
}
// remove leading zeros if needed
const char* hex_normalized(const char *in_hex) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(in_hex);
string str_result = my_x.str(32, std::ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
int32_t max2(int32_t x, int32_t y) {
return x < y ? y : x;
}
int32_t min2(int32_t x, int32_t y) {
return x < y ? x : y;
}
int32_t cast_to_number(int32_t x) {
return x;
}
char* cast_to_symbol(char* x) {
return x;
}
set<string> from_chars(const char *x) {
set<string> strs;
boost::split(strs,x,boost::is_any_of("|"));
return strs;
}
int32_t in_set(const char *x, const char *y) {
string y_str(y);
set<string> strs = from_chars(x);
return strs.find(y_str) != strs.end();
}
const char* add_set(const char *x, const char *y) {
string y_str(y);
set<string> strs = from_chars(x);
if (strs.find(y_str) != strs.end()) return x;
strs.insert(y_str);
string joinedString = boost::algorithm::join(strs, "|");
char* out = new char[joinedString.length() + 1];
strcpy(out, joinedString.c_str());
return out;
}
int32_t len_set(const char *x) {
char c = *x;
int32_t res = c != 0;
do {
res += c == '|';
} while ((c = *(x++)) != 0);
return res;
}
const char* add_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = my_x + my_y;
string str_result = result.str(32, std::ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* sub_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = my_x - my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* mul_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = my_x * my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* div_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = my_x / my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* mod_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = my_x % my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* and_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = my_x & my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* or_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = my_x | my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* xor_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = my_x ^ my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* gt_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = my_x > my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* eq_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = my_x == my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* lt_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = my_x < my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
// Note use of max 256 int for modulo base
const char* exp_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t my_y(y);
uint256_t result = powm(my_x, my_y, numeric_limits<uint256_t>::max());
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
// The next operations are for signed arithmetic, otherwise
// identical to above. Note the need to transform the result to
// an unsigned int before converting to hex, since hex-conversion
// for negative numbers is not supported by the Boost
// multiprecision library.
const char* smod_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
int256_t my_x(x);
int256_t my_y(y);
int256_t result = my_x % my_y;
uint256_t unsigned_result(result);
string str_result = unsigned_result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* sdiv_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
int256_t my_x(x);
int256_t my_y(y);
int256_t result = my_x / my_y;
uint256_t unsigned_result(result);
string str_result = unsigned_result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* sgt_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
int256_t my_x(x);
int256_t my_y(y);
int256_t result = my_x > my_y;
uint256_t unsigned_result(result);
string str_result = unsigned_result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* slt_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
int256_t my_x(x);
int256_t my_y(y);
int256_t result = my_x < my_y;
uint256_t unsigned_result(result);
string str_result = unsigned_result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
/// Bitwise operations, per EIP-145
// NOTE: INVERTED ARGUMENT ORDER!
const char* shl_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(y);
long my_y = strtol(x, NULL, 16);
uint256_t result = my_x << my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
// NOTE: INVERTED ARGUMENT ORDER!
const char* shr_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(y);
long my_y = strtol(x, NULL, 16);
uint256_t result = my_x >> my_y;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
// NOTE: INVERTED ARGUMENT ORDER!
const char* sar_256(const char *x, const char *y) {
thread_local static char out[STRING_LEN] = {"0x"};
int256_t my_x(y);
int256_t minus_zero = ((int256_t) 1) << 255;
int256_t sign_bit = my_x & minus_zero;
long my_y = strtol(x, NULL, 16);
int256_t result;
if (my_y >= 256) {
if (sign_bit) result = -1;
else result = 0;
} else { // there must be a better way to implement SAR w/ Boost, but who cares?
for (result = my_x; my_y > 0; my_y--) {
result = (result >> 1) | sign_bit;
}
}
uint256_t unsigned_result(result);
string str_result = unsigned_result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
const char* not_256(const char *x) {
thread_local static char out[STRING_LEN] = {"0x"};
uint256_t my_x(x);
uint256_t result = ~my_x;
string str_result = result.str(32, ios_base::hex);
transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
strcpy(out+2, str_result.c_str());
return out;
}
}