From 069f67e9245f2dc3872a801ba0a38d4f11702b8d Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 11 Mar 2014 15:21:24 -0300 Subject: [PATCH] fix Key tests --- Key.js | 5 +++-- browser/vendor.js | 34 ++++++++++++++++++++++------------ browser/vendor/ecdsa.js | 34 ++++++++++++++++++++++------------ 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/Key.js b/Key.js index 48f661b..49db7d0 100644 --- a/Key.js +++ b/Key.js @@ -89,10 +89,11 @@ if (process.versions) { } var eck = new ECKey(); - eck.setPub( bufferToArray(self.public)); + eck.setPub(bufferToArray(self.public)); eck.setCompressed(self.compressed); var sigA = bufferToArray(sig); - return eck.verify(hash,sigA); + var ret = eck.verify(hash,sigA); + return ret; }; diff --git a/browser/vendor.js b/browser/vendor.js index c6f7c4a..9081d23 100644 --- a/browser/vendor.js +++ b/browser/vendor.js @@ -2284,24 +2284,34 @@ ECPointFp.prototype.getEncoded = function (compressed) { return enc; }; -ECPointFp.decodeFrom = function (curve, enc) { +ECPointFp.decodeFrom = function (ecparams, enc) { var type = enc[0]; var dataLen = enc.length-1; // Extract x and y as byte arrays - var xBa = enc.slice(1, 1 + dataLen/2); - var yBa = enc.slice(1 + dataLen/2, 1 + dataLen); - - // Prepend zero byte to prevent interpretation as negative integer - xBa.unshift(0); - yBa.unshift(0); - - // Convert to BigIntegers - var x = new BigInteger(xBa); - var y = new BigInteger(yBa); + if (type === 4) { + var xBa = enc.slice(1, 1 + dataLen/2), + yBa = enc.slice(1 + dataLen/2, 1 + dataLen), + x = BigInteger.fromByteArrayUnsigned(xBa), + y = BigInteger.fromByteArrayUnsigned(yBa); + } + else { + var xBa = enc.slice(1), + x = BigInteger.fromByteArrayUnsigned(xBa), + p = ecparams.getQ(), + xCubedPlus7 = x.multiply(x).multiply(x).add(new BigInteger('7')).mod(p), + pPlus1Over4 = p.add(new BigInteger('1')) + .divide(new BigInteger('4')), + y = xCubedPlus7.modPow(pPlus1Over4,p); + if (y.mod(new BigInteger('2')).toString() != ''+(type % 2)) { + y = p.subtract(y) + } + } // Return point - return new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)); + return new ECPointFp(ecparams, + ecparams.fromBigInteger(x), + ecparams.fromBigInteger(y)); }; ECPointFp.prototype.add2D = function (b) { diff --git a/browser/vendor/ecdsa.js b/browser/vendor/ecdsa.js index a2497bf..57e0801 100644 --- a/browser/vendor/ecdsa.js +++ b/browser/vendor/ecdsa.js @@ -41,24 +41,34 @@ ECPointFp.prototype.getEncoded = function (compressed) { return enc; }; -ECPointFp.decodeFrom = function (curve, enc) { +ECPointFp.decodeFrom = function (ecparams, enc) { var type = enc[0]; var dataLen = enc.length-1; // Extract x and y as byte arrays - var xBa = enc.slice(1, 1 + dataLen/2); - var yBa = enc.slice(1 + dataLen/2, 1 + dataLen); - - // Prepend zero byte to prevent interpretation as negative integer - xBa.unshift(0); - yBa.unshift(0); - - // Convert to BigIntegers - var x = new BigInteger(xBa); - var y = new BigInteger(yBa); + if (type === 4) { + var xBa = enc.slice(1, 1 + dataLen/2), + yBa = enc.slice(1 + dataLen/2, 1 + dataLen), + x = BigInteger.fromByteArrayUnsigned(xBa), + y = BigInteger.fromByteArrayUnsigned(yBa); + } + else { + var xBa = enc.slice(1), + x = BigInteger.fromByteArrayUnsigned(xBa), + p = ecparams.getQ(), + xCubedPlus7 = x.multiply(x).multiply(x).add(new BigInteger('7')).mod(p), + pPlus1Over4 = p.add(new BigInteger('1')) + .divide(new BigInteger('4')), + y = xCubedPlus7.modPow(pPlus1Over4,p); + if (y.mod(new BigInteger('2')).toString() != ''+(type % 2)) { + y = p.subtract(y) + } + } // Return point - return new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)); + return new ECPointFp(ecparams, + ecparams.fromBigInteger(x), + ecparams.fromBigInteger(y)); }; ECPointFp.prototype.add2D = function (b) {