@btc-vision/btc-runtime 1.4.2 → 1.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/runtime/index.ts +1 -2
- package/runtime/types/SafeMathI128.ts +146 -0
- package/runtime/math/i256.ts +0 -606
- package/runtime/math/u160.ts +0 -779
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@btc-vision/btc-runtime",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
4
|
"description": "Bitcoin Smart Contract Runtime",
|
|
5
5
|
"main": "btc/index.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@assemblyscript/loader": "^0.27.30",
|
|
46
|
-
"@btc-vision/as-bignum": "^0.0.
|
|
46
|
+
"@btc-vision/as-bignum": "^0.0.3",
|
|
47
47
|
"@eslint/js": "^9.10.0",
|
|
48
48
|
"gulplog": "^2.2.0",
|
|
49
49
|
"mocha": "^10.7.3",
|
package/runtime/index.ts
CHANGED
|
@@ -38,6 +38,7 @@ export * from './lang/Definitions';
|
|
|
38
38
|
export * from './types/Address';
|
|
39
39
|
export * from './types/Revert';
|
|
40
40
|
export * from './types/SafeMath';
|
|
41
|
+
export * from './types/SafeMathI128';
|
|
41
42
|
|
|
42
43
|
/** Math */
|
|
43
44
|
export * from './math/abi';
|
|
@@ -45,8 +46,6 @@ export * from './math/bytes';
|
|
|
45
46
|
export * from './math/cyrb53';
|
|
46
47
|
export * from './math/sha256';
|
|
47
48
|
export * from './math/rnd';
|
|
48
|
-
export * from './math/i256';
|
|
49
|
-
//export * from './math/u160'; Broken at the moment
|
|
50
49
|
export * from './secp256k1/ECPoint';
|
|
51
50
|
|
|
52
51
|
/** Memory */
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { i128 } from '@btc-vision/as-bignum/assembly';
|
|
2
|
+
|
|
3
|
+
export class SafeMathI128 {
|
|
4
|
+
public static readonly ZERO: i128 = i128.fromI32(0);
|
|
5
|
+
public static readonly ONE: i128 = i128.fromI32(1);
|
|
6
|
+
public static readonly NEG_ONE: i128 = i128.fromI32(-1);
|
|
7
|
+
|
|
8
|
+
public static readonly MIN: i128 = i128.Min;
|
|
9
|
+
public static readonly MAX: i128 = i128.Max;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Safe addition for i128.
|
|
13
|
+
* Throws if (a + b) overflows or underflows the signed 128-bit range.
|
|
14
|
+
*/
|
|
15
|
+
public static add(a: i128, b: i128): i128 {
|
|
16
|
+
let c = i128.add(a, b);
|
|
17
|
+
|
|
18
|
+
// Overflow check for 2's complement:
|
|
19
|
+
// If a and b have the same sign, but c differs, overflow occurred.
|
|
20
|
+
// We can detect sign mismatch using ((a ^ c) & (b ^ c)) < 0
|
|
21
|
+
// (i.e., the sign bit is set in that expression).
|
|
22
|
+
if (((a ^ c) & (b ^ c)).isNeg()) {
|
|
23
|
+
throw new Error('SafeMathI128: addition overflow');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return c;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Safe subtraction for i128.
|
|
31
|
+
* Throws if (a - b) overflows or underflows the signed 128-bit range.
|
|
32
|
+
*/
|
|
33
|
+
public static sub(a: i128, b: i128): i128 {
|
|
34
|
+
let c = i128.sub(a, b);
|
|
35
|
+
|
|
36
|
+
// Subtraction is (a + (-b)). We can do a direct check like:
|
|
37
|
+
// If (a ^ b) & (a ^ c) has sign bit set => overflow.
|
|
38
|
+
if (((a ^ b) & (a ^ c)).isNeg()) {
|
|
39
|
+
throw new Error('SafeMathI128: subtraction overflow');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return c;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/*public static mul(a: i128, b: i128): i128 {
|
|
46
|
+
// Quick check: if either is ZERO, product is ZERO => no overflow
|
|
47
|
+
if (a == SafeMathI128.ZERO || b == SafeMathI128.ZERO) {
|
|
48
|
+
return SafeMathI128.ZERO;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let c = i128.mul(a, b);
|
|
52
|
+
|
|
53
|
+
// Check overflow: c / b should be exactly a (if b != 0).
|
|
54
|
+
// Also watch for the i128 edge case: MIN * -1 => possible overflow if not representable.
|
|
55
|
+
// We'll rely on the division check:
|
|
56
|
+
if (b != SafeMathI128.ZERO) {
|
|
57
|
+
let divCheck = i128.div(c, b);
|
|
58
|
+
if (divCheck != a) {
|
|
59
|
+
throw new Error('SafeMathI128: multiplication overflow');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return c;
|
|
64
|
+
}*/
|
|
65
|
+
|
|
66
|
+
/*public static div(a: i128, b: i128): i128 {
|
|
67
|
+
if (b == SafeMathI128.ZERO) {
|
|
68
|
+
throw new Error('SafeMathI128: division by zero');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Check i128 edge case: MIN / -1 => possible overflow if no corresponding positive.
|
|
72
|
+
if (a == SafeMathI128.MIN && b == SafeMathI128.NEG_ONE) {
|
|
73
|
+
throw new Error('SafeMathI128: division overflow (MIN / -1)');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return i128.div(a, b);
|
|
77
|
+
}*/
|
|
78
|
+
|
|
79
|
+
/*public static mod(a: i128, b: i128): i128 {
|
|
80
|
+
if (b == SafeMathI128.ZERO) {
|
|
81
|
+
throw new Error('SafeMathI128: modulo by zero');
|
|
82
|
+
}
|
|
83
|
+
// Similar edge case as division:
|
|
84
|
+
if (a == SafeMathI128.MIN && b == SafeMathI128.NEG_ONE) {
|
|
85
|
+
// Some implementations might treat MIN % -1 == 0,
|
|
86
|
+
// but if the library doesn't, you may handle it similarly to division.
|
|
87
|
+
// We'll assume we throw to be safe:
|
|
88
|
+
throw new Error('SafeMathI128: modulo overflow (MIN % -1)');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Use i128.rem, i128.mod, or the operator as appropriate.
|
|
92
|
+
return i128.rem(a, b);
|
|
93
|
+
}*/
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Increment an i128 by 1 with overflow check.
|
|
97
|
+
*/
|
|
98
|
+
public static inc(value: i128): i128 {
|
|
99
|
+
return SafeMathI128.add(value, SafeMathI128.ONE);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Decrement an i128 by 1 with underflow check.
|
|
104
|
+
*/
|
|
105
|
+
public static dec(value: i128): i128 {
|
|
106
|
+
return SafeMathI128.sub(value, SafeMathI128.ONE);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Return the absolute value of x, throwing if x == MIN (since |MIN| might not be representable).
|
|
111
|
+
*/
|
|
112
|
+
public static abs(x: i128): i128 {
|
|
113
|
+
if (x.isNeg()) {
|
|
114
|
+
// If x == MIN, -x can overflow.
|
|
115
|
+
if (x == SafeMathI128.MIN) {
|
|
116
|
+
throw new Error('SafeMathI128: abs overflow on MIN');
|
|
117
|
+
}
|
|
118
|
+
return x.neg();
|
|
119
|
+
}
|
|
120
|
+
return x;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Return the negation of x, throwing if x == MIN.
|
|
125
|
+
*/
|
|
126
|
+
public static neg(x: i128): i128 {
|
|
127
|
+
if (x == SafeMathI128.MIN) {
|
|
128
|
+
throw new Error('SafeMathI128: neg overflow on MIN');
|
|
129
|
+
}
|
|
130
|
+
return x.neg();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Returns the smaller of two i128s.
|
|
135
|
+
*/
|
|
136
|
+
public static min(a: i128, b: i128): i128 {
|
|
137
|
+
return i128.lt(a, b) ? a : b;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Returns the larger of two i128s.
|
|
142
|
+
*/
|
|
143
|
+
public static max(a: i128, b: i128): i128 {
|
|
144
|
+
return i128.gt(a, b) ? a : b;
|
|
145
|
+
}
|
|
146
|
+
}
|