@btc-vision/btc-runtime 1.0.6 → 1.0.7
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 +1 -1
- package/runtime/env/global.ts +1 -1
- package/runtime/types/SafeMath.ts +29 -0
package/package.json
CHANGED
package/runtime/env/global.ts
CHANGED
|
@@ -72,6 +72,35 @@ export class SafeMath {
|
|
|
72
72
|
return result;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
public static min(a: u256, b: u256): u256 {
|
|
76
|
+
return u256.lt(a, b) ? a : b;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public static max(a: u256, b: u256): u256 {
|
|
80
|
+
return u256.gt(a, b) ? a : b;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
@inline
|
|
84
|
+
@unsafe
|
|
85
|
+
public static sqrt(y: u256): u256 {
|
|
86
|
+
if (u256.gt(y, u256.fromU32(3))) {
|
|
87
|
+
let z = y;
|
|
88
|
+
|
|
89
|
+
let x = SafeMath.add(SafeMath.div(y, u256.fromU32(2)), u256.fromU32(1));
|
|
90
|
+
while (u256.lt(x, z)) {
|
|
91
|
+
z = x;
|
|
92
|
+
|
|
93
|
+
let u = SafeMath.div(y, x);
|
|
94
|
+
x = SafeMath.div(u256.add(u, x), u256.fromU32(2));
|
|
95
|
+
}
|
|
96
|
+
return z;
|
|
97
|
+
} else if (!u256.eq(y, u256.Zero)) {
|
|
98
|
+
return u256.fromU32(1);
|
|
99
|
+
} else {
|
|
100
|
+
return u256.Zero;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
75
104
|
@inline
|
|
76
105
|
@unsafe
|
|
77
106
|
static shl(value: u256, shift: i32): u256 {
|