Skip to content

Commit

Permalink
support for compressed pub key import
Browse files Browse the repository at this point in the history
  • Loading branch information
matiu committed Mar 4, 2014
1 parent 2ae2fcb commit 5acb847
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions browser/bitcoinjs-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2286,26 +2286,37 @@ ECPointFp.prototype.getEncoded = function (compressed) {
return enc;
};

ECPointFp.decodeFrom = function (curve, enc) {
ECPointFp.decodeFrom = function (ecparams, enc) {
var type = enc[0];
var dataLen = enc.length-1;

// Extract x and y as byte arrays
var xBa = enc.slice(1, 1 + dataLen/2);
var yBa = enc.slice(1 + dataLen/2, 1 + dataLen);

// Prepend zero byte to prevent interpretation as negative integer
xBa.unshift(0);
yBa.unshift(0);

// Convert to BigIntegers
var x = new BigInteger(xBa);
var y = new BigInteger(yBa);
if (type == 4) {
var xBa = enc.slice(1, 1 + dataLen/2),
yBa = enc.slice(1 + dataLen/2, 1 + dataLen),
x = BigInteger.fromByteArrayUnsigned(xBa),
y = BigInteger.fromByteArrayUnsigned(yBa);
}
else {
var xBa = enc.slice(1),
x = BigInteger.fromByteArrayUnsigned(xBa),
p = ecparams.getQ(),
xCubedPlus7 = x.multiply(x).multiply(x).add(new BigInteger('7')).mod(p),
pPlus1Over4 = p.add(new BigInteger('1'))
.divide(new BigInteger('4')),
y = xCubedPlus7.modPow(pPlus1Over4,p);
if (y.mod(new BigInteger('2')).toString() != ''+(type % 2)) {
y = p.subtract(y)
}
}

// Return point
return new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y));
return new ECPointFp(ecparams,
ecparams.fromBigInteger(x),
ecparams.fromBigInteger(y));
};


ECPointFp.prototype.add2D = function (b) {
if(this.isInfinity()) return b;
if(b.isInfinity()) return this;
Expand Down

0 comments on commit 5acb847

Please sign in to comment.