Skip to content

Commit 3fdc3f6

Browse files
committed
Move precomputes up to weierstrass() and edwards().
1 parent 81f0192 commit 3fdc3f6

2 files changed

Lines changed: 8 additions & 13 deletions

File tree

src/abstract/edwards.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
isBytes,
1919
memoized,
2020
notImplemented,
21-
randomBytes as wcRandomBytes,
21+
randomBytes as randomBytesWeb,
2222
type FHash,
2323
type Hex,
2424
} from '../utils.ts';
@@ -531,7 +531,8 @@ export function edwards(params: EdwardsOpts, extraOpts: EdwardsExtraOpts = {}):
531531
return this.toBytes();
532532
}
533533
}
534-
const wnaf = new wNAF(Point, Fn.BYTES * 8); // Fn.BITS?
534+
const wnaf = new wNAF(Point, Fn.BITS);
535+
Point.BASE.precompute(8); // Enable precomputes. Slows down first publicKey computation by 20ms.
535536
return Point;
536537
}
537538

@@ -665,7 +666,7 @@ export function eddsa(Point: EdwardsPointCons, cHash: FHash, eddsaOpts: EdDSAOpt
665666
const { prehash } = eddsaOpts;
666667
const { BASE, Fp, Fn } = Point;
667668

668-
const randomBytes = eddsaOpts.randomBytes || wcRandomBytes;
669+
const randomBytes = eddsaOpts.randomBytes || randomBytesWeb;
669670
const adjustScalarBytes = eddsaOpts.adjustScalarBytes || ((bytes: Uint8Array) => bytes);
670671
const domain =
671672
eddsaOpts.domain ||
@@ -765,8 +766,6 @@ export function eddsa(Point: EdwardsPointCons, cHash: FHash, eddsaOpts: EdDSAOpt
765766
return RkA.subtract(SB).clearCofactor().is0();
766767
}
767768

768-
BASE.precompute(8); // Enable precomputes. Slows down first publicKey computation by 20ms.
769-
770769
const _size = Fp.BYTES; // 32 for ed25519, 57 for ed448
771770
const lengths = {
772771
secret: _size,
@@ -781,11 +780,9 @@ export function eddsa(Point: EdwardsPointCons, cHash: FHash, eddsaOpts: EdDSAOpt
781780
const secretKey = utils.randomSecretKey(seed);
782781
return { secretKey, publicKey: getPublicKey(secretKey) };
783782
}
784-
785783
function isValidSecretKey(key: Uint8Array): boolean {
786784
return isBytes(key) && key.length === Fn.BYTES;
787785
}
788-
789786
function isValidPublicKey(key: Uint8Array, zip215?: boolean): boolean {
790787
try {
791788
return !!Point.fromBytes(key, zip215);

src/abstract/weierstrass.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import {
4444
isBytes,
4545
memoized,
4646
numberToHexUnpadded,
47-
randomBytes as wcRandomBytes,
47+
randomBytes as randomBytesWeb,
4848
type CHash,
4949
type Hex,
5050
type PrivKey,
@@ -988,6 +988,7 @@ export function weierstrassN<T>(
988988
}
989989
const bits = Fn.BITS;
990990
const wnaf = new wNAF(Point, extraOpts.endo ? Math.ceil(bits / 2) : bits);
991+
Point.BASE.precompute(8); // Enable precomputes. Slows down first publicKey computation by 20ms.
991992
return Point;
992993
}
993994

@@ -1188,7 +1189,7 @@ export function ecdh(
11881189
ecdhOpts: { randomBytes?: (bytesLength?: number) => Uint8Array } = {}
11891190
): ECDH {
11901191
const { Fn } = Point;
1191-
const randomBytes_ = ecdhOpts.randomBytes || wcRandomBytes;
1192+
const randomBytes_ = ecdhOpts.randomBytes || randomBytesWeb;
11921193
const lengths = Object.assign(getWLengths(Point.Fp, Fn), { seed: getMinHashLength(Fn.ORDER) });
11931194

11941195
function isValidSecretKey(secretKey: PrivKey) {
@@ -1311,7 +1312,7 @@ export function ecdsa(
13111312
}
13121313
);
13131314

1314-
const randomBytes = ecdsaOpts.randomBytes || wcRandomBytes;
1315+
const randomBytes = ecdsaOpts.randomBytes || randomBytesWeb;
13151316
const hmac: HmacFnSync =
13161317
ecdsaOpts.hmac ||
13171318
(((key, ...msgs) => nobleHmac(hash, key, concatBytes(...msgs))) satisfies HmacFnSync);
@@ -1572,9 +1573,6 @@ export function ecdsa(
15721573
return sig;
15731574
}
15741575

1575-
// Enable precomputes. Slows down first publicKey computation by 20ms.
1576-
Point.BASE.precompute(8);
1577-
15781576
function tryParsingSig(sg: Hex | SignatureLike) {
15791577
// Try to deduce format
15801578
let sig: Signature | undefined = undefined;

0 commit comments

Comments
 (0)