base.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. 'use strict';
  2. var BN = require('bn.js');
  3. var elliptic = require('../../elliptic');
  4. var utils = elliptic.utils;
  5. var getNAF = utils.getNAF;
  6. var getJSF = utils.getJSF;
  7. var assert = utils.assert;
  8. function BaseCurve(type, conf) {
  9. this.type = type;
  10. this.p = new BN(conf.p, 16);
  11. // Use Montgomery, when there is no fast reduction for the prime
  12. this.red = conf.prime ? BN.red(conf.prime) : BN.mont(this.p);
  13. // Useful for many curves
  14. this.zero = new BN(0).toRed(this.red);
  15. this.one = new BN(1).toRed(this.red);
  16. this.two = new BN(2).toRed(this.red);
  17. // Curve configuration, optional
  18. this.n = conf.n && new BN(conf.n, 16);
  19. this.g = conf.g && this.pointFromJSON(conf.g, conf.gRed);
  20. // Temporary arrays
  21. this._wnafT1 = new Array(4);
  22. this._wnafT2 = new Array(4);
  23. this._wnafT3 = new Array(4);
  24. this._wnafT4 = new Array(4);
  25. // Generalized Greg Maxwell's trick
  26. var adjustCount = this.n && this.p.div(this.n);
  27. if (!adjustCount || adjustCount.cmpn(100) > 0) {
  28. this.redN = null;
  29. } else {
  30. this._maxwellTrick = true;
  31. this.redN = this.n.toRed(this.red);
  32. }
  33. }
  34. module.exports = BaseCurve;
  35. BaseCurve.prototype.point = function point() {
  36. throw new Error('Not implemented');
  37. };
  38. BaseCurve.prototype.validate = function validate() {
  39. throw new Error('Not implemented');
  40. };
  41. BaseCurve.prototype._fixedNafMul = function _fixedNafMul(p, k) {
  42. assert(p.precomputed);
  43. var doubles = p._getDoubles();
  44. var naf = getNAF(k, 1);
  45. var I = (1 << (doubles.step + 1)) - (doubles.step % 2 === 0 ? 2 : 1);
  46. I /= 3;
  47. // Translate into more windowed form
  48. var repr = [];
  49. for (var j = 0; j < naf.length; j += doubles.step) {
  50. var nafW = 0;
  51. for (var k = j + doubles.step - 1; k >= j; k--)
  52. nafW = (nafW << 1) + naf[k];
  53. repr.push(nafW);
  54. }
  55. var a = this.jpoint(null, null, null);
  56. var b = this.jpoint(null, null, null);
  57. for (var i = I; i > 0; i--) {
  58. for (var j = 0; j < repr.length; j++) {
  59. var nafW = repr[j];
  60. if (nafW === i)
  61. b = b.mixedAdd(doubles.points[j]);
  62. else if (nafW === -i)
  63. b = b.mixedAdd(doubles.points[j].neg());
  64. }
  65. a = a.add(b);
  66. }
  67. return a.toP();
  68. };
  69. BaseCurve.prototype._wnafMul = function _wnafMul(p, k) {
  70. var w = 4;
  71. // Precompute window
  72. var nafPoints = p._getNAFPoints(w);
  73. w = nafPoints.wnd;
  74. var wnd = nafPoints.points;
  75. // Get NAF form
  76. var naf = getNAF(k, w);
  77. // Add `this`*(N+1) for every w-NAF index
  78. var acc = this.jpoint(null, null, null);
  79. for (var i = naf.length - 1; i >= 0; i--) {
  80. // Count zeroes
  81. for (var k = 0; i >= 0 && naf[i] === 0; i--)
  82. k++;
  83. if (i >= 0)
  84. k++;
  85. acc = acc.dblp(k);
  86. if (i < 0)
  87. break;
  88. var z = naf[i];
  89. assert(z !== 0);
  90. if (p.type === 'affine') {
  91. // J +- P
  92. if (z > 0)
  93. acc = acc.mixedAdd(wnd[(z - 1) >> 1]);
  94. else
  95. acc = acc.mixedAdd(wnd[(-z - 1) >> 1].neg());
  96. } else {
  97. // J +- J
  98. if (z > 0)
  99. acc = acc.add(wnd[(z - 1) >> 1]);
  100. else
  101. acc = acc.add(wnd[(-z - 1) >> 1].neg());
  102. }
  103. }
  104. return p.type === 'affine' ? acc.toP() : acc;
  105. };
  106. BaseCurve.prototype._wnafMulAdd = function _wnafMulAdd(defW,
  107. points,
  108. coeffs,
  109. len,
  110. jacobianResult) {
  111. var wndWidth = this._wnafT1;
  112. var wnd = this._wnafT2;
  113. var naf = this._wnafT3;
  114. // Fill all arrays
  115. var max = 0;
  116. for (var i = 0; i < len; i++) {
  117. var p = points[i];
  118. var nafPoints = p._getNAFPoints(defW);
  119. wndWidth[i] = nafPoints.wnd;
  120. wnd[i] = nafPoints.points;
  121. }
  122. // Comb small window NAFs
  123. for (var i = len - 1; i >= 1; i -= 2) {
  124. var a = i - 1;
  125. var b = i;
  126. if (wndWidth[a] !== 1 || wndWidth[b] !== 1) {
  127. naf[a] = getNAF(coeffs[a], wndWidth[a]);
  128. naf[b] = getNAF(coeffs[b], wndWidth[b]);
  129. max = Math.max(naf[a].length, max);
  130. max = Math.max(naf[b].length, max);
  131. continue;
  132. }
  133. var comb = [
  134. points[a], /* 1 */
  135. null, /* 3 */
  136. null, /* 5 */
  137. points[b] /* 7 */
  138. ];
  139. // Try to avoid Projective points, if possible
  140. if (points[a].y.cmp(points[b].y) === 0) {
  141. comb[1] = points[a].add(points[b]);
  142. comb[2] = points[a].toJ().mixedAdd(points[b].neg());
  143. } else if (points[a].y.cmp(points[b].y.redNeg()) === 0) {
  144. comb[1] = points[a].toJ().mixedAdd(points[b]);
  145. comb[2] = points[a].add(points[b].neg());
  146. } else {
  147. comb[1] = points[a].toJ().mixedAdd(points[b]);
  148. comb[2] = points[a].toJ().mixedAdd(points[b].neg());
  149. }
  150. var index = [
  151. -3, /* -1 -1 */
  152. -1, /* -1 0 */
  153. -5, /* -1 1 */
  154. -7, /* 0 -1 */
  155. 0, /* 0 0 */
  156. 7, /* 0 1 */
  157. 5, /* 1 -1 */
  158. 1, /* 1 0 */
  159. 3 /* 1 1 */
  160. ];
  161. var jsf = getJSF(coeffs[a], coeffs[b]);
  162. max = Math.max(jsf[0].length, max);
  163. naf[a] = new Array(max);
  164. naf[b] = new Array(max);
  165. for (var j = 0; j < max; j++) {
  166. var ja = jsf[0][j] | 0;
  167. var jb = jsf[1][j] | 0;
  168. naf[a][j] = index[(ja + 1) * 3 + (jb + 1)];
  169. naf[b][j] = 0;
  170. wnd[a] = comb;
  171. }
  172. }
  173. var acc = this.jpoint(null, null, null);
  174. var tmp = this._wnafT4;
  175. for (var i = max; i >= 0; i--) {
  176. var k = 0;
  177. while (i >= 0) {
  178. var zero = true;
  179. for (var j = 0; j < len; j++) {
  180. tmp[j] = naf[j][i] | 0;
  181. if (tmp[j] !== 0)
  182. zero = false;
  183. }
  184. if (!zero)
  185. break;
  186. k++;
  187. i--;
  188. }
  189. if (i >= 0)
  190. k++;
  191. acc = acc.dblp(k);
  192. if (i < 0)
  193. break;
  194. for (var j = 0; j < len; j++) {
  195. var z = tmp[j];
  196. var p;
  197. if (z === 0)
  198. continue;
  199. else if (z > 0)
  200. p = wnd[j][(z - 1) >> 1];
  201. else if (z < 0)
  202. p = wnd[j][(-z - 1) >> 1].neg();
  203. if (p.type === 'affine')
  204. acc = acc.mixedAdd(p);
  205. else
  206. acc = acc.add(p);
  207. }
  208. }
  209. // Zeroify references
  210. for (var i = 0; i < len; i++)
  211. wnd[i] = null;
  212. if (jacobianResult)
  213. return acc;
  214. else
  215. return acc.toP();
  216. };
  217. function BasePoint(curve, type) {
  218. this.curve = curve;
  219. this.type = type;
  220. this.precomputed = null;
  221. }
  222. BaseCurve.BasePoint = BasePoint;
  223. BasePoint.prototype.eq = function eq(/*other*/) {
  224. throw new Error('Not implemented');
  225. };
  226. BasePoint.prototype.validate = function validate() {
  227. return this.curve.validate(this);
  228. };
  229. BaseCurve.prototype.decodePoint = function decodePoint(bytes, enc) {
  230. bytes = utils.toArray(bytes, enc);
  231. var len = this.p.byteLength();
  232. // uncompressed, hybrid-odd, hybrid-even
  233. if ((bytes[0] === 0x04 || bytes[0] === 0x06 || bytes[0] === 0x07) &&
  234. bytes.length - 1 === 2 * len) {
  235. if (bytes[0] === 0x06)
  236. assert(bytes[bytes.length - 1] % 2 === 0);
  237. else if (bytes[0] === 0x07)
  238. assert(bytes[bytes.length - 1] % 2 === 1);
  239. var res = this.point(bytes.slice(1, 1 + len),
  240. bytes.slice(1 + len, 1 + 2 * len));
  241. return res;
  242. } else if ((bytes[0] === 0x02 || bytes[0] === 0x03) &&
  243. bytes.length - 1 === len) {
  244. return this.pointFromX(bytes.slice(1, 1 + len), bytes[0] === 0x03);
  245. }
  246. throw new Error('Unknown point format');
  247. };
  248. BasePoint.prototype.encodeCompressed = function encodeCompressed(enc) {
  249. return this.encode(enc, true);
  250. };
  251. BasePoint.prototype._encode = function _encode(compact) {
  252. var len = this.curve.p.byteLength();
  253. var x = this.getX().toArray('be', len);
  254. if (compact)
  255. return [ this.getY().isEven() ? 0x02 : 0x03 ].concat(x);
  256. return [ 0x04 ].concat(x, this.getY().toArray('be', len)) ;
  257. };
  258. BasePoint.prototype.encode = function encode(enc, compact) {
  259. return utils.encode(this._encode(compact), enc);
  260. };
  261. BasePoint.prototype.precompute = function precompute(power) {
  262. if (this.precomputed)
  263. return this;
  264. var precomputed = {
  265. doubles: null,
  266. naf: null,
  267. beta: null
  268. };
  269. precomputed.naf = this._getNAFPoints(8);
  270. precomputed.doubles = this._getDoubles(4, power);
  271. precomputed.beta = this._getBeta();
  272. this.precomputed = precomputed;
  273. return this;
  274. };
  275. BasePoint.prototype._hasDoubles = function _hasDoubles(k) {
  276. if (!this.precomputed)
  277. return false;
  278. var doubles = this.precomputed.doubles;
  279. if (!doubles)
  280. return false;
  281. return doubles.points.length >= Math.ceil((k.bitLength() + 1) / doubles.step);
  282. };
  283. BasePoint.prototype._getDoubles = function _getDoubles(step, power) {
  284. if (this.precomputed && this.precomputed.doubles)
  285. return this.precomputed.doubles;
  286. var doubles = [ this ];
  287. var acc = this;
  288. for (var i = 0; i < power; i += step) {
  289. for (var j = 0; j < step; j++)
  290. acc = acc.dbl();
  291. doubles.push(acc);
  292. }
  293. return {
  294. step: step,
  295. points: doubles
  296. };
  297. };
  298. BasePoint.prototype._getNAFPoints = function _getNAFPoints(wnd) {
  299. if (this.precomputed && this.precomputed.naf)
  300. return this.precomputed.naf;
  301. var res = [ this ];
  302. var max = (1 << wnd) - 1;
  303. var dbl = max === 1 ? null : this.dbl();
  304. for (var i = 1; i < max; i++)
  305. res[i] = res[i - 1].add(dbl);
  306. return {
  307. wnd: wnd,
  308. points: res
  309. };
  310. };
  311. BasePoint.prototype._getBeta = function _getBeta() {
  312. return null;
  313. };
  314. BasePoint.prototype.dblp = function dblp(k) {
  315. var r = this;
  316. for (var i = 0; i < k; i++)
  317. r = r.dbl();
  318. return r;
  319. };