@noble/curves 1.9.7 → 2.0.0-beta.2

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 (242) hide show
  1. package/README.md +520 -505
  2. package/abstract/bls.d.ts +58 -120
  3. package/abstract/bls.d.ts.map +1 -1
  4. package/abstract/bls.js +108 -152
  5. package/abstract/bls.js.map +1 -1
  6. package/abstract/curve.d.ts +18 -54
  7. package/abstract/curve.d.ts.map +1 -1
  8. package/abstract/curve.js +30 -49
  9. package/abstract/curve.js.map +1 -1
  10. package/abstract/edwards.d.ts +18 -77
  11. package/abstract/edwards.d.ts.map +1 -1
  12. package/abstract/edwards.js +68 -144
  13. package/abstract/edwards.js.map +1 -1
  14. package/abstract/fft.js +14 -27
  15. package/abstract/fft.js.map +1 -1
  16. package/abstract/hash-to-curve.d.ts +35 -47
  17. package/abstract/hash-to-curve.d.ts.map +1 -1
  18. package/abstract/hash-to-curve.js +42 -46
  19. package/abstract/hash-to-curve.js.map +1 -1
  20. package/abstract/modular.d.ts +5 -17
  21. package/abstract/modular.d.ts.map +1 -1
  22. package/abstract/modular.js +170 -169
  23. package/abstract/modular.js.map +1 -1
  24. package/abstract/montgomery.d.ts +7 -12
  25. package/abstract/montgomery.d.ts.map +1 -1
  26. package/abstract/montgomery.js +22 -29
  27. package/abstract/montgomery.js.map +1 -1
  28. package/abstract/oprf.d.ts +282 -0
  29. package/abstract/oprf.d.ts.map +1 -0
  30. package/abstract/oprf.js +297 -0
  31. package/abstract/oprf.js.map +1 -0
  32. package/abstract/poseidon.d.ts.map +1 -1
  33. package/abstract/poseidon.js +26 -31
  34. package/abstract/poseidon.js.map +1 -1
  35. package/abstract/tower.d.ts.map +1 -1
  36. package/abstract/tower.js +43 -19
  37. package/abstract/tower.js.map +1 -1
  38. package/abstract/weierstrass.d.ts +77 -168
  39. package/abstract/weierstrass.d.ts.map +1 -1
  40. package/abstract/weierstrass.js +184 -389
  41. package/abstract/weierstrass.js.map +1 -1
  42. package/bls12-381.d.ts +5 -11
  43. package/bls12-381.d.ts.map +1 -1
  44. package/bls12-381.js +161 -181
  45. package/bls12-381.js.map +1 -1
  46. package/bn254.d.ts +59 -11
  47. package/bn254.d.ts.map +1 -1
  48. package/bn254.js +69 -97
  49. package/bn254.js.map +1 -1
  50. package/ed25519.d.ts +33 -48
  51. package/ed25519.d.ts.map +1 -1
  52. package/ed25519.js +147 -161
  53. package/ed25519.js.map +1 -1
  54. package/ed448.d.ts +27 -36
  55. package/ed448.d.ts.map +1 -1
  56. package/ed448.js +143 -164
  57. package/ed448.js.map +1 -1
  58. package/index.d.ts +1 -0
  59. package/index.js +20 -4
  60. package/index.js.map +1 -1
  61. package/misc.d.ts +10 -14
  62. package/misc.d.ts.map +1 -1
  63. package/misc.js +53 -62
  64. package/misc.js.map +1 -1
  65. package/nist.d.ts +31 -16
  66. package/nist.d.ts.map +1 -1
  67. package/nist.js +75 -64
  68. package/nist.js.map +1 -1
  69. package/package.json +20 -234
  70. package/secp256k1.d.ts +17 -30
  71. package/secp256k1.d.ts.map +1 -1
  72. package/secp256k1.js +59 -73
  73. package/secp256k1.js.map +1 -1
  74. package/src/abstract/bls.ts +207 -354
  75. package/src/abstract/curve.ts +25 -84
  76. package/src/abstract/edwards.ts +68 -193
  77. package/src/abstract/hash-to-curve.ts +71 -85
  78. package/src/abstract/modular.ts +150 -134
  79. package/src/abstract/montgomery.ts +28 -35
  80. package/src/abstract/oprf.ts +600 -0
  81. package/src/abstract/poseidon.ts +6 -8
  82. package/src/abstract/tower.ts +0 -3
  83. package/src/abstract/weierstrass.ts +203 -525
  84. package/src/bls12-381.ts +133 -139
  85. package/src/bn254.ts +69 -93
  86. package/src/ed25519.ts +106 -133
  87. package/src/ed448.ts +111 -138
  88. package/src/index.ts +19 -3
  89. package/src/misc.ts +68 -51
  90. package/src/nist.ts +77 -70
  91. package/src/secp256k1.ts +46 -81
  92. package/src/utils.ts +67 -137
  93. package/src/webcrypto.ts +403 -0
  94. package/utils.d.ts +31 -38
  95. package/utils.d.ts.map +1 -1
  96. package/utils.js +66 -185
  97. package/utils.js.map +1 -1
  98. package/webcrypto.d.ts +99 -0
  99. package/webcrypto.d.ts.map +1 -0
  100. package/webcrypto.js +256 -0
  101. package/webcrypto.js.map +1 -0
  102. package/_shortw_utils.d.ts +0 -19
  103. package/_shortw_utils.d.ts.map +0 -1
  104. package/_shortw_utils.js +0 -20
  105. package/_shortw_utils.js.map +0 -1
  106. package/abstract/utils.d.ts +0 -78
  107. package/abstract/utils.d.ts.map +0 -1
  108. package/abstract/utils.js +0 -73
  109. package/abstract/utils.js.map +0 -1
  110. package/esm/_shortw_utils.d.ts +0 -19
  111. package/esm/_shortw_utils.d.ts.map +0 -1
  112. package/esm/_shortw_utils.js +0 -16
  113. package/esm/_shortw_utils.js.map +0 -1
  114. package/esm/abstract/bls.d.ts +0 -190
  115. package/esm/abstract/bls.d.ts.map +0 -1
  116. package/esm/abstract/bls.js +0 -408
  117. package/esm/abstract/bls.js.map +0 -1
  118. package/esm/abstract/curve.d.ts +0 -231
  119. package/esm/abstract/curve.d.ts.map +0 -1
  120. package/esm/abstract/curve.js +0 -465
  121. package/esm/abstract/curve.js.map +0 -1
  122. package/esm/abstract/edwards.d.ts +0 -243
  123. package/esm/abstract/edwards.d.ts.map +0 -1
  124. package/esm/abstract/edwards.js +0 -627
  125. package/esm/abstract/edwards.js.map +0 -1
  126. package/esm/abstract/fft.d.ts +0 -122
  127. package/esm/abstract/fft.d.ts.map +0 -1
  128. package/esm/abstract/fft.js +0 -425
  129. package/esm/abstract/fft.js.map +0 -1
  130. package/esm/abstract/hash-to-curve.d.ts +0 -102
  131. package/esm/abstract/hash-to-curve.d.ts.map +0 -1
  132. package/esm/abstract/hash-to-curve.js +0 -203
  133. package/esm/abstract/hash-to-curve.js.map +0 -1
  134. package/esm/abstract/modular.d.ts +0 -171
  135. package/esm/abstract/modular.d.ts.map +0 -1
  136. package/esm/abstract/modular.js +0 -530
  137. package/esm/abstract/modular.js.map +0 -1
  138. package/esm/abstract/montgomery.d.ts +0 -30
  139. package/esm/abstract/montgomery.d.ts.map +0 -1
  140. package/esm/abstract/montgomery.js +0 -157
  141. package/esm/abstract/montgomery.js.map +0 -1
  142. package/esm/abstract/poseidon.d.ts +0 -68
  143. package/esm/abstract/poseidon.d.ts.map +0 -1
  144. package/esm/abstract/poseidon.js +0 -296
  145. package/esm/abstract/poseidon.js.map +0 -1
  146. package/esm/abstract/tower.d.ts +0 -95
  147. package/esm/abstract/tower.d.ts.map +0 -1
  148. package/esm/abstract/tower.js +0 -714
  149. package/esm/abstract/tower.js.map +0 -1
  150. package/esm/abstract/utils.d.ts +0 -78
  151. package/esm/abstract/utils.d.ts.map +0 -1
  152. package/esm/abstract/utils.js +0 -70
  153. package/esm/abstract/utils.js.map +0 -1
  154. package/esm/abstract/weierstrass.d.ts +0 -416
  155. package/esm/abstract/weierstrass.d.ts.map +0 -1
  156. package/esm/abstract/weierstrass.js +0 -1413
  157. package/esm/abstract/weierstrass.js.map +0 -1
  158. package/esm/bls12-381.d.ts +0 -16
  159. package/esm/bls12-381.d.ts.map +0 -1
  160. package/esm/bls12-381.js +0 -705
  161. package/esm/bls12-381.js.map +0 -1
  162. package/esm/bn254.d.ts +0 -18
  163. package/esm/bn254.d.ts.map +0 -1
  164. package/esm/bn254.js +0 -214
  165. package/esm/bn254.js.map +0 -1
  166. package/esm/ed25519.d.ts +0 -106
  167. package/esm/ed25519.d.ts.map +0 -1
  168. package/esm/ed25519.js +0 -467
  169. package/esm/ed25519.js.map +0 -1
  170. package/esm/ed448.d.ts +0 -100
  171. package/esm/ed448.d.ts.map +0 -1
  172. package/esm/ed448.js +0 -459
  173. package/esm/ed448.js.map +0 -1
  174. package/esm/index.d.ts +0 -2
  175. package/esm/index.d.ts.map +0 -1
  176. package/esm/index.js +0 -17
  177. package/esm/index.js.map +0 -1
  178. package/esm/jubjub.d.ts +0 -12
  179. package/esm/jubjub.d.ts.map +0 -1
  180. package/esm/jubjub.js +0 -12
  181. package/esm/jubjub.js.map +0 -1
  182. package/esm/misc.d.ts +0 -19
  183. package/esm/misc.d.ts.map +0 -1
  184. package/esm/misc.js +0 -109
  185. package/esm/misc.js.map +0 -1
  186. package/esm/nist.d.ts +0 -21
  187. package/esm/nist.d.ts.map +0 -1
  188. package/esm/nist.js +0 -132
  189. package/esm/nist.js.map +0 -1
  190. package/esm/p256.d.ts +0 -16
  191. package/esm/p256.d.ts.map +0 -1
  192. package/esm/p256.js +0 -16
  193. package/esm/p256.js.map +0 -1
  194. package/esm/p384.d.ts +0 -16
  195. package/esm/p384.d.ts.map +0 -1
  196. package/esm/p384.js +0 -16
  197. package/esm/p384.js.map +0 -1
  198. package/esm/p521.d.ts +0 -16
  199. package/esm/p521.d.ts.map +0 -1
  200. package/esm/p521.js +0 -16
  201. package/esm/p521.js.map +0 -1
  202. package/esm/package.json +0 -4
  203. package/esm/pasta.d.ts +0 -10
  204. package/esm/pasta.d.ts.map +0 -1
  205. package/esm/pasta.js +0 -10
  206. package/esm/pasta.js.map +0 -1
  207. package/esm/secp256k1.d.ts +0 -89
  208. package/esm/secp256k1.d.ts.map +0 -1
  209. package/esm/secp256k1.js +0 -294
  210. package/esm/secp256k1.js.map +0 -1
  211. package/esm/utils.d.ts +0 -110
  212. package/esm/utils.d.ts.map +0 -1
  213. package/esm/utils.js +0 -322
  214. package/esm/utils.js.map +0 -1
  215. package/jubjub.d.ts +0 -12
  216. package/jubjub.d.ts.map +0 -1
  217. package/jubjub.js +0 -15
  218. package/jubjub.js.map +0 -1
  219. package/p256.d.ts +0 -16
  220. package/p256.d.ts.map +0 -1
  221. package/p256.js +0 -13
  222. package/p256.js.map +0 -1
  223. package/p384.d.ts +0 -16
  224. package/p384.d.ts.map +0 -1
  225. package/p384.js +0 -13
  226. package/p384.js.map +0 -1
  227. package/p521.d.ts +0 -16
  228. package/p521.d.ts.map +0 -1
  229. package/p521.js +0 -13
  230. package/p521.js.map +0 -1
  231. package/pasta.d.ts +0 -10
  232. package/pasta.d.ts.map +0 -1
  233. package/pasta.js +0 -13
  234. package/pasta.js.map +0 -1
  235. package/src/_shortw_utils.ts +0 -21
  236. package/src/abstract/utils.ts +0 -80
  237. package/src/jubjub.ts +0 -12
  238. package/src/p256.ts +0 -15
  239. package/src/p384.ts +0 -15
  240. package/src/p521.ts +0 -15
  241. package/src/package.json +0 -3
  242. package/src/pasta.ts +0 -9
@@ -4,29 +4,17 @@
4
4
  * @module
5
5
  */
6
6
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
- import { bitLen, bitMask, validateObject } from '../utils.ts';
8
- import { Field, FpInvertBatch, nLength, validateField, type IField } from './modular.ts';
7
+ import { bitLen, bitMask, type Signer } from '../utils.ts';
8
+ import { Field, FpInvertBatch, validateField, type IField } from './modular.ts';
9
9
 
10
- const _0n = BigInt(0);
11
- const _1n = BigInt(1);
10
+ const _0n = /* @__PURE__ */ BigInt(0);
11
+ const _1n = /* @__PURE__ */ BigInt(1);
12
12
 
13
13
  export type AffinePoint<T> = {
14
14
  x: T;
15
15
  y: T;
16
16
  } & { Z?: never };
17
17
 
18
- // This was initialy do this way to re-use montgomery ladder in field (add->mul,double->sqr), but
19
- // that didn't happen and there is probably not much reason to have separate Group like this?
20
- export interface Group<T extends Group<T>> {
21
- double(): T;
22
- negate(): T;
23
- add(other: T): T;
24
- subtract(other: T): T;
25
- equals(other: T): boolean;
26
- multiply(scalar: bigint): T;
27
- toAffine?(invertedZ?: any): AffinePoint<any>;
28
- }
29
-
30
18
  // We can't "abstract out" coordinates (X, Y, Z; and T in Edwards): argument names of constructor
31
19
  // are not accessible. See Typescript gh-56093, gh-41594.
32
20
  //
@@ -36,7 +24,7 @@ export interface Group<T extends Group<T>> {
36
24
  // but we lose all constrains on methods.
37
25
 
38
26
  /** Base interface for all elliptic curve Points. */
39
- export interface CurvePoint<F, P extends CurvePoint<F, P>> extends Group<P> {
27
+ export interface CurvePoint<F, P extends CurvePoint<F, P>> {
40
28
  /** Affine x coordinate. Different from projective / extended X coordinate. */
41
29
  x: F;
42
30
  /** Affine y coordinate. Different from projective / extended Y coordinate. */
@@ -77,7 +65,7 @@ export interface CurvePointCons<P extends CurvePoint<any, P>> {
77
65
  /** Creates point from x, y. Does NOT validate if the point is valid. Use `.assertValidity()`. */
78
66
  fromAffine(p: AffinePoint<P_F<P>>): P;
79
67
  fromBytes(bytes: Uint8Array): P;
80
- fromHex(hex: Uint8Array | string): P;
68
+ fromHex(hex: string): P;
81
69
  }
82
70
 
83
71
  // Type inference helpers: PC - PointConstructor, P - Point, Fp - Field element
@@ -134,16 +122,7 @@ export interface CurveLengths {
134
122
  signature?: number;
135
123
  seed?: number;
136
124
  }
137
- export type GroupConstructor<T> = {
138
- BASE: T;
139
- ZERO: T;
140
- };
141
- /** @deprecated */
142
- export type ExtendedGroupConstructor<T> = GroupConstructor<T> & {
143
- Fp: IField<any>;
144
- Fn: IField<bigint>;
145
- fromAffine(ap: AffinePoint<any>): T;
146
- };
125
+
147
126
  export type Mapper<T> = (i: T[]) => T[];
148
127
 
149
128
  export function negateCt<T extends { negate: () => T }>(condition: boolean, item: T): T {
@@ -174,7 +153,7 @@ function validateW(W: number, bits: number) {
174
153
  }
175
154
 
176
155
  /** Internal wNAF opts for specific W and scalarBits */
177
- export type WOpts = {
156
+ type WOpts = {
178
157
  windows: number;
179
158
  windowSize: number;
180
159
  mask: bigint;
@@ -464,7 +443,6 @@ export function mulEndoUnsafe<P extends CurvePoint<any, P>, PC extends CurvePoin
464
443
  */
465
444
  export function pippenger<P extends CurvePoint<any, P>, PC extends CurvePointCons<P>>(
466
445
  c: PC,
467
- fieldN: IField<bigint>,
468
446
  points: P[],
469
447
  scalars: bigint[]
470
448
  ): P {
@@ -474,6 +452,7 @@ export function pippenger<P extends CurvePoint<any, P>, PC extends CurvePointCon
474
452
  // - https://eprint.iacr.org/2024/750.pdf
475
453
  // - https://tches.iacr.org/index.php/TCHES/article/view/10287
476
454
  // 0 is accepted in scalars
455
+ const fieldN = c.Fn;
477
456
  validateMSMPoints(points, c);
478
457
  validateMSMScalars(scalars, fieldN);
479
458
  const plength = points.length;
@@ -517,7 +496,6 @@ export function pippenger<P extends CurvePoint<any, P>, PC extends CurvePointCon
517
496
  */
518
497
  export function precomputeMSMUnsafe<P extends CurvePoint<any, P>, PC extends CurvePointCons<P>>(
519
498
  c: PC,
520
- fieldN: IField<bigint>,
521
499
  points: P[],
522
500
  windowSize: number
523
501
  ): (scalars: bigint[]) => P {
@@ -556,6 +534,7 @@ export function precomputeMSMUnsafe<P extends CurvePoint<any, P>, PC extends Cur
556
534
  * - Optimal for ~256 scalars
557
535
  * - Less efficient for 4096+ scalars (Pippenger preferred)
558
536
  */
537
+ const fieldN = c.Fn;
559
538
  validateW(windowSize, fieldN.BITS);
560
539
  validateMSMPoints(points, c);
561
540
  const zero = c.ZERO;
@@ -590,58 +569,6 @@ export function precomputeMSMUnsafe<P extends CurvePoint<any, P>, PC extends Cur
590
569
  };
591
570
  }
592
571
 
593
- // TODO: remove
594
- /**
595
- * Generic BasicCurve interface: works even for polynomial fields (BLS): P, n, h would be ok.
596
- * Though generator can be different (Fp2 / Fp6 for BLS).
597
- */
598
- export type BasicCurve<T> = {
599
- Fp: IField<T>; // Field over which we'll do calculations (Fp)
600
- n: bigint; // Curve order, total count of valid points in the field
601
- nBitLength?: number; // bit length of curve order
602
- nByteLength?: number; // byte length of curve order
603
- h: bigint; // cofactor. we can assign default=1, but users will just ignore it w/o validation
604
- hEff?: bigint; // Number to multiply to clear cofactor
605
- Gx: T; // base point X coordinate
606
- Gy: T; // base point Y coordinate
607
- allowInfinityPoint?: boolean; // bls12-381 requires it. ZERO point is valid, but invalid pubkey
608
- };
609
-
610
- // TODO: remove
611
- /** @deprecated */
612
- export function validateBasic<FP, T>(
613
- curve: BasicCurve<FP> & T
614
- ): Readonly<
615
- {
616
- readonly nBitLength: number;
617
- readonly nByteLength: number;
618
- } & BasicCurve<FP> &
619
- T & {
620
- p: bigint;
621
- }
622
- > {
623
- validateField(curve.Fp);
624
- validateObject(
625
- curve,
626
- {
627
- n: 'bigint',
628
- h: 'bigint',
629
- Gx: 'field',
630
- Gy: 'field',
631
- },
632
- {
633
- nBitLength: 'isSafeInteger',
634
- nByteLength: 'isSafeInteger',
635
- }
636
- );
637
- // Set defaults
638
- return Object.freeze({
639
- ...nLength(curve.n, curve.nBitLength),
640
- ...curve,
641
- ...{ p: curve.Fp.ORDER },
642
- } as const);
643
- }
644
-
645
572
  export type ValidCurveParams<T> = {
646
573
  p: bigint;
647
574
  n: bigint;
@@ -665,7 +592,7 @@ function createField<T>(order: bigint, field?: IField<T>, isLE?: boolean): IFiel
665
592
  export type FpFn<T> = { Fp: IField<T>; Fn: IField<bigint> };
666
593
 
667
594
  /** Validates CURVE opts and creates fields */
668
- export function _createCurveFields<T>(
595
+ export function createCurveFields<T>(
669
596
  type: 'weierstrass' | 'edwards',
670
597
  CURVE: ValidCurveParams<T>,
671
598
  curveOpts: Partial<FpFn<T>> = {},
@@ -690,3 +617,17 @@ export function _createCurveFields<T>(
690
617
  CURVE = Object.freeze(Object.assign({}, CURVE));
691
618
  return { CURVE, Fp, Fn };
692
619
  }
620
+
621
+ type KeygenFn = (
622
+ seed?: Uint8Array,
623
+ isCompressed?: boolean
624
+ ) => { secretKey: Uint8Array; publicKey: Uint8Array };
625
+ export function createKeygen(
626
+ randomSecretKey: Function,
627
+ getPublicKey: Signer['getPublicKey']
628
+ ): KeygenFn {
629
+ return function keygen(seed?: Uint8Array) {
630
+ const secretKey = randomSecretKey(seed);
631
+ return { secretKey, publicKey: getPublicKey(secretKey) };
632
+ };
633
+ }
@@ -6,41 +6,38 @@
6
6
  */
7
7
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
8
8
  import {
9
- _validateObject,
10
- _abool2 as abool,
11
- _abytes2 as abytes,
9
+ abool,
10
+ abytes,
12
11
  aInRange,
13
12
  bytesToHex,
14
13
  bytesToNumberLE,
15
14
  concatBytes,
16
15
  copyBytes,
17
- ensureBytes,
16
+ hexToBytes,
18
17
  isBytes,
19
18
  memoized,
20
19
  notImplemented,
21
- randomBytes as randomBytesWeb,
20
+ validateObject,
21
+ randomBytes as wcRandomBytes,
22
22
  type FHash,
23
- type Hex,
23
+ type Signer,
24
24
  } from '../utils.ts';
25
25
  import {
26
- _createCurveFields,
26
+ createCurveFields,
27
+ createKeygen,
27
28
  normalizeZ,
28
- pippenger,
29
29
  wNAF,
30
30
  type AffinePoint,
31
- type BasicCurve,
32
31
  type CurveLengths,
33
32
  type CurvePoint,
34
33
  type CurvePointCons,
35
34
  } from './curve.ts';
36
- import { Field, type IField, type NLength } from './modular.ts';
35
+ import { type IField } from './modular.ts';
37
36
 
38
37
  // Be friendly to bad ECMAScript parsers by not using bigint literals
39
38
  // prettier-ignore
40
39
  const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _8n = BigInt(8);
41
40
 
42
- export type UVRatio = (u: bigint, v: bigint) => { isValid: boolean; value: bigint };
43
-
44
41
  /** Instance of Extended Point with coordinates in X, Y, Z, T. */
45
42
  export interface EdwardsPoint extends CurvePoint<bigint, EdwardsPoint> {
46
43
  /** extended X coordinate. Different from affine x. */
@@ -51,33 +48,14 @@ export interface EdwardsPoint extends CurvePoint<bigint, EdwardsPoint> {
51
48
  readonly Z: bigint;
52
49
  /** extended T coordinate */
53
50
  readonly T: bigint;
54
-
55
- /** @deprecated use `toBytes` */
56
- toRawBytes(): Uint8Array;
57
- /** @deprecated use `p.precompute(windowSize)` */
58
- _setWindowSize(windowSize: number): void;
59
- /** @deprecated use .X */
60
- readonly ex: bigint;
61
- /** @deprecated use .Y */
62
- readonly ey: bigint;
63
- /** @deprecated use .Z */
64
- readonly ez: bigint;
65
- /** @deprecated use .T */
66
- readonly et: bigint;
67
51
  }
68
52
  /** Static methods of Extended Point with coordinates in X, Y, Z, T. */
69
53
  export interface EdwardsPointCons extends CurvePointCons<EdwardsPoint> {
70
54
  new (X: bigint, Y: bigint, Z: bigint, T: bigint): EdwardsPoint;
71
55
  CURVE(): EdwardsOpts;
72
56
  fromBytes(bytes: Uint8Array, zip215?: boolean): EdwardsPoint;
73
- fromHex(hex: Hex, zip215?: boolean): EdwardsPoint;
74
- /** @deprecated use `import { pippenger } from '@noble/curves/abstract/curve.js';` */
75
- msm(points: EdwardsPoint[], scalars: bigint[]): EdwardsPoint;
57
+ fromHex(hex: string, zip215?: boolean): EdwardsPoint;
76
58
  }
77
- /** @deprecated use EdwardsPoint */
78
- export type ExtPointType = EdwardsPoint;
79
- /** @deprecated use EdwardsPointCons */
80
- export type ExtPointConstructor = EdwardsPointCons;
81
59
 
82
60
  /**
83
61
  * Twisted Edwards curve options.
@@ -139,13 +117,17 @@ export type EdDSAOpts = Partial<{
139
117
  */
140
118
  export interface EdDSA {
141
119
  keygen: (seed?: Uint8Array) => { secretKey: Uint8Array; publicKey: Uint8Array };
142
- getPublicKey: (secretKey: Hex) => Uint8Array;
143
- sign: (message: Hex, secretKey: Hex, options?: { context?: Hex }) => Uint8Array;
120
+ getPublicKey: (secretKey: Uint8Array) => Uint8Array;
121
+ sign: (
122
+ message: Uint8Array,
123
+ secretKey: Uint8Array,
124
+ options?: { context?: Uint8Array }
125
+ ) => Uint8Array;
144
126
  verify: (
145
- sig: Hex,
146
- message: Hex,
147
- publicKey: Hex,
148
- options?: { context?: Hex; zip215: boolean }
127
+ sig: Uint8Array,
128
+ message: Uint8Array,
129
+ publicKey: Uint8Array,
130
+ options?: { context?: Uint8Array; zip215: boolean }
149
131
  ) => boolean;
150
132
  Point: EdwardsPointCons;
151
133
  utils: {
@@ -163,9 +145,10 @@ export interface EdDSA {
163
145
  *
164
146
  * @example
165
147
  * ```js
166
- * const someonesPub = ed25519.getPublicKey(ed25519.utils.randomSecretKey());
148
+ * const someonesPub_ed = ed25519.getPublicKey(ed25519.utils.randomSecretKey());
149
+ * const someonesPub = ed25519.utils.toMontgomery(someonesPub);
167
150
  * const aPriv = x25519.utils.randomSecretKey();
168
- * x25519.getSharedSecret(aPriv, ed25519.utils.toMontgomery(someonesPub))
151
+ * const shared = x25519.getSharedSecret(aPriv, someonesPub)
169
152
  * ```
170
153
  */
171
154
  toMontgomery: (publicKey: Uint8Array) => Uint8Array;
@@ -174,23 +157,19 @@ export interface EdDSA {
174
157
  * @example
175
158
  * ```js
176
159
  * const someonesPub = x25519.getPublicKey(x25519.utils.randomSecretKey());
177
- * const aPriv = ed25519.utils.randomSecretKey();
178
- * x25519.getSharedSecret(ed25519.utils.toMontgomerySecret(aPriv), someonesPub)
160
+ * const aPriv_ed = ed25519.utils.randomSecretKey();
161
+ * const aPriv = ed25519.utils.toMontgomerySecret(aPriv_ed);
162
+ * const shared = x25519.getSharedSecret(aPriv, someonesPub)
179
163
  * ```
180
164
  */
181
- toMontgomerySecret: (privateKey: Uint8Array) => Uint8Array;
182
- getExtendedPublicKey: (key: Hex) => {
165
+ toMontgomerySecret: (secretKey: Uint8Array) => Uint8Array;
166
+ getExtendedPublicKey: (key: Uint8Array) => {
183
167
  head: Uint8Array;
184
168
  prefix: Uint8Array;
185
169
  scalar: bigint;
186
170
  point: EdwardsPoint;
187
171
  pointBytes: Uint8Array;
188
172
  };
189
-
190
- /** @deprecated use `randomSecretKey` */
191
- randomPrivateKey: (seed?: Uint8Array) => Uint8Array;
192
- /** @deprecated use `point.precompute()` */
193
- precompute: (windowSize?: number, point?: EdwardsPoint) => EdwardsPoint;
194
173
  };
195
174
  lengths: CurveLengths;
196
175
  }
@@ -204,11 +183,11 @@ function isEdValidXY(Fp: IField<bigint>, CURVE: EdwardsOpts, x: bigint, y: bigin
204
183
  }
205
184
 
206
185
  export function edwards(params: EdwardsOpts, extraOpts: EdwardsExtraOpts = {}): EdwardsPointCons {
207
- const validated = _createCurveFields('edwards', params, extraOpts, extraOpts.FpFnLE);
186
+ const validated = createCurveFields('edwards', params, extraOpts, extraOpts.FpFnLE);
208
187
  const { Fp, Fn } = validated;
209
188
  let CURVE = validated.CURVE as EdwardsOpts;
210
189
  const { h: cofactor } = CURVE;
211
- _validateObject(extraOpts, {}, { uvRatio: 'function' });
190
+ validateObject(extraOpts, {}, { uvRatio: 'function' });
212
191
 
213
192
  // Important:
214
193
  // There are some places where Fp.BYTES is used instead of nByteLength.
@@ -243,8 +222,8 @@ export function edwards(params: EdwardsOpts, extraOpts: EdwardsExtraOpts = {}):
243
222
  return n;
244
223
  }
245
224
 
246
- function aextpoint(other: unknown) {
247
- if (!(other instanceof Point)) throw new Error('ExtendedPoint expected');
225
+ function aedpoint(other: unknown) {
226
+ if (!(other instanceof Point)) throw new Error('EdwardsPoint expected');
248
227
  }
249
228
  // Converts Extended point to default (x, y) coordinates.
250
229
  // Can accept precomputed Z^-1 - for example, from invertBatch.
@@ -350,8 +329,9 @@ export function edwards(params: EdwardsOpts, extraOpts: EdwardsExtraOpts = {}):
350
329
  if (isLastByteOdd !== isXOdd) x = modP(-x); // if x_0 != x mod 2, set x = p-x
351
330
  return Point.fromAffine({ x, y });
352
331
  }
353
- static fromHex(bytes: Uint8Array, zip215 = false): Point {
354
- return Point.fromBytes(ensureBytes('point', bytes), zip215);
332
+
333
+ static fromHex(hex: string, zip215 = false): Point {
334
+ return Point.fromBytes(hexToBytes(hex), zip215);
355
335
  }
356
336
 
357
337
  get x(): bigint {
@@ -374,7 +354,7 @@ export function edwards(params: EdwardsOpts, extraOpts: EdwardsExtraOpts = {}):
374
354
 
375
355
  // Compare one point to another.
376
356
  equals(other: Point): boolean {
377
- aextpoint(other);
357
+ aedpoint(other);
378
358
  const { X: X1, Y: Y1, Z: Z1 } = this;
379
359
  const { X: X2, Y: Y2, Z: Z2 } = other;
380
360
  const X1Z2 = modP(X1 * Z2);
@@ -419,7 +399,7 @@ export function edwards(params: EdwardsOpts, extraOpts: EdwardsExtraOpts = {}):
419
399
  // https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
420
400
  // Cost: 9M + 1*a + 1*d + 7add.
421
401
  add(other: Point) {
422
- aextpoint(other);
402
+ aedpoint(other);
423
403
  const { a, d } = CURVE;
424
404
  const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
425
405
  const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
@@ -504,32 +484,6 @@ export function edwards(params: EdwardsOpts, extraOpts: EdwardsExtraOpts = {}):
504
484
  toString() {
505
485
  return `<Point ${this.is0() ? 'ZERO' : this.toHex()}>`;
506
486
  }
507
-
508
- // TODO: remove
509
- get ex(): bigint {
510
- return this.X;
511
- }
512
- get ey(): bigint {
513
- return this.Y;
514
- }
515
- get ez(): bigint {
516
- return this.Z;
517
- }
518
- get et(): bigint {
519
- return this.T;
520
- }
521
- static normalizeZ(points: Point[]): Point[] {
522
- return normalizeZ(Point, points);
523
- }
524
- static msm(points: Point[], scalars: bigint[]): Point {
525
- return pippenger(Point, Fn, points, scalars);
526
- }
527
- _setWindowSize(windowSize: number) {
528
- this.precompute(windowSize);
529
- }
530
- toRawBytes(): Uint8Array {
531
- return this.toBytes();
532
- }
533
487
  }
534
488
  const wnaf = new wNAF(Point, Fn.BITS);
535
489
  Point.BASE.precompute(8); // Enable precomputes. Slows down first publicKey computation by 20ms.
@@ -564,7 +518,7 @@ export abstract class PrimeEdwardsPoint<T extends PrimeEdwardsPoint<T>>
564
518
  notImplemented();
565
519
  }
566
520
 
567
- static fromHex(_hex: Hex): any {
521
+ static fromHex(_hex: string): any {
568
522
  notImplemented();
569
523
  }
570
524
 
@@ -639,11 +593,6 @@ export abstract class PrimeEdwardsPoint<T extends PrimeEdwardsPoint<T>>
639
593
  abstract is0(): boolean;
640
594
  protected abstract assertSame(other: T): void;
641
595
  protected abstract init(ep: EdwardsPoint): T;
642
-
643
- /** @deprecated use `toBytes` */
644
- toRawBytes(): Uint8Array {
645
- return this.toBytes();
646
- }
647
596
  }
648
597
 
649
598
  /**
@@ -651,7 +600,7 @@ export abstract class PrimeEdwardsPoint<T extends PrimeEdwardsPoint<T>>
651
600
  */
652
601
  export function eddsa(Point: EdwardsPointCons, cHash: FHash, eddsaOpts: EdDSAOpts = {}): EdDSA {
653
602
  if (typeof cHash !== 'function') throw new Error('"hash" function param is required');
654
- _validateObject(
603
+ validateObject(
655
604
  eddsaOpts,
656
605
  {},
657
606
  {
@@ -666,7 +615,7 @@ export function eddsa(Point: EdwardsPointCons, cHash: FHash, eddsaOpts: EdDSAOpt
666
615
  const { prehash } = eddsaOpts;
667
616
  const { BASE, Fp, Fn } = Point;
668
617
 
669
- const randomBytes = eddsaOpts.randomBytes || randomBytesWeb;
618
+ const randomBytes = eddsaOpts.randomBytes || wcRandomBytes;
670
619
  const adjustScalarBytes = eddsaOpts.adjustScalarBytes || ((bytes: Uint8Array) => bytes);
671
620
  const domain =
672
621
  eddsaOpts.domain ||
@@ -682,12 +631,12 @@ export function eddsa(Point: EdwardsPointCons, cHash: FHash, eddsaOpts: EdDSAOpt
682
631
  }
683
632
 
684
633
  // Get the hashed private scalar per RFC8032 5.1.5
685
- function getPrivateScalar(key: Hex) {
634
+ function getPrivateScalar(key: Uint8Array) {
686
635
  const len = lengths.secretKey;
687
- key = ensureBytes('private key', key, len);
636
+ abytes(key, lengths.secretKey, 'secretKey');
688
637
  // Hash private key with curve's hash function to produce uniformingly random input
689
638
  // Check byte lengths: ensure(64, h(ensure(32, key)))
690
- const hashed = ensureBytes('hashed private key', cHash(key), 2 * len);
639
+ const hashed = abytes(cHash(key), 2 * len, 'hashedSecretKey');
691
640
  const head = adjustScalarBytes(hashed.slice(0, len)); // clear first half bits, produce FE
692
641
  const prefix = hashed.slice(len, 2 * len); // second half is called key prefix (5.1.6)
693
642
  const scalar = modN_LE(head); // The actual private scalar
@@ -695,7 +644,7 @@ export function eddsa(Point: EdwardsPointCons, cHash: FHash, eddsaOpts: EdDSAOpt
695
644
  }
696
645
 
697
646
  /** Convenience method that creates public key from scalar. RFC8032 5.1.5 */
698
- function getExtendedPublicKey(secretKey: Hex) {
647
+ function getExtendedPublicKey(secretKey: Uint8Array) {
699
648
  const { head, prefix, scalar } = getPrivateScalar(secretKey);
700
649
  const point = BASE.multiply(scalar); // Point on Edwards curve aka public key
701
650
  const pointBytes = point.toBytes();
@@ -703,19 +652,23 @@ export function eddsa(Point: EdwardsPointCons, cHash: FHash, eddsaOpts: EdDSAOpt
703
652
  }
704
653
 
705
654
  /** Calculates EdDSA pub key. RFC8032 5.1.5. */
706
- function getPublicKey(secretKey: Hex): Uint8Array {
655
+ function getPublicKey(secretKey: Uint8Array): Uint8Array {
707
656
  return getExtendedPublicKey(secretKey).pointBytes;
708
657
  }
709
658
 
710
659
  // int('LE', SHA512(dom2(F, C) || msgs)) mod N
711
- function hashDomainToScalar(context: Hex = Uint8Array.of(), ...msgs: Uint8Array[]) {
660
+ function hashDomainToScalar(context: Uint8Array = Uint8Array.of(), ...msgs: Uint8Array[]) {
712
661
  const msg = concatBytes(...msgs);
713
- return modN_LE(cHash(domain(msg, ensureBytes('context', context), !!prehash)));
662
+ return modN_LE(cHash(domain(msg, abytes(context, undefined, 'context'), !!prehash)));
714
663
  }
715
664
 
716
- /** Signs message with privateKey. RFC8032 5.1.6 */
717
- function sign(msg: Hex, secretKey: Hex, options: { context?: Hex } = {}): Uint8Array {
718
- msg = ensureBytes('message', msg);
665
+ /** Signs message with secret key. RFC8032 5.1.6 */
666
+ function sign(
667
+ msg: Uint8Array,
668
+ secretKey: Uint8Array,
669
+ options: { context?: Uint8Array } = {}
670
+ ): Uint8Array {
671
+ msg = abytes(msg, undefined, 'message');
719
672
  if (prehash) msg = prehash(msg); // for ed25519ph etc.
720
673
  const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
721
674
  const r = hashDomainToScalar(options.context, prefix, msg); // r = dom2(F, C) || prefix || PH(M)
@@ -728,18 +681,23 @@ export function eddsa(Point: EdwardsPointCons, cHash: FHash, eddsaOpts: EdDSAOpt
728
681
  }
729
682
 
730
683
  // verification rule is either zip215 or rfc8032 / nist186-5. Consult fromHex:
731
- const verifyOpts: { context?: Hex; zip215?: boolean } = { zip215: true };
684
+ const verifyOpts: { context?: Uint8Array; zip215?: boolean } = { zip215: true };
732
685
 
733
686
  /**
734
687
  * Verifies EdDSA signature against message and public key. RFC8032 5.1.7.
735
688
  * An extended group equation is checked.
736
689
  */
737
- function verify(sig: Hex, msg: Hex, publicKey: Hex, options = verifyOpts): boolean {
690
+ function verify(
691
+ sig: Uint8Array,
692
+ msg: Uint8Array,
693
+ publicKey: Uint8Array,
694
+ options = verifyOpts
695
+ ): boolean {
738
696
  const { context, zip215 } = options;
739
697
  const len = lengths.signature;
740
- sig = ensureBytes('signature', sig, len);
741
- msg = ensureBytes('message', msg);
742
- publicKey = ensureBytes('publicKey', publicKey, lengths.publicKey);
698
+ sig = abytes(sig, len, 'signature');
699
+ msg = abytes(msg, undefined, 'message');
700
+ publicKey = abytes(publicKey, lengths.publicKey, 'publicKey');
743
701
  if (zip215 !== undefined) abool(zip215, 'zip215');
744
702
  if (prehash) msg = prehash(msg); // for ed25519ph, etc
745
703
 
@@ -776,13 +734,11 @@ export function eddsa(Point: EdwardsPointCons, cHash: FHash, eddsaOpts: EdDSAOpt
776
734
  function randomSecretKey(seed = randomBytes(lengths.seed)): Uint8Array {
777
735
  return abytes(seed, lengths.seed, 'seed');
778
736
  }
779
- function keygen(seed?: Uint8Array) {
780
- const secretKey = utils.randomSecretKey(seed);
781
- return { secretKey, publicKey: getPublicKey(secretKey) };
782
- }
737
+
783
738
  function isValidSecretKey(key: Uint8Array): boolean {
784
739
  return isBytes(key) && key.length === Fn.BYTES;
785
740
  }
741
+
786
742
  function isValidPublicKey(key: Uint8Array, zip215?: boolean): boolean {
787
743
  try {
788
744
  return !!Point.fromBytes(key, zip215);
@@ -813,102 +769,21 @@ export function eddsa(Point: EdwardsPointCons, cHash: FHash, eddsaOpts: EdDSAOpt
813
769
  const u = is25519 ? Fp.div(_1n + y, _1n - y) : Fp.div(y - _1n, y + _1n);
814
770
  return Fp.toBytes(u);
815
771
  },
816
-
817
772
  toMontgomerySecret(secretKey: Uint8Array): Uint8Array {
818
773
  const size = lengths.secretKey;
819
774
  abytes(secretKey, size);
820
775
  const hashed = cHash(secretKey.subarray(0, size));
821
776
  return adjustScalarBytes(hashed).subarray(0, size);
822
777
  },
823
-
824
- /** @deprecated */
825
- randomPrivateKey: randomSecretKey,
826
- /** @deprecated */
827
- precompute(windowSize = 8, point: EdwardsPoint = Point.BASE): EdwardsPoint {
828
- return point.precompute(windowSize, false);
829
- },
830
778
  };
831
779
 
832
780
  return Object.freeze({
833
- keygen,
781
+ keygen: createKeygen(randomSecretKey, getPublicKey),
834
782
  getPublicKey,
835
783
  sign,
836
784
  verify,
837
785
  utils,
838
786
  Point,
839
787
  lengths,
840
- });
841
- }
842
-
843
- // TODO: remove everything below
844
- export type CurveType = BasicCurve<bigint> & {
845
- a: bigint; // curve param a
846
- d: bigint; // curve param d
847
- /** @deprecated the property will be removed in next release */
848
- hash: FHash; // Hashing
849
- randomBytes?: (bytesLength?: number) => Uint8Array; // CSPRNG
850
- adjustScalarBytes?: (bytes: Uint8Array) => Uint8Array; // clears bits to get valid field elemtn
851
- domain?: (data: Uint8Array, ctx: Uint8Array, phflag: boolean) => Uint8Array; // Used for hashing
852
- uvRatio?: UVRatio; // Ratio √(u/v)
853
- prehash?: FHash; // RFC 8032 pre-hashing of messages to sign() / verify()
854
- mapToCurve?: (scalar: bigint[]) => AffinePoint<bigint>; // for hash-to-curve standard
855
- };
856
- export type CurveTypeWithLength = Readonly<CurveType & Partial<NLength>>;
857
- export type CurveFn = {
858
- /** @deprecated the property will be removed in next release */
859
- CURVE: CurveType;
860
- keygen: EdDSA['keygen'];
861
- getPublicKey: EdDSA['getPublicKey'];
862
- sign: EdDSA['sign'];
863
- verify: EdDSA['verify'];
864
- Point: EdwardsPointCons;
865
- /** @deprecated use `Point` */
866
- ExtendedPoint: EdwardsPointCons;
867
- utils: EdDSA['utils'];
868
- lengths: CurveLengths;
869
- };
870
- export type EdComposed = {
871
- CURVE: EdwardsOpts;
872
- curveOpts: EdwardsExtraOpts;
873
- hash: FHash;
874
- eddsaOpts: EdDSAOpts;
875
- };
876
- function _eddsa_legacy_opts_to_new(c: CurveTypeWithLength): EdComposed {
877
- const CURVE: EdwardsOpts = {
878
- a: c.a,
879
- d: c.d,
880
- p: c.Fp.ORDER,
881
- n: c.n,
882
- h: c.h,
883
- Gx: c.Gx,
884
- Gy: c.Gy,
885
- };
886
- const Fp = c.Fp;
887
- const Fn = Field(CURVE.n, c.nBitLength, true);
888
- const curveOpts: EdwardsExtraOpts = { Fp, Fn, uvRatio: c.uvRatio };
889
- const eddsaOpts: EdDSAOpts = {
890
- randomBytes: c.randomBytes,
891
- adjustScalarBytes: c.adjustScalarBytes,
892
- domain: c.domain,
893
- prehash: c.prehash,
894
- mapToCurve: c.mapToCurve,
895
- };
896
- return { CURVE, curveOpts, hash: c.hash, eddsaOpts };
897
- }
898
- function _eddsa_new_output_to_legacy(c: CurveTypeWithLength, eddsa: EdDSA): CurveFn {
899
- const Point = eddsa.Point;
900
- const legacy = Object.assign({}, eddsa, {
901
- ExtendedPoint: Point,
902
- CURVE: c,
903
- nBitLength: Point.Fn.BITS,
904
- nByteLength: Point.Fn.BYTES,
905
- });
906
- return legacy;
907
- }
908
- // TODO: remove. Use eddsa
909
- export function twistedEdwards(c: CurveTypeWithLength): CurveFn {
910
- const { CURVE, curveOpts, hash, eddsaOpts } = _eddsa_legacy_opts_to_new(c);
911
- const Point = edwards(CURVE, curveOpts);
912
- const EDDSA = eddsa(Point, hash, eddsaOpts);
913
- return _eddsa_new_output_to_legacy(c, EDDSA);
788
+ }) satisfies Signer;
914
789
  }