-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbebi.h
289 lines (257 loc) · 7.13 KB
/
bebi.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
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
#ifndef __BEBI_H
#define __BEBI_H
/**
* BEBI stands for Big Endian Big Int
*
* It is meant to be used in a small-endian environment, specifically WASM-32
*
* It is a library mostly meant to support encoding/decoding of big-endian big-int values
* Only addition/subtraction and comparisons are supported for math (which is enough for quite a lot)
* Currently only supporting unsigned.
*
* c-file: bebi.c
* requires: string.h
*/
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* bebi type does not have a predefined size or alignment requirements
*/
typedef uint8_t bebi[];
/**
* Set/get functions allow extracting or setting a u8/16/32/64 from any offset inside the bebi
*/
inline void bebi_set_u8(bebi dst, size_t offset, uint8_t val);
inline void bebi_set_u16(bebi dst, size_t offset, uint16_t val);
inline void bebi_set_u32(bebi dst, size_t offset, uint32_t val);
inline void bebi_set_u64(bebi dst, size_t offset, uint64_t val);
inline uint8_t bebi_get_u8(const bebi src, size_t offset);
inline uint16_t bebi_get_u16(const bebi src, size_t offset);
inline uint32_t bebi_get_u32(const bebi src, size_t offset);
inline uint64_t bebi_get_u64(const bebi src, size_t offset);
/**
* Adds rhs into lhs. Lhs must be at least as long as rhs.
*
* return values:
* -1 : ERROR rhs_size > lhs_size
* 0 : O.k
* 1 : O.k but there was an overflow
*/
inline int bebi_add(bebi lhs, size_t lhs_size, const bebi rhs, size_t rhs_size);
/**
* Subtracts rhs from lhs. Lhs must be at least as long as rhs.
*
* return values:
* -1 : ERROR rhs_size > lhs_size
* 0 : O.k
* 1 : O.k but there was an overflow
*/
inline int bebi_sub(bebi lhs, size_t lhs_size, const bebi rhs, size_t rhs_size);
/**
* compares two values
*
* return values:
* lhs > rhs: 1
* lhs < rhs: -1
* lhs == rhs: 0
*/
inline int bebi_cmp(const bebi lhs, size_t lhs_size, const bebi rhs, size_t rhs_size);
inline bool bebi_is_zero(const bebi bebi, size_t size);
/**
* bebi32 is a specialized bebi of size 32, which is used a lot in solidity
* there are no alignment requirements.
*/
typedef uint8_t bebi32[32];
/**
* bebi32 get/set functions are different from generic bebi.
* according to solidity standards - they require the value to be stored in
* the last (least significant) bytes, padded by zeroes
*
* setting a bebi32 to e.g. uint32 will pad it with zeroes accordingly
*
* get functions don't test if value is padded - testing can be done separately
* by the bebi32_is_*
*
* notice you can use generic bebi variants for a bebi32 when they fit better
*/
uint16_t bebi32_get_u16(const bebi32 dst);
uint32_t bebi32_get_u32(const bebi32 dst);
uint64_t bebi32_get_u64(const bebi32 dst);
void bebi32_set_u8(bebi32 dst, uint8_t val);
void bebi32_set_u16(bebi32 dst, uint16_t val);
void bebi32_set_u32(bebi32 dst, uint32_t val);
void bebi32_set_u64(bebi32 dst, uint64_t val);
bool bebi32_is_u8(const bebi32 dst);
bool bebi32_is_u16(const bebi32 dst);
bool bebi32_is_u32(const bebi32 dst);
bool bebi32_is_u64(const bebi32 dst);
bool bebi32_is_u160(const bebi32 dst);
int bebi32_add(bebi32 lhs, const bebi32 rhs);
int bebi32_sub(bebi32 lhs, const bebi32 rhs);
int bebi32_add_u64(bebi32 lhs, uint64_t rhs);
int bebi32_cmp(const bebi32 lhs, const bebi32 rhs);
bool bebi32_is_zero(const bebi bebi);
/******* implementation of previously-declated functions *********/
inline int bebi_add(bebi lhs, size_t lhs_size, const bebi rhs, size_t rhs_size) {
if (rhs_size > lhs_size) {
return -1;
}
size_t left = lhs_size;
size_t right = rhs_size;
uint8_t carry = 0;
while (left > 0 && right > 0) {
left--;
right--;
uint8_t res = lhs[left] + rhs[right] + carry;
carry = res < lhs[left] ? 1 : 0;
lhs[left] = res;
}
while (left > 0 && carry) {
left--;
uint8_t res = lhs[left] + carry;
carry = res < lhs[left] ? 1 : 0;
lhs[left] = res;
}
return carry;
}
inline int bebi_sub(bebi lhs, size_t lhs_size, const bebi rhs, size_t rhs_size) {
if (rhs_size > lhs_size) {
return -1;
}
size_t left = lhs_size;
size_t right = rhs_size;
uint8_t carry = 0;
while (left > 0 && right > 0) {
left--;
right--;
uint8_t res = lhs[left] - rhs[right] - carry;
carry = res > lhs[left] ? 1 : 0;
lhs[left] = res;
}
while (left > 0 && carry) {
left--;
uint8_t res = lhs[left] - carry;
carry = res > lhs[left] ? 1 : 0;
lhs[left] = res;
}
return carry;
}
inline int bebi_cmp(const bebi lhs, size_t lhs_size, const bebi rhs, size_t rhs_size) {
size_t left = 0;
size_t right = 0;
while (lhs_size - left > rhs_size) {
if (lhs[left] != 0) {
return 1;
}
left++;
}
while (rhs_size - right > lhs_size) {
if (rhs[right] != 0) {
return -1;
}
right++;
}
while (left < lhs_size) {
if (lhs[left] > rhs[right]) {
return 1;
}
if (lhs[left] < rhs[right]) {
return -1;
}
right++;
left++;
}
return 0;
}
inline void bebi_set_u8(bebi dst, size_t offset, uint8_t val) {
dst[offset] = val;
}
inline bool bebi_is_zero(const bebi bebi, size_t size) {
size_t idx = 0;
while (idx < size) {
if (bebi[idx] != 0) {
return false;
}
idx++;
}
return true;
}
inline uint8_t bebi_get_u8(const bebi src, size_t offset) {
return src[offset];
}
inline void bebi_set_u16(bebi dst, size_t offset, uint16_t val) {
dst[offset+1] = val & 0xff;
val >>= 8;
dst[offset] = val & 0xff;
}
inline uint16_t bebi_get_u16(const bebi src, size_t offset) {
uint16_t val;
val = src[offset];
val <<= 8;
val |= src[offset+1];
return val;
}
inline void bebi_set_u32(bebi dst, size_t offset, uint32_t val) {
dst[offset+3] = val & 0xff;
val >>= 8;
dst[offset+2] = val & 0xff;
val >>= 8;
dst[offset+1] = val & 0xff;
val >>= 8;
dst[offset] = val & 0xff;
}
inline uint32_t bebi_get_u32(const bebi src, size_t offset) {
uint32_t val;
val = src[offset];
val <<= 8;
val |= src[offset+1];
val <<= 8;
val |= src[offset+2];
val <<= 8;
val |= src[offset+3];
return val;
}
inline void bebi_set_u64(bebi dst, size_t offset, uint64_t val) {
dst[offset+7] = val & 0xff;
val >>= 8;
dst[offset+6] = val & 0xff;
val >>= 8;
dst[offset+5] = val & 0xff;
val >>= 8;
dst[offset+4] = val & 0xff;
val >>= 8;
dst[offset+3] = val & 0xff;
val >>= 8;
dst[offset+2] = val & 0xff;
val >>= 8;
dst[offset+1] = val & 0xff;
val >>= 8;
dst[offset] = val & 0xff;
}
inline uint64_t bebi_get_u64(const bebi src, size_t offset) {
uint64_t val;
val = src[offset];
val <<= 8;
val |= src[offset+1];
val <<= 8;
val |= src[offset+2];
val <<= 8;
val |= src[offset+3];
val <<= 8;
val |= src[offset+4];
val <<= 8;
val |= src[offset+5];
val <<= 8;
val |= src[offset+6];
val <<= 8;
val |= src[offset+7];
return val;
}
#ifdef __cplusplus
}
#endif
#endif // __BEBI_H