@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.
- package/README.md +520 -505
- package/abstract/bls.d.ts +58 -120
- package/abstract/bls.d.ts.map +1 -1
- package/abstract/bls.js +108 -152
- package/abstract/bls.js.map +1 -1
- package/abstract/curve.d.ts +18 -54
- package/abstract/curve.d.ts.map +1 -1
- package/abstract/curve.js +30 -49
- package/abstract/curve.js.map +1 -1
- package/abstract/edwards.d.ts +18 -77
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +68 -144
- package/abstract/edwards.js.map +1 -1
- package/abstract/fft.js +14 -27
- package/abstract/fft.js.map +1 -1
- package/abstract/hash-to-curve.d.ts +35 -47
- package/abstract/hash-to-curve.d.ts.map +1 -1
- package/abstract/hash-to-curve.js +42 -46
- package/abstract/hash-to-curve.js.map +1 -1
- package/abstract/modular.d.ts +5 -17
- package/abstract/modular.d.ts.map +1 -1
- package/abstract/modular.js +170 -169
- package/abstract/modular.js.map +1 -1
- package/abstract/montgomery.d.ts +7 -12
- package/abstract/montgomery.d.ts.map +1 -1
- package/abstract/montgomery.js +22 -29
- package/abstract/montgomery.js.map +1 -1
- package/abstract/oprf.d.ts +282 -0
- package/abstract/oprf.d.ts.map +1 -0
- package/abstract/oprf.js +297 -0
- package/abstract/oprf.js.map +1 -0
- package/abstract/poseidon.d.ts.map +1 -1
- package/abstract/poseidon.js +26 -31
- package/abstract/poseidon.js.map +1 -1
- package/abstract/tower.d.ts.map +1 -1
- package/abstract/tower.js +43 -19
- package/abstract/tower.js.map +1 -1
- package/abstract/weierstrass.d.ts +77 -168
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +184 -389
- package/abstract/weierstrass.js.map +1 -1
- package/bls12-381.d.ts +5 -11
- package/bls12-381.d.ts.map +1 -1
- package/bls12-381.js +161 -181
- package/bls12-381.js.map +1 -1
- package/bn254.d.ts +59 -11
- package/bn254.d.ts.map +1 -1
- package/bn254.js +69 -97
- package/bn254.js.map +1 -1
- package/ed25519.d.ts +33 -48
- package/ed25519.d.ts.map +1 -1
- package/ed25519.js +147 -161
- package/ed25519.js.map +1 -1
- package/ed448.d.ts +27 -36
- package/ed448.d.ts.map +1 -1
- package/ed448.js +143 -164
- package/ed448.js.map +1 -1
- package/index.d.ts +1 -0
- package/index.js +20 -4
- package/index.js.map +1 -1
- package/misc.d.ts +10 -14
- package/misc.d.ts.map +1 -1
- package/misc.js +53 -62
- package/misc.js.map +1 -1
- package/nist.d.ts +31 -16
- package/nist.d.ts.map +1 -1
- package/nist.js +75 -64
- package/nist.js.map +1 -1
- package/package.json +20 -234
- package/secp256k1.d.ts +17 -30
- package/secp256k1.d.ts.map +1 -1
- package/secp256k1.js +59 -73
- package/secp256k1.js.map +1 -1
- package/src/abstract/bls.ts +207 -354
- package/src/abstract/curve.ts +25 -84
- package/src/abstract/edwards.ts +68 -193
- package/src/abstract/hash-to-curve.ts +71 -85
- package/src/abstract/modular.ts +150 -134
- package/src/abstract/montgomery.ts +28 -35
- package/src/abstract/oprf.ts +600 -0
- package/src/abstract/poseidon.ts +6 -8
- package/src/abstract/tower.ts +0 -3
- package/src/abstract/weierstrass.ts +203 -525
- package/src/bls12-381.ts +133 -139
- package/src/bn254.ts +69 -93
- package/src/ed25519.ts +106 -133
- package/src/ed448.ts +111 -138
- package/src/index.ts +19 -3
- package/src/misc.ts +68 -51
- package/src/nist.ts +77 -70
- package/src/secp256k1.ts +46 -81
- package/src/utils.ts +67 -137
- package/src/webcrypto.ts +403 -0
- package/utils.d.ts +31 -38
- package/utils.d.ts.map +1 -1
- package/utils.js +66 -185
- package/utils.js.map +1 -1
- package/webcrypto.d.ts +99 -0
- package/webcrypto.d.ts.map +1 -0
- package/webcrypto.js +256 -0
- package/webcrypto.js.map +1 -0
- package/_shortw_utils.d.ts +0 -19
- package/_shortw_utils.d.ts.map +0 -1
- package/_shortw_utils.js +0 -20
- package/_shortw_utils.js.map +0 -1
- package/abstract/utils.d.ts +0 -78
- package/abstract/utils.d.ts.map +0 -1
- package/abstract/utils.js +0 -73
- package/abstract/utils.js.map +0 -1
- package/esm/_shortw_utils.d.ts +0 -19
- package/esm/_shortw_utils.d.ts.map +0 -1
- package/esm/_shortw_utils.js +0 -16
- package/esm/_shortw_utils.js.map +0 -1
- package/esm/abstract/bls.d.ts +0 -190
- package/esm/abstract/bls.d.ts.map +0 -1
- package/esm/abstract/bls.js +0 -408
- package/esm/abstract/bls.js.map +0 -1
- package/esm/abstract/curve.d.ts +0 -231
- package/esm/abstract/curve.d.ts.map +0 -1
- package/esm/abstract/curve.js +0 -465
- package/esm/abstract/curve.js.map +0 -1
- package/esm/abstract/edwards.d.ts +0 -243
- package/esm/abstract/edwards.d.ts.map +0 -1
- package/esm/abstract/edwards.js +0 -627
- package/esm/abstract/edwards.js.map +0 -1
- package/esm/abstract/fft.d.ts +0 -122
- package/esm/abstract/fft.d.ts.map +0 -1
- package/esm/abstract/fft.js +0 -425
- package/esm/abstract/fft.js.map +0 -1
- package/esm/abstract/hash-to-curve.d.ts +0 -102
- package/esm/abstract/hash-to-curve.d.ts.map +0 -1
- package/esm/abstract/hash-to-curve.js +0 -203
- package/esm/abstract/hash-to-curve.js.map +0 -1
- package/esm/abstract/modular.d.ts +0 -171
- package/esm/abstract/modular.d.ts.map +0 -1
- package/esm/abstract/modular.js +0 -530
- package/esm/abstract/modular.js.map +0 -1
- package/esm/abstract/montgomery.d.ts +0 -30
- package/esm/abstract/montgomery.d.ts.map +0 -1
- package/esm/abstract/montgomery.js +0 -157
- package/esm/abstract/montgomery.js.map +0 -1
- package/esm/abstract/poseidon.d.ts +0 -68
- package/esm/abstract/poseidon.d.ts.map +0 -1
- package/esm/abstract/poseidon.js +0 -296
- package/esm/abstract/poseidon.js.map +0 -1
- package/esm/abstract/tower.d.ts +0 -95
- package/esm/abstract/tower.d.ts.map +0 -1
- package/esm/abstract/tower.js +0 -714
- package/esm/abstract/tower.js.map +0 -1
- package/esm/abstract/utils.d.ts +0 -78
- package/esm/abstract/utils.d.ts.map +0 -1
- package/esm/abstract/utils.js +0 -70
- package/esm/abstract/utils.js.map +0 -1
- package/esm/abstract/weierstrass.d.ts +0 -416
- package/esm/abstract/weierstrass.d.ts.map +0 -1
- package/esm/abstract/weierstrass.js +0 -1413
- package/esm/abstract/weierstrass.js.map +0 -1
- package/esm/bls12-381.d.ts +0 -16
- package/esm/bls12-381.d.ts.map +0 -1
- package/esm/bls12-381.js +0 -705
- package/esm/bls12-381.js.map +0 -1
- package/esm/bn254.d.ts +0 -18
- package/esm/bn254.d.ts.map +0 -1
- package/esm/bn254.js +0 -214
- package/esm/bn254.js.map +0 -1
- package/esm/ed25519.d.ts +0 -106
- package/esm/ed25519.d.ts.map +0 -1
- package/esm/ed25519.js +0 -467
- package/esm/ed25519.js.map +0 -1
- package/esm/ed448.d.ts +0 -100
- package/esm/ed448.d.ts.map +0 -1
- package/esm/ed448.js +0 -459
- package/esm/ed448.js.map +0 -1
- package/esm/index.d.ts +0 -2
- package/esm/index.d.ts.map +0 -1
- package/esm/index.js +0 -17
- package/esm/index.js.map +0 -1
- package/esm/jubjub.d.ts +0 -12
- package/esm/jubjub.d.ts.map +0 -1
- package/esm/jubjub.js +0 -12
- package/esm/jubjub.js.map +0 -1
- package/esm/misc.d.ts +0 -19
- package/esm/misc.d.ts.map +0 -1
- package/esm/misc.js +0 -109
- package/esm/misc.js.map +0 -1
- package/esm/nist.d.ts +0 -21
- package/esm/nist.d.ts.map +0 -1
- package/esm/nist.js +0 -132
- package/esm/nist.js.map +0 -1
- package/esm/p256.d.ts +0 -16
- package/esm/p256.d.ts.map +0 -1
- package/esm/p256.js +0 -16
- package/esm/p256.js.map +0 -1
- package/esm/p384.d.ts +0 -16
- package/esm/p384.d.ts.map +0 -1
- package/esm/p384.js +0 -16
- package/esm/p384.js.map +0 -1
- package/esm/p521.d.ts +0 -16
- package/esm/p521.d.ts.map +0 -1
- package/esm/p521.js +0 -16
- package/esm/p521.js.map +0 -1
- package/esm/package.json +0 -4
- package/esm/pasta.d.ts +0 -10
- package/esm/pasta.d.ts.map +0 -1
- package/esm/pasta.js +0 -10
- package/esm/pasta.js.map +0 -1
- package/esm/secp256k1.d.ts +0 -89
- package/esm/secp256k1.d.ts.map +0 -1
- package/esm/secp256k1.js +0 -294
- package/esm/secp256k1.js.map +0 -1
- package/esm/utils.d.ts +0 -110
- package/esm/utils.d.ts.map +0 -1
- package/esm/utils.js +0 -322
- package/esm/utils.js.map +0 -1
- package/jubjub.d.ts +0 -12
- package/jubjub.d.ts.map +0 -1
- package/jubjub.js +0 -15
- package/jubjub.js.map +0 -1
- package/p256.d.ts +0 -16
- package/p256.d.ts.map +0 -1
- package/p256.js +0 -13
- package/p256.js.map +0 -1
- package/p384.d.ts +0 -16
- package/p384.d.ts.map +0 -1
- package/p384.js +0 -13
- package/p384.js.map +0 -1
- package/p521.d.ts +0 -16
- package/p521.d.ts.map +0 -1
- package/p521.js +0 -13
- package/p521.js.map +0 -1
- package/pasta.d.ts +0 -10
- package/pasta.d.ts.map +0 -1
- package/pasta.js +0 -13
- package/pasta.js.map +0 -1
- package/src/_shortw_utils.ts +0 -21
- package/src/abstract/utils.ts +0 -80
- package/src/jubjub.ts +0 -12
- package/src/p256.ts +0 -15
- package/src/p384.ts +0 -15
- package/src/p521.ts +0 -15
- package/src/package.json +0 -3
- package/src/pasta.ts +0 -9
package/src/abstract/curve.ts
CHANGED
|
@@ -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,
|
|
8
|
-
import { Field, FpInvertBatch,
|
|
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>>
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
+
}
|
package/src/abstract/edwards.ts
CHANGED
|
@@ -6,41 +6,38 @@
|
|
|
6
6
|
*/
|
|
7
7
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
8
8
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
_abytes2 as abytes,
|
|
9
|
+
abool,
|
|
10
|
+
abytes,
|
|
12
11
|
aInRange,
|
|
13
12
|
bytesToHex,
|
|
14
13
|
bytesToNumberLE,
|
|
15
14
|
concatBytes,
|
|
16
15
|
copyBytes,
|
|
17
|
-
|
|
16
|
+
hexToBytes,
|
|
18
17
|
isBytes,
|
|
19
18
|
memoized,
|
|
20
19
|
notImplemented,
|
|
21
|
-
|
|
20
|
+
validateObject,
|
|
21
|
+
randomBytes as wcRandomBytes,
|
|
22
22
|
type FHash,
|
|
23
|
-
type
|
|
23
|
+
type Signer,
|
|
24
24
|
} from '../utils.ts';
|
|
25
25
|
import {
|
|
26
|
-
|
|
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 {
|
|
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:
|
|
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:
|
|
143
|
-
sign: (
|
|
120
|
+
getPublicKey: (secretKey: Uint8Array) => Uint8Array;
|
|
121
|
+
sign: (
|
|
122
|
+
message: Uint8Array,
|
|
123
|
+
secretKey: Uint8Array,
|
|
124
|
+
options?: { context?: Uint8Array }
|
|
125
|
+
) => Uint8Array;
|
|
144
126
|
verify: (
|
|
145
|
-
sig:
|
|
146
|
-
message:
|
|
147
|
-
publicKey:
|
|
148
|
-
options?: { context?:
|
|
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
|
|
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,
|
|
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
|
|
178
|
-
*
|
|
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: (
|
|
182
|
-
getExtendedPublicKey: (key:
|
|
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 =
|
|
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
|
-
|
|
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
|
|
247
|
-
if (!(other instanceof Point)) throw new Error('
|
|
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
|
-
|
|
354
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
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 ||
|
|
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:
|
|
634
|
+
function getPrivateScalar(key: Uint8Array) {
|
|
686
635
|
const len = lengths.secretKey;
|
|
687
|
-
|
|
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 =
|
|
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:
|
|
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:
|
|
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:
|
|
660
|
+
function hashDomainToScalar(context: Uint8Array = Uint8Array.of(), ...msgs: Uint8Array[]) {
|
|
712
661
|
const msg = concatBytes(...msgs);
|
|
713
|
-
return modN_LE(cHash(domain(msg,
|
|
662
|
+
return modN_LE(cHash(domain(msg, abytes(context, undefined, 'context'), !!prehash)));
|
|
714
663
|
}
|
|
715
664
|
|
|
716
|
-
/** Signs message with
|
|
717
|
-
function sign(
|
|
718
|
-
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?:
|
|
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(
|
|
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 =
|
|
741
|
-
msg =
|
|
742
|
-
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
|
-
|
|
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
|
}
|