@bsv/sdk 1.6.15 → 1.6.17

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 (138) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/auth/Peer.js +16 -25
  3. package/dist/cjs/src/auth/Peer.js.map +1 -1
  4. package/dist/cjs/src/auth/SessionManager.js +2 -4
  5. package/dist/cjs/src/auth/SessionManager.js.map +1 -1
  6. package/dist/cjs/src/auth/certificates/Certificate.js +2 -4
  7. package/dist/cjs/src/auth/certificates/Certificate.js.map +1 -1
  8. package/dist/cjs/src/auth/certificates/MasterCertificate.js +1 -1
  9. package/dist/cjs/src/auth/certificates/MasterCertificate.js.map +1 -1
  10. package/dist/cjs/src/auth/certificates/__tests/CompletedProtoWallet.js +1 -1
  11. package/dist/cjs/src/auth/certificates/__tests/CompletedProtoWallet.js.map +1 -1
  12. package/dist/cjs/src/auth/clients/AuthFetch.js +2 -4
  13. package/dist/cjs/src/auth/clients/AuthFetch.js.map +1 -1
  14. package/dist/cjs/src/compat/ECIES.js +1 -1
  15. package/dist/cjs/src/compat/ECIES.js.map +1 -1
  16. package/dist/cjs/src/compat/Mnemonic.js +2 -2
  17. package/dist/cjs/src/compat/Mnemonic.js.map +1 -1
  18. package/dist/cjs/src/identity/IdentityClient.js +1 -1
  19. package/dist/cjs/src/identity/IdentityClient.js.map +1 -1
  20. package/dist/cjs/src/kvstore/LocalKVStore.js +1 -2
  21. package/dist/cjs/src/kvstore/LocalKVStore.js.map +1 -1
  22. package/dist/cjs/src/overlay-tools/LookupResolver.js +6 -8
  23. package/dist/cjs/src/overlay-tools/LookupResolver.js.map +1 -1
  24. package/dist/cjs/src/overlay-tools/SHIPBroadcaster.js +9 -10
  25. package/dist/cjs/src/overlay-tools/SHIPBroadcaster.js.map +1 -1
  26. package/dist/cjs/src/primitives/AESGCM.js +1 -2
  27. package/dist/cjs/src/primitives/AESGCM.js.map +1 -1
  28. package/dist/cjs/src/primitives/BigNumber.js +2 -3
  29. package/dist/cjs/src/primitives/BigNumber.js.map +1 -1
  30. package/dist/cjs/src/primitives/Curve.js +2 -3
  31. package/dist/cjs/src/primitives/Curve.js.map +1 -1
  32. package/dist/cjs/src/primitives/ECDSA.js +174 -396
  33. package/dist/cjs/src/primitives/ECDSA.js.map +1 -1
  34. package/dist/cjs/src/primitives/JacobianPoint.js +1 -2
  35. package/dist/cjs/src/primitives/JacobianPoint.js.map +1 -1
  36. package/dist/cjs/src/primitives/Point.js +279 -126
  37. package/dist/cjs/src/primitives/Point.js.map +1 -1
  38. package/dist/cjs/src/primitives/Polynomial.js +1 -1
  39. package/dist/cjs/src/primitives/Polynomial.js.map +1 -1
  40. package/dist/cjs/src/primitives/PrivateKey.js +19 -2
  41. package/dist/cjs/src/primitives/PrivateKey.js.map +1 -1
  42. package/dist/cjs/src/primitives/PublicKey.js +19 -2
  43. package/dist/cjs/src/primitives/PublicKey.js.map +1 -1
  44. package/dist/cjs/src/primitives/Random.js +1 -2
  45. package/dist/cjs/src/primitives/Random.js.map +1 -1
  46. package/dist/cjs/src/primitives/TransactionSignature.js +5 -7
  47. package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -1
  48. package/dist/cjs/src/primitives/utils.js +1 -2
  49. package/dist/cjs/src/primitives/utils.js.map +1 -1
  50. package/dist/cjs/src/registry/RegistryClient.js +2 -4
  51. package/dist/cjs/src/registry/RegistryClient.js.map +1 -1
  52. package/dist/cjs/src/script/Spend.js +1 -2
  53. package/dist/cjs/src/script/Spend.js.map +1 -1
  54. package/dist/cjs/src/script/templates/P2PKH.js +4 -4
  55. package/dist/cjs/src/script/templates/P2PKH.js.map +1 -1
  56. package/dist/cjs/src/script/templates/PushDrop.js +7 -8
  57. package/dist/cjs/src/script/templates/PushDrop.js.map +1 -1
  58. package/dist/cjs/src/script/templates/RPuzzle.js +7 -6
  59. package/dist/cjs/src/script/templates/RPuzzle.js.map +1 -1
  60. package/dist/cjs/src/storage/StorageDownloader.js +1 -1
  61. package/dist/cjs/src/storage/StorageDownloader.js.map +1 -1
  62. package/dist/cjs/src/storage/StorageUploader.js +6 -9
  63. package/dist/cjs/src/storage/StorageUploader.js.map +1 -1
  64. package/dist/cjs/src/transaction/Beef.js +2 -3
  65. package/dist/cjs/src/transaction/Beef.js.map +1 -1
  66. package/dist/cjs/src/transaction/MerklePath.js +9 -12
  67. package/dist/cjs/src/transaction/MerklePath.js.map +1 -1
  68. package/dist/cjs/src/transaction/Transaction.js +15 -22
  69. package/dist/cjs/src/transaction/Transaction.js.map +1 -1
  70. package/dist/cjs/src/transaction/broadcasters/ARC.js +3 -3
  71. package/dist/cjs/src/transaction/broadcasters/ARC.js.map +1 -1
  72. package/dist/cjs/src/transaction/broadcasters/Teranode.js +2 -3
  73. package/dist/cjs/src/transaction/broadcasters/Teranode.js.map +1 -1
  74. package/dist/cjs/src/transaction/broadcasters/WhatsOnChainBroadcaster.js +2 -3
  75. package/dist/cjs/src/transaction/broadcasters/WhatsOnChainBroadcaster.js.map +1 -1
  76. package/dist/cjs/src/transaction/chaintrackers/BlockHeadersService.js +2 -2
  77. package/dist/cjs/src/transaction/chaintrackers/BlockHeadersService.js.map +1 -1
  78. package/dist/cjs/src/transaction/chaintrackers/WhatsOnChain.js +2 -2
  79. package/dist/cjs/src/transaction/chaintrackers/WhatsOnChain.js.map +1 -1
  80. package/dist/cjs/src/transaction/http/FetchHttpClient.js +1 -2
  81. package/dist/cjs/src/transaction/http/FetchHttpClient.js.map +1 -1
  82. package/dist/cjs/src/wallet/CachedKeyDeriver.js +13 -2
  83. package/dist/cjs/src/wallet/CachedKeyDeriver.js.map +1 -1
  84. package/dist/cjs/src/wallet/KeyDeriver.js +12 -8
  85. package/dist/cjs/src/wallet/KeyDeriver.js.map +1 -1
  86. package/dist/cjs/src/wallet/ProtoWallet.js +26 -27
  87. package/dist/cjs/src/wallet/ProtoWallet.js.map +1 -1
  88. package/dist/cjs/src/wallet/substrates/HTTPWalletJSON.js +2 -3
  89. package/dist/cjs/src/wallet/substrates/HTTPWalletJSON.js.map +1 -1
  90. package/dist/cjs/src/wallet/substrates/HTTPWalletWire.js +1 -1
  91. package/dist/cjs/src/wallet/substrates/HTTPWalletWire.js.map +1 -1
  92. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js +12 -19
  93. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js.map +1 -1
  94. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  95. package/dist/esm/src/auth/certificates/__tests/CompletedProtoWallet.js +2 -2
  96. package/dist/esm/src/auth/certificates/__tests/CompletedProtoWallet.js.map +1 -1
  97. package/dist/esm/src/primitives/ECDSA.js +174 -395
  98. package/dist/esm/src/primitives/ECDSA.js.map +1 -1
  99. package/dist/esm/src/primitives/Point.js +254 -91
  100. package/dist/esm/src/primitives/Point.js.map +1 -1
  101. package/dist/esm/src/primitives/PrivateKey.js +19 -2
  102. package/dist/esm/src/primitives/PrivateKey.js.map +1 -1
  103. package/dist/esm/src/primitives/PublicKey.js +19 -2
  104. package/dist/esm/src/primitives/PublicKey.js.map +1 -1
  105. package/dist/esm/src/wallet/CachedKeyDeriver.js +20 -1
  106. package/dist/esm/src/wallet/CachedKeyDeriver.js.map +1 -1
  107. package/dist/esm/src/wallet/KeyDeriver.js +14 -6
  108. package/dist/esm/src/wallet/KeyDeriver.js.map +1 -1
  109. package/dist/esm/src/wallet/ProtoWallet.js +2 -2
  110. package/dist/esm/src/wallet/ProtoWallet.js.map +1 -1
  111. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  112. package/dist/types/src/auth/certificates/__tests/CompletedProtoWallet.d.ts.map +1 -1
  113. package/dist/types/src/primitives/ECDSA.d.ts.map +1 -1
  114. package/dist/types/src/primitives/Point.d.ts.map +1 -1
  115. package/dist/types/src/primitives/PrivateKey.d.ts +3 -1
  116. package/dist/types/src/primitives/PrivateKey.d.ts.map +1 -1
  117. package/dist/types/src/primitives/PublicKey.d.ts +3 -1
  118. package/dist/types/src/primitives/PublicKey.d.ts.map +1 -1
  119. package/dist/types/src/wallet/CachedKeyDeriver.d.ts +10 -2
  120. package/dist/types/src/wallet/CachedKeyDeriver.d.ts.map +1 -1
  121. package/dist/types/src/wallet/KeyDeriver.d.ts +5 -2
  122. package/dist/types/src/wallet/KeyDeriver.d.ts.map +1 -1
  123. package/dist/types/src/wallet/ProtoWallet.d.ts.map +1 -1
  124. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  125. package/dist/umd/bundle.js +1 -1
  126. package/docs/reference/primitives.md +179 -383
  127. package/docs/reference/wallet.md +25 -6
  128. package/package.json +1 -1
  129. package/src/auth/certificates/__tests/CompletedProtoWallet.ts +3 -2
  130. package/src/primitives/ECDSA.ts +218 -488
  131. package/src/primitives/Point.ts +291 -94
  132. package/src/primitives/PrivateKey.ts +22 -2
  133. package/src/primitives/PublicKey.ts +22 -2
  134. package/src/transaction/__tests/Transaction.test.ts +1 -1
  135. package/src/wallet/CachedKeyDeriver.ts +32 -8
  136. package/src/wallet/KeyDeriver.ts +24 -7
  137. package/src/wallet/ProtoWallet.ts +3 -2
  138. package/src/wallet/__tests/ProtoWallet.test.ts +46 -1
@@ -2396,7 +2396,7 @@ export default class PrivateKey extends BigNumber {
2396
2396
  toHex(): string
2397
2397
  toString(base: number | "hex" = "hex", padding: number = 64): string
2398
2398
  deriveSharedSecret(key: PublicKey): Point
2399
- deriveChild(publicKey: PublicKey, invoiceNumber: string): PrivateKey
2399
+ deriveChild(publicKey: PublicKey, invoiceNumber: string, cacheSharedSecret?: ((priv: PrivateKey, pub: Point, point: Point) => void), retrieveCachedSharedSecret?: ((priv: PrivateKey, pub: Point) => (Point | undefined))): PrivateKey
2400
2400
  toKeyShares(threshold: number, totalShares: number): KeyShares
2401
2401
  toBackupShares(threshold: number, totalShares: number): string[]
2402
2402
  static fromBackupShares(shares: string[]): PrivateKey
@@ -2453,9 +2453,9 @@ Returns
2453
2453
  Derives a child key with BRC-42.
2454
2454
 
2455
2455
  ```ts
2456
- deriveChild(publicKey: PublicKey, invoiceNumber: string): PrivateKey
2456
+ deriveChild(publicKey: PublicKey, invoiceNumber: string, cacheSharedSecret?: ((priv: PrivateKey, pub: Point, point: Point) => void), retrieveCachedSharedSecret?: ((priv: PrivateKey, pub: Point) => (Point | undefined))): PrivateKey
2457
2457
  ```
2458
- See also: [PrivateKey](./primitives.md#class-privatekey), [PublicKey](./primitives.md#class-publickey)
2458
+ See also: [Point](./primitives.md#class-point), [PrivateKey](./primitives.md#class-privatekey), [PublicKey](./primitives.md#class-publickey)
2459
2459
 
2460
2460
  Returns
2461
2461
 
@@ -2467,6 +2467,10 @@ Argument Details
2467
2467
  + The public key of the other party
2468
2468
  + **invoiceNumber**
2469
2469
  + The invoice number used to derive the child key
2470
+ + **cacheSharedSecret**
2471
+ + Optional function to cache shared secrets
2472
+ + **retrieveCachedSharedSecret**
2473
+ + Optional function to retrieve shared secrets from the cache
2470
2474
 
2471
2475
  #### Method deriveSharedSecret
2472
2476
 
@@ -2885,7 +2889,7 @@ export default class PublicKey extends Point {
2885
2889
  toDER(enc?: "hex" | undefined): number[] | string
2886
2890
  toHash(enc?: "hex"): number[] | string
2887
2891
  toAddress(prefix: number[] | string = [0]): string
2888
- deriveChild(privateKey: PrivateKey, invoiceNumber: string): PublicKey
2892
+ deriveChild(privateKey: PrivateKey, invoiceNumber: string, cacheSharedSecret?: ((priv: PrivateKey, pub: Point, point: Point) => void), retrieveCachedSharedSecret?: ((priv: PrivateKey, pub: Point) => (Point | undefined))): PublicKey
2889
2893
  static fromMsgHashAndCompactSignature(msgHash: BigNumber, signature: number[] | string, enc?: "hex" | "base64"): PublicKey
2890
2894
  }
2891
2895
  ```
@@ -2920,9 +2924,9 @@ new PublicKey('abc123', 'def456');
2920
2924
  Derives a child key with BRC-42.
2921
2925
 
2922
2926
  ```ts
2923
- deriveChild(privateKey: PrivateKey, invoiceNumber: string): PublicKey
2927
+ deriveChild(privateKey: PrivateKey, invoiceNumber: string, cacheSharedSecret?: ((priv: PrivateKey, pub: Point, point: Point) => void), retrieveCachedSharedSecret?: ((priv: PrivateKey, pub: Point) => (Point | undefined))): PublicKey
2924
2928
  ```
2925
- See also: [PrivateKey](./primitives.md#class-privatekey), [PublicKey](./primitives.md#class-publickey)
2929
+ See also: [Point](./primitives.md#class-point), [PrivateKey](./primitives.md#class-privatekey), [PublicKey](./primitives.md#class-publickey)
2926
2930
 
2927
2931
  Returns
2928
2932
 
@@ -2934,6 +2938,10 @@ Argument Details
2934
2938
  + The private key of the other party
2935
2939
  + **invoiceNumber**
2936
2940
  + The invoice number used to derive the child key
2941
+ + **cacheSharedSecret**
2942
+ + Optional function to cache shared secrets
2943
+ + **retrieveCachedSharedSecret**
2944
+ + Optional function to retrieve shared secrets from the cache
2937
2945
 
2938
2946
  #### Method deriveSharedSecret
2939
2947
 
@@ -5329,257 +5337,67 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5329
5337
 
5330
5338
  ```ts
5331
5339
  sign = (msg: BigNumber, key: BigNumber, forceLowS: boolean = false, customK?: BigNumber | ((iter: number) => BigNumber)): Signature => {
5332
- if (typeof BigInt === "function") {
5333
- const zero = BigInt(0);
5334
- const one = BigInt(1);
5335
- const two = BigInt(2);
5336
- const n = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
5337
- const p = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F");
5338
- const Gx = BigInt("0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798");
5339
- const Gy = BigInt("0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8");
5340
- const G = { x: Gx, y: Gy };
5341
- const z = BigInt("0x" + msg.toString(16));
5342
- const d = BigInt("0x" + key.toString(16));
5343
- if (d <= zero || d >= n) {
5344
- throw new Error("Invalid private key");
5340
+ const curve = new Curve();
5341
+ msg = truncateToN(msg);
5342
+ const bytes = curve.n.byteLength();
5343
+ const bkey = key.toArray("be", bytes);
5344
+ const nonce = msg.toArray("be", bytes);
5345
+ const drbg = new DRBG(bkey, nonce);
5346
+ const ns1 = curve.n.subn(1);
5347
+ for (let iter = 0;; iter++) {
5348
+ let k = typeof customK === "function"
5349
+ ? customK(iter)
5350
+ : BigNumber.isBN(customK)
5351
+ ? customK
5352
+ : new BigNumber(drbg.generate(bytes), 16);
5353
+ if (k != null) {
5354
+ k = truncateToN(k, true);
5345
5355
  }
5346
- function bigIntToBytes(value: bigint, length: number): Uint8Array {
5347
- const hex = value.toString(16).padStart(length * 2, "0");
5348
- const bytes = new Uint8Array(length);
5349
- for (let i = 0; i < length; i++) {
5350
- bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
5351
- }
5352
- return bytes;
5356
+ else {
5357
+ throw new Error("k is undefined");
5353
5358
  }
5354
- const bytes = 32;
5355
- const bkey = bigIntToBytes(d, bytes);
5356
- const nonce = bigIntToBytes(z, bytes);
5357
- const drbg = new DRBG(Array.from(bkey), Array.from(nonce));
5358
- const ns1 = n - one;
5359
- let iter = 0;
5360
- function truncateToN(k: bigint, n: bigint, truncOnly: boolean = true): bigint {
5361
- const kBitLength = k.toString(2).length;
5362
- const nBitLength = n.toString(2).length;
5363
- const delta = kBitLength - nBitLength;
5364
- if (delta > 0) {
5365
- k = k >> BigInt(delta);
5366
- }
5367
- if (!truncOnly && k >= n) {
5368
- return k - n;
5359
+ if (k.cmpn(1) <= 0 || k.cmp(ns1) >= 0) {
5360
+ if (BigNumber.isBN(customK)) {
5361
+ throw new Error("Invalid fixed custom K value (must be more than 1 and less than N-1)");
5369
5362
  }
5370
5363
  else {
5371
- return k;
5364
+ continue;
5372
5365
  }
5373
5366
  }
5374
- function generateK(customK?: BigNumber | ((iter: number) => BigNumber)): bigint {
5375
- if (typeof customK === "function") {
5376
- const kbn = customK(iter);
5377
- const kstr = kbn.toString(16);
5378
- return BigInt("0x" + kstr);
5379
- }
5380
- else if ((customK != null) && BigNumber.isBN(customK)) {
5381
- const kstr = customK.toString(16);
5382
- return BigInt("0x" + kstr);
5367
+ const kp = curve.g.mul(k);
5368
+ if (kp.isInfinity()) {
5369
+ if (BigNumber.isBN(customK)) {
5370
+ throw new Error("Invalid fixed custom K value (must not create a point at infinity when multiplied by the generator point)");
5383
5371
  }
5384
5372
  else {
5385
- const khex = drbg.generate(bytes);
5386
- return BigInt("0x" + khex);
5387
- }
5388
- }
5389
- function mod(a: bigint, m: bigint): bigint {
5390
- return ((a % m) + m) % m;
5391
- }
5392
- function modInv(a: bigint, m: bigint): bigint {
5393
- let lm = one;
5394
- let hm = zero;
5395
- let low = mod(a, m);
5396
- let high = m;
5397
- while (low > one) {
5398
- const r = high / low;
5399
- const nm = hm - lm * r;
5400
- const neww = high - low * r;
5401
- hm = lm;
5402
- lm = nm;
5403
- high = low;
5404
- low = neww;
5373
+ continue;
5405
5374
  }
5406
- return mod(lm, m);
5407
5375
  }
5408
- function pointAdd(P: {
5409
- x: bigint;
5410
- y: bigint;
5411
- } | null, Q: {
5412
- x: bigint;
5413
- y: bigint;
5414
- } | null): {
5415
- x: bigint;
5416
- y: bigint;
5417
- } | null {
5418
- if (P === null)
5419
- return Q;
5420
- if (Q === null)
5421
- return P;
5422
- if (P.x === Q.x && P.y === mod(-Q.y, p)) {
5423
- return null;
5424
- }
5425
- let m: bigint;
5426
- if (P.x === Q.x && P.y === Q.y) {
5427
- if (P.y === zero) {
5428
- return null;
5429
- }
5430
- const numerator = mod(BigInt(3) * P.x * P.x, p);
5431
- const denominator = modInv(two * P.y, p);
5432
- m = mod(numerator * denominator, p);
5376
+ const kpX = kp.getX();
5377
+ const r = kpX.umod(curve.n);
5378
+ if (r.cmpn(0) === 0) {
5379
+ if (BigNumber.isBN(customK)) {
5380
+ throw new Error("Invalid fixed custom K value (when multiplied by G, the resulting x coordinate mod N must not be zero)");
5433
5381
  }
5434
5382
  else {
5435
- const numerator = mod(Q.y - P.y, p);
5436
- const denominator = modInv(Q.x - P.x, p);
5437
- m = mod(numerator * denominator, p);
5383
+ continue;
5438
5384
  }
5439
- const xR = mod(m * m - P.x - Q.x, p);
5440
- const yR = mod(m * (P.x - xR) - P.y, p);
5441
- return { x: xR, y: yR };
5442
- }
5443
- function scalarMul(k: bigint, P: {
5444
- x: bigint;
5445
- y: bigint;
5446
- }): {
5447
- x: bigint;
5448
- y: bigint;
5449
- } {
5450
- let N = P;
5451
- let Q: {
5452
- x: bigint;
5453
- y: bigint;
5454
- } | null = null;
5455
- while (k > BigInt(0)) {
5456
- if (k % BigInt(2) === BigInt(1)) {
5457
- Q = Q === null ? N : (pointAdd(Q, N) ?? Q);
5458
- }
5459
- N = pointAdd(N, N) ?? N;
5460
- k >>= BigInt(1);
5461
- }
5462
- if (Q === null) {
5463
- throw new Error("Scalar multiplication resulted in an invalid point.");
5464
- }
5465
- return Q;
5466
5385
  }
5467
- let validSignature = false;
5468
- while (!validSignature) {
5469
- iter += 1;
5470
- validSignature = true;
5471
- iter += 1;
5472
- let k = generateK(customK);
5473
- k = truncateToN(k, n, true);
5474
- if (k <= one || k >= ns1) {
5475
- if (customK instanceof BigNumber) {
5476
- throw new Error("Invalid fixed custom K value (must be more than 1 and less than N-1)");
5477
- }
5478
- else {
5479
- continue;
5480
- }
5481
- }
5482
- const R = scalarMul(k, G);
5483
- if (R === null) {
5484
- if (customK instanceof BigNumber) {
5485
- throw new Error("Invalid fixed custom K value (must not create a point at infinity when multiplied by the generator point)");
5486
- }
5487
- else {
5488
- continue;
5489
- }
5490
- }
5491
- const r = mod(R.x, n);
5492
- if (r === zero) {
5493
- if (customK instanceof BigNumber) {
5494
- throw new Error("Invalid fixed custom K value (when multiplied by G, the resulting x coordinate mod N must not be zero)");
5495
- }
5496
- else {
5497
- continue;
5498
- }
5499
- }
5500
- const kInv = modInv(k, n);
5501
- const rd = mod(r * d, n);
5502
- let s = mod(kInv * (z + rd), n);
5503
- if (s === zero) {
5504
- if (customK instanceof BigNumber) {
5505
- throw new Error("Invalid fixed custom K value (when used with the key, it cannot create a zero value for S)");
5506
- }
5507
- else {
5508
- continue;
5509
- }
5510
- }
5511
- if (forceLowS && s > n / two) {
5512
- s = n - s;
5513
- }
5514
- const rbn = new BigNumber(r.toString(16), 16);
5515
- const sbn = new BigNumber(s.toString(16), 16);
5516
- return new Signature(rbn, sbn);
5517
- }
5518
- }
5519
- else {
5520
- const curve = new Curve();
5521
- msg = truncateToN(msg);
5522
- const bytes = curve.n.byteLength();
5523
- const bkey = key.toArray("be", bytes);
5524
- const nonce = msg.toArray("be", bytes);
5525
- const drbg = new DRBG(bkey, nonce);
5526
- const ns1 = curve.n.subn(1);
5527
- for (let iter = 0;; iter++) {
5528
- let k = typeof customK === "function"
5529
- ? customK(iter)
5530
- : BigNumber.isBN(customK)
5531
- ? customK
5532
- : new BigNumber(drbg.generate(bytes), 16);
5533
- if (k != null) {
5534
- k = truncateToN(k, true);
5386
+ let s = k.invm(curve.n).mul(r.mul(key).iadd(msg));
5387
+ s = s.umod(curve.n);
5388
+ if (s.cmpn(0) === 0) {
5389
+ if (BigNumber.isBN(customK)) {
5390
+ throw new Error("Invalid fixed custom K value (when used with the key, it cannot create a zero value for S)");
5535
5391
  }
5536
5392
  else {
5537
- throw new Error("k is undefined");
5538
- }
5539
- if (k.cmpn(1) <= 0 || k.cmp(ns1) >= 0) {
5540
- if (BigNumber.isBN(customK)) {
5541
- throw new Error("Invalid fixed custom K value (must be more than 1 and less than N-1)");
5542
- }
5543
- else {
5544
- continue;
5545
- }
5546
- }
5547
- const kp = curve.g.mul(k);
5548
- if (kp.isInfinity()) {
5549
- if (BigNumber.isBN(customK)) {
5550
- throw new Error("Invalid fixed custom K value (must not create a point at infinity when multiplied by the generator point)");
5551
- }
5552
- else {
5553
- continue;
5554
- }
5555
- }
5556
- const kpX = kp.getX();
5557
- const r = kpX.umod(curve.n);
5558
- if (r.cmpn(0) === 0) {
5559
- if (BigNumber.isBN(customK)) {
5560
- throw new Error("Invalid fixed custom K value (when multiplied by G, the resulting x coordinate mod N must not be zero)");
5561
- }
5562
- else {
5563
- continue;
5564
- }
5565
- }
5566
- let s = k.invm(curve.n).mul(r.mul(key).iadd(msg));
5567
- s = s.umod(curve.n);
5568
- if (s.cmpn(0) === 0) {
5569
- if (BigNumber.isBN(customK)) {
5570
- throw new Error("Invalid fixed custom K value (when used with the key, it cannot create a zero value for S)");
5571
- }
5572
- else {
5573
- continue;
5574
- }
5393
+ continue;
5575
5394
  }
5576
- if (forceLowS && s.cmp(curve.n.ushrn(1)) > 0) {
5577
- s = curve.n.sub(s);
5578
- }
5579
- return new Signature(r, s);
5580
5395
  }
5396
+ if (forceLowS && s.cmp(curve.n.ushrn(1)) > 0) {
5397
+ s = curve.n.sub(s);
5398
+ }
5399
+ return new Signature(r, s);
5581
5400
  }
5582
- throw new Error("Failed to generate a valid signature");
5583
5401
  }
5584
5402
  ```
5585
5403
 
@@ -5733,167 +5551,145 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5733
5551
 
5734
5552
  ```ts
5735
5553
  verify = (msg: BigNumber, sig: Signature, key: Point): boolean => {
5736
- if (typeof BigInt === "function") {
5737
- const zero = BigInt(0);
5738
- const one = BigInt(1);
5739
- const two = BigInt(2);
5740
- const three = BigInt(3);
5741
- const p = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F");
5742
- const n = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
5743
- const G = {
5744
- x: BigInt("0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"),
5745
- y: BigInt("0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8")
5746
- };
5747
- const mod = (a: bigint, m: bigint): bigint => ((a % m) + m) % m;
5748
- const modInv = (a: bigint, m: bigint): bigint => {
5749
- let [oldr, r] = [a, m];
5750
- let [olds, s] = [BigInt(1), BigInt(0)];
5751
- while (r !== zero) {
5752
- const q = oldr / r;
5753
- [oldr, r] = [r, oldr - q * r];
5754
- [olds, s] = [s, olds - q * s];
5755
- }
5756
- if (oldr > one)
5757
- return zero;
5758
- return mod(olds, m);
5759
- };
5760
- const modMul = (a: bigint, b: bigint, m: bigint): bigint => mod(a * b, m);
5761
- const modSub = (a: bigint, b: bigint, m: bigint): bigint => mod(a - b, m);
5762
- const four = BigInt(4);
5763
- const eight = BigInt(8);
5764
- interface JacobianPoint {
5765
- X: bigint;
5766
- Y: bigint;
5767
- Z: bigint;
5554
+ const zero = BigInt(0);
5555
+ const one = BigInt(1);
5556
+ const two = BigInt(2);
5557
+ const three = BigInt(3);
5558
+ const p = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F");
5559
+ const n = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
5560
+ const G = {
5561
+ x: BigInt("0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"),
5562
+ y: BigInt("0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8")
5563
+ };
5564
+ const mod = (a: bigint, m: bigint): bigint => ((a % m) + m) % m;
5565
+ const modInv = (a: bigint, m: bigint): bigint => {
5566
+ let [oldr, r] = [a, m];
5567
+ let [olds, s] = [BigInt(1), BigInt(0)];
5568
+ while (r !== zero) {
5569
+ const q = oldr / r;
5570
+ [oldr, r] = [r, oldr - q * r];
5571
+ [olds, s] = [s, olds - q * s];
5768
5572
  }
5769
- const pointDouble = (P: JacobianPoint): JacobianPoint => {
5770
- const { X: X1, Y: Y1, Z: Z1 } = P;
5771
- if (Y1 === zero) {
5772
- return { X: zero, Y: one, Z: zero };
5773
- }
5774
- const Y1sq = modMul(Y1, Y1, p);
5775
- const S = modMul(four, modMul(X1, Y1sq, p), p);
5776
- const M = modMul(three, modMul(X1, X1, p), p);
5777
- const X3 = modSub(modMul(M, M, p), modMul(two, S, p), p);
5778
- const Y3 = modSub(modMul(M, modSub(S, X3, p), p), modMul(eight, modMul(Y1sq, Y1sq, p), p), p);
5779
- const Z3 = modMul(two, modMul(Y1, Z1, p), p);
5780
- return { X: X3, Y: Y3, Z: Z3 };
5781
- };
5782
- const pointAdd = (P: JacobianPoint, Q: JacobianPoint): JacobianPoint => {
5783
- if (P.Z === zero)
5784
- return Q;
5785
- if (Q.Z === zero)
5786
- return P;
5787
- const Z1Z1 = modMul(P.Z, P.Z, p);
5788
- const Z2Z2 = modMul(Q.Z, Q.Z, p);
5789
- const U1 = modMul(P.X, Z2Z2, p);
5790
- const U2 = modMul(Q.X, Z1Z1, p);
5791
- const S1 = modMul(P.Y, modMul(Z2Z2, Q.Z, p), p);
5792
- const S2 = modMul(Q.Y, modMul(Z1Z1, P.Z, p), p);
5793
- const H = modSub(U2, U1, p);
5794
- const r = modSub(S2, S1, p);
5795
- if (H === zero) {
5796
- if (r === zero) {
5797
- return pointDouble(P);
5798
- }
5799
- else {
5800
- return { X: zero, Y: one, Z: zero };
5801
- }
5802
- }
5803
- const HH = modMul(H, H, p);
5804
- const HHH = modMul(H, HH, p);
5805
- const V = modMul(U1, HH, p);
5806
- const X3 = modSub(modSub(modMul(r, r, p), HHH, p), modMul(two, V, p), p);
5807
- const Y3 = modSub(modMul(r, modSub(V, X3, p), p), modMul(S1, HHH, p), p);
5808
- const Z3 = modMul(H, modMul(P.Z, Q.Z, p), p);
5809
- return { X: X3, Y: Y3, Z: Z3 };
5810
- };
5811
- const scalarMultiply = (k: bigint, P: {
5812
- x: bigint;
5813
- y: bigint;
5814
- }): JacobianPoint => {
5815
- const N: JacobianPoint = { X: P.x, Y: P.y, Z: one };
5816
- let Q: JacobianPoint = { X: zero, Y: one, Z: zero };
5817
- const kBin = k.toString(2);
5818
- for (let i = 0; i < kBin.length; i++) {
5819
- Q = pointDouble(Q);
5820
- if (kBin[i] === "1") {
5821
- Q = pointAdd(Q, N);
5822
- }
5823
- }
5573
+ if (oldr > one)
5574
+ return zero;
5575
+ return mod(olds, m);
5576
+ };
5577
+ const modMul = (a: bigint, b: bigint, m: bigint): bigint => mod(a * b, m);
5578
+ const modSub = (a: bigint, b: bigint, m: bigint): bigint => mod(a - b, m);
5579
+ const four = BigInt(4);
5580
+ const eight = BigInt(8);
5581
+ interface JacobianPoint {
5582
+ X: bigint;
5583
+ Y: bigint;
5584
+ Z: bigint;
5585
+ }
5586
+ const pointDouble = (P: JacobianPoint): JacobianPoint => {
5587
+ const { X: X1, Y: Y1, Z: Z1 } = P;
5588
+ if (Y1 === zero) {
5589
+ return { X: zero, Y: one, Z: zero };
5590
+ }
5591
+ const Y1sq = modMul(Y1, Y1, p);
5592
+ const S = modMul(four, modMul(X1, Y1sq, p), p);
5593
+ const M = modMul(three, modMul(X1, X1, p), p);
5594
+ const X3 = modSub(modMul(M, M, p), modMul(two, S, p), p);
5595
+ const Y3 = modSub(modMul(M, modSub(S, X3, p), p), modMul(eight, modMul(Y1sq, Y1sq, p), p), p);
5596
+ const Z3 = modMul(two, modMul(Y1, Z1, p), p);
5597
+ return { X: X3, Y: Y3, Z: Z3 };
5598
+ };
5599
+ const pointAdd = (P: JacobianPoint, Q: JacobianPoint): JacobianPoint => {
5600
+ if (P.Z === zero)
5824
5601
  return Q;
5825
- };
5826
- const verifyECDSA = (hash: bigint, publicKey: {
5827
- x: bigint;
5828
- y: bigint;
5829
- }, signature: {
5830
- r: bigint;
5831
- s: bigint;
5832
- }): boolean => {
5833
- const { r, s } = signature;
5834
- const z = hash;
5835
- if (r <= zero || r >= n || s <= zero || s >= n) {
5836
- return false;
5837
- }
5838
- const w = modInv(s, n);
5839
- if (w === zero) {
5840
- return false;
5602
+ if (Q.Z === zero)
5603
+ return P;
5604
+ const Z1Z1 = modMul(P.Z, P.Z, p);
5605
+ const Z2Z2 = modMul(Q.Z, Q.Z, p);
5606
+ const U1 = modMul(P.X, Z2Z2, p);
5607
+ const U2 = modMul(Q.X, Z1Z1, p);
5608
+ const S1 = modMul(P.Y, modMul(Z2Z2, Q.Z, p), p);
5609
+ const S2 = modMul(Q.Y, modMul(Z1Z1, P.Z, p), p);
5610
+ const H = modSub(U2, U1, p);
5611
+ const r = modSub(S2, S1, p);
5612
+ if (H === zero) {
5613
+ if (r === zero) {
5614
+ return pointDouble(P);
5841
5615
  }
5842
- const u1 = modMul(z, w, n);
5843
- const u2 = modMul(r, w, n);
5844
- const RG = scalarMultiply(u1, G);
5845
- const RQ = scalarMultiply(u2, publicKey);
5846
- const R = pointAdd(RG, RQ);
5847
- if (R.Z === zero) {
5848
- return false;
5616
+ else {
5617
+ return { X: zero, Y: one, Z: zero };
5849
5618
  }
5850
- const ZInv = modInv(R.Z, p);
5851
- if (ZInv === zero) {
5852
- return false;
5619
+ }
5620
+ const HH = modMul(H, H, p);
5621
+ const HHH = modMul(H, HH, p);
5622
+ const V = modMul(U1, HH, p);
5623
+ const X3 = modSub(modSub(modMul(r, r, p), HHH, p), modMul(two, V, p), p);
5624
+ const Y3 = modSub(modMul(r, modSub(V, X3, p), p), modMul(S1, HHH, p), p);
5625
+ const Z3 = modMul(H, modMul(P.Z, Q.Z, p), p);
5626
+ return { X: X3, Y: Y3, Z: Z3 };
5627
+ };
5628
+ const scalarMultiply = (k: bigint, P: {
5629
+ x: bigint;
5630
+ y: bigint;
5631
+ }): JacobianPoint => {
5632
+ const N: JacobianPoint = { X: P.x, Y: P.y, Z: one };
5633
+ let Q: JacobianPoint = { X: zero, Y: one, Z: zero };
5634
+ const kBin = k.toString(2);
5635
+ for (let i = 0; i < kBin.length; i++) {
5636
+ Q = pointDouble(Q);
5637
+ if (kBin[i] === "1") {
5638
+ Q = pointAdd(Q, N);
5853
5639
  }
5854
- const ZInv2 = modMul(ZInv, ZInv, p);
5855
- const x1affine = modMul(R.X, ZInv2, p);
5856
- const v = mod(x1affine, n);
5857
- return v === r;
5858
- };
5859
- const hash = BigInt("0x" + msg.toString(16));
5860
- if ((key.x == null) || (key.y == null)) {
5861
- throw new Error("Invalid public key: missing coordinates.");
5862
5640
  }
5863
- const publicKey = {
5864
- x: BigInt("0x" + key.x.toString(16)),
5865
- y: BigInt("0x" + key.y.toString(16))
5866
- };
5867
- const signature = {
5868
- r: BigInt("0x" + sig.r.toString(16)),
5869
- s: BigInt("0x" + sig.s.toString(16))
5870
- };
5871
- return verifyECDSA(hash, publicKey, signature);
5872
- }
5873
- else {
5874
- const curve = new Curve();
5875
- msg = truncateToN(msg);
5876
- const r = sig.r;
5877
- const s = sig.s;
5878
- if (r.cmpn(1) < 0 || r.cmp(curve.n) >= 0) {
5641
+ return Q;
5642
+ };
5643
+ const verifyECDSA = (hash: bigint, publicKey: {
5644
+ x: bigint;
5645
+ y: bigint;
5646
+ }, signature: {
5647
+ r: bigint;
5648
+ s: bigint;
5649
+ }): boolean => {
5650
+ const { r, s } = signature;
5651
+ const z = hash;
5652
+ if (r <= zero || r >= n || s <= zero || s >= n) {
5653
+ return false;
5654
+ }
5655
+ const w = modInv(s, n);
5656
+ if (w === zero) {
5879
5657
  return false;
5880
5658
  }
5881
- if (s.cmpn(1) < 0 || s.cmp(curve.n) >= 0) {
5659
+ const u1 = modMul(z, w, n);
5660
+ const u2 = modMul(r, w, n);
5661
+ const RG = scalarMultiply(u1, G);
5662
+ const RQ = scalarMultiply(u2, publicKey);
5663
+ const R = pointAdd(RG, RQ);
5664
+ if (R.Z === zero) {
5882
5665
  return false;
5883
5666
  }
5884
- const sinv = s.invm(curve.n);
5885
- const u1 = sinv.mul(msg).umod(curve.n);
5886
- const u2 = sinv.mul(r).umod(curve.n);
5887
- const p = curve.g.jmulAdd(u1, key, u2);
5888
- if (p.isInfinity()) {
5667
+ const ZInv = modInv(R.Z, p);
5668
+ if (ZInv === zero) {
5889
5669
  return false;
5890
5670
  }
5891
- return p.eqXToP(r);
5671
+ const ZInv2 = modMul(ZInv, ZInv, p);
5672
+ const x1affine = modMul(R.X, ZInv2, p);
5673
+ const v = mod(x1affine, n);
5674
+ return v === r;
5675
+ };
5676
+ const hash = BigInt("0x" + msg.toString(16));
5677
+ if ((key.x == null) || (key.y == null)) {
5678
+ throw new Error("Invalid public key: missing coordinates.");
5892
5679
  }
5680
+ const publicKey = {
5681
+ x: BigInt("0x" + key.x.toString(16)),
5682
+ y: BigInt("0x" + key.y.toString(16))
5683
+ };
5684
+ const signature = {
5685
+ r: BigInt("0x" + sig.r.toString(16)),
5686
+ s: BigInt("0x" + sig.s.toString(16))
5687
+ };
5688
+ return verifyECDSA(hash, publicKey, signature);
5893
5689
  }
5894
5690
  ```
5895
5691
 
5896
- See also: [BigNumber](./primitives.md#class-bignumber), [Curve](./primitives.md#class-curve), [JacobianPoint](./primitives.md#class-jacobianpoint), [Point](./primitives.md#class-point), [Signature](./primitives.md#class-signature)
5692
+ See also: [BigNumber](./primitives.md#class-bignumber), [JacobianPoint](./primitives.md#class-jacobianpoint), [Point](./primitives.md#class-point), [Signature](./primitives.md#class-signature)
5897
5693
 
5898
5694
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5899
5695