1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.bigint = bigint;
|
7 | exports.bigStringInt = bigStringInt;
|
8 | exports.dHexDump = dHexDump;
|
9 | exports.bytesToHex = bytesToHex;
|
10 | exports.bytesFromHex = bytesFromHex;
|
11 | exports.bytesToBase64 = bytesToBase64;
|
12 | exports.uint6ToBase64 = uint6ToBase64;
|
13 | exports.bytesCmp = bytesCmp;
|
14 | exports.bytesXor = bytesXor;
|
15 | exports.bytesToWords = bytesToWords;
|
16 | exports.bytesFromWords = bytesFromWords;
|
17 | exports.bytesFromBigInt = bytesFromBigInt;
|
18 | exports.bytesFromLeemonBigInt = bytesFromLeemonBigInt;
|
19 | exports.bytesToArrayBuffer = bytesToArrayBuffer;
|
20 | exports.convertToArrayBuffer = convertToArrayBuffer;
|
21 | exports.convertToUint8Array = convertToUint8Array;
|
22 | exports.convertToByteArray = convertToByteArray;
|
23 | exports.bytesFromArrayBuffer = bytesFromArrayBuffer;
|
24 | exports.bufferConcat = bufferConcat;
|
25 | exports.longToInts = longToInts;
|
26 | exports.longToBytes = longToBytes;
|
27 | exports.longFromInts = longFromInts;
|
28 | exports.intToUint = intToUint;
|
29 | exports.uintToInt = uintToInt;
|
30 | exports.sha1HashSync = sha1HashSync;
|
31 | exports.sha1BytesSync = sha1BytesSync;
|
32 | exports.sha256HashSync = sha256HashSync;
|
33 | exports.rsaEncrypt = rsaEncrypt;
|
34 | exports.addPadding = addPadding;
|
35 | exports.aesEncryptSync = aesEncryptSync;
|
36 | exports.aesDecryptSync = aesDecryptSync;
|
37 | exports.gzipUncompress = gzipUncompress;
|
38 | exports.nextRandomInt = nextRandomInt;
|
39 | exports.pqPrimeFactorization = pqPrimeFactorization;
|
40 | exports.pqPrimeJsbn = pqPrimeJsbn;
|
41 | exports.pqPrimeLeemon = pqPrimeLeemon;
|
42 | exports.bytesModPow = bytesModPow;
|
43 |
|
44 | var _jsbn = require('jsbn');
|
45 |
|
46 | var _rusha = require('rusha');
|
47 |
|
48 | var _rusha2 = _interopRequireDefault(_rusha);
|
49 |
|
50 | var _nodeCryptojsAes = require('@goodmind/node-cryptojs-aes');
|
51 |
|
52 | var CryptoJSlib = _interopRequireWildcard(_nodeCryptojsAes);
|
53 |
|
54 | var _inflate = require('pako/lib/inflate');
|
55 |
|
56 | var _leemon = require('./leemon');
|
57 |
|
58 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
59 |
|
60 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
61 |
|
62 | const { CryptoJS } = CryptoJSlib;
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | const rushaInstance = new _rusha2.default(1024 * 1024);
|
73 |
|
74 | function bigint(num) {
|
75 | return new _jsbn.BigInteger(num.toString(16), 16);
|
76 | }
|
77 |
|
78 | function bigStringInt(strNum) {
|
79 | return new _jsbn.BigInteger(strNum, 10);
|
80 | }
|
81 |
|
82 | function dHexDump(bytes) {
|
83 | const arr = [];
|
84 | for (let i = 0; i < bytes.length; i++) {
|
85 | if (i && !(i % 2)) {
|
86 | if (!(i % 16)) {
|
87 | arr.push('\n');
|
88 | } else if (!(i % 4)) {
|
89 | arr.push(' ');
|
90 | } else {
|
91 | arr.push(' ');
|
92 | }
|
93 | }
|
94 | arr.push((bytes[i] < 16 ? '0' : '') + bytes[i].toString(16));
|
95 | }
|
96 |
|
97 | console.log(arr.join(''));
|
98 | }
|
99 |
|
100 | function bytesToHex(bytes = []) {
|
101 | const arr = [];
|
102 | for (let i = 0; i < bytes.length; i++) {
|
103 | arr.push((bytes[i] < 16 ? '0' : '') + (bytes[i] || 0).toString(16));
|
104 | }
|
105 | return arr.join('');
|
106 | }
|
107 |
|
108 | function bytesFromHex(hexString) {
|
109 | const len = hexString.length;
|
110 | let start = 0;
|
111 | const bytes = [];
|
112 |
|
113 | if (hexString.length % 2) {
|
114 | bytes.push(parseInt(hexString.charAt(0), 16));
|
115 | start++;
|
116 | }
|
117 |
|
118 | for (let i = start; i < len; i += 2) {
|
119 | bytes.push(parseInt(hexString.substr(i, 2), 16));
|
120 | }
|
121 |
|
122 | return bytes;
|
123 | }
|
124 |
|
125 | function bytesToBase64(bytes) {
|
126 | let mod3;
|
127 | let result = '';
|
128 |
|
129 | for (let nLen = bytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
|
130 | mod3 = nIdx % 3;
|
131 | nUint24 |= bytes[nIdx] << (16 >>> mod3 & 24);
|
132 | if (mod3 === 2 || nLen - nIdx === 1) {
|
133 | result += String.fromCharCode(uint6ToBase64(nUint24 >>> 18 & 63), uint6ToBase64(nUint24 >>> 12 & 63), uint6ToBase64(nUint24 >>> 6 & 63), uint6ToBase64(nUint24 & 63));
|
134 | nUint24 = 0;
|
135 | }
|
136 | }
|
137 |
|
138 | return result.replace(/A(?=A$|$)/g, '=');
|
139 | }
|
140 |
|
141 | function uint6ToBase64(nUint6) {
|
142 | return nUint6 < 26 ? nUint6 + 65 : nUint6 < 52 ? nUint6 + 71 : nUint6 < 62 ? nUint6 - 4 : nUint6 === 62 ? 43 : nUint6 === 63 ? 47 : 65;
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | function bytesCmp(bytes1, bytes2) {
|
192 | const len = bytes1.length;
|
193 | if (len !== bytes2.length) {
|
194 | return false;
|
195 | }
|
196 |
|
197 | for (let i = 0; i < len; i++) {
|
198 | if (bytes1[i] !== bytes2[i]) return false;
|
199 | }
|
200 | return true;
|
201 | }
|
202 |
|
203 | function bytesXor(bytes1, bytes2) {
|
204 | const len = bytes1.length;
|
205 | const bytes = [];
|
206 |
|
207 | for (let i = 0; i < len; ++i) {
|
208 | bytes[i] = bytes1[i] ^ bytes2[i];
|
209 | }
|
210 |
|
211 | return bytes;
|
212 | }
|
213 |
|
214 | function bytesToWords(bytes) {
|
215 | if (bytes instanceof ArrayBuffer) {
|
216 | bytes = new Uint8Array(bytes);
|
217 | }
|
218 | const len = bytes.length;
|
219 | const words = [];
|
220 | let i;
|
221 | for (i = 0; i < len; i++) {
|
222 | words[i >>> 2] |= bytes[i] << 24 - i % 4 * 8;
|
223 | }
|
224 |
|
225 | return new CryptoJS.lib.WordArray.init(words, len);
|
226 | }
|
227 |
|
228 | function bytesFromWords(wordArray) {
|
229 | const words = wordArray.words;
|
230 | const sigBytes = wordArray.sigBytes;
|
231 | const bytes = [];
|
232 |
|
233 | for (let i = 0; i < sigBytes; i++) {
|
234 | bytes.push(words[i >>> 2] >>> 24 - i % 4 * 8 & 0xff);
|
235 | }
|
236 |
|
237 | return bytes;
|
238 | }
|
239 |
|
240 | function bytesFromBigInt(bigInt, len) {
|
241 | let bytes = bigInt.toByteArray();
|
242 |
|
243 | if (len && bytes.length < len) {
|
244 | const padding = [];
|
245 | for (let i = 0, needPadding = len - bytes.length; i < needPadding; i++) {
|
246 | padding[i] = 0;
|
247 | }
|
248 | if (bytes instanceof ArrayBuffer) {
|
249 | bytes = bufferConcat(padding, bytes);
|
250 | } else {
|
251 | bytes = padding.concat(bytes);
|
252 | }
|
253 | } else {
|
254 | while (!bytes[0] && (!len || bytes.length > len)) {
|
255 | bytes = bytes.slice(1);
|
256 | }
|
257 | }
|
258 |
|
259 | return bytes;
|
260 | }
|
261 |
|
262 | function bytesFromLeemonBigInt(bigInt, len) {
|
263 | const str = (0, _leemon.bigInt2str)(bigInt, 16);
|
264 | return bytesFromHex(str);
|
265 | }
|
266 |
|
267 | function bytesToArrayBuffer(b) {
|
268 | return new Uint8Array(b).buffer;
|
269 | }
|
270 |
|
271 | function convertToArrayBuffer(bytes) {
|
272 |
|
273 | if (bytes instanceof ArrayBuffer) {
|
274 | return bytes;
|
275 | }
|
276 | if (bytes.buffer !== undefined && bytes.buffer.byteLength == bytes.length * bytes.BYTES_PER_ELEMENT) {
|
277 | return bytes.buffer;
|
278 | }
|
279 | return bytesToArrayBuffer(bytes);
|
280 | }
|
281 |
|
282 | function convertToUint8Array(bytes) {
|
283 | if (bytes.buffer !== undefined) {
|
284 | return bytes;
|
285 | }
|
286 | return new Uint8Array(bytes);
|
287 | }
|
288 |
|
289 | function convertToByteArray(bytes) {
|
290 | if (Array.isArray(bytes)) {
|
291 | return bytes;
|
292 | }
|
293 | bytes = convertToUint8Array(bytes);
|
294 | const newBytes = [];
|
295 | for (let i = 0, len = bytes.length; i < len; i++) {
|
296 | newBytes.push(bytes[i]);
|
297 | }
|
298 | return newBytes;
|
299 | }
|
300 |
|
301 | function bytesFromArrayBuffer(buffer) {
|
302 | const byteView = new Uint8Array(buffer);
|
303 | const bytes = Array.from(byteView);
|
304 | return bytes;
|
305 | }
|
306 |
|
307 | function bufferConcat(buffer1, buffer2) {
|
308 | const l1 = buffer1.byteLength || buffer1.length;
|
309 | const l2 = buffer2.byteLength || buffer2.length;
|
310 | const tmp = new Uint8Array(l1 + l2);
|
311 | tmp.set(buffer1 instanceof ArrayBuffer ? new Uint8Array(buffer1) : buffer1, 0);
|
312 | tmp.set(buffer2 instanceof ArrayBuffer ? new Uint8Array(buffer2) : buffer2, l1);
|
313 |
|
314 | return tmp.buffer;
|
315 | }
|
316 |
|
317 | function longToInts(sLong) {
|
318 | const divRem = bigStringInt(sLong).divideAndRemainder(bigint(0x100000000));
|
319 |
|
320 | return [divRem[0].intValue(), divRem[1].intValue()];
|
321 | }
|
322 |
|
323 | function longToBytes(sLong) {
|
324 | return bytesFromWords({ words: longToInts(sLong), sigBytes: 8 }).reverse();
|
325 | }
|
326 |
|
327 | function longFromInts(high, low) {
|
328 | return bigint(high).shiftLeft(32).add(bigint(low)).toString(10);
|
329 | }
|
330 |
|
331 | function intToUint(val) {
|
332 | val = parseInt(val);
|
333 | if (val < 0) {
|
334 | val = val + 4294967296;
|
335 | }
|
336 | return val;
|
337 | }
|
338 |
|
339 | function uintToInt(val) {
|
340 | if (val > 2147483647) {
|
341 | val = val - 4294967296;
|
342 | }
|
343 | return val;
|
344 | }
|
345 |
|
346 | function sha1HashSync(bytes) {
|
347 |
|
348 | const hashBytes = rushaInstance.rawDigest(bytes).buffer;
|
349 |
|
350 |
|
351 | return hashBytes;
|
352 | }
|
353 |
|
354 | function sha1BytesSync(bytes) {
|
355 | return bytesFromArrayBuffer(sha1HashSync(bytes));
|
356 | }
|
357 |
|
358 | function sha256HashSync(bytes) {
|
359 |
|
360 | const hashWords = CryptoJS.SHA256(bytesToWords(bytes));
|
361 |
|
362 |
|
363 | const hashBytes = bytesFromWords(hashWords);
|
364 |
|
365 | return hashBytes;
|
366 | }
|
367 |
|
368 | function rsaEncrypt(publicKey, bytes) {
|
369 | bytes = addPadding(bytes, 255);
|
370 |
|
371 |
|
372 | const N = new _jsbn.BigInteger(publicKey.modulus, 16);
|
373 | const E = new _jsbn.BigInteger(publicKey.exponent, 16);
|
374 | const X = new _jsbn.BigInteger(bytes);
|
375 | const encryptedBigInt = X.modPowInt(E, N),
|
376 | encryptedBytes = bytesFromBigInt(encryptedBigInt, 256);
|
377 |
|
378 |
|
379 | return encryptedBytes;
|
380 | }
|
381 |
|
382 | function addPadding(bytes, blockSize, zeroes) {
|
383 | blockSize = blockSize || 16;
|
384 | const len = bytes.byteLength || bytes.length;
|
385 | const needPadding = blockSize - len % blockSize;
|
386 | if (needPadding > 0 && needPadding < blockSize) {
|
387 | const padding = new Array(needPadding);
|
388 | if (zeroes) {
|
389 | for (let i = 0; i < needPadding; i++) {
|
390 | padding[i] = 0;
|
391 | }
|
392 | } else {
|
393 | new _jsbn.SecureRandom().nextBytes(padding);
|
394 | }
|
395 |
|
396 | if (bytes instanceof ArrayBuffer) {
|
397 | bytes = bufferConcat(bytes, padding);
|
398 | } else {
|
399 | bytes = bytes.concat(padding);
|
400 | }
|
401 | }
|
402 |
|
403 | return bytes;
|
404 | }
|
405 |
|
406 | function aesEncryptSync(bytes, keyBytes, ivBytes) {
|
407 | const len = bytes.byteLength || bytes.length;
|
408 |
|
409 |
|
410 | bytes = addPadding(bytes);
|
411 |
|
412 | const encryptedWords = CryptoJS.AES.encrypt(bytesToWords(bytes), bytesToWords(keyBytes), {
|
413 | iv: bytesToWords(ivBytes),
|
414 | padding: CryptoJS.pad.NoPadding,
|
415 | mode: CryptoJS.mode.IGE
|
416 | }).ciphertext;
|
417 |
|
418 | const encryptedBytes = bytesFromWords(encryptedWords);
|
419 |
|
420 |
|
421 | return encryptedBytes;
|
422 | }
|
423 |
|
424 | function aesDecryptSync(encryptedBytes, keyBytes, ivBytes) {
|
425 |
|
426 |
|
427 | const decryptedWords = CryptoJS.AES.decrypt({ ciphertext: bytesToWords(encryptedBytes) }, bytesToWords(keyBytes), {
|
428 | iv: bytesToWords(ivBytes),
|
429 | padding: CryptoJS.pad.NoPadding,
|
430 | mode: CryptoJS.mode.IGE
|
431 | });
|
432 |
|
433 | const bytes = bytesFromWords(decryptedWords);
|
434 |
|
435 |
|
436 | return bytes;
|
437 | }
|
438 |
|
439 | function gzipUncompress(bytes) {
|
440 |
|
441 | const result = (0, _inflate.inflate)(bytes);
|
442 |
|
443 | return result;
|
444 | }
|
445 |
|
446 | function nextRandomInt(maxValue) {
|
447 | return Math.floor(Math.random() * maxValue);
|
448 | }
|
449 |
|
450 |
|
451 |
|
452 |
|
453 | function pqPrimeFactorization(pqBytes) {
|
454 | const what = new _jsbn.BigInteger(pqBytes);
|
455 |
|
456 |
|
457 | let result = false;
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 | result = pqPrimeLeemon((0, _leemon.str2bigInt)(what.toString(16), 16, Math.ceil(64 / _leemon.bpe) + 1));
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 | |
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 | return result;
|
495 |
|
496 | }
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 |
|
557 |
|
558 | function pqPrimeJsbn(what) {
|
559 | let it = 0,
|
560 | g;
|
561 | for (let i = 0; i < 3; i++) {
|
562 | const q = (nextRandomInt(128) & 15) + 17;
|
563 | let x = bigint(nextRandomInt(1000000000) + 1);
|
564 | let y = x.clone();
|
565 | const lim = 1 << i + 18;
|
566 |
|
567 | for (let j = 1; j < lim; j++) {
|
568 | ++it;
|
569 | let a = x.clone();
|
570 | let b = x.clone();
|
571 | let c = bigint(q);
|
572 |
|
573 | while (!b.equals(_jsbn.BigInteger.ZERO)) {
|
574 | if (!b.and(_jsbn.BigInteger.ONE).equals(_jsbn.BigInteger.ZERO)) {
|
575 | c = c.add(a);
|
576 | if (c.compareTo(what) > 0) {
|
577 | c = c.subtract(what);
|
578 | }
|
579 | }
|
580 | a = a.add(a);
|
581 | if (a.compareTo(what) > 0) {
|
582 | a = a.subtract(what);
|
583 | }
|
584 | b = b.shiftRight(1);
|
585 | }
|
586 |
|
587 | x = c.clone();
|
588 | const z = x.compareTo(y) < 0 ? y.subtract(x) : x.subtract(y);
|
589 | g = z.gcd(what);
|
590 | if (!g.equals(_jsbn.BigInteger.ONE)) {
|
591 | break;
|
592 | }
|
593 | if ((j & j - 1) == 0) {
|
594 | y = x.clone();
|
595 | }
|
596 | }
|
597 | if (g.compareTo(_jsbn.BigInteger.ONE) > 0) {
|
598 | break;
|
599 | }
|
600 | }
|
601 |
|
602 | let f = what.divide(g),
|
603 | P,
|
604 | Q;
|
605 |
|
606 | if (g.compareTo(f) > 0) {
|
607 | P = f;
|
608 | Q = g;
|
609 | } else {
|
610 | P = g;
|
611 | Q = f;
|
612 | }
|
613 |
|
614 | return [bytesFromBigInt(P), bytesFromBigInt(Q), it];
|
615 | }
|
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 |
|
691 |
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 |
|
753 |
|
754 |
|
755 |
|
756 |
|
757 |
|
758 | function pqPrimeLeemon(what) {
|
759 | const minBits = 64;
|
760 | const minLen = Math.ceil(minBits / _leemon.bpe) + 1;
|
761 | let it = 0;
|
762 | let q, lim;
|
763 | const a = new Array(minLen);
|
764 | const b = new Array(minLen);
|
765 | const c = new Array(minLen);
|
766 | const g = new Array(minLen);
|
767 | const z = new Array(minLen);
|
768 | const x = new Array(minLen);
|
769 | const y = new Array(minLen);
|
770 |
|
771 | for (let i = 0; i < 3; i++) {
|
772 | q = (nextRandomInt(128) & 15) + 17;
|
773 | (0, _leemon.copyInt_)(x, nextRandomInt(1000000000) + 1);
|
774 | (0, _leemon.copy_)(y, x);
|
775 | lim = 1 << i + 18;
|
776 |
|
777 | for (let j = 1; j < lim; j++) {
|
778 | ++it;
|
779 | (0, _leemon.copy_)(a, x);
|
780 | (0, _leemon.copy_)(b, x);
|
781 | (0, _leemon.copyInt_)(c, q);
|
782 |
|
783 | while (!(0, _leemon.isZero)(b)) {
|
784 | if (b[0] & 1) {
|
785 | (0, _leemon.add_)(c, a);
|
786 | if ((0, _leemon.greater)(c, what)) {
|
787 | (0, _leemon.sub_)(c, what);
|
788 | }
|
789 | }
|
790 | (0, _leemon.add_)(a, a);
|
791 | if ((0, _leemon.greater)(a, what)) {
|
792 | (0, _leemon.sub_)(a, what);
|
793 | }
|
794 | (0, _leemon.rightShift_)(b, 1);
|
795 | }
|
796 |
|
797 | (0, _leemon.copy_)(x, c);
|
798 | if ((0, _leemon.greater)(x, y)) {
|
799 | (0, _leemon.copy_)(z, x);
|
800 | (0, _leemon.sub_)(z, y);
|
801 | } else {
|
802 | (0, _leemon.copy_)(z, y);
|
803 | (0, _leemon.sub_)(z, x);
|
804 | }
|
805 | (0, _leemon.eGCD_)(z, what, g, a, b);
|
806 | if (!(0, _leemon.equalsInt)(g, 1)) {
|
807 | break;
|
808 | }
|
809 | if ((j & j - 1) === 0) {
|
810 | (0, _leemon.copy_)(y, x);
|
811 | }
|
812 | }
|
813 | if ((0, _leemon.greater)(g, _leemon.one)) {
|
814 | break;
|
815 | }
|
816 | }
|
817 |
|
818 | (0, _leemon.divide_)(what, g, x, y);
|
819 |
|
820 | const [P, Q] = (0, _leemon.greater)(g, x) ? [x, g] : [g, x];
|
821 |
|
822 |
|
823 |
|
824 | return [bytesFromLeemonBigInt(P), bytesFromLeemonBigInt(Q), it];
|
825 | }
|
826 |
|
827 | function bytesModPow(x, y, m) {
|
828 | try {
|
829 | const xBigInt = (0, _leemon.str2bigInt)(bytesToHex(x), 16);
|
830 | const yBigInt = (0, _leemon.str2bigInt)(bytesToHex(y), 16);
|
831 | const mBigInt = (0, _leemon.str2bigInt)(bytesToHex(m), 16);
|
832 | const resBigInt = (0, _leemon.powMod)(xBigInt, yBigInt, mBigInt);
|
833 |
|
834 | return bytesFromHex((0, _leemon.bigInt2str)(resBigInt, 16));
|
835 | } catch (e) {
|
836 | console.error('mod pow error', e);
|
837 | }
|
838 |
|
839 | return bytesFromBigInt(new _jsbn.BigInteger(x).modPow(new _jsbn.BigInteger(y), new _jsbn.BigInteger(m)), 256);
|
840 | }
|
841 |
|
\ | No newline at end of file |