@bsv/sdk 1.6.20 → 1.6.23

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.
@@ -4,6 +4,19 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
4
4
 
5
5
  ## Interfaces
6
6
 
7
+ ### Interface: JacobianPointBI
8
+
9
+ ```ts
10
+ export interface JacobianPointBI {
11
+ X: bigint;
12
+ Y: bigint;
13
+ Z: bigint;
14
+ }
15
+ ```
16
+
17
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
18
+
19
+ ---
7
20
  ## Classes
8
21
 
9
22
  | | | |
@@ -218,7 +231,7 @@ export default class BigNumber {
218
231
  }
219
232
  ```
220
233
 
221
- See also: [ReductionContext](./primitives.md#class-reductioncontext), [toArray](./primitives.md#variable-toarray), [toHex](./primitives.md#variable-tohex)
234
+ See also: [ReductionContext](./primitives.md#class-reductioncontext), [red](./primitives.md#function-red), [toArray](./primitives.md#variable-toarray), [toHex](./primitives.md#variable-tohex)
222
235
 
223
236
  #### Constructor
224
237
 
@@ -774,7 +787,7 @@ export default class Curve {
774
787
  }
775
788
  ```
776
789
 
777
- See also: [BigNumber](./primitives.md#class-bignumber), [Point](./primitives.md#class-point), [ReductionContext](./primitives.md#class-reductioncontext)
790
+ See also: [BigNumber](./primitives.md#class-bignumber), [Point](./primitives.md#class-point), [ReductionContext](./primitives.md#class-reductioncontext), [red](./primitives.md#function-red)
778
791
 
779
792
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
780
793
 
@@ -1700,7 +1713,6 @@ export default class Point extends BasePoint {
1700
1713
  inf: boolean;
1701
1714
  static fromDER(bytes: number[]): Point
1702
1715
  static fromString(str: string): Point
1703
- static redSqrtOptimized(y2: BigNumber): BigNumber
1704
1716
  static fromX(x: BigNumber | number | number[] | string, odd: boolean): Point
1705
1717
  static fromJSON(obj: string | any[], isRed: boolean): Point
1706
1718
  constructor(x: BigNumber | number | number[] | string | null, y: BigNumber | number | number[] | string | null, isRed: boolean = true)
@@ -2404,14 +2416,14 @@ export default class PrivateKey extends BigNumber {
2404
2416
  }
2405
2417
  ```
2406
2418
 
2407
- See also: [BigNumber](./primitives.md#class-bignumber), [KeyShares](./primitives.md#class-keyshares), [Point](./primitives.md#class-point), [PublicKey](./primitives.md#class-publickey), [Signature](./primitives.md#class-signature), [sign](./compat.md#variable-sign), [toHex](./primitives.md#variable-tohex), [verify](./compat.md#variable-verify)
2419
+ See also: [BigNumber](./primitives.md#class-bignumber), [KeyShares](./primitives.md#class-keyshares), [Point](./primitives.md#class-point), [PublicKey](./primitives.md#class-publickey), [Signature](./primitives.md#class-signature), [modN](./primitives.md#variable-modn), [sign](./compat.md#variable-sign), [toHex](./primitives.md#variable-tohex), [verify](./compat.md#variable-verify)
2408
2420
 
2409
2421
  #### Constructor
2410
2422
 
2411
2423
  ```ts
2412
2424
  constructor(number: BigNumber | number | string | number[] = 0, base: number | "be" | "le" | "hex" = 10, endian: "be" | "le" = "be", modN: "apply" | "nocheck" | "error" = "apply")
2413
2425
  ```
2414
- See also: [BigNumber](./primitives.md#class-bignumber)
2426
+ See also: [BigNumber](./primitives.md#class-bignumber), [modN](./primitives.md#variable-modn)
2415
2427
 
2416
2428
  Argument Details
2417
2429
 
@@ -2442,7 +2454,7 @@ checkInField(): {
2442
2454
  modN: BigNumber;
2443
2455
  }
2444
2456
  ```
2445
- See also: [BigNumber](./primitives.md#class-bignumber)
2457
+ See also: [BigNumber](./primitives.md#class-bignumber), [modN](./primitives.md#variable-modn)
2446
2458
 
2447
2459
  Returns
2448
2460
 
@@ -3884,41 +3896,14 @@ const sha256 = new SHA256();
3884
3896
  ```
3885
3897
 
3886
3898
  ```ts
3887
- export class SHA256 extends BaseHash {
3888
- h: number[];
3889
- W: number[];
3890
- k: number[];
3899
+ export class SHA256 {
3891
3900
  constructor()
3892
- _update(msg: number[], start?: number): void
3893
- _digest(): number[]
3894
- _digestHex(): string
3901
+ update(msg: number[] | string, enc?: "hex" | "utf8"): this
3902
+ digest(): number[]
3903
+ digestHex(): string
3895
3904
  }
3896
3905
  ```
3897
3906
 
3898
- #### Property W
3899
-
3900
- Provides a way to recycle usage of the array memory.
3901
-
3902
- ```ts
3903
- W: number[]
3904
- ```
3905
-
3906
- #### Property h
3907
-
3908
- The initial hash constants
3909
-
3910
- ```ts
3911
- h: number[]
3912
- ```
3913
-
3914
- #### Property k
3915
-
3916
- The round constants used for each round of SHA-256
3917
-
3918
- ```ts
3919
- k: number[]
3920
- ```
3921
-
3922
3907
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
3923
3908
 
3924
3909
  ---
@@ -3932,8 +3917,6 @@ This class also uses the SHA-256 cryptographic hash algorithm that produces a 25
3932
3917
 
3933
3918
  ```ts
3934
3919
  export class SHA256HMAC {
3935
- inner: SHA256;
3936
- outer: SHA256;
3937
3920
  blockSize = 64;
3938
3921
  outSize = 32;
3939
3922
  constructor(key: number[] | string)
@@ -3943,8 +3926,6 @@ export class SHA256HMAC {
3943
3926
  }
3944
3927
  ```
3945
3928
 
3946
- See also: [SHA256](./primitives.md#class-sha256)
3947
-
3948
3929
  #### Constructor
3949
3930
 
3950
3931
  The constructor for the `SHA256HMAC` class.
@@ -3976,15 +3957,6 @@ The block size for the SHA-256 hash function, in bytes. It's set to 64 bytes.
3976
3957
  blockSize = 64
3977
3958
  ```
3978
3959
 
3979
- #### Property inner
3980
-
3981
- Represents the inner hash of SHA-256.
3982
-
3983
- ```ts
3984
- inner: SHA256
3985
- ```
3986
- See also: [SHA256](./primitives.md#class-sha256)
3987
-
3988
3960
  #### Property outSize
3989
3961
 
3990
3962
  The output size of the SHA-256 hash function, in bytes. It's set to 32 bytes.
@@ -3993,15 +3965,6 @@ The output size of the SHA-256 hash function, in bytes. It's set to 32 bytes.
3993
3965
  outSize = 32
3994
3966
  ```
3995
3967
 
3996
- #### Property outer
3997
-
3998
- Represents the outer hash of SHA-256.
3999
-
4000
- ```ts
4001
- outer: SHA256
4002
- ```
4003
- See also: [SHA256](./primitives.md#class-sha256)
4004
-
4005
3968
  #### Method digest
4006
3969
 
4007
3970
  Finalizes the HMAC computation and returns the resultant hash.
@@ -4081,42 +4044,14 @@ const sha512 = new SHA512();
4081
4044
  ```
4082
4045
 
4083
4046
  ```ts
4084
- export class SHA512 extends BaseHash {
4085
- h: number[];
4086
- W: number[];
4087
- k: number[];
4047
+ export class SHA512 {
4088
4048
  constructor()
4089
- _prepareBlock(msg: number[], start: number): void
4090
- _update(msg: any, start: number): void
4091
- _digest(): number[]
4092
- _digestHex(): string
4049
+ update(msg: number[] | string, enc?: "hex" | "utf8"): this
4050
+ digest(): number[]
4051
+ digestHex(): string
4093
4052
  }
4094
4053
  ```
4095
4054
 
4096
- #### Property W
4097
-
4098
- Provides a way to recycle usage of the array memory.
4099
-
4100
- ```ts
4101
- W: number[]
4102
- ```
4103
-
4104
- #### Property h
4105
-
4106
- The initial hash constants.
4107
-
4108
- ```ts
4109
- h: number[]
4110
- ```
4111
-
4112
- #### Property k
4113
-
4114
- The round constants used for each round of SHA-512.
4115
-
4116
- ```ts
4117
- k: number[]
4118
- ```
4119
-
4120
4055
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4121
4056
 
4122
4057
  ---
@@ -4130,8 +4065,6 @@ This class also uses the SHA-512 cryptographic hash algorithm that produces a 51
4130
4065
 
4131
4066
  ```ts
4132
4067
  export class SHA512HMAC {
4133
- inner: SHA512;
4134
- outer: SHA512;
4135
4068
  blockSize = 128;
4136
4069
  outSize = 32;
4137
4070
  constructor(key: number[] | string)
@@ -4141,8 +4074,6 @@ export class SHA512HMAC {
4141
4074
  }
4142
4075
  ```
4143
4076
 
4144
- See also: [SHA512](./primitives.md#class-sha512)
4145
-
4146
4077
  #### Constructor
4147
4078
 
4148
4079
  The constructor for the `SHA512HMAC` class.
@@ -4174,15 +4105,6 @@ The block size for the SHA-512 hash function, in bytes. It's set to 128 bytes.
4174
4105
  blockSize = 128
4175
4106
  ```
4176
4107
 
4177
- #### Property inner
4178
-
4179
- Represents the inner hash of SHA-512.
4180
-
4181
- ```ts
4182
- inner: SHA512
4183
- ```
4184
- See also: [SHA512](./primitives.md#class-sha512)
4185
-
4186
4108
  #### Property outSize
4187
4109
 
4188
4110
  The output size of the SHA-512 hash function, in bytes. It's set to 64 bytes.
@@ -4191,15 +4113,6 @@ The output size of the SHA-512 hash function, in bytes. It's set to 64 bytes.
4191
4113
  outSize = 32
4192
4114
  ```
4193
4115
 
4194
- #### Property outer
4195
-
4196
- Represents the outer hash of SHA-512.
4197
-
4198
- ```ts
4199
- outer: SHA512
4200
- ```
4201
- See also: [SHA512](./primitives.md#class-sha512)
4202
-
4203
4116
  #### Method digest
4204
4117
 
4205
4118
  Finalizes the HMAC computation and returns the resultant hash.
@@ -4848,6 +4761,7 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
4848
4761
  | [AESGCMDecrypt](#function-aesgcmdecrypt) |
4849
4762
  | [ghash](#function-ghash) |
4850
4763
  | [pbkdf2](#function-pbkdf2) |
4764
+ | [red](#function-red) |
4851
4765
  | [toArray](#function-toarray) |
4852
4766
  | [toBase64](#function-tobase64) |
4853
4767
 
@@ -4921,6 +4835,15 @@ Argument Details
4921
4835
 
4922
4836
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4923
4837
 
4838
+ ---
4839
+ ### Function: red
4840
+
4841
+ ```ts
4842
+ export function red(x: bigint): bigint
4843
+ ```
4844
+
4845
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4846
+
4924
4847
  ---
4925
4848
  ### Function: toArray
4926
4849
 
@@ -4975,20 +4898,252 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
4975
4898
 
4976
4899
  | | | |
4977
4900
  | --- | --- | --- |
4978
- | [checkBit](#variable-checkbit) | [minimallyEncode](#variable-minimallyencode) | [sign](#variable-sign) |
4979
- | [encode](#variable-encode) | [multiply](#variable-multiply) | [toArray](#variable-toarray) |
4980
- | [exclusiveOR](#variable-exclusiveor) | [rightShift](#variable-rightshift) | [toBase58](#variable-tobase58) |
4981
- | [fromBase58](#variable-frombase58) | [ripemd160](#variable-ripemd160) | [toBase58Check](#variable-tobase58check) |
4982
- | [fromBase58Check](#variable-frombase58check) | [sha1](#variable-sha1) | [toHex](#variable-tohex) |
4983
- | [getBytes](#variable-getbytes) | [sha256](#variable-sha256) | [toUTF8](#variable-toutf8) |
4984
- | [hash160](#variable-hash160) | [sha256hmac](#variable-sha256hmac) | [verify](#variable-verify) |
4985
- | [hash256](#variable-hash256) | [sha512](#variable-sha512) | [zero2](#variable-zero2) |
4986
- | [incrementLeastSignificantThirtyTwoBits](#variable-incrementleastsignificantthirtytwobits) | [sha512hmac](#variable-sha512hmac) | |
4901
+ | [BI_EIGHT](#variable-bi_eight) | [biModSqrt](#variable-bimodsqrt) | [multiply](#variable-multiply) |
4902
+ | [BI_FOUR](#variable-bi_four) | [biModSub](#variable-bimodsub) | [rightShift](#variable-rightshift) |
4903
+ | [BI_ONE](#variable-bi_one) | [checkBit](#variable-checkbit) | [ripemd160](#variable-ripemd160) |
4904
+ | [BI_THREE](#variable-bi_three) | [encode](#variable-encode) | [scalarMultiplyWNAF](#variable-scalarmultiplywnaf) |
4905
+ | [BI_TWO](#variable-bi_two) | [exclusiveOR](#variable-exclusiveor) | [sha1](#variable-sha1) |
4906
+ | [BI_ZERO](#variable-bi_zero) | [fromBase58](#variable-frombase58) | [sha256](#variable-sha256) |
4907
+ | [GX_BIGINT](#variable-gx_bigint) | [fromBase58Check](#variable-frombase58check) | [sha256hmac](#variable-sha256hmac) |
4908
+ | [GY_BIGINT](#variable-gy_bigint) | [getBytes](#variable-getbytes) | [sha512](#variable-sha512) |
4909
+ | [MASK_256](#variable-mask_256) | [hash160](#variable-hash160) | [sha512hmac](#variable-sha512hmac) |
4910
+ | [N_BIGINT](#variable-n_bigint) | [hash256](#variable-hash256) | [sign](#variable-sign) |
4911
+ | [P_BIGINT](#variable-p_bigint) | [incrementLeastSignificantThirtyTwoBits](#variable-incrementleastsignificantthirtytwobits) | [toArray](#variable-toarray) |
4912
+ | [P_PLUS1_DIV4](#variable-p_plus1_div4) | [jpAdd](#variable-jpadd) | [toBase58](#variable-tobase58) |
4913
+ | [biMod](#variable-bimod) | [jpDouble](#variable-jpdouble) | [toBase58Check](#variable-tobase58check) |
4914
+ | [biModAdd](#variable-bimodadd) | [jpNeg](#variable-jpneg) | [toHex](#variable-tohex) |
4915
+ | [biModInv](#variable-bimodinv) | [minimallyEncode](#variable-minimallyencode) | [toUTF8](#variable-toutf8) |
4916
+ | [biModMul](#variable-bimodmul) | [modInvN](#variable-modinvn) | [verify](#variable-verify) |
4917
+ | [biModPow](#variable-bimodpow) | [modMulN](#variable-modmuln) | [zero2](#variable-zero2) |
4918
+ | [biModSqr](#variable-bimodsqr) | [modN](#variable-modn) | |
4919
+
4920
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4921
+
4922
+ ---
4923
+
4924
+ ### Variable: BI_EIGHT
4925
+
4926
+ ```ts
4927
+ BI_EIGHT = 8n
4928
+ ```
4929
+
4930
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4931
+
4932
+ ---
4933
+ ### Variable: BI_FOUR
4934
+
4935
+ ```ts
4936
+ BI_FOUR = 4n
4937
+ ```
4938
+
4939
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4940
+
4941
+ ---
4942
+ ### Variable: BI_ONE
4943
+
4944
+ ```ts
4945
+ BI_ONE = 1n
4946
+ ```
4947
+
4948
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4949
+
4950
+ ---
4951
+ ### Variable: BI_THREE
4952
+
4953
+ ```ts
4954
+ BI_THREE = 3n
4955
+ ```
4956
+
4957
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4958
+
4959
+ ---
4960
+ ### Variable: BI_TWO
4961
+
4962
+ ```ts
4963
+ BI_TWO = 2n
4964
+ ```
4965
+
4966
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4967
+
4968
+ ---
4969
+ ### Variable: BI_ZERO
4970
+
4971
+ ```ts
4972
+ BI_ZERO = 0n
4973
+ ```
4987
4974
 
4988
4975
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4989
4976
 
4990
4977
  ---
4978
+ ### Variable: GX_BIGINT
4979
+
4980
+ ```ts
4981
+ GX_BIGINT = BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")
4982
+ ```
4983
+
4984
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4985
+
4986
+ ---
4987
+ ### Variable: GY_BIGINT
4988
+
4989
+ ```ts
4990
+ GY_BIGINT = BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")
4991
+ ```
4992
+
4993
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4994
+
4995
+ ---
4996
+ ### Variable: MASK_256
4997
+
4998
+ ```ts
4999
+ MASK_256 = (1n << 256n) - 1n
5000
+ ```
5001
+
5002
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5003
+
5004
+ ---
5005
+ ### Variable: N_BIGINT
5006
+
5007
+ ```ts
5008
+ N_BIGINT = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n
5009
+ ```
5010
+
5011
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5012
+
5013
+ ---
5014
+ ### Variable: P_BIGINT
5015
+
5016
+ ```ts
5017
+ P_BIGINT = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn
5018
+ ```
5019
+
5020
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
4991
5021
 
5022
+ ---
5023
+ ### Variable: P_PLUS1_DIV4
5024
+
5025
+ ```ts
5026
+ P_PLUS1_DIV4 = (P_BIGINT + 1n) >> 2n
5027
+ ```
5028
+
5029
+ See also: [P_BIGINT](./primitives.md#variable-p_bigint)
5030
+
5031
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5032
+
5033
+ ---
5034
+ ### Variable: biMod
5035
+
5036
+ ```ts
5037
+ biMod = (a: bigint): bigint => red((a % P_BIGINT + P_BIGINT) % P_BIGINT)
5038
+ ```
5039
+
5040
+ See also: [P_BIGINT](./primitives.md#variable-p_bigint), [red](./primitives.md#function-red)
5041
+
5042
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5043
+
5044
+ ---
5045
+ ### Variable: biModAdd
5046
+
5047
+ ```ts
5048
+ biModAdd = (a: bigint, b: bigint): bigint => red(a + b)
5049
+ ```
5050
+
5051
+ See also: [red](./primitives.md#function-red)
5052
+
5053
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5054
+
5055
+ ---
5056
+ ### Variable: biModInv
5057
+
5058
+ ```ts
5059
+ biModInv = (a: bigint): bigint => {
5060
+ let lm = BI_ONE;
5061
+ let hm = BI_ZERO;
5062
+ let low = biMod(a);
5063
+ let high = P_BIGINT;
5064
+ while (low > BI_ONE) {
5065
+ const r = high / low;
5066
+ [lm, hm] = [hm - lm * r, lm];
5067
+ [low, high] = [high - low * r, low];
5068
+ }
5069
+ return biMod(lm);
5070
+ }
5071
+ ```
5072
+
5073
+ See also: [BI_ONE](./primitives.md#variable-bi_one), [BI_ZERO](./primitives.md#variable-bi_zero), [P_BIGINT](./primitives.md#variable-p_bigint), [biMod](./primitives.md#variable-bimod)
5074
+
5075
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5076
+
5077
+ ---
5078
+ ### Variable: biModMul
5079
+
5080
+ ```ts
5081
+ biModMul = (a: bigint, b: bigint): bigint => red(a * b)
5082
+ ```
5083
+
5084
+ See also: [red](./primitives.md#function-red)
5085
+
5086
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5087
+
5088
+ ---
5089
+ ### Variable: biModPow
5090
+
5091
+ ```ts
5092
+ biModPow = (base: bigint, exp: bigint): bigint => {
5093
+ let result = BI_ONE;
5094
+ base = biMod(base);
5095
+ let e = exp;
5096
+ while (e > BI_ZERO) {
5097
+ if ((e & BI_ONE) === BI_ONE)
5098
+ result = biModMul(result, base);
5099
+ base = biModMul(base, base);
5100
+ e >>= BI_ONE;
5101
+ }
5102
+ return result;
5103
+ }
5104
+ ```
5105
+
5106
+ See also: [BI_ONE](./primitives.md#variable-bi_one), [BI_ZERO](./primitives.md#variable-bi_zero), [biMod](./primitives.md#variable-bimod), [biModMul](./primitives.md#variable-bimodmul)
5107
+
5108
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5109
+
5110
+ ---
5111
+ ### Variable: biModSqr
5112
+
5113
+ ```ts
5114
+ biModSqr = (a: bigint): bigint => biModMul(a, a)
5115
+ ```
5116
+
5117
+ See also: [biModMul](./primitives.md#variable-bimodmul)
5118
+
5119
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5120
+
5121
+ ---
5122
+ ### Variable: biModSqrt
5123
+
5124
+ ```ts
5125
+ biModSqrt = (a: bigint): bigint | null => {
5126
+ const r = biModPow(a, P_PLUS1_DIV4);
5127
+ return biModMul(r, r) === biMod(a) ? r : null;
5128
+ }
5129
+ ```
5130
+
5131
+ See also: [P_PLUS1_DIV4](./primitives.md#variable-p_plus1_div4), [biMod](./primitives.md#variable-bimod), [biModMul](./primitives.md#variable-bimodmul), [biModPow](./primitives.md#variable-bimodpow)
5132
+
5133
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5134
+
5135
+ ---
5136
+ ### Variable: biModSub
5137
+
5138
+ ```ts
5139
+ biModSub = (a: bigint, b: bigint): bigint => (a >= b ? a - b : P_BIGINT - (b - a))
5140
+ ```
5141
+
5142
+ See also: [P_BIGINT](./primitives.md#variable-p_bigint)
5143
+
5144
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5145
+
5146
+ ---
4992
5147
  ### Variable: checkBit
4993
5148
 
4994
5149
  ```ts
@@ -5167,6 +5322,79 @@ incrementLeastSignificantThirtyTwoBits = function (block: number[]): number[] {
5167
5322
 
5168
5323
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5169
5324
 
5325
+ ---
5326
+ ### Variable: jpAdd
5327
+
5328
+ ```ts
5329
+ jpAdd = (P: JacobianPointBI, Q: JacobianPointBI): JacobianPointBI => {
5330
+ if (P.Z === BI_ZERO)
5331
+ return Q;
5332
+ if (Q.Z === BI_ZERO)
5333
+ return P;
5334
+ const Z1Z1 = biModMul(P.Z, P.Z);
5335
+ const Z2Z2 = biModMul(Q.Z, Q.Z);
5336
+ const U1 = biModMul(P.X, Z2Z2);
5337
+ const U2 = biModMul(Q.X, Z1Z1);
5338
+ const S1 = biModMul(P.Y, biModMul(Z2Z2, Q.Z));
5339
+ const S2 = biModMul(Q.Y, biModMul(Z1Z1, P.Z));
5340
+ const H = biModSub(U2, U1);
5341
+ const r = biModSub(S2, S1);
5342
+ if (H === BI_ZERO) {
5343
+ if (r === BI_ZERO)
5344
+ return jpDouble(P);
5345
+ return { X: BI_ZERO, Y: BI_ONE, Z: BI_ZERO };
5346
+ }
5347
+ const HH = biModMul(H, H);
5348
+ const HHH = biModMul(H, HH);
5349
+ const V = biModMul(U1, HH);
5350
+ const X3 = biModSub(biModSub(biModMul(r, r), HHH), biModMul(BI_TWO, V));
5351
+ const Y3 = biModSub(biModMul(r, biModSub(V, X3)), biModMul(S1, HHH));
5352
+ const Z3 = biModMul(H, biModMul(P.Z, Q.Z));
5353
+ return { X: X3, Y: Y3, Z: Z3 };
5354
+ }
5355
+ ```
5356
+
5357
+ See also: [BI_ONE](./primitives.md#variable-bi_one), [BI_TWO](./primitives.md#variable-bi_two), [BI_ZERO](./primitives.md#variable-bi_zero), [JacobianPointBI](./primitives.md#interface-jacobianpointbi), [biModMul](./primitives.md#variable-bimodmul), [biModSub](./primitives.md#variable-bimodsub), [jpDouble](./primitives.md#variable-jpdouble)
5358
+
5359
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5360
+
5361
+ ---
5362
+ ### Variable: jpDouble
5363
+
5364
+ ```ts
5365
+ jpDouble = (P: JacobianPointBI): JacobianPointBI => {
5366
+ const { X: X1, Y: Y1, Z: Z1 } = P;
5367
+ if (Y1 === BI_ZERO)
5368
+ return { X: BI_ZERO, Y: BI_ONE, Z: BI_ZERO };
5369
+ const Y1sq = biModMul(Y1, Y1);
5370
+ const S = biModMul(BI_FOUR, biModMul(X1, Y1sq));
5371
+ const M = biModMul(BI_THREE, biModMul(X1, X1));
5372
+ const X3 = biModSub(biModMul(M, M), biModMul(BI_TWO, S));
5373
+ const Y3 = biModSub(biModMul(M, biModSub(S, X3)), biModMul(BI_EIGHT, biModMul(Y1sq, Y1sq)));
5374
+ const Z3 = biModMul(BI_TWO, biModMul(Y1, Z1));
5375
+ return { X: X3, Y: Y3, Z: Z3 };
5376
+ }
5377
+ ```
5378
+
5379
+ See also: [BI_EIGHT](./primitives.md#variable-bi_eight), [BI_FOUR](./primitives.md#variable-bi_four), [BI_ONE](./primitives.md#variable-bi_one), [BI_THREE](./primitives.md#variable-bi_three), [BI_TWO](./primitives.md#variable-bi_two), [BI_ZERO](./primitives.md#variable-bi_zero), [JacobianPointBI](./primitives.md#interface-jacobianpointbi), [biModMul](./primitives.md#variable-bimodmul), [biModSub](./primitives.md#variable-bimodsub)
5380
+
5381
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5382
+
5383
+ ---
5384
+ ### Variable: jpNeg
5385
+
5386
+ ```ts
5387
+ jpNeg = (P: JacobianPointBI): JacobianPointBI => {
5388
+ if (P.Z === BI_ZERO)
5389
+ return P;
5390
+ return { X: P.X, Y: P_BIGINT - P.Y, Z: P.Z };
5391
+ }
5392
+ ```
5393
+
5394
+ See also: [BI_ZERO](./primitives.md#variable-bi_zero), [JacobianPointBI](./primitives.md#interface-jacobianpointbi), [P_BIGINT](./primitives.md#variable-p_bigint)
5395
+
5396
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5397
+
5170
5398
  ---
5171
5399
  ### Variable: minimallyEncode
5172
5400
 
@@ -5203,6 +5431,55 @@ minimallyEncode = (buf: number[]): number[] => {
5203
5431
 
5204
5432
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5205
5433
 
5434
+ ---
5435
+ ### Variable: modInvN
5436
+
5437
+ ```ts
5438
+ modInvN = (a: bigint): bigint => {
5439
+ let lm = 1n;
5440
+ let hm = 0n;
5441
+ let low = modN(a);
5442
+ let high = N_BIGINT;
5443
+ while (low > 1n) {
5444
+ const q = high / low;
5445
+ [lm, hm] = [hm - lm * q, lm];
5446
+ [low, high] = [high - low * q, low];
5447
+ }
5448
+ return modN(lm);
5449
+ }
5450
+ ```
5451
+
5452
+ See also: [N_BIGINT](./primitives.md#variable-n_bigint), [modN](./primitives.md#variable-modn)
5453
+
5454
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5455
+
5456
+ ---
5457
+ ### Variable: modMulN
5458
+
5459
+ ```ts
5460
+ modMulN = (a: bigint, b: bigint): bigint => modN(a * b)
5461
+ ```
5462
+
5463
+ See also: [modN](./primitives.md#variable-modn)
5464
+
5465
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5466
+
5467
+ ---
5468
+ ### Variable: modN
5469
+
5470
+ ```ts
5471
+ modN = (a: bigint): bigint => {
5472
+ let r = a % N_BIGINT;
5473
+ if (r < 0n)
5474
+ r += N_BIGINT;
5475
+ return r;
5476
+ }
5477
+ ```
5478
+
5479
+ See also: [N_BIGINT](./primitives.md#variable-n_bigint)
5480
+
5481
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5482
+
5206
5483
  ---
5207
5484
  ### Variable: multiply
5208
5485
 
@@ -5267,6 +5544,67 @@ See also: [RIPEMD160](./primitives.md#class-ripemd160)
5267
5544
 
5268
5545
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5269
5546
 
5547
+ ---
5548
+ ### Variable: scalarMultiplyWNAF
5549
+
5550
+ ```ts
5551
+ scalarMultiplyWNAF = (k: bigint, P0: {
5552
+ x: bigint;
5553
+ y: bigint;
5554
+ }, window: number = 5): JacobianPointBI => {
5555
+ const key = `${window}:${P0.x.toString(16)}:${P0.y.toString(16)}`;
5556
+ let tbl = WNAF_TABLE_CACHE.get(key);
5557
+ let P: JacobianPointBI;
5558
+ if (tbl === undefined) {
5559
+ const tblSize = 1 << (window - 1);
5560
+ tbl = new Array(tblSize);
5561
+ P = { X: P0.x, Y: P0.y, Z: BI_ONE };
5562
+ tbl[0] = P;
5563
+ const twoP = jpDouble(P);
5564
+ for (let i = 1; i < tblSize; i++) {
5565
+ tbl[i] = jpAdd(tbl[i - 1], twoP);
5566
+ }
5567
+ WNAF_TABLE_CACHE.set(key, tbl);
5568
+ }
5569
+ else {
5570
+ P = tbl[0];
5571
+ }
5572
+ const wnaf: number[] = [];
5573
+ const wBig = 1n << BigInt(window);
5574
+ const wHalf = wBig >> 1n;
5575
+ let kTmp = k;
5576
+ while (kTmp > 0n) {
5577
+ if ((kTmp & BI_ONE) === BI_ZERO) {
5578
+ wnaf.push(0);
5579
+ kTmp >>= BI_ONE;
5580
+ }
5581
+ else {
5582
+ let z = kTmp & (wBig - 1n);
5583
+ if (z > wHalf)
5584
+ z -= wBig;
5585
+ wnaf.push(Number(z));
5586
+ kTmp -= z;
5587
+ kTmp >>= BI_ONE;
5588
+ }
5589
+ }
5590
+ let Q: JacobianPointBI = { X: BI_ZERO, Y: BI_ONE, Z: BI_ZERO };
5591
+ for (let i = wnaf.length - 1; i >= 0; i--) {
5592
+ Q = jpDouble(Q);
5593
+ const di = wnaf[i];
5594
+ if (di !== 0) {
5595
+ const idx = Math.abs(di) >> 1;
5596
+ const addend = di > 0 ? tbl[idx] : jpNeg(tbl[idx]);
5597
+ Q = jpAdd(Q, addend);
5598
+ }
5599
+ }
5600
+ return Q;
5601
+ }
5602
+ ```
5603
+
5604
+ See also: [BI_ONE](./primitives.md#variable-bi_one), [BI_ZERO](./primitives.md#variable-bi_zero), [JacobianPointBI](./primitives.md#interface-jacobianpointbi), [jpAdd](./primitives.md#variable-jpadd), [jpDouble](./primitives.md#variable-jpdouble), [jpNeg](./primitives.md#variable-jpneg)
5605
+
5606
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5607
+
5270
5608
  ---
5271
5609
  ### Variable: sha1
5272
5610
 
@@ -5337,71 +5675,66 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5337
5675
 
5338
5676
  ```ts
5339
5677
  sign = (msg: BigNumber, key: BigNumber, forceLowS: boolean = false, customK?: BigNumber | ((iter: number) => BigNumber)): Signature => {
5340
- const curve = new Curve();
5341
5678
  msg = truncateToN(msg);
5342
- const bytes = curve.n.byteLength();
5679
+ const msgBig = BigInt("0x" + msg.toString(16));
5680
+ const keyBig = BigInt("0x" + key.toString(16));
5343
5681
  const bkey = key.toArray("be", bytes);
5344
5682
  const nonce = msg.toArray("be", bytes);
5345
5683
  const drbg = new DRBG(bkey, nonce);
5346
- const ns1 = curve.n.subn(1);
5347
5684
  for (let iter = 0;; iter++) {
5348
- let k = typeof customK === "function"
5685
+ let kBN = typeof customK === "function"
5349
5686
  ? customK(iter)
5350
5687
  : BigNumber.isBN(customK)
5351
5688
  ? customK
5352
5689
  : new BigNumber(drbg.generate(bytes), 16);
5353
- if (k != null) {
5354
- k = truncateToN(k, true);
5355
- }
5356
- else {
5690
+ if (kBN == null)
5357
5691
  throw new Error("k is undefined");
5358
- }
5359
- if (k.cmpn(1) <= 0 || k.cmp(ns1) >= 0) {
5692
+ kBN = truncateToN(kBN, true);
5693
+ if (kBN.cmpn(1) <= 0 || kBN.cmp(ns1) >= 0) {
5360
5694
  if (BigNumber.isBN(customK)) {
5361
- throw new Error("Invalid fixed custom K value (must be more than 1 and less than N-1)");
5362
- }
5363
- else {
5364
- continue;
5695
+ throw new Error("Invalid fixed custom K value (must be >1 and <N\u20111)");
5365
5696
  }
5697
+ continue;
5366
5698
  }
5367
- const kp = curve.g.mul(k);
5368
- if (kp.isInfinity()) {
5699
+ const kBig = BigInt("0x" + kBN.toString(16));
5700
+ const R = scalarMultiplyWNAF(kBig, { x: GX_BIGINT, y: GY_BIGINT });
5701
+ if (R.Z === 0n) {
5369
5702
  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)");
5371
- }
5372
- else {
5373
- continue;
5703
+ throw new Error("Invalid fixed custom K value (k\u00B7G at infinity)");
5374
5704
  }
5705
+ continue;
5375
5706
  }
5376
- const kpX = kp.getX();
5377
- const r = kpX.umod(curve.n);
5378
- if (r.cmpn(0) === 0) {
5707
+ const zInv = biModInv(R.Z);
5708
+ const zInv2 = biModMul(zInv, zInv);
5709
+ const xAff = biModMul(R.X, zInv2);
5710
+ const rBig = modN(xAff);
5711
+ if (rBig === 0n) {
5379
5712
  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)");
5381
- }
5382
- else {
5383
- continue;
5713
+ throw new Error("Invalid fixed custom K value (r == 0)");
5384
5714
  }
5715
+ continue;
5385
5716
  }
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) {
5717
+ const kInv = modInvN(kBig);
5718
+ const rTimesKey = modMulN(rBig, keyBig);
5719
+ const sum = modN(msgBig + rTimesKey);
5720
+ let sBig = modMulN(kInv, sum);
5721
+ if (sBig === 0n) {
5389
5722
  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)");
5391
- }
5392
- else {
5393
- continue;
5723
+ throw new Error("Invalid fixed custom K value (s == 0)");
5394
5724
  }
5725
+ continue;
5395
5726
  }
5396
- if (forceLowS && s.cmp(curve.n.ushrn(1)) > 0) {
5397
- s = curve.n.sub(s);
5727
+ if (forceLowS && sBig > halfN) {
5728
+ sBig = N_BIGINT - sBig;
5398
5729
  }
5730
+ const r = new BigNumber(rBig.toString(16), 16);
5731
+ const s = new BigNumber(sBig.toString(16), 16);
5399
5732
  return new Signature(r, s);
5400
5733
  }
5401
5734
  }
5402
5735
  ```
5403
5736
 
5404
- See also: [BigNumber](./primitives.md#class-bignumber), [Curve](./primitives.md#class-curve), [DRBG](./primitives.md#class-drbg), [Signature](./primitives.md#class-signature), [toArray](./primitives.md#variable-toarray)
5737
+ See also: [BigNumber](./primitives.md#class-bignumber), [DRBG](./primitives.md#class-drbg), [GX_BIGINT](./primitives.md#variable-gx_bigint), [GY_BIGINT](./primitives.md#variable-gy_bigint), [N_BIGINT](./primitives.md#variable-n_bigint), [Signature](./primitives.md#class-signature), [biModInv](./primitives.md#variable-bimodinv), [biModMul](./primitives.md#variable-bimodmul), [modInvN](./primitives.md#variable-modinvn), [modMulN](./primitives.md#variable-modmuln), [modN](./primitives.md#variable-modn), [scalarMultiplyWNAF](./primitives.md#variable-scalarmultiplywnaf), [toArray](./primitives.md#variable-toarray)
5405
5738
 
5406
5739
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5407
5740
 
@@ -5551,128 +5884,6 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5551
5884
 
5552
5885
  ```ts
5553
5886
  verify = (msg: BigNumber, sig: Signature, key: Point): boolean => {
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];
5572
- }
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)
5601
- return Q;
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);
5615
- }
5616
- else {
5617
- return { X: zero, Y: one, Z: zero };
5618
- }
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);
5639
- }
5640
- }
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) {
5657
- return false;
5658
- }
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) {
5665
- return false;
5666
- }
5667
- const ZInv = modInv(R.Z, p);
5668
- if (ZInv === zero) {
5669
- return false;
5670
- }
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
5887
  const hash = BigInt("0x" + msg.toString(16));
5677
5888
  if ((key.x == null) || (key.y == null)) {
5678
5889
  throw new Error("Invalid public key: missing coordinates.");
@@ -5685,11 +5896,30 @@ verify = (msg: BigNumber, sig: Signature, key: Point): boolean => {
5685
5896
  r: BigInt("0x" + sig.r.toString(16)),
5686
5897
  s: BigInt("0x" + sig.s.toString(16))
5687
5898
  };
5688
- return verifyECDSA(hash, publicKey, signature);
5899
+ const { r, s } = signature;
5900
+ const z = hash;
5901
+ if (r <= BI_ZERO || r >= N_BIGINT || s <= BI_ZERO || s >= N_BIGINT) {
5902
+ return false;
5903
+ }
5904
+ const w = modInvN(s);
5905
+ if (w === 0n)
5906
+ return false;
5907
+ const u1 = modMulN(z, w);
5908
+ const u2 = modMulN(r, w);
5909
+ const RG = scalarMultiplyWNAF(u1, { x: GX_BIGINT, y: GY_BIGINT });
5910
+ const RQ = scalarMultiplyWNAF(u2, publicKey);
5911
+ const R = jpAdd(RG, RQ);
5912
+ if (R.Z === 0n)
5913
+ return false;
5914
+ const zInv = biModInv(R.Z);
5915
+ const zInv2 = biModMul(zInv, zInv);
5916
+ const xAff = biModMul(R.X, zInv2);
5917
+ const v = modN(xAff);
5918
+ return v === r;
5689
5919
  }
5690
5920
  ```
5691
5921
 
5692
- See also: [BigNumber](./primitives.md#class-bignumber), [JacobianPoint](./primitives.md#class-jacobianpoint), [Point](./primitives.md#class-point), [Signature](./primitives.md#class-signature)
5922
+ See also: [BI_ZERO](./primitives.md#variable-bi_zero), [BigNumber](./primitives.md#class-bignumber), [GX_BIGINT](./primitives.md#variable-gx_bigint), [GY_BIGINT](./primitives.md#variable-gy_bigint), [N_BIGINT](./primitives.md#variable-n_bigint), [Point](./primitives.md#class-point), [Signature](./primitives.md#class-signature), [biModInv](./primitives.md#variable-bimodinv), [biModMul](./primitives.md#variable-bimodmul), [jpAdd](./primitives.md#variable-jpadd), [modInvN](./primitives.md#variable-modinvn), [modMulN](./primitives.md#variable-modmuln), [modN](./primitives.md#variable-modn), [scalarMultiplyWNAF](./primitives.md#variable-scalarmultiplywnaf)
5693
5923
 
5694
5924
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5695
5925