Skip to content
Merged
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
12 changes: 6 additions & 6 deletions src/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions src/eckey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
6 changes: 3 additions & 3 deletions src/hdnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
])

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/bitcoin.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/ec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 16 additions & 16 deletions test/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ var fixtures = require('./fixtures/ecdsa.json')

describe('ecdsa', function() {
describe('deterministicGenerateK', function() {
it('matches the test vectors', function() {
fixtures.valid.forEach(function(f) {
var D = BigInteger.fromHex(f.D)
fixtures.valid.forEach(function(f) {
it('determines k for \"' + f.message + '\"', function() {
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)
})
})
})

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)
Expand All @@ -40,11 +40,11 @@ describe('ecdsa', function() {
})

describe('sign', function() {
it('matches the test vectors', function() {
fixtures.valid.forEach(function(f) {
var D = BigInteger.fromHex(f.D)
fixtures.valid.forEach(function(f) {
it('produces a deterministic signature for \"' + f.message + '\"', function() {
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)
Expand All @@ -62,10 +62,10 @@ 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)
fixtures.valid.forEach(function(f) {
it('verifies a valid signature for \"' + f.message + '\"', function() {
var d = BigInteger.fromHex(f.d)
var Q = ecparams.getG().multiply(d)

var signature = {
r: new BigInteger(f.signature.r),
Expand All @@ -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)
})
Expand Down
13 changes: 7 additions & 6 deletions test/eckey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
Expand All @@ -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)
})
})
Expand Down
24 changes: 12 additions & 12 deletions test/fixtures/ecdsa.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"valid": [
{
"D": "01",
"d": "01",
"k": "ec633bd56a5774a0940cb97e27a9e4e51dc94af737596a0c5cbb3d30332d92a5",
"message": "Everything should be made as simple as possible, but not simpler.",
"compact": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand All @@ -61,7 +61,7 @@
}
},
{
"D": "69ec59eaa1f4f2e36b639716b7c30ca86d9a5375c7b38d8918bd9c0ebc80ba64",
"d": "69ec59eaa1f4f2e36b639716b7c30ca86d9a5375c7b38d8918bd9c0ebc80ba64",
"k": "6bb4a594ad57c1aa22dbe991a9d8501daf4688bf50a4892ef21bd7c711afda97",
"message": "Computer science is no more about computers than astronomy is about telescopes.",
"compact": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -146,7 +146,7 @@
"verifyRaw": [
{
"description": "The wrong signature",
"D": "01",
"d": "01",
"e": "06ef2b193b83b3d701f765f1db34672ab84897e1252343cc2197829af3a30456",
"signature": {
"r": "38341707918488238920692284707283974715538935465589664377561695343399725051885",
Expand All @@ -155,7 +155,7 @@
},
{
"description": "Invalid r value (== 0)",
"D": "01",
"d": "01",
"e": "01",
"signature": {
"r": "00",
Expand All @@ -164,7 +164,7 @@
},
{
"description": "Invalid r value (>= n)",
"D": "01",
"d": "01",
"e": "01",
"signature": {
"r": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141",
Expand All @@ -173,7 +173,7 @@
},
{
"description": "Invalid s value (== 0)",
"D": "01",
"d": "01",
"e": "01",
"signature": {
"r": "02",
Expand All @@ -182,7 +182,7 @@
},
{
"description": "Invalid s value (>= n)",
"D": "01",
"d": "01",
"e": "01",
"signature": {
"r": "02",
Expand Down
Loading