Skip to content

Commit bec7609

Browse files
committed
Improve tree-shaking
1 parent 9c89dc4 commit bec7609

File tree

7 files changed

+60
-57
lines changed

7 files changed

+60
-57
lines changed

src/abstract/curve.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import { bitLen, bitMask, type Signer } from '../utils.ts';
88
import { Field, FpInvertBatch, validateField, type IField } from './modular.ts';
99

10-
const _0n = BigInt(0);
11-
const _1n = BigInt(1);
10+
const _0n = /* @__PURE__ */ BigInt(0);
11+
const _1n = /* @__PURE__ */ BigInt(1);
1212

1313
export type AffinePoint<T> = {
1414
x: T;
@@ -592,7 +592,7 @@ function createField<T>(order: bigint, field?: IField<T>, isLE?: boolean): IFiel
592592
export type FpFn<T> = { Fp: IField<T>; Fn: IField<bigint> };
593593

594594
/** Validates CURVE opts and creates fields */
595-
export function _createCurveFields<T>(
595+
export function createCurveFields<T>(
596596
type: 'weierstrass' | 'edwards',
597597
CURVE: ValidCurveParams<T>,
598598
curveOpts: Partial<FpFn<T>> = {},

src/abstract/edwards.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
type Signer,
2424
} from '../utils.ts';
2525
import {
26-
_createCurveFields,
26+
createCurveFields,
2727
createKeygen,
2828
normalizeZ,
2929
wNAF,
@@ -185,7 +185,7 @@ function isEdValidXY(Fp: IField<bigint>, CURVE: EdwardsOpts, x: bigint, y: bigin
185185
}
186186

187187
export function edwards(params: EdwardsOpts, extraOpts: EdwardsExtraOpts = {}): EdwardsPointCons {
188-
const validated = _createCurveFields('edwards', params, extraOpts, extraOpts.FpFnLE);
188+
const validated = createCurveFields('edwards', params, extraOpts, extraOpts.FpFnLE);
189189
const { Fp, Fn } = validated;
190190
let CURVE = validated.CURVE as EdwardsOpts;
191191
const { h: cofactor } = CURVE;

src/abstract/modular.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import {
1515
numberToBytesLE,
1616
} from '../utils.ts';
1717

18+
// Numbers aren't used in x25519 / x448 builds
1819
// prettier-ignore
19-
const _0n = BigInt(0), _1n = /* @__PURE__ */ BigInt(1), _2n = /* @__PURE__ */ BigInt(2), _3n = /* @__PURE__ */ BigInt(3);
20+
const _0n = /* @__PURE__ */ BigInt(0), _1n = /* @__PURE__ */ BigInt(1), _2n = /* @__PURE__ */ BigInt(2);
2021
// prettier-ignore
21-
const _4n = /* @__PURE__ */ BigInt(4), _5n = /* @__PURE__ */ BigInt(5), _7n = /* @__PURE__ */ BigInt(7);
22+
const _3n = /* @__PURE__ */ BigInt(3), _4n = /* @__PURE__ */ BigInt(4), _5n = /* @__PURE__ */ BigInt(5);
2223
// prettier-ignore
23-
const _8n = /* @__PURE__ */ BigInt(8), _9n = /* @__PURE__ */ BigInt(9), _16n = /* @__PURE__ */ BigInt(16);
24+
const _7n = /* @__PURE__ */ BigInt(7), _8n = /* @__PURE__ */ BigInt(8), _9n = /* @__PURE__ */ BigInt(9);
25+
const _16n = /* @__PURE__ */ BigInt(16);
2426

2527
// Calculates a modulo b
2628
export function mod(a: bigint, b: bigint): bigint {

src/abstract/weierstrass.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import {
4747
type Signer,
4848
} from '../utils.ts';
4949
import {
50-
_createCurveFields,
50+
createCurveFields,
5151
createKeygen,
5252
mulEndoUnsafe,
5353
negateCt,
@@ -444,7 +444,7 @@ export function weierstrass<T>(
444444
params: WeierstrassOpts<T>,
445445
extraOpts: WeierstrassExtraOpts<T> = {}
446446
): WeierstrassPointCons<T> {
447-
const validated = _createCurveFields('weierstrass', params, extraOpts);
447+
const validated = createCurveFields('weierstrass', params, extraOpts);
448448
const { Fp, Fn } = validated;
449449
let CURVE = validated.CURVE as WeierstrassOpts<T>;
450450
const { h: cofactor, n: CURVE_ORDER } = CURVE;

src/ed25519.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { createORPF, type OPRF } from './abstract/oprf.ts';
4040
import { asciiToBytes, bytesToNumberLE, equalBytes } from './utils.ts';
4141

4242
// prettier-ignore
43-
const _0n = /* @__PURE__ */ BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
43+
const _0n = /* @__PURE__ */ BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = /* @__PURE__ */ BigInt(3);
4444
// prettier-ignore
4545
const _5n = BigInt(5), _8n = BigInt(8);
4646

src/ed448.ts

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ import { abytes, asciiToBytes, bytesToNumberLE, equalBytes } from './utils.ts';
3838
// Finite field 2n**448n - 2n**224n - 1n
3939
// Subgroup order
4040
// 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
41-
const ed448_CURVE: EdwardsOpts = {
42-
p: BigInt(
43-
'0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
44-
),
41+
const ed448_CURVE_p = BigInt(
42+
'0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
43+
);
44+
const ed448_CURVE: EdwardsOpts = /* @__PURE__ */ (() => ({
45+
p: ed448_CURVE_p,
4546
n: BigInt(
4647
'0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3'
4748
),
@@ -56,36 +57,37 @@ const ed448_CURVE: EdwardsOpts = {
5657
Gy: BigInt(
5758
'0x693f46716eb6bc248876203756c9c7624bea73736ca3984087789c1e05a0c2d73ad3ff1ce67c39c4fdbd132c4ed7c8ad9808795bf230fa14'
5859
),
59-
};
60+
}))();
6061

6162
// E448 NIST curve is identical to edwards448, except for:
6263
// d = 39082/39081
6364
// Gx = 3/2
64-
const E448_CURVE: EdwardsOpts = Object.assign({}, ed448_CURVE, {
65-
d: BigInt(
66-
'0xd78b4bdc7f0daf19f24f38c29373a2ccad46157242a50f37809b1da3412a12e79ccc9c81264cfe9ad080997058fb61c4243cc32dbaa156b9'
67-
),
68-
Gx: BigInt(
69-
'0x79a70b2b70400553ae7c9df416c792c61128751ac92969240c25a07d728bdc93e21f7787ed6972249de732f38496cd11698713093e9c04fc'
70-
),
71-
Gy: BigInt(
72-
'0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000001'
73-
),
74-
});
65+
const E448_CURVE: EdwardsOpts = /* @__PURE__ */ (() =>
66+
Object.assign({}, ed448_CURVE, {
67+
d: BigInt(
68+
'0xd78b4bdc7f0daf19f24f38c29373a2ccad46157242a50f37809b1da3412a12e79ccc9c81264cfe9ad080997058fb61c4243cc32dbaa156b9'
69+
),
70+
Gx: BigInt(
71+
'0x79a70b2b70400553ae7c9df416c792c61128751ac92969240c25a07d728bdc93e21f7787ed6972249de732f38496cd11698713093e9c04fc'
72+
),
73+
Gy: BigInt(
74+
'0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000001'
75+
),
76+
}))();
7577

7678
const shake256_114 = /* @__PURE__ */ wrapConstructor(() => shake256.create({ dkLen: 114 }));
7779
const shake256_64 = /* @__PURE__ */ wrapConstructor(() => shake256.create({ dkLen: 64 }));
7880

7981
// prettier-ignore
80-
const _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4), _11n = BigInt(11);
82+
const _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = /* @__PURE__ */ BigInt(4), _11n = BigInt(11);
8183
// prettier-ignore
8284
const _22n = BigInt(22), _44n = BigInt(44), _88n = BigInt(88), _223n = BigInt(223);
8385

8486
// powPminus3div4 calculates z = x^k mod p, where k = (p-3)/4.
8587
// Used for efficient square root calculation.
8688
// ((P-3)/4).toString(2) would produce bits [223x 1, 0, 222x 1]
8789
function ed448_pow_Pminus3div4(x: bigint): bigint {
88-
const P = ed448_CURVE.p;
90+
const P = ed448_CURVE_p;
8991
const b2 = (x * x * x) % P;
9092
const b3 = (b2 * b2 * x) % P;
9193
const b6 = (pow2(b3, _3n, P) * b3) % P;
@@ -114,7 +116,7 @@ function adjustScalarBytes(bytes: Uint8Array): Uint8Array {
114116
// Constant-time ratio of u to v. Allows to combine inversion and square root u/√v.
115117
// Uses algo from RFC8032 5.1.3.
116118
function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
117-
const P = ed448_CURVE.p;
119+
const P = ed448_CURVE_p;
118120
// https://www.rfc-editor.org/rfc/rfc8032#section-5.2.3
119121
// To compute the square root of (u/v), the first step is to compute the
120122
// candidate root x = (u/v)^((p+1)/4). This can be done using the
@@ -137,10 +139,10 @@ function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
137139
// The value fits in 448 bits, but we use 456-bit (57-byte) elements because of bitflags.
138140
// - ed25519 fits in 255 bits, allowing using last 1 byte for specifying bit flag of point negation.
139141
// - ed448 fits in 448 bits. We can't use last 1 byte: we can only use a bit 224 in the middle.
140-
const Fp = /* @__PURE__ */ (() => Field(ed448_CURVE.p, { BITS: 456, isLE: true }))();
142+
const Fp = /* @__PURE__ */ (() => Field(ed448_CURVE_p, { BITS: 456, isLE: true }))();
141143
const Fn = /* @__PURE__ */ (() => Field(ed448_CURVE.n, { BITS: 456, isLE: true }))();
142144
// decaf448 uses 448-bit (56-byte) keys
143-
const Fp448 = /* @__PURE__ */ (() => Field(ed448_CURVE.p, { BITS: 448, isLE: true }))();
145+
const Fp448 = /* @__PURE__ */ (() => Field(ed448_CURVE_p, { BITS: 448, isLE: true }))();
144146
const Fn448 = /* @__PURE__ */ (() => Field(ed448_CURVE.n, { BITS: 448, isLE: true }))();
145147

146148
// SHAKE256(dom4(phflag,context)||x, 114)
@@ -154,7 +156,7 @@ function dom4(data: Uint8Array, ctx: Uint8Array, phflag: boolean) {
154156
);
155157
}
156158
const ed448_eddsa_opts = { adjustScalarBytes, domain: dom4 };
157-
const ed448_Point = edwards(ed448_CURVE, { Fp, Fn, uvRatio });
159+
const ed448_Point = /* @__PURE__ */ edwards(ed448_CURVE, { Fp, Fn, uvRatio });
158160

159161
/**
160162
* ed448 EdDSA curve and methods.
@@ -168,20 +170,21 @@ const ed448_Point = edwards(ed448_CURVE, { Fp, Fn, uvRatio });
168170
* const isValid = ed448.verify(sig, msg, publicKey);
169171
* ```
170172
*/
171-
export const ed448: EdDSA = eddsa(ed448_Point, shake256_114, ed448_eddsa_opts);
173+
export const ed448: EdDSA = /* @__PURE__ */ eddsa(ed448_Point, shake256_114, ed448_eddsa_opts);
172174

173175
// There is no ed448ctx, since ed448 supports ctx by default
174176
/** Prehashed version of ed448. See {@link ed448} */
175-
export const ed448ph: EdDSA = /* @__PURE__ */ eddsa(ed448_Point, shake256_114, {
176-
...ed448_eddsa_opts,
177-
prehash: shake256_64,
178-
});
177+
export const ed448ph: EdDSA = /* @__PURE__ */ (() =>
178+
eddsa(ed448_Point, shake256_114, {
179+
...ed448_eddsa_opts,
180+
prehash: shake256_64,
181+
}))();
179182

180183
/**
181184
* E448 (NIST) != edwards448 used in ed448.
182185
* E448 is birationally equivalent to edwards448.
183186
*/
184-
export const E448: EdwardsPointCons = edwards(E448_CURVE);
187+
export const E448: EdwardsPointCons = /* @__PURE__ */ edwards(E448_CURVE);
185188

186189
/**
187190
* ECDH using curve448 aka x448.
@@ -195,7 +198,7 @@ export const E448: EdwardsPointCons = edwards(E448_CURVE);
195198
* ```
196199
*/
197200
export const x448: MontgomeryECDH = /* @__PURE__ */ (() => {
198-
const P = ed448_CURVE.p;
201+
const P = ed448_CURVE_p;
199202
return montgomery({
200203
P,
201204
type: 'x448',
@@ -209,7 +212,7 @@ export const x448: MontgomeryECDH = /* @__PURE__ */ (() => {
209212
})();
210213

211214
// Hash To Curve Elligator2 Map
212-
const ELL2_C1 = /* @__PURE__ */ (() => (Fp.ORDER - BigInt(3)) / BigInt(4))(); // 1. c1 = (q - 3) / 4 # Integer arithmetic
215+
const ELL2_C1 = /* @__PURE__ */ (() => (ed448_CURVE_p - BigInt(3)) / BigInt(4))(); // 1. c1 = (q - 3) / 4 # Integer arithmetic
213216
const ELL2_J = /* @__PURE__ */ BigInt(156326);
214217

215218
function map_to_curve_elligator2_curve448(u: bigint) {
@@ -290,7 +293,7 @@ export const ed448_hasher: H2CHasher<EdwardsPointCons> = /* @__PURE__ */ (() =>
290293
createHasher(ed448_Point, (scalars: bigint[]) => map_to_curve_elligator2_edwards448(scalars[0]), {
291294
DST: 'edwards448_XOF:SHAKE256_ELL2_RO_',
292295
encodeDST: 'edwards448_XOF:SHAKE256_ELL2_NU_',
293-
p: Fp.ORDER,
296+
p: ed448_CURVE_p,
294297
m: 1,
295298
k: 224,
296299
expand: 'xof',
@@ -318,9 +321,8 @@ const invertSqrt = (number: bigint) => uvRatio(_1n, number);
318321
* and [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-element-derivation-2).
319322
*/
320323
function calcElligatorDecafMap(r0: bigint): EdwardsPoint {
321-
const { d } = ed448_CURVE;
322-
const P = Fp.ORDER;
323-
const mod = (n: bigint) => Fp.create(n);
324+
const { d, p: P } = ed448_CURVE;
325+
const mod = (n: bigint) => Fp448.create(n);
324326

325327
const r = mod(-(r0 * r0)); // 1
326328
const u0 = mod(d * (r - _1n)); // 2
@@ -386,8 +388,7 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
386388

387389
static fromBytes(bytes: Uint8Array): _DecafPoint {
388390
abytes(bytes, 56);
389-
const { d } = ed448_CURVE;
390-
const P = Fp.ORDER;
391+
const { d, p: P } = ed448_CURVE;
391392
const mod = (n: bigint) => Fp448.create(n);
392393
const s = Fp448.fromBytes(bytes);
393394

@@ -429,8 +430,8 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
429430
*/
430431
toBytes(): Uint8Array {
431432
const { X, Z, T } = this.ep;
432-
const P = Fp.ORDER;
433-
const mod = (n: bigint) => Fp.create(n);
433+
const P = ed448_CURVE.p;
434+
const mod = (n: bigint) => Fp448.create(n);
434435
const u1 = mod(mod(X + T) * mod(X - T)); // 1
435436
const x2 = mod(X * X);
436437
const { value: invsqrt } = invertSqrt(mod(u1 * ONE_MINUS_D * x2)); // 2
@@ -451,7 +452,7 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
451452
const { X: X1, Y: Y1 } = this.ep;
452453
const { X: X2, Y: Y2 } = other.ep;
453454
// (x1 * y2 == y1 * x2)
454-
return Fp.create(X1 * Y2) === Fp.create(Y1 * X2);
455+
return Fp448.create(X1 * Y2) === Fp448.create(Y1 * X2);
455456
}
456457

457458
is0(): boolean {

src/nist.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ import {
1919

2020
// p = 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n - 1n
2121
// a = Fp256.create(BigInt('-3'));
22-
const p256_CURVE: WeierstrassOpts<bigint> = {
22+
const p256_CURVE: WeierstrassOpts<bigint> = /* @__PURE__ */ (() => ({
2323
p: BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'),
2424
n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
2525
h: BigInt(1),
2626
a: BigInt('0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc'),
2727
b: BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b'),
2828
Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
2929
Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
30-
};
30+
}))();
3131

3232
// p = 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
33-
const p384_CURVE: WeierstrassOpts<bigint> = {
33+
const p384_CURVE: WeierstrassOpts<bigint> = /* @__PURE__ */ (() => ({
3434
p: BigInt(
3535
'0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff'
3636
),
@@ -50,10 +50,10 @@ const p384_CURVE: WeierstrassOpts<bigint> = {
5050
Gy: BigInt(
5151
'0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'
5252
),
53-
};
53+
}))();
5454

5555
// p = 2n**521n - 1n
56-
const p521_CURVE: WeierstrassOpts<bigint> = {
56+
const p521_CURVE: WeierstrassOpts<bigint> = /* @__PURE__ */ (() => ({
5757
p: BigInt(
5858
'0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
5959
),
@@ -73,7 +73,7 @@ const p521_CURVE: WeierstrassOpts<bigint> = {
7373
Gy: BigInt(
7474
'0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'
7575
),
76-
};
76+
}))();
7777

7878
type SwuOpts = {
7979
A: bigint;
@@ -155,7 +155,7 @@ export const p384_oprf: OPRF = /* @__PURE__ */ (() =>
155155
hashToScalar: p384_hasher.hashToScalar,
156156
}))();
157157

158-
const Fn521 = /* @__PURE__ */ Field(p521_CURVE.n, { allowedLengths: [65, 66] });
158+
const Fn521 = /* @__PURE__ */ (() => Field(p521_CURVE.n, { allowedLengths: [65, 66] }))();
159159
const p521_Point = /* @__PURE__ */ weierstrass(p521_CURVE, { Fn: Fn521 });
160160
/** NIST P521 (aka secp521r1) curve, ECDSA and ECDH methods. */
161161
export const p521: ECDSA = /* @__PURE__ */ ecdsa(p521_Point, sha512);

0 commit comments

Comments
 (0)