Merge pull request #207 from dcousens/genclean

General cleanup
This commit is contained in:
Kyle Drake 2014-06-08 17:23:50 -07:00
commit e01f1d0b6d
14 changed files with 90 additions and 82 deletions

View file

@ -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)

View file

@ -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

View file

@ -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) {

View file

@ -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)
})
})

View file

@ -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)

View file

@ -13,12 +13,12 @@ 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)
})
})
@ -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)
@ -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)
@ -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),
@ -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)
})

View file

@ -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)
})
})

View file

@ -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",

View file

@ -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": [

View file

@ -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="
}
]

View file

@ -6,6 +6,7 @@
"type": "pubkey",
"hash": "26e645ab170255f2a0a82d29e48f35b14ae7c826",
"pubKey": "031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95",
"asm": "33 031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95 OP_CHECKSIG",
"scriptPubKey": true
},
{
@ -13,6 +14,7 @@
"hex": "a914e8c300c87986efa84c37c0519929019ef86eb5b487",
"type": "scripthash",
"hash": "0ba47b56a573bab4b430ad6ed3ec79270e04b066",
"asm": "OP_HASH160 20 e8c300c87986efa84c37c0519929019ef86eb5b4 OP_EQUAL",
"scriptPubKey": true
},
{
@ -20,6 +22,7 @@
"hex": "76a9145a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a888ac",
"type": "pubkeyhash",
"hash": "a5313f33d5c7b81674b35f7f3febc3522ef234db",
"asm": "OP_DUP OP_HASH160 20 5a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a8 OP_EQUALVERIFY OP_CHECKSIG",
"scriptPubKey": true
},
{
@ -27,6 +30,7 @@
"hex": "48304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f30141040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8",
"type": "pubkeyhash",
"hash": "b9bac2a5c5c29bb27c382d41fa3d179c646c78fd",
"asm": "72 304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301 65 040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8",
"scriptPubKey": false
},
{
@ -34,6 +38,7 @@
"hex": "5121032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca330162102308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a52ae",
"type": "multisig",
"hash": "f1c98f0b74ecabcf78ae20dfa224bb6666051fbe",
"asm": "OP_TRUE 33 032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca33016 33 02308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a OP_2 OP_CHECKMULTISIG",
"scriptPubKey": true
},
{
@ -41,6 +46,7 @@
"hex":"6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474",
"type": "nulldata",
"hash": "ec88f016655477663455fe6a8e83508c348ea145",
"asm": "OP_RETURN 38 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474",
"scriptPubKey": true
},
{
@ -48,14 +54,15 @@
"hex": "aa206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000087",
"type": "nonstandard",
"hash": "3823382e70d1930989813d3459988e0d7c2861d8",
"asm": "OP_HASH256 32 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000 OP_EQUAL",
"scriptPubKey": true
},
{
"description": "Invalid multisig script",
"asm": "0 0 0 OP_CHECKmulTISIG",
"hex": "000000ae",
"type": "nonstandard",
"hash": "62ede8963f9387544935f168745262f703dab1fb",
"asm": "0 0 0 OP_CHECKMULTISIG",
"scriptPubKey": true
}
]

View file

@ -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/)
})
})

View file

@ -9,8 +9,8 @@ var fixtures = require('./fixtures/message.json')
describe('Message', function() {
describe('magicHash', function() {
it('matches the test vectors', function() {
fixtures.valid.magicHash.forEach(function(f) {
fixtures.valid.magicHash.forEach(function(f) {
it('produces the correct magicHash for \"' + f.message + '\" (' + f.network + ')', function() {
var network = networks[f.network]
var actual = Message.magicHash(f.message, network)
@ -20,8 +20,8 @@ describe('Message', function() {
})
describe('verify', function() {
it('verifies a valid signature', function() {
fixtures.valid.verify.forEach(function(f) {
fixtures.valid.verify.forEach(function(f) {
it('verifies a valid signature for \"' + f.message + '\" (' + f.network + ')', function() {
var network = networks[f.network]
var signature = new Buffer(f.signature, 'base64')
@ -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)

View file

@ -36,8 +36,8 @@ describe('Script', function() {
})
describe('getHash', function() {
it('matches the test vectors', function() {
fixtures.valid.forEach(function(f) {
fixtures.valid.forEach(function(f) {
it('produces a HASH160 of \"' + f.asm + '\"', function() {
var script = Script.fromHex(f.hex)
assert.equal(script.getHash().toString('hex'), f.hash)