From da48c988f63cf0df0ec92a3eb02e2e2b15c17f7a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 16 May 2014 11:30:28 +1000 Subject: [PATCH 1/5] sec: use a hashtable for lookup --- src/sec.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/sec.js b/src/sec.js index 79bc51a..cb4616e 100644 --- a/src/sec.js +++ b/src/sec.js @@ -193,17 +193,17 @@ function secp256r1() { return new X9ECParameters(curve, G, n, h); } -// TODO: make this into a proper hashtable function getSECCurveByName(name) { - if(name == "secp128r1") return secp128r1(); - if(name == "secp160k1") return secp160k1(); - if(name == "secp160r1") return secp160r1(); - if(name == "secp192k1") return secp192k1(); - if(name == "secp192r1") return secp192r1(); - if(name == "secp224r1") return secp224r1(); - if(name == "secp256k1") return secp256k1(); - if(name == "secp256r1") return secp256r1(); - return null; + return ({ + "secp128r1": secp128r1, + "secp160k1": secp160k1, + "secp160r1": secp160r1, + "secp192k1": secp192k1, + "secp192r1": secp192r1, + "secp224r1": secp224r1, + "secp256k1": secp256k1, + "secp256r1": secp256r1 + }[name])() } -module.exports = getSECCurveByName; +module.exports = getSECCurveByName From b8858c0bc921c393d701bdcddc03f43a62687829 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 16 May 2014 11:31:08 +1000 Subject: [PATCH 2/5] sec: use BigInteger.fromHex directly --- src/sec.js | 178 ++++++++++++++++++++++++++--------------------------- 1 file changed, 88 insertions(+), 90 deletions(-) diff --git a/src/sec.js b/src/sec.js index cb4616e..ab0f84e 100644 --- a/src/sec.js +++ b/src/sec.js @@ -39,158 +39,156 @@ X9ECParameters.prototype.getH = x9getH; // ---------------- // SECNamedCurves -function fromHex(s) { return new BigInteger(s, 16); } - function secp128r1() { // p = 2^128 - 2^97 - 1 - var p = fromHex("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF"); - var a = fromHex("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC"); - var b = fromHex("E87579C11079F43DD824993C2CEE5ED3"); - //byte[] S = fromHex("000E0D4D696E6768756151750CC03A4473D03679"); - var n = fromHex("FFFFFFFE0000000075A30D1B9038A115"); - var h = BigInteger.ONE; - var curve = new ECCurveFp(p, a, b); - - var x = fromHex("161FF7528B899B2D0C28607CA52C5B86") - var y = fromHex("CF5AC8395BAFEB13C02DA292DDED7A83") + var p = BigInteger.fromHex("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF") + var a = BigInteger.fromHex("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC") + var b = BigInteger.fromHex("E87579C11079F43DD824993C2CEE5ED3") + //byte[] S = BigInteger.fromHex("000E0D4D696E6768756151750CC03A4473D03679") + var n = BigInteger.fromHex("FFFFFFFE0000000075A30D1B9038A115") + var h = BigInteger.ONE + var curve = new ECCurveFp(p, a, b) + + var x = BigInteger.fromHex("161FF7528B899B2D0C28607CA52C5B86") + var y = BigInteger.fromHex("CF5AC8395BAFEB13C02DA292DDED7A83") var G = new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)) - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, n, h) } function secp160k1() { // p = 2^160 - 2^32 - 2^14 - 2^12 - 2^9 - 2^8 - 2^7 - 2^3 - 2^2 - 1 - var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73"); - var a = BigInteger.ZERO; - var b = fromHex("7"); - //byte[] S = null; - var n = fromHex("0100000000000000000001B8FA16DFAB9ACA16B6B3"); - var h = BigInteger.ONE; - var curve = new ECCurveFp(p, a, b); - - var x = fromHex("3B4C382CE37AA192A4019E763036F4F5DD4D7EBB") - var y = fromHex("938CF935318FDCED6BC28286531733C3F03C4FEE") + var p = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73") + var a = BigInteger.ZERO + var b = BigInteger.fromHex("07") + //byte[] S = null + var n = BigInteger.fromHex("0100000000000000000001B8FA16DFAB9ACA16B6B3") + var h = BigInteger.ONE + var curve = new ECCurveFp(p, a, b) + + var x = BigInteger.fromHex("3B4C382CE37AA192A4019E763036F4F5DD4D7EBB") + var y = BigInteger.fromHex("938CF935318FDCED6BC28286531733C3F03C4FEE") var G = new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)) - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, n, h) } function secp160r1() { // p = 2^160 - 2^31 - 1 - var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"); - var a = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"); - var b = fromHex("1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45"); - //byte[] S = fromHex("1053CDE42C14D696E67687561517533BF3F83345"); - var n = fromHex("0100000000000000000001F4C8F927AED3CA752257"); - var h = BigInteger.ONE; - var curve = new ECCurveFp(p, a, b); - - var x = fromHex("4A96B5688EF573284664698968C38BB913CBFC82") - var y = fromHex("23A628553168947D59DCC912042351377AC5FB32") + var p = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF") + var a = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC") + var b = BigInteger.fromHex("1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45") + //byte[] S = BigInteger.fromHex("1053CDE42C14D696E67687561517533BF3F83345") + var n = BigInteger.fromHex("0100000000000000000001F4C8F927AED3CA752257") + var h = BigInteger.ONE + var curve = new ECCurveFp(p, a, b) + + var x = BigInteger.fromHex("4A96B5688EF573284664698968C38BB913CBFC82") + var y = BigInteger.fromHex("23A628553168947D59DCC912042351377AC5FB32") var G = new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)) - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, n, h) } function secp192k1() { // p = 2^192 - 2^32 - 2^12 - 2^8 - 2^7 - 2^6 - 2^3 - 1 - var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37"); - var a = BigInteger.ZERO; - var b = fromHex("3"); - //byte[] S = null; - var n = fromHex("FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D"); - var h = BigInteger.ONE; - var curve = new ECCurveFp(p, a, b); - - var x = fromHex("DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D") - var y = fromHex("9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D") + var p = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37") + var a = BigInteger.ZERO + var b = BigInteger.fromHex("03") + //byte[] S = null + var n = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D") + var h = BigInteger.ONE + var curve = new ECCurveFp(p, a, b) + + var x = BigInteger.fromHex("DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D") + var y = BigInteger.fromHex("9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D") var G = new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)) - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, n, h) } function secp192r1() { // p = 2^192 - 2^64 - 1 - var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"); - var a = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"); - var b = fromHex("64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"); - //byte[] S = fromHex("3045AE6FC8422F64ED579528D38120EAE12196D5"); - var n = fromHex("FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"); - var h = BigInteger.ONE; - var curve = new ECCurveFp(p, a, b); - - var x = fromHex("188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012") - var y = fromHex("07192B95FFC8DA78631011ED6B24CDD573F977A11E794811") + var p = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF") + var a = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC") + var b = BigInteger.fromHex("64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1") + //byte[] S = BigInteger.fromHex("3045AE6FC8422F64ED579528D38120EAE12196D5") + var n = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831") + var h = BigInteger.ONE + var curve = new ECCurveFp(p, a, b) + + var x = BigInteger.fromHex("188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012") + var y = BigInteger.fromHex("07192B95FFC8DA78631011ED6B24CDD573F977A11E794811") var G = new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)) - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, n, h) } function secp224r1() { // p = 2^224 - 2^96 + 1 - var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"); - var a = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"); - var b = fromHex("B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"); - //byte[] S = fromHex("BD71344799D5C7FCDC45B59FA3B9AB8F6A948BC5"); - var n = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"); - var h = BigInteger.ONE; - var curve = new ECCurveFp(p, a, b); - - var x = fromHex("B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21") - var y = fromHex("BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34") + var p = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001") + var a = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE") + var b = BigInteger.fromHex("B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4") + //byte[] S = BigInteger.fromHex("BD71344799D5C7FCDC45B59FA3B9AB8F6A948BC5") + var n = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D") + var h = BigInteger.ONE + var curve = new ECCurveFp(p, a, b) + + var x = BigInteger.fromHex("B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21") + var y = BigInteger.fromHex("BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34") var G = new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)) - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, n, h) } function secp256k1() { // p = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1 - var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"); - var a = BigInteger.ZERO; - var b = fromHex("7"); - //byte[] S = null; - var n = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"); - var h = BigInteger.ONE; - var curve = new ECCurveFp(p, a, b); - - var x = fromHex("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798") - var y = fromHex("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8") + var p = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F") + var a = BigInteger.ZERO + var b = BigInteger.fromHex("07") + //byte[] S = null + var n = BigInteger.fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141") + var h = BigInteger.ONE + var curve = new ECCurveFp(p, a, b) + + var x = BigInteger.fromHex("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798") + var y = BigInteger.fromHex("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8") var G = new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)) - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, n, h) } function secp256r1() { // p = 2^224 (2^32 - 1) + 2^192 + 2^96 - 1 - var p = fromHex("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"); - var a = fromHex("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC"); - var b = fromHex("5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"); - //byte[] S = fromHex("C49D360886E704936A6678E1139D26B7819F7E90"); - var n = fromHex("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"); - var h = BigInteger.ONE; - var curve = new ECCurveFp(p, a, b); - - var x = fromHex("6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296") - var y = fromHex("4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5") + var p = BigInteger.fromHex("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF") + var a = BigInteger.fromHex("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC") + var b = BigInteger.fromHex("5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B") + //byte[] S = BigInteger.fromHex("C49D360886E704936A6678E1139D26B7819F7E90") + var n = BigInteger.fromHex("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551") + var h = BigInteger.ONE + var curve = new ECCurveFp(p, a, b) + + var x = BigInteger.fromHex("6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296") + var y = BigInteger.fromHex("4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5") var G = new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)) - return new X9ECParameters(curve, G, n, h); + return new X9ECParameters(curve, G, n, h) } function getSECCurveByName(name) { From 1087ac07decfc74a75d4b1118cbf9ea23384374d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 16 May 2014 11:31:49 +1000 Subject: [PATCH 3/5] sec: fix formatting --- src/sec.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/sec.js b/src/sec.js index ab0f84e..3e25b80 100644 --- a/src/sec.js +++ b/src/sec.js @@ -9,32 +9,32 @@ var ECPointFp = ECCurveFp.ECPointFp // constructor function X9ECParameters(curve,g,n,h) { - this.curve = curve; - this.g = g; - this.n = n; - this.h = h; + this.curve = curve + this.g = g + this.n = n + this.h = h } function x9getCurve() { - return this.curve; + return this.curve } function x9getG() { - return this.g; + return this.g } function x9getN() { - return this.n; + return this.n } function x9getH() { - return this.h; + return this.h } -X9ECParameters.prototype.getCurve = x9getCurve; -X9ECParameters.prototype.getG = x9getG; -X9ECParameters.prototype.getN = x9getN; -X9ECParameters.prototype.getH = x9getH; +X9ECParameters.prototype.getCurve = x9getCurve +X9ECParameters.prototype.getG = x9getG +X9ECParameters.prototype.getN = x9getN +X9ECParameters.prototype.getH = x9getH // ---------------- // SECNamedCurves From cd326921bc38ce1aa5805ec00300506ca3102155 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 13 May 2014 19:10:20 +1000 Subject: [PATCH 4/5] bigi: upgrade to 1.1.0 --- package.json | 2 +- src/ecdsa.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 495e486..6633688 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "compile": "./node_modules/.bin/browserify ./src/index.js -s Bitcoin | ./node_modules/.bin/uglifyjs > bitcoinjs-min.js" }, "dependencies": { - "bigi": "1.0.0", + "bigi": "1.1.0", "crypto-js": "3.1.2-3", "secure-random": "0.2.1" } diff --git a/src/ecdsa.js b/src/ecdsa.js index 1536918..7d73a6d 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -88,8 +88,8 @@ function verifyRaw(ecparams, e, r, s, Q) { * Takes two BigIntegers representing r and s and returns a byte array. */ function serializeSig(r, s) { - var rBa = r.toByteArraySigned() - var sBa = s.toByteArraySigned() + var rBa = r.toDERInteger() + var sBa = s.toDERInteger() var sequence = [] sequence.push(0x02); // INTEGER @@ -130,8 +130,8 @@ function parseSig(buffer) { var sB = buffer.slice(2 + offset) return { - r: BigInteger.fromByteArraySigned(rB), - s: BigInteger.fromByteArraySigned(sB) + r: BigInteger.fromDERInteger(rB), + s: BigInteger.fromDERInteger(sB) } } From aca565239a985da27969ddf527ecade843baee5d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 17 May 2014 17:06:02 +1000 Subject: [PATCH 5/5] ECKey: remove unnecessary slice --- src/eckey.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/eckey.js b/src/eckey.js index 0cb1d45..b109ae3 100644 --- a/src/eckey.js +++ b/src/eckey.js @@ -35,7 +35,7 @@ ECKey.fromWIF = function(string) { assert.equal(payload.length, 32, 'Invalid WIF payload length') - var D = BigInteger.fromBuffer(payload.slice(0, 32)) + var D = BigInteger.fromBuffer(payload) return new ECKey(D, compressed) }