index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. var hash = require('hash.js');
  3. var elliptic = require('../../elliptic');
  4. var utils = elliptic.utils;
  5. var assert = utils.assert;
  6. var parseBytes = utils.parseBytes;
  7. var KeyPair = require('./key');
  8. var Signature = require('./signature');
  9. function EDDSA(curve) {
  10. assert(curve === 'ed25519', 'only tested with ed25519 so far');
  11. if (!(this instanceof EDDSA))
  12. return new EDDSA(curve);
  13. var curve = elliptic.curves[curve].curve;
  14. this.curve = curve;
  15. this.g = curve.g;
  16. this.g.precompute(curve.n.bitLength() + 1);
  17. this.pointClass = curve.point().constructor;
  18. this.encodingLength = Math.ceil(curve.n.bitLength() / 8);
  19. this.hash = hash.sha512;
  20. }
  21. module.exports = EDDSA;
  22. /**
  23. * @param {Array|String} message - message bytes
  24. * @param {Array|String|KeyPair} secret - secret bytes or a keypair
  25. * @returns {Signature} - signature
  26. */
  27. EDDSA.prototype.sign = function sign(message, secret) {
  28. message = parseBytes(message);
  29. var key = this.keyFromSecret(secret);
  30. var r = this.hashInt(key.messagePrefix(), message);
  31. var R = this.g.mul(r);
  32. var Rencoded = this.encodePoint(R);
  33. var s_ = this.hashInt(Rencoded, key.pubBytes(), message)
  34. .mul(key.priv());
  35. var S = r.add(s_).umod(this.curve.n);
  36. return this.makeSignature({ R: R, S: S, Rencoded: Rencoded });
  37. };
  38. /**
  39. * @param {Array} message - message bytes
  40. * @param {Array|String|Signature} sig - sig bytes
  41. * @param {Array|String|Point|KeyPair} pub - public key
  42. * @returns {Boolean} - true if public key matches sig of message
  43. */
  44. EDDSA.prototype.verify = function verify(message, sig, pub) {
  45. message = parseBytes(message);
  46. sig = this.makeSignature(sig);
  47. var key = this.keyFromPublic(pub);
  48. var h = this.hashInt(sig.Rencoded(), key.pubBytes(), message);
  49. var SG = this.g.mul(sig.S());
  50. var RplusAh = sig.R().add(key.pub().mul(h));
  51. return RplusAh.eq(SG);
  52. };
  53. EDDSA.prototype.hashInt = function hashInt() {
  54. var hash = this.hash();
  55. for (var i = 0; i < arguments.length; i++)
  56. hash.update(arguments[i]);
  57. return utils.intFromLE(hash.digest()).umod(this.curve.n);
  58. };
  59. EDDSA.prototype.keyFromPublic = function keyFromPublic(pub) {
  60. return KeyPair.fromPublic(this, pub);
  61. };
  62. EDDSA.prototype.keyFromSecret = function keyFromSecret(secret) {
  63. return KeyPair.fromSecret(this, secret);
  64. };
  65. EDDSA.prototype.makeSignature = function makeSignature(sig) {
  66. if (sig instanceof Signature)
  67. return sig;
  68. return new Signature(this, sig);
  69. };
  70. /**
  71. * * https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-03#section-5.2
  72. *
  73. * EDDSA defines methods for encoding and decoding points and integers. These are
  74. * helper convenience methods, that pass along to utility functions implied
  75. * parameters.
  76. *
  77. */
  78. EDDSA.prototype.encodePoint = function encodePoint(point) {
  79. var enc = point.getY().toArray('le', this.encodingLength);
  80. enc[this.encodingLength - 1] |= point.getX().isOdd() ? 0x80 : 0;
  81. return enc;
  82. };
  83. EDDSA.prototype.decodePoint = function decodePoint(bytes) {
  84. bytes = utils.parseBytes(bytes);
  85. var lastIx = bytes.length - 1;
  86. var normed = bytes.slice(0, lastIx).concat(bytes[lastIx] & ~0x80);
  87. var xIsOdd = (bytes[lastIx] & 0x80) !== 0;
  88. var y = utils.intFromLE(normed);
  89. return this.curve.pointFromY(y, xIsOdd);
  90. };
  91. EDDSA.prototype.encodeInt = function encodeInt(num) {
  92. return num.toArray('le', this.encodingLength);
  93. };
  94. EDDSA.prototype.decodeInt = function decodeInt(bytes) {
  95. return utils.intFromLE(bytes);
  96. };
  97. EDDSA.prototype.isPoint = function isPoint(val) {
  98. return val instanceof this.pointClass;
  99. };