From eb3a6bcb31f7b15e9b9cce345afe607f4ff2427a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 7 Jun 2014 18:24:16 +1000 Subject: [PATCH] all: rename D to d as per SEC convention --- src/ecdsa.js | 12 ++++++------ src/eckey.js | 24 ++++++++++++------------ src/hdnode.js | 6 +++--- test/bitcoin.core.js | 2 +- test/ec.js | 4 ++-- test/ecdsa.js | 20 ++++++++++---------- test/eckey.js | 13 +++++++------ test/fixtures/ecdsa.json | 24 ++++++++++++------------ test/fixtures/eckey.json | 16 ++++++++-------- test/fixtures/message.json | 4 ++-- test/hdnode.js | 10 +++++----- test/message.js | 4 ++-- 12 files changed, 70 insertions(+), 69 deletions(-) diff --git a/src/ecdsa.js b/src/ecdsa.js index 2eeffad..cf4b182 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -4,12 +4,12 @@ var crypto = require('./crypto') var BigInteger = require('bigi') var ECPointFp = require('./ec').ECPointFp -function deterministicGenerateK(ecparams, hash, D) { +function deterministicGenerateK(ecparams, hash, d) { assert(Buffer.isBuffer(hash), 'Hash must be a Buffer, not ' + hash) assert.equal(hash.length, 32, 'Hash must be 256 bit') - assert(D instanceof BigInteger, 'Private key must be a BigInteger') + assert(d instanceof BigInteger, 'Private key must be a BigInteger') - var x = D.toBuffer(32) + var x = d.toBuffer(32) var k = new Buffer(32) var v = new Buffer(32) k.fill(0) @@ -30,8 +30,8 @@ function deterministicGenerateK(ecparams, hash, D) { return kB } -function sign(ecparams, hash, D) { - var k = deterministicGenerateK(ecparams, hash, D) +function sign(ecparams, hash, d) { + var k = deterministicGenerateK(ecparams, hash, d) var n = ecparams.getN() var G = ecparams.getG() @@ -41,7 +41,7 @@ function sign(ecparams, hash, D) { var r = Q.getX().toBigInteger().mod(n) assert.notEqual(r.signum(), 0, 'Invalid R value') - var s = k.modInverse(n).multiply(e.add(D.multiply(r))).mod(n) + var s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n) assert.notEqual(s.signum(), 0, 'Invalid S value') var N_OVER_TWO = n.shiftRight(1) diff --git a/src/eckey.js b/src/eckey.js index 15e78a5..c78c261 100644 --- a/src/eckey.js +++ b/src/eckey.js @@ -10,13 +10,13 @@ var ECPubKey = require('./ecpubkey') var sec = require('./sec') var ecparams = sec('secp256k1') -function ECKey(D, compressed) { - assert(D.signum() > 0, 'Private key must be greater than 0') - assert(D.compareTo(ecparams.getN()) < 0, 'Private key must be less than the curve order') +function ECKey(d, compressed) { + assert(d.signum() > 0, 'Private key must be greater than 0') + assert(d.compareTo(ecparams.getN()) < 0, 'Private key must be less than the curve order') - var Q = ecparams.getG().multiply(D) + var Q = ecparams.getG().multiply(d) - this.D = D + this.d = d this.pub = new ECPubKey(Q, compressed) } @@ -38,18 +38,18 @@ ECKey.fromWIF = function(string) { assert.equal(payload.length, 32, 'Invalid WIF payload length') - var D = BigInteger.fromBuffer(payload) - return new ECKey(D, compressed) + var d = BigInteger.fromBuffer(payload) + return new ECKey(d, compressed) } ECKey.makeRandom = function(compressed, rng) { rng = rng || secureRandom var buffer = new Buffer(rng(32)) - var D = BigInteger.fromBuffer(buffer) - D = D.mod(ecparams.getN()) + var d = BigInteger.fromBuffer(buffer) + d = d.mod(ecparams.getN()) - return new ECKey(D, compressed) + return new ECKey(d, compressed) } // Export functions @@ -60,7 +60,7 @@ ECKey.prototype.toWIF = function(network) { var buffer = new Buffer(bufferLen) buffer.writeUInt8(network.wif, 0) - this.D.toBuffer(32).copy(buffer, 1) + this.d.toBuffer(32).copy(buffer, 1) if (this.pub.compressed) { buffer.writeUInt8(0x01, 33) @@ -71,7 +71,7 @@ ECKey.prototype.toWIF = function(network) { // Operations ECKey.prototype.sign = function(hash) { - return ecdsa.sign(ecparams, hash, this.D) + return ecdsa.sign(ecparams, hash, this.d) } module.exports = ECKey diff --git a/src/hdnode.js b/src/hdnode.js index 80fee26..741c1a5 100644 --- a/src/hdnode.js +++ b/src/hdnode.js @@ -174,7 +174,7 @@ HDNode.prototype.toBuffer = function(isPrivate) { // 0x00 + k for private keys buffer.writeUInt8(0, 45) - this.privKey.D.toBuffer(32).copy(buffer, 46) + this.privKey.d.toBuffer(32).copy(buffer, 46) } else { // X9.62 encoding for public keys @@ -202,7 +202,7 @@ HDNode.prototype.derive = function(index) { // data = 0x00 || ser256(kpar) || ser32(index) data = Buffer.concat([ - this.privKey.D.toBuffer(33), + this.privKey.d.toBuffer(33), indexBuffer ]) @@ -231,7 +231,7 @@ HDNode.prototype.derive = function(index) { var hd if (this.privKey) { // ki = parse256(IL) + kpar (mod n) - var ki = pIL.add(this.privKey.D).mod(ecparams.getN()) + var ki = pIL.add(this.privKey.d).mod(ecparams.getN()) // In case ki == 0, proceed with the next value for i if (ki.signum() === 0) { diff --git a/test/bitcoin.core.js b/test/bitcoin.core.js index 3c76541..b246f64 100644 --- a/test/bitcoin.core.js +++ b/test/bitcoin.core.js @@ -101,7 +101,7 @@ describe('Bitcoin-core', function() { it('imports ' + string + ' correctly', function() { var privKey = ECKey.fromWIF(string) - assert.equal(privKey.D.toHex(), hex) + assert.equal(privKey.d.toHex(), hex) assert.equal(privKey.pub.compressed, params.isCompressed) }) }) diff --git a/test/ec.js b/test/ec.js index d1f4592..38b1784 100644 --- a/test/ec.js +++ b/test/ec.js @@ -70,8 +70,8 @@ describe('ec', function() { var ecparams2 = sec('secp256r1') var curve = ecparams2.getCurve() - var D = BigInteger.ONE - var Q = ecparams2.getG().multiply(D) + var d = BigInteger.ONE + var Q = ecparams2.getG().multiply(d) var buffer = Q.getEncoded(true) var decoded = ECPointFp.decodeFrom(curve, buffer) diff --git a/test/ecdsa.js b/test/ecdsa.js index 0c66c69..ca97bbc 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -15,10 +15,10 @@ describe('ecdsa', function() { describe('deterministicGenerateK', function() { it('matches the test vectors', function() { fixtures.valid.forEach(function(f) { - var D = BigInteger.fromHex(f.D) + var d = BigInteger.fromHex(f.d) var h1 = crypto.sha256(f.message) - var k = ecdsa.deterministicGenerateK(ecparams, h1, D) + var k = ecdsa.deterministicGenerateK(ecparams, h1, d) assert.equal(k.toHex(), f.k) }) }) @@ -26,10 +26,10 @@ describe('ecdsa', function() { describe('recoverPubKey', function() { it('succesfully recovers a public key', function() { - var D = BigInteger.ONE + var d = BigInteger.ONE var signature = new Buffer('INcvXVVEFyIfHLbDX+xoxlKFn3Wzj9g0UbhObXdMq+YMKC252o5RHFr0/cKdQe1WsBLUBi4morhgZ77obDJVuV0=', 'base64') - var Q = ecparams.getG().multiply(D) + var Q = ecparams.getG().multiply(d) var hash = message.magicHash('1111', networks.bitcoin) var e = BigInteger.fromBuffer(hash) var parsed = ecdsa.parseSigCompact(signature) @@ -42,9 +42,9 @@ describe('ecdsa', function() { describe('sign', function() { it('matches the test vectors', function() { fixtures.valid.forEach(function(f) { - var D = BigInteger.fromHex(f.D) + var d = BigInteger.fromHex(f.d) var hash = crypto.sha256(f.message) - var signature = ecdsa.sign(ecparams, hash, D) + var signature = ecdsa.sign(ecparams, hash, d) assert.equal(signature.r.toString(), f.signature.r) assert.equal(signature.s.toString(), f.signature.s) @@ -64,8 +64,8 @@ describe('ecdsa', function() { describe('verifyRaw', function() { it('verifies valid signatures', function() { fixtures.valid.forEach(function(f) { - var D = BigInteger.fromHex(f.D) - var Q = ecparams.getG().multiply(D) + var d = BigInteger.fromHex(f.d) + var Q = ecparams.getG().multiply(d) var signature = { r: new BigInteger(f.signature.r), @@ -79,13 +79,13 @@ describe('ecdsa', function() { fixtures.invalid.verifyRaw.forEach(function(f) { it('fails to verify with ' + f.description, function() { - var D = BigInteger.fromHex(f.D) + var d = BigInteger.fromHex(f.d) var e = BigInteger.fromHex(f.e) var signature = { r: new BigInteger(f.signature.r), s: new BigInteger(f.signature.s) } - var Q = ecparams.getG().multiply(D) + var Q = ecparams.getG().multiply(d) assert.equal(ecdsa.verifyRaw(ecparams, e, signature, Q), false) }) diff --git a/test/eckey.js b/test/eckey.js index 0bc65a8..ea884ad 100644 --- a/test/eckey.js +++ b/test/eckey.js @@ -22,19 +22,20 @@ describe('ECKey', function() { }) fixtures.valid.forEach(function(f) { - it('calculates the matching pubKey for ' + f.D, function() { - var privKey = new ECKey(new BigInteger(f.D)) + it('calculates the matching pubKey for ' + f.d, function() { + var d = new BigInteger(f.d) + var privKey = new ECKey(d) assert.equal(privKey.pub.Q.toString(), f.Q.toString()) }) }) fixtures.invalid.constructor.forEach(function(f) { - it('throws on ' + f.D, function() { - var D = new BigInteger(f.D) + it('throws on ' + f.d, function() { + var d = new BigInteger(f.d) assert.throws(function() { - new ECKey(D) + new ECKey(d) }, new RegExp(f.exception)) }) }) @@ -46,7 +47,7 @@ describe('ECKey', function() { it('imports ' + wif.string + ' correctly', function() { var privKey = ECKey.fromWIF(wif.string) - assert.equal(privKey.D.toString(), f.D) + assert.equal(privKey.d.toString(), f.d) assert.equal(privKey.pub.compressed, wif.compressed) }) }) diff --git a/test/fixtures/ecdsa.json b/test/fixtures/ecdsa.json index e3ff799..4795d07 100644 --- a/test/fixtures/ecdsa.json +++ b/test/fixtures/ecdsa.json @@ -1,7 +1,7 @@ { "valid": [ { - "D": "01", + "d": "01", "k": "ec633bd56a5774a0940cb97e27a9e4e51dc94af737596a0c5cbb3d30332d92a5", "message": "Everything should be made as simple as possible, but not simpler.", "compact": { @@ -16,7 +16,7 @@ } }, { - "D": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140", + "d": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140", "k": "9dc74cbfd383980fb4ae5d2680acddac9dac956dca65a28c80ac9c847c2374e4", "message": "Equations are more important to me, because politics is for the present, but an equation is something for eternity.", "compact": { @@ -31,7 +31,7 @@ } }, { - "D": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140", + "d": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140", "k": "fd27071f01648ebbdd3e1cfbae48facc9fa97edc43bbbc9a7fdc28eae13296f5", "message": "Not only is the Universe stranger than we think, it is stranger than we can think.", "compact": { @@ -46,7 +46,7 @@ } }, { - "D": "0000000000000000000000000000000000000000000000000000000000000001", + "d": "0000000000000000000000000000000000000000000000000000000000000001", "k": "f0cd2ba5fc7c183de589f6416220a36775a146740798756d8d949f7166dcc87f", "message": "How wonderful that we have met with a paradox. Now we have some hope of making progress.", "compact": { @@ -61,7 +61,7 @@ } }, { - "D": "69ec59eaa1f4f2e36b639716b7c30ca86d9a5375c7b38d8918bd9c0ebc80ba64", + "d": "69ec59eaa1f4f2e36b639716b7c30ca86d9a5375c7b38d8918bd9c0ebc80ba64", "k": "6bb4a594ad57c1aa22dbe991a9d8501daf4688bf50a4892ef21bd7c711afda97", "message": "Computer science is no more about computers than astronomy is about telescopes.", "compact": { @@ -76,7 +76,7 @@ } }, { - "D": "00000000000000000000000000007246174ab1e92e9149c6e446fe194d072637", + "d": "00000000000000000000000000007246174ab1e92e9149c6e446fe194d072637", "k": "097b5c8ee22c3ea78a4d3635e0ff6fe85a1eb92ce317ded90b9e71aab2b861cb", "message": "...if you aren't, at any given time, scandalized by code you wrote five or even three years ago, you're not learning anywhere near enough", "compact": { @@ -91,7 +91,7 @@ } }, { - "D": "000000000000000000000000000000000000000000056916d0f9b31dc9b637f3", + "d": "000000000000000000000000000000000000000000056916d0f9b31dc9b637f3", "k": "19355c36c8cbcdfb2382e23b194b79f8c97bf650040fc7728dfbf6b39a97c25b", "message": "The question of whether computers can think is like the question of whether submarines can swim.", "compact": { @@ -146,7 +146,7 @@ "verifyRaw": [ { "description": "The wrong signature", - "D": "01", + "d": "01", "e": "06ef2b193b83b3d701f765f1db34672ab84897e1252343cc2197829af3a30456", "signature": { "r": "38341707918488238920692284707283974715538935465589664377561695343399725051885", @@ -155,7 +155,7 @@ }, { "description": "Invalid r value (== 0)", - "D": "01", + "d": "01", "e": "01", "signature": { "r": "00", @@ -164,7 +164,7 @@ }, { "description": "Invalid r value (>= n)", - "D": "01", + "d": "01", "e": "01", "signature": { "r": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", @@ -173,7 +173,7 @@ }, { "description": "Invalid s value (== 0)", - "D": "01", + "d": "01", "e": "01", "signature": { "r": "02", @@ -182,7 +182,7 @@ }, { "description": "Invalid s value (>= n)", - "D": "01", + "d": "01", "e": "01", "signature": { "r": "02", diff --git a/test/fixtures/eckey.json b/test/fixtures/eckey.json index fff0164..7d1c4a4 100644 --- a/test/fixtures/eckey.json +++ b/test/fixtures/eckey.json @@ -1,7 +1,7 @@ { "valid": [ { - "D": "1", + "d": "1", "Q": "(55066263022277343669578718895168534326250603453777594175500187360389116729240,32670510020758816978083085130507043184471273380659243275938904335757337482424)", "WIFs": [ { @@ -17,7 +17,7 @@ ] }, { - "D": "19898843618908353587043383062236220484949425084007183071220218307100305431102", + "d": "19898843618908353587043383062236220484949425084007183071220218307100305431102", "Q": "(83225686012142088543596389522774768397204444195709443235253141114409346958144,23739058578904784236915560265041168694780215705543362357495033621678991351768)", "WIFs": [ { @@ -28,7 +28,7 @@ ] }, { - "D": "48968302285117906840285529799176770990048954789747953886390402978935544927851", + "d": "48968302285117906840285529799176770990048954789747953886390402978935544927851", "Q": "(30095590000961171681152428142595206241714764354580127609094760797518133922356,93521207164355458151597931319591130635754976513751247168472016818884561919702)", "WIFs": [ { @@ -54,7 +54,7 @@ ] }, { - "D": "115792089237316195423570985008687907852837564279074904382605163141518161494336", + "d": "115792089237316195423570985008687907852837564279074904382605163141518161494336", "Q": "(55066263022277343669578718895168534326250603453777594175500187360389116729240,83121579216557378445487899878180864668798711284981320763518679672151497189239)", "WIFs": [ { @@ -69,19 +69,19 @@ "constructor": [ { "exception": "Private key must be greater than 0", - "D": "-1" + "d": "-1" }, { "exception": "Private key must be greater than 0", - "D": "0" + "d": "0" }, { "exception": "Private key must be less than the curve order", - "D": "115792089237316195423570985008687907852837564279074904382605163141518161494337" + "d": "115792089237316195423570985008687907852837564279074904382605163141518161494337" }, { "exception": "Private key must be less than the curve order", - "D": "115792089237316195423570985008687907853269984665640564039457584007913129639935" + "d": "115792089237316195423570985008687907853269984665640564039457584007913129639935" } ], "WIF": [ diff --git a/test/fixtures/message.json b/test/fixtures/message.json index 550609e..efedb4d 100644 --- a/test/fixtures/message.json +++ b/test/fixtures/message.json @@ -40,7 +40,7 @@ "description": "gives equal r, s values irrespective of point compression", "message": "vires is numeris", "network": "bitcoin", - "D": "1", + "d": "1", "signature": "HF8nHqFr3K2UKYahhX3soVeoW8W1ECNbr0wfck7lzyXjCS5Q16Ek45zyBuy1Fiy9sTPKVgsqqOuPvbycuVSSVl8=", "compressed": { "signature": "IF8nHqFr3K2UKYahhX3soVeoW8W1ECNbr0wfck7lzyXjCS5Q16Ek45zyBuy1Fiy9sTPKVgsqqOuPvbycuVSSVl8=" @@ -50,7 +50,7 @@ "description": "supports alternative networks", "message": "vires is numeris", "network": "dogecoin", - "D": "1", + "d": "1", "signature": "G6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s=" } ] diff --git a/test/hdnode.js b/test/hdnode.js index 02ee85f..5906b27 100644 --- a/test/hdnode.js +++ b/test/hdnode.js @@ -10,20 +10,20 @@ var fixtures = require('./fixtures/hdnode.json') describe('HDNode', function() { describe('Constructor', function() { - var D = BigInteger.ONE - var Q = ecparams.getG().multiply(D) + var d = BigInteger.ONE + var Q = ecparams.getG().multiply(d) var chainCode = new Buffer(32) chainCode.fill(1) it('calculates the publicKey from a BigInteger', function() { - var hd = new HDNode(D, chainCode) + var hd = new HDNode(d, chainCode) assert(hd.pubKey.Q.equals(Q)) }) it('only uses compressed points', function() { var hd = new HDNode(Q, chainCode) - var hdP = new HDNode(D, chainCode) + var hdP = new HDNode(d, chainCode) assert.strictEqual(hd.pubKey.compressed, true) assert.strictEqual(hdP.pubKey.compressed, true) @@ -50,7 +50,7 @@ describe('HDNode', function() { it('throws an exception when an unknown network is given', function() { assert.throws(function() { - new HDNode(D, chainCode, {}) + new HDNode(d, chainCode, {}) }, /Unknown BIP32 constants for network/) }) }) diff --git a/test/message.js b/test/message.js index 8a798ee..a0aa730 100644 --- a/test/message.js +++ b/test/message.js @@ -48,12 +48,12 @@ describe('Message', function() { it(f.description, function() { var network = networks[f.network] - var privKey = new ECKey(new BigInteger(f.D), false) + var privKey = new ECKey(new BigInteger(f.d), false) var signature = Message.sign(privKey, f.message, network) assert.equal(signature.toString('base64'), f.signature) if (f.compressed) { - var compressedPrivKey = new ECKey(new BigInteger(f.D)) + var compressedPrivKey = new ECKey(new BigInteger(f.d)) var compressedSignature = Message.sign(compressedPrivKey, f.message) assert.equal(compressedSignature.toString('base64'), f.compressed.signature)