@@ -2,10 +2,23 @@ import { Network } from './networks';
2
2
import * as NETWORKS from './networks' ;
3
3
import * as types from './types' ;
4
4
const ecc = require ( 'tiny-secp256k1' ) ;
5
+ const BN = require ( 'bn.js' ) ;
5
6
const randomBytes = require ( 'randombytes' ) ;
6
7
const typeforce = require ( 'typeforce' ) ;
7
8
const wif = require ( 'wif' ) ;
8
9
10
+ const EC_P = new BN (
11
+ Buffer . from (
12
+ 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f' ,
13
+ 'hex' ,
14
+ ) ,
15
+ ) ;
16
+ const EC_P_REDUCTION = BN . red ( EC_P ) ;
17
+ const EC_P_QUADRATIC_RESIDUE = EC_P . addn ( 1 ) . divn ( 4 ) ;
18
+ const BN_2 = new BN ( 2 ) ;
19
+ const BN_3 = new BN ( 3 ) ;
20
+ const BN_7 = new BN ( 7 ) ;
21
+
9
22
const isOptions = typeforce . maybe (
10
23
typeforce . compile ( {
11
24
compressed : types . maybe ( types . Boolean ) ,
@@ -116,6 +129,28 @@ function fromPublicKey(buffer: Buffer, options?: ECPairOptions): ECPair {
116
129
return new ECPair ( undefined , buffer , options ) ;
117
130
}
118
131
132
+ function liftX ( buffer : Buffer ) : Buffer | null {
133
+ typeforce ( types . Buffer256bit , buffer ) ;
134
+ const x = new BN ( buffer ) ;
135
+ if ( x . gte ( EC_P ) ) return null ;
136
+ const xRed = x . toRed ( EC_P_REDUCTION ) ;
137
+ const ySq = xRed
138
+ . redPow ( BN_3 )
139
+ . add ( BN_7 )
140
+ . mod ( EC_P ) ;
141
+
142
+ const y = ySq . redPow ( EC_P_QUADRATIC_RESIDUE ) ;
143
+
144
+ if ( ! ySq . eq ( y . redPow ( BN_2 ) ) ) {
145
+ return null ;
146
+ }
147
+ const y1 = ( y & 1 ) === 0 ? y : EC_P . sub ( y ) ;
148
+ return Buffer . concat ( [
149
+ Buffer . from ( x . toBuffer ( 'be' ) ) ,
150
+ Buffer . from ( y1 . toBuffer ( 'be' ) ) ,
151
+ ] ) ;
152
+ }
153
+
119
154
function fromWIF ( wifString : string , network ?: Network | Network [ ] ) : ECPair {
120
155
const decoded = wif . decode ( wifString ) ;
121
156
const version = decoded . version ;
@@ -158,4 +193,4 @@ function makeRandom(options?: ECPairOptions): ECPair {
158
193
return fromPrivateKey ( d , options ) ;
159
194
}
160
195
161
- export { makeRandom , fromPrivateKey , fromPublicKey , fromWIF } ;
196
+ export { makeRandom , fromPrivateKey , fromPublicKey , fromWIF , liftX } ;
0 commit comments