@noble/curves 0.4.0 → 0.5.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 (72) hide show
  1. package/README.md +203 -162
  2. package/lib/_shortw_utils.d.ts +75 -0
  3. package/lib/_shortw_utils.js +20 -0
  4. package/lib/{bls.d.ts → abstract/bls.d.ts} +2 -1
  5. package/lib/{bls.js → abstract/bls.js} +28 -27
  6. package/lib/{edwards.d.ts → abstract/edwards.d.ts} +17 -0
  7. package/lib/{edwards.js → abstract/edwards.js} +45 -4
  8. package/lib/{group.d.ts → abstract/group.d.ts} +2 -1
  9. package/lib/{group.js → abstract/group.js} +4 -3
  10. package/lib/{hashToCurve.d.ts → abstract/hash-to-curve.d.ts} +6 -0
  11. package/lib/{hashToCurve.js → abstract/hash-to-curve.js} +15 -2
  12. package/lib/{modular.d.ts → abstract/modular.d.ts} +10 -4
  13. package/lib/{modular.js → abstract/modular.js} +110 -19
  14. package/lib/{montgomery.d.ts → abstract/montgomery.d.ts} +2 -1
  15. package/lib/{montgomery.js → abstract/montgomery.js} +17 -8
  16. package/lib/{utils.d.ts → abstract/utils.d.ts} +1 -1
  17. package/lib/{utils.js → abstract/utils.js} +1 -1
  18. package/lib/{weierstrass.d.ts → abstract/weierstrass.d.ts} +28 -16
  19. package/lib/{weierstrass.js → abstract/weierstrass.js} +261 -127
  20. package/lib/bls12-381.d.ts +66 -0
  21. package/lib/bls12-381.js +1132 -0
  22. package/lib/bn.d.ts +7 -0
  23. package/lib/bn.js +24 -0
  24. package/lib/ed25519.d.ts +48 -0
  25. package/lib/ed25519.js +322 -0
  26. package/lib/ed448.d.ts +3 -0
  27. package/lib/ed448.js +128 -0
  28. package/lib/esm/_shortw_utils.js +15 -0
  29. package/lib/esm/{bls.js → abstract/bls.js} +25 -24
  30. package/lib/esm/{edwards.js → abstract/edwards.js} +45 -4
  31. package/lib/esm/{group.js → abstract/group.js} +4 -3
  32. package/lib/esm/{hashToCurve.js → abstract/hash-to-curve.js} +13 -1
  33. package/lib/esm/{modular.js → abstract/modular.js} +108 -18
  34. package/lib/esm/{montgomery.js → abstract/montgomery.js} +17 -8
  35. package/lib/esm/{utils.js → abstract/utils.js} +1 -1
  36. package/lib/esm/{weierstrass.js → abstract/weierstrass.js} +255 -123
  37. package/lib/esm/bls12-381.js +1129 -0
  38. package/lib/esm/bn.js +21 -0
  39. package/lib/esm/ed25519.js +318 -0
  40. package/lib/esm/ed448.js +125 -0
  41. package/lib/esm/index.js +2 -0
  42. package/lib/esm/jubjub.js +52 -0
  43. package/lib/esm/p192.js +21 -0
  44. package/lib/esm/p224.js +21 -0
  45. package/lib/esm/p256.js +39 -0
  46. package/lib/esm/p384.js +44 -0
  47. package/lib/esm/p521.js +58 -0
  48. package/lib/esm/pasta.js +29 -0
  49. package/lib/esm/secp256k1.js +290 -0
  50. package/lib/esm/stark.js +222 -0
  51. package/lib/index.d.ts +0 -0
  52. package/lib/index.js +2 -0
  53. package/lib/jubjub.d.ts +7 -0
  54. package/lib/jubjub.js +57 -0
  55. package/lib/p192.d.ts +130 -0
  56. package/lib/p192.js +24 -0
  57. package/lib/p224.d.ts +130 -0
  58. package/lib/p224.js +24 -0
  59. package/lib/p256.d.ts +130 -0
  60. package/lib/p256.js +42 -0
  61. package/lib/p384.d.ts +130 -0
  62. package/lib/p384.js +47 -0
  63. package/lib/p521.d.ts +131 -0
  64. package/lib/p521.js +61 -0
  65. package/lib/pasta.d.ts +4 -0
  66. package/lib/pasta.js +32 -0
  67. package/lib/secp256k1.d.ts +96 -0
  68. package/lib/secp256k1.js +294 -0
  69. package/lib/stark.d.ts +72 -0
  70. package/lib/stark.js +243 -0
  71. package/package.json +146 -50
  72. package/index.js +0 -1
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.montgomery = void 0;
4
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
4
5
  const mod = require("./modular.js");
5
6
  const utils_js_1 = require("./utils.js");
6
7
  const _0n = BigInt(0);
@@ -162,8 +163,14 @@ function montgomery(curveDef) {
162
163
  throw new Error(`Expected ${montgomeryBytes} or ${fieldLen} bytes, got ${bytes.length}`);
163
164
  return (0, utils_js_1.bytesToNumberLE)(adjustScalarBytes(bytes));
164
165
  }
165
- // Multiply point u by scalar
166
- function scalarMult(u, scalar) {
166
+ /**
167
+ * Computes shared secret between private key "scalar" and public key's "u" (x) coordinate.
168
+ * We can get 'y' coordinate from 'u',
169
+ * but Point.fromHex also wants 'x' coordinate oddity flag,
170
+ * and we cannot get 'x' without knowing 'v'.
171
+ * Need to add generic conversion between twisted edwards and complimentary curve for JubJub.
172
+ */
173
+ function scalarMult(scalar, u) {
167
174
  const pointU = decodeUCoordinate(u);
168
175
  const _scalar = decodeScalar(scalar);
169
176
  const pu = montgomeryLadder(pointU, _scalar);
@@ -173,17 +180,19 @@ function montgomery(curveDef) {
173
180
  throw new Error('Invalid private or public key received');
174
181
  return encodeUCoordinate(pu);
175
182
  }
176
- // Multiply base point by scalar
183
+ /**
184
+ * Computes public key from private.
185
+ * Executes scalar multiplication of curve's base point by scalar.
186
+ * @param scalar private key
187
+ * @returns new public key
188
+ */
177
189
  function scalarMultBase(scalar) {
178
- return scalarMult(CURVE.Gu, scalar);
190
+ return scalarMult(scalar, CURVE.Gu);
179
191
  }
180
192
  return {
181
- // NOTE: we can get 'y' coordinate from 'u', but Point.fromHex also wants 'x' coordinate oddity flag, and we cannot get 'x' without knowing 'v'
182
- // Need to add generic conversion between twisted edwards and complimentary curve for JubJub
183
193
  scalarMult,
184
194
  scalarMultBase,
185
- // NOTE: these function work on complimentary montgomery curve
186
- // getSharedSecret: (privateKey: Hex, publicKey: Hex) => scalarMult(publicKey, privateKey),
195
+ getSharedSecret: (privateKey, publicKey) => scalarMult(privateKey, publicKey),
187
196
  getPublicKey: (privateKey) => scalarMultBase(privateKey),
188
197
  Gu: CURVE.Gu,
189
198
  };
@@ -1,4 +1,4 @@
1
- /*! @noble/curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
2
  import * as mod from './modular.js';
3
3
  export declare type Hex = Uint8Array | string;
4
4
  export declare type PrivKey = Hex | bigint | number;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.bitMask = exports.bitSet = exports.bitGet = exports.bitLen = exports.equalBytes = exports.hashToPrivateScalar = exports.nLength = exports.concatBytes = exports.ensureBytes = exports.numberToBytesLE = exports.numberToBytesBE = exports.bytesToNumberLE = exports.bytesToNumberBE = exports.hexToBytes = exports.hexToNumber = exports.numberToHexUnpadded = exports.bytesToHex = exports.validateOpts = void 0;
4
- /*! @noble/curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
4
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
5
5
  const mod = require("./modular.js");
6
6
  const _0n = BigInt(0);
7
7
  const _1n = BigInt(1);
@@ -2,7 +2,7 @@
2
2
  import * as mod from './modular.js';
3
3
  import { Hex, PrivKey } from './utils.js';
4
4
  import * as utils from './utils.js';
5
- import { htfOpts } from './hashToCurve.js';
5
+ import { htfOpts } from './hash-to-curve.js';
6
6
  import { Group, GroupConstructor } from './group.js';
7
7
  declare type HmacFnSync = (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
8
8
  declare type EndomorphismOpts = {
@@ -19,8 +19,8 @@ export declare type BasicCurve<T> = utils.BasicCurve<T> & {
19
19
  b: T;
20
20
  normalizePrivateKey?: (key: PrivKey) => PrivKey;
21
21
  endo?: EndomorphismOpts;
22
- isTorsionFree?: (c: JacobianConstructor<T>, point: JacobianPointType<T>) => boolean;
23
- clearCofactor?: (c: JacobianConstructor<T>, point: JacobianPointType<T>) => JacobianPointType<T>;
22
+ isTorsionFree?: (c: ProjectiveConstructor<T>, point: ProjectivePointType<T>) => boolean;
23
+ clearCofactor?: (c: ProjectiveConstructor<T>, point: ProjectivePointType<T>) => ProjectivePointType<T>;
24
24
  htfDefaults?: htfOpts;
25
25
  mapToCurve?: (scalar: bigint[]) => {
26
26
  x: T;
@@ -53,19 +53,19 @@ declare type SignOpts = {
53
53
  *
54
54
  * TODO: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol
55
55
  */
56
- export interface JacobianPointType<T> extends Group<JacobianPointType<T>> {
56
+ export interface ProjectivePointType<T> extends Group<ProjectivePointType<T>> {
57
57
  readonly x: T;
58
58
  readonly y: T;
59
59
  readonly z: T;
60
- multiply(scalar: number | bigint, affinePoint?: PointType<T>): JacobianPointType<T>;
61
- multiplyUnsafe(scalar: bigint): JacobianPointType<T>;
60
+ multiply(scalar: number | bigint, affinePoint?: PointType<T>): ProjectivePointType<T>;
61
+ multiplyUnsafe(scalar: bigint): ProjectivePointType<T>;
62
62
  toAffine(invZ?: T): PointType<T>;
63
63
  }
64
- export interface JacobianConstructor<T> extends GroupConstructor<JacobianPointType<T>> {
65
- new (x: T, y: T, z: T): JacobianPointType<T>;
66
- fromAffine(p: PointType<T>): JacobianPointType<T>;
67
- toAffineBatch(points: JacobianPointType<T>[]): PointType<T>[];
68
- normalizeZ(points: JacobianPointType<T>[]): JacobianPointType<T>[];
64
+ export interface ProjectiveConstructor<T> extends GroupConstructor<ProjectivePointType<T>> {
65
+ new (x: T, y: T, z: T): ProjectivePointType<T>;
66
+ fromAffine(p: PointType<T>): ProjectivePointType<T>;
67
+ toAffineBatch(points: ProjectivePointType<T>[]): PointType<T>[];
68
+ normalizeZ(points: ProjectivePointType<T>[]): ProjectivePointType<T>[];
69
69
  }
70
70
  export interface PointType<T> extends Group<PointType<T>> {
71
71
  readonly x: T;
@@ -93,14 +93,14 @@ export declare type CurvePointsType<T> = BasicCurve<T> & {
93
93
  };
94
94
  export declare type CurvePointsRes<T> = {
95
95
  Point: PointConstructor<T>;
96
- JacobianPoint: JacobianConstructor<T>;
96
+ ProjectivePoint: ProjectiveConstructor<T>;
97
97
  normalizePrivateKey: (key: PrivKey) => bigint;
98
98
  weierstrassEquation: (x: T) => T;
99
99
  isWithinCurveOrder: (num: bigint) => boolean;
100
100
  };
101
101
  export declare function weierstrassPoints<T>(opts: CurvePointsType<T>): {
102
102
  Point: PointConstructor<T>;
103
- JacobianPoint: JacobianConstructor<T>;
103
+ ProjectivePoint: ProjectiveConstructor<T>;
104
104
  normalizePrivateKey: (key: PrivKey) => bigint;
105
105
  weierstrassEquation: (x: T) => T;
106
106
  isWithinCurveOrder: (num: bigint) => boolean;
@@ -147,8 +147,8 @@ declare function validateOpts(curve: CurveType): Readonly<{
147
147
  readonly b: bigint;
148
148
  readonly normalizePrivateKey?: ((key: PrivKey) => PrivKey) | undefined;
149
149
  readonly endo?: EndomorphismOpts | undefined;
150
- readonly isTorsionFree?: ((c: JacobianConstructor<bigint>, point: JacobianPointType<bigint>) => boolean) | undefined;
151
- readonly clearCofactor?: ((c: JacobianConstructor<bigint>, point: JacobianPointType<bigint>) => JacobianPointType<bigint>) | undefined;
150
+ readonly isTorsionFree?: ((c: ProjectiveConstructor<bigint>, point: ProjectivePointType<bigint>) => boolean) | undefined;
151
+ readonly clearCofactor?: ((c: ProjectiveConstructor<bigint>, point: ProjectivePointType<bigint>) => ProjectivePointType<bigint>) | undefined;
152
152
  readonly htfDefaults?: htfOpts | undefined;
153
153
  readonly mapToCurve?: ((scalar: bigint[]) => {
154
154
  x: bigint;
@@ -169,7 +169,7 @@ export declare type CurveFn = {
169
169
  lowS?: boolean;
170
170
  }) => boolean;
171
171
  Point: PointConstructor<bigint>;
172
- JacobianPoint: JacobianConstructor<bigint>;
172
+ ProjectivePoint: ProjectiveConstructor<bigint>;
173
173
  Signature: SignatureConstructor;
174
174
  utils: {
175
175
  mod: (a: bigint, b?: bigint) => bigint;
@@ -187,4 +187,16 @@ export declare type CurveFn = {
187
187
  };
188
188
  };
189
189
  export declare function weierstrass(curveDef: CurveType): CurveFn;
190
+ export declare function SWUFpSqrtRatio<T>(Fp: mod.Field<T>, Z: T): (u: T, v: T) => {
191
+ isValid: boolean;
192
+ value: T;
193
+ };
194
+ export declare function mapToCurveSimpleSWU<T>(Fp: mod.Field<T>, opts: {
195
+ A: T;
196
+ B: T;
197
+ Z: T;
198
+ }): (u: T) => {
199
+ x: T;
200
+ y: T;
201
+ };
190
202
  export {};