ec: getEncoded now uses Buffer API

This commit is contained in:
Daniel Cousens 2014-05-17 00:33:33 +10:00
parent db3ffe58d1
commit ccca6989b5
3 changed files with 20 additions and 24 deletions

View file

@ -303,32 +303,28 @@ ECFieldElementFp.prototype.getByteLength = function () {
return Math.floor((this.toBigInteger().bitLength() + 7) / 8);
};
ECPointFp.prototype.getEncoded = function (compressed) {
var x = this.getX().toBigInteger();
var y = this.getY().toBigInteger();
// Get value as a 32-byte Buffer
// Fixed length based on a patch by bitaddress.org and Casascius
var enc = integerToBytes(x, 32);
ECPointFp.prototype.getEncoded = function(compressed) {
var x = this.getX().toBigInteger()
var y = this.getY().toBigInteger()
var buffer
// 0x02/0x03 | X
if (compressed) {
if (y.isEven()) {
// Compressed even pubkey
// M = 02 || X
enc.unshift(0x02);
} else {
// Compressed uneven pubkey
// M = 03 || X
enc.unshift(0x03);
}
buffer = new Buffer(33)
buffer.writeUInt8(y.isEven() ? 0x02 : 0x03, 0)
// 0x04 | X | Y
} else {
// Uncompressed pubkey
// M = 04 || X || Y
enc.unshift(0x04);
enc = enc.concat(integerToBytes(y, 32));
buffer = new Buffer(65)
buffer.writeUInt8(0x04, 0)
y.toBuffer(32).copy(buffer, 33)
}
return enc;
};
x.toBuffer(32).copy(buffer, 1)
return buffer
}
ECPointFp.decodeFrom = function (curve, enc) {
var type = enc[0];

View file

@ -48,7 +48,7 @@ ECPubKey.prototype.verify = function(hash, sig) {
// Export functions
ECPubKey.prototype.toBuffer = function() {
return new Buffer(this.Q.getEncoded(this.compressed))
return this.Q.getEncoded(this.compressed)
}
ECPubKey.prototype.toHex = function() {

View file

@ -35,7 +35,7 @@ describe('ec', function() {
curve.fromBigInteger(new BigInteger(f.y))
)
var encoded = new Buffer(Q.getEncoded(f.compressed))
var encoded = Q.getEncoded(f.compressed)
assert.equal(encoded.toString('hex'), f.hex)
})
})