From a2e471ae9e0b2d3721a7e1778dc6bfcabed2ba88 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Wed, 13 Aug 2014 15:23:06 -0400 Subject: [PATCH] more consistency: n -> bn, p -> point --- lib/bip32.js | 10 +++---- lib/ecdsa.js | 4 +-- lib/key.js | 20 +++++++------- lib/privkey.js | 16 +++++------ lib/pubkey.js | 22 +++++++-------- test/test.key.js | 6 ++--- test/test.privkey.js | 8 +++--- test/test.pubkey.js | 64 ++++++++++++++++++++++---------------------- 8 files changed, 75 insertions(+), 75 deletions(-) diff --git a/lib/bip32.js b/lib/bip32.js index 2ad3af9..0df8129 100644 --- a/lib/bip32.js +++ b/lib/bip32.js @@ -185,7 +185,7 @@ BIP32.prototype.buildExtendedPrivateKey = function() { new Buffer([this.childIndex & 0xff]), this.chainCode, 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; 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 { 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); // 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.chainCode = ir; @@ -287,10 +287,10 @@ BIP32.prototype.deriveChild = function(i) { // Ki = (IL + kpar)*G = IL*G + Kpar var ilG = Point.getG().mul(il); - var Kpar = this.key.pubkey.p; + var Kpar = this.key.pubkey.point; var Ki = ilG.add(Kpar); var newpub = new Pubkey(); - newpub.p = Ki; + newpub.point = Ki; ret = new BIP32(); ret.chainCode = ir; diff --git a/lib/ecdsa.js b/lib/ecdsa.js index 1130f99..76cd580 100644 --- a/lib/ecdsa.js +++ b/lib/ecdsa.js @@ -58,7 +58,7 @@ ECDSA.prototype.sigError = function() { var u1 = sinv.mul(e).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()) return 'p is infinity'; @@ -72,7 +72,7 @@ ECDSA.prototype.sign = function() { var hash = this.hash; var privkey = this.key.privkey; var k = this.k; - var d = privkey.n; + var d = privkey.bn; if (!hash || !privkey || !k || !d) throw new Error('ecdsa: invalid parameters'); diff --git a/lib/key.js b/lib/key.js index 4a897a2..11e11b2 100644 --- a/lib/key.js +++ b/lib/key.js @@ -1,7 +1,7 @@ var Privkey = require('./privkey'); var Pubkey = require('./pubkey'); var Random = require('./random'); -var bn = require('./bn'); +var Bn = require('./bn'); var point = require('./point'); function Key(privkey, pubkey) { @@ -12,8 +12,8 @@ function Key(privkey, pubkey) { Key.prototype.fromRandom = function() { do { var privbuf = Random.getRandomBuffer(32); - this.privkey = new Privkey(bn(privbuf)); - var condition = this.privkey.n.lt(point.getN()); + this.privkey = new Privkey(Bn(privbuf)); + var condition = this.privkey.bn.lt(point.getN()); } while (!condition); this.privkey2pubkey(); return this; @@ -21,26 +21,26 @@ Key.prototype.fromRandom = function() { Key.prototype.fromString = function(str) { var obj = JSON.parse(str); - if (obj.priv) { + if (obj.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.fromString(obj.pub); + this.pubkey.fromString(obj.pubkey); } }; 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() { var obj = {}; if (this.privkey) - obj.priv = this.privkey.toString(); + obj.privkey = this.privkey.toString(); if (this.pubkey) - obj.pub = this.pubkey.toString(); + obj.pubkey = this.pubkey.toString(); return JSON.stringify(obj); }; diff --git a/lib/privkey.js b/lib/privkey.js index dac93e5..7aa09cd 100644 --- a/lib/privkey.js +++ b/lib/privkey.js @@ -1,16 +1,16 @@ -var bn = require('./bn'); +var Bn = require('./bn'); var point = require('./point'); var constants = require('./constants'); var base58check = require('./base58check'); -var Privkey = function(n, network, compressed) { - this.n = n; +var Privkey = function(bn, network, compressed) { + this.bn = bn; this.network = network; this.compressed = compressed; }; 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'); if (typeof constants[this.network] === undefined) throw new Error('privkey: Must specify the network ("mainnet" or "testnet")'); @@ -27,12 +27,12 @@ Privkey.prototype.toWIF = function() { if (typeof this.compressed === 'undefined') compressed = true; - var privbuf = this.n.toBuffer({size: 32}); + var privbuf = this.bn.toBuffer({size: 32}); var buf; 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 - 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); }; @@ -54,7 +54,7 @@ Privkey.prototype.fromWIF = function(str) { else 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() { diff --git a/lib/pubkey.js b/lib/pubkey.js index b00fd72..ded1060 100644 --- a/lib/pubkey.js +++ b/lib/pubkey.js @@ -1,10 +1,10 @@ -var point = require('./point'); +var Point = require('./point'); var bn = require('./bn'); -var Pubkey = function(p) { - if (p && !p.getX() && !p.getY()) +var Pubkey = function(point) { + if (point && !point.getX() && !point.getY()) throw new Error('pubkey: Invalid point'); - this.p = p; + this.point = point; }; 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'); var x = bn(xbuf); var y = bn(ybuf); - this.p = point(x, y); + this.point = Point(x, y); } else if (buf[0] == 0x03) { var xbuf = buf.slice(1); var x = bn(xbuf); @@ -37,7 +37,7 @@ Pubkey.prototype.fromString = function(str) { Pubkey.prototype.fromX = function(odd, x) { if (typeof odd !== 'boolean') 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() { @@ -48,8 +48,8 @@ Pubkey.prototype.toDER = function(compressed) { if (typeof compressed !== 'boolean') throw new Error('pubkey: Must specify whether the public key is compressed or not (true or false)'); - var x = this.p.getX(); - var y = this.p.getY(); + var x = this.point.getX(); + var y = this.point.getY(); var xbuf = x.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 Pubkey.prototype.validate = function() { - if (this.p.isInfinity()) + if (this.point.isInfinity()) 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'); - this.p.validate(); + this.point.validate(); return this; }; diff --git a/test/test.key.js b/test/test.key.js index 2ba0382..37a4559 100644 --- a/test/test.key.js +++ b/test/test.key.js @@ -28,9 +28,9 @@ describe('key', function() { key.fromRandom(); should.exist(key.privkey); should.exist(key.pubkey); - key.privkey.n.gt(bn(0)).should.equal(true); - key.pubkey.p.getX().gt(bn(0)).should.equal(true); - key.pubkey.p.getY().gt(bn(0)).should.equal(true); + key.privkey.bn.gt(bn(0)).should.equal(true); + key.pubkey.point.getX().gt(bn(0)).should.equal(true); + key.pubkey.point.getY().gt(bn(0)).should.equal(true); }); }); diff --git a/test/test.privkey.js b/test/test.privkey.js index 8ee56d3..2ab2e86 100644 --- a/test/test.privkey.js +++ b/test/test.privkey.js @@ -1,6 +1,6 @@ var Privkey = require('../lib/privkey'); var base58check = require('../lib/base58check'); -var bn = require('../lib/bn'); +var Bn = require('../lib/bn'); var should = require('chai').should(); describe('privkey', function() { @@ -17,17 +17,17 @@ describe('privkey', 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); }); 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); }); 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); }); diff --git a/test/test.pubkey.js b/test/test.pubkey.js index 0921130..9eeb7df 100644 --- a/test/test.pubkey.js +++ b/test/test.pubkey.js @@ -1,39 +1,39 @@ var should = require('chai').should(); -var pubkey = require('../lib/pubkey'); -var point = require('../lib/point'); -var bn = require('../lib/bn'); +var Pubkey = require('../lib/pubkey'); +var Point = require('../lib/point'); +var Bn = require('../lib/bn'); describe('pubkey', function() { it('should create a blank public key', function() { - var pk = new pubkey(); + var pk = new Pubkey(); should.exist(pk); }); it('should create a public key with a point', function() { - var p = point(); - var pk = new pubkey(p); - should.exist(pk.p); + var p = Point(); + var pk = new Pubkey(p); + should.exist(pk.point); }); describe('#fromDER', function() { it('should parse this uncompressed public key', function() { - var pk = new pubkey(); + var pk = new Pubkey(); pk.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex')); - pk.p.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); - pk.p.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); + pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); + pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); }); it('should parse this compressed public key', function() { - var pk = new pubkey(); + var pk = new Pubkey(); pk.fromDER(new Buffer('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); - pk.p.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); - pk.p.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); + pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); + pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); }); it('should throw an error on this invalid public key', function() { - var pk = new pubkey(); + var pk = new Pubkey(); (function() { pk.fromDER(new Buffer('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); }).should.throw(); @@ -44,10 +44,10 @@ describe('pubkey', function() { describe('#fromString', function() { it('should parse this known valid public key', function() { - pk = new pubkey(); + pk = new Pubkey(); pk.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); - pk.p.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); - pk.p.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); + pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); + pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); }); }); @@ -55,11 +55,11 @@ describe('pubkey', function() { describe('#fromX', function() { it('should create this known public key', function() { - var x = bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); - var pk = new pubkey(); + var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); + var pk = new Pubkey(); pk.fromX(true, x); - pk.p.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); - pk.p.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); + pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); + pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); }); }); @@ -67,8 +67,8 @@ describe('pubkey', function() { describe('#toBuffer', function() { it('should return this compressed DER format', function() { - var x = bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); - var pk = new pubkey(); + var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); + var pk = new Pubkey(); pk.fromX(true, x); pk.toBuffer().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); }); @@ -78,15 +78,15 @@ describe('pubkey', function() { describe('#toDER', function() { it('should return this compressed DER format', function() { - var x = bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); - var pk = new pubkey(); + var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); + var pk = new Pubkey(); pk.fromX(true, x); pk.toDER(true).toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); }); it('should return this uncompressed DER format', function() { - var x = bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); - var pk = new pubkey(); + var x = Bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex')); + var pk = new Pubkey(); pk.fromX(true, x); pk.toDER(false).toString('hex').should.equal('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); }); @@ -97,7 +97,7 @@ describe('pubkey', function() { it('should print this known public key', function() { var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'; - var pk = new pubkey(); + var pk = new Pubkey(); pk.fromString(hex); pk.toString().should.equal(hex); }); @@ -108,14 +108,14 @@ describe('pubkey', function() { it('should not throw an error if pubkey is valid', function() { var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'; - var pk = new pubkey(); + var pk = new Pubkey(); pk.fromString(hex); should.exist(pk.validate()); }); it('should not throw an error if pubkey is invalid', function() { var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a0000000000000000000000000000000000000000000000000000000000000000'; - var pk = new pubkey(); + var pk = new Pubkey(); pk.fromString(hex); (function() { pk.validate(); @@ -123,8 +123,8 @@ describe('pubkey', function() { }); it('should not throw an error if pubkey is infinity', function() { - var pk = new pubkey(); - pk.p = point.getG().mul(point.getN()); + var pk = new Pubkey(); + pk.point = Point.getG().mul(Point.getN()); (function() { pk.validate(); }).should.throw('point: Point cannot be equal to Infinity');