-
Notifications
You must be signed in to change notification settings - Fork 26
/
U256.move
442 lines (378 loc) · 13.1 KB
/
U256.move
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
address StarcoinFramework {
/// Helper module to do u64 arith.
module Arith {
use StarcoinFramework::Errors;
const ERR_INVALID_CARRY: u64 = 301;
const ERR_INVALID_BORROW: u64 = 302;
const P32: u64 = 0x100000000;
const P64: u128 = 0x10000000000000000;
spec module {
pragma verify = true;
pragma aborts_if_is_strict;
}
/// split u64 to (high, low)
public fun split_u64(i: u64): (u64, u64) {
(i >> 32, i & 0xFFFFFFFF)
}
spec split_u64 {
pragma verify = false;
pragma opaque; // MVP cannot reason about bitwise operation
ensures [abstract] result_1 == i / P32;
ensures [abstract] result_2 == i % P32;
}
/// combine (high, low) to u64,
/// any lower bits of `high` will be erased, any higher bits of `low` will be erased.
public fun combine_u64(hi: u64, lo: u64): u64 {
(hi << 32) | (lo & 0xFFFFFFFF)
}
spec combine_u64 {
pragma verify = false;
pragma opaque = true; // MVP cannot reason about bitwise operation
let hi_32 = hi % P32;
let lo_32 = lo % P32;
ensures [abstract] result == hi_32 * P32 + lo_32;
}
/// a + b, with carry
public fun adc(a: u64, b: u64, carry: &mut u64) : u64 {
assert!(*carry <= 1, Errors::invalid_argument(ERR_INVALID_CARRY));
let (a1, a0) = split_u64(a);
let (b1, b0) = split_u64(b);
let (c, r0) = split_u64(a0 + b0 + *carry);
let (c, r1) = split_u64(a1 + b1 + c);
*carry = c;
combine_u64(r1, r0)
}
spec adc {
// Carry has either to be 0 or 1
aborts_if !(carry == 0 || carry == 1);
ensures carry == 0 || carry == 1;
// Result with or without carry
ensures carry == 0 ==> result == a + b + old(carry);
ensures carry == 1 ==> P64 + result == a + b + old(carry);
}
/// a - b, with borrow
public fun sbb(a: u64, b: u64, borrow: &mut u64): u64 {
assert!(*borrow <= 1, Errors::invalid_argument(ERR_INVALID_BORROW));
let (a1, a0) = split_u64(a);
let (b1, b0) = split_u64(b);
let (b, r0) = split_u64(P32 + a0 - b0 - *borrow);
let borrowed = 1 - b;
let (b, r1) = split_u64(P32 + a1 - b1 - borrowed);
*borrow = 1 - b;
combine_u64(r1, r0)
}
spec sbb {
// Borrow has either to be 0 or 1
aborts_if !(borrow == 0 || borrow == 1);
ensures borrow == 0 || borrow == 1;
// Result with or without borrow
ensures borrow == 0 ==> result == a - b - old(borrow);
ensures borrow == 1 ==> result == P64 + a - b - old(borrow);
}
}
/// Implementation u256.
module U256 {
spec module {
pragma verify = true;
}
use StarcoinFramework::Vector;
use StarcoinFramework::Errors;
const WORD: u8 = 4;
const P32: u64 = 0x100000000;
const P64: u128 = 0x10000000000000000;
const ERR_INVALID_LENGTH: u64 = 100;
const ERR_OVERFLOW: u64 = 200;
/// use vector to represent data.
/// so that we can use buildin vector ops later to construct U256.
/// vector should always has two elements.
struct U256 has copy, drop, store {
/// little endian representation
bits: vector<u64>,
}
spec U256 {
invariant len(bits) == 4;
}
spec fun value_of_U256(a: U256): num {
a.bits[0] +
a.bits[1] * P64 +
a.bits[2] * P64 * P64 +
a.bits[3] * P64 * P64 * P64
}
public fun zero(): U256 {
from_u128(0u128)
}
public fun one(): U256 {
from_u128(1u128)
}
public fun from_u64(v: u64): U256 {
from_u128((v as u128))
}
public fun from_u128(v: u128): U256 {
let low = ((v & 0xffffffffffffffff) as u64);
let high = ((v >> 64) as u64);
let bits = Vector::singleton(low);
Vector::push_back(&mut bits, high);
Vector::push_back(&mut bits, 0u64);
Vector::push_back(&mut bits, 0u64);
U256 { bits }
}
spec from_u128 {
pragma verify = false;
pragma opaque; // Original function has bitwise operator
ensures value_of_U256(result) == v;
}
#[test]
fun test_from_u128() {
// 2^64 + 1
let v = from_u128(18446744073709551617u128);
assert!(*Vector::borrow(&v.bits, 0) == 1, 0);
assert!(*Vector::borrow(&v.bits, 1) == 1, 1);
assert!(*Vector::borrow(&v.bits, 2) == 0, 2);
assert!(*Vector::borrow(&v.bits, 3) == 0, 3);
}
public fun from_big_endian(data: vector<u8>): U256 {
// TODO: define error code.
assert!(Vector::length(&data) <= 32, Errors::invalid_argument(ERR_INVALID_LENGTH));
from_bytes(&data, true)
}
spec from_big_endian {
pragma verify = false; // TODO: How to interpret the value of vector data of bytes
}
public fun from_little_endian(data: vector<u8>): U256 {
// TODO: define error code.
assert!(Vector::length(&data) <= 32, Errors::invalid_argument(ERR_INVALID_LENGTH));
from_bytes(&data, false)
}
spec from_little_endian {
pragma verify = false; // TODO: How to interpret the value of vector data of bytes
}
public fun to_u128(v: &U256): u128 {
assert!(*Vector::borrow(&v.bits, 3) == 0, Errors::invalid_state(ERR_OVERFLOW));
assert!(*Vector::borrow(&v.bits, 2) == 0, Errors::invalid_state(ERR_OVERFLOW));
((*Vector::borrow(&v.bits, 1) as u128) << 64) | (*Vector::borrow(&v.bits, 0) as u128)
}
spec to_u128 {
pragma verify = false;
pragma opaque; // Original function has bitwise operator
aborts_if value_of_U256(v) >= P64 * P64;
ensures value_of_U256(v) == result;
}
#[test]
fun test_to_u128() {
// 2^^128 - 1
let i = 340282366920938463463374607431768211455u128;
let v = from_u128(i);
assert!(to_u128(&v) == i, 128);
}
#[test]
#[expected_failure]
fun test_to_u128_overflow() {
// 2^^128 - 1
let i = 340282366920938463463374607431768211455u128;
let v = from_u128(i);
let v = add(v, one());
to_u128(&v);
}
const EQUAL: u8 = 0;
const LESS_THAN: u8 = 1;
const GREATER_THAN: u8 = 2;
public fun compare(a: &U256, b: &U256): u8 {
let i = (WORD as u64);
while (i > 0) {
i = i - 1;
let a_bits = *Vector::borrow(&a.bits, i);
let b_bits = *Vector::borrow(&b.bits, i);
if (a_bits != b_bits) {
if (a_bits < b_bits) {
return LESS_THAN
} else {
return GREATER_THAN
}
}
};
return EQUAL
}
// TODO: MVP interprets it wrong
// spec compare {
// let va = value_of_U256(a);
// let vb = value_of_U256(b);
// ensures (va > vb) ==> (result == GREATER_THAN);
// ensures (va < vb) ==> (result == LESS_THAN);
// ensures (va == vb) ==> (result == EQUAL);
// }
#[test]
fun test_compare() {
let a = from_u64(111);
let b = from_u64(111);
let c = from_u64(112);
let d = from_u64(110);
assert!(compare(&a, &b) == EQUAL, 0);
assert!(compare(&a, &c) == LESS_THAN, 1);
assert!(compare(&a, &d) == GREATER_THAN, 2);
}
public fun add(a: U256, b: U256): U256 {
native_add(&mut a, &b);
a
}
spec add {
aborts_if value_of_U256(a) + value_of_U256(b) >= P64 * P64 * P64 * P64;
ensures value_of_U256(result) == value_of_U256(a) + value_of_U256(b);
}
#[test]
fun test_add() {
let a = Self::one();
let b = Self::from_u128(10);
let ret = Self::add(a, b);
assert!(compare(&ret, &from_u64(11)) == EQUAL, 0);
}
public fun sub(a: U256, b: U256): U256 {
native_sub(&mut a, &b);
a
}
spec sub {
aborts_if value_of_U256(a) < value_of_U256(b);
ensures value_of_U256(result) == value_of_U256(a) - value_of_U256(b);
}
#[test]
#[expected_failure]
fun test_sub_overflow() {
let a = Self::one();
let b = Self::from_u128(10);
let _ = Self::sub(a, b);
}
#[test]
fun test_sub_ok() {
let a = Self::from_u128(10);
let b = Self::one();
let ret = Self::sub(a, b);
assert!(compare(&ret, &from_u64(9)) == EQUAL, 0);
}
public fun mul(a: U256, b: U256): U256 {
native_mul(&mut a, &b);
a
}
spec mul {
pragma verify = false;
pragma timeout = 200; // Take longer time
aborts_if value_of_U256(a) * value_of_U256(b) >= P64 * P64 * P64 * P64;
ensures value_of_U256(result) == value_of_U256(a) * value_of_U256(b);
}
#[test]
fun test_mul() {
let a = Self::from_u128(10);
let b = Self::from_u64(10);
let ret = Self::mul(a, b);
assert!(compare(&ret, &from_u64(100)) == EQUAL, 0);
}
public fun div(a: U256, b: U256): U256 {
native_div(&mut a, &b);
a
}
spec div {
pragma verify = false;
pragma timeout = 160; // Might take longer time
aborts_if value_of_U256(b) == 0;
ensures value_of_U256(result) == value_of_U256(a) / value_of_U256(b);
}
#[test]
fun test_div() {
let a = Self::from_u128(10);
let b = Self::from_u64(2);
let c = Self::from_u64(3);
// as U256 cannot be implicitly copied, we need to add copy keyword.
assert!(compare(&Self::div(copy a, b), &from_u64(5)) == EQUAL, 0);
assert!(compare(&Self::div(copy a, c), &from_u64(3)) == EQUAL, 0);
}
public fun rem(a: U256, b: U256): U256 {
native_rem(&mut a, &b);
a
}
spec rem {
pragma verify = false;
pragma timeout = 160; // Might take longer time
aborts_if value_of_U256(b) == 0;
ensures value_of_U256(result) == value_of_U256(a) % value_of_U256(b);
}
#[test]
fun test_rem() {
let a = Self::from_u128(10);
let b = Self::from_u64(2);
let c = Self::from_u64(3);
assert!(compare(&Self::rem(copy a, b), &from_u64(0)) == EQUAL, 0);
assert!(compare(&Self::rem(copy a, c), &from_u64(1)) == EQUAL, 0);
}
public fun pow(a: U256, b: U256): U256 {
native_pow(&mut a, &b);
a
}
spec pow {
// Verfication of Pow takes enormous amount of time
// Don't verify it, and make it opaque so that the caller
// can make use of the properties listed here.
pragma verify = false;
pragma opaque;
pragma timeout = 600;
let p = pow_spec(value_of_U256(a), value_of_U256(b));
aborts_if p >= P64 * P64 * P64 * P64;
ensures value_of_U256(result) == p;
}
#[test]
fun test_pow() {
let a = Self::from_u128(10);
let b = Self::from_u64(1);
let c = Self::from_u64(2);
let d = Self::zero();
assert!(compare(&Self::pow(copy a, b), &from_u64(10)) == EQUAL, 0);
assert!(compare(&Self::pow(copy a, c), &from_u64(100)) == EQUAL, 0);
assert!(compare(&Self::pow(copy a, d), &from_u64(1)) == EQUAL, 0);
}
native fun from_bytes(data: &vector<u8>, be: bool): U256;
native fun native_add(a: &mut U256, b: &U256);
native fun native_sub(a: &mut U256, b: &U256);
native fun native_mul(a: &mut U256, b: &U256);
native fun native_div(a: &mut U256, b: &U256);
native fun native_rem(a: &mut U256, b: &U256);
native fun native_pow(a: &mut U256, b: &U256);
spec native_add {
pragma opaque;
aborts_if value_of_U256(a) + value_of_U256(b) >= P64 * P64 * P64 * P64;
ensures value_of_U256(a) == value_of_U256(old(a)) + value_of_U256(b);
}
spec native_sub {
pragma opaque;
aborts_if value_of_U256(a) - value_of_U256(b) < 0;
ensures value_of_U256(a) == value_of_U256(old(a)) - value_of_U256(b);
}
spec native_mul {
pragma opaque;
aborts_if value_of_U256(a) * value_of_U256(b) >= P64 * P64 * P64 * P64;
ensures value_of_U256(a) == value_of_U256(old(a)) * value_of_U256(b);
}
spec native_div {
pragma opaque;
aborts_if value_of_U256(b) == 0;
ensures value_of_U256(a) == value_of_U256(old(a)) / value_of_U256(b);
}
spec native_rem {
pragma opaque;
aborts_if value_of_U256(b) == 0;
ensures value_of_U256(a) == value_of_U256(old(a)) % value_of_U256(b);
}
spec native_pow {
pragma opaque;
aborts_if pow_spec(value_of_U256(a), value_of_U256(b)) >= P64 * P64 * P64 * P64;
ensures value_of_U256(a) == pow_spec(value_of_U256(old(a)), value_of_U256(b));
}
spec fun pow_spec(base: num, expon: num): num {
// This actually doesn't follow a strict definition as 0^0 is undefined
// mathematically. But the U256::pow of Rust is defined to be like this:
// Link: https://docs.rs/uint/0.9.3/src/uint/uint.rs.html#1000-1003
if (expon > 0) {
let x = pow_spec(base, expon / 2);
if (expon % 2 == 0) { x * x } else { x * x * base }
} else {
1
}
}
}
}