Browse Source

ec: getEncoded now uses Buffer API

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
ccca6989b5
  1. 40
      src/ec.js
  2. 2
      src/ecpubkey.js
  3. 2
      test/ec.js

40
src/ec.js

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

2
src/ecpubkey.js

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

2
test/ec.js

@ -35,7 +35,7 @@ describe('ec', function() {
curve.fromBigInteger(new BigInteger(f.y)) 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) assert.equal(encoded.toString('hex'), f.hex)
}) })
}) })

Loading…
Cancel
Save