Skip to content

Commit 437a581

Browse files
committed
Renamings
1 parent e8852fa commit 437a581

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ class Point {
185185
}
186186
toBytes(isCompressed = true) {
187187
const { x, y } = this.ok().aff(); // convert to 2d xy affine point
188-
const x32b = numberTo32b(x);
188+
const x32b = numTo32b(x);
189189
if (isCompressed)
190190
return concatBytes(getPrefix(y), x32b);
191-
return concatBytes(u8of(0x04), x32b, numberTo32b(y));
191+
return concatBytes(u8of(0x04), x32b, numTo32b(y));
192192
}
193193
// Can be commented-out:
194194
is0() { return this.equals(I); }
@@ -247,7 +247,7 @@ const hexToBytes = (hex) => {
247247
const bytesToNum = (b) => big('0x' + (bytesToHex(b) || '0'));
248248
const sliceBytesNum = (b, from, to) => bytesToNum(b.subarray(from, to));
249249
// Number to 32b. Must be 0 <= num < B256. validate, pad, to bytes
250-
const numberTo32b = (num) => hexToBytes(padh(arange(num, _0, B256), L2));
250+
const numTo32b = (num) => hexToBytes(padh(arange(num, _0, B256), L2));
251251
const concatBytes = (...arrs) => {
252252
const r = u8n(arrs.reduce((sum, a) => sum + abytes(a).length, 0)); // create u8a of summed length
253253
let pad = 0; // walk through each array,
@@ -295,7 +295,7 @@ class Signature {
295295
}
296296
toBytes() {
297297
const { r, s } = this;
298-
return concatBytes(numberTo32b(r), numberTo32b(s));
298+
return concatBytes(numTo32b(r), numTo32b(s));
299299
}
300300
// Can be commented-out:
301301
// 0.04kb
@@ -346,7 +346,7 @@ const prepSig = (msgh, priv, opts = optS) => {
346346
let { lowS, extraEntropy } = opts; // generates low-s sigs by default
347347
if (lowS == null)
348348
lowS = true; // RFC6979 3.2: we skip step A
349-
const i2o = numberTo32b; // int to octets
349+
const i2o = numTo32b; // int to octets
350350
const h1i = bits2int_modN(msgh); // msg bigint
351351
const h1o = i2o(h1i); // msg octets
352352
const d = toPrivScalar(priv); // validate private key, convert to bigint
@@ -518,7 +518,7 @@ const recoverPublicKey = (sig, msgh) => {
518518
const radj = recovery === 2 || recovery === 3 ? r + N : r; // q.x > n when rec was 2 or 3,
519519
afield(radj); // ensure q.x is still a field element
520520
const head = getPrefix(big(recovery)); // head is 0x02 or 0x03
521-
const Rb = concatBytes(head, numberTo32b(radj));
521+
const Rb = concatBytes(head, numTo32b(radj));
522522
const R = Point.fromBytes(Rb); // concat head + hex repr of r
523523
const ir = invert(radj, N); // r^-1
524524
const u1 = modN(-h * ir); // -hr^-1
@@ -554,14 +554,14 @@ const etc2 = {
554554
bytesToHex: bytesToHex,
555555
concatBytes: concatBytes,
556556
bytesToNumberBE: bytesToNum,
557-
numberToBytesBE: numberTo32b,
557+
numberToBytesBE: numTo32b,
558558
mod: M,
559559
invert: invert, // math utilities
560560
randomBytes: randomBytes,
561561
};
562562
const randomPrivateKey = () => {
563563
const num = M(bytesToNum(randomBytes(L + L / 2)), N - _1); // takes n+8 bytes
564-
return numberTo32b(num + _1); // returns (hash mod n-1)+1
564+
return numTo32b(num + _1); // returns (hash mod n-1)+1
565565
}; // FIPS 186 B.4.1.
566566
/** Curve-specific utilities for private keys. */
567567
const utils = {
@@ -664,12 +664,12 @@ const extractK = (rand) => {
664664
const k_ = modN(bytesToNum(rand)); // Let k' = int(rand) mod n
665665
if (k_ === _0)
666666
err('sign failed: k is zero'); // Fail if k' = 0.
667-
const { px, d } = extpubSchnorr(numberTo32b(k_)); // Let R = k'⋅G.
667+
const { px, d } = extpubSchnorr(numTo32b(k_)); // Let R = k'⋅G.
668668
return { rx: px, k: d };
669669
};
670670
// Common signature creation helper
671671
const createSigSchnorr = (k, px, e, d) => {
672-
return concatBytes(px, numberTo32b(modN(k + e * d)));
672+
return concatBytes(px, numTo32b(modN(k + e * d)));
673673
};
674674
const E_INVSIG = 'invalid signature produced';
675675
/**
@@ -679,7 +679,7 @@ const E_INVSIG = 'invalid signature produced';
679679
const signSchnorr = (message, privateKey, auxRand = randomBytes(L)) => {
680680
const { m, px, d, a } = prepSigSchnorr(message, privateKey, auxRand);
681681
const aux = taggedHash(T_AUX, a);
682-
const t = numberTo32b(d ^ bytesToNum(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
682+
const t = numTo32b(d ^ bytesToNum(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
683683
const rand = taggedHash(T_NONCE, t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
684684
const { rx, k } = extractK(rand);
685685
const e = challenge(rx, px, m); // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
@@ -691,7 +691,7 @@ const signSchnorr = (message, privateKey, auxRand = randomBytes(L)) => {
691691
const signAsyncSchnorr = async (message, privateKey, auxRand = randomBytes(L)) => {
692692
const { m, px, d, a } = prepSigSchnorr(message, privateKey, auxRand);
693693
const aux = await taggedHashAsync(T_AUX, a);
694-
const t = numberTo32b(d ^ bytesToNum(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
694+
const t = numTo32b(d ^ bytesToNum(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
695695
const rand = await taggedHashAsync(T_NONCE, t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
696696
const { rx, k } = extractK(rand);
697697
const e = await challengeAsync(rx, px, m); // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
@@ -723,7 +723,7 @@ const verifSchnorr = (signature, message, publicKey, sync = true) => {
723723
arange(r, _1, P);
724724
const s = sliceBytesNum(sig, L, L2); // Let s = int(sig[32:64]); fail if s ≥ n.
725725
arange(s, _1, N);
726-
const i = concatBytes(numberTo32b(r), pointToBytes(P_), msg);
726+
const i = concatBytes(numTo32b(r), pointToBytes(P_), msg);
727727
if (sync)
728728
return finishVerif(P_, r, s, challenge(i)); // int(challenge(bytes(r)||bytes(P)||m))%n
729729
return challengeAsync(i).then(e => finishVerif(P_, r, s, e));

index.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ class Point {
8282
const x = sliceBytesNum(tail, 0, L), len = bytes.length; // next 32 bytes are x coordinate
8383
if (len === (L+1) && [0x02, 0x03].includes(head)) { // Compressed 33-byte point
8484
let y = lift_x(x); // x³+b is right side of equation
85-
const evenY = isEven(y); // y² is equivalent left-side
86-
const evenH = isEven(big(head)); // y = √y²; there are two solutions: y, -y
85+
const evenY = isEven(y); // y² is equivalent left-side
86+
const evenH = isEven(big(head)); // y = √y²; there are two solutions: y, -y
8787
if (evenH !== evenY) y = M(-y); // determine proper solution
8888
p = new Point(x, y, _1); // create point
8989
}
@@ -163,9 +163,9 @@ class Point {
163163
}
164164
toBytes(isCompressed = true): Bytes { // Encode point to Uint8Array.
165165
const { x, y } = this.ok().aff(); // convert to 2d xy affine point
166-
const x32b = numberTo32b(x);
166+
const x32b = numTo32b(x);
167167
if (isCompressed) return concatBytes(getPrefix(y), x32b);
168-
return concatBytes(u8of(0x04), x32b, numberTo32b(y));
168+
return concatBytes(u8of(0x04), x32b, numTo32b(y));
169169
}
170170

171171
// Can be commented-out:
@@ -218,7 +218,7 @@ const hexToBytes = (hex: string): Bytes => {
218218
const bytesToNum = (b: Bytes): bigint => big('0x' + (bytesToHex(b) || '0'));
219219
const sliceBytesNum = (b: Bytes, from: number, to: number) => bytesToNum(b.subarray(from, to));
220220
// Number to 32b. Must be 0 <= num < B256. validate, pad, to bytes
221-
const numberTo32b = (num: bigint): Bytes => hexToBytes(padh(arange(num, _0, B256), L2));
221+
const numTo32b = (num: bigint): Bytes => hexToBytes(padh(arange(num, _0, B256), L2));
222222
const concatBytes = (...arrs: Bytes[]): Bytes => { // concatenate Uint8Array-s
223223
const r = u8n(arrs.reduce((sum, a) => sum + abytes(a).length, 0)); // create u8a of summed length
224224
let pad = 0; // walk through each array,
@@ -265,7 +265,7 @@ class Signature {
265265
}
266266
toBytes(): Bytes {
267267
const { r, s } = this;
268-
return concatBytes(numberTo32b(r), numberTo32b(s));
268+
return concatBytes(numTo32b(r), numTo32b(s));
269269
}
270270
// Can be commented-out:
271271
// 0.04kb
@@ -318,7 +318,7 @@ type BC = { seed: Bytes, k2sig : (kb: Bytes) => SignatureWithRecovery | undefine
318318
const prepSig = (msgh: Bytes, priv: Bytes, opts: OptS = optS): BC => {// prepare for RFC6979 sig generation
319319
let { lowS, extraEntropy } = opts; // generates low-s sigs by default
320320
if (lowS == null) lowS = true; // RFC6979 3.2: we skip step A
321-
const i2o = numberTo32b; // int to octets
321+
const i2o = numTo32b; // int to octets
322322
const h1i = bits2int_modN(msgh); // msg bigint
323323
const h1o = i2o(h1i); // msg octets
324324
const d = toPrivScalar(priv); // validate private key, convert to bigint
@@ -471,7 +471,7 @@ const recoverPublicKey = (sig: SignatureWithRecovery, msgh: Bytes): Point => {
471471
const radj = recovery === 2 || recovery === 3 ? r + N : r; // q.x > n when rec was 2 or 3,
472472
afield(radj); // ensure q.x is still a field element
473473
const head = getPrefix(big(recovery)); // head is 0x02 or 0x03
474-
const Rb = concatBytes(head, numberTo32b(radj));
474+
const Rb = concatBytes(head, numTo32b(radj));
475475
const R = Point.fromBytes(Rb); // concat head + hex repr of r
476476
const ir = invert(radj, N); // r^-1
477477
const u1 = modN(-h * ir); // -hr^-1
@@ -508,14 +508,14 @@ const etc2 = {
508508
bytesToHex: bytesToHex as (bytes: Bytes) => string,
509509
concatBytes: concatBytes as (...arrs: Bytes[]) => Bytes,
510510
bytesToNumberBE: bytesToNum as (a: Bytes) => bigint,
511-
numberToBytesBE: numberTo32b as (n: bigint) => Bytes,
511+
numberToBytesBE: numTo32b as (n: bigint) => Bytes,
512512
mod: M as (a: bigint, md?: bigint) => bigint,
513513
invert: invert as (num: bigint, md?: bigint) => bigint, // math utilities
514514
randomBytes: randomBytes as (len?: number) => Bytes,
515515
}
516516
const randomPrivateKey = (): Bytes => {
517517
const num = M(bytesToNum(randomBytes(L + L / 2)), N - _1); // takes n+8 bytes
518-
return numberTo32b(num + _1); // returns (hash mod n-1)+1
518+
return numTo32b(num + _1); // returns (hash mod n-1)+1
519519
}; // FIPS 186 B.4.1.
520520
/** Curve-specific utilities for private keys. */
521521
const utils = { // utilities
@@ -616,13 +616,13 @@ const prepSigSchnorr = (message: Bytes, privateKey: Bytes, auxRand: Bytes) => {
616616
const extractK = (rand: Bytes) => {
617617
const k_ = modN(bytesToNum(rand)); // Let k' = int(rand) mod n
618618
if (k_ === _0) err('sign failed: k is zero'); // Fail if k' = 0.
619-
const { px, d } = extpubSchnorr(numberTo32b(k_)); // Let R = k'⋅G.
619+
const { px, d } = extpubSchnorr(numTo32b(k_)); // Let R = k'⋅G.
620620
return { rx: px, k: d }
621621
}
622622

623623
// Common signature creation helper
624624
const createSigSchnorr = (k: bigint, px: Bytes, e: bigint, d: bigint): Bytes => {
625-
return concatBytes(px, numberTo32b(modN(k + e * d)));
625+
return concatBytes(px, numTo32b(modN(k + e * d)));
626626
}
627627

628628
const E_INVSIG = 'invalid signature produced';
@@ -637,7 +637,7 @@ const signSchnorr = (
637637
): Bytes => {
638638
const { m, px, d, a } = prepSigSchnorr(message, privateKey, auxRand);
639639
const aux = taggedHash(T_AUX, a);
640-
const t = numberTo32b(d ^ bytesToNum(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
640+
const t = numTo32b(d ^ bytesToNum(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
641641
const rand = taggedHash(T_NONCE, t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
642642
const { rx, k } = extractK(rand);
643643
const e = challenge(rx, px, m); // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
@@ -648,7 +648,7 @@ const signSchnorr = (
648648
const signAsyncSchnorr = async (message: Bytes, privateKey: Bytes, auxRand: Bytes = randomBytes(L)): Promise<Bytes> => {
649649
const { m, px, d, a } = prepSigSchnorr(message, privateKey, auxRand);
650650
const aux = await taggedHashAsync(T_AUX, a);
651-
const t = numberTo32b(d ^ bytesToNum(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
651+
const t = numTo32b(d ^ bytesToNum(aux)); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
652652
const rand = await taggedHashAsync(T_NONCE, t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
653653
const { rx, k } = extractK(rand);
654654
const e = await challengeAsync(rx, px, m); // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
@@ -680,7 +680,7 @@ const verifSchnorr = (signature: Bytes, message: Bytes, publicKey: Bytes, sync =
680680
arange(r, _1, P);
681681
const s = sliceBytesNum(sig, L, L2); // Let s = int(sig[32:64]); fail if s ≥ n.
682682
arange(s, _1, N);
683-
const i = concatBytes(numberTo32b(r), pointToBytes(P_), msg);
683+
const i = concatBytes(numTo32b(r), pointToBytes(P_), msg);
684684
if (sync) return finishVerif(P_, r, s, challenge(i)); // int(challenge(bytes(r)||bytes(P)||m))%n
685685
return challengeAsync(i).then(e => finishVerif(P_, r, s, e));
686686
} catch (error) {

0 commit comments

Comments
 (0)