elliptic.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  1. import BN from 'bn.js';
  2. import hash from 'hash.js';
  3. var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  4. function getDefaultExportFromCjs (x) {
  5. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
  6. }
  7. function createCommonjsModule(fn, basedir, module) {
  8. return module = {
  9. path: basedir,
  10. exports: {},
  11. require: function (path, base) {
  12. return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
  13. }
  14. }, fn(module, module.exports), module.exports;
  15. }
  16. function getDefaultExportFromNamespaceIfPresent (n) {
  17. return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n;
  18. }
  19. function getDefaultExportFromNamespaceIfNotNamed (n) {
  20. return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
  21. }
  22. function getAugmentedNamespace(n) {
  23. if (n.__esModule) return n;
  24. var a = Object.defineProperty({}, '__esModule', {value: true});
  25. Object.keys(n).forEach(function (k) {
  26. var d = Object.getOwnPropertyDescriptor(n, k);
  27. Object.defineProperty(a, k, d.get ? d : {
  28. enumerable: true,
  29. get: function () {
  30. return n[k];
  31. }
  32. });
  33. });
  34. return a;
  35. }
  36. function commonjsRequire () {
  37. throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
  38. }
  39. var minimalisticAssert = assert;
  40. function assert(val, msg) {
  41. if (!val)
  42. throw new Error(msg || 'Assertion failed');
  43. }
  44. assert.equal = function assertEqual(l, r, msg) {
  45. if (l != r)
  46. throw new Error(msg || ('Assertion failed: ' + l + ' != ' + r));
  47. };
  48. var utils_1 = createCommonjsModule(function (module, exports) {
  49. 'use strict';
  50. var utils = exports;
  51. function toArray(msg, enc) {
  52. if (Array.isArray(msg))
  53. return msg.slice();
  54. if (!msg)
  55. return [];
  56. var res = [];
  57. if (typeof msg !== 'string') {
  58. for (var i = 0; i < msg.length; i++)
  59. res[i] = msg[i] | 0;
  60. return res;
  61. }
  62. if (enc === 'hex') {
  63. msg = msg.replace(/[^a-z0-9]+/ig, '');
  64. if (msg.length % 2 !== 0)
  65. msg = '0' + msg;
  66. for (var i = 0; i < msg.length; i += 2)
  67. res.push(parseInt(msg[i] + msg[i + 1], 16));
  68. } else {
  69. for (var i = 0; i < msg.length; i++) {
  70. var c = msg.charCodeAt(i);
  71. var hi = c >> 8;
  72. var lo = c & 0xff;
  73. if (hi)
  74. res.push(hi, lo);
  75. else
  76. res.push(lo);
  77. }
  78. }
  79. return res;
  80. }
  81. utils.toArray = toArray;
  82. function zero2(word) {
  83. if (word.length === 1)
  84. return '0' + word;
  85. else
  86. return word;
  87. }
  88. utils.zero2 = zero2;
  89. function toHex(msg) {
  90. var res = '';
  91. for (var i = 0; i < msg.length; i++)
  92. res += zero2(msg[i].toString(16));
  93. return res;
  94. }
  95. utils.toHex = toHex;
  96. utils.encode = function encode(arr, enc) {
  97. if (enc === 'hex')
  98. return toHex(arr);
  99. else
  100. return arr;
  101. };
  102. });
  103. var utils_1$1 = createCommonjsModule(function (module, exports) {
  104. 'use strict';
  105. var utils = exports;
  106. utils.assert = minimalisticAssert;
  107. utils.toArray = utils_1.toArray;
  108. utils.zero2 = utils_1.zero2;
  109. utils.toHex = utils_1.toHex;
  110. utils.encode = utils_1.encode;
  111. // Represent num in a w-NAF form
  112. function getNAF(num, w, bits) {
  113. var naf = new Array(Math.max(num.bitLength(), bits) + 1);
  114. naf.fill(0);
  115. var ws = 1 << (w + 1);
  116. var k = num.clone();
  117. for (var i = 0; i < naf.length; i++) {
  118. var z;
  119. var mod = k.andln(ws - 1);
  120. if (k.isOdd()) {
  121. if (mod > (ws >> 1) - 1)
  122. z = (ws >> 1) - mod;
  123. else
  124. z = mod;
  125. k.isubn(z);
  126. } else {
  127. z = 0;
  128. }
  129. naf[i] = z;
  130. k.iushrn(1);
  131. }
  132. return naf;
  133. }
  134. utils.getNAF = getNAF;
  135. // Represent k1, k2 in a Joint Sparse Form
  136. function getJSF(k1, k2) {
  137. var jsf = [
  138. [],
  139. [],
  140. ];
  141. k1 = k1.clone();
  142. k2 = k2.clone();
  143. var d1 = 0;
  144. var d2 = 0;
  145. var m8;
  146. while (k1.cmpn(-d1) > 0 || k2.cmpn(-d2) > 0) {
  147. // First phase
  148. var m14 = (k1.andln(3) + d1) & 3;
  149. var m24 = (k2.andln(3) + d2) & 3;
  150. if (m14 === 3)
  151. m14 = -1;
  152. if (m24 === 3)
  153. m24 = -1;
  154. var u1;
  155. if ((m14 & 1) === 0) {
  156. u1 = 0;
  157. } else {
  158. m8 = (k1.andln(7) + d1) & 7;
  159. if ((m8 === 3 || m8 === 5) && m24 === 2)
  160. u1 = -m14;
  161. else
  162. u1 = m14;
  163. }
  164. jsf[0].push(u1);
  165. var u2;
  166. if ((m24 & 1) === 0) {
  167. u2 = 0;
  168. } else {
  169. m8 = (k2.andln(7) + d2) & 7;
  170. if ((m8 === 3 || m8 === 5) && m14 === 2)
  171. u2 = -m24;
  172. else
  173. u2 = m24;
  174. }
  175. jsf[1].push(u2);
  176. // Second phase
  177. if (2 * d1 === u1 + 1)
  178. d1 = 1 - d1;
  179. if (2 * d2 === u2 + 1)
  180. d2 = 1 - d2;
  181. k1.iushrn(1);
  182. k2.iushrn(1);
  183. }
  184. return jsf;
  185. }
  186. utils.getJSF = getJSF;
  187. function cachedProperty(obj, name, computer) {
  188. var key = '_' + name;
  189. obj.prototype[name] = function cachedProperty() {
  190. return this[key] !== undefined ? this[key] :
  191. this[key] = computer.call(this);
  192. };
  193. }
  194. utils.cachedProperty = cachedProperty;
  195. function parseBytes(bytes) {
  196. return typeof bytes === 'string' ? utils.toArray(bytes, 'hex') :
  197. bytes;
  198. }
  199. utils.parseBytes = parseBytes;
  200. function intFromLE(bytes) {
  201. return new BN(bytes, 'hex', 'le');
  202. }
  203. utils.intFromLE = intFromLE;
  204. });
  205. 'use strict';
  206. var getNAF = utils_1$1.getNAF;
  207. var getJSF = utils_1$1.getJSF;
  208. var assert$1 = utils_1$1.assert;
  209. function BaseCurve(type, conf) {
  210. this.type = type;
  211. this.p = new BN(conf.p, 16);
  212. // Use Montgomery, when there is no fast reduction for the prime
  213. this.red = conf.prime ? BN.red(conf.prime) : BN.mont(this.p);
  214. // Useful for many curves
  215. this.zero = new BN(0).toRed(this.red);
  216. this.one = new BN(1).toRed(this.red);
  217. this.two = new BN(2).toRed(this.red);
  218. // Curve configuration, optional
  219. this.n = conf.n && new BN(conf.n, 16);
  220. this.g = conf.g && this.pointFromJSON(conf.g, conf.gRed);
  221. // Temporary arrays
  222. this._wnafT1 = new Array(4);
  223. this._wnafT2 = new Array(4);
  224. this._wnafT3 = new Array(4);
  225. this._wnafT4 = new Array(4);
  226. this._bitLength = this.n ? this.n.bitLength() : 0;
  227. // Generalized Greg Maxwell's trick
  228. var adjustCount = this.n && this.p.div(this.n);
  229. if (!adjustCount || adjustCount.cmpn(100) > 0) {
  230. this.redN = null;
  231. } else {
  232. this._maxwellTrick = true;
  233. this.redN = this.n.toRed(this.red);
  234. }
  235. }
  236. var base = BaseCurve;
  237. BaseCurve.prototype.point = function point() {
  238. throw new Error('Not implemented');
  239. };
  240. BaseCurve.prototype.validate = function validate() {
  241. throw new Error('Not implemented');
  242. };
  243. BaseCurve.prototype._fixedNafMul = function _fixedNafMul(p, k) {
  244. assert$1(p.precomputed);
  245. var doubles = p._getDoubles();
  246. var naf = getNAF(k, 1, this._bitLength);
  247. var I = (1 << (doubles.step + 1)) - (doubles.step % 2 === 0 ? 2 : 1);
  248. I /= 3;
  249. // Translate into more windowed form
  250. var repr = [];
  251. var j;
  252. var nafW;
  253. for (j = 0; j < naf.length; j += doubles.step) {
  254. nafW = 0;
  255. for (var l = j + doubles.step - 1; l >= j; l--)
  256. nafW = (nafW << 1) + naf[l];
  257. repr.push(nafW);
  258. }
  259. var a = this.jpoint(null, null, null);
  260. var b = this.jpoint(null, null, null);
  261. for (var i = I; i > 0; i--) {
  262. for (j = 0; j < repr.length; j++) {
  263. nafW = repr[j];
  264. if (nafW === i)
  265. b = b.mixedAdd(doubles.points[j]);
  266. else if (nafW === -i)
  267. b = b.mixedAdd(doubles.points[j].neg());
  268. }
  269. a = a.add(b);
  270. }
  271. return a.toP();
  272. };
  273. BaseCurve.prototype._wnafMul = function _wnafMul(p, k) {
  274. var w = 4;
  275. // Precompute window
  276. var nafPoints = p._getNAFPoints(w);
  277. w = nafPoints.wnd;
  278. var wnd = nafPoints.points;
  279. // Get NAF form
  280. var naf = getNAF(k, w, this._bitLength);
  281. // Add `this`*(N+1) for every w-NAF index
  282. var acc = this.jpoint(null, null, null);
  283. for (var i = naf.length - 1; i >= 0; i--) {
  284. // Count zeroes
  285. for (var l = 0; i >= 0 && naf[i] === 0; i--)
  286. l++;
  287. if (i >= 0)
  288. l++;
  289. acc = acc.dblp(l);
  290. if (i < 0)
  291. break;
  292. var z = naf[i];
  293. assert$1(z !== 0);
  294. if (p.type === 'affine') {
  295. // J +- P
  296. if (z > 0)
  297. acc = acc.mixedAdd(wnd[(z - 1) >> 1]);
  298. else
  299. acc = acc.mixedAdd(wnd[(-z - 1) >> 1].neg());
  300. } else {
  301. // J +- J
  302. if (z > 0)
  303. acc = acc.add(wnd[(z - 1) >> 1]);
  304. else
  305. acc = acc.add(wnd[(-z - 1) >> 1].neg());
  306. }
  307. }
  308. return p.type === 'affine' ? acc.toP() : acc;
  309. };
  310. BaseCurve.prototype._wnafMulAdd = function _wnafMulAdd(defW,
  311. points,
  312. coeffs,
  313. len,
  314. jacobianResult) {
  315. var wndWidth = this._wnafT1;
  316. var wnd = this._wnafT2;
  317. var naf = this._wnafT3;
  318. // Fill all arrays
  319. var max = 0;
  320. var i;
  321. var j;
  322. var p;
  323. for (i = 0; i < len; i++) {
  324. p = points[i];
  325. var nafPoints = p._getNAFPoints(defW);
  326. wndWidth[i] = nafPoints.wnd;
  327. wnd[i] = nafPoints.points;
  328. }
  329. // Comb small window NAFs
  330. for (i = len - 1; i >= 1; i -= 2) {
  331. var a = i - 1;
  332. var b = i;
  333. if (wndWidth[a] !== 1 || wndWidth[b] !== 1) {
  334. naf[a] = getNAF(coeffs[a], wndWidth[a], this._bitLength);
  335. naf[b] = getNAF(coeffs[b], wndWidth[b], this._bitLength);
  336. max = Math.max(naf[a].length, max);
  337. max = Math.max(naf[b].length, max);
  338. continue;
  339. }
  340. var comb = [
  341. points[a], /* 1 */
  342. null, /* 3 */
  343. null, /* 5 */
  344. points[b], /* 7 */
  345. ];
  346. // Try to avoid Projective points, if possible
  347. if (points[a].y.cmp(points[b].y) === 0) {
  348. comb[1] = points[a].add(points[b]);
  349. comb[2] = points[a].toJ().mixedAdd(points[b].neg());
  350. } else if (points[a].y.cmp(points[b].y.redNeg()) === 0) {
  351. comb[1] = points[a].toJ().mixedAdd(points[b]);
  352. comb[2] = points[a].add(points[b].neg());
  353. } else {
  354. comb[1] = points[a].toJ().mixedAdd(points[b]);
  355. comb[2] = points[a].toJ().mixedAdd(points[b].neg());
  356. }
  357. var index = [
  358. -3, /* -1 -1 */
  359. -1, /* -1 0 */
  360. -5, /* -1 1 */
  361. -7, /* 0 -1 */
  362. 0, /* 0 0 */
  363. 7, /* 0 1 */
  364. 5, /* 1 -1 */
  365. 1, /* 1 0 */
  366. 3, /* 1 1 */
  367. ];
  368. var jsf = getJSF(coeffs[a], coeffs[b]);
  369. max = Math.max(jsf[0].length, max);
  370. naf[a] = new Array(max);
  371. naf[b] = new Array(max);
  372. for (j = 0; j < max; j++) {
  373. var ja = jsf[0][j] | 0;
  374. var jb = jsf[1][j] | 0;
  375. naf[a][j] = index[(ja + 1) * 3 + (jb + 1)];
  376. naf[b][j] = 0;
  377. wnd[a] = comb;
  378. }
  379. }
  380. var acc = this.jpoint(null, null, null);
  381. var tmp = this._wnafT4;
  382. for (i = max; i >= 0; i--) {
  383. var k = 0;
  384. while (i >= 0) {
  385. var zero = true;
  386. for (j = 0; j < len; j++) {
  387. tmp[j] = naf[j][i] | 0;
  388. if (tmp[j] !== 0)
  389. zero = false;
  390. }
  391. if (!zero)
  392. break;
  393. k++;
  394. i--;
  395. }
  396. if (i >= 0)
  397. k++;
  398. acc = acc.dblp(k);
  399. if (i < 0)
  400. break;
  401. for (j = 0; j < len; j++) {
  402. var z = tmp[j];
  403. p;
  404. if (z === 0)
  405. continue;
  406. else if (z > 0)
  407. p = wnd[j][(z - 1) >> 1];
  408. else if (z < 0)
  409. p = wnd[j][(-z - 1) >> 1].neg();
  410. if (p.type === 'affine')
  411. acc = acc.mixedAdd(p);
  412. else
  413. acc = acc.add(p);
  414. }
  415. }
  416. // Zeroify references
  417. for (i = 0; i < len; i++)
  418. wnd[i] = null;
  419. if (jacobianResult)
  420. return acc;
  421. else
  422. return acc.toP();
  423. };
  424. function BasePoint(curve, type) {
  425. this.curve = curve;
  426. this.type = type;
  427. this.precomputed = null;
  428. }
  429. BaseCurve.BasePoint = BasePoint;
  430. BasePoint.prototype.eq = function eq(/*other*/) {
  431. throw new Error('Not implemented');
  432. };
  433. BasePoint.prototype.validate = function validate() {
  434. return this.curve.validate(this);
  435. };
  436. BaseCurve.prototype.decodePoint = function decodePoint(bytes, enc) {
  437. bytes = utils_1$1.toArray(bytes, enc);
  438. var len = this.p.byteLength();
  439. // uncompressed, hybrid-odd, hybrid-even
  440. if ((bytes[0] === 0x04 || bytes[0] === 0x06 || bytes[0] === 0x07) &&
  441. bytes.length - 1 === 2 * len) {
  442. if (bytes[0] === 0x06)
  443. assert$1(bytes[bytes.length - 1] % 2 === 0);
  444. else if (bytes[0] === 0x07)
  445. assert$1(bytes[bytes.length - 1] % 2 === 1);
  446. var res = this.point(bytes.slice(1, 1 + len),
  447. bytes.slice(1 + len, 1 + 2 * len));
  448. return res;
  449. } else if ((bytes[0] === 0x02 || bytes[0] === 0x03) &&
  450. bytes.length - 1 === len) {
  451. return this.pointFromX(bytes.slice(1, 1 + len), bytes[0] === 0x03);
  452. }
  453. throw new Error('Unknown point format');
  454. };
  455. BasePoint.prototype.encodeCompressed = function encodeCompressed(enc) {
  456. return this.encode(enc, true);
  457. };
  458. BasePoint.prototype._encode = function _encode(compact) {
  459. var len = this.curve.p.byteLength();
  460. var x = this.getX().toArray('be', len);
  461. if (compact)
  462. return [ this.getY().isEven() ? 0x02 : 0x03 ].concat(x);
  463. return [ 0x04 ].concat(x, this.getY().toArray('be', len));
  464. };
  465. BasePoint.prototype.encode = function encode(enc, compact) {
  466. return utils_1$1.encode(this._encode(compact), enc);
  467. };
  468. BasePoint.prototype.precompute = function precompute(power) {
  469. if (this.precomputed)
  470. return this;
  471. var precomputed = {
  472. doubles: null,
  473. naf: null,
  474. beta: null,
  475. };
  476. precomputed.naf = this._getNAFPoints(8);
  477. precomputed.doubles = this._getDoubles(4, power);
  478. precomputed.beta = this._getBeta();
  479. this.precomputed = precomputed;
  480. return this;
  481. };
  482. BasePoint.prototype._hasDoubles = function _hasDoubles(k) {
  483. if (!this.precomputed)
  484. return false;
  485. var doubles = this.precomputed.doubles;
  486. if (!doubles)
  487. return false;
  488. return doubles.points.length >= Math.ceil((k.bitLength() + 1) / doubles.step);
  489. };
  490. BasePoint.prototype._getDoubles = function _getDoubles(step, power) {
  491. if (this.precomputed && this.precomputed.doubles)
  492. return this.precomputed.doubles;
  493. var doubles = [ this ];
  494. var acc = this;
  495. for (var i = 0; i < power; i += step) {
  496. for (var j = 0; j < step; j++)
  497. acc = acc.dbl();
  498. doubles.push(acc);
  499. }
  500. return {
  501. step: step,
  502. points: doubles,
  503. };
  504. };
  505. BasePoint.prototype._getNAFPoints = function _getNAFPoints(wnd) {
  506. if (this.precomputed && this.precomputed.naf)
  507. return this.precomputed.naf;
  508. var res = [ this ];
  509. var max = (1 << wnd) - 1;
  510. var dbl = max === 1 ? null : this.dbl();
  511. for (var i = 1; i < max; i++)
  512. res[i] = res[i - 1].add(dbl);
  513. return {
  514. wnd: wnd,
  515. points: res,
  516. };
  517. };
  518. BasePoint.prototype._getBeta = function _getBeta() {
  519. return null;
  520. };
  521. BasePoint.prototype.dblp = function dblp(k) {
  522. var r = this;
  523. for (var i = 0; i < k; i++)
  524. r = r.dbl();
  525. return r;
  526. };
  527. var inherits_browser = createCommonjsModule(function (module) {
  528. if (typeof Object.create === 'function') {
  529. // implementation from standard node.js 'util' module
  530. module.exports = function inherits(ctor, superCtor) {
  531. if (superCtor) {
  532. ctor.super_ = superCtor;
  533. ctor.prototype = Object.create(superCtor.prototype, {
  534. constructor: {
  535. value: ctor,
  536. enumerable: false,
  537. writable: true,
  538. configurable: true
  539. }
  540. });
  541. }
  542. };
  543. } else {
  544. // old school shim for old browsers
  545. module.exports = function inherits(ctor, superCtor) {
  546. if (superCtor) {
  547. ctor.super_ = superCtor;
  548. var TempCtor = function () {};
  549. TempCtor.prototype = superCtor.prototype;
  550. ctor.prototype = new TempCtor();
  551. ctor.prototype.constructor = ctor;
  552. }
  553. };
  554. }
  555. });
  556. 'use strict';
  557. var assert$2 = utils_1$1.assert;
  558. function ShortCurve(conf) {
  559. base.call(this, 'short', conf);
  560. this.a = new BN(conf.a, 16).toRed(this.red);
  561. this.b = new BN(conf.b, 16).toRed(this.red);
  562. this.tinv = this.two.redInvm();
  563. this.zeroA = this.a.fromRed().cmpn(0) === 0;
  564. this.threeA = this.a.fromRed().sub(this.p).cmpn(-3) === 0;
  565. // If the curve is endomorphic, precalculate beta and lambda
  566. this.endo = this._getEndomorphism(conf);
  567. this._endoWnafT1 = new Array(4);
  568. this._endoWnafT2 = new Array(4);
  569. }
  570. inherits_browser(ShortCurve, base);
  571. var short_1 = ShortCurve;
  572. ShortCurve.prototype._getEndomorphism = function _getEndomorphism(conf) {
  573. // No efficient endomorphism
  574. if (!this.zeroA || !this.g || !this.n || this.p.modn(3) !== 1)
  575. return;
  576. // Compute beta and lambda, that lambda * P = (beta * Px; Py)
  577. var beta;
  578. var lambda;
  579. if (conf.beta) {
  580. beta = new BN(conf.beta, 16).toRed(this.red);
  581. } else {
  582. var betas = this._getEndoRoots(this.p);
  583. // Choose the smallest beta
  584. beta = betas[0].cmp(betas[1]) < 0 ? betas[0] : betas[1];
  585. beta = beta.toRed(this.red);
  586. }
  587. if (conf.lambda) {
  588. lambda = new BN(conf.lambda, 16);
  589. } else {
  590. // Choose the lambda that is matching selected beta
  591. var lambdas = this._getEndoRoots(this.n);
  592. if (this.g.mul(lambdas[0]).x.cmp(this.g.x.redMul(beta)) === 0) {
  593. lambda = lambdas[0];
  594. } else {
  595. lambda = lambdas[1];
  596. assert$2(this.g.mul(lambda).x.cmp(this.g.x.redMul(beta)) === 0);
  597. }
  598. }
  599. // Get basis vectors, used for balanced length-two representation
  600. var basis;
  601. if (conf.basis) {
  602. basis = conf.basis.map(function(vec) {
  603. return {
  604. a: new BN(vec.a, 16),
  605. b: new BN(vec.b, 16),
  606. };
  607. });
  608. } else {
  609. basis = this._getEndoBasis(lambda);
  610. }
  611. return {
  612. beta: beta,
  613. lambda: lambda,
  614. basis: basis,
  615. };
  616. };
  617. ShortCurve.prototype._getEndoRoots = function _getEndoRoots(num) {
  618. // Find roots of for x^2 + x + 1 in F
  619. // Root = (-1 +- Sqrt(-3)) / 2
  620. //
  621. var red = num === this.p ? this.red : BN.mont(num);
  622. var tinv = new BN(2).toRed(red).redInvm();
  623. var ntinv = tinv.redNeg();
  624. var s = new BN(3).toRed(red).redNeg().redSqrt().redMul(tinv);
  625. var l1 = ntinv.redAdd(s).fromRed();
  626. var l2 = ntinv.redSub(s).fromRed();
  627. return [ l1, l2 ];
  628. };
  629. ShortCurve.prototype._getEndoBasis = function _getEndoBasis(lambda) {
  630. // aprxSqrt >= sqrt(this.n)
  631. var aprxSqrt = this.n.ushrn(Math.floor(this.n.bitLength() / 2));
  632. // 3.74
  633. // Run EGCD, until r(L + 1) < aprxSqrt
  634. var u = lambda;
  635. var v = this.n.clone();
  636. var x1 = new BN(1);
  637. var y1 = new BN(0);
  638. var x2 = new BN(0);
  639. var y2 = new BN(1);
  640. // NOTE: all vectors are roots of: a + b * lambda = 0 (mod n)
  641. var a0;
  642. var b0;
  643. // First vector
  644. var a1;
  645. var b1;
  646. // Second vector
  647. var a2;
  648. var b2;
  649. var prevR;
  650. var i = 0;
  651. var r;
  652. var x;
  653. while (u.cmpn(0) !== 0) {
  654. var q = v.div(u);
  655. r = v.sub(q.mul(u));
  656. x = x2.sub(q.mul(x1));
  657. var y = y2.sub(q.mul(y1));
  658. if (!a1 && r.cmp(aprxSqrt) < 0) {
  659. a0 = prevR.neg();
  660. b0 = x1;
  661. a1 = r.neg();
  662. b1 = x;
  663. } else if (a1 && ++i === 2) {
  664. break;
  665. }
  666. prevR = r;
  667. v = u;
  668. u = r;
  669. x2 = x1;
  670. x1 = x;
  671. y2 = y1;
  672. y1 = y;
  673. }
  674. a2 = r.neg();
  675. b2 = x;
  676. var len1 = a1.sqr().add(b1.sqr());
  677. var len2 = a2.sqr().add(b2.sqr());
  678. if (len2.cmp(len1) >= 0) {
  679. a2 = a0;
  680. b2 = b0;
  681. }
  682. // Normalize signs
  683. if (a1.negative) {
  684. a1 = a1.neg();
  685. b1 = b1.neg();
  686. }
  687. if (a2.negative) {
  688. a2 = a2.neg();
  689. b2 = b2.neg();
  690. }
  691. return [
  692. { a: a1, b: b1 },
  693. { a: a2, b: b2 },
  694. ];
  695. };
  696. ShortCurve.prototype._endoSplit = function _endoSplit(k) {
  697. var basis = this.endo.basis;
  698. var v1 = basis[0];
  699. var v2 = basis[1];
  700. var c1 = v2.b.mul(k).divRound(this.n);
  701. var c2 = v1.b.neg().mul(k).divRound(this.n);
  702. var p1 = c1.mul(v1.a);
  703. var p2 = c2.mul(v2.a);
  704. var q1 = c1.mul(v1.b);
  705. var q2 = c2.mul(v2.b);
  706. // Calculate answer
  707. var k1 = k.sub(p1).sub(p2);
  708. var k2 = q1.add(q2).neg();
  709. return { k1: k1, k2: k2 };
  710. };
  711. ShortCurve.prototype.pointFromX = function pointFromX(x, odd) {
  712. x = new BN(x, 16);
  713. if (!x.red)
  714. x = x.toRed(this.red);
  715. var y2 = x.redSqr().redMul(x).redIAdd(x.redMul(this.a)).redIAdd(this.b);
  716. var y = y2.redSqrt();
  717. if (y.redSqr().redSub(y2).cmp(this.zero) !== 0)
  718. throw new Error('invalid point');
  719. // XXX Is there any way to tell if the number is odd without converting it
  720. // to non-red form?
  721. var isOdd = y.fromRed().isOdd();
  722. if (odd && !isOdd || !odd && isOdd)
  723. y = y.redNeg();
  724. return this.point(x, y);
  725. };
  726. ShortCurve.prototype.validate = function validate(point) {
  727. if (point.inf)
  728. return true;
  729. var x = point.x;
  730. var y = point.y;
  731. var ax = this.a.redMul(x);
  732. var rhs = x.redSqr().redMul(x).redIAdd(ax).redIAdd(this.b);
  733. return y.redSqr().redISub(rhs).cmpn(0) === 0;
  734. };
  735. ShortCurve.prototype._endoWnafMulAdd =
  736. function _endoWnafMulAdd(points, coeffs, jacobianResult) {
  737. var npoints = this._endoWnafT1;
  738. var ncoeffs = this._endoWnafT2;
  739. for (var i = 0; i < points.length; i++) {
  740. var split = this._endoSplit(coeffs[i]);
  741. var p = points[i];
  742. var beta = p._getBeta();
  743. if (split.k1.negative) {
  744. split.k1.ineg();
  745. p = p.neg(true);
  746. }
  747. if (split.k2.negative) {
  748. split.k2.ineg();
  749. beta = beta.neg(true);
  750. }
  751. npoints[i * 2] = p;
  752. npoints[i * 2 + 1] = beta;
  753. ncoeffs[i * 2] = split.k1;
  754. ncoeffs[i * 2 + 1] = split.k2;
  755. }
  756. var res = this._wnafMulAdd(1, npoints, ncoeffs, i * 2, jacobianResult);
  757. // Clean-up references to points and coefficients
  758. for (var j = 0; j < i * 2; j++) {
  759. npoints[j] = null;
  760. ncoeffs[j] = null;
  761. }
  762. return res;
  763. };
  764. function Point(curve, x, y, isRed) {
  765. base.BasePoint.call(this, curve, 'affine');
  766. if (x === null && y === null) {
  767. this.x = null;
  768. this.y = null;
  769. this.inf = true;
  770. } else {
  771. this.x = new BN(x, 16);
  772. this.y = new BN(y, 16);
  773. // Force redgomery representation when loading from JSON
  774. if (isRed) {
  775. this.x.forceRed(this.curve.red);
  776. this.y.forceRed(this.curve.red);
  777. }
  778. if (!this.x.red)
  779. this.x = this.x.toRed(this.curve.red);
  780. if (!this.y.red)
  781. this.y = this.y.toRed(this.curve.red);
  782. this.inf = false;
  783. }
  784. }
  785. inherits_browser(Point, base.BasePoint);
  786. ShortCurve.prototype.point = function point(x, y, isRed) {
  787. return new Point(this, x, y, isRed);
  788. };
  789. ShortCurve.prototype.pointFromJSON = function pointFromJSON(obj, red) {
  790. return Point.fromJSON(this, obj, red);
  791. };
  792. Point.prototype._getBeta = function _getBeta() {
  793. if (!this.curve.endo)
  794. return;
  795. var pre = this.precomputed;
  796. if (pre && pre.beta)
  797. return pre.beta;
  798. var beta = this.curve.point(this.x.redMul(this.curve.endo.beta), this.y);
  799. if (pre) {
  800. var curve = this.curve;
  801. var endoMul = function(p) {
  802. return curve.point(p.x.redMul(curve.endo.beta), p.y);
  803. };
  804. pre.beta = beta;
  805. beta.precomputed = {
  806. beta: null,
  807. naf: pre.naf && {
  808. wnd: pre.naf.wnd,
  809. points: pre.naf.points.map(endoMul),
  810. },
  811. doubles: pre.doubles && {
  812. step: pre.doubles.step,
  813. points: pre.doubles.points.map(endoMul),
  814. },
  815. };
  816. }
  817. return beta;
  818. };
  819. Point.prototype.toJSON = function toJSON() {
  820. if (!this.precomputed)
  821. return [ this.x, this.y ];
  822. return [ this.x, this.y, this.precomputed && {
  823. doubles: this.precomputed.doubles && {
  824. step: this.precomputed.doubles.step,
  825. points: this.precomputed.doubles.points.slice(1),
  826. },
  827. naf: this.precomputed.naf && {
  828. wnd: this.precomputed.naf.wnd,
  829. points: this.precomputed.naf.points.slice(1),
  830. },
  831. } ];
  832. };
  833. Point.fromJSON = function fromJSON(curve, obj, red) {
  834. if (typeof obj === 'string')
  835. obj = JSON.parse(obj);
  836. var res = curve.point(obj[0], obj[1], red);
  837. if (!obj[2])
  838. return res;
  839. function obj2point(obj) {
  840. return curve.point(obj[0], obj[1], red);
  841. }
  842. var pre = obj[2];
  843. res.precomputed = {
  844. beta: null,
  845. doubles: pre.doubles && {
  846. step: pre.doubles.step,
  847. points: [ res ].concat(pre.doubles.points.map(obj2point)),
  848. },
  849. naf: pre.naf && {
  850. wnd: pre.naf.wnd,
  851. points: [ res ].concat(pre.naf.points.map(obj2point)),
  852. },
  853. };
  854. return res;
  855. };
  856. Point.prototype.inspect = function inspect() {
  857. if (this.isInfinity())
  858. return '<EC Point Infinity>';
  859. return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
  860. ' y: ' + this.y.fromRed().toString(16, 2) + '>';
  861. };
  862. Point.prototype.isInfinity = function isInfinity() {
  863. return this.inf;
  864. };
  865. Point.prototype.add = function add(p) {
  866. // O + P = P
  867. if (this.inf)
  868. return p;
  869. // P + O = P
  870. if (p.inf)
  871. return this;
  872. // P + P = 2P
  873. if (this.eq(p))
  874. return this.dbl();
  875. // P + (-P) = O
  876. if (this.neg().eq(p))
  877. return this.curve.point(null, null);
  878. // P + Q = O
  879. if (this.x.cmp(p.x) === 0)
  880. return this.curve.point(null, null);
  881. var c = this.y.redSub(p.y);
  882. if (c.cmpn(0) !== 0)
  883. c = c.redMul(this.x.redSub(p.x).redInvm());
  884. var nx = c.redSqr().redISub(this.x).redISub(p.x);
  885. var ny = c.redMul(this.x.redSub(nx)).redISub(this.y);
  886. return this.curve.point(nx, ny);
  887. };
  888. Point.prototype.dbl = function dbl() {
  889. if (this.inf)
  890. return this;
  891. // 2P = O
  892. var ys1 = this.y.redAdd(this.y);
  893. if (ys1.cmpn(0) === 0)
  894. return this.curve.point(null, null);
  895. var a = this.curve.a;
  896. var x2 = this.x.redSqr();
  897. var dyinv = ys1.redInvm();
  898. var c = x2.redAdd(x2).redIAdd(x2).redIAdd(a).redMul(dyinv);
  899. var nx = c.redSqr().redISub(this.x.redAdd(this.x));
  900. var ny = c.redMul(this.x.redSub(nx)).redISub(this.y);
  901. return this.curve.point(nx, ny);
  902. };
  903. Point.prototype.getX = function getX() {
  904. return this.x.fromRed();
  905. };
  906. Point.prototype.getY = function getY() {
  907. return this.y.fromRed();
  908. };
  909. Point.prototype.mul = function mul(k) {
  910. k = new BN(k, 16);
  911. if (this.isInfinity())
  912. return this;
  913. else if (this._hasDoubles(k))
  914. return this.curve._fixedNafMul(this, k);
  915. else if (this.curve.endo)
  916. return this.curve._endoWnafMulAdd([ this ], [ k ]);
  917. else
  918. return this.curve._wnafMul(this, k);
  919. };
  920. Point.prototype.mulAdd = function mulAdd(k1, p2, k2) {
  921. var points = [ this, p2 ];
  922. var coeffs = [ k1, k2 ];
  923. if (this.curve.endo)
  924. return this.curve._endoWnafMulAdd(points, coeffs);
  925. else
  926. return this.curve._wnafMulAdd(1, points, coeffs, 2);
  927. };
  928. Point.prototype.jmulAdd = function jmulAdd(k1, p2, k2) {
  929. var points = [ this, p2 ];
  930. var coeffs = [ k1, k2 ];
  931. if (this.curve.endo)
  932. return this.curve._endoWnafMulAdd(points, coeffs, true);
  933. else
  934. return this.curve._wnafMulAdd(1, points, coeffs, 2, true);
  935. };
  936. Point.prototype.eq = function eq(p) {
  937. return this === p ||
  938. this.inf === p.inf &&
  939. (this.inf || this.x.cmp(p.x) === 0 && this.y.cmp(p.y) === 0);
  940. };
  941. Point.prototype.neg = function neg(_precompute) {
  942. if (this.inf)
  943. return this;
  944. var res = this.curve.point(this.x, this.y.redNeg());
  945. if (_precompute && this.precomputed) {
  946. var pre = this.precomputed;
  947. var negate = function(p) {
  948. return p.neg();
  949. };
  950. res.precomputed = {
  951. naf: pre.naf && {
  952. wnd: pre.naf.wnd,
  953. points: pre.naf.points.map(negate),
  954. },
  955. doubles: pre.doubles && {
  956. step: pre.doubles.step,
  957. points: pre.doubles.points.map(negate),
  958. },
  959. };
  960. }
  961. return res;
  962. };
  963. Point.prototype.toJ = function toJ() {
  964. if (this.inf)
  965. return this.curve.jpoint(null, null, null);
  966. var res = this.curve.jpoint(this.x, this.y, this.curve.one);
  967. return res;
  968. };
  969. function JPoint(curve, x, y, z) {
  970. base.BasePoint.call(this, curve, 'jacobian');
  971. if (x === null && y === null && z === null) {
  972. this.x = this.curve.one;
  973. this.y = this.curve.one;
  974. this.z = new BN(0);
  975. } else {
  976. this.x = new BN(x, 16);
  977. this.y = new BN(y, 16);
  978. this.z = new BN(z, 16);
  979. }
  980. if (!this.x.red)
  981. this.x = this.x.toRed(this.curve.red);
  982. if (!this.y.red)
  983. this.y = this.y.toRed(this.curve.red);
  984. if (!this.z.red)
  985. this.z = this.z.toRed(this.curve.red);
  986. this.zOne = this.z === this.curve.one;
  987. }
  988. inherits_browser(JPoint, base.BasePoint);
  989. ShortCurve.prototype.jpoint = function jpoint(x, y, z) {
  990. return new JPoint(this, x, y, z);
  991. };
  992. JPoint.prototype.toP = function toP() {
  993. if (this.isInfinity())
  994. return this.curve.point(null, null);
  995. var zinv = this.z.redInvm();
  996. var zinv2 = zinv.redSqr();
  997. var ax = this.x.redMul(zinv2);
  998. var ay = this.y.redMul(zinv2).redMul(zinv);
  999. return this.curve.point(ax, ay);
  1000. };
  1001. JPoint.prototype.neg = function neg() {
  1002. return this.curve.jpoint(this.x, this.y.redNeg(), this.z);
  1003. };
  1004. JPoint.prototype.add = function add(p) {
  1005. // O + P = P
  1006. if (this.isInfinity())
  1007. return p;
  1008. // P + O = P
  1009. if (p.isInfinity())
  1010. return this;
  1011. // 12M + 4S + 7A
  1012. var pz2 = p.z.redSqr();
  1013. var z2 = this.z.redSqr();
  1014. var u1 = this.x.redMul(pz2);
  1015. var u2 = p.x.redMul(z2);
  1016. var s1 = this.y.redMul(pz2.redMul(p.z));
  1017. var s2 = p.y.redMul(z2.redMul(this.z));
  1018. var h = u1.redSub(u2);
  1019. var r = s1.redSub(s2);
  1020. if (h.cmpn(0) === 0) {
  1021. if (r.cmpn(0) !== 0)
  1022. return this.curve.jpoint(null, null, null);
  1023. else
  1024. return this.dbl();
  1025. }
  1026. var h2 = h.redSqr();
  1027. var h3 = h2.redMul(h);
  1028. var v = u1.redMul(h2);
  1029. var nx = r.redSqr().redIAdd(h3).redISub(v).redISub(v);
  1030. var ny = r.redMul(v.redISub(nx)).redISub(s1.redMul(h3));
  1031. var nz = this.z.redMul(p.z).redMul(h);
  1032. return this.curve.jpoint(nx, ny, nz);
  1033. };
  1034. JPoint.prototype.mixedAdd = function mixedAdd(p) {
  1035. // O + P = P
  1036. if (this.isInfinity())
  1037. return p.toJ();
  1038. // P + O = P
  1039. if (p.isInfinity())
  1040. return this;
  1041. // 8M + 3S + 7A
  1042. var z2 = this.z.redSqr();
  1043. var u1 = this.x;
  1044. var u2 = p.x.redMul(z2);
  1045. var s1 = this.y;
  1046. var s2 = p.y.redMul(z2).redMul(this.z);
  1047. var h = u1.redSub(u2);
  1048. var r = s1.redSub(s2);
  1049. if (h.cmpn(0) === 0) {
  1050. if (r.cmpn(0) !== 0)
  1051. return this.curve.jpoint(null, null, null);
  1052. else
  1053. return this.dbl();
  1054. }
  1055. var h2 = h.redSqr();
  1056. var h3 = h2.redMul(h);
  1057. var v = u1.redMul(h2);
  1058. var nx = r.redSqr().redIAdd(h3).redISub(v).redISub(v);
  1059. var ny = r.redMul(v.redISub(nx)).redISub(s1.redMul(h3));
  1060. var nz = this.z.redMul(h);
  1061. return this.curve.jpoint(nx, ny, nz);
  1062. };
  1063. JPoint.prototype.dblp = function dblp(pow) {
  1064. if (pow === 0)
  1065. return this;
  1066. if (this.isInfinity())
  1067. return this;
  1068. if (!pow)
  1069. return this.dbl();
  1070. var i;
  1071. if (this.curve.zeroA || this.curve.threeA) {
  1072. var r = this;
  1073. for (i = 0; i < pow; i++)
  1074. r = r.dbl();
  1075. return r;
  1076. }
  1077. // 1M + 2S + 1A + N * (4S + 5M + 8A)
  1078. // N = 1 => 6M + 6S + 9A
  1079. var a = this.curve.a;
  1080. var tinv = this.curve.tinv;
  1081. var jx = this.x;
  1082. var jy = this.y;
  1083. var jz = this.z;
  1084. var jz4 = jz.redSqr().redSqr();
  1085. // Reuse results
  1086. var jyd = jy.redAdd(jy);
  1087. for (i = 0; i < pow; i++) {
  1088. var jx2 = jx.redSqr();
  1089. var jyd2 = jyd.redSqr();
  1090. var jyd4 = jyd2.redSqr();
  1091. var c = jx2.redAdd(jx2).redIAdd(jx2).redIAdd(a.redMul(jz4));
  1092. var t1 = jx.redMul(jyd2);
  1093. var nx = c.redSqr().redISub(t1.redAdd(t1));
  1094. var t2 = t1.redISub(nx);
  1095. var dny = c.redMul(t2);
  1096. dny = dny.redIAdd(dny).redISub(jyd4);
  1097. var nz = jyd.redMul(jz);
  1098. if (i + 1 < pow)
  1099. jz4 = jz4.redMul(jyd4);
  1100. jx = nx;
  1101. jz = nz;
  1102. jyd = dny;
  1103. }
  1104. return this.curve.jpoint(jx, jyd.redMul(tinv), jz);
  1105. };
  1106. JPoint.prototype.dbl = function dbl() {
  1107. if (this.isInfinity())
  1108. return this;
  1109. if (this.curve.zeroA)
  1110. return this._zeroDbl();
  1111. else if (this.curve.threeA)
  1112. return this._threeDbl();
  1113. else
  1114. return this._dbl();
  1115. };
  1116. JPoint.prototype._zeroDbl = function _zeroDbl() {
  1117. var nx;
  1118. var ny;
  1119. var nz;
  1120. // Z = 1
  1121. if (this.zOne) {
  1122. // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html
  1123. // #doubling-mdbl-2007-bl
  1124. // 1M + 5S + 14A
  1125. // XX = X1^2
  1126. var xx = this.x.redSqr();
  1127. // YY = Y1^2
  1128. var yy = this.y.redSqr();
  1129. // YYYY = YY^2
  1130. var yyyy = yy.redSqr();
  1131. // S = 2 * ((X1 + YY)^2 - XX - YYYY)
  1132. var s = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
  1133. s = s.redIAdd(s);
  1134. // M = 3 * XX + a; a = 0
  1135. var m = xx.redAdd(xx).redIAdd(xx);
  1136. // T = M ^ 2 - 2*S
  1137. var t = m.redSqr().redISub(s).redISub(s);
  1138. // 8 * YYYY
  1139. var yyyy8 = yyyy.redIAdd(yyyy);
  1140. yyyy8 = yyyy8.redIAdd(yyyy8);
  1141. yyyy8 = yyyy8.redIAdd(yyyy8);
  1142. // X3 = T
  1143. nx = t;
  1144. // Y3 = M * (S - T) - 8 * YYYY
  1145. ny = m.redMul(s.redISub(t)).redISub(yyyy8);
  1146. // Z3 = 2*Y1
  1147. nz = this.y.redAdd(this.y);
  1148. } else {
  1149. // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html
  1150. // #doubling-dbl-2009-l
  1151. // 2M + 5S + 13A
  1152. // A = X1^2
  1153. var a = this.x.redSqr();
  1154. // B = Y1^2
  1155. var b = this.y.redSqr();
  1156. // C = B^2
  1157. var c = b.redSqr();
  1158. // D = 2 * ((X1 + B)^2 - A - C)
  1159. var d = this.x.redAdd(b).redSqr().redISub(a).redISub(c);
  1160. d = d.redIAdd(d);
  1161. // E = 3 * A
  1162. var e = a.redAdd(a).redIAdd(a);
  1163. // F = E^2
  1164. var f = e.redSqr();
  1165. // 8 * C
  1166. var c8 = c.redIAdd(c);
  1167. c8 = c8.redIAdd(c8);
  1168. c8 = c8.redIAdd(c8);
  1169. // X3 = F - 2 * D
  1170. nx = f.redISub(d).redISub(d);
  1171. // Y3 = E * (D - X3) - 8 * C
  1172. ny = e.redMul(d.redISub(nx)).redISub(c8);
  1173. // Z3 = 2 * Y1 * Z1
  1174. nz = this.y.redMul(this.z);
  1175. nz = nz.redIAdd(nz);
  1176. }
  1177. return this.curve.jpoint(nx, ny, nz);
  1178. };
  1179. JPoint.prototype._threeDbl = function _threeDbl() {
  1180. var nx;
  1181. var ny;
  1182. var nz;
  1183. // Z = 1
  1184. if (this.zOne) {
  1185. // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html
  1186. // #doubling-mdbl-2007-bl
  1187. // 1M + 5S + 15A
  1188. // XX = X1^2
  1189. var xx = this.x.redSqr();
  1190. // YY = Y1^2
  1191. var yy = this.y.redSqr();
  1192. // YYYY = YY^2
  1193. var yyyy = yy.redSqr();
  1194. // S = 2 * ((X1 + YY)^2 - XX - YYYY)
  1195. var s = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
  1196. s = s.redIAdd(s);
  1197. // M = 3 * XX + a
  1198. var m = xx.redAdd(xx).redIAdd(xx).redIAdd(this.curve.a);
  1199. // T = M^2 - 2 * S
  1200. var t = m.redSqr().redISub(s).redISub(s);
  1201. // X3 = T
  1202. nx = t;
  1203. // Y3 = M * (S - T) - 8 * YYYY
  1204. var yyyy8 = yyyy.redIAdd(yyyy);
  1205. yyyy8 = yyyy8.redIAdd(yyyy8);
  1206. yyyy8 = yyyy8.redIAdd(yyyy8);
  1207. ny = m.redMul(s.redISub(t)).redISub(yyyy8);
  1208. // Z3 = 2 * Y1
  1209. nz = this.y.redAdd(this.y);
  1210. } else {
  1211. // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
  1212. // 3M + 5S
  1213. // delta = Z1^2
  1214. var delta = this.z.redSqr();
  1215. // gamma = Y1^2
  1216. var gamma = this.y.redSqr();
  1217. // beta = X1 * gamma
  1218. var beta = this.x.redMul(gamma);
  1219. // alpha = 3 * (X1 - delta) * (X1 + delta)
  1220. var alpha = this.x.redSub(delta).redMul(this.x.redAdd(delta));
  1221. alpha = alpha.redAdd(alpha).redIAdd(alpha);
  1222. // X3 = alpha^2 - 8 * beta
  1223. var beta4 = beta.redIAdd(beta);
  1224. beta4 = beta4.redIAdd(beta4);
  1225. var beta8 = beta4.redAdd(beta4);
  1226. nx = alpha.redSqr().redISub(beta8);
  1227. // Z3 = (Y1 + Z1)^2 - gamma - delta
  1228. nz = this.y.redAdd(this.z).redSqr().redISub(gamma).redISub(delta);
  1229. // Y3 = alpha * (4 * beta - X3) - 8 * gamma^2
  1230. var ggamma8 = gamma.redSqr();
  1231. ggamma8 = ggamma8.redIAdd(ggamma8);
  1232. ggamma8 = ggamma8.redIAdd(ggamma8);
  1233. ggamma8 = ggamma8.redIAdd(ggamma8);
  1234. ny = alpha.redMul(beta4.redISub(nx)).redISub(ggamma8);
  1235. }
  1236. return this.curve.jpoint(nx, ny, nz);
  1237. };
  1238. JPoint.prototype._dbl = function _dbl() {
  1239. var a = this.curve.a;
  1240. // 4M + 6S + 10A
  1241. var jx = this.x;
  1242. var jy = this.y;
  1243. var jz = this.z;
  1244. var jz4 = jz.redSqr().redSqr();
  1245. var jx2 = jx.redSqr();
  1246. var jy2 = jy.redSqr();
  1247. var c = jx2.redAdd(jx2).redIAdd(jx2).redIAdd(a.redMul(jz4));
  1248. var jxd4 = jx.redAdd(jx);
  1249. jxd4 = jxd4.redIAdd(jxd4);
  1250. var t1 = jxd4.redMul(jy2);
  1251. var nx = c.redSqr().redISub(t1.redAdd(t1));
  1252. var t2 = t1.redISub(nx);
  1253. var jyd8 = jy2.redSqr();
  1254. jyd8 = jyd8.redIAdd(jyd8);
  1255. jyd8 = jyd8.redIAdd(jyd8);
  1256. jyd8 = jyd8.redIAdd(jyd8);
  1257. var ny = c.redMul(t2).redISub(jyd8);
  1258. var nz = jy.redAdd(jy).redMul(jz);
  1259. return this.curve.jpoint(nx, ny, nz);
  1260. };
  1261. JPoint.prototype.trpl = function trpl() {
  1262. if (!this.curve.zeroA)
  1263. return this.dbl().add(this);
  1264. // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#tripling-tpl-2007-bl
  1265. // 5M + 10S + ...
  1266. // XX = X1^2
  1267. var xx = this.x.redSqr();
  1268. // YY = Y1^2
  1269. var yy = this.y.redSqr();
  1270. // ZZ = Z1^2
  1271. var zz = this.z.redSqr();
  1272. // YYYY = YY^2
  1273. var yyyy = yy.redSqr();
  1274. // M = 3 * XX + a * ZZ2; a = 0
  1275. var m = xx.redAdd(xx).redIAdd(xx);
  1276. // MM = M^2
  1277. var mm = m.redSqr();
  1278. // E = 6 * ((X1 + YY)^2 - XX - YYYY) - MM
  1279. var e = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
  1280. e = e.redIAdd(e);
  1281. e = e.redAdd(e).redIAdd(e);
  1282. e = e.redISub(mm);
  1283. // EE = E^2
  1284. var ee = e.redSqr();
  1285. // T = 16*YYYY
  1286. var t = yyyy.redIAdd(yyyy);
  1287. t = t.redIAdd(t);
  1288. t = t.redIAdd(t);
  1289. t = t.redIAdd(t);
  1290. // U = (M + E)^2 - MM - EE - T
  1291. var u = m.redIAdd(e).redSqr().redISub(mm).redISub(ee).redISub(t);
  1292. // X3 = 4 * (X1 * EE - 4 * YY * U)
  1293. var yyu4 = yy.redMul(u);
  1294. yyu4 = yyu4.redIAdd(yyu4);
  1295. yyu4 = yyu4.redIAdd(yyu4);
  1296. var nx = this.x.redMul(ee).redISub(yyu4);
  1297. nx = nx.redIAdd(nx);
  1298. nx = nx.redIAdd(nx);
  1299. // Y3 = 8 * Y1 * (U * (T - U) - E * EE)
  1300. var ny = this.y.redMul(u.redMul(t.redISub(u)).redISub(e.redMul(ee)));
  1301. ny = ny.redIAdd(ny);
  1302. ny = ny.redIAdd(ny);
  1303. ny = ny.redIAdd(ny);
  1304. // Z3 = (Z1 + E)^2 - ZZ - EE
  1305. var nz = this.z.redAdd(e).redSqr().redISub(zz).redISub(ee);
  1306. return this.curve.jpoint(nx, ny, nz);
  1307. };
  1308. JPoint.prototype.mul = function mul(k, kbase) {
  1309. k = new BN(k, kbase);
  1310. return this.curve._wnafMul(this, k);
  1311. };
  1312. JPoint.prototype.eq = function eq(p) {
  1313. if (p.type === 'affine')
  1314. return this.eq(p.toJ());
  1315. if (this === p)
  1316. return true;
  1317. // x1 * z2^2 == x2 * z1^2
  1318. var z2 = this.z.redSqr();
  1319. var pz2 = p.z.redSqr();
  1320. if (this.x.redMul(pz2).redISub(p.x.redMul(z2)).cmpn(0) !== 0)
  1321. return false;
  1322. // y1 * z2^3 == y2 * z1^3
  1323. var z3 = z2.redMul(this.z);
  1324. var pz3 = pz2.redMul(p.z);
  1325. return this.y.redMul(pz3).redISub(p.y.redMul(z3)).cmpn(0) === 0;
  1326. };
  1327. JPoint.prototype.eqXToP = function eqXToP(x) {
  1328. var zs = this.z.redSqr();
  1329. var rx = x.toRed(this.curve.red).redMul(zs);
  1330. if (this.x.cmp(rx) === 0)
  1331. return true;
  1332. var xc = x.clone();
  1333. var t = this.curve.redN.redMul(zs);
  1334. for (;;) {
  1335. xc.iadd(this.curve.n);
  1336. if (xc.cmp(this.curve.p) >= 0)
  1337. return false;
  1338. rx.redIAdd(t);
  1339. if (this.x.cmp(rx) === 0)
  1340. return true;
  1341. }
  1342. };
  1343. JPoint.prototype.inspect = function inspect() {
  1344. if (this.isInfinity())
  1345. return '<EC JPoint Infinity>';
  1346. return '<EC JPoint x: ' + this.x.toString(16, 2) +
  1347. ' y: ' + this.y.toString(16, 2) +
  1348. ' z: ' + this.z.toString(16, 2) + '>';
  1349. };
  1350. JPoint.prototype.isInfinity = function isInfinity() {
  1351. // XXX This code assumes that zero is always zero in red
  1352. return this.z.cmpn(0) === 0;
  1353. };
  1354. var curve_1 = createCommonjsModule(function (module, exports) {
  1355. 'use strict';
  1356. var curve = exports;
  1357. curve.base = base;
  1358. curve.short = short_1;
  1359. curve.mont = /*RicMoo:ethers:require(./mont)*/(null);
  1360. curve.edwards = /*RicMoo:ethers:require(./edwards)*/(null);
  1361. });
  1362. var curves_1 = createCommonjsModule(function (module, exports) {
  1363. 'use strict';
  1364. var curves = exports;
  1365. var assert = utils_1$1.assert;
  1366. function PresetCurve(options) {
  1367. if (options.type === 'short')
  1368. this.curve = new curve_1.short(options);
  1369. else if (options.type === 'edwards')
  1370. this.curve = new curve_1.edwards(options);
  1371. else
  1372. this.curve = new curve_1.mont(options);
  1373. this.g = this.curve.g;
  1374. this.n = this.curve.n;
  1375. this.hash = options.hash;
  1376. assert(this.g.validate(), 'Invalid curve');
  1377. assert(this.g.mul(this.n).isInfinity(), 'Invalid curve, G*N != O');
  1378. }
  1379. curves.PresetCurve = PresetCurve;
  1380. function defineCurve(name, options) {
  1381. Object.defineProperty(curves, name, {
  1382. configurable: true,
  1383. enumerable: true,
  1384. get: function() {
  1385. var curve = new PresetCurve(options);
  1386. Object.defineProperty(curves, name, {
  1387. configurable: true,
  1388. enumerable: true,
  1389. value: curve,
  1390. });
  1391. return curve;
  1392. },
  1393. });
  1394. }
  1395. defineCurve('p192', {
  1396. type: 'short',
  1397. prime: 'p192',
  1398. p: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff',
  1399. a: 'ffffffff ffffffff ffffffff fffffffe ffffffff fffffffc',
  1400. b: '64210519 e59c80e7 0fa7e9ab 72243049 feb8deec c146b9b1',
  1401. n: 'ffffffff ffffffff ffffffff 99def836 146bc9b1 b4d22831',
  1402. hash: hash.sha256,
  1403. gRed: false,
  1404. g: [
  1405. '188da80e b03090f6 7cbf20eb 43a18800 f4ff0afd 82ff1012',
  1406. '07192b95 ffc8da78 631011ed 6b24cdd5 73f977a1 1e794811',
  1407. ],
  1408. });
  1409. defineCurve('p224', {
  1410. type: 'short',
  1411. prime: 'p224',
  1412. p: 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001',
  1413. a: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff fffffffe',
  1414. b: 'b4050a85 0c04b3ab f5413256 5044b0b7 d7bfd8ba 270b3943 2355ffb4',
  1415. n: 'ffffffff ffffffff ffffffff ffff16a2 e0b8f03e 13dd2945 5c5c2a3d',
  1416. hash: hash.sha256,
  1417. gRed: false,
  1418. g: [
  1419. 'b70e0cbd 6bb4bf7f 321390b9 4a03c1d3 56c21122 343280d6 115c1d21',
  1420. 'bd376388 b5f723fb 4c22dfe6 cd4375a0 5a074764 44d58199 85007e34',
  1421. ],
  1422. });
  1423. defineCurve('p256', {
  1424. type: 'short',
  1425. prime: null,
  1426. p: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff',
  1427. a: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff fffffffc',
  1428. b: '5ac635d8 aa3a93e7 b3ebbd55 769886bc 651d06b0 cc53b0f6 3bce3c3e 27d2604b',
  1429. n: 'ffffffff 00000000 ffffffff ffffffff bce6faad a7179e84 f3b9cac2 fc632551',
  1430. hash: hash.sha256,
  1431. gRed: false,
  1432. g: [
  1433. '6b17d1f2 e12c4247 f8bce6e5 63a440f2 77037d81 2deb33a0 f4a13945 d898c296',
  1434. '4fe342e2 fe1a7f9b 8ee7eb4a 7c0f9e16 2bce3357 6b315ece cbb64068 37bf51f5',
  1435. ],
  1436. });
  1437. defineCurve('p384', {
  1438. type: 'short',
  1439. prime: null,
  1440. p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  1441. 'fffffffe ffffffff 00000000 00000000 ffffffff',
  1442. a: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  1443. 'fffffffe ffffffff 00000000 00000000 fffffffc',
  1444. b: 'b3312fa7 e23ee7e4 988e056b e3f82d19 181d9c6e fe814112 0314088f ' +
  1445. '5013875a c656398d 8a2ed19d 2a85c8ed d3ec2aef',
  1446. n: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff c7634d81 ' +
  1447. 'f4372ddf 581a0db2 48b0a77a ecec196a ccc52973',
  1448. hash: hash.sha384,
  1449. gRed: false,
  1450. g: [
  1451. 'aa87ca22 be8b0537 8eb1c71e f320ad74 6e1d3b62 8ba79b98 59f741e0 82542a38 ' +
  1452. '5502f25d bf55296c 3a545e38 72760ab7',
  1453. '3617de4a 96262c6f 5d9e98bf 9292dc29 f8f41dbd 289a147c e9da3113 b5f0b8c0 ' +
  1454. '0a60b1ce 1d7e819d 7a431d7c 90ea0e5f',
  1455. ],
  1456. });
  1457. defineCurve('p521', {
  1458. type: 'short',
  1459. prime: null,
  1460. p: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  1461. 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  1462. 'ffffffff ffffffff ffffffff ffffffff ffffffff',
  1463. a: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  1464. 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  1465. 'ffffffff ffffffff ffffffff ffffffff fffffffc',
  1466. b: '00000051 953eb961 8e1c9a1f 929a21a0 b68540ee a2da725b ' +
  1467. '99b315f3 b8b48991 8ef109e1 56193951 ec7e937b 1652c0bd ' +
  1468. '3bb1bf07 3573df88 3d2c34f1 ef451fd4 6b503f00',
  1469. n: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  1470. 'ffffffff ffffffff fffffffa 51868783 bf2f966b 7fcc0148 ' +
  1471. 'f709a5d0 3bb5c9b8 899c47ae bb6fb71e 91386409',
  1472. hash: hash.sha512,
  1473. gRed: false,
  1474. g: [
  1475. '000000c6 858e06b7 0404e9cd 9e3ecb66 2395b442 9c648139 ' +
  1476. '053fb521 f828af60 6b4d3dba a14b5e77 efe75928 fe1dc127 ' +
  1477. 'a2ffa8de 3348b3c1 856a429b f97e7e31 c2e5bd66',
  1478. '00000118 39296a78 9a3bc004 5c8a5fb4 2c7d1bd9 98f54449 ' +
  1479. '579b4468 17afbd17 273e662c 97ee7299 5ef42640 c550b901 ' +
  1480. '3fad0761 353c7086 a272c240 88be9476 9fd16650',
  1481. ],
  1482. });
  1483. defineCurve('curve25519', {
  1484. type: 'mont',
  1485. prime: 'p25519',
  1486. p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
  1487. a: '76d06',
  1488. b: '1',
  1489. n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
  1490. hash: hash.sha256,
  1491. gRed: false,
  1492. g: [
  1493. '9',
  1494. ],
  1495. });
  1496. defineCurve('ed25519', {
  1497. type: 'edwards',
  1498. prime: 'p25519',
  1499. p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
  1500. a: '-1',
  1501. c: '1',
  1502. // -121665 * (121666^(-1)) (mod P)
  1503. d: '52036cee2b6ffe73 8cc740797779e898 00700a4d4141d8ab 75eb4dca135978a3',
  1504. n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
  1505. hash: hash.sha256,
  1506. gRed: false,
  1507. g: [
  1508. '216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a',
  1509. // 4/5
  1510. '6666666666666666666666666666666666666666666666666666666666666658',
  1511. ],
  1512. });
  1513. var pre;
  1514. try {
  1515. pre = /*RicMoo:ethers:require(./precomputed/secp256k1)*/(null).crash();
  1516. } catch (e) {
  1517. pre = undefined;
  1518. }
  1519. defineCurve('secp256k1', {
  1520. type: 'short',
  1521. prime: 'k256',
  1522. p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f',
  1523. a: '0',
  1524. b: '7',
  1525. n: 'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141',
  1526. h: '1',
  1527. hash: hash.sha256,
  1528. // Precomputed endomorphism
  1529. beta: '7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee',
  1530. lambda: '5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72',
  1531. basis: [
  1532. {
  1533. a: '3086d221a7d46bcde86c90e49284eb15',
  1534. b: '-e4437ed6010e88286f547fa90abfe4c3',
  1535. },
  1536. {
  1537. a: '114ca50f7a8e2f3f657c1108d9d44cfd8',
  1538. b: '3086d221a7d46bcde86c90e49284eb15',
  1539. },
  1540. ],
  1541. gRed: false,
  1542. g: [
  1543. '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
  1544. '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
  1545. pre,
  1546. ],
  1547. });
  1548. });
  1549. 'use strict';
  1550. function HmacDRBG(options) {
  1551. if (!(this instanceof HmacDRBG))
  1552. return new HmacDRBG(options);
  1553. this.hash = options.hash;
  1554. this.predResist = !!options.predResist;
  1555. this.outLen = this.hash.outSize;
  1556. this.minEntropy = options.minEntropy || this.hash.hmacStrength;
  1557. this._reseed = null;
  1558. this.reseedInterval = null;
  1559. this.K = null;
  1560. this.V = null;
  1561. var entropy = utils_1.toArray(options.entropy, options.entropyEnc || 'hex');
  1562. var nonce = utils_1.toArray(options.nonce, options.nonceEnc || 'hex');
  1563. var pers = utils_1.toArray(options.pers, options.persEnc || 'hex');
  1564. minimalisticAssert(entropy.length >= (this.minEntropy / 8),
  1565. 'Not enough entropy. Minimum is: ' + this.minEntropy + ' bits');
  1566. this._init(entropy, nonce, pers);
  1567. }
  1568. var hmacDrbg = HmacDRBG;
  1569. HmacDRBG.prototype._init = function init(entropy, nonce, pers) {
  1570. var seed = entropy.concat(nonce).concat(pers);
  1571. this.K = new Array(this.outLen / 8);
  1572. this.V = new Array(this.outLen / 8);
  1573. for (var i = 0; i < this.V.length; i++) {
  1574. this.K[i] = 0x00;
  1575. this.V[i] = 0x01;
  1576. }
  1577. this._update(seed);
  1578. this._reseed = 1;
  1579. this.reseedInterval = 0x1000000000000; // 2^48
  1580. };
  1581. HmacDRBG.prototype._hmac = function hmac() {
  1582. return new hash.hmac(this.hash, this.K);
  1583. };
  1584. HmacDRBG.prototype._update = function update(seed) {
  1585. var kmac = this._hmac()
  1586. .update(this.V)
  1587. .update([ 0x00 ]);
  1588. if (seed)
  1589. kmac = kmac.update(seed);
  1590. this.K = kmac.digest();
  1591. this.V = this._hmac().update(this.V).digest();
  1592. if (!seed)
  1593. return;
  1594. this.K = this._hmac()
  1595. .update(this.V)
  1596. .update([ 0x01 ])
  1597. .update(seed)
  1598. .digest();
  1599. this.V = this._hmac().update(this.V).digest();
  1600. };
  1601. HmacDRBG.prototype.reseed = function reseed(entropy, entropyEnc, add, addEnc) {
  1602. // Optional entropy enc
  1603. if (typeof entropyEnc !== 'string') {
  1604. addEnc = add;
  1605. add = entropyEnc;
  1606. entropyEnc = null;
  1607. }
  1608. entropy = utils_1.toArray(entropy, entropyEnc);
  1609. add = utils_1.toArray(add, addEnc);
  1610. minimalisticAssert(entropy.length >= (this.minEntropy / 8),
  1611. 'Not enough entropy. Minimum is: ' + this.minEntropy + ' bits');
  1612. this._update(entropy.concat(add || []));
  1613. this._reseed = 1;
  1614. };
  1615. HmacDRBG.prototype.generate = function generate(len, enc, add, addEnc) {
  1616. if (this._reseed > this.reseedInterval)
  1617. throw new Error('Reseed is required');
  1618. // Optional encoding
  1619. if (typeof enc !== 'string') {
  1620. addEnc = add;
  1621. add = enc;
  1622. enc = null;
  1623. }
  1624. // Optional additional data
  1625. if (add) {
  1626. add = utils_1.toArray(add, addEnc || 'hex');
  1627. this._update(add);
  1628. }
  1629. var temp = [];
  1630. while (temp.length < len) {
  1631. this.V = this._hmac().update(this.V).digest();
  1632. temp = temp.concat(this.V);
  1633. }
  1634. var res = temp.slice(0, len);
  1635. this._update(add);
  1636. this._reseed++;
  1637. return utils_1.encode(res, enc);
  1638. };
  1639. 'use strict';
  1640. var assert$3 = utils_1$1.assert;
  1641. function KeyPair(ec, options) {
  1642. this.ec = ec;
  1643. this.priv = null;
  1644. this.pub = null;
  1645. // KeyPair(ec, { priv: ..., pub: ... })
  1646. if (options.priv)
  1647. this._importPrivate(options.priv, options.privEnc);
  1648. if (options.pub)
  1649. this._importPublic(options.pub, options.pubEnc);
  1650. }
  1651. var key = KeyPair;
  1652. KeyPair.fromPublic = function fromPublic(ec, pub, enc) {
  1653. if (pub instanceof KeyPair)
  1654. return pub;
  1655. return new KeyPair(ec, {
  1656. pub: pub,
  1657. pubEnc: enc,
  1658. });
  1659. };
  1660. KeyPair.fromPrivate = function fromPrivate(ec, priv, enc) {
  1661. if (priv instanceof KeyPair)
  1662. return priv;
  1663. return new KeyPair(ec, {
  1664. priv: priv,
  1665. privEnc: enc,
  1666. });
  1667. };
  1668. KeyPair.prototype.validate = function validate() {
  1669. var pub = this.getPublic();
  1670. if (pub.isInfinity())
  1671. return { result: false, reason: 'Invalid public key' };
  1672. if (!pub.validate())
  1673. return { result: false, reason: 'Public key is not a point' };
  1674. if (!pub.mul(this.ec.curve.n).isInfinity())
  1675. return { result: false, reason: 'Public key * N != O' };
  1676. return { result: true, reason: null };
  1677. };
  1678. KeyPair.prototype.getPublic = function getPublic(compact, enc) {
  1679. // compact is optional argument
  1680. if (typeof compact === 'string') {
  1681. enc = compact;
  1682. compact = null;
  1683. }
  1684. if (!this.pub)
  1685. this.pub = this.ec.g.mul(this.priv);
  1686. if (!enc)
  1687. return this.pub;
  1688. return this.pub.encode(enc, compact);
  1689. };
  1690. KeyPair.prototype.getPrivate = function getPrivate(enc) {
  1691. if (enc === 'hex')
  1692. return this.priv.toString(16, 2);
  1693. else
  1694. return this.priv;
  1695. };
  1696. KeyPair.prototype._importPrivate = function _importPrivate(key, enc) {
  1697. this.priv = new BN(key, enc || 16);
  1698. // Ensure that the priv won't be bigger than n, otherwise we may fail
  1699. // in fixed multiplication method
  1700. this.priv = this.priv.umod(this.ec.curve.n);
  1701. };
  1702. KeyPair.prototype._importPublic = function _importPublic(key, enc) {
  1703. if (key.x || key.y) {
  1704. // Montgomery points only have an `x` coordinate.
  1705. // Weierstrass/Edwards points on the other hand have both `x` and
  1706. // `y` coordinates.
  1707. if (this.ec.curve.type === 'mont') {
  1708. assert$3(key.x, 'Need x coordinate');
  1709. } else if (this.ec.curve.type === 'short' ||
  1710. this.ec.curve.type === 'edwards') {
  1711. assert$3(key.x && key.y, 'Need both x and y coordinate');
  1712. }
  1713. this.pub = this.ec.curve.point(key.x, key.y);
  1714. return;
  1715. }
  1716. this.pub = this.ec.curve.decodePoint(key, enc);
  1717. };
  1718. // ECDH
  1719. KeyPair.prototype.derive = function derive(pub) {
  1720. if(!pub.validate()) {
  1721. assert$3(pub.validate(), 'public point not validated');
  1722. }
  1723. return pub.mul(this.priv).getX();
  1724. };
  1725. // ECDSA
  1726. KeyPair.prototype.sign = function sign(msg, enc, options) {
  1727. return this.ec.sign(msg, this, enc, options);
  1728. };
  1729. KeyPair.prototype.verify = function verify(msg, signature) {
  1730. return this.ec.verify(msg, signature, this);
  1731. };
  1732. KeyPair.prototype.inspect = function inspect() {
  1733. return '<Key priv: ' + (this.priv && this.priv.toString(16, 2)) +
  1734. ' pub: ' + (this.pub && this.pub.inspect()) + ' >';
  1735. };
  1736. 'use strict';
  1737. var assert$4 = utils_1$1.assert;
  1738. function Signature(options, enc) {
  1739. if (options instanceof Signature)
  1740. return options;
  1741. if (this._importDER(options, enc))
  1742. return;
  1743. assert$4(options.r && options.s, 'Signature without r or s');
  1744. this.r = new BN(options.r, 16);
  1745. this.s = new BN(options.s, 16);
  1746. if (options.recoveryParam === undefined)
  1747. this.recoveryParam = null;
  1748. else
  1749. this.recoveryParam = options.recoveryParam;
  1750. }
  1751. var signature = Signature;
  1752. function Position() {
  1753. this.place = 0;
  1754. }
  1755. function getLength(buf, p) {
  1756. var initial = buf[p.place++];
  1757. if (!(initial & 0x80)) {
  1758. return initial;
  1759. }
  1760. var octetLen = initial & 0xf;
  1761. // Indefinite length or overflow
  1762. if (octetLen === 0 || octetLen > 4) {
  1763. return false;
  1764. }
  1765. var val = 0;
  1766. for (var i = 0, off = p.place; i < octetLen; i++, off++) {
  1767. val <<= 8;
  1768. val |= buf[off];
  1769. val >>>= 0;
  1770. }
  1771. // Leading zeroes
  1772. if (val <= 0x7f) {
  1773. return false;
  1774. }
  1775. p.place = off;
  1776. return val;
  1777. }
  1778. function rmPadding(buf) {
  1779. var i = 0;
  1780. var len = buf.length - 1;
  1781. while (!buf[i] && !(buf[i + 1] & 0x80) && i < len) {
  1782. i++;
  1783. }
  1784. if (i === 0) {
  1785. return buf;
  1786. }
  1787. return buf.slice(i);
  1788. }
  1789. Signature.prototype._importDER = function _importDER(data, enc) {
  1790. data = utils_1$1.toArray(data, enc);
  1791. var p = new Position();
  1792. if (data[p.place++] !== 0x30) {
  1793. return false;
  1794. }
  1795. var len = getLength(data, p);
  1796. if (len === false) {
  1797. return false;
  1798. }
  1799. if ((len + p.place) !== data.length) {
  1800. return false;
  1801. }
  1802. if (data[p.place++] !== 0x02) {
  1803. return false;
  1804. }
  1805. var rlen = getLength(data, p);
  1806. if (rlen === false) {
  1807. return false;
  1808. }
  1809. var r = data.slice(p.place, rlen + p.place);
  1810. p.place += rlen;
  1811. if (data[p.place++] !== 0x02) {
  1812. return false;
  1813. }
  1814. var slen = getLength(data, p);
  1815. if (slen === false) {
  1816. return false;
  1817. }
  1818. if (data.length !== slen + p.place) {
  1819. return false;
  1820. }
  1821. var s = data.slice(p.place, slen + p.place);
  1822. if (r[0] === 0) {
  1823. if (r[1] & 0x80) {
  1824. r = r.slice(1);
  1825. } else {
  1826. // Leading zeroes
  1827. return false;
  1828. }
  1829. }
  1830. if (s[0] === 0) {
  1831. if (s[1] & 0x80) {
  1832. s = s.slice(1);
  1833. } else {
  1834. // Leading zeroes
  1835. return false;
  1836. }
  1837. }
  1838. this.r = new BN(r);
  1839. this.s = new BN(s);
  1840. this.recoveryParam = null;
  1841. return true;
  1842. };
  1843. function constructLength(arr, len) {
  1844. if (len < 0x80) {
  1845. arr.push(len);
  1846. return;
  1847. }
  1848. var octets = 1 + (Math.log(len) / Math.LN2 >>> 3);
  1849. arr.push(octets | 0x80);
  1850. while (--octets) {
  1851. arr.push((len >>> (octets << 3)) & 0xff);
  1852. }
  1853. arr.push(len);
  1854. }
  1855. Signature.prototype.toDER = function toDER(enc) {
  1856. var r = this.r.toArray();
  1857. var s = this.s.toArray();
  1858. // Pad values
  1859. if (r[0] & 0x80)
  1860. r = [ 0 ].concat(r);
  1861. // Pad values
  1862. if (s[0] & 0x80)
  1863. s = [ 0 ].concat(s);
  1864. r = rmPadding(r);
  1865. s = rmPadding(s);
  1866. while (!s[0] && !(s[1] & 0x80)) {
  1867. s = s.slice(1);
  1868. }
  1869. var arr = [ 0x02 ];
  1870. constructLength(arr, r.length);
  1871. arr = arr.concat(r);
  1872. arr.push(0x02);
  1873. constructLength(arr, s.length);
  1874. var backHalf = arr.concat(s);
  1875. var res = [ 0x30 ];
  1876. constructLength(res, backHalf.length);
  1877. res = res.concat(backHalf);
  1878. return utils_1$1.encode(res, enc);
  1879. };
  1880. 'use strict';
  1881. var rand = /*RicMoo:ethers:require(brorand)*/(function() { throw new Error('unsupported'); });
  1882. var assert$5 = utils_1$1.assert;
  1883. function EC(options) {
  1884. if (!(this instanceof EC))
  1885. return new EC(options);
  1886. // Shortcut `elliptic.ec(curve-name)`
  1887. if (typeof options === 'string') {
  1888. assert$5(Object.prototype.hasOwnProperty.call(curves_1, options),
  1889. 'Unknown curve ' + options);
  1890. options = curves_1[options];
  1891. }
  1892. // Shortcut for `elliptic.ec(elliptic.curves.curveName)`
  1893. if (options instanceof curves_1.PresetCurve)
  1894. options = { curve: options };
  1895. this.curve = options.curve.curve;
  1896. this.n = this.curve.n;
  1897. this.nh = this.n.ushrn(1);
  1898. this.g = this.curve.g;
  1899. // Point on curve
  1900. this.g = options.curve.g;
  1901. this.g.precompute(options.curve.n.bitLength() + 1);
  1902. // Hash for function for DRBG
  1903. this.hash = options.hash || options.curve.hash;
  1904. }
  1905. var ec = EC;
  1906. EC.prototype.keyPair = function keyPair(options) {
  1907. return new key(this, options);
  1908. };
  1909. EC.prototype.keyFromPrivate = function keyFromPrivate(priv, enc) {
  1910. return key.fromPrivate(this, priv, enc);
  1911. };
  1912. EC.prototype.keyFromPublic = function keyFromPublic(pub, enc) {
  1913. return key.fromPublic(this, pub, enc);
  1914. };
  1915. EC.prototype.genKeyPair = function genKeyPair(options) {
  1916. if (!options)
  1917. options = {};
  1918. // Instantiate Hmac_DRBG
  1919. var drbg = new hmacDrbg({
  1920. hash: this.hash,
  1921. pers: options.pers,
  1922. persEnc: options.persEnc || 'utf8',
  1923. entropy: options.entropy || rand(this.hash.hmacStrength),
  1924. entropyEnc: options.entropy && options.entropyEnc || 'utf8',
  1925. nonce: this.n.toArray(),
  1926. });
  1927. var bytes = this.n.byteLength();
  1928. var ns2 = this.n.sub(new BN(2));
  1929. for (;;) {
  1930. var priv = new BN(drbg.generate(bytes));
  1931. if (priv.cmp(ns2) > 0)
  1932. continue;
  1933. priv.iaddn(1);
  1934. return this.keyFromPrivate(priv);
  1935. }
  1936. };
  1937. EC.prototype._truncateToN = function _truncateToN(msg, truncOnly) {
  1938. var delta = msg.byteLength() * 8 - this.n.bitLength();
  1939. if (delta > 0)
  1940. msg = msg.ushrn(delta);
  1941. if (!truncOnly && msg.cmp(this.n) >= 0)
  1942. return msg.sub(this.n);
  1943. else
  1944. return msg;
  1945. };
  1946. EC.prototype.sign = function sign(msg, key, enc, options) {
  1947. if (typeof enc === 'object') {
  1948. options = enc;
  1949. enc = null;
  1950. }
  1951. if (!options)
  1952. options = {};
  1953. key = this.keyFromPrivate(key, enc);
  1954. msg = this._truncateToN(new BN(msg, 16));
  1955. // Zero-extend key to provide enough entropy
  1956. var bytes = this.n.byteLength();
  1957. var bkey = key.getPrivate().toArray('be', bytes);
  1958. // Zero-extend nonce to have the same byte size as N
  1959. var nonce = msg.toArray('be', bytes);
  1960. // Instantiate Hmac_DRBG
  1961. var drbg = new hmacDrbg({
  1962. hash: this.hash,
  1963. entropy: bkey,
  1964. nonce: nonce,
  1965. pers: options.pers,
  1966. persEnc: options.persEnc || 'utf8',
  1967. });
  1968. // Number of bytes to generate
  1969. var ns1 = this.n.sub(new BN(1));
  1970. for (var iter = 0; ; iter++) {
  1971. var k = options.k ?
  1972. options.k(iter) :
  1973. new BN(drbg.generate(this.n.byteLength()));
  1974. k = this._truncateToN(k, true);
  1975. if (k.cmpn(1) <= 0 || k.cmp(ns1) >= 0)
  1976. continue;
  1977. var kp = this.g.mul(k);
  1978. if (kp.isInfinity())
  1979. continue;
  1980. var kpX = kp.getX();
  1981. var r = kpX.umod(this.n);
  1982. if (r.cmpn(0) === 0)
  1983. continue;
  1984. var s = k.invm(this.n).mul(r.mul(key.getPrivate()).iadd(msg));
  1985. s = s.umod(this.n);
  1986. if (s.cmpn(0) === 0)
  1987. continue;
  1988. var recoveryParam = (kp.getY().isOdd() ? 1 : 0) |
  1989. (kpX.cmp(r) !== 0 ? 2 : 0);
  1990. // Use complement of `s`, if it is > `n / 2`
  1991. if (options.canonical && s.cmp(this.nh) > 0) {
  1992. s = this.n.sub(s);
  1993. recoveryParam ^= 1;
  1994. }
  1995. return new signature({ r: r, s: s, recoveryParam: recoveryParam });
  1996. }
  1997. };
  1998. EC.prototype.verify = function verify(msg, signature$1, key, enc) {
  1999. msg = this._truncateToN(new BN(msg, 16));
  2000. key = this.keyFromPublic(key, enc);
  2001. signature$1 = new signature(signature$1, 'hex');
  2002. // Perform primitive values validation
  2003. var r = signature$1.r;
  2004. var s = signature$1.s;
  2005. if (r.cmpn(1) < 0 || r.cmp(this.n) >= 0)
  2006. return false;
  2007. if (s.cmpn(1) < 0 || s.cmp(this.n) >= 0)
  2008. return false;
  2009. // Validate signature
  2010. var sinv = s.invm(this.n);
  2011. var u1 = sinv.mul(msg).umod(this.n);
  2012. var u2 = sinv.mul(r).umod(this.n);
  2013. var p;
  2014. if (!this.curve._maxwellTrick) {
  2015. p = this.g.mulAdd(u1, key.getPublic(), u2);
  2016. if (p.isInfinity())
  2017. return false;
  2018. return p.getX().umod(this.n).cmp(r) === 0;
  2019. }
  2020. // NOTE: Greg Maxwell's trick, inspired by:
  2021. // https://git.io/vad3K
  2022. p = this.g.jmulAdd(u1, key.getPublic(), u2);
  2023. if (p.isInfinity())
  2024. return false;
  2025. // Compare `p.x` of Jacobian point with `r`,
  2026. // this will do `p.x == r * p.z^2` instead of multiplying `p.x` by the
  2027. // inverse of `p.z^2`
  2028. return p.eqXToP(r);
  2029. };
  2030. EC.prototype.recoverPubKey = function(msg, signature$1, j, enc) {
  2031. assert$5((3 & j) === j, 'The recovery param is more than two bits');
  2032. signature$1 = new signature(signature$1, enc);
  2033. var n = this.n;
  2034. var e = new BN(msg);
  2035. var r = signature$1.r;
  2036. var s = signature$1.s;
  2037. // A set LSB signifies that the y-coordinate is odd
  2038. var isYOdd = j & 1;
  2039. var isSecondKey = j >> 1;
  2040. if (r.cmp(this.curve.p.umod(this.curve.n)) >= 0 && isSecondKey)
  2041. throw new Error('Unable to find sencond key candinate');
  2042. // 1.1. Let x = r + jn.
  2043. if (isSecondKey)
  2044. r = this.curve.pointFromX(r.add(this.curve.n), isYOdd);
  2045. else
  2046. r = this.curve.pointFromX(r, isYOdd);
  2047. var rInv = signature$1.r.invm(n);
  2048. var s1 = n.sub(e).mul(rInv).umod(n);
  2049. var s2 = s.mul(rInv).umod(n);
  2050. // 1.6.1 Compute Q = r^-1 (sR - eG)
  2051. // Q = r^-1 (sR + -eG)
  2052. return this.g.mulAdd(s1, r, s2);
  2053. };
  2054. EC.prototype.getKeyRecoveryParam = function(e, signature$1, Q, enc) {
  2055. signature$1 = new signature(signature$1, enc);
  2056. if (signature$1.recoveryParam !== null)
  2057. return signature$1.recoveryParam;
  2058. for (var i = 0; i < 4; i++) {
  2059. var Qprime;
  2060. try {
  2061. Qprime = this.recoverPubKey(e, signature$1, i);
  2062. } catch (e) {
  2063. continue;
  2064. }
  2065. if (Qprime.eq(Q))
  2066. return i;
  2067. }
  2068. throw new Error('Unable to find valid recovery factor');
  2069. };
  2070. var elliptic_1 = createCommonjsModule(function (module, exports) {
  2071. 'use strict';
  2072. var elliptic = exports;
  2073. elliptic.version = /*RicMoo:ethers*/{ version: "6.5.4" }.version;
  2074. elliptic.utils = utils_1$1;
  2075. elliptic.rand = /*RicMoo:ethers:require(brorand)*/(function() { throw new Error('unsupported'); });
  2076. elliptic.curve = curve_1;
  2077. elliptic.curves = curves_1;
  2078. // Protocols
  2079. elliptic.ec = ec;
  2080. elliptic.eddsa = /*RicMoo:ethers:require(./elliptic/eddsa)*/(null);
  2081. });
  2082. var EC$1 = elliptic_1.ec;
  2083. export { EC$1 as EC };
  2084. //# sourceMappingURL=elliptic.js.map