@btc-vision/btc-runtime 1.4.3 → 1.4.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@btc-vision/btc-runtime",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
4
4
  "description": "Bitcoin Smart Contract Runtime",
5
5
  "main": "btc/index.ts",
6
6
  "scripts": {
@@ -2,13 +2,11 @@ import { i128, u128, u256 } from '@btc-vision/as-bignum/assembly';
2
2
  import { TransactionInput, TransactionOutput } from '../env/classes/UTXO';
3
3
  import { AddressMap } from '../generic/AddressMap';
4
4
  import { Selector } from '../math/abi';
5
- import { i256 } from '../math/i256';
6
5
  import { Address } from '../types/Address';
7
6
  import { Revert } from '../types/Revert';
8
7
  import {
9
8
  ADDRESS_BYTE_LENGTH,
10
9
  I128_BYTE_LENGTH,
11
- I256_BYTE_LENGTH,
12
10
  I64_BYTE_LENGTH,
13
11
  U128_BYTE_LENGTH,
14
12
  U16_BYTE_LENGTH,
@@ -16,7 +14,7 @@ import {
16
14
  U32_BYTE_LENGTH,
17
15
  U64_BYTE_LENGTH,
18
16
  U8_BYTE_LENGTH,
19
- } from '../utils/lengths';
17
+ } from '../utils';
20
18
 
21
19
  @final
22
20
  export class BytesReader {
@@ -187,14 +185,6 @@ export class BytesReader {
187
185
  return result;
188
186
  }
189
187
 
190
- public readI256(): i256 {
191
- this.verifyEnd(this.currentOffset + I256_BYTE_LENGTH);
192
-
193
- const next32Bytes: u8[] = this.readBytesBE(I256_BYTE_LENGTH);
194
-
195
- return i256.fromBytesBE(next32Bytes);
196
- }
197
-
198
188
  public readTuple(): u256[] {
199
189
  const length = this.readU32();
200
190
  const result: u256[] = new Array<u256>(length);
@@ -2,20 +2,18 @@ import { i128, u128, u256 } from '@btc-vision/as-bignum/assembly';
2
2
  import { ArrayBuffer } from 'arraybuffer';
3
3
  import { AddressMap } from '../generic/AddressMap';
4
4
  import { Selector } from '../math/abi';
5
- import { i256 } from '../math/i256';
6
5
  import { Address } from '../types/Address';
7
6
  import { Revert } from '../types/Revert';
8
7
  import {
9
8
  ADDRESS_BYTE_LENGTH,
10
9
  I128_BYTE_LENGTH,
11
- I256_BYTE_LENGTH,
12
10
  U128_BYTE_LENGTH,
13
11
  U16_BYTE_LENGTH,
14
12
  U256_BYTE_LENGTH,
15
13
  U32_BYTE_LENGTH,
16
14
  U64_BYTE_LENGTH,
17
15
  U8_BYTE_LENGTH,
18
- } from '../utils/lengths';
16
+ } from '../utils';
19
17
  import { BytesReader } from './BytesReader';
20
18
 
21
19
  @final
@@ -76,15 +74,6 @@ export class BytesWriter {
76
74
  this.writeU8(value ? 1 : 0);
77
75
  }
78
76
 
79
- public writeI256(value: i256): void {
80
- this.allocSafe(I256_BYTE_LENGTH);
81
-
82
- const bytes = value.toUint8Array(true);
83
- for (let i: i32 = 0; i < I256_BYTE_LENGTH; i++) {
84
- this.writeU8(bytes[i] || 0);
85
- }
86
- }
87
-
88
77
  public writeU8At(value: u8, offset: u32): void {
89
78
  this.buffer.setUint8(offset, value);
90
79
  }
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
+ }
@@ -8,7 +8,6 @@ export const U32_BYTE_LENGTH: i32 = 4;
8
8
  export const U16_BYTE_LENGTH: i32 = 2;
9
9
  export const U8_BYTE_LENGTH: i32 = 1;
10
10
 
11
- export const I256_BYTE_LENGTH: i32 = 32;
12
11
  export const I128_BYTE_LENGTH: i32 = 16;
13
12
  export const I64_BYTE_LENGTH: i32 = 8;
14
13
  export const I32_BYTE_LENGTH: i32 = 4;