ecc.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. sjcl.ecc = {};
  2. /**
  3. * Represents a point on a curve in affine coordinates.
  4. * @constructor
  5. * @param {sjcl.ecc.curve} curve The curve that this point lies on.
  6. * @param {bigInt} x The x coordinate.
  7. * @param {bigInt} y The y coordinate.
  8. */
  9. sjcl.ecc.point = function(curve,x,y) {
  10. if (x === undefined) {
  11. this.isIdentity = true;
  12. } else {
  13. this.x = x;
  14. this.y = y;
  15. this.isIdentity = false;
  16. }
  17. this.curve = curve;
  18. };
  19. sjcl.ecc.point.prototype = {
  20. toJac: function() {
  21. return new sjcl.ecc.pointJac(this.curve, this.x, this.y, new this.curve.field(1));
  22. },
  23. mult: function(k) {
  24. return this.toJac().mult(k, this).toAffine();
  25. },
  26. isValid: function() {
  27. return this.y.square().equals(this.curve.b.add(this.x.mul(this.curve.a.add(this.x.square()))));
  28. },
  29. toBits: function() {
  30. return sjcl.bitArray.concat(this.x.toBits(), this.y.toBits());
  31. }
  32. };
  33. /**
  34. * Represents a point on a curve in Jacobian coordinates. Coordinates can be specified as bigInts or strings (which
  35. * will be converted to bigInts).
  36. *
  37. * @constructor
  38. * @param {bigInt/string} x The x coordinate.
  39. * @param {bigInt/string} y The y coordinate.
  40. * @param {bigInt/string} z The z coordinate.
  41. * @param {sjcl.ecc.curve} curve The curve that this point lies on.
  42. */
  43. sjcl.ecc.pointJac = function(curve, x, y, z) {
  44. if (x === undefined) {
  45. this.isIdentity = true;
  46. } else {
  47. this.x = x;
  48. this.y = y;
  49. this.z = z;
  50. this.isIdentity = false;
  51. }
  52. this.curve = curve;
  53. };
  54. sjcl.ecc.pointJac.prototype = {
  55. /**
  56. * Adds S and T and returns the result in Jacobian coordinates. Note that S must be in Jacobian coordinates and T must be in affine coordinates.
  57. * @param {sjcl.ecc.pointJac} S One of the points to add, in Jacobian coordinates.
  58. * @param {sjcl.ecc.point} T The other point to add, in affine coordinates.
  59. * @return {sjcl.ecc.pointJac} The sum of the two points, in Jacobian coordinates.
  60. */
  61. add: function(T) {
  62. var S = this;
  63. if (S.curve !== T.curve) {
  64. throw("sjcl.ecc.add(): Points must be on the same curve to add them!");
  65. }
  66. if (S.isIdentity) {
  67. return T.toJac();
  68. } else if (T.isIdentity) {
  69. return S;
  70. }
  71. var
  72. sz2 = S.z.square(),
  73. c = T.x.mul(sz2).subM(S.x);
  74. if (c.equals(0)) {
  75. if (S.y.equals(T.y.mul(sz2.mul(S.z)))) {
  76. // same point
  77. return S.doubl();
  78. } else {
  79. // inverses
  80. return new sjcl.ecc.pointJac(S.curve);
  81. }
  82. }
  83. var
  84. d = T.y.mul(sz2.mul(S.z)).subM(S.y),
  85. c2 = c.square(),
  86. x1 = d.square(),
  87. x2 = c.square().mul(c).addM( S.x.add(S.x).mul(c2) ),
  88. x = x1.subM(x2),
  89. y1 = S.x.mul(c2).subM(x).mul(d),
  90. y2 = S.y.mul(c.square().mul(c)),
  91. y = y1.subM(y2),
  92. z = S.z.mul(c);
  93. //return new sjcl.ecc.pointJac(this.curve,x,y,z);
  94. var U = new sjcl.ecc.pointJac(this.curve,x,y,z);
  95. if (!U.isValid()) { throw "FOOOOOOOO"; }
  96. return U;
  97. },
  98. /**
  99. * doubles this point.
  100. * @return {sjcl.ecc.pointJac} The doubled point.
  101. */
  102. doubl: function() {
  103. if (this.isIdentity) { return this; }
  104. var
  105. y2 = this.y.square(),
  106. a = y2.mul(this.x.mul(4)),
  107. b = y2.square().mul(8),
  108. z2 = this.z.square(),
  109. c = this.x.sub(z2).mul(3).mul(this.x.add(z2)),
  110. x = c.square().subM(a).subM(a),
  111. y = a.sub(x).mul(c).subM(b),
  112. z = this.y.add(this.y).mul(this.z);
  113. return new sjcl.ecc.pointJac(this.curve, x, y, z);
  114. },
  115. /**
  116. * Returns a copy of this point converted to affine coordinates.
  117. * @return {sjcl.ecc.point} The converted point.
  118. */
  119. toAffine: function() {
  120. if (this.isIdentity || this.z.equals(0)) {
  121. return new sjcl.ecc.point(this.curve);
  122. }
  123. var zi = this.z.inverse(), zi2 = zi.square();
  124. return new sjcl.ecc.point(this.curve, this.x.mul(zi2).fullReduce(), this.y.mul(zi2.mul(zi)).fullReduce());
  125. },
  126. /**
  127. * Multiply this point by k and return the answer in Jacobian coordinates.
  128. * @param {bigInt} k The coefficient to multiply by.
  129. * @param {sjcl.ecc.point} affine This point in affine coordinates.
  130. * @return {sjcl.ecc.pointJac} The result of the multiplication, in Jacobian coordinates.
  131. */
  132. mult: function(k, affine) {
  133. if (typeof(k) == "number") {
  134. k = [k];
  135. } else if (k.limbs !== undefined) {
  136. k = k.normalize().limbs;
  137. }
  138. var i, j, out = new sjcl.ecc.point(this.curve).toJac(), multiples, aff2;
  139. if (affine === undefined) {
  140. affine = this.toAffine();
  141. }
  142. if (affine.multiples === undefined) {
  143. j = this.doubl();
  144. affine.multiples = [new sjcl.ecc.point(this.curve), affine, j.toAffine()];
  145. for (i=3; i<16; i++) {
  146. j = j.add(affine);
  147. affine.multiples[i] = j.toAffine();
  148. }
  149. }
  150. multiples = affine.multiples;
  151. for (i=k.length-1; i>=0; i--) {
  152. for (j=sjcl.bn.prototype.radix-4; j>=0; j-=4) {
  153. out = out.doubl().doubl().doubl().doubl().add(multiples[k[i]>>j & 0xF]);
  154. }
  155. }
  156. return out;
  157. },
  158. isValid: function() {
  159. var z2 = this.z.square(), z4 = z2.square(), z6 = z4.mul(z2);
  160. return this.y.square().equals(
  161. this.curve.b.mul(z6).add(this.x.mul(
  162. this.curve.a.mul(z4).add(this.x.square()))));
  163. }
  164. };
  165. /**
  166. * Construct an elliptic curve. Most users will not use this and instead start with one of the NIST curves defined below.
  167. *
  168. * @constructor
  169. * @param {bigInt} p The prime modulus.
  170. * @param {bigInt} r The prime order of the curve.
  171. * @param {bigInt} a The constant a in the equation of the curve y^2 = x^3 + ax + b (for NIST curves, a is always -3).
  172. * @param {bigInt} x The x coordinate of a base point of the curve.
  173. * @param {bigInt} y The y coordinate of a base point of the curve.
  174. */
  175. sjcl.ecc.curve = function(field, r, a, b, x, y) {
  176. this.field = field;
  177. this.r = field.prototype.modulus.sub(r);
  178. this.a = new field(a);
  179. this.b = new field(b);
  180. this.G = new sjcl.ecc.point(this, new field(x), new field(y));
  181. };
  182. sjcl.ecc.curve.prototype.fromBits = function (bits) {
  183. var w = sjcl.bitArray, l = this.field.prototype.exponent + 7 & -8;
  184. p = new sjcl.ecc.point(this, this.field.fromBits(w.bitSlice(bits, 0, l)),
  185. this.field.fromBits(w.bitSlice(bits, l, 2*l)));
  186. if (!p.isValid()) {
  187. throw new sjcl.exception.corrupt("not on the curve!");
  188. }
  189. return p;
  190. };
  191. sjcl.ecc.curves = {
  192. c192: new sjcl.ecc.curve(
  193. sjcl.bn.prime.p192,
  194. "0x662107c8eb94364e4b2dd7ce",
  195. -3,
  196. "0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1",
  197. "0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012",
  198. "0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811"),
  199. c224: new sjcl.ecc.curve(
  200. sjcl.bn.prime.p224,
  201. "0xe95c1f470fc1ec22d6baa3a3d5c4",
  202. -3,
  203. "0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4",
  204. "0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21",
  205. "0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34"),
  206. c256: new sjcl.ecc.curve(
  207. sjcl.bn.prime.p256,
  208. "0x4319055358e8617b0c46353d039cdaae",
  209. -3,
  210. "0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
  211. "0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
  212. "0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"),
  213. c384: new sjcl.ecc.curve(
  214. sjcl.bn.prime.p384,
  215. "0x389cb27e0bc8d21fa7e5f24cb74f58851313e696333ad68c",
  216. -3,
  217. "0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef",
  218. "0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7",
  219. "0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f")
  220. };
  221. /* Diffie-Hellman-like public-key system */
  222. sjcl.ecc._dh = function(cn) {
  223. sjcl.ecc[cn] = {
  224. publicKey: function(curve, point) {
  225. this._curve = curve;
  226. if (point instanceof Array) {
  227. this._point = curve.fromBits(point);
  228. } else {
  229. this._point = point;
  230. }
  231. },
  232. secretKey: function(curve, exponent) {
  233. this._curve = curve;
  234. this._exponent = exponent;
  235. },
  236. generateKeys: function(curve, paranoia) {
  237. if (typeof curve == "number") {
  238. curve = sjcl.ecc.curves['c'+curve];
  239. if (curve === undefined) {
  240. throw new sjcl.exception.invalid("no such curve");
  241. }
  242. }
  243. var sec = sjcl.bn.random(curve.r, paranoia), pub = curve.G.mult(sec);
  244. return { pub: new sjcl.ecc[cn].publicKey(curve, pub),
  245. sec: new sjcl.ecc[cn].secretKey(curve, sec) };
  246. }
  247. };
  248. };
  249. sjcl.ecc._dh("elGamal");
  250. sjcl.ecc.elGamal.publicKey.prototype = {
  251. kem: function(paranoia) {
  252. var sec = sjcl.bn.random(this._curve.r, paranoia),
  253. tag = this._curve.G.mult(sec).toBits(),
  254. key = sjcl.hash.sha256.hash(this._point.mult(sec).toBits());
  255. return { key: key, tag: tag };
  256. }
  257. };
  258. sjcl.ecc.elGamal.secretKey.prototype = {
  259. unkem: function(tag) {
  260. return sjcl.hash.sha256.hash(this._curve.fromBits(tag).mult(this._exponent).toBits());
  261. }
  262. };
  263. sjcl.ecc._dh("dsa");
  264. sjcl.ecc.dsa.secretKey.prototype = {
  265. sign: function(hash, paranoia) {
  266. var R = this._curve.r,
  267. l = R.bitLength(),
  268. k = kkkk = sjcl.bn.random(R.sub(1), paranoia).add(1),
  269. r = this._curve.G.mult(k).x.mod(R),
  270. s = sjcl.bn.fromBits(hash).add(r.mul(this._exponent)).inverseMod(R).mul(kkkk).mod(R);
  271. return sjcl.bitArray.concat(r.toBits(l), s.toBits(l));
  272. }
  273. };
  274. sjcl.ecc.dsa.publicKey.prototype = {
  275. verify: function(hash, rs) {
  276. var w = sjcl.bitArray,
  277. R = this._curve.r,
  278. l = R.bitLength(),
  279. r = sjcl.bn.fromBits(w.bitSlice(rs,0,l)),
  280. s = sjcl.bn.fromBits(w.bitSlice(rs,l,2*l)),
  281. hG = sjcl.bn.fromBits(hash).mul(s).mod(R),
  282. hA = r.mul(s).mod(R),
  283. jG = this._curve.G.toJac(),
  284. r2 = jG.mult(hG, this._curve.G).add(this._point.mult(hA)).toAffine().x,
  285. corrupt = sjcl.exception.corrupt;
  286. if (r.equals(0) || s.equals(0) || r.greaterEquals(R) || s.greaterEquals(R) || !r2.equals(r)) {
  287. throw (new corrupt("signature didn't check out"));
  288. }
  289. return true;
  290. }
  291. }