Comparing version 0.6.0 to 0.6.1
@@ -10,2 +10,3 @@ var assert = require('assert'); | ||
this.b = new bn(conf.b, 16).toMont(this.mont); | ||
this.tinv = new bn(2).invm(this.p).toMont(this.mont); | ||
} | ||
@@ -79,7 +80,13 @@ module.exports = Curve; | ||
for (var i = naf.length - 1; i >= 0; i--) { | ||
// Count zeroes | ||
for (var k = 0; i >= 0 && naf[i] === 0; i--) | ||
k++; | ||
if (i >= 0) | ||
k++; | ||
acc = acc.dbl(k); | ||
if (i < 0) | ||
break; | ||
var z = naf[i]; | ||
acc = acc.dbl(); | ||
if (z === 0) | ||
continue; | ||
assert(z !== 0); | ||
if (z > 0) | ||
@@ -189,3 +196,4 @@ acc = acc.add(wnd[(z - 1) >> 1]); | ||
Point.prototype.dbl = function dbl() { | ||
Point.prototype.dbl = function dbl(pow) { | ||
assert(!pow, 'Power in supported in regular point .dbl()'); | ||
if (this.inf) | ||
@@ -359,14 +367,20 @@ return this; | ||
JPoint.prototype.dbl = function dbl() { | ||
JPoint.prototype.dbl = function dbl(pow) { | ||
if (pow === 0) | ||
return this; | ||
if (this.isInfinity()) | ||
return this; | ||
if (!pow) | ||
pow = 1; | ||
if (this.precomputed && this.precomputed.length) { | ||
var cached = this.precomputed[0]; | ||
var i = Math.min(this.precomputed.length, pow); | ||
var cached = this.precomputed[i - 1]; | ||
var res = this.curve.point(cached.x, cached.y); | ||
if (this.precomputed.length > 1) | ||
res.precomputed = this.precomputed.slice(1); | ||
return res.toJ(); | ||
return res.toJ().dbl(pow - i); | ||
} | ||
var a = this.curve.a; | ||
var tinv = this.curve.tinv; | ||
@@ -376,15 +390,40 @@ var jx = this.x; | ||
var jz = this.z; | ||
var jx2 = jx.montSqr(); | ||
var jy2 = jy.montSqr(); | ||
var jz4 = jz.montSqr().montSqr(); | ||
var c = jx2.montMul(3).montAdd(a.montMul(jz4)); | ||
var t1 = jx.montShl(2).montMul(jy2); | ||
var nx = c.montSqr().montSub(t1.montShl(1)); | ||
var t2 = t1.montSub(nx); | ||
var ny = c.montMul(t2).montSub(jy2.montSqr().montShl(3)); | ||
var nz = jy.montShl(1).montMul(jz); | ||
if (pow === 1) { | ||
var jx2 = jx.montSqr(); | ||
var jy2 = jy.montSqr(); | ||
var c = jx2.montMul(3).montAdd(a.montMul(jz4)); | ||
return this.curve.jpoint(nx, ny, nz); | ||
var t1 = jx.montShl(2).montMul(jy2); | ||
var nx = c.montSqr().montSub(t1.montShl(1)); | ||
var t2 = t1.montSub(nx); | ||
var ny = c.montMul(t2).montSub(jy2.montSqr().montShl(3)); | ||
var nz = jy.montShl(1).montMul(jz); | ||
return this.curve.jpoint(nx, ny, nz); | ||
} | ||
// Reuse results | ||
var jyd = jy.montShl(1); | ||
for (var i = 0; i < pow; i++) { | ||
var jx2 = jx.montSqr(); | ||
var jyd2 = jyd.montSqr(); | ||
var jyd4 = jyd2.montSqr(); | ||
var c = jx2.montMul(3).montAdd(a.montMul(jz4)); | ||
var t1 = jx.montMul(jyd2); | ||
var nx = c.montSqr().montSub(t1.montShl(1)); | ||
var t2 = t1.montSub(nx); | ||
var dny = c.montMul(t2).montShl(1).montSub(jyd4); | ||
var nz = jyd.montMul(jz); | ||
if (i + 1 < pow) | ||
jz4 = jz4.montMul(jyd4); | ||
jx = nx; | ||
jz = nz; | ||
jyd = dny; | ||
} | ||
return this.curve.jpoint(jx, jyd.montMul(tinv), jz); | ||
}; | ||
@@ -391,0 +430,0 @@ |
@@ -33,2 +33,9 @@ var assert = require('assert'); | ||
// KeyPair(public, 'hex') | ||
if (this._importPublicHex(priv, pub)) | ||
return; | ||
if (pub === 'hex') | ||
pub = null; | ||
// KeyPair(priv, pub) | ||
@@ -58,5 +65,18 @@ if (priv) | ||
this.pub = this.ecdsa.g.mul(this.priv); | ||
if (enc === 'hex') | ||
return '04' + this.pub.x.toString(16) + this.pub.y.toString(16); | ||
return this.pub; | ||
if (!enc) | ||
return this.pub; | ||
var len = this.ecdsa.curve.p.byteLength(); | ||
var x = this.pub.getX().toArray(); | ||
var y = this.pub.getY().toArray(); | ||
// Ensure that both x and y have enough bits | ||
for (var i = x.length; i < len; i++) | ||
x.unshift(0); | ||
for (var i = y.length; i < len; i++) | ||
y.unshift(0); | ||
var res = [ 0x04 ].concat(x, y); | ||
return utils.encode(res, enc); | ||
}; | ||
@@ -78,1 +98,14 @@ | ||
}; | ||
KeyPair.prototype._importPublicHex = function _importPublic(key, enc) { | ||
key = utils.toArray(key, enc); | ||
var len = this.ecdsa.curve.p.byteLength(); | ||
if (key[0] !== 0x04 || key.length - 1 !== 2 * len) | ||
return false; | ||
this.pub = this.ecdsa.curve.point( | ||
key.slice(1, 1 + len), | ||
key.slice(1 + len, 1 + 2 * len)); | ||
return true; | ||
}; |
{ | ||
"name": "elliptic", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "EC cryptography", | ||
@@ -5,0 +5,0 @@ "main": "lib/elliptic.js", |
@@ -16,4 +16,4 @@ var assert = require('assert'); | ||
assert(p.dbl().add(p).validate()); | ||
assert(p.dbl().add(p.dbl(p)).validate()); | ||
assert(p.dbl().add(p.dbl(p)).eq(p.add(p).add(p).add(p))); | ||
assert(p.dbl().add(p.dbl()).validate()); | ||
assert(p.dbl().add(p.dbl()).eq(p.add(p).add(p).add(p))); | ||
}); | ||
@@ -20,0 +20,0 @@ |
@@ -24,2 +24,10 @@ var assert = require('assert'); | ||
// Load private key from hex | ||
var keys = ecdsa.keyPair(keys.getPrivate('hex'), 'hex'); | ||
var signature = ecdsa.sign(msg, keys); | ||
assert(ecdsa.verify(msg, signature, keys), 'hex-private verify'); | ||
// Load public key from hex | ||
var keys = ecdsa.keyPair(keys.getPublic('hex'), 'hex'); | ||
// DER encoding | ||
@@ -26,0 +34,0 @@ var dsign = signature.toDER('hex'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
99266
2462
19