Comparing version 1.0.1 to 2.0.0
@@ -16,3 +16,3 @@ var curve = require('../curve'); | ||
Base.call(this, 'mont', conf); | ||
Base.call(this, 'edwards', conf); | ||
@@ -19,0 +19,0 @@ this.a = new bn(conf.a, 16).mod(this.red.m).toRed(this.red); |
@@ -28,3 +28,3 @@ var curve = require('../curve'); | ||
return Point.fromJSON(this, obj); | ||
} | ||
}; | ||
@@ -31,0 +31,0 @@ MontCurve.prototype.validate = function validate(point) { |
@@ -38,6 +38,14 @@ var bn = require('bn.js'); | ||
EC.prototype.keyPair = function keyPair(priv, pub) { | ||
return new KeyPair(this, priv, pub); | ||
EC.prototype.keyPair = function keyPair(options) { | ||
return new KeyPair(this, options); | ||
}; | ||
EC.prototype.keyFromPrivate = function keyFromPrivate(priv, enc) { | ||
return KeyPair.fromPrivate(this, priv, enc); | ||
}; | ||
EC.prototype.keyFromPublic = function keyFromPublic(pub, enc) { | ||
return KeyPair.fromPublic(this, pub, enc); | ||
}; | ||
EC.prototype.genKeyPair = function genKeyPair(options) { | ||
@@ -63,3 +71,3 @@ if (!options) | ||
priv.iaddn(1); | ||
return this.keyPair(priv); | ||
return this.keyFromPrivate(priv); | ||
} while (true); | ||
@@ -78,8 +86,13 @@ }; | ||
EC.prototype.sign = function sign(msg, key, options) { | ||
key = this.keyPair(key, 'hex'); | ||
msg = this._truncateToN(new bn(msg, 16)); | ||
EC.prototype.sign = function sign(msg, key, enc, options) { | ||
if (typeof enc === 'object') { | ||
options = enc; | ||
enc = null; | ||
} | ||
if (!options) | ||
options = {}; | ||
key = this.keyFromPrivate(key, enc); | ||
msg = this._truncateToN(new bn(msg, 16)); | ||
// Zero-extend key to provide enough entropy | ||
@@ -131,5 +144,5 @@ var bytes = this.n.byteLength(); | ||
EC.prototype.verify = function verify(msg, signature, key) { | ||
EC.prototype.verify = function verify(msg, signature, key, enc) { | ||
msg = this._truncateToN(new bn(msg, 16)); | ||
key = this.keyPair(key, 'hex'); | ||
key = this.keyFromPublic(key, enc); | ||
signature = new Signature(signature, 'hex'); | ||
@@ -136,0 +149,0 @@ |
@@ -7,24 +7,3 @@ var bn = require('bn.js'); | ||
function KeyPair(ec, priv, pub) { | ||
if (priv instanceof KeyPair) | ||
return priv; | ||
if (pub instanceof KeyPair) | ||
return pub; | ||
if (!priv) { | ||
priv = pub; | ||
pub = null; | ||
} | ||
if (priv !== null && typeof priv === 'object') { | ||
if (priv.x) { | ||
// KeyPair(public) | ||
pub = priv; | ||
priv = null; | ||
} else if (priv.priv || priv.pub) { | ||
// KeyPair({ priv: ..., pub: ... }) | ||
pub = priv.pub; | ||
priv = priv.priv; | ||
} | ||
} | ||
function KeyPair(ec, options) { | ||
this.ec = ec; | ||
@@ -34,17 +13,30 @@ this.priv = null; | ||
// KeyPair(public, 'hex') | ||
if (this._importPublicHex(priv, pub)) | ||
return; | ||
if (pub === 'hex') | ||
pub = null; | ||
// KeyPair(priv, pub) | ||
if (priv) | ||
this._importPrivate(priv); | ||
if (pub) | ||
this._importPublic(pub); | ||
// KeyPair(ec, { priv: ..., pub: ... }) | ||
if (options.priv) | ||
this._importPrivate(options.priv, options.privEnc); | ||
if (options.pub) | ||
this._importPublic(options.pub, options.pubEnc); | ||
} | ||
module.exports = KeyPair; | ||
KeyPair.fromPublic = function fromPublic(ec, pub, enc) { | ||
if (pub instanceof KeyPair) | ||
return pub; | ||
return new KeyPair(ec, { | ||
pub: pub, | ||
pubEnc: enc | ||
}); | ||
}; | ||
KeyPair.fromPrivate = function fromPrivate(ec, priv, enc) { | ||
if (priv instanceof KeyPair) | ||
return priv; | ||
return new KeyPair(ec, { | ||
priv: priv, | ||
privEnc: enc | ||
}); | ||
}; | ||
KeyPair.prototype.validate = function validate() { | ||
@@ -82,10 +74,15 @@ var pub = this.getPublic(); | ||
if (compact) { | ||
var res = [ this.pub.getY().isEven() ? 0x02 : 0x03 ].concat(x); | ||
if (this.ec.curve.type !== 'mont') { | ||
if (compact) { | ||
var res = [ this.pub.getY().isEven() ? 0x02 : 0x03 ].concat(x); | ||
} else { | ||
var y = this.pub.getY().toArray(); | ||
for (var i = y.length; i < len; i++) | ||
y.unshift(0); | ||
var res = [ 0x04 ].concat(x, y); | ||
} | ||
} else { | ||
var y = this.pub.getY().toArray(); | ||
for (var i = y.length; i < len; i++) | ||
y.unshift(0); | ||
var res = [ 0x04 ].concat(x, y); | ||
res = x; | ||
} | ||
return utils.encode(res, enc); | ||
@@ -101,3 +98,3 @@ }; | ||
KeyPair.prototype._importPrivate = function _importPrivate(key) { | ||
KeyPair.prototype._importPrivate = function _importPrivate(key, enc) { | ||
this.priv = new bn(key, 16); | ||
@@ -110,8 +107,16 @@ | ||
KeyPair.prototype._importPublic = function _importPublic(key) { | ||
this.pub = this.ec.curve.point(key.x, key.y); | ||
KeyPair.prototype._importPublic = function _importPublic(key, enc) { | ||
if (key.x || key.y) { | ||
this.pub = this.ec.curve.point(key.x, key.y); | ||
return; | ||
} | ||
key = utils.toArray(key, enc); | ||
if (this.ec.curve.type !== 'mont') | ||
return this._importPublicShort(key); | ||
else | ||
return this._importPublicMont(key); | ||
}; | ||
KeyPair.prototype._importPublicHex = function _importPublic(key, enc) { | ||
key = utils.toArray(key, enc); | ||
KeyPair.prototype._importPublicShort = function _importPublicShort(key) { | ||
var len = this.ec.curve.p.byteLength(); | ||
@@ -125,7 +130,7 @@ if (key[0] === 0x04 && key.length - 1 === 2 * len) { | ||
key.slice(1, 1 +len)); | ||
} else { | ||
return false; | ||
} | ||
}; | ||
return true; | ||
KeyPair.prototype._importPublicMont = function _importPublicMont(key) { | ||
this.pub = this.ec.curve.point(key, 1); | ||
}; | ||
@@ -132,0 +137,0 @@ |
{ | ||
"name": "elliptic", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "EC cryptography", | ||
@@ -5,0 +5,0 @@ "main": "lib/elliptic.js", |
@@ -15,2 +15,8 @@ var assert = require('assert'); | ||
assert.equal(sh1.toString(16), sh2.toString(16)); | ||
var sh1 = s1.derive(ecdh.keyFromPublic(s2.getPublic('hex'), 'hex') | ||
.getPublic()); | ||
var sh2 = s2.derive(ecdh.keyFromPublic(s1.getPublic('hex'), 'hex') | ||
.getPublic()); | ||
assert.equal(sh1.toString(16), sh2.toString(16)); | ||
}); | ||
@@ -17,0 +23,0 @@ } |
@@ -37,3 +37,3 @@ var assert = require('assert'); | ||
// Load private key from hex | ||
var keys = ecdsa.keyPair(keys.getPrivate('hex'), 'hex'); | ||
var keys = ecdsa.keyFromPrivate(keys.getPrivate('hex'), 'hex'); | ||
var signature = ecdsa.sign(msg, keys); | ||
@@ -43,6 +43,6 @@ assert(ecdsa.verify(msg, signature, keys), 'hex-private verify'); | ||
// Load public key from compact hex | ||
var keys = ecdsa.keyPair(keys.getPublic(true, 'hex'), 'hex'); | ||
var keys = ecdsa.keyFromPublic(keys.getPublic(true, 'hex'), 'hex'); | ||
// Load public key from hex | ||
var keys = ecdsa.keyPair(keys.getPublic('hex'), 'hex'); | ||
var keys = ecdsa.keyFromPublic(keys.getPublic('hex'), 'hex'); | ||
@@ -60,4 +60,4 @@ // DER encoding | ||
// Invalid private key | ||
var keys = ecdsa.keyPair(keys.getPrivate('hex') + keys.getPrivate('hex'), | ||
'hex'); | ||
var keys = ecdsa.keyFromPrivate(keys.getPrivate('hex') + | ||
keys.getPrivate('hex')); | ||
assert(!ecdsa.verify(msg, signature, keys), 'Wrong key verify'); | ||
@@ -83,3 +83,3 @@ }); | ||
assert.equal(sign.s.toString(16), c.s); | ||
assert.ok(ecdsa.keyPair(opt.pub).validate().result, | ||
assert.ok(ecdsa.keyFromPublic(opt.pub).validate().result, | ||
'Invalid public key'); | ||
@@ -86,0 +86,0 @@ assert.ok(ecdsa.verify(dgst, sign, opt.pub), |
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
137374
3633