eddsa: cleanup

This commit is contained in:
Fedor Indutny
2015-06-24 22:55:03 -07:00
parent e3e2bc5374
commit e5a3ba2b38
3 changed files with 13 additions and 27 deletions
+12 -4
View File
@@ -61,9 +61,8 @@ EDDSA.prototype.verify = function verify(message, sig, pub) {
EDDSA.prototype.hashInt = function hashInt() {
var hash = this.hash();
for (var i = 0; i < arguments.length; i++) {
for (var i = 0; i < arguments.length; i++)
hash.update(arguments[i]);
}
return utils.intFromLE(hash.digest()).mod(this.curve.n);
};
@@ -90,11 +89,20 @@ EDDSA.prototype.makeSignature = function makeSignature(sig) {
*
*/
EDDSA.prototype.encodePoint = function encodePoint(point) {
return utils.pointToLEYoddX(point, this.encodingLength);
var enc = utils.intToLE(point.getY(), this.encodingLength);
enc[this.encodingLength - 1] |= point.getX().isOdd() ? 0x80 : 0;
return enc;
};
EDDSA.prototype.decodePoint = function decodePoint(bytes) {
return utils.pointFromLEYoddX(this.curve, bytes, bytes.length);
bytes = utils.parseBytes(bytes);
var lastIx = bytes.length - 1;
var normed = bytes.slice(0, lastIx).concat(bytes[lastIx] & ~0x80);
var xIsOdd = (bytes[lastIx] & 0x80) !== 0;
var y = utils.intFromLE(normed);
return this.curve.pointFromY(y, xIsOdd);
};
EDDSA.prototype.encodeInt = function encodeInt(num) {
+1 -1
View File
@@ -26,7 +26,7 @@ function KeyPair(eddsa, params) {
KeyPair.fromPublic = function fromPublic(eddsa, pub) {
if (pub instanceof KeyPair)
return pub;
return pub;
return new KeyPair(eddsa, { pub: pub });
};
-22
View File
@@ -177,25 +177,3 @@ function intToLE(num, padTo) {
return bytes;
}
utils.intToLE = intToLE;
function pointToLEYoddX(point, length) {
var enc = intToLE(point.getY(), length);
enc[length - 1] |= point.getX().isOdd() ? 0x80 : 0;
return enc;
}
utils.pointToLEYoddX = pointToLEYoddX;
function pointFromLEYoddX(curve, bytes, expectedLength) {
bytes = parseBytes(bytes);
if (expectedLength !== undefined)
utils.assert(bytes.length === expectedLength);
var lastIx = bytes.length - 1;
var normed = bytes.slice(0, lastIx).concat(bytes[lastIx] & ~0x80);
var xIsOdd = Boolean(bytes[lastIx] & 0x80);
var y = intFromLE(normed);
return curve.pointFromY(y, xIsOdd);
}
utils.pointFromLEYoddX = pointFromLEYoddX;