ecc.js 12 KB

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