Skip to content

Commit

Permalink
bug in bn.js - upgrade to latest bug-fixed version
Browse files Browse the repository at this point in the history
This upgrades bn.js due to a bug that results in incorrect point
multiplication, and therefore incorrect public keys and addresses, in some
cases.. See these discussions:

bitpay/bitcore#894
indutny/elliptic#17
indutny/elliptic#18
indutny/elliptic#19
indutny/bn.js@3557d78

Furthermore, the getG function is update to return the value of G that has
precomputed values, as per the above discussions.
  • Loading branch information
ryanxcharles committed Jan 4, 2015
1 parent 1f9616e commit dbeab00
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ Point.prototype.fromString = function(str) {
};

Point.getG = function() {
var p = Point(ec.curve.g.getX(), ec.curve.g.getY());
return p;
var g = ec.curve.g;

This comment has been minimized.

Copy link
@dcousens

dcousens Jan 4, 2015

why not just return ec.curve.g?

This comment has been minimized.

Copy link
@ryanxcharles

ryanxcharles Jan 5, 2015

Author Contributor

No reason. Do you think it is better practice not to create superfluous variables?

This comment has been minimized.

Copy link
@dcousens

dcousens via email Jan 5, 2015

return g;
};

Point.getN = function() {
Expand Down
12 changes: 6 additions & 6 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
},
"dependencies": {
"aes": "=0.1.0",
"bn.js": "=0.16.0",
"bn.js": "=0.16.1",
"bs58": "=1.2.1",
"elliptic": "=0.16.0",
"hash.js": "=0.3.2",
Expand Down
21 changes: 21 additions & 0 deletions test/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ describe('Point', function() {
b.getY().toString().should.equal('32670510020758816978083085130507043184471273380659243275938904335757337482424');
});

it('should accurate multiply this problematic value related to a bug in bn.js', function() {
// see these discussions:
// https://github.com/bitpay/bitcore/pull/894
// https://github.com/indutny/elliptic/issues/17
// https://github.com/indutny/elliptic/pull/18
// https://github.com/indutny/elliptic/pull/19
// https://github.com/indutny/bn.js/commit/3557d780b07ed0ed301e128f326f83c2226fb679
var nhex = '6d1229a6b24c2e775c062870ad26bc261051e0198c67203167273c7c62538846';
var n = BN(nhex, 16);
var g1 = Point.getG(); // precomputed g
var g2 = Point().fromX(false, BN('79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', 16)); //non-precomputed g
var p1 = g1.mul(n);
var p2 = g2.mul(n);
var pxhex = 'd6106302d2698d6a41e9c9a114269e7be7c6a0081317de444bb2980bf9265a01';
var pyhex = 'e05fb262e64b108991a29979809fcef9d3e70cafceb3248c922c17d83d66bc9d';
p1.getX().toBuffer().toString('hex').should.equal(pxhex);
p1.getY().toBuffer().toString('hex').should.equal(pyhex);
p2.getX().toBuffer().toString('hex').should.equal(pxhex);
p2.getY().toBuffer().toString('hex').should.equal(pyhex);
});

});

describe('@fromX', function() {
Expand Down

0 comments on commit dbeab00

Please sign in to comment.