Comparing version 5.0.0 to 5.1.0
@@ -5,7 +5,7 @@ 'use strict'; | ||
var elliptic = require('../../elliptic'); | ||
var utils = elliptic.utils; | ||
var getNAF = utils.getNAF; | ||
var getJSF = utils.getJSF; | ||
var assert = utils.assert; | ||
var getNAF = elliptic.utils.getNAF; | ||
var getJSF = elliptic.utils.getJSF; | ||
var assert = elliptic.utils.assert; | ||
function BaseCurve(type, conf) { | ||
@@ -243,2 +243,6 @@ this.type = type; | ||
BasePoint.prototype.eq = function eq(/*other*/) { | ||
throw new Error('Not implemented'); | ||
}; | ||
BasePoint.prototype.validate = function validate() { | ||
@@ -248,2 +252,34 @@ return this.curve.validate(this); | ||
BaseCurve.prototype.decodePoint = function decodePoint(bytes, enc) { | ||
bytes = utils.toArray(bytes, enc); | ||
var len = this.p.byteLength(); | ||
if (bytes[0] === 0x04 && bytes.length - 1 === 2 * len) { | ||
return this.point(bytes.slice(1, 1 + len), | ||
bytes.slice(1 + len, 1 + 2 * len)); | ||
} else if ((bytes[0] === 0x02 || bytes[0] === 0x03) && | ||
bytes.length - 1 === len) { | ||
return this.pointFromX(bytes.slice(1, 1 + len), bytes[0] === 0x03); | ||
} | ||
throw new Error('Unknown point format'); | ||
}; | ||
BasePoint.prototype.encodeCompressed = function encodeCompressed(enc) { | ||
return this.encode(enc, true); | ||
}; | ||
BasePoint.prototype._encode = function _encode(compact) { | ||
var len = this.curve.p.byteLength(); | ||
var x = this.getX().toArray('be', len); | ||
if (compact) | ||
return [ this.getY().isEven() ? 0x02 : 0x03 ].concat(x); | ||
return [ 0x04 ].concat(x, this.getY().toArray('be', len)) ; | ||
}; | ||
BasePoint.prototype.encode = function encode(enc, compact) { | ||
return utils.encode(this._encode(compact), enc); | ||
}; | ||
BasePoint.prototype.precompute = function precompute(power) { | ||
@@ -250,0 +286,0 @@ if (this.precomputed) |
@@ -8,2 +8,5 @@ 'use strict'; | ||
var elliptic = require('../../elliptic'); | ||
var utils = elliptic.utils; | ||
function MontCurve(conf) { | ||
@@ -46,2 +49,6 @@ Base.call(this, 'mont', conf); | ||
MontCurve.prototype.decodePoint = function decodePoint(bytes, enc) { | ||
return this.point(utils.toArray(bytes, enc), 1); | ||
}; | ||
MontCurve.prototype.point = function point(x, z) { | ||
@@ -59,2 +66,6 @@ return new Point(this, x, z); | ||
Point.prototype._encode = function _encode() { | ||
return this.getX().toArray('be', this.curve.p.byteLength()); | ||
}; | ||
Point.fromJSON = function fromJSON(curve, obj) { | ||
@@ -153,2 +164,6 @@ return new Point(curve, obj[0], obj[1] || curve.one); | ||
Point.prototype.eq = function eq(other) { | ||
return this.getX().cmp(other.getX()) === 0; | ||
}; | ||
Point.prototype.normalize = function normalize() { | ||
@@ -155,0 +170,0 @@ this.x = this.x.redMul(this.z.redInvm()); |
@@ -5,5 +5,2 @@ 'use strict'; | ||
var elliptic = require('../../elliptic'); | ||
var utils = elliptic.utils; | ||
function KeyPair(ec, options) { | ||
@@ -56,5 +53,2 @@ this.ec = ec; | ||
KeyPair.prototype.getPublic = function getPublic(compact, enc) { | ||
if (!this.pub) | ||
this.pub = this.ec.g.mul(this.priv); | ||
// compact is optional argument | ||
@@ -66,26 +60,9 @@ if (typeof compact === 'string') { | ||
if (!this.pub) | ||
this.pub = this.ec.g.mul(this.priv); | ||
if (!enc) | ||
return this.pub; | ||
var len = this.ec.curve.p.byteLength(); | ||
var x = this.pub.getX().toArray(); | ||
for (var i = x.length; i < len; i++) | ||
x.unshift(0); | ||
var res; | ||
if (this.ec.curve.type !== 'mont') { | ||
if (compact) { | ||
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 { | ||
res = x; | ||
} | ||
return utils.encode(res, enc); | ||
return this.pub.encode(enc, compact); | ||
}; | ||
@@ -113,25 +90,5 @@ | ||
} | ||
key = utils.toArray(key, enc); | ||
if (this.ec.curve.type !== 'mont') | ||
return this._importPublicShort(key); | ||
else | ||
return this._importPublicMont(key); | ||
this.pub = this.ec.curve.decodePoint(key, enc); | ||
}; | ||
KeyPair.prototype._importPublicShort = function _importPublicShort(key) { | ||
var len = this.ec.curve.p.byteLength(); | ||
if (key[0] === 0x04 && key.length - 1 === 2 * len) { | ||
this.pub = this.ec.curve.point( | ||
key.slice(1, 1 + len), | ||
key.slice(1 + len, 1 + 2 * len)); | ||
} else if ((key[0] === 0x02 || key[0] === 0x03) && key.length - 1 === len) { | ||
this.pub = this.ec.curve.pointFromX(key.slice(1, 1 + len), key[0] === 0x03); | ||
} | ||
}; | ||
KeyPair.prototype._importPublicMont = function _importPublicMont(key) { | ||
this.pub = this.ec.curve.point(key, 1); | ||
}; | ||
// ECDH | ||
@@ -138,0 +95,0 @@ KeyPair.prototype.derive = function derive(pub) { |
@@ -92,3 +92,3 @@ 'use strict'; | ||
EDDSA.prototype.encodePoint = function encodePoint(point) { | ||
var enc = utils.intToLE(point.getY(), this.encodingLength); | ||
var enc = point.getY().toArray('le', this.encodingLength); | ||
enc[this.encodingLength - 1] |= point.getX().isOdd() ? 0x80 : 0; | ||
@@ -110,3 +110,3 @@ return enc; | ||
EDDSA.prototype.encodeInt = function encodeInt(num) { | ||
return utils.intToLE(num, this.encodingLength); | ||
return num.toArray('le', this.encodingLength); | ||
}; | ||
@@ -113,0 +113,0 @@ |
@@ -173,8 +173,1 @@ 'use strict'; | ||
function intToLE(num, padTo) { | ||
var bytes = num.toArray('le'); | ||
while (bytes.length < padTo) | ||
bytes.push(0); | ||
return bytes; | ||
} | ||
utils.intToLE = intToLE; |
{ | ||
"name": "elliptic", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "EC cryptography", | ||
@@ -33,3 +33,3 @@ "main": "lib/elliptic.js", | ||
"dependencies": { | ||
"bn.js": "^3.0.0", | ||
"bn.js": "^3.1.1", | ||
"brorand": "^1.0.1", | ||
@@ -36,0 +36,0 @@ "hash.js": "^1.0.0", |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var assert = require('assert'); | ||
@@ -165,1 +167,85 @@ var bn = require('bn.js'); | ||
}); | ||
describe('Point codec', function () { | ||
function makeShortTest(definition) { | ||
var curve = elliptic.curves.secp256k1.curve; | ||
return function() { | ||
var co = definition.coordinates; | ||
var p = curve.point(co.x, co.y); | ||
// Encodes as expected | ||
assert.equal(p.encode('hex'), definition.encoded); | ||
assert.equal(p.encodeCompressed('hex'), definition.compactEncoded); | ||
// Decodes as expected | ||
assert(curve.decodePoint(definition.encoded, 'hex').eq(p)); | ||
assert(curve.decodePoint(definition.compactEncoded, 'hex').eq(p)); | ||
}; | ||
} | ||
function makeMontTest(definition) { | ||
var curve = elliptic.curves.curve25519.curve; | ||
return function() { | ||
var co = definition.coordinates; | ||
var p = curve.point(co.x, co.z); | ||
var encoded = p.encode('hex'); | ||
var decoded = curve.decodePoint(encoded, 'hex'); | ||
assert(decoded.eq(p)); | ||
assert.equal(encoded, definition.encoded); | ||
}; | ||
} | ||
var shortPointEvenY = { | ||
coordinates: { | ||
x: '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', | ||
y: '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8' | ||
}, | ||
compactEncoded: | ||
'02' + | ||
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', | ||
encoded: | ||
'04' + | ||
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798' + | ||
'483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8' | ||
}; | ||
var shortPointOddY = { | ||
coordinates: { | ||
x: 'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556', | ||
y: 'ae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297' | ||
}, | ||
compactEncoded: | ||
'03' + | ||
'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556', | ||
encoded: | ||
'04' + | ||
'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556' + | ||
'ae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297' | ||
}; | ||
it('should throw when trying to decode random bytes', function() { | ||
assert.throws(function() { | ||
elliptic.curves.secp256k1.curve.decodePoint( | ||
'05' + | ||
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'); | ||
}); | ||
}); | ||
it('should be able to encode/decode a short curve point with even Y', | ||
makeShortTest(shortPointEvenY)); | ||
it('should be able to encode/decode a short curve point with odd Y', | ||
makeShortTest(shortPointOddY)); | ||
it('should be able to encode/decode a mont curve point', makeMontTest({ | ||
coordinates: { | ||
// curve25519.curve.g.mul(new bn('6')).getX().toString(16, 2) | ||
x: '26954ccdc99ebf34f8f1dde5e6bb080685fec73640494c28f9fe0bfa8c794531', | ||
z: '1' | ||
}, | ||
encoded: | ||
'26954ccdc99ebf34f8f1dde5e6bb080685fec73640494c28f9fe0bfa8c794531' | ||
})); | ||
}); |
@@ -5,4 +5,2 @@ 'use strict'; | ||
var fs = require('fs'); | ||
var bn = require('bn.js'); | ||
var hash = require('hash.js'); | ||
var elliptic = require('../'); | ||
@@ -56,3 +54,3 @@ var utils = elliptic.utils; | ||
done(); | ||
}) | ||
}); | ||
}); | ||
@@ -79,7 +77,7 @@ | ||
msg.slice(0, msg.length-1).concat( | ||
(msg[(msg.length-1)] + 1) % 256) | ||
(msg[(msg.length-1)] + 1) % 256); | ||
assert.equal(msg.length || 1, forged.length); | ||
assert(!key.verify(forged, sig)); | ||
}) | ||
}); | ||
} | ||
@@ -91,3 +89,3 @@ for (var i = 0; i < Math.min(expectedTests, MAX_PROGRAMMATIC); i++) | ||
describe('EDDSA(\'ed25519\')', function() { | ||
var ed25519 | ||
var ed25519; | ||
@@ -104,3 +102,3 @@ before(function() { | ||
var secret = toArray(new Array(65).join('0'), 'hex'); | ||
assert(secret.length == 32); | ||
assert(secret.length === 32); | ||
var msg = [0xB, 0xE, 0xE, 0xF]; | ||
@@ -110,4 +108,4 @@ var key = ed25519.keyFromSecret(secret); | ||
var R = "8F1B9A7FDB22BCD2C15D4695B1CE2B063CBFAEC9B00BE360427BAC9533943F6C"; | ||
var S = "5F0B380FD7F2E43B70AB2FA29F6C6E3FFC1012710E174786814012324BF19B0C"; | ||
var R = '8F1B9A7FDB22BCD2C15D4695B1CE2B063CBFAEC9B00BE360427BAC9533943F6C'; | ||
var S = '5F0B380FD7F2E43B70AB2FA29F6C6E3FFC1012710E174786814012324BF19B0C'; | ||
@@ -114,0 +112,0 @@ assert.equal(sig.slice(0, 64), R); |
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
2786744
8046
Updatedbn.js@^3.1.1