Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Point.prototype.toJSON for short curves #188

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/elliptic/curve/short.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,9 @@ Point.prototype._getBeta = function _getBeta() {

Point.prototype.toJSON = function toJSON() {
if (!this.precomputed)
return [ this.x, this.y ];
return [ this.x.fromRed(), this.y.fromRed() ];

return [ this.x, this.y, this.precomputed && {
return [ this.x.fromRed(), this.y.fromRed(), this.precomputed && {
doubles: this.precomputed.doubles && {
step: this.precomputed.doubles.step,
points: this.precomputed.doubles.points.slice(1)
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ describe('Test specs', function () {
require('./ecdh-test.js');
require('./ecdsa-test.js');
require('./ed25519-test.js');
require('./toJSON-test.js');
});
28 changes: 28 additions & 0 deletions test/toJSON-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

var fs = require('fs');
var path = require('path');
var assert = require('assert');
var elliptic = require('../lib/elliptic');
var curves = elliptic.curves;
var compare = assert.deepStrictEqual || assert.deepEqual;

function exportPoint (p) {
return JSON.parse(JSON.stringify(p.toJSON()));
}

describe('toJSON <-> pointFromJSON symmetry', function() {
it('toJSON is the inverse of pointFromJSON', function () {
for (var key in curves) {
if (key === 'PresetCurve') continue;

var curve = curves[key];
if (!curve.g.toJSON) continue;

var exported = exportPoint(curve.g);
var reimported = curve.curve.pointFromJSON(exported);
var reexported = exportPoint(reimported);
compare(exported, reexported, 'toJSON is the inverse of pointFromJSON');
}
});
});