Added BIP32 support

This commit is contained in:
vub 2013-11-18 23:47:56 -05:00
parent aa22b9d89e
commit 5e2a9f0c26
16 changed files with 546 additions and 313 deletions
src/jsbn

View file

@ -2,7 +2,8 @@
// Ported loosely from BouncyCastle's Java EC code
// Only Fp curves implemented for now
var BigInteger = require('./jsbn');
var BigInteger = require('./jsbn'),
sec = require('./sec');
// ----------------
// ECFieldElementFp
@ -360,24 +361,38 @@ 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);
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 Bitcoin.BigInteger('7')).mod(p),
pPlus1Over4 = p.add(new Bitcoin.BigInteger('1'))
.divide(new Bitcoin.BigInteger('4')),
y = xCubedPlus7.modPow(pPlus1Over4,p);
if (y.mod(new Bitcoin.BigInteger('2')).toString() != ''+(type % 2)) {
y = p.subtract(y)
}
}
// 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);
// 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) {