@waku/rln 0.1.5-9901863.0 → 0.1.5-a824fff.0

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