shogun-core 6.2.4 → 6.3.0

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 (233) hide show
  1. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js +93 -341
  2. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js.map +1 -1
  3. package/dist/browser/shogun-core.js +138979 -146789
  4. package/dist/browser/shogun-core.js.map +1 -1
  5. package/dist/{core.js → src/core.js} +167 -107
  6. package/dist/src/crypto/asymmetric.js +168 -0
  7. package/dist/src/crypto/double-ratchet.js +908 -0
  8. package/dist/src/crypto/file-encryption.js +352 -0
  9. package/dist/src/crypto/hashing.js +160 -0
  10. package/dist/src/crypto/index.js +18 -0
  11. package/dist/{crypto → src/crypto}/mls-codec.js +24 -34
  12. package/dist/src/crypto/mls.js +734 -0
  13. package/dist/src/crypto/pgp.js +619 -0
  14. package/dist/{crypto → src/crypto}/random-generation.js +125 -103
  15. package/dist/src/crypto/sframe.js +466 -0
  16. package/dist/src/crypto/signal-protocol.js +943 -0
  17. package/dist/src/crypto/symmetric.js +165 -0
  18. package/dist/src/crypto/utils.js +220 -0
  19. package/dist/src/examples/auth-test.js +535 -0
  20. package/dist/src/examples/crypto-identity-example.js +294 -0
  21. package/dist/src/examples/crypto-working-test.js +149 -0
  22. package/dist/src/examples/double-ratchet-test.js +240 -0
  23. package/dist/src/examples/mls-3-member-test.js +183 -0
  24. package/dist/src/examples/mls-multi-member.js +439 -0
  25. package/dist/src/examples/mls-sframe-test.js +491 -0
  26. package/dist/src/examples/mls-simple-test.js +122 -0
  27. package/dist/src/examples/pgp-example.js +354 -0
  28. package/dist/src/examples/random-generation-test.js +191 -0
  29. package/dist/src/examples/shogun-core-example.js +204 -0
  30. package/dist/src/examples/signal-protocol-test.js +82 -0
  31. package/dist/src/examples/zkproof-credentials-example.js +357 -0
  32. package/dist/src/examples/zkproof-example.js +357 -0
  33. package/dist/src/gundb/crypto.js +420 -0
  34. package/dist/src/gundb/db.js +728 -0
  35. package/dist/src/gundb/derive.js +327 -0
  36. package/dist/src/gundb/errors.js +115 -0
  37. package/dist/src/gundb/gun-es.js +8 -0
  38. package/dist/src/gundb/index.js +5 -0
  39. package/dist/{gundb → src/gundb}/rxjs.js +147 -111
  40. package/dist/{gundb → src/gundb}/types.js +1 -2
  41. package/dist/src/index.js +19 -0
  42. package/dist/src/interfaces/events.js +57 -0
  43. package/dist/{interfaces → src/interfaces}/shogun.js +4 -7
  44. package/dist/src/managers/AuthManager.js +301 -0
  45. package/dist/src/managers/CoreInitializer.js +304 -0
  46. package/dist/src/managers/CryptoIdentityManager.js +230 -0
  47. package/dist/{managers → src/managers}/EventManager.js +19 -21
  48. package/dist/{managers → src/managers}/PluginManager.js +123 -89
  49. package/dist/src/plugins/base.js +90 -0
  50. package/dist/src/plugins/index.js +17 -0
  51. package/dist/src/plugins/nostr/index.js +4 -0
  52. package/dist/src/plugins/nostr/nostrConnector.js +539 -0
  53. package/dist/src/plugins/nostr/nostrConnectorPlugin.js +663 -0
  54. package/dist/src/plugins/nostr/nostrSigner.js +414 -0
  55. package/dist/src/plugins/smartwallet/index.js +2 -0
  56. package/dist/src/plugins/smartwallet/smartWalletPlugin.js +824 -0
  57. package/dist/src/plugins/web3/index.js +4 -0
  58. package/dist/src/plugins/web3/types.js +1 -0
  59. package/dist/src/plugins/web3/web3Connector.js +738 -0
  60. package/dist/src/plugins/web3/web3ConnectorPlugin.js +639 -0
  61. package/dist/src/plugins/web3/web3Signer.js +432 -0
  62. package/dist/src/plugins/webauthn/index.js +3 -0
  63. package/dist/{plugins → src/plugins}/webauthn/types.js +2 -5
  64. package/dist/src/plugins/webauthn/webauthn.js +647 -0
  65. package/dist/src/plugins/webauthn/webauthnPlugin.js +689 -0
  66. package/dist/src/plugins/webauthn/webauthnSigner.js +419 -0
  67. package/dist/{plugins → src/plugins}/zkproof/index.js +3 -10
  68. package/dist/src/plugins/zkproof/types.js +1 -0
  69. package/dist/src/plugins/zkproof/zkCredentials.js +287 -0
  70. package/dist/src/plugins/zkproof/zkProofConnector.js +267 -0
  71. package/dist/src/plugins/zkproof/zkProofPlugin.js +405 -0
  72. package/dist/src/storage/storage.js +189 -0
  73. package/dist/src/utils/errorHandler.js +339 -0
  74. package/dist/{utils → src/utils}/eventEmitter.js +26 -26
  75. package/dist/{utils → src/utils}/seedPhrase.js +23 -32
  76. package/dist/{utils → src/utils}/validation.js +14 -21
  77. package/dist/tsconfig.tsbuildinfo +1 -0
  78. package/dist/types/{crypto → src/crypto}/double-ratchet.d.ts +1 -1
  79. package/dist/types/{crypto → src/crypto}/signal-protocol.d.ts +25 -0
  80. package/dist/types/{crypto → src/crypto}/types.d.ts +3 -1
  81. package/dist/types/src/examples/crypto-working-test.d.ts +1 -0
  82. package/dist/types/src/examples/double-ratchet-test.d.ts +1 -0
  83. package/dist/types/src/examples/mls-sframe-test.d.ts +1 -0
  84. package/dist/types/src/examples/random-generation-test.d.ts +1 -0
  85. package/dist/types/src/examples/signal-protocol-test.d.ts +1 -0
  86. package/dist/types/{gundb → src/gundb}/db.d.ts +14 -1
  87. package/dist/types/src/gundb/gun-es.d.ts +8 -0
  88. package/dist/types/src/gundb/min.d.ts +3 -0
  89. package/dist/types/{index.d.ts → src/index.d.ts} +1 -0
  90. package/package.json +14 -11
  91. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js +0 -1651
  92. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js.map +0 -1
  93. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js +0 -1608
  94. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map +0 -1
  95. package/dist/crypto/asymmetric.js +0 -99
  96. package/dist/crypto/double-ratchet.js +0 -392
  97. package/dist/crypto/file-encryption.js +0 -213
  98. package/dist/crypto/hashing.js +0 -87
  99. package/dist/crypto/index.js +0 -34
  100. package/dist/crypto/mls.js +0 -569
  101. package/dist/crypto/pgp.js +0 -390
  102. package/dist/crypto/sframe.js +0 -352
  103. package/dist/crypto/signal-protocol.js +0 -456
  104. package/dist/crypto/symmetric.js +0 -91
  105. package/dist/crypto/types.js +0 -2
  106. package/dist/crypto/utils.js +0 -140
  107. package/dist/examples/auth-test.js +0 -453
  108. package/dist/examples/crypto-identity-example.js +0 -196
  109. package/dist/examples/crypto-working-test.js +0 -83
  110. package/dist/examples/double-ratchet-test.js +0 -155
  111. package/dist/examples/mls-3-member-test.js +0 -97
  112. package/dist/examples/mls-multi-member.js +0 -153
  113. package/dist/examples/mls-sframe-test.js +0 -307
  114. package/dist/examples/mls-simple-test.js +0 -58
  115. package/dist/examples/pgp-example.js +0 -200
  116. package/dist/examples/random-generation-test.js +0 -151
  117. package/dist/examples/shogun-core-example.js +0 -150
  118. package/dist/examples/signal-protocol-test.js +0 -38
  119. package/dist/examples/zkproof-credentials-example.js +0 -217
  120. package/dist/examples/zkproof-example.js +0 -242
  121. package/dist/gundb/crypto.js +0 -306
  122. package/dist/gundb/db.js +0 -485
  123. package/dist/gundb/derive.js +0 -232
  124. package/dist/gundb/errors.js +0 -76
  125. package/dist/gundb/gun-es.js +0 -12
  126. package/dist/gundb/index.js +0 -21
  127. package/dist/gundb/min.js +0 -10
  128. package/dist/index.esm.js +0 -22
  129. package/dist/index.js +0 -47
  130. package/dist/interfaces/common.js +0 -2
  131. package/dist/interfaces/events.js +0 -40
  132. package/dist/interfaces/plugin.js +0 -2
  133. package/dist/managers/AuthManager.js +0 -226
  134. package/dist/managers/CoreInitializer.js +0 -250
  135. package/dist/managers/CryptoIdentityManager.js +0 -138
  136. package/dist/plugins/base.js +0 -50
  137. package/dist/plugins/index.js +0 -32
  138. package/dist/plugins/nostr/index.js +0 -20
  139. package/dist/plugins/nostr/nostrConnector.js +0 -419
  140. package/dist/plugins/nostr/nostrConnectorPlugin.js +0 -453
  141. package/dist/plugins/nostr/nostrSigner.js +0 -319
  142. package/dist/plugins/nostr/types.js +0 -2
  143. package/dist/plugins/smartwallet/index.js +0 -18
  144. package/dist/plugins/smartwallet/smartWalletPlugin.js +0 -511
  145. package/dist/plugins/smartwallet/types.js +0 -2
  146. package/dist/plugins/web3/index.js +0 -20
  147. package/dist/plugins/web3/types.js +0 -2
  148. package/dist/plugins/web3/web3Connector.js +0 -533
  149. package/dist/plugins/web3/web3ConnectorPlugin.js +0 -455
  150. package/dist/plugins/web3/web3Signer.js +0 -314
  151. package/dist/plugins/webauthn/index.js +0 -19
  152. package/dist/plugins/webauthn/webauthn.js +0 -496
  153. package/dist/plugins/webauthn/webauthnPlugin.js +0 -490
  154. package/dist/plugins/webauthn/webauthnSigner.js +0 -310
  155. package/dist/plugins/zkproof/types.js +0 -2
  156. package/dist/plugins/zkproof/zkCredentials.js +0 -216
  157. package/dist/plugins/zkproof/zkProofConnector.js +0 -198
  158. package/dist/plugins/zkproof/zkProofPlugin.js +0 -272
  159. package/dist/storage/storage.js +0 -145
  160. package/dist/types/gundb/gun-es.d.ts +0 -8
  161. package/dist/utils/errorHandler.js +0 -246
  162. /package/dist/{types/examples/crypto-working-test.d.ts → src/crypto/types.js} +0 -0
  163. /package/dist/{types/gundb/min.d.ts → src/gundb/min.js} +0 -0
  164. /package/dist/{types/examples/double-ratchet-test.d.ts → src/interfaces/common.js} +0 -0
  165. /package/dist/{types/examples/mls-sframe-test.d.ts → src/interfaces/plugin.js} +0 -0
  166. /package/dist/{types/examples/random-generation-test.d.ts → src/plugins/nostr/types.js} +0 -0
  167. /package/dist/{types/examples/signal-protocol-test.d.ts → src/plugins/smartwallet/types.js} +0 -0
  168. /package/dist/types/{core.d.ts → src/core.d.ts} +0 -0
  169. /package/dist/types/{crypto → src/crypto}/asymmetric.d.ts +0 -0
  170. /package/dist/types/{crypto → src/crypto}/file-encryption.d.ts +0 -0
  171. /package/dist/types/{crypto → src/crypto}/hashing.d.ts +0 -0
  172. /package/dist/types/{crypto → src/crypto}/index.d.ts +0 -0
  173. /package/dist/types/{crypto → src/crypto}/mls-codec.d.ts +0 -0
  174. /package/dist/types/{crypto → src/crypto}/mls.d.ts +0 -0
  175. /package/dist/types/{crypto → src/crypto}/pgp.d.ts +0 -0
  176. /package/dist/types/{crypto → src/crypto}/random-generation.d.ts +0 -0
  177. /package/dist/types/{crypto → src/crypto}/sframe.d.ts +0 -0
  178. /package/dist/types/{crypto → src/crypto}/symmetric.d.ts +0 -0
  179. /package/dist/types/{crypto → src/crypto}/utils.d.ts +0 -0
  180. /package/dist/types/{examples → src/examples}/auth-test.d.ts +0 -0
  181. /package/dist/types/{examples → src/examples}/crypto-identity-example.d.ts +0 -0
  182. /package/dist/types/{examples → src/examples}/mls-3-member-test.d.ts +0 -0
  183. /package/dist/types/{examples → src/examples}/mls-multi-member.d.ts +0 -0
  184. /package/dist/types/{examples → src/examples}/mls-simple-test.d.ts +0 -0
  185. /package/dist/types/{examples → src/examples}/pgp-example.d.ts +0 -0
  186. /package/dist/types/{examples → src/examples}/shogun-core-example.d.ts +0 -0
  187. /package/dist/types/{examples → src/examples}/zkproof-credentials-example.d.ts +0 -0
  188. /package/dist/types/{examples → src/examples}/zkproof-example.d.ts +0 -0
  189. /package/dist/types/{gundb → src/gundb}/crypto.d.ts +0 -0
  190. /package/dist/types/{gundb → src/gundb}/derive.d.ts +0 -0
  191. /package/dist/types/{gundb → src/gundb}/errors.d.ts +0 -0
  192. /package/dist/types/{gundb → src/gundb}/index.d.ts +0 -0
  193. /package/dist/types/{gundb → src/gundb}/rxjs.d.ts +0 -0
  194. /package/dist/types/{gundb → src/gundb}/types.d.ts +0 -0
  195. /package/dist/types/{interfaces → src/interfaces}/common.d.ts +0 -0
  196. /package/dist/types/{interfaces → src/interfaces}/events.d.ts +0 -0
  197. /package/dist/types/{interfaces → src/interfaces}/plugin.d.ts +0 -0
  198. /package/dist/types/{interfaces → src/interfaces}/shogun.d.ts +0 -0
  199. /package/dist/types/{managers → src/managers}/AuthManager.d.ts +0 -0
  200. /package/dist/types/{managers → src/managers}/CoreInitializer.d.ts +0 -0
  201. /package/dist/types/{managers → src/managers}/CryptoIdentityManager.d.ts +0 -0
  202. /package/dist/types/{managers → src/managers}/EventManager.d.ts +0 -0
  203. /package/dist/types/{managers → src/managers}/PluginManager.d.ts +0 -0
  204. /package/dist/types/{plugins → src/plugins}/base.d.ts +0 -0
  205. /package/dist/types/{plugins → src/plugins}/index.d.ts +0 -0
  206. /package/dist/types/{plugins → src/plugins}/nostr/index.d.ts +0 -0
  207. /package/dist/types/{plugins → src/plugins}/nostr/nostrConnector.d.ts +0 -0
  208. /package/dist/types/{plugins → src/plugins}/nostr/nostrConnectorPlugin.d.ts +0 -0
  209. /package/dist/types/{plugins → src/plugins}/nostr/nostrSigner.d.ts +0 -0
  210. /package/dist/types/{plugins → src/plugins}/nostr/types.d.ts +0 -0
  211. /package/dist/types/{plugins → src/plugins}/smartwallet/index.d.ts +0 -0
  212. /package/dist/types/{plugins → src/plugins}/smartwallet/smartWalletPlugin.d.ts +0 -0
  213. /package/dist/types/{plugins → src/plugins}/smartwallet/types.d.ts +0 -0
  214. /package/dist/types/{plugins → src/plugins}/web3/index.d.ts +0 -0
  215. /package/dist/types/{plugins → src/plugins}/web3/types.d.ts +0 -0
  216. /package/dist/types/{plugins → src/plugins}/web3/web3Connector.d.ts +0 -0
  217. /package/dist/types/{plugins → src/plugins}/web3/web3ConnectorPlugin.d.ts +0 -0
  218. /package/dist/types/{plugins → src/plugins}/web3/web3Signer.d.ts +0 -0
  219. /package/dist/types/{plugins → src/plugins}/webauthn/index.d.ts +0 -0
  220. /package/dist/types/{plugins → src/plugins}/webauthn/types.d.ts +0 -0
  221. /package/dist/types/{plugins → src/plugins}/webauthn/webauthn.d.ts +0 -0
  222. /package/dist/types/{plugins → src/plugins}/webauthn/webauthnPlugin.d.ts +0 -0
  223. /package/dist/types/{plugins → src/plugins}/webauthn/webauthnSigner.d.ts +0 -0
  224. /package/dist/types/{plugins → src/plugins}/zkproof/index.d.ts +0 -0
  225. /package/dist/types/{plugins → src/plugins}/zkproof/types.d.ts +0 -0
  226. /package/dist/types/{plugins → src/plugins}/zkproof/zkCredentials.d.ts +0 -0
  227. /package/dist/types/{plugins → src/plugins}/zkproof/zkProofConnector.d.ts +0 -0
  228. /package/dist/types/{plugins → src/plugins}/zkproof/zkProofPlugin.d.ts +0 -0
  229. /package/dist/types/{storage → src/storage}/storage.d.ts +0 -0
  230. /package/dist/types/{utils → src/utils}/errorHandler.d.ts +0 -0
  231. /package/dist/types/{utils → src/utils}/eventEmitter.d.ts +0 -0
  232. /package/dist/types/{utils → src/utils}/seedPhrase.d.ts +0 -0
  233. /package/dist/types/{utils → src/utils}/validation.d.ts +0 -0
@@ -1,1608 +0,0 @@
1
- "use strict";
2
- (this["webpackChunkShogunCore"] = this["webpackChunkShogunCore"] || []).push([["defaultVendors-node_modules_noble_curves_esm_nist_js"],{
3
-
4
- /***/ "./node_modules/@noble/curves/esm/nist.js":
5
- /*!************************************************************!*\
6
- !*** ./node_modules/@noble/curves/esm/nist.js + 2 modules ***!
7
- \************************************************************/
8
- /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
9
-
10
- // ESM COMPAT FLAG
11
- __webpack_require__.r(__webpack_exports__);
12
-
13
- // EXPORTS
14
- __webpack_require__.d(__webpack_exports__, {
15
- p256: () => (/* binding */ p256),
16
- p256_hasher: () => (/* binding */ p256_hasher),
17
- p384: () => (/* binding */ p384),
18
- p384_hasher: () => (/* binding */ p384_hasher),
19
- p521: () => (/* binding */ p521),
20
- p521_hasher: () => (/* binding */ p521_hasher),
21
- secp256r1: () => (/* binding */ secp256r1),
22
- secp384r1: () => (/* binding */ secp384r1),
23
- secp521r1: () => (/* binding */ secp521r1)
24
- });
25
-
26
- // EXTERNAL MODULE: ./node_modules/@noble/hashes/esm/sha2.js + 1 modules
27
- var sha2 = __webpack_require__("./node_modules/@noble/hashes/esm/sha2.js");
28
- // EXTERNAL MODULE: ./node_modules/@noble/hashes/esm/hmac.js
29
- var esm_hmac = __webpack_require__("./node_modules/@noble/hashes/esm/hmac.js");
30
- // EXTERNAL MODULE: ./node_modules/@noble/hashes/esm/utils.js + 1 modules
31
- var esm_utils = __webpack_require__("./node_modules/@noble/hashes/esm/utils.js");
32
- // EXTERNAL MODULE: ./node_modules/@noble/curves/esm/utils.js
33
- var curves_esm_utils = __webpack_require__("./node_modules/@noble/curves/esm/utils.js");
34
- // EXTERNAL MODULE: ./node_modules/@noble/curves/esm/abstract/curve.js
35
- var curve = __webpack_require__("./node_modules/@noble/curves/esm/abstract/curve.js");
36
- // EXTERNAL MODULE: ./node_modules/@noble/curves/esm/abstract/modular.js
37
- var modular = __webpack_require__("./node_modules/@noble/curves/esm/abstract/modular.js");
38
- ;// ./node_modules/@noble/curves/esm/abstract/weierstrass.js
39
- /**
40
- * Short Weierstrass curve methods. The formula is: y² = x³ + ax + b.
41
- *
42
- * ### Design rationale for types
43
- *
44
- * * Interaction between classes from different curves should fail:
45
- * `k256.Point.BASE.add(p256.Point.BASE)`
46
- * * For this purpose we want to use `instanceof` operator, which is fast and works during runtime
47
- * * Different calls of `curve()` would return different classes -
48
- * `curve(params) !== curve(params)`: if somebody decided to monkey-patch their curve,
49
- * it won't affect others
50
- *
51
- * TypeScript can't infer types for classes created inside a function. Classes is one instance
52
- * of nominative types in TypeScript and interfaces only check for shape, so it's hard to create
53
- * unique type for every function call.
54
- *
55
- * We can use generic types via some param, like curve opts, but that would:
56
- * 1. Enable interaction between `curve(params)` and `curve(params)` (curves of same params)
57
- * which is hard to debug.
58
- * 2. Params can be generic and we can't enforce them to be constant value:
59
- * if somebody creates curve from non-constant params,
60
- * it would be allowed to interact with other curves with non-constant params
61
- *
62
- * @todo https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol
63
- * @module
64
- */
65
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
66
-
67
-
68
-
69
-
70
-
71
- // We construct basis in such way that den is always positive and equals n, but num sign depends on basis (not on secret value)
72
- const divNearest = (num, den) => (num + (num >= 0 ? den : -den) / _2n) / den;
73
- /**
74
- * Splits scalar for GLV endomorphism.
75
- */
76
- function _splitEndoScalar(k, basis, n) {
77
- // Split scalar into two such that part is ~half bits: `abs(part) < sqrt(N)`
78
- // Since part can be negative, we need to do this on point.
79
- // TODO: verifyScalar function which consumes lambda
80
- const [[a1, b1], [a2, b2]] = basis;
81
- const c1 = divNearest(b2 * k, n);
82
- const c2 = divNearest(-b1 * k, n);
83
- // |k1|/|k2| is < sqrt(N), but can be negative.
84
- // If we do `k1 mod N`, we'll get big scalar (`> sqrt(N)`): so, we do cheaper negation instead.
85
- let k1 = k - c1 * a1 - c2 * a2;
86
- let k2 = -c1 * b1 - c2 * b2;
87
- const k1neg = k1 < _0n;
88
- const k2neg = k2 < _0n;
89
- if (k1neg)
90
- k1 = -k1;
91
- if (k2neg)
92
- k2 = -k2;
93
- // Double check that resulting scalar less than half bits of N: otherwise wNAF will fail.
94
- // This should only happen on wrong basises. Also, math inside is too complex and I don't trust it.
95
- const MAX_NUM = (0,curves_esm_utils.bitMask)(Math.ceil((0,curves_esm_utils.bitLen)(n) / 2)) + _1n; // Half bits of N
96
- if (k1 < _0n || k1 >= MAX_NUM || k2 < _0n || k2 >= MAX_NUM) {
97
- throw new Error('splitScalar (endomorphism): failed, k=' + k);
98
- }
99
- return { k1neg, k1, k2neg, k2 };
100
- }
101
- function validateSigFormat(format) {
102
- if (!['compact', 'recovered', 'der'].includes(format))
103
- throw new Error('Signature format must be "compact", "recovered", or "der"');
104
- return format;
105
- }
106
- function validateSigOpts(opts, def) {
107
- const optsn = {};
108
- for (let optName of Object.keys(def)) {
109
- // @ts-ignore
110
- optsn[optName] = opts[optName] === undefined ? def[optName] : opts[optName];
111
- }
112
- (0,curves_esm_utils._abool2)(optsn.lowS, 'lowS');
113
- (0,curves_esm_utils._abool2)(optsn.prehash, 'prehash');
114
- if (optsn.format !== undefined)
115
- validateSigFormat(optsn.format);
116
- return optsn;
117
- }
118
- class DERErr extends Error {
119
- constructor(m = '') {
120
- super(m);
121
- }
122
- }
123
- /**
124
- * ASN.1 DER encoding utilities. ASN is very complex & fragile. Format:
125
- *
126
- * [0x30 (SEQUENCE), bytelength, 0x02 (INTEGER), intLength, R, 0x02 (INTEGER), intLength, S]
127
- *
128
- * Docs: https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/, https://luca.ntop.org/Teaching/Appunti/asn1.html
129
- */
130
- const DER = {
131
- // asn.1 DER encoding utils
132
- Err: DERErr,
133
- // Basic building block is TLV (Tag-Length-Value)
134
- _tlv: {
135
- encode: (tag, data) => {
136
- const { Err: E } = DER;
137
- if (tag < 0 || tag > 256)
138
- throw new E('tlv.encode: wrong tag');
139
- if (data.length & 1)
140
- throw new E('tlv.encode: unpadded data');
141
- const dataLen = data.length / 2;
142
- const len = (0,curves_esm_utils.numberToHexUnpadded)(dataLen);
143
- if ((len.length / 2) & 128)
144
- throw new E('tlv.encode: long form length too big');
145
- // length of length with long form flag
146
- const lenLen = dataLen > 127 ? (0,curves_esm_utils.numberToHexUnpadded)((len.length / 2) | 128) : '';
147
- const t = (0,curves_esm_utils.numberToHexUnpadded)(tag);
148
- return t + lenLen + len + data;
149
- },
150
- // v - value, l - left bytes (unparsed)
151
- decode(tag, data) {
152
- const { Err: E } = DER;
153
- let pos = 0;
154
- if (tag < 0 || tag > 256)
155
- throw new E('tlv.encode: wrong tag');
156
- if (data.length < 2 || data[pos++] !== tag)
157
- throw new E('tlv.decode: wrong tlv');
158
- const first = data[pos++];
159
- const isLong = !!(first & 128); // First bit of first length byte is flag for short/long form
160
- let length = 0;
161
- if (!isLong)
162
- length = first;
163
- else {
164
- // Long form: [longFlag(1bit), lengthLength(7bit), length (BE)]
165
- const lenLen = first & 127;
166
- if (!lenLen)
167
- throw new E('tlv.decode(long): indefinite length not supported');
168
- if (lenLen > 4)
169
- throw new E('tlv.decode(long): byte length is too big'); // this will overflow u32 in js
170
- const lengthBytes = data.subarray(pos, pos + lenLen);
171
- if (lengthBytes.length !== lenLen)
172
- throw new E('tlv.decode: length bytes not complete');
173
- if (lengthBytes[0] === 0)
174
- throw new E('tlv.decode(long): zero leftmost byte');
175
- for (const b of lengthBytes)
176
- length = (length << 8) | b;
177
- pos += lenLen;
178
- if (length < 128)
179
- throw new E('tlv.decode(long): not minimal encoding');
180
- }
181
- const v = data.subarray(pos, pos + length);
182
- if (v.length !== length)
183
- throw new E('tlv.decode: wrong value length');
184
- return { v, l: data.subarray(pos + length) };
185
- },
186
- },
187
- // https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
188
- // since we always use positive integers here. It must always be empty:
189
- // - add zero byte if exists
190
- // - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
191
- _int: {
192
- encode(num) {
193
- const { Err: E } = DER;
194
- if (num < _0n)
195
- throw new E('integer: negative integers are not allowed');
196
- let hex = (0,curves_esm_utils.numberToHexUnpadded)(num);
197
- // Pad with zero byte if negative flag is present
198
- if (Number.parseInt(hex[0], 16) & 0b1000)
199
- hex = '00' + hex;
200
- if (hex.length & 1)
201
- throw new E('unexpected DER parsing assertion: unpadded hex');
202
- return hex;
203
- },
204
- decode(data) {
205
- const { Err: E } = DER;
206
- if (data[0] & 128)
207
- throw new E('invalid signature integer: negative');
208
- if (data[0] === 0x00 && !(data[1] & 128))
209
- throw new E('invalid signature integer: unnecessary leading zero');
210
- return (0,curves_esm_utils.bytesToNumberBE)(data);
211
- },
212
- },
213
- toSig(hex) {
214
- // parse DER signature
215
- const { Err: E, _int: int, _tlv: tlv } = DER;
216
- const data = (0,curves_esm_utils.ensureBytes)('signature', hex);
217
- const { v: seqBytes, l: seqLeftBytes } = tlv.decode(0x30, data);
218
- if (seqLeftBytes.length)
219
- throw new E('invalid signature: left bytes after parsing');
220
- const { v: rBytes, l: rLeftBytes } = tlv.decode(0x02, seqBytes);
221
- const { v: sBytes, l: sLeftBytes } = tlv.decode(0x02, rLeftBytes);
222
- if (sLeftBytes.length)
223
- throw new E('invalid signature: left bytes after parsing');
224
- return { r: int.decode(rBytes), s: int.decode(sBytes) };
225
- },
226
- hexFromSig(sig) {
227
- const { _tlv: tlv, _int: int } = DER;
228
- const rs = tlv.encode(0x02, int.encode(sig.r));
229
- const ss = tlv.encode(0x02, int.encode(sig.s));
230
- const seq = rs + ss;
231
- return tlv.encode(0x30, seq);
232
- },
233
- };
234
- // Be friendly to bad ECMAScript parsers by not using bigint literals
235
- // prettier-ignore
236
- const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4);
237
- function _normFnElement(Fn, key) {
238
- const { BYTES: expected } = Fn;
239
- let num;
240
- if (typeof key === 'bigint') {
241
- num = key;
242
- }
243
- else {
244
- let bytes = (0,curves_esm_utils.ensureBytes)('private key', key);
245
- try {
246
- num = Fn.fromBytes(bytes);
247
- }
248
- catch (error) {
249
- throw new Error(`invalid private key: expected ui8a of size ${expected}, got ${typeof key}`);
250
- }
251
- }
252
- if (!Fn.isValidNot0(num))
253
- throw new Error('invalid private key: out of range [1..N-1]');
254
- return num;
255
- }
256
- /**
257
- * Creates weierstrass Point constructor, based on specified curve options.
258
- *
259
- * @example
260
- ```js
261
- const opts = {
262
- p: BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'),
263
- n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
264
- h: BigInt(1),
265
- a: BigInt('0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc'),
266
- b: BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b'),
267
- Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
268
- Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
269
- };
270
- const p256_Point = weierstrass(opts);
271
- ```
272
- */
273
- function weierstrassN(params, extraOpts = {}) {
274
- const validated = (0,curve._createCurveFields)('weierstrass', params, extraOpts);
275
- const { Fp, Fn } = validated;
276
- let CURVE = validated.CURVE;
277
- const { h: cofactor, n: CURVE_ORDER } = CURVE;
278
- (0,curves_esm_utils._validateObject)(extraOpts, {}, {
279
- allowInfinityPoint: 'boolean',
280
- clearCofactor: 'function',
281
- isTorsionFree: 'function',
282
- fromBytes: 'function',
283
- toBytes: 'function',
284
- endo: 'object',
285
- wrapPrivateKey: 'boolean',
286
- });
287
- const { endo } = extraOpts;
288
- if (endo) {
289
- // validateObject(endo, { beta: 'bigint', splitScalar: 'function' });
290
- if (!Fp.is0(CURVE.a) || typeof endo.beta !== 'bigint' || !Array.isArray(endo.basises)) {
291
- throw new Error('invalid endo: expected "beta": bigint and "basises": array');
292
- }
293
- }
294
- const lengths = getWLengths(Fp, Fn);
295
- function assertCompressionIsSupported() {
296
- if (!Fp.isOdd)
297
- throw new Error('compression is not supported: Field does not have .isOdd()');
298
- }
299
- // Implements IEEE P1363 point encoding
300
- function pointToBytes(_c, point, isCompressed) {
301
- const { x, y } = point.toAffine();
302
- const bx = Fp.toBytes(x);
303
- (0,curves_esm_utils._abool2)(isCompressed, 'isCompressed');
304
- if (isCompressed) {
305
- assertCompressionIsSupported();
306
- const hasEvenY = !Fp.isOdd(y);
307
- return (0,esm_utils.concatBytes)(pprefix(hasEvenY), bx);
308
- }
309
- else {
310
- return (0,esm_utils.concatBytes)(Uint8Array.of(0x04), bx, Fp.toBytes(y));
311
- }
312
- }
313
- function pointFromBytes(bytes) {
314
- (0,curves_esm_utils._abytes2)(bytes, undefined, 'Point');
315
- const { publicKey: comp, publicKeyUncompressed: uncomp } = lengths; // e.g. for 32-byte: 33, 65
316
- const length = bytes.length;
317
- const head = bytes[0];
318
- const tail = bytes.subarray(1);
319
- // No actual validation is done here: use .assertValidity()
320
- if (length === comp && (head === 0x02 || head === 0x03)) {
321
- const x = Fp.fromBytes(tail);
322
- if (!Fp.isValid(x))
323
- throw new Error('bad point: is not on curve, wrong x');
324
- const y2 = weierstrassEquation(x); // y² = x³ + ax + b
325
- let y;
326
- try {
327
- y = Fp.sqrt(y2); // y = y² ^ (p+1)/4
328
- }
329
- catch (sqrtError) {
330
- const err = sqrtError instanceof Error ? ': ' + sqrtError.message : '';
331
- throw new Error('bad point: is not on curve, sqrt error' + err);
332
- }
333
- assertCompressionIsSupported();
334
- const isYOdd = Fp.isOdd(y); // (y & _1n) === _1n;
335
- const isHeadOdd = (head & 1) === 1; // ECDSA-specific
336
- if (isHeadOdd !== isYOdd)
337
- y = Fp.neg(y);
338
- return { x, y };
339
- }
340
- else if (length === uncomp && head === 0x04) {
341
- // TODO: more checks
342
- const L = Fp.BYTES;
343
- const x = Fp.fromBytes(tail.subarray(0, L));
344
- const y = Fp.fromBytes(tail.subarray(L, L * 2));
345
- if (!isValidXY(x, y))
346
- throw new Error('bad point: is not on curve');
347
- return { x, y };
348
- }
349
- else {
350
- throw new Error(`bad point: got length ${length}, expected compressed=${comp} or uncompressed=${uncomp}`);
351
- }
352
- }
353
- const encodePoint = extraOpts.toBytes || pointToBytes;
354
- const decodePoint = extraOpts.fromBytes || pointFromBytes;
355
- function weierstrassEquation(x) {
356
- const x2 = Fp.sqr(x); // x * x
357
- const x3 = Fp.mul(x2, x); // x² * x
358
- return Fp.add(Fp.add(x3, Fp.mul(x, CURVE.a)), CURVE.b); // x³ + a * x + b
359
- }
360
- // TODO: move top-level
361
- /** Checks whether equation holds for given x, y: y² == x³ + ax + b */
362
- function isValidXY(x, y) {
363
- const left = Fp.sqr(y); // y²
364
- const right = weierstrassEquation(x); // x³ + ax + b
365
- return Fp.eql(left, right);
366
- }
367
- // Validate whether the passed curve params are valid.
368
- // Test 1: equation y² = x³ + ax + b should work for generator point.
369
- if (!isValidXY(CURVE.Gx, CURVE.Gy))
370
- throw new Error('bad curve params: generator point');
371
- // Test 2: discriminant Δ part should be non-zero: 4a³ + 27b² != 0.
372
- // Guarantees curve is genus-1, smooth (non-singular).
373
- const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n), _4n);
374
- const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
375
- if (Fp.is0(Fp.add(_4a3, _27b2)))
376
- throw new Error('bad curve params: a or b');
377
- /** Asserts coordinate is valid: 0 <= n < Fp.ORDER. */
378
- function acoord(title, n, banZero = false) {
379
- if (!Fp.isValid(n) || (banZero && Fp.is0(n)))
380
- throw new Error(`bad point coordinate ${title}`);
381
- return n;
382
- }
383
- function aprjpoint(other) {
384
- if (!(other instanceof Point))
385
- throw new Error('ProjectivePoint expected');
386
- }
387
- function splitEndoScalarN(k) {
388
- if (!endo || !endo.basises)
389
- throw new Error('no endo');
390
- return _splitEndoScalar(k, endo.basises, Fn.ORDER);
391
- }
392
- // Memoized toAffine / validity check. They are heavy. Points are immutable.
393
- // Converts Projective point to affine (x, y) coordinates.
394
- // Can accept precomputed Z^-1 - for example, from invertBatch.
395
- // (X, Y, Z) ∋ (x=X/Z, y=Y/Z)
396
- const toAffineMemo = (0,curves_esm_utils.memoized)((p, iz) => {
397
- const { X, Y, Z } = p;
398
- // Fast-path for normalized points
399
- if (Fp.eql(Z, Fp.ONE))
400
- return { x: X, y: Y };
401
- const is0 = p.is0();
402
- // If invZ was 0, we return zero point. However we still want to execute
403
- // all operations, so we replace invZ with a random number, 1.
404
- if (iz == null)
405
- iz = is0 ? Fp.ONE : Fp.inv(Z);
406
- const x = Fp.mul(X, iz);
407
- const y = Fp.mul(Y, iz);
408
- const zz = Fp.mul(Z, iz);
409
- if (is0)
410
- return { x: Fp.ZERO, y: Fp.ZERO };
411
- if (!Fp.eql(zz, Fp.ONE))
412
- throw new Error('invZ was invalid');
413
- return { x, y };
414
- });
415
- // NOTE: on exception this will crash 'cached' and no value will be set.
416
- // Otherwise true will be return
417
- const assertValidMemo = (0,curves_esm_utils.memoized)((p) => {
418
- if (p.is0()) {
419
- // (0, 1, 0) aka ZERO is invalid in most contexts.
420
- // In BLS, ZERO can be serialized, so we allow it.
421
- // (0, 0, 0) is invalid representation of ZERO.
422
- if (extraOpts.allowInfinityPoint && !Fp.is0(p.Y))
423
- return;
424
- throw new Error('bad point: ZERO');
425
- }
426
- // Some 3rd-party test vectors require different wording between here & `fromCompressedHex`
427
- const { x, y } = p.toAffine();
428
- if (!Fp.isValid(x) || !Fp.isValid(y))
429
- throw new Error('bad point: x or y not field elements');
430
- if (!isValidXY(x, y))
431
- throw new Error('bad point: equation left != right');
432
- if (!p.isTorsionFree())
433
- throw new Error('bad point: not in prime-order subgroup');
434
- return true;
435
- });
436
- function finishEndo(endoBeta, k1p, k2p, k1neg, k2neg) {
437
- k2p = new Point(Fp.mul(k2p.X, endoBeta), k2p.Y, k2p.Z);
438
- k1p = (0,curve.negateCt)(k1neg, k1p);
439
- k2p = (0,curve.negateCt)(k2neg, k2p);
440
- return k1p.add(k2p);
441
- }
442
- /**
443
- * Projective Point works in 3d / projective (homogeneous) coordinates:(X, Y, Z) ∋ (x=X/Z, y=Y/Z).
444
- * Default Point works in 2d / affine coordinates: (x, y).
445
- * We're doing calculations in projective, because its operations don't require costly inversion.
446
- */
447
- class Point {
448
- /** Does NOT validate if the point is valid. Use `.assertValidity()`. */
449
- constructor(X, Y, Z) {
450
- this.X = acoord('x', X);
451
- this.Y = acoord('y', Y, true);
452
- this.Z = acoord('z', Z);
453
- Object.freeze(this);
454
- }
455
- static CURVE() {
456
- return CURVE;
457
- }
458
- /** Does NOT validate if the point is valid. Use `.assertValidity()`. */
459
- static fromAffine(p) {
460
- const { x, y } = p || {};
461
- if (!p || !Fp.isValid(x) || !Fp.isValid(y))
462
- throw new Error('invalid affine point');
463
- if (p instanceof Point)
464
- throw new Error('projective point not allowed');
465
- // (0, 0) would've produced (0, 0, 1) - instead, we need (0, 1, 0)
466
- if (Fp.is0(x) && Fp.is0(y))
467
- return Point.ZERO;
468
- return new Point(x, y, Fp.ONE);
469
- }
470
- static fromBytes(bytes) {
471
- const P = Point.fromAffine(decodePoint((0,curves_esm_utils._abytes2)(bytes, undefined, 'point')));
472
- P.assertValidity();
473
- return P;
474
- }
475
- static fromHex(hex) {
476
- return Point.fromBytes((0,curves_esm_utils.ensureBytes)('pointHex', hex));
477
- }
478
- get x() {
479
- return this.toAffine().x;
480
- }
481
- get y() {
482
- return this.toAffine().y;
483
- }
484
- /**
485
- *
486
- * @param windowSize
487
- * @param isLazy true will defer table computation until the first multiplication
488
- * @returns
489
- */
490
- precompute(windowSize = 8, isLazy = true) {
491
- wnaf.createCache(this, windowSize);
492
- if (!isLazy)
493
- this.multiply(_3n); // random number
494
- return this;
495
- }
496
- // TODO: return `this`
497
- /** A point on curve is valid if it conforms to equation. */
498
- assertValidity() {
499
- assertValidMemo(this);
500
- }
501
- hasEvenY() {
502
- const { y } = this.toAffine();
503
- if (!Fp.isOdd)
504
- throw new Error("Field doesn't support isOdd");
505
- return !Fp.isOdd(y);
506
- }
507
- /** Compare one point to another. */
508
- equals(other) {
509
- aprjpoint(other);
510
- const { X: X1, Y: Y1, Z: Z1 } = this;
511
- const { X: X2, Y: Y2, Z: Z2 } = other;
512
- const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
513
- const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
514
- return U1 && U2;
515
- }
516
- /** Flips point to one corresponding to (x, -y) in Affine coordinates. */
517
- negate() {
518
- return new Point(this.X, Fp.neg(this.Y), this.Z);
519
- }
520
- // Renes-Costello-Batina exception-free doubling formula.
521
- // There is 30% faster Jacobian formula, but it is not complete.
522
- // https://eprint.iacr.org/2015/1060, algorithm 3
523
- // Cost: 8M + 3S + 3*a + 2*b3 + 15add.
524
- double() {
525
- const { a, b } = CURVE;
526
- const b3 = Fp.mul(b, _3n);
527
- const { X: X1, Y: Y1, Z: Z1 } = this;
528
- let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO; // prettier-ignore
529
- let t0 = Fp.mul(X1, X1); // step 1
530
- let t1 = Fp.mul(Y1, Y1);
531
- let t2 = Fp.mul(Z1, Z1);
532
- let t3 = Fp.mul(X1, Y1);
533
- t3 = Fp.add(t3, t3); // step 5
534
- Z3 = Fp.mul(X1, Z1);
535
- Z3 = Fp.add(Z3, Z3);
536
- X3 = Fp.mul(a, Z3);
537
- Y3 = Fp.mul(b3, t2);
538
- Y3 = Fp.add(X3, Y3); // step 10
539
- X3 = Fp.sub(t1, Y3);
540
- Y3 = Fp.add(t1, Y3);
541
- Y3 = Fp.mul(X3, Y3);
542
- X3 = Fp.mul(t3, X3);
543
- Z3 = Fp.mul(b3, Z3); // step 15
544
- t2 = Fp.mul(a, t2);
545
- t3 = Fp.sub(t0, t2);
546
- t3 = Fp.mul(a, t3);
547
- t3 = Fp.add(t3, Z3);
548
- Z3 = Fp.add(t0, t0); // step 20
549
- t0 = Fp.add(Z3, t0);
550
- t0 = Fp.add(t0, t2);
551
- t0 = Fp.mul(t0, t3);
552
- Y3 = Fp.add(Y3, t0);
553
- t2 = Fp.mul(Y1, Z1); // step 25
554
- t2 = Fp.add(t2, t2);
555
- t0 = Fp.mul(t2, t3);
556
- X3 = Fp.sub(X3, t0);
557
- Z3 = Fp.mul(t2, t1);
558
- Z3 = Fp.add(Z3, Z3); // step 30
559
- Z3 = Fp.add(Z3, Z3);
560
- return new Point(X3, Y3, Z3);
561
- }
562
- // Renes-Costello-Batina exception-free addition formula.
563
- // There is 30% faster Jacobian formula, but it is not complete.
564
- // https://eprint.iacr.org/2015/1060, algorithm 1
565
- // Cost: 12M + 0S + 3*a + 3*b3 + 23add.
566
- add(other) {
567
- aprjpoint(other);
568
- const { X: X1, Y: Y1, Z: Z1 } = this;
569
- const { X: X2, Y: Y2, Z: Z2 } = other;
570
- let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO; // prettier-ignore
571
- const a = CURVE.a;
572
- const b3 = Fp.mul(CURVE.b, _3n);
573
- let t0 = Fp.mul(X1, X2); // step 1
574
- let t1 = Fp.mul(Y1, Y2);
575
- let t2 = Fp.mul(Z1, Z2);
576
- let t3 = Fp.add(X1, Y1);
577
- let t4 = Fp.add(X2, Y2); // step 5
578
- t3 = Fp.mul(t3, t4);
579
- t4 = Fp.add(t0, t1);
580
- t3 = Fp.sub(t3, t4);
581
- t4 = Fp.add(X1, Z1);
582
- let t5 = Fp.add(X2, Z2); // step 10
583
- t4 = Fp.mul(t4, t5);
584
- t5 = Fp.add(t0, t2);
585
- t4 = Fp.sub(t4, t5);
586
- t5 = Fp.add(Y1, Z1);
587
- X3 = Fp.add(Y2, Z2); // step 15
588
- t5 = Fp.mul(t5, X3);
589
- X3 = Fp.add(t1, t2);
590
- t5 = Fp.sub(t5, X3);
591
- Z3 = Fp.mul(a, t4);
592
- X3 = Fp.mul(b3, t2); // step 20
593
- Z3 = Fp.add(X3, Z3);
594
- X3 = Fp.sub(t1, Z3);
595
- Z3 = Fp.add(t1, Z3);
596
- Y3 = Fp.mul(X3, Z3);
597
- t1 = Fp.add(t0, t0); // step 25
598
- t1 = Fp.add(t1, t0);
599
- t2 = Fp.mul(a, t2);
600
- t4 = Fp.mul(b3, t4);
601
- t1 = Fp.add(t1, t2);
602
- t2 = Fp.sub(t0, t2); // step 30
603
- t2 = Fp.mul(a, t2);
604
- t4 = Fp.add(t4, t2);
605
- t0 = Fp.mul(t1, t4);
606
- Y3 = Fp.add(Y3, t0);
607
- t0 = Fp.mul(t5, t4); // step 35
608
- X3 = Fp.mul(t3, X3);
609
- X3 = Fp.sub(X3, t0);
610
- t0 = Fp.mul(t3, t1);
611
- Z3 = Fp.mul(t5, Z3);
612
- Z3 = Fp.add(Z3, t0); // step 40
613
- return new Point(X3, Y3, Z3);
614
- }
615
- subtract(other) {
616
- return this.add(other.negate());
617
- }
618
- is0() {
619
- return this.equals(Point.ZERO);
620
- }
621
- /**
622
- * Constant time multiplication.
623
- * Uses wNAF method. Windowed method may be 10% faster,
624
- * but takes 2x longer to generate and consumes 2x memory.
625
- * Uses precomputes when available.
626
- * Uses endomorphism for Koblitz curves.
627
- * @param scalar by which the point would be multiplied
628
- * @returns New point
629
- */
630
- multiply(scalar) {
631
- const { endo } = extraOpts;
632
- if (!Fn.isValidNot0(scalar))
633
- throw new Error('invalid scalar: out of range'); // 0 is invalid
634
- let point, fake; // Fake point is used to const-time mult
635
- const mul = (n) => wnaf.cached(this, n, (p) => (0,curve.normalizeZ)(Point, p));
636
- /** See docs for {@link EndomorphismOpts} */
637
- if (endo) {
638
- const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(scalar);
639
- const { p: k1p, f: k1f } = mul(k1);
640
- const { p: k2p, f: k2f } = mul(k2);
641
- fake = k1f.add(k2f);
642
- point = finishEndo(endo.beta, k1p, k2p, k1neg, k2neg);
643
- }
644
- else {
645
- const { p, f } = mul(scalar);
646
- point = p;
647
- fake = f;
648
- }
649
- // Normalize `z` for both points, but return only real one
650
- return (0,curve.normalizeZ)(Point, [point, fake])[0];
651
- }
652
- /**
653
- * Non-constant-time multiplication. Uses double-and-add algorithm.
654
- * It's faster, but should only be used when you don't care about
655
- * an exposed secret key e.g. sig verification, which works over *public* keys.
656
- */
657
- multiplyUnsafe(sc) {
658
- const { endo } = extraOpts;
659
- const p = this;
660
- if (!Fn.isValid(sc))
661
- throw new Error('invalid scalar: out of range'); // 0 is valid
662
- if (sc === _0n || p.is0())
663
- return Point.ZERO;
664
- if (sc === _1n)
665
- return p; // fast-path
666
- if (wnaf.hasCache(this))
667
- return this.multiply(sc);
668
- if (endo) {
669
- const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(sc);
670
- const { p1, p2 } = (0,curve.mulEndoUnsafe)(Point, p, k1, k2); // 30% faster vs wnaf.unsafe
671
- return finishEndo(endo.beta, p1, p2, k1neg, k2neg);
672
- }
673
- else {
674
- return wnaf.unsafe(p, sc);
675
- }
676
- }
677
- multiplyAndAddUnsafe(Q, a, b) {
678
- const sum = this.multiplyUnsafe(a).add(Q.multiplyUnsafe(b));
679
- return sum.is0() ? undefined : sum;
680
- }
681
- /**
682
- * Converts Projective point to affine (x, y) coordinates.
683
- * @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
684
- */
685
- toAffine(invertedZ) {
686
- return toAffineMemo(this, invertedZ);
687
- }
688
- /**
689
- * Checks whether Point is free of torsion elements (is in prime subgroup).
690
- * Always torsion-free for cofactor=1 curves.
691
- */
692
- isTorsionFree() {
693
- const { isTorsionFree } = extraOpts;
694
- if (cofactor === _1n)
695
- return true;
696
- if (isTorsionFree)
697
- return isTorsionFree(Point, this);
698
- return wnaf.unsafe(this, CURVE_ORDER).is0();
699
- }
700
- clearCofactor() {
701
- const { clearCofactor } = extraOpts;
702
- if (cofactor === _1n)
703
- return this; // Fast-path
704
- if (clearCofactor)
705
- return clearCofactor(Point, this);
706
- return this.multiplyUnsafe(cofactor);
707
- }
708
- isSmallOrder() {
709
- // can we use this.clearCofactor()?
710
- return this.multiplyUnsafe(cofactor).is0();
711
- }
712
- toBytes(isCompressed = true) {
713
- (0,curves_esm_utils._abool2)(isCompressed, 'isCompressed');
714
- this.assertValidity();
715
- return encodePoint(Point, this, isCompressed);
716
- }
717
- toHex(isCompressed = true) {
718
- return (0,esm_utils.bytesToHex)(this.toBytes(isCompressed));
719
- }
720
- toString() {
721
- return `<Point ${this.is0() ? 'ZERO' : this.toHex()}>`;
722
- }
723
- // TODO: remove
724
- get px() {
725
- return this.X;
726
- }
727
- get py() {
728
- return this.X;
729
- }
730
- get pz() {
731
- return this.Z;
732
- }
733
- toRawBytes(isCompressed = true) {
734
- return this.toBytes(isCompressed);
735
- }
736
- _setWindowSize(windowSize) {
737
- this.precompute(windowSize);
738
- }
739
- static normalizeZ(points) {
740
- return (0,curve.normalizeZ)(Point, points);
741
- }
742
- static msm(points, scalars) {
743
- return (0,curve.pippenger)(Point, Fn, points, scalars);
744
- }
745
- static fromPrivateKey(privateKey) {
746
- return Point.BASE.multiply(_normFnElement(Fn, privateKey));
747
- }
748
- }
749
- // base / generator point
750
- Point.BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE);
751
- // zero / infinity / identity point
752
- Point.ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO); // 0, 1, 0
753
- // math field
754
- Point.Fp = Fp;
755
- // scalar field
756
- Point.Fn = Fn;
757
- const bits = Fn.BITS;
758
- const wnaf = new curve.wNAF(Point, extraOpts.endo ? Math.ceil(bits / 2) : bits);
759
- Point.BASE.precompute(8); // Enable precomputes. Slows down first publicKey computation by 20ms.
760
- return Point;
761
- }
762
- // Points start with byte 0x02 when y is even; otherwise 0x03
763
- function pprefix(hasEvenY) {
764
- return Uint8Array.of(hasEvenY ? 0x02 : 0x03);
765
- }
766
- /**
767
- * Implementation of the Shallue and van de Woestijne method for any weierstrass curve.
768
- * TODO: check if there is a way to merge this with uvRatio in Edwards; move to modular.
769
- * b = True and y = sqrt(u / v) if (u / v) is square in F, and
770
- * b = False and y = sqrt(Z * (u / v)) otherwise.
771
- * @param Fp
772
- * @param Z
773
- * @returns
774
- */
775
- function SWUFpSqrtRatio(Fp, Z) {
776
- // Generic implementation
777
- const q = Fp.ORDER;
778
- let l = _0n;
779
- for (let o = q - _1n; o % _2n === _0n; o /= _2n)
780
- l += _1n;
781
- const c1 = l; // 1. c1, the largest integer such that 2^c1 divides q - 1.
782
- // We need 2n ** c1 and 2n ** (c1-1). We can't use **; but we can use <<.
783
- // 2n ** c1 == 2n << (c1-1)
784
- const _2n_pow_c1_1 = _2n << (c1 - _1n - _1n);
785
- const _2n_pow_c1 = _2n_pow_c1_1 * _2n;
786
- const c2 = (q - _1n) / _2n_pow_c1; // 2. c2 = (q - 1) / (2^c1) # Integer arithmetic
787
- const c3 = (c2 - _1n) / _2n; // 3. c3 = (c2 - 1) / 2 # Integer arithmetic
788
- const c4 = _2n_pow_c1 - _1n; // 4. c4 = 2^c1 - 1 # Integer arithmetic
789
- const c5 = _2n_pow_c1_1; // 5. c5 = 2^(c1 - 1) # Integer arithmetic
790
- const c6 = Fp.pow(Z, c2); // 6. c6 = Z^c2
791
- const c7 = Fp.pow(Z, (c2 + _1n) / _2n); // 7. c7 = Z^((c2 + 1) / 2)
792
- let sqrtRatio = (u, v) => {
793
- let tv1 = c6; // 1. tv1 = c6
794
- let tv2 = Fp.pow(v, c4); // 2. tv2 = v^c4
795
- let tv3 = Fp.sqr(tv2); // 3. tv3 = tv2^2
796
- tv3 = Fp.mul(tv3, v); // 4. tv3 = tv3 * v
797
- let tv5 = Fp.mul(u, tv3); // 5. tv5 = u * tv3
798
- tv5 = Fp.pow(tv5, c3); // 6. tv5 = tv5^c3
799
- tv5 = Fp.mul(tv5, tv2); // 7. tv5 = tv5 * tv2
800
- tv2 = Fp.mul(tv5, v); // 8. tv2 = tv5 * v
801
- tv3 = Fp.mul(tv5, u); // 9. tv3 = tv5 * u
802
- let tv4 = Fp.mul(tv3, tv2); // 10. tv4 = tv3 * tv2
803
- tv5 = Fp.pow(tv4, c5); // 11. tv5 = tv4^c5
804
- let isQR = Fp.eql(tv5, Fp.ONE); // 12. isQR = tv5 == 1
805
- tv2 = Fp.mul(tv3, c7); // 13. tv2 = tv3 * c7
806
- tv5 = Fp.mul(tv4, tv1); // 14. tv5 = tv4 * tv1
807
- tv3 = Fp.cmov(tv2, tv3, isQR); // 15. tv3 = CMOV(tv2, tv3, isQR)
808
- tv4 = Fp.cmov(tv5, tv4, isQR); // 16. tv4 = CMOV(tv5, tv4, isQR)
809
- // 17. for i in (c1, c1 - 1, ..., 2):
810
- for (let i = c1; i > _1n; i--) {
811
- let tv5 = i - _2n; // 18. tv5 = i - 2
812
- tv5 = _2n << (tv5 - _1n); // 19. tv5 = 2^tv5
813
- let tvv5 = Fp.pow(tv4, tv5); // 20. tv5 = tv4^tv5
814
- const e1 = Fp.eql(tvv5, Fp.ONE); // 21. e1 = tv5 == 1
815
- tv2 = Fp.mul(tv3, tv1); // 22. tv2 = tv3 * tv1
816
- tv1 = Fp.mul(tv1, tv1); // 23. tv1 = tv1 * tv1
817
- tvv5 = Fp.mul(tv4, tv1); // 24. tv5 = tv4 * tv1
818
- tv3 = Fp.cmov(tv2, tv3, e1); // 25. tv3 = CMOV(tv2, tv3, e1)
819
- tv4 = Fp.cmov(tvv5, tv4, e1); // 26. tv4 = CMOV(tv5, tv4, e1)
820
- }
821
- return { isValid: isQR, value: tv3 };
822
- };
823
- if (Fp.ORDER % _4n === _3n) {
824
- // sqrt_ratio_3mod4(u, v)
825
- const c1 = (Fp.ORDER - _3n) / _4n; // 1. c1 = (q - 3) / 4 # Integer arithmetic
826
- const c2 = Fp.sqrt(Fp.neg(Z)); // 2. c2 = sqrt(-Z)
827
- sqrtRatio = (u, v) => {
828
- let tv1 = Fp.sqr(v); // 1. tv1 = v^2
829
- const tv2 = Fp.mul(u, v); // 2. tv2 = u * v
830
- tv1 = Fp.mul(tv1, tv2); // 3. tv1 = tv1 * tv2
831
- let y1 = Fp.pow(tv1, c1); // 4. y1 = tv1^c1
832
- y1 = Fp.mul(y1, tv2); // 5. y1 = y1 * tv2
833
- const y2 = Fp.mul(y1, c2); // 6. y2 = y1 * c2
834
- const tv3 = Fp.mul(Fp.sqr(y1), v); // 7. tv3 = y1^2; 8. tv3 = tv3 * v
835
- const isQR = Fp.eql(tv3, u); // 9. isQR = tv3 == u
836
- let y = Fp.cmov(y2, y1, isQR); // 10. y = CMOV(y2, y1, isQR)
837
- return { isValid: isQR, value: y }; // 11. return (isQR, y) isQR ? y : y*c2
838
- };
839
- }
840
- // No curves uses that
841
- // if (Fp.ORDER % _8n === _5n) // sqrt_ratio_5mod8
842
- return sqrtRatio;
843
- }
844
- /**
845
- * Simplified Shallue-van de Woestijne-Ulas Method
846
- * https://www.rfc-editor.org/rfc/rfc9380#section-6.6.2
847
- */
848
- function mapToCurveSimpleSWU(Fp, opts) {
849
- (0,modular.validateField)(Fp);
850
- const { A, B, Z } = opts;
851
- if (!Fp.isValid(A) || !Fp.isValid(B) || !Fp.isValid(Z))
852
- throw new Error('mapToCurveSimpleSWU: invalid opts');
853
- const sqrtRatio = SWUFpSqrtRatio(Fp, Z);
854
- if (!Fp.isOdd)
855
- throw new Error('Field does not have .isOdd()');
856
- // Input: u, an element of F.
857
- // Output: (x, y), a point on E.
858
- return (u) => {
859
- // prettier-ignore
860
- let tv1, tv2, tv3, tv4, tv5, tv6, x, y;
861
- tv1 = Fp.sqr(u); // 1. tv1 = u^2
862
- tv1 = Fp.mul(tv1, Z); // 2. tv1 = Z * tv1
863
- tv2 = Fp.sqr(tv1); // 3. tv2 = tv1^2
864
- tv2 = Fp.add(tv2, tv1); // 4. tv2 = tv2 + tv1
865
- tv3 = Fp.add(tv2, Fp.ONE); // 5. tv3 = tv2 + 1
866
- tv3 = Fp.mul(tv3, B); // 6. tv3 = B * tv3
867
- tv4 = Fp.cmov(Z, Fp.neg(tv2), !Fp.eql(tv2, Fp.ZERO)); // 7. tv4 = CMOV(Z, -tv2, tv2 != 0)
868
- tv4 = Fp.mul(tv4, A); // 8. tv4 = A * tv4
869
- tv2 = Fp.sqr(tv3); // 9. tv2 = tv3^2
870
- tv6 = Fp.sqr(tv4); // 10. tv6 = tv4^2
871
- tv5 = Fp.mul(tv6, A); // 11. tv5 = A * tv6
872
- tv2 = Fp.add(tv2, tv5); // 12. tv2 = tv2 + tv5
873
- tv2 = Fp.mul(tv2, tv3); // 13. tv2 = tv2 * tv3
874
- tv6 = Fp.mul(tv6, tv4); // 14. tv6 = tv6 * tv4
875
- tv5 = Fp.mul(tv6, B); // 15. tv5 = B * tv6
876
- tv2 = Fp.add(tv2, tv5); // 16. tv2 = tv2 + tv5
877
- x = Fp.mul(tv1, tv3); // 17. x = tv1 * tv3
878
- const { isValid, value } = sqrtRatio(tv2, tv6); // 18. (is_gx1_square, y1) = sqrt_ratio(tv2, tv6)
879
- y = Fp.mul(tv1, u); // 19. y = tv1 * u -> Z * u^3 * y1
880
- y = Fp.mul(y, value); // 20. y = y * y1
881
- x = Fp.cmov(x, tv3, isValid); // 21. x = CMOV(x, tv3, is_gx1_square)
882
- y = Fp.cmov(y, value, isValid); // 22. y = CMOV(y, y1, is_gx1_square)
883
- const e1 = Fp.isOdd(u) === Fp.isOdd(y); // 23. e1 = sgn0(u) == sgn0(y)
884
- y = Fp.cmov(Fp.neg(y), y, e1); // 24. y = CMOV(-y, y, e1)
885
- const tv4_inv = (0,modular.FpInvertBatch)(Fp, [tv4], true)[0];
886
- x = Fp.mul(x, tv4_inv); // 25. x = x / tv4
887
- return { x, y };
888
- };
889
- }
890
- function getWLengths(Fp, Fn) {
891
- return {
892
- secretKey: Fn.BYTES,
893
- publicKey: 1 + Fp.BYTES,
894
- publicKeyUncompressed: 1 + 2 * Fp.BYTES,
895
- publicKeyHasPrefix: true,
896
- signature: 2 * Fn.BYTES,
897
- };
898
- }
899
- /**
900
- * Sometimes users only need getPublicKey, getSharedSecret, and secret key handling.
901
- * This helper ensures no signature functionality is present. Less code, smaller bundle size.
902
- */
903
- function ecdh(Point, ecdhOpts = {}) {
904
- const { Fn } = Point;
905
- const randomBytes_ = ecdhOpts.randomBytes || esm_utils.randomBytes;
906
- const lengths = Object.assign(getWLengths(Point.Fp, Fn), { seed: (0,modular.getMinHashLength)(Fn.ORDER) });
907
- function isValidSecretKey(secretKey) {
908
- try {
909
- return !!_normFnElement(Fn, secretKey);
910
- }
911
- catch (error) {
912
- return false;
913
- }
914
- }
915
- function isValidPublicKey(publicKey, isCompressed) {
916
- const { publicKey: comp, publicKeyUncompressed } = lengths;
917
- try {
918
- const l = publicKey.length;
919
- if (isCompressed === true && l !== comp)
920
- return false;
921
- if (isCompressed === false && l !== publicKeyUncompressed)
922
- return false;
923
- return !!Point.fromBytes(publicKey);
924
- }
925
- catch (error) {
926
- return false;
927
- }
928
- }
929
- /**
930
- * Produces cryptographically secure secret key from random of size
931
- * (groupLen + ceil(groupLen / 2)) with modulo bias being negligible.
932
- */
933
- function randomSecretKey(seed = randomBytes_(lengths.seed)) {
934
- return (0,modular.mapHashToField)((0,curves_esm_utils._abytes2)(seed, lengths.seed, 'seed'), Fn.ORDER);
935
- }
936
- /**
937
- * Computes public key for a secret key. Checks for validity of the secret key.
938
- * @param isCompressed whether to return compact (default), or full key
939
- * @returns Public key, full when isCompressed=false; short when isCompressed=true
940
- */
941
- function getPublicKey(secretKey, isCompressed = true) {
942
- return Point.BASE.multiply(_normFnElement(Fn, secretKey)).toBytes(isCompressed);
943
- }
944
- function keygen(seed) {
945
- const secretKey = randomSecretKey(seed);
946
- return { secretKey, publicKey: getPublicKey(secretKey) };
947
- }
948
- /**
949
- * Quick and dirty check for item being public key. Does not validate hex, or being on-curve.
950
- */
951
- function isProbPub(item) {
952
- if (typeof item === 'bigint')
953
- return false;
954
- if (item instanceof Point)
955
- return true;
956
- const { secretKey, publicKey, publicKeyUncompressed } = lengths;
957
- if (Fn.allowedLengths || secretKey === publicKey)
958
- return undefined;
959
- const l = (0,curves_esm_utils.ensureBytes)('key', item).length;
960
- return l === publicKey || l === publicKeyUncompressed;
961
- }
962
- /**
963
- * ECDH (Elliptic Curve Diffie Hellman).
964
- * Computes shared public key from secret key A and public key B.
965
- * Checks: 1) secret key validity 2) shared key is on-curve.
966
- * Does NOT hash the result.
967
- * @param isCompressed whether to return compact (default), or full key
968
- * @returns shared public key
969
- */
970
- function getSharedSecret(secretKeyA, publicKeyB, isCompressed = true) {
971
- if (isProbPub(secretKeyA) === true)
972
- throw new Error('first arg must be private key');
973
- if (isProbPub(publicKeyB) === false)
974
- throw new Error('second arg must be public key');
975
- const s = _normFnElement(Fn, secretKeyA);
976
- const b = Point.fromHex(publicKeyB); // checks for being on-curve
977
- return b.multiply(s).toBytes(isCompressed);
978
- }
979
- const utils = {
980
- isValidSecretKey,
981
- isValidPublicKey,
982
- randomSecretKey,
983
- // TODO: remove
984
- isValidPrivateKey: isValidSecretKey,
985
- randomPrivateKey: randomSecretKey,
986
- normPrivateKeyToScalar: (key) => _normFnElement(Fn, key),
987
- precompute(windowSize = 8, point = Point.BASE) {
988
- return point.precompute(windowSize, false);
989
- },
990
- };
991
- return Object.freeze({ getPublicKey, getSharedSecret, keygen, Point, utils, lengths });
992
- }
993
- /**
994
- * Creates ECDSA signing interface for given elliptic curve `Point` and `hash` function.
995
- * We need `hash` for 2 features:
996
- * 1. Message prehash-ing. NOT used if `sign` / `verify` are called with `prehash: false`
997
- * 2. k generation in `sign`, using HMAC-drbg(hash)
998
- *
999
- * ECDSAOpts are only rarely needed.
1000
- *
1001
- * @example
1002
- * ```js
1003
- * const p256_Point = weierstrass(...);
1004
- * const p256_sha256 = ecdsa(p256_Point, sha256);
1005
- * const p256_sha224 = ecdsa(p256_Point, sha224);
1006
- * const p256_sha224_r = ecdsa(p256_Point, sha224, { randomBytes: (length) => { ... } });
1007
- * ```
1008
- */
1009
- function ecdsa(Point, hash, ecdsaOpts = {}) {
1010
- (0,esm_utils.ahash)(hash);
1011
- (0,curves_esm_utils._validateObject)(ecdsaOpts, {}, {
1012
- hmac: 'function',
1013
- lowS: 'boolean',
1014
- randomBytes: 'function',
1015
- bits2int: 'function',
1016
- bits2int_modN: 'function',
1017
- });
1018
- const randomBytes = ecdsaOpts.randomBytes || esm_utils.randomBytes;
1019
- const hmac = ecdsaOpts.hmac ||
1020
- ((key, ...msgs) => (0,esm_hmac.hmac)(hash, key, (0,esm_utils.concatBytes)(...msgs)));
1021
- const { Fp, Fn } = Point;
1022
- const { ORDER: CURVE_ORDER, BITS: fnBits } = Fn;
1023
- const { keygen, getPublicKey, getSharedSecret, utils, lengths } = ecdh(Point, ecdsaOpts);
1024
- const defaultSigOpts = {
1025
- prehash: false,
1026
- lowS: typeof ecdsaOpts.lowS === 'boolean' ? ecdsaOpts.lowS : false,
1027
- format: undefined, //'compact' as ECDSASigFormat,
1028
- extraEntropy: false,
1029
- };
1030
- const defaultSigOpts_format = 'compact';
1031
- function isBiggerThanHalfOrder(number) {
1032
- const HALF = CURVE_ORDER >> _1n;
1033
- return number > HALF;
1034
- }
1035
- function validateRS(title, num) {
1036
- if (!Fn.isValidNot0(num))
1037
- throw new Error(`invalid signature ${title}: out of range 1..Point.Fn.ORDER`);
1038
- return num;
1039
- }
1040
- function validateSigLength(bytes, format) {
1041
- validateSigFormat(format);
1042
- const size = lengths.signature;
1043
- const sizer = format === 'compact' ? size : format === 'recovered' ? size + 1 : undefined;
1044
- return (0,curves_esm_utils._abytes2)(bytes, sizer, `${format} signature`);
1045
- }
1046
- /**
1047
- * ECDSA signature with its (r, s) properties. Supports compact, recovered & DER representations.
1048
- */
1049
- class Signature {
1050
- constructor(r, s, recovery) {
1051
- this.r = validateRS('r', r); // r in [1..N-1];
1052
- this.s = validateRS('s', s); // s in [1..N-1];
1053
- if (recovery != null)
1054
- this.recovery = recovery;
1055
- Object.freeze(this);
1056
- }
1057
- static fromBytes(bytes, format = defaultSigOpts_format) {
1058
- validateSigLength(bytes, format);
1059
- let recid;
1060
- if (format === 'der') {
1061
- const { r, s } = DER.toSig((0,curves_esm_utils._abytes2)(bytes));
1062
- return new Signature(r, s);
1063
- }
1064
- if (format === 'recovered') {
1065
- recid = bytes[0];
1066
- format = 'compact';
1067
- bytes = bytes.subarray(1);
1068
- }
1069
- const L = Fn.BYTES;
1070
- const r = bytes.subarray(0, L);
1071
- const s = bytes.subarray(L, L * 2);
1072
- return new Signature(Fn.fromBytes(r), Fn.fromBytes(s), recid);
1073
- }
1074
- static fromHex(hex, format) {
1075
- return this.fromBytes((0,esm_utils.hexToBytes)(hex), format);
1076
- }
1077
- addRecoveryBit(recovery) {
1078
- return new Signature(this.r, this.s, recovery);
1079
- }
1080
- recoverPublicKey(messageHash) {
1081
- const FIELD_ORDER = Fp.ORDER;
1082
- const { r, s, recovery: rec } = this;
1083
- if (rec == null || ![0, 1, 2, 3].includes(rec))
1084
- throw new Error('recovery id invalid');
1085
- // ECDSA recovery is hard for cofactor > 1 curves.
1086
- // In sign, `r = q.x mod n`, and here we recover q.x from r.
1087
- // While recovering q.x >= n, we need to add r+n for cofactor=1 curves.
1088
- // However, for cofactor>1, r+n may not get q.x:
1089
- // r+n*i would need to be done instead where i is unknown.
1090
- // To easily get i, we either need to:
1091
- // a. increase amount of valid recid values (4, 5...); OR
1092
- // b. prohibit non-prime-order signatures (recid > 1).
1093
- const hasCofactor = CURVE_ORDER * _2n < FIELD_ORDER;
1094
- if (hasCofactor && rec > 1)
1095
- throw new Error('recovery id is ambiguous for h>1 curve');
1096
- const radj = rec === 2 || rec === 3 ? r + CURVE_ORDER : r;
1097
- if (!Fp.isValid(radj))
1098
- throw new Error('recovery id 2 or 3 invalid');
1099
- const x = Fp.toBytes(radj);
1100
- const R = Point.fromBytes((0,esm_utils.concatBytes)(pprefix((rec & 1) === 0), x));
1101
- const ir = Fn.inv(radj); // r^-1
1102
- const h = bits2int_modN((0,curves_esm_utils.ensureBytes)('msgHash', messageHash)); // Truncate hash
1103
- const u1 = Fn.create(-h * ir); // -hr^-1
1104
- const u2 = Fn.create(s * ir); // sr^-1
1105
- // (sr^-1)R-(hr^-1)G = -(hr^-1)G + (sr^-1). unsafe is fine: there is no private data.
1106
- const Q = Point.BASE.multiplyUnsafe(u1).add(R.multiplyUnsafe(u2));
1107
- if (Q.is0())
1108
- throw new Error('point at infinify');
1109
- Q.assertValidity();
1110
- return Q;
1111
- }
1112
- // Signatures should be low-s, to prevent malleability.
1113
- hasHighS() {
1114
- return isBiggerThanHalfOrder(this.s);
1115
- }
1116
- toBytes(format = defaultSigOpts_format) {
1117
- validateSigFormat(format);
1118
- if (format === 'der')
1119
- return (0,esm_utils.hexToBytes)(DER.hexFromSig(this));
1120
- const r = Fn.toBytes(this.r);
1121
- const s = Fn.toBytes(this.s);
1122
- if (format === 'recovered') {
1123
- if (this.recovery == null)
1124
- throw new Error('recovery bit must be present');
1125
- return (0,esm_utils.concatBytes)(Uint8Array.of(this.recovery), r, s);
1126
- }
1127
- return (0,esm_utils.concatBytes)(r, s);
1128
- }
1129
- toHex(format) {
1130
- return (0,esm_utils.bytesToHex)(this.toBytes(format));
1131
- }
1132
- // TODO: remove
1133
- assertValidity() { }
1134
- static fromCompact(hex) {
1135
- return Signature.fromBytes((0,curves_esm_utils.ensureBytes)('sig', hex), 'compact');
1136
- }
1137
- static fromDER(hex) {
1138
- return Signature.fromBytes((0,curves_esm_utils.ensureBytes)('sig', hex), 'der');
1139
- }
1140
- normalizeS() {
1141
- return this.hasHighS() ? new Signature(this.r, Fn.neg(this.s), this.recovery) : this;
1142
- }
1143
- toDERRawBytes() {
1144
- return this.toBytes('der');
1145
- }
1146
- toDERHex() {
1147
- return (0,esm_utils.bytesToHex)(this.toBytes('der'));
1148
- }
1149
- toCompactRawBytes() {
1150
- return this.toBytes('compact');
1151
- }
1152
- toCompactHex() {
1153
- return (0,esm_utils.bytesToHex)(this.toBytes('compact'));
1154
- }
1155
- }
1156
- // RFC6979: ensure ECDSA msg is X bytes and < N. RFC suggests optional truncating via bits2octets.
1157
- // FIPS 186-4 4.6 suggests the leftmost min(nBitLen, outLen) bits, which matches bits2int.
1158
- // bits2int can produce res>N, we can do mod(res, N) since the bitLen is the same.
1159
- // int2octets can't be used; pads small msgs with 0: unacceptatble for trunc as per RFC vectors
1160
- const bits2int = ecdsaOpts.bits2int ||
1161
- function bits2int_def(bytes) {
1162
- // Our custom check "just in case", for protection against DoS
1163
- if (bytes.length > 8192)
1164
- throw new Error('input is too large');
1165
- // For curves with nBitLength % 8 !== 0: bits2octets(bits2octets(m)) !== bits2octets(m)
1166
- // for some cases, since bytes.length * 8 is not actual bitLength.
1167
- const num = (0,curves_esm_utils.bytesToNumberBE)(bytes); // check for == u8 done here
1168
- const delta = bytes.length * 8 - fnBits; // truncate to nBitLength leftmost bits
1169
- return delta > 0 ? num >> BigInt(delta) : num;
1170
- };
1171
- const bits2int_modN = ecdsaOpts.bits2int_modN ||
1172
- function bits2int_modN_def(bytes) {
1173
- return Fn.create(bits2int(bytes)); // can't use bytesToNumberBE here
1174
- };
1175
- // Pads output with zero as per spec
1176
- const ORDER_MASK = (0,curves_esm_utils.bitMask)(fnBits);
1177
- /** Converts to bytes. Checks if num in `[0..ORDER_MASK-1]` e.g.: `[0..2^256-1]`. */
1178
- function int2octets(num) {
1179
- // IMPORTANT: the check ensures working for case `Fn.BYTES != Fn.BITS * 8`
1180
- (0,curves_esm_utils.aInRange)('num < 2^' + fnBits, num, _0n, ORDER_MASK);
1181
- return Fn.toBytes(num);
1182
- }
1183
- function validateMsgAndHash(message, prehash) {
1184
- (0,curves_esm_utils._abytes2)(message, undefined, 'message');
1185
- return prehash ? (0,curves_esm_utils._abytes2)(hash(message), undefined, 'prehashed message') : message;
1186
- }
1187
- /**
1188
- * Steps A, D of RFC6979 3.2.
1189
- * Creates RFC6979 seed; converts msg/privKey to numbers.
1190
- * Used only in sign, not in verify.
1191
- *
1192
- * Warning: we cannot assume here that message has same amount of bytes as curve order,
1193
- * this will be invalid at least for P521. Also it can be bigger for P224 + SHA256.
1194
- */
1195
- function prepSig(message, privateKey, opts) {
1196
- if (['recovered', 'canonical'].some((k) => k in opts))
1197
- throw new Error('sign() legacy options not supported');
1198
- const { lowS, prehash, extraEntropy } = validateSigOpts(opts, defaultSigOpts);
1199
- message = validateMsgAndHash(message, prehash); // RFC6979 3.2 A: h1 = H(m)
1200
- // We can't later call bits2octets, since nested bits2int is broken for curves
1201
- // with fnBits % 8 !== 0. Because of that, we unwrap it here as int2octets call.
1202
- // const bits2octets = (bits) => int2octets(bits2int_modN(bits))
1203
- const h1int = bits2int_modN(message);
1204
- const d = _normFnElement(Fn, privateKey); // validate secret key, convert to bigint
1205
- const seedArgs = [int2octets(d), int2octets(h1int)];
1206
- // extraEntropy. RFC6979 3.6: additional k' (optional).
1207
- if (extraEntropy != null && extraEntropy !== false) {
1208
- // K = HMAC_K(V || 0x00 || int2octets(x) || bits2octets(h1) || k')
1209
- // gen random bytes OR pass as-is
1210
- const e = extraEntropy === true ? randomBytes(lengths.secretKey) : extraEntropy;
1211
- seedArgs.push((0,curves_esm_utils.ensureBytes)('extraEntropy', e)); // check for being bytes
1212
- }
1213
- const seed = (0,esm_utils.concatBytes)(...seedArgs); // Step D of RFC6979 3.2
1214
- const m = h1int; // NOTE: no need to call bits2int second time here, it is inside truncateHash!
1215
- // Converts signature params into point w r/s, checks result for validity.
1216
- // To transform k => Signature:
1217
- // q = k⋅G
1218
- // r = q.x mod n
1219
- // s = k^-1(m + rd) mod n
1220
- // Can use scalar blinding b^-1(bm + bdr) where b ∈ [1,q−1] according to
1221
- // https://tches.iacr.org/index.php/TCHES/article/view/7337/6509. We've decided against it:
1222
- // a) dependency on CSPRNG b) 15% slowdown c) doesn't really help since bigints are not CT
1223
- function k2sig(kBytes) {
1224
- // RFC 6979 Section 3.2, step 3: k = bits2int(T)
1225
- // Important: all mod() calls here must be done over N
1226
- const k = bits2int(kBytes); // mod n, not mod p
1227
- if (!Fn.isValidNot0(k))
1228
- return; // Valid scalars (including k) must be in 1..N-1
1229
- const ik = Fn.inv(k); // k^-1 mod n
1230
- const q = Point.BASE.multiply(k).toAffine(); // q = k⋅G
1231
- const r = Fn.create(q.x); // r = q.x mod n
1232
- if (r === _0n)
1233
- return;
1234
- const s = Fn.create(ik * Fn.create(m + r * d)); // Not using blinding here, see comment above
1235
- if (s === _0n)
1236
- return;
1237
- let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n); // recovery bit (2 or 3, when q.x > n)
1238
- let normS = s;
1239
- if (lowS && isBiggerThanHalfOrder(s)) {
1240
- normS = Fn.neg(s); // if lowS was passed, ensure s is always
1241
- recovery ^= 1; // // in the bottom half of N
1242
- }
1243
- return new Signature(r, normS, recovery); // use normS, not s
1244
- }
1245
- return { seed, k2sig };
1246
- }
1247
- /**
1248
- * Signs message hash with a secret key.
1249
- *
1250
- * ```
1251
- * sign(m, d) where
1252
- * k = rfc6979_hmac_drbg(m, d)
1253
- * (x, y) = G × k
1254
- * r = x mod n
1255
- * s = (m + dr) / k mod n
1256
- * ```
1257
- */
1258
- function sign(message, secretKey, opts = {}) {
1259
- message = (0,curves_esm_utils.ensureBytes)('message', message);
1260
- const { seed, k2sig } = prepSig(message, secretKey, opts); // Steps A, D of RFC6979 3.2.
1261
- const drbg = (0,curves_esm_utils.createHmacDrbg)(hash.outputLen, Fn.BYTES, hmac);
1262
- const sig = drbg(seed, k2sig); // Steps B, C, D, E, F, G
1263
- return sig;
1264
- }
1265
- function tryParsingSig(sg) {
1266
- // Try to deduce format
1267
- let sig = undefined;
1268
- const isHex = typeof sg === 'string' || (0,esm_utils.isBytes)(sg);
1269
- const isObj = !isHex &&
1270
- sg !== null &&
1271
- typeof sg === 'object' &&
1272
- typeof sg.r === 'bigint' &&
1273
- typeof sg.s === 'bigint';
1274
- if (!isHex && !isObj)
1275
- throw new Error('invalid signature, expected Uint8Array, hex string or Signature instance');
1276
- if (isObj) {
1277
- sig = new Signature(sg.r, sg.s);
1278
- }
1279
- else if (isHex) {
1280
- try {
1281
- sig = Signature.fromBytes((0,curves_esm_utils.ensureBytes)('sig', sg), 'der');
1282
- }
1283
- catch (derError) {
1284
- if (!(derError instanceof DER.Err))
1285
- throw derError;
1286
- }
1287
- if (!sig) {
1288
- try {
1289
- sig = Signature.fromBytes((0,curves_esm_utils.ensureBytes)('sig', sg), 'compact');
1290
- }
1291
- catch (error) {
1292
- return false;
1293
- }
1294
- }
1295
- }
1296
- if (!sig)
1297
- return false;
1298
- return sig;
1299
- }
1300
- /**
1301
- * Verifies a signature against message and public key.
1302
- * Rejects lowS signatures by default: see {@link ECDSAVerifyOpts}.
1303
- * Implements section 4.1.4 from https://www.secg.org/sec1-v2.pdf:
1304
- *
1305
- * ```
1306
- * verify(r, s, h, P) where
1307
- * u1 = hs^-1 mod n
1308
- * u2 = rs^-1 mod n
1309
- * R = u1⋅G + u2⋅P
1310
- * mod(R.x, n) == r
1311
- * ```
1312
- */
1313
- function verify(signature, message, publicKey, opts = {}) {
1314
- const { lowS, prehash, format } = validateSigOpts(opts, defaultSigOpts);
1315
- publicKey = (0,curves_esm_utils.ensureBytes)('publicKey', publicKey);
1316
- message = validateMsgAndHash((0,curves_esm_utils.ensureBytes)('message', message), prehash);
1317
- if ('strict' in opts)
1318
- throw new Error('options.strict was renamed to lowS');
1319
- const sig = format === undefined
1320
- ? tryParsingSig(signature)
1321
- : Signature.fromBytes((0,curves_esm_utils.ensureBytes)('sig', signature), format);
1322
- if (sig === false)
1323
- return false;
1324
- try {
1325
- const P = Point.fromBytes(publicKey);
1326
- if (lowS && sig.hasHighS())
1327
- return false;
1328
- const { r, s } = sig;
1329
- const h = bits2int_modN(message); // mod n, not mod p
1330
- const is = Fn.inv(s); // s^-1 mod n
1331
- const u1 = Fn.create(h * is); // u1 = hs^-1 mod n
1332
- const u2 = Fn.create(r * is); // u2 = rs^-1 mod n
1333
- const R = Point.BASE.multiplyUnsafe(u1).add(P.multiplyUnsafe(u2)); // u1⋅G + u2⋅P
1334
- if (R.is0())
1335
- return false;
1336
- const v = Fn.create(R.x); // v = r.x mod n
1337
- return v === r;
1338
- }
1339
- catch (e) {
1340
- return false;
1341
- }
1342
- }
1343
- function recoverPublicKey(signature, message, opts = {}) {
1344
- const { prehash } = validateSigOpts(opts, defaultSigOpts);
1345
- message = validateMsgAndHash(message, prehash);
1346
- return Signature.fromBytes(signature, 'recovered').recoverPublicKey(message).toBytes();
1347
- }
1348
- return Object.freeze({
1349
- keygen,
1350
- getPublicKey,
1351
- getSharedSecret,
1352
- utils,
1353
- lengths,
1354
- Point,
1355
- sign,
1356
- verify,
1357
- recoverPublicKey,
1358
- Signature,
1359
- hash,
1360
- });
1361
- }
1362
- /** @deprecated use `weierstrass` in newer releases */
1363
- function weierstrassPoints(c) {
1364
- const { CURVE, curveOpts } = _weierstrass_legacy_opts_to_new(c);
1365
- const Point = weierstrassN(CURVE, curveOpts);
1366
- return _weierstrass_new_output_to_legacy(c, Point);
1367
- }
1368
- function _weierstrass_legacy_opts_to_new(c) {
1369
- const CURVE = {
1370
- a: c.a,
1371
- b: c.b,
1372
- p: c.Fp.ORDER,
1373
- n: c.n,
1374
- h: c.h,
1375
- Gx: c.Gx,
1376
- Gy: c.Gy,
1377
- };
1378
- const Fp = c.Fp;
1379
- let allowedLengths = c.allowedPrivateKeyLengths
1380
- ? Array.from(new Set(c.allowedPrivateKeyLengths.map((l) => Math.ceil(l / 2))))
1381
- : undefined;
1382
- const Fn = (0,modular.Field)(CURVE.n, {
1383
- BITS: c.nBitLength,
1384
- allowedLengths: allowedLengths,
1385
- modFromBytes: c.wrapPrivateKey,
1386
- });
1387
- const curveOpts = {
1388
- Fp,
1389
- Fn,
1390
- allowInfinityPoint: c.allowInfinityPoint,
1391
- endo: c.endo,
1392
- isTorsionFree: c.isTorsionFree,
1393
- clearCofactor: c.clearCofactor,
1394
- fromBytes: c.fromBytes,
1395
- toBytes: c.toBytes,
1396
- };
1397
- return { CURVE, curveOpts };
1398
- }
1399
- function _ecdsa_legacy_opts_to_new(c) {
1400
- const { CURVE, curveOpts } = _weierstrass_legacy_opts_to_new(c);
1401
- const ecdsaOpts = {
1402
- hmac: c.hmac,
1403
- randomBytes: c.randomBytes,
1404
- lowS: c.lowS,
1405
- bits2int: c.bits2int,
1406
- bits2int_modN: c.bits2int_modN,
1407
- };
1408
- return { CURVE, curveOpts, hash: c.hash, ecdsaOpts };
1409
- }
1410
- function _legacyHelperEquat(Fp, a, b) {
1411
- /**
1412
- * y² = x³ + ax + b: Short weierstrass curve formula. Takes x, returns y².
1413
- * @returns y²
1414
- */
1415
- function weierstrassEquation(x) {
1416
- const x2 = Fp.sqr(x); // x * x
1417
- const x3 = Fp.mul(x2, x); // x² * x
1418
- return Fp.add(Fp.add(x3, Fp.mul(x, a)), b); // x³ + a * x + b
1419
- }
1420
- return weierstrassEquation;
1421
- }
1422
- function _weierstrass_new_output_to_legacy(c, Point) {
1423
- const { Fp, Fn } = Point;
1424
- function isWithinCurveOrder(num) {
1425
- return (0,curves_esm_utils.inRange)(num, _1n, Fn.ORDER);
1426
- }
1427
- const weierstrassEquation = _legacyHelperEquat(Fp, c.a, c.b);
1428
- return Object.assign({}, {
1429
- CURVE: c,
1430
- Point: Point,
1431
- ProjectivePoint: Point,
1432
- normPrivateKeyToScalar: (key) => _normFnElement(Fn, key),
1433
- weierstrassEquation,
1434
- isWithinCurveOrder,
1435
- });
1436
- }
1437
- function _ecdsa_new_output_to_legacy(c, _ecdsa) {
1438
- const Point = _ecdsa.Point;
1439
- return Object.assign({}, _ecdsa, {
1440
- ProjectivePoint: Point,
1441
- CURVE: Object.assign({}, c, (0,modular.nLength)(Point.Fn.ORDER, Point.Fn.BITS)),
1442
- });
1443
- }
1444
- // _ecdsa_legacy
1445
- function weierstrass(c) {
1446
- const { CURVE, curveOpts, hash, ecdsaOpts } = _ecdsa_legacy_opts_to_new(c);
1447
- const Point = weierstrassN(CURVE, curveOpts);
1448
- const signs = ecdsa(Point, hash, ecdsaOpts);
1449
- return _ecdsa_new_output_to_legacy(c, signs);
1450
- }
1451
- //# sourceMappingURL=weierstrass.js.map
1452
- ;// ./node_modules/@noble/curves/esm/_shortw_utils.js
1453
- /**
1454
- * Utilities for short weierstrass curves, combined with noble-hashes.
1455
- * @module
1456
- */
1457
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
1458
-
1459
- /** connects noble-curves to noble-hashes */
1460
- function getHash(hash) {
1461
- return { hash };
1462
- }
1463
- /** @deprecated use new `weierstrass()` and `ecdsa()` methods */
1464
- function createCurve(curveDef, defHash) {
1465
- const create = (hash) => weierstrass({ ...curveDef, hash: hash });
1466
- return { ...create(defHash), create };
1467
- }
1468
- //# sourceMappingURL=_shortw_utils.js.map
1469
- // EXTERNAL MODULE: ./node_modules/@noble/curves/esm/abstract/hash-to-curve.js
1470
- var hash_to_curve = __webpack_require__("./node_modules/@noble/curves/esm/abstract/hash-to-curve.js");
1471
- ;// ./node_modules/@noble/curves/esm/nist.js
1472
- /**
1473
- * Internal module for NIST P256, P384, P521 curves.
1474
- * Do not use for now.
1475
- * @module
1476
- */
1477
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
1478
-
1479
-
1480
-
1481
-
1482
-
1483
- // p = 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n - 1n
1484
- // a = Fp256.create(BigInt('-3'));
1485
- const p256_CURVE = {
1486
- p: BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'),
1487
- n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
1488
- h: BigInt(1),
1489
- a: BigInt('0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc'),
1490
- b: BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b'),
1491
- Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
1492
- Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
1493
- };
1494
- // p = 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
1495
- const p384_CURVE = {
1496
- p: BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff'),
1497
- n: BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'),
1498
- h: BigInt(1),
1499
- a: BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc'),
1500
- b: BigInt('0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef'),
1501
- Gx: BigInt('0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'),
1502
- Gy: BigInt('0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'),
1503
- };
1504
- // p = 2n**521n - 1n
1505
- const p521_CURVE = {
1506
- p: BigInt('0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'),
1507
- n: BigInt('0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'),
1508
- h: BigInt(1),
1509
- a: BigInt('0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc'),
1510
- b: BigInt('0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00'),
1511
- Gx: BigInt('0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66'),
1512
- Gy: BigInt('0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'),
1513
- };
1514
- const Fp256 = (0,modular.Field)(p256_CURVE.p);
1515
- const Fp384 = (0,modular.Field)(p384_CURVE.p);
1516
- const Fp521 = (0,modular.Field)(p521_CURVE.p);
1517
- function createSWU(Point, opts) {
1518
- const map = mapToCurveSimpleSWU(Point.Fp, opts);
1519
- return (scalars) => map(scalars[0]);
1520
- }
1521
- /** NIST P256 (aka secp256r1, prime256v1) curve, ECDSA and ECDH methods. */
1522
- const p256 = createCurve({ ...p256_CURVE, Fp: Fp256, lowS: false }, sha2.sha256);
1523
- /** Hashing / encoding to p256 points / field. RFC 9380 methods. */
1524
- const p256_hasher = /* @__PURE__ */ (() => {
1525
- return (0,hash_to_curve.createHasher)(p256.Point, createSWU(p256.Point, {
1526
- A: p256_CURVE.a,
1527
- B: p256_CURVE.b,
1528
- Z: p256.Point.Fp.create(BigInt('-10')),
1529
- }), {
1530
- DST: 'P256_XMD:SHA-256_SSWU_RO_',
1531
- encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
1532
- p: p256_CURVE.p,
1533
- m: 1,
1534
- k: 128,
1535
- expand: 'xmd',
1536
- hash: sha2.sha256,
1537
- });
1538
- })();
1539
- // export const p256_oprf: OPRF = createORPF({
1540
- // name: 'P256-SHA256',
1541
- // Point: p256.Point,
1542
- // hash: sha256,
1543
- // hashToGroup: p256_hasher.hashToCurve,
1544
- // hashToScalar: p256_hasher.hashToScalar,
1545
- // });
1546
- /** NIST P384 (aka secp384r1) curve, ECDSA and ECDH methods. */
1547
- const p384 = createCurve({ ...p384_CURVE, Fp: Fp384, lowS: false }, sha2.sha384);
1548
- /** Hashing / encoding to p384 points / field. RFC 9380 methods. */
1549
- const p384_hasher = /* @__PURE__ */ (() => {
1550
- return (0,hash_to_curve.createHasher)(p384.Point, createSWU(p384.Point, {
1551
- A: p384_CURVE.a,
1552
- B: p384_CURVE.b,
1553
- Z: p384.Point.Fp.create(BigInt('-12')),
1554
- }), {
1555
- DST: 'P384_XMD:SHA-384_SSWU_RO_',
1556
- encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
1557
- p: p384_CURVE.p,
1558
- m: 1,
1559
- k: 192,
1560
- expand: 'xmd',
1561
- hash: sha2.sha384,
1562
- });
1563
- })();
1564
- // export const p384_oprf: OPRF = createORPF({
1565
- // name: 'P384-SHA384',
1566
- // Point: p384.Point,
1567
- // hash: sha384,
1568
- // hashToGroup: p384_hasher.hashToCurve,
1569
- // hashToScalar: p384_hasher.hashToScalar,
1570
- // });
1571
- // const Fn521 = Field(p521_CURVE.n, { allowedScalarLengths: [65, 66] });
1572
- /** NIST P521 (aka secp521r1) curve, ECDSA and ECDH methods. */
1573
- const p521 = createCurve({ ...p521_CURVE, Fp: Fp521, lowS: false, allowedPrivateKeyLengths: [130, 131, 132] }, sha2.sha512);
1574
- /** @deprecated use `p256` for consistency with `p256_hasher` */
1575
- const secp256r1 = p256;
1576
- /** @deprecated use `p384` for consistency with `p384_hasher` */
1577
- const secp384r1 = p384;
1578
- /** @deprecated use `p521` for consistency with `p521_hasher` */
1579
- const secp521r1 = p521;
1580
- /** Hashing / encoding to p521 points / field. RFC 9380 methods. */
1581
- const p521_hasher = /* @__PURE__ */ (() => {
1582
- return (0,hash_to_curve.createHasher)(p521.Point, createSWU(p521.Point, {
1583
- A: p521_CURVE.a,
1584
- B: p521_CURVE.b,
1585
- Z: p521.Point.Fp.create(BigInt('-4')),
1586
- }), {
1587
- DST: 'P521_XMD:SHA-512_SSWU_RO_',
1588
- encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
1589
- p: p521_CURVE.p,
1590
- m: 1,
1591
- k: 256,
1592
- expand: 'xmd',
1593
- hash: sha2.sha512,
1594
- });
1595
- })();
1596
- // export const p521_oprf: OPRF = createORPF({
1597
- // name: 'P521-SHA512',
1598
- // Point: p521.Point,
1599
- // hash: sha512,
1600
- // hashToGroup: p521_hasher.hashToCurve,
1601
- // hashToScalar: p521_hasher.hashToScalar, // produces L=98 just like in RFC
1602
- // });
1603
- //# sourceMappingURL=nist.js.map
1604
-
1605
- /***/ })
1606
-
1607
- }]);
1608
- //# sourceMappingURL=defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map