@noble/curves 1.6.0 → 1.8.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.
- package/README.md +78 -30
- package/_shortw_utils.d.ts +8 -54
- package/_shortw_utils.d.ts.map +1 -1
- package/_shortw_utils.js +6 -2
- package/_shortw_utils.js.map +1 -1
- package/abstract/bls.d.ts +15 -11
- package/abstract/bls.d.ts.map +1 -1
- package/abstract/bls.js +26 -10
- package/abstract/bls.js.map +1 -1
- package/abstract/curve.d.ts +42 -24
- package/abstract/curve.d.ts.map +1 -1
- package/abstract/curve.js +203 -53
- package/abstract/curve.js.map +1 -1
- package/abstract/edwards.d.ts +14 -23
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +25 -10
- package/abstract/edwards.js.map +1 -1
- package/abstract/hash-to-curve.d.ts +28 -8
- package/abstract/hash-to-curve.d.ts.map +1 -1
- package/abstract/hash-to-curve.js +18 -14
- package/abstract/hash-to-curve.js.map +1 -1
- package/abstract/modular.d.ts +30 -5
- package/abstract/modular.d.ts.map +1 -1
- package/abstract/modular.js +68 -37
- package/abstract/modular.js.map +1 -1
- package/abstract/montgomery.d.ts.map +1 -1
- package/abstract/montgomery.js +11 -4
- package/abstract/montgomery.js.map +1 -1
- package/abstract/poseidon.d.ts +9 -0
- package/abstract/poseidon.d.ts.map +1 -1
- package/abstract/poseidon.js +31 -23
- package/abstract/poseidon.js.map +1 -1
- package/abstract/tower.d.ts +13 -1
- package/abstract/tower.d.ts.map +1 -1
- package/abstract/tower.js +20 -17
- package/abstract/tower.js.map +1 -1
- package/abstract/utils.d.ts +10 -5
- package/abstract/utils.d.ts.map +1 -1
- package/abstract/utils.js +26 -24
- package/abstract/utils.js.map +1 -1
- package/abstract/weierstrass.d.ts +47 -80
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +88 -43
- package/abstract/weierstrass.js.map +1 -1
- package/bls12-381.d.ts +11 -0
- package/bls12-381.d.ts.map +1 -1
- package/bls12-381.js +80 -67
- package/bls12-381.js.map +1 -1
- package/bn254.d.ts +5 -3
- package/bn254.d.ts.map +1 -1
- package/bn254.js +33 -27
- package/bn254.js.map +1 -1
- package/ed25519.d.ts +24 -4
- package/ed25519.d.ts.map +1 -1
- package/ed25519.js +30 -6
- package/ed25519.js.map +1 -1
- package/ed448.d.ts +23 -8
- package/ed448.d.ts.map +1 -1
- package/ed448.js +31 -9
- package/ed448.js.map +1 -1
- package/esm/_shortw_utils.d.ts +8 -54
- package/esm/_shortw_utils.d.ts.map +1 -1
- package/esm/_shortw_utils.js +6 -2
- package/esm/_shortw_utils.js.map +1 -1
- package/esm/abstract/bls.d.ts +15 -11
- package/esm/abstract/bls.d.ts.map +1 -1
- package/esm/abstract/bls.js +26 -10
- package/esm/abstract/bls.js.map +1 -1
- package/esm/abstract/curve.d.ts +42 -24
- package/esm/abstract/curve.d.ts.map +1 -1
- package/esm/abstract/curve.js +202 -53
- package/esm/abstract/curve.js.map +1 -1
- package/esm/abstract/edwards.d.ts +14 -23
- package/esm/abstract/edwards.d.ts.map +1 -1
- package/esm/abstract/edwards.js +25 -10
- package/esm/abstract/edwards.js.map +1 -1
- package/esm/abstract/hash-to-curve.d.ts +28 -8
- package/esm/abstract/hash-to-curve.d.ts.map +1 -1
- package/esm/abstract/hash-to-curve.js +18 -14
- package/esm/abstract/hash-to-curve.js.map +1 -1
- package/esm/abstract/modular.d.ts +30 -5
- package/esm/abstract/modular.d.ts.map +1 -1
- package/esm/abstract/modular.js +68 -37
- package/esm/abstract/modular.js.map +1 -1
- package/esm/abstract/montgomery.d.ts.map +1 -1
- package/esm/abstract/montgomery.js +11 -4
- package/esm/abstract/montgomery.js.map +1 -1
- package/esm/abstract/poseidon.d.ts +9 -0
- package/esm/abstract/poseidon.d.ts.map +1 -1
- package/esm/abstract/poseidon.js +31 -23
- package/esm/abstract/poseidon.js.map +1 -1
- package/esm/abstract/tower.d.ts +13 -1
- package/esm/abstract/tower.d.ts.map +1 -1
- package/esm/abstract/tower.js +20 -17
- package/esm/abstract/tower.js.map +1 -1
- package/esm/abstract/utils.d.ts +10 -5
- package/esm/abstract/utils.d.ts.map +1 -1
- package/esm/abstract/utils.js +25 -23
- package/esm/abstract/utils.js.map +1 -1
- package/esm/abstract/weierstrass.d.ts +47 -80
- package/esm/abstract/weierstrass.d.ts.map +1 -1
- package/esm/abstract/weierstrass.js +86 -42
- package/esm/abstract/weierstrass.js.map +1 -1
- package/esm/bls12-381.d.ts +11 -0
- package/esm/bls12-381.d.ts.map +1 -1
- package/esm/bls12-381.js +80 -67
- package/esm/bls12-381.js.map +1 -1
- package/esm/bn254.d.ts +5 -3
- package/esm/bn254.d.ts.map +1 -1
- package/esm/bn254.js +31 -26
- package/esm/bn254.js.map +1 -1
- package/esm/ed25519.d.ts +24 -4
- package/esm/ed25519.d.ts.map +1 -1
- package/esm/ed25519.js +31 -7
- package/esm/ed25519.js.map +1 -1
- package/esm/ed448.d.ts +23 -8
- package/esm/ed448.d.ts.map +1 -1
- package/esm/ed448.js +32 -10
- package/esm/ed448.js.map +1 -1
- package/esm/index.js +4 -0
- package/esm/index.js.map +1 -1
- package/esm/jubjub.d.ts +4 -8
- package/esm/jubjub.d.ts.map +1 -1
- package/esm/jubjub.js +6 -5
- package/esm/jubjub.js.map +1 -1
- package/esm/p256.d.ts +10 -104
- package/esm/p256.d.ts.map +1 -1
- package/esm/p256.js +15 -8
- package/esm/p256.js.map +1 -1
- package/esm/p384.d.ts +10 -104
- package/esm/p384.d.ts.map +1 -1
- package/esm/p384.js +15 -8
- package/esm/p384.js.map +1 -1
- package/esm/p521.d.ts +11 -104
- package/esm/p521.d.ts.map +1 -1
- package/esm/p521.js +18 -10
- package/esm/p521.js.map +1 -1
- package/esm/pasta.d.ts +5 -2
- package/esm/pasta.d.ts.map +1 -1
- package/esm/pasta.js +6 -2
- package/esm/pasta.js.map +1 -1
- package/esm/secp256k1.d.ts +29 -57
- package/esm/secp256k1.d.ts.map +1 -1
- package/esm/secp256k1.js +41 -15
- package/esm/secp256k1.js.map +1 -1
- package/index.js +4 -0
- package/index.js.map +1 -1
- package/jubjub.d.ts +4 -8
- package/jubjub.d.ts.map +1 -1
- package/jubjub.js +6 -5
- package/jubjub.js.map +1 -1
- package/p256.d.ts +10 -104
- package/p256.d.ts.map +1 -1
- package/p256.js +15 -8
- package/p256.js.map +1 -1
- package/p384.d.ts +10 -104
- package/p384.d.ts.map +1 -1
- package/p384.js +15 -8
- package/p384.js.map +1 -1
- package/p521.d.ts +11 -104
- package/p521.d.ts.map +1 -1
- package/p521.js +18 -10
- package/p521.js.map +1 -1
- package/package.json +11 -8
- package/pasta.d.ts +5 -2
- package/pasta.d.ts.map +1 -1
- package/pasta.js +6 -2
- package/pasta.js.map +1 -1
- package/secp256k1.d.ts +29 -57
- package/secp256k1.d.ts.map +1 -1
- package/secp256k1.js +41 -15
- package/secp256k1.js.map +1 -1
- package/src/_shortw_utils.ts +18 -8
- package/src/abstract/bls.ts +42 -30
- package/src/abstract/curve.ts +237 -55
- package/src/abstract/edwards.ts +36 -15
- package/src/abstract/hash-to-curve.ts +43 -21
- package/src/abstract/modular.ts +84 -46
- package/src/abstract/montgomery.ts +12 -4
- package/src/abstract/poseidon.ts +48 -30
- package/src/abstract/tower.ts +66 -20
- package/src/abstract/utils.ts +44 -43
- package/src/abstract/weierstrass.ts +125 -70
- package/src/bls12-381.ts +80 -68
- package/src/bn254.ts +47 -30
- package/src/ed25519.ts +50 -20
- package/src/ed448.ts +49 -22
- package/src/index.ts +4 -0
- package/src/jubjub.ts +10 -10
- package/src/p256.ts +21 -15
- package/src/p384.ts +21 -15
- package/src/p521.ts +24 -17
- package/src/pasta.ts +15 -7
- package/src/secp256k1.ts +63 -21
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Short Weierstrass curve methods. The formula is: y² = x³ + ax + b.
|
|
3
|
+
*
|
|
4
|
+
* ### Design rationale for types
|
|
5
|
+
*
|
|
6
|
+
* * Interaction between classes from different curves should fail:
|
|
7
|
+
* `k256.Point.BASE.add(p256.Point.BASE)`
|
|
8
|
+
* * For this purpose we want to use `instanceof` operator, which is fast and works during runtime
|
|
9
|
+
* * Different calls of `curve()` would return different classes -
|
|
10
|
+
* `curve(params) !== curve(params)`: if somebody decided to monkey-patch their curve,
|
|
11
|
+
* it won't affect others
|
|
12
|
+
*
|
|
13
|
+
* TypeScript can't infer types for classes created inside a function. Classes is one instance
|
|
14
|
+
* of nominative types in TypeScript and interfaces only check for shape, so it's hard to create
|
|
15
|
+
* unique type for every function call.
|
|
16
|
+
*
|
|
17
|
+
* We can use generic types via some param, like curve opts, but that would:
|
|
18
|
+
* 1. Enable interaction between `curve(params)` and `curve(params)` (curves of same params)
|
|
19
|
+
* which is hard to debug.
|
|
20
|
+
* 2. Params can be generic and we can't enforce them to be constant value:
|
|
21
|
+
* if somebody creates curve from non-constant params,
|
|
22
|
+
* it would be allowed to interact with other curves with non-constant params
|
|
23
|
+
*
|
|
24
|
+
* @todo https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol
|
|
25
|
+
* @module
|
|
26
|
+
*/
|
|
1
27
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
2
28
|
import { AffinePoint, BasicCurve, Group, GroupConstructor } from './curve.js';
|
|
3
29
|
import * as mod from './modular.js';
|
|
@@ -31,28 +57,8 @@ export type SignOpts = {
|
|
|
31
57
|
export type VerOpts = {
|
|
32
58
|
lowS?: boolean;
|
|
33
59
|
prehash?: boolean;
|
|
60
|
+
format?: 'compact' | 'der' | undefined;
|
|
34
61
|
};
|
|
35
|
-
/**
|
|
36
|
-
* ### Design rationale for types
|
|
37
|
-
*
|
|
38
|
-
* * Interaction between classes from different curves should fail:
|
|
39
|
-
* `k256.Point.BASE.add(p256.Point.BASE)`
|
|
40
|
-
* * For this purpose we want to use `instanceof` operator, which is fast and works during runtime
|
|
41
|
-
* * Different calls of `curve()` would return different classes -
|
|
42
|
-
* `curve(params) !== curve(params)`: if somebody decided to monkey-patch their curve,
|
|
43
|
-
* it won't affect others
|
|
44
|
-
*
|
|
45
|
-
* TypeScript can't infer types for classes created inside a function. Classes is one instance of nominative types in TypeScript and interfaces only check for shape, so it's hard to create unique type for every function call.
|
|
46
|
-
*
|
|
47
|
-
* We can use generic types via some param, like curve opts, but that would:
|
|
48
|
-
* 1. Enable interaction between `curve(params)` and `curve(params)` (curves of same params)
|
|
49
|
-
* which is hard to debug.
|
|
50
|
-
* 2. Params can be generic and we can't enforce them to be constant value:
|
|
51
|
-
* if somebody creates curve from non-constant params,
|
|
52
|
-
* it would be allowed to interact with other curves with non-constant params
|
|
53
|
-
*
|
|
54
|
-
* TODO: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol
|
|
55
|
-
*/
|
|
56
62
|
export interface ProjPointType<T> extends Group<ProjPointType<T>> {
|
|
57
63
|
readonly px: T;
|
|
58
64
|
readonly py: T;
|
|
@@ -83,27 +89,11 @@ export type CurvePointsType<T> = BasicWCurve<T> & {
|
|
|
83
89
|
fromBytes?: (bytes: Uint8Array) => AffinePoint<T>;
|
|
84
90
|
toBytes?: (c: ProjConstructor<T>, point: ProjPointType<T>, isCompressed: boolean) => Uint8Array;
|
|
85
91
|
};
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
readonly Fp: mod.IField<T>;
|
|
90
|
-
readonly n: bigint;
|
|
91
|
-
readonly h: bigint;
|
|
92
|
-
readonly hEff?: bigint;
|
|
93
|
-
readonly Gx: T;
|
|
94
|
-
readonly Gy: T;
|
|
95
|
-
readonly allowInfinityPoint?: boolean;
|
|
96
|
-
readonly a: T;
|
|
97
|
-
readonly b: T;
|
|
98
|
-
readonly allowedPrivateKeyLengths?: readonly number[];
|
|
99
|
-
readonly wrapPrivateKey?: boolean;
|
|
100
|
-
readonly endo?: EndomorphismOpts;
|
|
101
|
-
readonly isTorsionFree?: ((c: ProjConstructor<T>, point: ProjPointType<T>) => boolean) | undefined;
|
|
102
|
-
readonly clearCofactor?: ((c: ProjConstructor<T>, point: ProjPointType<T>) => ProjPointType<T>) | undefined;
|
|
103
|
-
readonly fromBytes?: ((bytes: Uint8Array) => AffinePoint<T>) | undefined;
|
|
104
|
-
readonly toBytes?: ((c: ProjConstructor<T>, point: ProjPointType<T>, isCompressed: boolean) => Uint8Array) | undefined;
|
|
105
|
-
readonly p: bigint;
|
|
92
|
+
export type CurvePointsTypeWithLength<T> = Readonly<CurvePointsType<T> & {
|
|
93
|
+
nByteLength: number;
|
|
94
|
+
nBitLength: number;
|
|
106
95
|
}>;
|
|
96
|
+
declare function validatePointOpts<T>(curve: CurvePointsType<T>): CurvePointsTypeWithLength<T>;
|
|
107
97
|
export type CurvePointsRes<T> = {
|
|
108
98
|
CURVE: ReturnType<typeof validatePointOpts<T>>;
|
|
109
99
|
ProjectivePoint: ProjConstructor<T>;
|
|
@@ -111,21 +101,11 @@ export type CurvePointsRes<T> = {
|
|
|
111
101
|
weierstrassEquation: (x: T) => T;
|
|
112
102
|
isWithinCurveOrder: (num: bigint) => boolean;
|
|
113
103
|
};
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
* Docs: https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/, https://luca.ntop.org/Teaching/Appunti/asn1.html
|
|
120
|
-
*/
|
|
121
|
-
export declare const DER: {
|
|
122
|
-
Err: {
|
|
123
|
-
new (m?: string): {
|
|
124
|
-
name: string;
|
|
125
|
-
message: string;
|
|
126
|
-
stack?: string;
|
|
127
|
-
};
|
|
128
|
-
};
|
|
104
|
+
export declare class DERErr extends Error {
|
|
105
|
+
constructor(m?: string);
|
|
106
|
+
}
|
|
107
|
+
export type IDER = {
|
|
108
|
+
Err: typeof DERErr;
|
|
129
109
|
_tlv: {
|
|
130
110
|
encode: (tag: number, data: string) => string;
|
|
131
111
|
decode(tag: number, data: Uint8Array): {
|
|
@@ -146,6 +126,14 @@ export declare const DER: {
|
|
|
146
126
|
s: bigint;
|
|
147
127
|
}): string;
|
|
148
128
|
};
|
|
129
|
+
/**
|
|
130
|
+
* ASN.1 DER encoding utilities. ASN is very complex & fragile. Format:
|
|
131
|
+
*
|
|
132
|
+
* [0x30 (SEQUENCE), bytelength, 0x02 (INTEGER), intLength, R, 0x02 (INTEGER), intLength, S]
|
|
133
|
+
*
|
|
134
|
+
* Docs: https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/, https://luca.ntop.org/Teaching/Appunti/asn1.html
|
|
135
|
+
*/
|
|
136
|
+
export declare const DER: IDER;
|
|
149
137
|
export declare function weierstrassPoints<T>(opts: CurvePointsType<T>): CurvePointsRes<T>;
|
|
150
138
|
export interface SignatureType {
|
|
151
139
|
readonly r: bigint;
|
|
@@ -182,30 +170,9 @@ export type CurveType = BasicWCurve<bigint> & {
|
|
|
182
170
|
bits2int?: (bytes: Uint8Array) => bigint;
|
|
183
171
|
bits2int_modN?: (bytes: Uint8Array) => bigint;
|
|
184
172
|
};
|
|
185
|
-
declare function validateOpts(curve: CurveType): Readonly<{
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
readonly Fp: mod.IField<bigint>;
|
|
189
|
-
readonly n: bigint;
|
|
190
|
-
readonly h: bigint;
|
|
191
|
-
readonly hEff?: bigint;
|
|
192
|
-
readonly Gx: bigint;
|
|
193
|
-
readonly Gy: bigint;
|
|
194
|
-
readonly allowInfinityPoint?: boolean;
|
|
195
|
-
readonly a: bigint;
|
|
196
|
-
readonly b: bigint;
|
|
197
|
-
readonly allowedPrivateKeyLengths?: readonly number[];
|
|
198
|
-
readonly wrapPrivateKey?: boolean;
|
|
199
|
-
readonly endo?: EndomorphismOpts;
|
|
200
|
-
readonly isTorsionFree?: ((c: ProjConstructor<bigint>, point: ProjPointType<bigint>) => boolean) | undefined;
|
|
201
|
-
readonly clearCofactor?: ((c: ProjConstructor<bigint>, point: ProjPointType<bigint>) => ProjPointType<bigint>) | undefined;
|
|
202
|
-
readonly hash: CHash;
|
|
203
|
-
readonly hmac: HmacFnSync;
|
|
204
|
-
readonly randomBytes: (bytesLength?: number) => Uint8Array;
|
|
205
|
-
lowS: boolean;
|
|
206
|
-
readonly bits2int?: (bytes: Uint8Array) => bigint;
|
|
207
|
-
readonly bits2int_modN?: (bytes: Uint8Array) => bigint;
|
|
208
|
-
readonly p: bigint;
|
|
173
|
+
declare function validateOpts(curve: CurveType): Readonly<CurveType & {
|
|
174
|
+
nByteLength: number;
|
|
175
|
+
nBitLength: number;
|
|
209
176
|
}>;
|
|
210
177
|
export type CurveFn = {
|
|
211
178
|
CURVE: ReturnType<typeof validateOpts>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"weierstrass.d.ts","sourceRoot":"","sources":["../src/abstract/weierstrass.ts"],"names":[],"mappings":"AAAA,sEAAsE;
|
|
1
|
+
{"version":3,"file":"weierstrass.d.ts","sourceRoot":"","sources":["../src/abstract/weierstrass.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,sEAAsE;AACtE,OAAO,EACL,WAAW,EACX,UAAU,EACV,KAAK,EACL,gBAAgB,EAIjB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AAEpC,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAgC,MAAM,YAAY,CAAC;AAE/E,YAAY,EAAE,WAAW,EAAE,CAAC;AAC5B,KAAK,UAAU,GAAG,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,QAAQ,EAAE,UAAU,EAAE,KAAK,UAAU,CAAC;AAC7E,KAAK,gBAAgB,GAAG;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK;QAAE,KAAK,EAAE,OAAO,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,OAAO,CAAC;Q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C,KAAK;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,CAmEhD;AACD;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EACnC,EAAE,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EACjB,IAAI,EAAE;IACJ,CAAC,EAAE,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,CAAC;CACN,GACA,CAAC,CAAC,EAAE,CAAC,KAAK;IAAE,CAAC,EAAE,CAAC,CAAC;IAAC,CAAC,EAAE,CAAC,CAAA;CAAE,CAsC1B"}
|
package/abstract/weierstrass.js
CHANGED
|
@@ -1,12 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DER = void 0;
|
|
3
|
+
exports.DER = exports.DERErr = void 0;
|
|
4
4
|
exports.weierstrassPoints = weierstrassPoints;
|
|
5
5
|
exports.weierstrass = weierstrass;
|
|
6
6
|
exports.SWUFpSqrtRatio = SWUFpSqrtRatio;
|
|
7
7
|
exports.mapToCurveSimpleSWU = mapToCurveSimpleSWU;
|
|
8
|
+
/**
|
|
9
|
+
* Short Weierstrass curve methods. The formula is: y² = x³ + ax + b.
|
|
10
|
+
*
|
|
11
|
+
* ### Design rationale for types
|
|
12
|
+
*
|
|
13
|
+
* * Interaction between classes from different curves should fail:
|
|
14
|
+
* `k256.Point.BASE.add(p256.Point.BASE)`
|
|
15
|
+
* * For this purpose we want to use `instanceof` operator, which is fast and works during runtime
|
|
16
|
+
* * Different calls of `curve()` would return different classes -
|
|
17
|
+
* `curve(params) !== curve(params)`: if somebody decided to monkey-patch their curve,
|
|
18
|
+
* it won't affect others
|
|
19
|
+
*
|
|
20
|
+
* TypeScript can't infer types for classes created inside a function. Classes is one instance
|
|
21
|
+
* of nominative types in TypeScript and interfaces only check for shape, so it's hard to create
|
|
22
|
+
* unique type for every function call.
|
|
23
|
+
*
|
|
24
|
+
* We can use generic types via some param, like curve opts, but that would:
|
|
25
|
+
* 1. Enable interaction between `curve(params)` and `curve(params)` (curves of same params)
|
|
26
|
+
* which is hard to debug.
|
|
27
|
+
* 2. Params can be generic and we can't enforce them to be constant value:
|
|
28
|
+
* if somebody creates curve from non-constant params,
|
|
29
|
+
* it would be allowed to interact with other curves with non-constant params
|
|
30
|
+
*
|
|
31
|
+
* @todo https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol
|
|
32
|
+
* @module
|
|
33
|
+
*/
|
|
8
34
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
9
|
-
// Short Weierstrass curve. The formula is: y² = x³ + ax + b
|
|
10
35
|
const curve_js_1 = require("./curve.js");
|
|
11
36
|
const mod = require("./modular.js");
|
|
12
37
|
const ut = require("./utils.js");
|
|
@@ -34,17 +59,23 @@ function validatePointOpts(curve) {
|
|
|
34
59
|
const { endo, Fp, a } = opts;
|
|
35
60
|
if (endo) {
|
|
36
61
|
if (!Fp.eql(a, Fp.ZERO)) {
|
|
37
|
-
throw new Error('
|
|
62
|
+
throw new Error('invalid endomorphism, can only be defined for Koblitz curves that have a=0');
|
|
38
63
|
}
|
|
39
64
|
if (typeof endo !== 'object' ||
|
|
40
65
|
typeof endo.beta !== 'bigint' ||
|
|
41
66
|
typeof endo.splitScalar !== 'function') {
|
|
42
|
-
throw new Error('
|
|
67
|
+
throw new Error('invalid endomorphism, expected beta: bigint and splitScalar: function');
|
|
43
68
|
}
|
|
44
69
|
}
|
|
45
70
|
return Object.freeze({ ...opts });
|
|
46
71
|
}
|
|
47
72
|
const { bytesToNumberBE: b2n, hexToBytes: h2b } = ut;
|
|
73
|
+
class DERErr extends Error {
|
|
74
|
+
constructor(m = '') {
|
|
75
|
+
super(m);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.DERErr = DERErr;
|
|
48
79
|
/**
|
|
49
80
|
* ASN.1 DER encoding utilities. ASN is very complex & fragile. Format:
|
|
50
81
|
*
|
|
@@ -54,11 +85,7 @@ const { bytesToNumberBE: b2n, hexToBytes: h2b } = ut;
|
|
|
54
85
|
*/
|
|
55
86
|
exports.DER = {
|
|
56
87
|
// asn.1 DER encoding utils
|
|
57
|
-
Err:
|
|
58
|
-
constructor(m = '') {
|
|
59
|
-
super(m);
|
|
60
|
-
}
|
|
61
|
-
},
|
|
88
|
+
Err: DERErr,
|
|
62
89
|
// Basic building block is TLV (Tag-Length-Value)
|
|
63
90
|
_tlv: {
|
|
64
91
|
encode: (tag, data) => {
|
|
@@ -73,7 +100,8 @@ exports.DER = {
|
|
|
73
100
|
throw new E('tlv.encode: long form length too big');
|
|
74
101
|
// length of length with long form flag
|
|
75
102
|
const lenLen = dataLen > 127 ? ut.numberToHexUnpadded((len.length / 2) | 128) : '';
|
|
76
|
-
|
|
103
|
+
const t = ut.numberToHexUnpadded(tag);
|
|
104
|
+
return t + lenLen + len + data;
|
|
77
105
|
},
|
|
78
106
|
// v - value, l - left bytes (unparsed)
|
|
79
107
|
decode(tag, data) {
|
|
@@ -126,15 +154,15 @@ exports.DER = {
|
|
|
126
154
|
if (Number.parseInt(hex[0], 16) & 0b1000)
|
|
127
155
|
hex = '00' + hex;
|
|
128
156
|
if (hex.length & 1)
|
|
129
|
-
throw new E('unexpected assertion');
|
|
157
|
+
throw new E('unexpected DER parsing assertion: unpadded hex');
|
|
130
158
|
return hex;
|
|
131
159
|
},
|
|
132
160
|
decode(data) {
|
|
133
161
|
const { Err: E } = exports.DER;
|
|
134
162
|
if (data[0] & 128)
|
|
135
|
-
throw new E('
|
|
163
|
+
throw new E('invalid signature integer: negative');
|
|
136
164
|
if (data[0] === 0x00 && !(data[1] & 128))
|
|
137
|
-
throw new E('
|
|
165
|
+
throw new E('invalid signature integer: unnecessary leading zero');
|
|
138
166
|
return b2n(data);
|
|
139
167
|
},
|
|
140
168
|
},
|
|
@@ -145,16 +173,18 @@ exports.DER = {
|
|
|
145
173
|
ut.abytes(data);
|
|
146
174
|
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(0x30, data);
|
|
147
175
|
if (seqLeftBytes.length)
|
|
148
|
-
throw new E('
|
|
176
|
+
throw new E('invalid signature: left bytes after parsing');
|
|
149
177
|
const { v: rBytes, l: rLeftBytes } = tlv.decode(0x02, seqBytes);
|
|
150
178
|
const { v: sBytes, l: sLeftBytes } = tlv.decode(0x02, rLeftBytes);
|
|
151
179
|
if (sLeftBytes.length)
|
|
152
|
-
throw new E('
|
|
180
|
+
throw new E('invalid signature: left bytes after parsing');
|
|
153
181
|
return { r: int.decode(rBytes), s: int.decode(sBytes) };
|
|
154
182
|
},
|
|
155
183
|
hexFromSig(sig) {
|
|
156
184
|
const { _tlv: tlv, _int: int } = exports.DER;
|
|
157
|
-
const
|
|
185
|
+
const rs = tlv.encode(0x02, int.encode(sig.r));
|
|
186
|
+
const ss = tlv.encode(0x02, int.encode(sig.s));
|
|
187
|
+
const seq = rs + ss;
|
|
158
188
|
return tlv.encode(0x30, seq);
|
|
159
189
|
},
|
|
160
190
|
};
|
|
@@ -208,7 +238,7 @@ function weierstrassPoints(opts) {
|
|
|
208
238
|
key = ut.bytesToHex(key);
|
|
209
239
|
// Normalize to hex string, pad. E.g. P521 would norm 130-132 char hex to 132-char bytes
|
|
210
240
|
if (typeof key !== 'string' || !lengths.includes(key.length))
|
|
211
|
-
throw new Error('
|
|
241
|
+
throw new Error('invalid private key');
|
|
212
242
|
key = key.padStart(nByteLength * 2, '0');
|
|
213
243
|
}
|
|
214
244
|
let num;
|
|
@@ -219,7 +249,7 @@ function weierstrassPoints(opts) {
|
|
|
219
249
|
: ut.bytesToNumberBE((0, utils_js_1.ensureBytes)('private key', key, nByteLength));
|
|
220
250
|
}
|
|
221
251
|
catch (error) {
|
|
222
|
-
throw new Error(
|
|
252
|
+
throw new Error('invalid private key, expected hex or ' + nByteLength + ' bytes, got ' + typeof key);
|
|
223
253
|
}
|
|
224
254
|
if (wrapPrivateKey)
|
|
225
255
|
num = mod.mod(num, N); // disabled by default, enabled for BLS
|
|
@@ -259,7 +289,7 @@ function weierstrassPoints(opts) {
|
|
|
259
289
|
if (p.is0()) {
|
|
260
290
|
// (0, 1, 0) aka ZERO is invalid in most contexts.
|
|
261
291
|
// In BLS, ZERO can be serialized, so we allow it.
|
|
262
|
-
// (0, 0, 0) is
|
|
292
|
+
// (0, 0, 0) is invalid representation of ZERO.
|
|
263
293
|
if (CURVE.allowInfinityPoint && !Fp.is0(p.py))
|
|
264
294
|
return;
|
|
265
295
|
throw new Error('bad point: ZERO');
|
|
@@ -483,16 +513,17 @@ function weierstrassPoints(opts) {
|
|
|
483
513
|
* an exposed private key e.g. sig verification, which works over *public* keys.
|
|
484
514
|
*/
|
|
485
515
|
multiplyUnsafe(sc) {
|
|
486
|
-
|
|
516
|
+
const { endo, n: N } = CURVE;
|
|
517
|
+
ut.aInRange('scalar', sc, _0n, N);
|
|
487
518
|
const I = Point.ZERO;
|
|
488
519
|
if (sc === _0n)
|
|
489
520
|
return I;
|
|
490
|
-
if (sc === _1n)
|
|
521
|
+
if (this.is0() || sc === _1n)
|
|
491
522
|
return this;
|
|
492
|
-
|
|
493
|
-
if (!endo)
|
|
494
|
-
return wnaf.
|
|
495
|
-
//
|
|
523
|
+
// Case a: no endomorphism. Case b: has precomputes.
|
|
524
|
+
if (!endo || wnaf.hasPrecomputes(this))
|
|
525
|
+
return wnaf.wNAFCachedUnsafe(this, sc, Point.normalizeZ);
|
|
526
|
+
// Case c: endomorphism
|
|
496
527
|
let { k1neg, k1, k2neg, k2 } = endo.splitScalar(sc);
|
|
497
528
|
let k1p = I;
|
|
498
529
|
let k2p = I;
|
|
@@ -678,7 +709,9 @@ function weierstrass(curveDef) {
|
|
|
678
709
|
return { x, y };
|
|
679
710
|
}
|
|
680
711
|
else {
|
|
681
|
-
|
|
712
|
+
const cl = compressedLen;
|
|
713
|
+
const ul = uncompressedLen;
|
|
714
|
+
throw new Error('invalid Point, expected length of ' + cl + ', or uncompressed ' + ul + ', got ' + len);
|
|
682
715
|
}
|
|
683
716
|
},
|
|
684
717
|
});
|
|
@@ -843,6 +876,9 @@ function weierstrass(curveDef) {
|
|
|
843
876
|
// int2octets can't be used; pads small msgs with 0: unacceptatble for trunc as per RFC vectors
|
|
844
877
|
const bits2int = CURVE.bits2int ||
|
|
845
878
|
function (bytes) {
|
|
879
|
+
// Our custom check "just in case"
|
|
880
|
+
if (bytes.length > 8192)
|
|
881
|
+
throw new Error('input is too large');
|
|
846
882
|
// For curves with nBitLength % 8 !== 0: bits2octets(bits2octets(m)) !== bits2octets(m)
|
|
847
883
|
// for some cases, since bytes.length * 8 is not actual bitLength.
|
|
848
884
|
const num = ut.bytesToNumberBE(bytes); // check for == u8 done here
|
|
@@ -859,15 +895,15 @@ function weierstrass(curveDef) {
|
|
|
859
895
|
* Converts to bytes. Checks if num in `[0..ORDER_MASK-1]` e.g.: `[0..2^256-1]`.
|
|
860
896
|
*/
|
|
861
897
|
function int2octets(num) {
|
|
862
|
-
ut.aInRange(
|
|
898
|
+
ut.aInRange('num < 2^' + CURVE.nBitLength, num, _0n, ORDER_MASK);
|
|
863
899
|
// works with order, can have different size than numToField!
|
|
864
900
|
return ut.numberToBytesBE(num, CURVE.nByteLength);
|
|
865
901
|
}
|
|
866
902
|
// Steps A, D of RFC6979 3.2
|
|
867
903
|
// Creates RFC6979 seed; converts msg/privKey to numbers.
|
|
868
904
|
// Used only in sign, not in verify.
|
|
869
|
-
// NOTE: we cannot assume here that msgHash has same amount of bytes as curve order,
|
|
870
|
-
// Also it can be bigger for P224 + SHA256
|
|
905
|
+
// NOTE: we cannot assume here that msgHash has same amount of bytes as curve order,
|
|
906
|
+
// this will be invalid at least for P521. Also it can be bigger for P224 + SHA256
|
|
871
907
|
function prepSig(msgHash, privateKey, opts = defaultSigOpts) {
|
|
872
908
|
if (['recovered', 'canonical'].some((k) => k in opts))
|
|
873
909
|
throw new Error('sign() legacy options not supported');
|
|
@@ -961,39 +997,48 @@ function weierstrass(curveDef) {
|
|
|
961
997
|
const sg = signature;
|
|
962
998
|
msgHash = (0, utils_js_1.ensureBytes)('msgHash', msgHash);
|
|
963
999
|
publicKey = (0, utils_js_1.ensureBytes)('publicKey', publicKey);
|
|
1000
|
+
const { lowS, prehash, format } = opts;
|
|
1001
|
+
// Verify opts, deduce signature format
|
|
1002
|
+
validateSigVerOpts(opts);
|
|
964
1003
|
if ('strict' in opts)
|
|
965
1004
|
throw new Error('options.strict was renamed to lowS');
|
|
966
|
-
|
|
967
|
-
|
|
1005
|
+
if (format !== undefined && format !== 'compact' && format !== 'der')
|
|
1006
|
+
throw new Error('format must be compact or der');
|
|
1007
|
+
const isHex = typeof sg === 'string' || ut.isBytes(sg);
|
|
1008
|
+
const isObj = !isHex &&
|
|
1009
|
+
!format &&
|
|
1010
|
+
typeof sg === 'object' &&
|
|
1011
|
+
sg !== null &&
|
|
1012
|
+
typeof sg.r === 'bigint' &&
|
|
1013
|
+
typeof sg.s === 'bigint';
|
|
1014
|
+
if (!isHex && !isObj)
|
|
1015
|
+
throw new Error('invalid signature, expected Uint8Array, hex string or Signature instance');
|
|
968
1016
|
let _sig = undefined;
|
|
969
1017
|
let P;
|
|
970
1018
|
try {
|
|
971
|
-
if (
|
|
1019
|
+
if (isObj)
|
|
1020
|
+
_sig = new Signature(sg.r, sg.s);
|
|
1021
|
+
if (isHex) {
|
|
972
1022
|
// Signature can be represented in 2 ways: compact (2*nByteLength) & DER (variable-length).
|
|
973
1023
|
// Since DER can also be 2*nByteLength bytes, we check for it first.
|
|
974
1024
|
try {
|
|
975
|
-
|
|
1025
|
+
if (format !== 'compact')
|
|
1026
|
+
_sig = Signature.fromDER(sg);
|
|
976
1027
|
}
|
|
977
1028
|
catch (derError) {
|
|
978
1029
|
if (!(derError instanceof exports.DER.Err))
|
|
979
1030
|
throw derError;
|
|
980
|
-
_sig = Signature.fromCompact(sg);
|
|
981
1031
|
}
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
const { r, s } = sg;
|
|
985
|
-
_sig = new Signature(r, s);
|
|
986
|
-
}
|
|
987
|
-
else {
|
|
988
|
-
throw new Error('PARSE');
|
|
1032
|
+
if (!_sig && format !== 'der')
|
|
1033
|
+
_sig = Signature.fromCompact(sg);
|
|
989
1034
|
}
|
|
990
1035
|
P = Point.fromHex(publicKey);
|
|
991
1036
|
}
|
|
992
1037
|
catch (error) {
|
|
993
|
-
if (error.message === 'PARSE')
|
|
994
|
-
throw new Error(`signature must be Signature instance, Uint8Array or hex string`);
|
|
995
1038
|
return false;
|
|
996
1039
|
}
|
|
1040
|
+
if (!_sig)
|
|
1041
|
+
return false;
|
|
997
1042
|
if (lowS && _sig.hasHighS())
|
|
998
1043
|
return false;
|
|
999
1044
|
if (prehash)
|