@@ -82,8 +82,8 @@ class Point {
82
82
const x = sliceBytesNum ( tail , 0 , L ) , len = bytes . length ; // next 32 bytes are x coordinate
83
83
if ( len === ( L + 1 ) && [ 0x02 , 0x03 ] . includes ( head ) ) { // Compressed 33-byte point
84
84
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
87
87
if ( evenH !== evenY ) y = M ( - y ) ; // determine proper solution
88
88
p = new Point ( x , y , _1 ) ; // create point
89
89
}
@@ -163,9 +163,9 @@ class Point {
163
163
}
164
164
toBytes ( isCompressed = true ) : Bytes { // Encode point to Uint8Array.
165
165
const { x, y } = this . ok ( ) . aff ( ) ; // convert to 2d xy affine point
166
- const x32b = numberTo32b ( x ) ;
166
+ const x32b = numTo32b ( x ) ;
167
167
if ( isCompressed ) return concatBytes ( getPrefix ( y ) , x32b ) ;
168
- return concatBytes ( u8of ( 0x04 ) , x32b , numberTo32b ( y ) ) ;
168
+ return concatBytes ( u8of ( 0x04 ) , x32b , numTo32b ( y ) ) ;
169
169
}
170
170
171
171
// Can be commented-out:
@@ -218,7 +218,7 @@ const hexToBytes = (hex: string): Bytes => {
218
218
const bytesToNum = ( b : Bytes ) : bigint => big ( '0x' + ( bytesToHex ( b ) || '0' ) ) ;
219
219
const sliceBytesNum = ( b : Bytes , from : number , to : number ) => bytesToNum ( b . subarray ( from , to ) ) ;
220
220
// 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 ) ) ;
222
222
const concatBytes = ( ...arrs : Bytes [ ] ) : Bytes => { // concatenate Uint8Array-s
223
223
const r = u8n ( arrs . reduce ( ( sum , a ) => sum + abytes ( a ) . length , 0 ) ) ; // create u8a of summed length
224
224
let pad = 0 ; // walk through each array,
@@ -265,7 +265,7 @@ class Signature {
265
265
}
266
266
toBytes ( ) : Bytes {
267
267
const { r, s } = this ;
268
- return concatBytes ( numberTo32b ( r ) , numberTo32b ( s ) ) ;
268
+ return concatBytes ( numTo32b ( r ) , numTo32b ( s ) ) ;
269
269
}
270
270
// Can be commented-out:
271
271
// 0.04kb
@@ -318,7 +318,7 @@ type BC = { seed: Bytes, k2sig : (kb: Bytes) => SignatureWithRecovery | undefine
318
318
const prepSig = ( msgh : Bytes , priv : Bytes , opts : OptS = optS ) : BC => { // prepare for RFC6979 sig generation
319
319
let { lowS, extraEntropy } = opts ; // generates low-s sigs by default
320
320
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
322
322
const h1i = bits2int_modN ( msgh ) ; // msg bigint
323
323
const h1o = i2o ( h1i ) ; // msg octets
324
324
const d = toPrivScalar ( priv ) ; // validate private key, convert to bigint
@@ -471,7 +471,7 @@ const recoverPublicKey = (sig: SignatureWithRecovery, msgh: Bytes): Point => {
471
471
const radj = recovery === 2 || recovery === 3 ? r + N : r ; // q.x > n when rec was 2 or 3,
472
472
afield ( radj ) ; // ensure q.x is still a field element
473
473
const head = getPrefix ( big ( recovery ) ) ; // head is 0x02 or 0x03
474
- const Rb = concatBytes ( head , numberTo32b ( radj ) ) ;
474
+ const Rb = concatBytes ( head , numTo32b ( radj ) ) ;
475
475
const R = Point . fromBytes ( Rb ) ; // concat head + hex repr of r
476
476
const ir = invert ( radj , N ) ; // r^-1
477
477
const u1 = modN ( - h * ir ) ; // -hr^-1
@@ -508,14 +508,14 @@ const etc2 = {
508
508
bytesToHex : bytesToHex as ( bytes : Bytes ) => string ,
509
509
concatBytes : concatBytes as ( ...arrs : Bytes [ ] ) => Bytes ,
510
510
bytesToNumberBE : bytesToNum as ( a : Bytes ) => bigint ,
511
- numberToBytesBE : numberTo32b as ( n : bigint ) => Bytes ,
511
+ numberToBytesBE : numTo32b as ( n : bigint ) => Bytes ,
512
512
mod : M as ( a : bigint , md ?: bigint ) => bigint ,
513
513
invert : invert as ( num : bigint , md ?: bigint ) => bigint , // math utilities
514
514
randomBytes : randomBytes as ( len ?: number ) => Bytes ,
515
515
}
516
516
const randomPrivateKey = ( ) : Bytes => {
517
517
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
519
519
} ; // FIPS 186 B.4.1.
520
520
/** Curve-specific utilities for private keys. */
521
521
const utils = { // utilities
@@ -616,13 +616,13 @@ const prepSigSchnorr = (message: Bytes, privateKey: Bytes, auxRand: Bytes) => {
616
616
const extractK = ( rand : Bytes ) => {
617
617
const k_ = modN ( bytesToNum ( rand ) ) ; // Let k' = int(rand) mod n
618
618
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.
620
620
return { rx : px , k : d }
621
621
}
622
622
623
623
// Common signature creation helper
624
624
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 ) ) ) ;
626
626
}
627
627
628
628
const E_INVSIG = 'invalid signature produced' ;
@@ -637,7 +637,7 @@ const signSchnorr = (
637
637
) : Bytes => {
638
638
const { m, px, d, a } = prepSigSchnorr ( message , privateKey , auxRand ) ;
639
639
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)
641
641
const rand = taggedHash ( T_NONCE , t , px , m ) ; // Let rand = hash/nonce(t || bytes(P) || m)
642
642
const { rx, k } = extractK ( rand ) ;
643
643
const e = challenge ( rx , px , m ) ; // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
@@ -648,7 +648,7 @@ const signSchnorr = (
648
648
const signAsyncSchnorr = async ( message : Bytes , privateKey : Bytes , auxRand : Bytes = randomBytes ( L ) ) : Promise < Bytes > => {
649
649
const { m, px, d, a } = prepSigSchnorr ( message , privateKey , auxRand ) ;
650
650
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)
652
652
const rand = await taggedHashAsync ( T_NONCE , t , px , m ) ; // Let rand = hash/nonce(t || bytes(P) || m)
653
653
const { rx, k } = extractK ( rand ) ;
654
654
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 =
680
680
arange ( r , _1 , P ) ;
681
681
const s = sliceBytesNum ( sig , L , L2 ) ; // Let s = int(sig[32:64]); fail if s ≥ n.
682
682
arange ( s , _1 , N ) ;
683
- const i = concatBytes ( numberTo32b ( r ) , pointToBytes ( P_ ) , msg ) ;
683
+ const i = concatBytes ( numTo32b ( r ) , pointToBytes ( P_ ) , msg ) ;
684
684
if ( sync ) return finishVerif ( P_ , r , s , challenge ( i ) ) ; // int(challenge(bytes(r)||bytes(P)||m))%n
685
685
return challengeAsync ( i ) . then ( e => finishVerif ( P_ , r , s , e ) ) ;
686
686
} catch ( error ) {
0 commit comments