more consistency: n -> bn, p -> point

This commit is contained in:
Ryan X. Charles 2014-08-13 15:23:06 -04:00
parent 1fa7fb527e
commit a2e471ae9e
8 changed files with 75 additions and 75 deletions

View File

@ -185,7 +185,7 @@ BIP32.prototype.buildExtendedPrivateKey = function() {
new Buffer([this.childIndex & 0xff]), new Buffer([this.childIndex & 0xff]),
this.chainCode, this.chainCode,
new Buffer([0]), new Buffer([0]),
this.key.privkey.n.toBuffer({size: 32}) this.key.privkey.bn.toBuffer({size: 32})
]); ]);
} }
@ -259,7 +259,7 @@ BIP32.prototype.deriveChild = function(i) {
var data = null; var data = null;
if (usePrivate) { if (usePrivate) {
data = Buffer.concat([new Buffer([0]), this.key.privkey.n.toBuffer({size: 32}), ib]); data = Buffer.concat([new Buffer([0]), this.key.privkey.bn.toBuffer({size: 32}), ib]);
} else { } else {
data = Buffer.concat([this.key.pubkey.toBuffer({size: 32}), ib]); data = Buffer.concat([this.key.pubkey.toBuffer({size: 32}), ib]);
} }
@ -269,7 +269,7 @@ BIP32.prototype.deriveChild = function(i) {
var ir = hash.slice(32, 64); var ir = hash.slice(32, 64);
// ki = IL + kpar (mod n). // ki = IL + kpar (mod n).
var k = il.add(this.key.privkey.n).mod(Point.getN()); var k = il.add(this.key.privkey.bn).mod(Point.getN());
ret = new BIP32(); ret = new BIP32();
ret.chainCode = ir; ret.chainCode = ir;
@ -287,10 +287,10 @@ BIP32.prototype.deriveChild = function(i) {
// Ki = (IL + kpar)*G = IL*G + Kpar // Ki = (IL + kpar)*G = IL*G + Kpar
var ilG = Point.getG().mul(il); var ilG = Point.getG().mul(il);
var Kpar = this.key.pubkey.p; var Kpar = this.key.pubkey.point;
var Ki = ilG.add(Kpar); var Ki = ilG.add(Kpar);
var newpub = new Pubkey(); var newpub = new Pubkey();
newpub.p = Ki; newpub.point = Ki;
ret = new BIP32(); ret = new BIP32();
ret.chainCode = ir; ret.chainCode = ir;

View File

@ -58,7 +58,7 @@ ECDSA.prototype.sigError = function() {
var u1 = sinv.mul(e).mod(n); var u1 = sinv.mul(e).mod(n);
var u2 = sinv.mul(r).mod(n); var u2 = sinv.mul(r).mod(n);
var p = point.getG().mulAdd(u1, this.key.pubkey.p, u2); var p = point.getG().mulAdd(u1, this.key.pubkey.point, u2);
if (p.isInfinity()) if (p.isInfinity())
return 'p is infinity'; return 'p is infinity';
@ -72,7 +72,7 @@ ECDSA.prototype.sign = function() {
var hash = this.hash; var hash = this.hash;
var privkey = this.key.privkey; var privkey = this.key.privkey;
var k = this.k; var k = this.k;
var d = privkey.n; var d = privkey.bn;
if (!hash || !privkey || !k || !d) if (!hash || !privkey || !k || !d)
throw new Error('ecdsa: invalid parameters'); throw new Error('ecdsa: invalid parameters');

View File

@ -1,7 +1,7 @@
var Privkey = require('./privkey'); var Privkey = require('./privkey');
var Pubkey = require('./pubkey'); var Pubkey = require('./pubkey');
var Random = require('./random'); var Random = require('./random');
var bn = require('./bn'); var Bn = require('./bn');
var point = require('./point'); var point = require('./point');
function Key(privkey, pubkey) { function Key(privkey, pubkey) {
@ -12,8 +12,8 @@ function Key(privkey, pubkey) {
Key.prototype.fromRandom = function() { Key.prototype.fromRandom = function() {
do { do {
var privbuf = Random.getRandomBuffer(32); var privbuf = Random.getRandomBuffer(32);
this.privkey = new Privkey(bn(privbuf)); this.privkey = new Privkey(Bn(privbuf));
var condition = this.privkey.n.lt(point.getN()); var condition = this.privkey.bn.lt(point.getN());
} while (!condition); } while (!condition);
this.privkey2pubkey(); this.privkey2pubkey();
return this; return this;
@ -21,26 +21,26 @@ Key.prototype.fromRandom = function() {
Key.prototype.fromString = function(str) { Key.prototype.fromString = function(str) {
var obj = JSON.parse(str); var obj = JSON.parse(str);
if (obj.priv) { if (obj.privkey) {
this.privkey = new Privkey(); this.privkey = new Privkey();
this.privkey.fromString(obj.priv); this.privkey.fromString(obj.privkey);
} }
if (obj.pub) { if (obj.pubkey) {
this.pubkey = new Pubkey(); this.pubkey = new Pubkey();
this.pubkey.fromString(obj.pub); this.pubkey.fromString(obj.pubkey);
} }
}; };
Key.prototype.privkey2pubkey = function() { Key.prototype.privkey2pubkey = function() {
this.pubkey = new Pubkey(point.getG().mul(this.privkey.n)); this.pubkey = new Pubkey(point.getG().mul(this.privkey.bn));
}; };
Key.prototype.toString = function() { Key.prototype.toString = function() {
var obj = {}; var obj = {};
if (this.privkey) if (this.privkey)
obj.priv = this.privkey.toString(); obj.privkey = this.privkey.toString();
if (this.pubkey) if (this.pubkey)
obj.pub = this.pubkey.toString(); obj.pubkey = this.pubkey.toString();
return JSON.stringify(obj); return JSON.stringify(obj);
}; };

View File

@ -1,16 +1,16 @@
var bn = require('./bn'); var Bn = require('./bn');
var point = require('./point'); var point = require('./point');
var constants = require('./constants'); var constants = require('./constants');
var base58check = require('./base58check'); var base58check = require('./base58check');
var Privkey = function(n, network, compressed) { var Privkey = function(bn, network, compressed) {
this.n = n; this.bn = bn;
this.network = network; this.network = network;
this.compressed = compressed; this.compressed = compressed;
}; };
Privkey.prototype.validate = function() { Privkey.prototype.validate = function() {
if (!this.n.lt(point.getN())) if (!this.bn.lt(point.getN()))
throw new Error('privkey: Number must be less than N'); throw new Error('privkey: Number must be less than N');
if (typeof constants[this.network] === undefined) if (typeof constants[this.network] === undefined)
throw new Error('privkey: Must specify the network ("mainnet" or "testnet")'); throw new Error('privkey: Must specify the network ("mainnet" or "testnet")');
@ -27,12 +27,12 @@ Privkey.prototype.toWIF = function() {
if (typeof this.compressed === 'undefined') if (typeof this.compressed === 'undefined')
compressed = true; compressed = true;
var privbuf = this.n.toBuffer({size: 32}); var privbuf = this.bn.toBuffer({size: 32});
var buf; var buf;
if (compressed) if (compressed)
buf = Buffer.concat([new Buffer([constants[network].privkey]), this.n.toBuffer({size: 32}), new Buffer([0x01])]); buf = Buffer.concat([new Buffer([constants[network].privkey]), this.bn.toBuffer({size: 32}), new Buffer([0x01])]);
else else
buf = Buffer.concat([new Buffer([constants[network].privkey]), this.n.toBuffer({size: 32})]); buf = Buffer.concat([new Buffer([constants[network].privkey]), this.bn.toBuffer({size: 32})]);
return base58check.encode(buf); return base58check.encode(buf);
}; };
@ -54,7 +54,7 @@ Privkey.prototype.fromWIF = function(str) {
else else
throw new Error('privkey: Invalid network'); throw new Error('privkey: Invalid network');
this.n = bn.fromBuffer(buf.slice(1, 32 + 1)); this.bn = Bn.fromBuffer(buf.slice(1, 32 + 1));
}; };
Privkey.prototype.toString = function() { Privkey.prototype.toString = function() {

View File

@ -1,10 +1,10 @@
var point = require('./point'); var Point = require('./point');
var bn = require('./bn'); var bn = require('./bn');
var Pubkey = function(p) { var Pubkey = function(point) {
if (p && !p.getX() && !p.getY()) if (point && !point.getX() && !point.getY())
throw new Error('pubkey: Invalid point'); throw new Error('pubkey: Invalid point');
this.p = p; this.point = point;
}; };
Pubkey.prototype.fromDER = function(buf) { Pubkey.prototype.fromDER = function(buf) {
@ -15,7 +15,7 @@ Pubkey.prototype.fromDER = function(buf) {
throw new Error('pubkey: Length of x and y must be 32 bytes'); throw new Error('pubkey: Length of x and y must be 32 bytes');
var x = bn(xbuf); var x = bn(xbuf);
var y = bn(ybuf); var y = bn(ybuf);
this.p = point(x, y); this.point = Point(x, y);
} else if (buf[0] == 0x03) { } else if (buf[0] == 0x03) {
var xbuf = buf.slice(1); var xbuf = buf.slice(1);
var x = bn(xbuf); var x = bn(xbuf);
@ -37,7 +37,7 @@ Pubkey.prototype.fromString = function(str) {
Pubkey.prototype.fromX = function(odd, x) { Pubkey.prototype.fromX = function(odd, x) {
if (typeof odd !== 'boolean') if (typeof odd !== 'boolean')
throw new Error('pubkey: Must specify whether y is odd or not (true or false)'); throw new Error('pubkey: Must specify whether y is odd or not (true or false)');
this.p = point.fromX(odd, x); this.point = Point.fromX(odd, x);
}; };
Pubkey.prototype.toBuffer = function() { Pubkey.prototype.toBuffer = function() {
@ -48,8 +48,8 @@ Pubkey.prototype.toDER = function(compressed) {
if (typeof compressed !== 'boolean') if (typeof compressed !== 'boolean')
throw new Error('pubkey: Must specify whether the public key is compressed or not (true or false)'); throw new Error('pubkey: Must specify whether the public key is compressed or not (true or false)');
var x = this.p.getX(); var x = this.point.getX();
var y = this.p.getY(); var y = this.point.getY();
var xbuf = x.toBuffer({size: 32}); var xbuf = x.toBuffer({size: 32});
var ybuf = y.toBuffer({size: 32}); var ybuf = y.toBuffer({size: 32});
@ -73,11 +73,11 @@ Pubkey.prototype.toString = function() {
//https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf //https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf
Pubkey.prototype.validate = function() { Pubkey.prototype.validate = function() {
if (this.p.isInfinity()) if (this.point.isInfinity())
throw new Error('point: Point cannot be equal to Infinity'); throw new Error('point: Point cannot be equal to Infinity');
if (this.p.eq(point(bn(0), bn(0)))) if (this.point.eq(Point(bn(0), bn(0))))
throw new Error('point: Point cannot be equal to 0, 0'); throw new Error('point: Point cannot be equal to 0, 0');
this.p.validate(); this.point.validate();
return this; return this;
}; };

View File

@ -28,9 +28,9 @@ describe('key', function() {
key.fromRandom(); key.fromRandom();
should.exist(key.privkey); should.exist(key.privkey);
should.exist(key.pubkey); should.exist(key.pubkey);
key.privkey.n.gt(bn(0)).should.equal(true); key.privkey.bn.gt(bn(0)).should.equal(true);
key.pubkey.p.getX().gt(bn(0)).should.equal(true); key.pubkey.point.getX().gt(bn(0)).should.equal(true);
key.pubkey.p.getY().gt(bn(0)).should.equal(true); key.pubkey.point.getY().gt(bn(0)).should.equal(true);
}); });
}); });

View File

@ -1,6 +1,6 @@
var Privkey = require('../lib/privkey'); var Privkey = require('../lib/privkey');
var base58check = require('../lib/base58check'); var base58check = require('../lib/base58check');
var bn = require('../lib/bn'); var Bn = require('../lib/bn');
var should = require('chai').should(); var should = require('chai').should();
describe('privkey', function() { describe('privkey', function() {
@ -17,17 +17,17 @@ describe('privkey', function() {
}); });
it('should create a mainnet private key', function() { it('should create a mainnet private key', function() {
var privkey = new Privkey(bn.fromBuffer(buf), 'mainnet', true); var privkey = new Privkey(Bn.fromBuffer(buf), 'mainnet', true);
privkey.toString().should.equal(encmainnet); privkey.toString().should.equal(encmainnet);
}); });
it('should create an uncompressed testnet private key', function() { it('should create an uncompressed testnet private key', function() {
var privkey = new Privkey(bn.fromBuffer(buf), 'testnet', false); var privkey = new Privkey(Bn.fromBuffer(buf), 'testnet', false);
privkey.toString().should.equal(enctu); privkey.toString().should.equal(enctu);
}); });
it('should create an uncompressed mainnet private key', function() { it('should create an uncompressed mainnet private key', function() {
var privkey = new Privkey(bn.fromBuffer(buf), 'mainnet', false); var privkey = new Privkey(Bn.fromBuffer(buf), 'mainnet', false);
privkey.toString().should.equal(encmu); privkey.toString().should.equal(encmu);
}); });

View File

@ -1,39 +1,39 @@
var should = require('chai').should(); var should = require('chai').should();
var pubkey = require('../lib/pubkey'); var Pubkey = require('../lib/pubkey');
var point = require('../lib/point'); var Point = require('../lib/point');
var bn = require('../lib/bn'); var Bn = require('../lib/bn');
describe('pubkey', function() { describe('pubkey', function() {
it('should create a blank public key', function() { it('should create a blank public key', function() {
var pk = new pubkey(); var pk = new Pubkey();
should.exist(pk); should.exist(pk);
}); });
it('should create a public key with a point', function() { it('should create a public key with a point', function() {
var p = point(); var p = Point();
var pk = new pubkey(p); var pk = new Pubkey(p);
should.exist(pk.p); should.exist(pk.point);
}); });
describe('#fromDER', function() { describe('#fromDER', function() {
it('should parse this uncompressed public key', function() { it('should parse this uncompressed public key', function() {
var pk = new pubkey(); var pk = new Pubkey();
pk.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex')); pk.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
pk.p.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
pk.p.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
}); });
it('should parse this compressed public key', function() { it('should parse this compressed public key', function() {
var pk = new pubkey(); var pk = new Pubkey();
pk.fromDER(new Buffer('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); pk.fromDER(new Buffer('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
pk.p.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
pk.p.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
}); });
it('should throw an error on this invalid public key', function() { it('should throw an error on this invalid public key', function() {
var pk = new pubkey(); var pk = new Pubkey();
(function() { (function() {
pk.fromDER(new Buffer('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); pk.fromDER(new Buffer('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
}).should.throw(); }).should.throw();
@ -44,10 +44,10 @@ describe('pubkey', function() {
describe('#fromString', function() { describe('#fromString', function() {
it('should parse this known valid public key', function() { it('should parse this known valid public key', function() {
pk = new pubkey(); pk = new Pubkey();
pk.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); pk.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
pk.p.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
pk.p.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
}); });
}); });
@ -55,11 +55,11 @@ describe('pubkey', function() {
describe('#fromX', function() { describe('#fromX', function() {
it('should create this known public key', function() { it('should create this known public key', function() {
var x = bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
var pk = new pubkey(); var pk = new Pubkey();
pk.fromX(true, x); pk.fromX(true, x);
pk.p.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
pk.p.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
}); });
}); });
@ -67,8 +67,8 @@ describe('pubkey', function() {
describe('#toBuffer', function() { describe('#toBuffer', function() {
it('should return this compressed DER format', function() { it('should return this compressed DER format', function() {
var x = bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
var pk = new pubkey(); var pk = new Pubkey();
pk.fromX(true, x); pk.fromX(true, x);
pk.toBuffer().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); pk.toBuffer().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
}); });
@ -78,15 +78,15 @@ describe('pubkey', function() {
describe('#toDER', function() { describe('#toDER', function() {
it('should return this compressed DER format', function() { it('should return this compressed DER format', function() {
var x = bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
var pk = new pubkey(); var pk = new Pubkey();
pk.fromX(true, x); pk.fromX(true, x);
pk.toDER(true).toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); pk.toDER(true).toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
}); });
it('should return this uncompressed DER format', function() { it('should return this uncompressed DER format', function() {
var x = bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
var pk = new pubkey(); var pk = new Pubkey();
pk.fromX(true, x); pk.fromX(true, x);
pk.toDER(false).toString('hex').should.equal('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); pk.toDER(false).toString('hex').should.equal('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
}); });
@ -97,7 +97,7 @@ describe('pubkey', function() {
it('should print this known public key', function() { it('should print this known public key', function() {
var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'; var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
var pk = new pubkey(); var pk = new Pubkey();
pk.fromString(hex); pk.fromString(hex);
pk.toString().should.equal(hex); pk.toString().should.equal(hex);
}); });
@ -108,14 +108,14 @@ describe('pubkey', function() {
it('should not throw an error if pubkey is valid', function() { it('should not throw an error if pubkey is valid', function() {
var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'; var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
var pk = new pubkey(); var pk = new Pubkey();
pk.fromString(hex); pk.fromString(hex);
should.exist(pk.validate()); should.exist(pk.validate());
}); });
it('should not throw an error if pubkey is invalid', function() { it('should not throw an error if pubkey is invalid', function() {
var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a0000000000000000000000000000000000000000000000000000000000000000'; var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a0000000000000000000000000000000000000000000000000000000000000000';
var pk = new pubkey(); var pk = new Pubkey();
pk.fromString(hex); pk.fromString(hex);
(function() { (function() {
pk.validate(); pk.validate();
@ -123,8 +123,8 @@ describe('pubkey', function() {
}); });
it('should not throw an error if pubkey is infinity', function() { it('should not throw an error if pubkey is infinity', function() {
var pk = new pubkey(); var pk = new Pubkey();
pk.p = point.getG().mul(point.getN()); pk.point = Point.getG().mul(Point.getN());
(function() { (function() {
pk.validate(); pk.validate();
}).should.throw('point: Point cannot be equal to Infinity'); }).should.throw('point: Point cannot be equal to Infinity');