UNPKG

21.3 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.bigint = bigint;
7exports.bigStringInt = bigStringInt;
8exports.dHexDump = dHexDump;
9exports.bytesToHex = bytesToHex;
10exports.bytesFromHex = bytesFromHex;
11exports.bytesToBase64 = bytesToBase64;
12exports.uint6ToBase64 = uint6ToBase64;
13exports.bytesCmp = bytesCmp;
14exports.bytesXor = bytesXor;
15exports.bytesToWords = bytesToWords;
16exports.bytesFromWords = bytesFromWords;
17exports.bytesFromBigInt = bytesFromBigInt;
18exports.bytesFromLeemonBigInt = bytesFromLeemonBigInt;
19exports.bytesToArrayBuffer = bytesToArrayBuffer;
20exports.convertToArrayBuffer = convertToArrayBuffer;
21exports.convertToUint8Array = convertToUint8Array;
22exports.convertToByteArray = convertToByteArray;
23exports.bytesFromArrayBuffer = bytesFromArrayBuffer;
24exports.bufferConcat = bufferConcat;
25exports.longToInts = longToInts;
26exports.longToBytes = longToBytes;
27exports.longFromInts = longFromInts;
28exports.intToUint = intToUint;
29exports.uintToInt = uintToInt;
30exports.sha1HashSync = sha1HashSync;
31exports.sha1BytesSync = sha1BytesSync;
32exports.sha256HashSync = sha256HashSync;
33exports.rsaEncrypt = rsaEncrypt;
34exports.addPadding = addPadding;
35exports.aesEncryptSync = aesEncryptSync;
36exports.aesDecryptSync = aesDecryptSync;
37exports.gzipUncompress = gzipUncompress;
38exports.nextRandomInt = nextRandomInt;
39exports.pqPrimeFactorization = pqPrimeFactorization;
40exports.pqPrimeJsbn = pqPrimeJsbn;
41exports.pqPrimeLeemon = pqPrimeLeemon;
42exports.bytesModPow = bytesModPow;
43
44var _jsbn = require('jsbn');
45
46var _rusha = require('rusha');
47
48var _rusha2 = _interopRequireDefault(_rusha);
49
50var _nodeCryptojsAes = require('@goodmind/node-cryptojs-aes');
51
52var CryptoJSlib = _interopRequireWildcard(_nodeCryptojsAes);
53
54var _inflate = require('pako/lib/inflate');
55
56var _leemon = require('./leemon');
57
58function _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
60function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
61
62const { CryptoJS } = CryptoJSlib;
63
64// import Int from 'big-integer'
65
66// import BN from 'bn.js'
67
68// import { bigInt2str } from 'BigInt'
69
70// const { BigInteger } = jsbn
71
72const rushaInstance = new _rusha2.default(1024 * 1024);
73
74function bigint(num) {
75 return new _jsbn.BigInteger(num.toString(16), 16);
76}
77
78function bigStringInt(strNum) {
79 return new _jsbn.BigInteger(strNum, 10);
80}
81
82function 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
100function 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
108function 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
125function 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
141function 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// export function base64ToBlob(base64str, mimeType) {
146// const sliceSize = 1024
147// const byteCharacters = atob(base64str)
148// const bytesLength = byteCharacters.length
149// const slicesCount = Math.ceil(bytesLength / sliceSize)
150// const byteArrays = new Array(slicesCount)
151
152// for (let sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
153// const begin = sliceIndex * sliceSize
154// const end = Math.min(begin + sliceSize, bytesLength)
155
156// const bytes = new Array(end - begin)
157// for (let offset = begin, i = 0; offset < end; ++i, ++offset) {
158// bytes[i] = byteCharacters[offset].charCodeAt(0)
159// }
160// byteArrays[sliceIndex] = new Uint8Array(bytes)
161// }
162
163// return blobConstruct(byteArrays, mimeType)
164// }
165
166// export function dataUrlToBlob(url) {
167// // var name = 'b64blob ' + url.length
168// // console.time(name)
169// const urlParts = url.split(',')
170// const base64str = urlParts[1]
171// const mimeType = urlParts[0].split(':')[1].split(';')[0]
172// const blob = base64ToBlob(base64str, mimeType)
173// // console.timeEnd(name)
174// return blob
175// }
176
177// export function blobConstruct(blobParts, mimeType) {
178// let blob
179// try {
180// blob = new Blob(blobParts, { type: mimeType })
181// } catch (e) {
182// const bb = new BlobBuilder
183// angular.forEach(blobParts, function(blobPart) {
184// bb.append(blobPart)
185// })
186// blob = bb.getBlob(mimeType)
187// }
188// return blob
189// }
190
191function 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
203function 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
214function 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
228function 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
240function 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
262function bytesFromLeemonBigInt(bigInt, len) {
263 const str = (0, _leemon.bigInt2str)(bigInt, 16);
264 return bytesFromHex(str);
265}
266
267function bytesToArrayBuffer(b) {
268 return new Uint8Array(b).buffer;
269}
270
271function convertToArrayBuffer(bytes) {
272 // Be careful with converting subarrays!!
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
282function convertToUint8Array(bytes) {
283 if (bytes.buffer !== undefined) {
284 return bytes;
285 }
286 return new Uint8Array(bytes);
287}
288
289function 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
301function bytesFromArrayBuffer(buffer) {
302 const byteView = new Uint8Array(buffer);
303 const bytes = Array.from(byteView);
304 return bytes;
305}
306
307function 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
317function longToInts(sLong) {
318 const divRem = bigStringInt(sLong).divideAndRemainder(bigint(0x100000000));
319
320 return [divRem[0].intValue(), divRem[1].intValue()];
321}
322
323function longToBytes(sLong) {
324 return bytesFromWords({ words: longToInts(sLong), sigBytes: 8 }).reverse();
325}
326
327function longFromInts(high, low) {
328 return bigint(high).shiftLeft(32).add(bigint(low)).toString(10);
329}
330
331function intToUint(val) {
332 val = parseInt(val);
333 if (val < 0) {
334 val = val + 4294967296;
335 }
336 return val;
337}
338
339function uintToInt(val) {
340 if (val > 2147483647) {
341 val = val - 4294967296;
342 }
343 return val;
344}
345
346function sha1HashSync(bytes) {
347 // console.log(dT(), 'SHA-1 hash start', bytes.byteLength || bytes.length)
348 const hashBytes = rushaInstance.rawDigest(bytes).buffer;
349 // console.log(dT(), 'SHA-1 hash finish')
350
351 return hashBytes;
352}
353
354function sha1BytesSync(bytes) {
355 return bytesFromArrayBuffer(sha1HashSync(bytes));
356}
357
358function sha256HashSync(bytes) {
359 // console.log(dT(), 'SHA-2 hash start', bytes.byteLength || bytes.length)
360 const hashWords = CryptoJS.SHA256(bytesToWords(bytes));
361 // console.log(dT(), 'SHA-2 hash finish')
362
363 const hashBytes = bytesFromWords(hashWords);
364
365 return hashBytes;
366}
367
368function rsaEncrypt(publicKey, bytes) {
369 bytes = addPadding(bytes, 255);
370
371 // console.log('RSA encrypt start')
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 // console.log('RSA encrypt finish')
378
379 return encryptedBytes;
380}
381
382function 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
406function aesEncryptSync(bytes, keyBytes, ivBytes) {
407 const len = bytes.byteLength || bytes.length;
408
409 // console.log(dT(), 'AES encrypt start', len/*, bytesToHex(keyBytes), bytesToHex(ivBytes)*/)
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 // console.log(dT(), 'AES encrypt finish')
420
421 return encryptedBytes;
422}
423
424function aesDecryptSync(encryptedBytes, keyBytes, ivBytes) {
425
426 // console.log(dT(), 'AES decrypt start', encryptedBytes.length)
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 // console.log(dT(), 'AES decrypt finish')
435
436 return bytes;
437}
438
439function gzipUncompress(bytes) {
440 // console.log('Gzip uncompress start')
441 const result = (0, _inflate.inflate)(bytes);
442 // console.log('Gzip uncompress finish')
443 return result;
444}
445
446function nextRandomInt(maxValue) {
447 return Math.floor(Math.random() * maxValue);
448}
449
450// const bytesToInt = bytes => Int(bytesToHex(bytes), 16)
451// const bytesFromInt = int => bytesFromHex(int.toString(16))
452
453function pqPrimeFactorization(pqBytes) {
454 const what = new _jsbn.BigInteger(pqBytes);
455 // const whatInt = bytesToInt(pqBytes)
456 // const whatBn = new BN(bytesToHex(pqBytes), 16)
457 let result = false;
458 // let intRes = []
459 // const bnRes = []
460 // console.log(dT(), 'PQ start', pqBytes, what.toString(16), what.bitLength())
461
462 // try {
463 // console.time('pq leemon')
464 // const toHex = bytesToHex(pqBytes)
465 result = pqPrimeLeemon((0, _leemon.str2bigInt)(what.toString(16), 16, Math.ceil(64 / _leemon.bpe) + 1));
466 // console.timeEnd('pq leemon')
467 // } catch (e) {
468 // console.error('Pq leemon Exception', e)
469 // }
470
471 /*if (result === false && what.bitLength() <= 64) {
472 // console.time('PQ long')
473 try {
474 result = pqPrimeLong(goog.math.Long.fromString(what.toString(16), 16))
475 } catch (e) {
476 console.error('Pq long Exception', e)
477 }
478 // console.timeEnd('PQ long')
479 }*/
480 // console.log(result)
481
482 // if (result === false) {
483 // console.time('pq BigInt')
484 // intRes = pqPrimeJsbn(what)
485 // console.timeEnd('pq BigInt')
486
487 // console.time('pq bn')
488 // bnRes = pqPrimeBN(whatBn)
489 // console.timeEnd('pq bn')
490 // }
491 // console.log(...result, ...bnRes)
492 // console.log(dT(), 'PQ finish')
493
494 return result;
495 //intRes//result//bnRes
496}
497
498/*export function pqPrimeBN(what) {
499 let it = 0,
500 g
501 const nOne = new BN(1)
502 for (let i = 0; i < 3; i++) {
503 const q = (nextRandomInt(128) & 15) + 17
504 let x = new BN(nextRandomInt(1000000000) + 1)
505 let y = x.clone()
506 const lim = 1 << (i + 18)
507
508 for (let j = 1; j < lim; j++) {
509 ++it
510 let a = x.clone()
511 let b = x.clone()
512 let c = new BN(q)
513
514 while (!b.isZero()) {
515 if (!b.and(nOne).isZero()) {
516 c = c.add(a)
517 if (c.gt(what)) {
518 c = c.sub(what)
519 }
520 }
521 a = a.add(a)
522 if (a.gt(what)) {
523 a = a.sub(what)
524 }
525 b = b.shrn(1)
526 }
527
528 x = c.clone()
529 const z = x.lt(y)
530 ? y.sub(x)
531 : x.sub(y)
532 g = z.gcd(what)
533 if (!g.eq(nOne)) {
534 break
535 }
536 if ((j & (j - 1)) == 0) {
537 y = x.clone()
538 }
539 }
540 if (g.gt(nOne)) {
541 break
542 }
543 }
544
545 let f = what.div(g), P, Q
546
547 if (g.gt(f)) {
548 P = f
549 Q = g
550 } else {
551 P = g
552 Q = f
553 }
554
555 return [P.toArray(), Q.toArray(), it]
556}*/
557
558function 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/*export function pqPrimeBigInteger(what) {
618 let it = 0,
619 g
620 for (let i = 0; i < 3; i++) {
621 const q = (nextRandomInt(128) & 15) + 17
622 let x = Int(nextRandomInt(1000000000) + 1)
623 let y = Int(x)
624 const lim = 1 << (i + 18)
625
626 for (let j = 1; j < lim; j++) {
627 ++it
628 let a = Int(x)
629 let b = Int(x)
630 let c = Int(q)
631
632 while (!b.isZero()) {
633 if (!b.and(Int.one).isZero()) {
634 c = c.add(a)
635 if (c.greater(what))
636 c = c.subtract(what)
637 }
638 a = a.add(a)
639 if (a.greater(what))
640 a = a.subtract(what)
641 b = b.shiftRight(1)
642 }
643
644 x = Int(c)
645 const z = x.lesser(y)
646 ? y.subtract(x)
647 : x.subtract(y)
648 g = Int.gcd(z, what)
649 if (g.notEquals(Int.one))
650 break
651 if ((j & (j - 1)) == 0)
652 y = Int(x)
653 }
654 if (g.greater(Int.one))
655 break
656 }
657
658 const f = what.divide(g)
659 let P, Q
660
661 if (g.greater(f)) {
662 P = f
663 Q = g
664 } else {
665 P = g
666 Q = f
667 }
668
669 return [bytesFromInt(P), bytesFromInt(Q), it]
670}*/
671
672/*export function gcdLong(a, b) {
673 while (a.notEquals(goog.math.Long.ZERO) && b.notEquals(goog.math.Long.ZERO)) {
674 while (b.and(goog.math.Long.ONE).equals(goog.math.Long.ZERO)) {
675 b = b.shiftRight(1)
676 }
677 while (a.and(goog.math.Long.ONE).equals(goog.math.Long.ZERO)) {
678 a = a.shiftRight(1)
679 }
680 if (a.compare(b) > 0) {
681 a = a.subtract(b)
682 } else {
683 b = b.subtract(a)
684 }
685 }
686 return b.equals(goog.math.Long.ZERO) ? a : b
687}
688
689export function pqPrimeLong(what) {
690 let it = 0,
691 g
692 for (let i = 0; i < 3; i++) {
693 const q = goog.math.Long.fromInt((nextRandomInt(128) & 15) + 17)
694 let x = goog.math.Long.fromInt(nextRandomInt(1000000000) + 1)
695 let y = x
696 const lim = 1 << (i + 18)
697
698 for (let j = 1; j < lim; j++) {
699 ++it
700 let a = x
701 let b = x
702 let c = q
703
704 while (b.notEquals(goog.math.Long.ZERO)) {
705 if (b.and(goog.math.Long.ONE).notEquals(goog.math.Long.ZERO)) {
706 c = c.add(a)
707 if (c.compare(what) > 0) {
708 c = c.subtract(what)
709 }
710 }
711 a = a.add(a)
712 if (a.compare(what) > 0) {
713 a = a.subtract(what)
714 }
715 b = b.shiftRight(1)
716 }
717
718 x = c
719 const z = x.compare(y) < 0 ? y.subtract(x) : x.subtract(y)
720 g = gcdLong(z, what)
721 if (g.notEquals(goog.math.Long.ONE)) {
722 break
723 }
724 if ((j & (j - 1)) == 0) {
725 y = x
726 }
727 }
728 if (g.compare(goog.math.Long.ONE) > 0) {
729 break
730 }
731 }
732
733 let f = what.div(g), P, Q
734
735 if (g.compare(f) > 0) {
736 P = f
737 Q = g
738 } else {
739 P = g
740 Q = f
741 }
742
743 return [bytesFromHex(P.toString(16)), bytesFromHex(Q.toString(16)), it]
744}*/
745
746/*//is bigint x equal to integer y?
747//y must have less than bpe bits
748function equalsInt(x,y) {
749 var i;
750 if (x[0]!=y)
751 return 0;
752 for (i=1;i<x.length;i++)
753 if (x[i])
754 return 0;
755 return 1;
756}*/
757
758function 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 // console.log(dT(), 'done', bigInt2str(what, 10), bigInt2str(P, 10), bigInt2str(Q, 10))
823
824 return [bytesFromLeemonBigInt(P), bytesFromLeemonBigInt(Q), it];
825}
826
827function 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//# sourceMappingURL=bin.js.map
\No newline at end of file