Skip to content

Commit 7fda75e

Browse files
committed
Rename back
1 parent 77413a6 commit 7fda75e

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

index.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Point {
6060
au8(bytes);
6161
let p: Point | undefined = undefined;
6262
const head = bytes[0], tail = bytes.subarray(1); // first byte is prefix, rest is data
63-
const x = slcNum(tail, 0, L), len = bytes.length; // next 32 bytes are x coordinate
63+
const x = slc(tail, 0, L), len = bytes.length; // next 32 bytes are x coordinate
6464
if (len === (L + 1) && [0x02, 0x03].includes(head)) { // compressed points: 33b, start
6565
// afield(x); // with byte 0x02 or 0x03. check 1<=x<P.
6666
let y = lift(x); // x³ + ax + b is right side of equation
@@ -69,7 +69,7 @@ class Point {
6969
if (headOdd !== isYOdd) y = M(-y); // determine proper solution
7070
p = new Point(x, y, _1); // create point
7171
} // Uncompressed points: 65b, start with 0x04
72-
if (len === (L2 + 1) && head === 0x04) p = new Point(x, slcNum(tail, L, L2), _1);
72+
if (len === (L2 + 1) && head === 0x04) p = new Point(x, slc(tail, L, L2), _1);
7373
return p ? p.ok() : err('Point invalid: not on curve'); // Verify the result
7474
}
7575
/** Equality check: compare points P&Q. */
@@ -183,9 +183,9 @@ const h2b = (hex: string): Bytes => { // hex to bytes
183183
return array;
184184
};
185185
const b2n = (b: Bytes): bigint => BigInt('0x' + (b2h(b) || '0')); // bytes to number
186-
const slcNum = (b: Bytes, from: number, to: number) => b2n(b.subarray(from, to)); // slice bytes num
187-
const numTo32b = (n: bigint): Bytes => h2b(padh(arange(n, _0, B256), L2)); // number to 32b. Must be 0 <= num < B256
188-
const n2h = (num: bigint): string => b2h(numTo32b(num)); // number to 32b hex
186+
const slc = (b: Bytes, from: number, to: number) => b2n(b.subarray(from, to)); // slice bytes num
187+
const n2b = (n: bigint): Bytes => h2b(padh(arange(n, _0, B256), L2)); // number to 32b. Must be 0 <= num < B256
188+
const n2h = (num: bigint): string => b2h(n2b(num)); // number to 32b hex
189189
const concatB = (...arrs: Bytes[]): Bytes => { // concatenate Uint8Array-s
190190
const r = u8n(arrs.reduce((sum, a) => sum + au8(a).length, 0)); // create u8a of summed length
191191
let pad = 0; // walk through each array,
@@ -202,14 +202,14 @@ const inv = (num: bigint, md: bigint): bigint => { // modular inversion
202202
}
203203
return b === _1 ? M(x, md) : err('no inverse'); // b is gcd at this point
204204
};
205-
const scalar = (pr: Bytes): bigint => { // normalize private key to bigint
205+
const normPriv = (pr: Bytes): bigint => { // normalize private key to bigint
206206
let num = b2n(au8(pr, L)); // convert to bigint when bytes
207207
return arange(num, _1, N, 'private key invalid 3'); // check if bigint is in range
208208
};
209209
const highS = (n: bigint): boolean => n > (N >> _1); // if a number is bigger than CURVE.n/2
210210
/** Creates 33/65-byte public key from 32-byte private key. */
211211
const getPublicKey = (privKey: Bytes, isCompressed = true): Bytes => {
212-
return G.mul(scalar(privKey)).toBytes(isCompressed);
212+
return G.mul(normPriv(privKey)).toBytes(isCompressed);
213213
}
214214
/** ECDSA Signature class. Supports only compact 64-byte representation, not DER. */
215215
class Signature {
@@ -232,7 +232,7 @@ const bits2int = (bytes: Bytes): bigint => { // RFC6979: ensure ECDSA
232232
const bits2int_modN = (bytes: Bytes): bigint => { // int2octets can't be used; pads small msgs
233233
return modN(bits2int(au8(bytes))); // with 0: BAD for trunc as per RFC vectors
234234
};
235-
const i2o = (num: bigint): Bytes => numTo32b(num); // int to octets
235+
const i2o = (num: bigint): Bytes => n2b(num); // int to octets
236236
declare const globalThis: Record<string, any> | undefined; // Typescript symbol present in browsers
237237
const cr = () => // We support: 1) browsers 2) node.js 19+ 3) deno, other envs with crypto
238238
typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
@@ -254,7 +254,7 @@ const prepSig = (msgh: Bytes, priv: Bytes, opts: OptS = optS): BC => {// prepare
254254
if (lowS == null) lowS = true; // RFC6979 3.2: we skip step A
255255
const h1i = bits2int_modN(msgh); // msg bigint
256256
const h1o = i2o(h1i); // msg octets
257-
const d = scalar(priv); // validate private key, convert to bigint
257+
const d = normPriv(priv); // validate private key, convert to bigint
258258
const seed = [i2o(d), h1o]; // Step D of RFC6979 3.2
259259
// RFC6979 3.6: additional k' (optional)
260260
if (extraEntropy) { // K = HMAC_K(V || 0x00 || int2octets(x) || bits2octets(h1) || k')
@@ -378,7 +378,7 @@ const verify = (sig: Bytes, msgh: Bytes, pub: Bytes, opts: OptV = optV): boolean
378378
let { lowS } = opts; // ECDSA signature verification
379379
if (lowS == null) lowS = true; // Default lowS=true
380380
let h: bigint, P: Point; // secg.org/sec1-v2.pdf 4.1.4
381-
let { r, s } = new Signature(slcNum(sig, 0, L), slcNum(sig, L, L2)) // throw error when DER is suspected now.
381+
let { r, s } = new Signature(slc(sig, 0, L), slc(sig, L, L2)) // throw error when DER is suspected now.
382382
try {
383383
h = bits2int_modN(msgh); // Truncate hash
384384
P = Point.from(pub); // Validate public key
@@ -423,7 +423,7 @@ const recoverPublicKey = (point: SignatureWithRecovery, msgh: Bytes): Point => {
423423
* @returns public key C
424424
*/
425425
const getSharedSecret = (privA: Bytes, pubB: Bytes, isCompressed = true): Bytes => {
426-
return Point.from(pubB).mul(scalar(privA)).toBytes(isCompressed); // ECDH
426+
return Point.from(pubB).mul(normPriv(privA)).toBytes(isCompressed); // ECDH
427427
};
428428
const randomBytes = (len = L): Bytes => { // CSPRNG (random number generator)
429429
const crypto = cr(); // Must be shimmed in node.js <= 18 to prevent error. See README.
@@ -450,18 +450,18 @@ const etc2 = {
450450
bytesToHex: b2h as (bytes: Bytes) => string,
451451
concatBytes: concatB as (...arrs: Bytes[]) => Bytes,
452452
bytesToNumberBE: b2n as (a: Bytes) => bigint,
453-
numberToBytesBE: numTo32b as (n: bigint) => Bytes,
453+
numberToBytesBE: n2b as (n: bigint) => Bytes,
454454
mod: M as (a: bigint, md?: bigint) => bigint,
455455
invert: inv as (num: bigint, md?: bigint) => bigint, // math utilities
456456
randomBytes: randomBytes as (len?: number) => Bytes,
457457
}
458458
const randomPrivateKey = (): Bytes => {
459459
const num = M(b2n(randomBytes(L + L / 2)), N - _1); // takes n+8 bytes
460-
return numTo32b(num + _1); // returns (hash mod n-1)+1
460+
return n2b(num + _1); // returns (hash mod n-1)+1
461461
}; // FIPS 186 B.4.1.
462462
/** Curve-specific utilities for private keys. */
463463
const utils = { // utilities
464-
isValidPrivateKey: (key: Bytes): boolean => { try { return !!scalar(key); } catch (e) { return false; } },
464+
isValidPrivateKey: (key: Bytes): boolean => { try { return !!normPriv(key); } catch (e) { return false; } },
465465
randomPrivateKey: randomPrivateKey as () => Bytes,
466466
// precompute: (w=8, p: Point = G): Point => { p.multiply(3n); w; return p; }, // no-op
467467
};
@@ -529,7 +529,7 @@ const taggedHashAsync = async (tag: string, ...messages: Bytes[]): Promise<Bytes
529529
const pointToBytes = (point: Point): Uint8Array<ArrayBuffer> => point.toBytes(true).slice(1);
530530
// Calculate point, scalar and bytes
531531
const extpubSchnorr = (priv: Bytes) => {
532-
const d_ = scalar(priv); // same method executed in fromPrivateKey
532+
const d_ = normPriv(priv); // same method executed in fromPrivateKey
533533
const p = G.mul(d_); // P = d'⋅G; 0 < d' < n check is done inside
534534
const d = isEvenB(p.aff().y) ? d_ : modN(-d_);
535535
const px = pointToBytes(p);
@@ -565,15 +565,15 @@ const prepSigSchnorr = (message: Bytes, privateKey: Bytes, auxRand: Bytes) => {
565565
const extractK = (rand: Bytes) => {
566566
const k_ = modN(b2n(rand)); // Let k' = int(rand) mod n
567567
if (k_ === _0) err('sign failed: k is zero'); // Fail if k' = 0.
568-
const { px, d } = extpubSchnorr(numTo32b(k_)); // Let R = k'⋅G.
568+
const { px, d } = extpubSchnorr(n2b(k_)); // Let R = k'⋅G.
569569
return { rx: px, k: d }
570570
}
571571

572572
// Common signature creation helper
573573
const createSigSchnorr = (k: bigint, px: Bytes, e: bigint, d: bigint): Bytes => {
574574
const sig = u8n(L2);
575575
sig.set(px, 0);
576-
sig.set(numTo32b(modN(k + e * d)), L);
576+
sig.set(n2b(modN(k + e * d)), L);
577577
return sig;
578578
}
579579

@@ -589,7 +589,7 @@ const signSchnorr = (
589589
): Bytes => {
590590
const { m, px, d, a } = prepSigSchnorr(message, privateKey, auxRand);
591591
const aux = taggedHash(T_AUX, a);
592-
const t = numTo32b(d ^ b2n(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
592+
const t = n2b(d ^ b2n(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
593593
const rand = taggedHash(T_NONCE, t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
594594
const { rx, k } = extractK(rand);
595595
const e = challenge(rx, px, m); // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
@@ -600,7 +600,7 @@ const signSchnorr = (
600600
const signAsyncSchnorr = async (message: Bytes, privateKey: Bytes, auxRand: Bytes = randomBytes(L)): Promise<Bytes> => {
601601
const { m, px, d, a } = prepSigSchnorr(message, privateKey, auxRand);
602602
const aux = await taggedHashAsync(T_AUX, a);
603-
const t = numTo32b(d ^ b2n(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
603+
const t = n2b(d ^ b2n(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
604604
const rand = await taggedHashAsync(T_NONCE, t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
605605
const { rx, k } = extractK(rand);
606606
const e = await challengeAsync(rx, px, m); // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
@@ -628,11 +628,11 @@ const verifSchnorr = (signature: Bytes, message: Bytes, publicKey: Bytes, sync =
628628
// Return the unique point P such that x(P) = x and
629629
const P_ = new Point(x, isEvenB(y) ? y : M(-y), _1).ok(); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
630630
// P = lift_x(int(pk)); fail if that fails
631-
const r = slcNum(sig, 0, L); // Let r = int(sig[0:32]); fail if r ≥ p.
631+
const r = slc(sig, 0, L); // Let r = int(sig[0:32]); fail if r ≥ p.
632632
arange(r, _1, P);
633-
const s = slcNum(sig, L, L2); // Let s = int(sig[32:64]); fail if s ≥ n.
633+
const s = slc(sig, L, L2); // Let s = int(sig[32:64]); fail if s ≥ n.
634634
arange(s, _1, N);
635-
const i = concatB(numTo32b(r), pointToBytes(P_), msg);
635+
const i = concatB(n2b(r), pointToBytes(P_), msg);
636636
if (sync) return finishVerif(P_, r, s, challenge(i)); // int(challenge(bytes(r)||bytes(P)||m))%n
637637
return challengeAsync(i).then(e => finishVerif(P_, r, s, e));
638638
} catch (error) {

0 commit comments

Comments
 (0)