Skip to content

Commit

Permalink
base: support hybrid format
Browse files Browse the repository at this point in the history
Fix: #91
  • Loading branch information
indutny committed Jun 1, 2016
1 parent 6a8ef14 commit 8db7448
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/elliptic/curve/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,19 @@ BaseCurve.prototype.decodePoint = function decodePoint(bytes, enc) {
bytes = utils.toArray(bytes, enc);

var len = this.p.byteLength();
if (bytes[0] === 0x04 && bytes.length - 1 === 2 * len) {
return this.point(bytes.slice(1, 1 + len),
bytes.slice(1 + len, 1 + 2 * len));

// uncompressed, hybrid-odd, hybrid-even
if ((bytes[0] === 0x04 || bytes[0] === 0x06 || bytes[0] === 0x07) &&
bytes.length - 1 === 2 * len) {
if (bytes[0] === 0x06)
assert(bytes[bytes.length - 1] % 2 === 0);
else if (bytes[0] === 0x07)
assert(bytes[bytes.length - 1] % 2 === 1);

var res = this.point(bytes.slice(1, 1 + len),
bytes.slice(1 + len, 1 + 2 * len));

return res;
} else if ((bytes[0] === 0x02 || bytes[0] === 0x03) &&
bytes.length - 1 === len) {
return this.pointFromX(bytes.slice(1, 1 + len), bytes[0] === 0x03);
Expand Down
9 changes: 9 additions & 0 deletions test/curve-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ describe('Point codec', function () {
// Decodes as expected
assert(curve.decodePoint(definition.encoded, 'hex').eq(p));
assert(curve.decodePoint(definition.compactEncoded, 'hex').eq(p));
assert(curve.decodePoint(definition.hybrid, 'hex').eq(p));
};
}

Expand Down Expand Up @@ -207,6 +208,10 @@ describe('Point codec', function () {
encoded:
'04' +
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798' +
'483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
hybrid:
'06' +
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798' +
'483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'
};

Expand All @@ -221,6 +226,10 @@ describe('Point codec', function () {
encoded:
'04' +
'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556' +
'ae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297',
hybrid:
'07' +
'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556' +
'ae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297'
};

Expand Down

0 comments on commit 8db7448

Please sign in to comment.