@btc-vision/btc-runtime 1.10.8 → 1.10.11

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.
Files changed (44) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +258 -137
  3. package/SECURITY.md +226 -0
  4. package/docs/README.md +614 -0
  5. package/docs/advanced/bitcoin-scripts.md +939 -0
  6. package/docs/advanced/cross-contract-calls.md +579 -0
  7. package/docs/advanced/plugins.md +1006 -0
  8. package/docs/advanced/quantum-resistance.md +660 -0
  9. package/docs/advanced/signature-verification.md +715 -0
  10. package/docs/api-reference/blockchain.md +729 -0
  11. package/docs/api-reference/events.md +642 -0
  12. package/docs/api-reference/op20.md +902 -0
  13. package/docs/api-reference/op721.md +819 -0
  14. package/docs/api-reference/safe-math.md +510 -0
  15. package/docs/api-reference/storage.md +840 -0
  16. package/docs/contracts/op-net-base.md +786 -0
  17. package/docs/contracts/op20-token.md +687 -0
  18. package/docs/contracts/op20s-signatures.md +614 -0
  19. package/docs/contracts/op721-nft.md +785 -0
  20. package/docs/contracts/reentrancy-guard.md +787 -0
  21. package/docs/core-concepts/blockchain-environment.md +724 -0
  22. package/docs/core-concepts/decorators.md +466 -0
  23. package/docs/core-concepts/events.md +652 -0
  24. package/docs/core-concepts/pointers.md +391 -0
  25. package/docs/core-concepts/security.md +473 -0
  26. package/docs/core-concepts/storage-system.md +969 -0
  27. package/docs/examples/basic-token.md +745 -0
  28. package/docs/examples/nft-with-reservations.md +1440 -0
  29. package/docs/examples/oracle-integration.md +1212 -0
  30. package/docs/examples/stablecoin.md +1180 -0
  31. package/docs/getting-started/first-contract.md +575 -0
  32. package/docs/getting-started/installation.md +384 -0
  33. package/docs/getting-started/project-structure.md +630 -0
  34. package/docs/storage/memory-maps.md +764 -0
  35. package/docs/storage/stored-arrays.md +778 -0
  36. package/docs/storage/stored-maps.md +758 -0
  37. package/docs/storage/stored-primitives.md +655 -0
  38. package/docs/types/address.md +773 -0
  39. package/docs/types/bytes-writer-reader.md +938 -0
  40. package/docs/types/calldata.md +744 -0
  41. package/docs/types/safe-math.md +446 -0
  42. package/package.json +52 -27
  43. package/runtime/memory/MapOfMap.ts +1 -0
  44. package/LICENSE.md +0 -21
@@ -0,0 +1,510 @@
1
+ # SafeMath API Reference
2
+
3
+ The `SafeMath` class provides overflow-safe arithmetic operations for `u256` values.
4
+
5
+ ## Import
6
+
7
+ ```typescript
8
+ import { SafeMath } from '@btc-vision/btc-runtime/runtime';
9
+ import { u256 } from '@btc-vision/as-bignum/assembly';
10
+ ```
11
+
12
+ ## Basic Operations
13
+
14
+ ### add
15
+
16
+ Adds two u256 values with overflow checking.
17
+
18
+ ```typescript
19
+ static add(a: u256, b: u256): u256
20
+ ```
21
+
22
+ ```typescript
23
+ const sum = SafeMath.add(u256.fromU64(100), u256.fromU64(50)); // 150
24
+ ```
25
+
26
+ **Throws:** `Revert` if result overflows
27
+
28
+ ### sub
29
+
30
+ Subtracts b from a with underflow checking.
31
+
32
+ ```typescript
33
+ static sub(a: u256, b: u256): u256
34
+ ```
35
+
36
+ ```typescript
37
+ const diff = SafeMath.sub(u256.fromU64(100), u256.fromU64(30)); // 70
38
+ ```
39
+
40
+ **Throws:** `Revert` if b > a (underflow)
41
+
42
+ ### mul
43
+
44
+ Multiplies two u256 values with overflow checking.
45
+
46
+ ```typescript
47
+ static mul(a: u256, b: u256): u256
48
+ ```
49
+
50
+ ```typescript
51
+ const product = SafeMath.mul(u256.fromU64(100), u256.fromU64(5)); // 500
52
+ ```
53
+
54
+ **Throws:** `Revert` if result overflows
55
+
56
+ ### div
57
+
58
+ Divides a by b.
59
+
60
+ ```typescript
61
+ static div(a: u256, b: u256): u256
62
+ ```
63
+
64
+ ```typescript
65
+ const quotient = SafeMath.div(u256.fromU64(100), u256.fromU64(4)); // 25
66
+ ```
67
+
68
+ **Throws:** `Revert` if b is zero
69
+
70
+ ### mod
71
+
72
+ Returns remainder of a divided by b.
73
+
74
+ ```typescript
75
+ static mod(a: u256, b: u256): u256
76
+ ```
77
+
78
+ ```typescript
79
+ const remainder = SafeMath.mod(u256.fromU64(100), u256.fromU64(30)); // 10
80
+ ```
81
+
82
+ **Throws:** `Revert` if b is zero
83
+
84
+ ## Advanced Operations
85
+
86
+ ### pow
87
+
88
+ Raises base to power exponent using binary exponentiation.
89
+
90
+ ```typescript
91
+ static pow(base: u256, exponent: u256): u256
92
+ ```
93
+
94
+ ```typescript
95
+ const power = SafeMath.pow(u256.fromU64(2), u256.fromU64(10)); // 1024
96
+ ```
97
+
98
+ **Throws:** `Revert` on overflow
99
+
100
+ ### pow10
101
+
102
+ Computes 10^exponent (optimized for base 10).
103
+
104
+ ```typescript
105
+ static pow10(exponent: u8): u256
106
+ ```
107
+
108
+ ```typescript
109
+ const million = SafeMath.pow10(6); // 1,000,000
110
+ ```
111
+
112
+ **Throws:** `Revert` if exponent > 77 (would overflow)
113
+
114
+ ### sqrt
115
+
116
+ Computes square root using Newton-Raphson method.
117
+
118
+ ```typescript
119
+ static sqrt(a: u256): u256
120
+ ```
121
+
122
+ ```typescript
123
+ const root = SafeMath.sqrt(u256.fromU64(144)); // 12
124
+ ```
125
+
126
+ ### min
127
+
128
+ Returns smaller of two values.
129
+
130
+ ```typescript
131
+ static min(a: u256, b: u256): u256
132
+ ```
133
+
134
+ ```typescript
135
+ const smaller = SafeMath.min(u256.fromU64(100), u256.fromU64(50)); // 50
136
+ ```
137
+
138
+ ### max
139
+
140
+ Returns larger of two values.
141
+
142
+ ```typescript
143
+ static max(a: u256, b: u256): u256
144
+ ```
145
+
146
+ ```typescript
147
+ const larger = SafeMath.max(u256.fromU64(100), u256.fromU64(50)); // 100
148
+ ```
149
+
150
+ ### inc
151
+
152
+ Increments value by 1 with overflow protection.
153
+
154
+ ```typescript
155
+ static inc(value: u256): u256
156
+ ```
157
+
158
+ ```typescript
159
+ const incremented = SafeMath.inc(u256.fromU64(100)); // 101
160
+ ```
161
+
162
+ **Throws:** `Revert` if value equals u256.Max
163
+
164
+ ### dec
165
+
166
+ Decrements value by 1 with underflow protection.
167
+
168
+ ```typescript
169
+ static dec(value: u256): u256
170
+ ```
171
+
172
+ ```typescript
173
+ const decremented = SafeMath.dec(u256.fromU64(100)); // 99
174
+ ```
175
+
176
+ **Throws:** `Revert` if value is zero
177
+
178
+ ## Cryptographic Operations
179
+
180
+ ### mulmod
181
+
182
+ Computes (a * b) % modulus without intermediate overflow.
183
+
184
+ ```typescript
185
+ static mulmod(a: u256, b: u256, modulus: u256): u256
186
+ ```
187
+
188
+ ```typescript
189
+ // For cryptographic operations where intermediate product would overflow
190
+ const result = SafeMath.mulmod(largeA, largeB, prime);
191
+ ```
192
+
193
+ **Use case:** Elliptic curve operations, modular arithmetic
194
+
195
+ ### modInverse
196
+
197
+ Computes modular multiplicative inverse.
198
+
199
+ ```typescript
200
+ static modInverse(a: u256, modulus: u256): u256
201
+ ```
202
+
203
+ ```typescript
204
+ // Find x such that (a * x) % modulus = 1
205
+ const inverse = SafeMath.modInverse(value, prime);
206
+ ```
207
+
208
+ **Throws:** If inverse doesn't exist
209
+
210
+ ## Logarithm Operations
211
+
212
+ ### approximateLog2
213
+
214
+ Returns floor of base-2 logarithm (position of highest set bit).
215
+
216
+ ```typescript
217
+ static approximateLog2(x: u256): u256
218
+ ```
219
+
220
+ ```typescript
221
+ const log = SafeMath.approximateLog2(u256.fromU64(1024)); // 10
222
+ const log2 = SafeMath.approximateLog2(u256.fromU64(1000)); // 9 (floor)
223
+ ```
224
+
225
+ **Returns:** 0 for both input 0 and input 1
226
+
227
+ ### preciseLog
228
+
229
+ Computes natural logarithm (ln) with high precision.
230
+
231
+ ```typescript
232
+ static preciseLog(x: u256): u256
233
+ ```
234
+
235
+ ```typescript
236
+ // Returns ln(x) scaled by 10^6 for fixed-point precision
237
+ const lnScaled = SafeMath.preciseLog(u256.fromU64(10)); // ~2,302,585
238
+ ```
239
+
240
+ **Returns:** ln(x) * 1,000,000
241
+
242
+ ### approxLog
243
+
244
+ Computes approximate natural logarithm using bit length.
245
+
246
+ ```typescript
247
+ static approxLog(x: u256): u256
248
+ ```
249
+
250
+ ```typescript
251
+ // Uses bitLength * ln(2) approximation, scaled by 10^6
252
+ const approxLn = SafeMath.approxLog(u256.fromU64(8)); // ~2,079,441 (3 * ln(2))
253
+ ```
254
+
255
+ **Returns:** Approximate ln(x) * 1,000,000
256
+
257
+ ### bitLength256
258
+
259
+ Returns number of bits needed to represent the value.
260
+
261
+ ```typescript
262
+ static bitLength256(x: u256): u32
263
+ ```
264
+
265
+ ```typescript
266
+ const bits = SafeMath.bitLength256(u256.fromU64(255)); // 8
267
+ const bits2 = SafeMath.bitLength256(u256.fromU64(256)); // 9
268
+ ```
269
+
270
+ ## Bitwise Operations
271
+
272
+ ### shl
273
+
274
+ Shifts left by specified bits.
275
+
276
+ ```typescript
277
+ static shl(value: u256, shift: i32): u256
278
+ ```
279
+
280
+ ```typescript
281
+ const shifted = SafeMath.shl(u256.fromU64(1), 10); // 1024
282
+ ```
283
+
284
+ **Warning:** Unlike other SafeMath operations, bits shifted beyond type width are silently lost (no throw). Shifts >= 256 return 0.
285
+
286
+ ### shl128
287
+
288
+ Shifts a u128 value left by specified bits.
289
+
290
+ ```typescript
291
+ static shl128(value: u128, shift: i32): u128
292
+ ```
293
+
294
+ **Warning:** Same behavior as shl - overflow bits are silently lost.
295
+
296
+ ### shr
297
+
298
+ Shifts right by specified bits.
299
+
300
+ ```typescript
301
+ static shr(value: u256, shift: i32): u256
302
+ ```
303
+
304
+ ```typescript
305
+ const shifted = SafeMath.shr(u256.fromU64(1024), 5); // 32
306
+ ```
307
+
308
+ ### and
309
+
310
+ Performs bitwise AND.
311
+
312
+ ```typescript
313
+ static and(a: u256, b: u256): u256
314
+ ```
315
+
316
+ ### or
317
+
318
+ Performs bitwise OR.
319
+
320
+ ```typescript
321
+ static or(a: u256, b: u256): u256
322
+ ```
323
+
324
+ ### xor
325
+
326
+ Performs bitwise XOR.
327
+
328
+ ```typescript
329
+ static xor(a: u256, b: u256): u256
330
+ ```
331
+
332
+ ### isEven
333
+
334
+ Checks if value is even.
335
+
336
+ ```typescript
337
+ static isEven(a: u256): bool
338
+ ```
339
+
340
+ ```typescript
341
+ if (SafeMath.isEven(value)) {
342
+ // Value is even
343
+ }
344
+ ```
345
+
346
+ ## Comparison Operations
347
+
348
+ SafeMath does not provide comparison methods. Use the built-in u256 methods instead:
349
+
350
+ ```typescript
351
+ // Use u256 built-in comparison methods
352
+ u256.eq(a, b) // Equal
353
+ u256.ne(a, b) // Not equal
354
+ u256.lt(a, b) // Less than
355
+ u256.le(a, b) // Less than or equal
356
+ u256.gt(a, b) // Greater than
357
+ u256.ge(a, b) // Greater than or equal
358
+ ```
359
+
360
+ ### min
361
+
362
+ Returns the smaller of two u256 values.
363
+
364
+ ```typescript
365
+ static min(a: u256, b: u256): u256
366
+ ```
367
+
368
+ ### max
369
+
370
+ Returns the larger of two u256 values.
371
+
372
+ ```typescript
373
+ static max(a: u256, b: u256): u256
374
+ ```
375
+
376
+ ### 128-bit and 64-bit variants
377
+
378
+ ```typescript
379
+ // u128 min/max
380
+ SafeMath.min128(a: u128, b: u128): u128
381
+ SafeMath.max128(a: u128, b: u128): u128
382
+
383
+ // u64 min/max
384
+ SafeMath.min64(a: u64, b: u64): u64
385
+ SafeMath.max64(a: u64, b: u64): u64
386
+ ```
387
+
388
+ ## u128 Operations
389
+
390
+ All u256 operations have u128 variants for better gas efficiency with smaller values:
391
+
392
+ ```typescript
393
+ import { u128 } from '@btc-vision/as-bignum/assembly';
394
+
395
+ SafeMath.add128(a: u128, b: u128): u128 // Addition
396
+ SafeMath.sub128(a: u128, b: u128): u128 // Subtraction
397
+ SafeMath.mul128(a: u128, b: u128): u128 // Multiplication
398
+ SafeMath.div128(a: u128, b: u128): u128 // Division
399
+ SafeMath.min128(a: u128, b: u128): u128 // Minimum
400
+ SafeMath.max128(a: u128, b: u128): u128 // Maximum
401
+ SafeMath.shl128(value: u128, shift: i32): u128 // Left shift
402
+ ```
403
+
404
+ ## u64 Operations
405
+
406
+ Native u64 variants for optimal performance:
407
+
408
+ ```typescript
409
+ SafeMath.add64(a: u64, b: u64): u64 // Addition
410
+ SafeMath.sub64(a: u64, b: u64): u64 // Subtraction
411
+ SafeMath.mul64(a: u64, b: u64): u64 // Multiplication
412
+ SafeMath.div64(a: u64, b: u64): u64 // Division
413
+ SafeMath.min64(a: u64, b: u64): u64 // Minimum
414
+ SafeMath.max64(a: u64, b: u64): u64 // Maximum
415
+ ```
416
+
417
+ ## SafeMathI128
418
+
419
+ For signed 128-bit operations (separate module):
420
+
421
+ ```typescript
422
+ import { SafeMathI128 } from '@btc-vision/btc-runtime/runtime';
423
+ import { i128 } from '@btc-vision/as-bignum/assembly';
424
+ ```
425
+
426
+ See the SafeMathI128 module for signed integer operations.
427
+
428
+ ## Common Patterns
429
+
430
+ ### Percentage Calculation
431
+
432
+ ```typescript
433
+ // Calculate 5% fee
434
+ const FEE_PERCENT = u256.fromU64(5);
435
+ const HUNDRED = u256.fromU64(100);
436
+
437
+ function calculateFee(amount: u256): u256 {
438
+ return SafeMath.div(SafeMath.mul(amount, FEE_PERCENT), HUNDRED);
439
+ }
440
+ ```
441
+
442
+ ### Basis Points
443
+
444
+ ```typescript
445
+ // 100 basis points = 1%
446
+ const BPS = u256.fromU64(10000);
447
+
448
+ function applyBps(amount: u256, bps: u256): u256 {
449
+ return SafeMath.div(SafeMath.mul(amount, bps), BPS);
450
+ }
451
+ ```
452
+
453
+ ### Fixed-Point Multiplication
454
+
455
+ ```typescript
456
+ const PRECISION = u256.fromU64(1_000_000); // 6 decimals
457
+
458
+ function mulFixed(a: u256, b: u256): u256 {
459
+ return SafeMath.div(SafeMath.mul(a, b), PRECISION);
460
+ }
461
+ ```
462
+
463
+ ### Average Without Overflow
464
+
465
+ ```typescript
466
+ function average(a: u256, b: u256): u256 {
467
+ // (a + b) / 2 without overflow
468
+ return SafeMath.add(
469
+ SafeMath.shr(a, 1),
470
+ SafeMath.add(
471
+ SafeMath.shr(b, 1),
472
+ SafeMath.mul(
473
+ SafeMath.mod(a, u256.fromU64(2)),
474
+ SafeMath.mod(b, u256.fromU64(2))
475
+ )
476
+ )
477
+ );
478
+ }
479
+ ```
480
+
481
+ ## Error Handling
482
+
483
+ All SafeMath operations throw `Revert` on error:
484
+
485
+ ```typescript
486
+ try {
487
+ const result = SafeMath.div(amount, u256.Zero);
488
+ } catch (e) {
489
+ // Division by zero caught
490
+ }
491
+ ```
492
+
493
+ ## Solidity Comparison
494
+
495
+ | Solidity | SafeMath |
496
+ |----------|----------|
497
+ | `a + b` (checked) | `SafeMath.add(a, b)` |
498
+ | `a - b` (checked) | `SafeMath.sub(a, b)` |
499
+ | `a * b` (checked) | `SafeMath.mul(a, b)` |
500
+ | `a / b` | `SafeMath.div(a, b)` |
501
+ | `a % b` | `SafeMath.mod(a, b)` |
502
+ | `a ** b` | `SafeMath.pow(a, b)` |
503
+ | `Math.sqrt(a)` | `SafeMath.sqrt(a)` |
504
+ | `mulmod(a, b, m)` | `SafeMath.mulmod(a, b, m)` |
505
+
506
+ ---
507
+
508
+ **Navigation:**
509
+ - Previous: [OP721 API](./op721.md)
510
+ - Next: [Storage API](./storage.md)