ec: getEncoded now uses Buffer API
This commit is contained in:
parent
db3ffe58d1
commit
ccca6989b5
3 changed files with 20 additions and 24 deletions
36
src/ec.js
36
src/ec.js
|
@ -304,31 +304,27 @@ ECFieldElementFp.prototype.getByteLength = function () {
|
||||||
};
|
};
|
||||||
|
|
||||||
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 {
|
} else {
|
||||||
// Compressed uneven pubkey
|
buffer = new Buffer(65)
|
||||||
// M = 03 || X
|
buffer.writeUInt8(0x04, 0)
|
||||||
enc.unshift(0x03);
|
|
||||||
|
y.toBuffer(32).copy(buffer, 33)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// Uncompressed pubkey
|
x.toBuffer(32).copy(buffer, 1)
|
||||||
// M = 04 || X || Y
|
|
||||||
enc.unshift(0x04);
|
return buffer
|
||||||
enc = enc.concat(integerToBytes(y, 32));
|
|
||||||
}
|
}
|
||||||
return enc;
|
|
||||||
};
|
|
||||||
|
|
||||||
ECPointFp.decodeFrom = function (curve, enc) {
|
ECPointFp.decodeFrom = function (curve, enc) {
|
||||||
var type = enc[0];
|
var type = enc[0];
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
|
@ -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…
Add table
Reference in a new issue