-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64.ts
2238 lines (2042 loc) · 64.5 KB
/
base64.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Base64 performs several functions:
* Compress a string to base64, Diffie Hellman Merkle key exchange,
* SHA256 hash, and generate high entropy random numbers.
* Generated public Keys are numeric and 300 digits in length (~1000 bit equivelent).
*
* Last Modified: November 26, 2021
* Copyright (C) 2021 Graeme Goodkey github.com/ggoodkey
* All rights reserved
*
* Released free of charge for inclusion in your own applications, as is or as modified by you.
* DISTRIBUTED "AS IS", WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
*
* Converting to Base 64 is based on Vassilis Petroulias' base64v1_0.js
* http://jsbase64.codeplex.com/
*
* Compression is based on Sam Hocevar's lz-string-1.3.3.js
* http://pieroxy.net/blog/pages/lz-string/index.html
*
* Handling big integers is a subset of BigInteger.js by peterolson
* https://www.npmjs.com/package/big-integer/
*
*/
interface Base64ShareObj {
Version: number;
Expires: number | false;
Compressed: boolean;
Data: string;
PublicKey: string;
UserKeys: string;
Signature: string;
}
interface Base64ReadObj {
/**
* "results" if successful, "error" if not
*/
type: "results" | "error";
/**
* an error message if process failed
*/
message: string;
/**
* the shared file content
*/
data: string;
/**
* the hashed message authentication code of the shared file contents
*/
hmac: string;
}
interface Base64Interface {
Version: number;
/**
* generic atob (A to B) function
* decodes (system created, non compressed) base64 encoded text
*/
atob: (str: string) => string;
/**
* generic btoa (B to A) function
* encodes text to utf-8 then to base64 encoding (without compressing it)
*/
btoa: (str: string) => string;
/**
* converts any string to a positive integer of the requiredLength
*/
number_hash: (str: string, requiredLength?: number) => string;
/**
* generate a random number of the "requiredLength" (input a Number from 1-300),
* based on timing, proccessor speed, and any "additionalEntropy" you want to provide (optional)
*/
rand: (requiredLength?: number, additionalEntropy?: any) => string;
/**
* salted SHA256 hash
*/
hash: (message: string, salt?: string) => string;
/**
* generate a hashed message authentication code
*/
hmac: (message: string, key: string) => string;
/**
* compress and convert any text to base64, with our without a key
*/
write: (str: string, key?: string | null | false) => string;
/**
* revert from base64, and decompress
*/
read: (str: string, key?: string | null | false) => string | null;
/**
* same as Base64.write, but does some error checking as well
*/
write_and_verify: (str: string, key?: string) => string;
/**
* creates a Diffie Hellman Merkle public key from any string (usually from a secret password)
*/
createPublicKey: (myPrivateKey: string) => string;
/**
* create a secondary secret key to share with someone if you know their public key.
* You must also give them the secret key's corresponding public key. Similar to a session key
*/
createUserKey: (userPublicKey: string, masterKey: string) => string;
/**
* same as Base64.createUserKey, but needs the user's private key to verify
*/
createUserKey_and_verify: (userPrivateKey: string, masterKey: string) => string;
/**
* read a secret key (userKey) that was shared with you, using your own secret key and the person who shared it
* with you's public key (the mainPublicKey is the public key that corresponds to the secret key)
*/
readUserKey: (myPrivateKey: string, userKey: string, mainPublicKey: string) => string | false;
/**
* share plaintext with someone if you know their public key (as generated by base64.js)
* expires = Number in months till data is no longer valid (optional, can be easily bypassed if source code is known)
*/
share: (str: string, myPrivateKey: string, theirPublicKeys: string[], expires?: number | false) => Base64ShareObj;
/**
* read a shared object
*/
readShared: (obj: Base64ShareObj, myPrivateKey: string) => Base64ReadObj;
}
var Base64: Base64Interface = (function () {
var b64: any = {};
/* eslint-disable */
b64.Version = 1.2;
/*base 64 charectors*/
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
hexAlf = '00,01,02,03,04,05,06,07,08,09,0a,0b,0c,0d,0e,0f,10,11,12,13,14,15,16,17,18,19,1a,1b,1c,1d,1e,1f,20,21,22,23,24,25,26,27,28,29,2a,2b,2c,2d,2e,2f,30,31,32,33,34,35,36,37,38,39,3a,3b,3c,3d,3e,3f,40,41,42,43,44,45,46,47,48,49,4a,4b,4c,4d,4e,4f,50,51,52,53,54,55,56,57,58,59,5a,5b,5c,5d,5e,5f,60,61,62,63,64,65,66,67,68,69,6a,6b,6c,6d,6e,6f,70,71,72,73,74,75,76,77,78,79,7a,7b,7c,7d,7e,7f,80,81,82,83,84,85,86,87,88,89,8a,8b,8c,8d,8e,8f,90,91,92,93,94,95,96,97,98,99,9a,9b,9c,9d,9e,9f,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,ad,ae,af,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,ca,cb,cc,cd,ce,cf,d0,d1,d2,d3,d4,d5,d6,d7,d8,d9,da,db,dc,dd,de,df,e0,e1,e2,e3,e4,e5,e6,e7,e8,e9,ea,eb,ec,ed,ee,ef,f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,fa,fb,fc,fd,fe,ff'.split(','),
//100 digit fast
//prime = "6513516734600035718300327211250928237178281758494417357560086828416863929270451437126021949850746381";
//300 digit
prime = "319705304701141539155720137200974664666792526059405792539680974929469783512821793995613718943171723765238853752439032835985158829038528214925658918372196742089464683960239919950882355844766055365179937610326127675178857306260955550407044463370239890187189750909036833976197804646589380690779463976173";
//644 digit slow
//prime = "1475979915214180235084898622737381736312066145333169775147771216478570297878078949377407337049389289382748507531496480477281264838760259191814463365330269540496961201113430156902396093989090226259326935025281409614983499388222831448598601834318536230923772641390209490231836446899608210795482963763094236630945410832793769905399982457186322944729636418890623372171723742105636440368218459649632948538696905872650486914434637457507280441823676813517852099348660847172579408422316678097670224011990280170474894487426924742108823536808485072502240519452587542875349976558572670229633962575212637477897785501552646522609988869914013540483809865681250419497686697771007";
function toHex(arr: Uint8Array | number[]) {
var hex = '';
for (var i = 0, len = arr.length; i < len; i++) {
hex += hexAlf[arr[i]];
}
return hex;
}
/*compress and convert text to Base64*/
function convertTo(input: string) {
if (input === null) return "";
var output = "",
orig = input;
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i,
value,
context_dictionary: any = {},
context_dictionaryToCreate: any = {},
context_c = "",
context_wc = "",
context_w = "",
context_enlargeIn = 2, // Compensate for the first entry which should not count
context_dictSize = 3,
context_numBits = 2,
context_data_string = "",
context_data_val = 0,
context_data_position = 0,
ii,
f = String.fromCharCode;
for (ii = 0; ii < input.length; ii += 1) {
context_c = input.charAt(ii);
if (!Object.prototype.hasOwnProperty.call(context_dictionary, context_c)) {
context_dictionary[context_c] = context_dictSize++;
context_dictionaryToCreate[context_c] = true;
}
context_wc = context_w + context_c;
if (Object.prototype.hasOwnProperty.call(context_dictionary, context_wc)) {
context_w = context_wc;
} else {
if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate, context_w)) {
if (context_w.charCodeAt(0) < 256) {
for (i = 0; i < context_numBits; i++) {
context_data_val = context_data_val << 1;
if (context_data_position === 15) {
context_data_position = 0;
context_data_string += f(context_data_val);
context_data_val = 0;
} else {
context_data_position++;
}
}
value = context_w.charCodeAt(0);
for (i = 0; i < 8; i++) {
context_data_val = context_data_val << 1 | value & 1;
if (context_data_position === 15) {
context_data_position = 0;
context_data_string += f(context_data_val);
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
} else {
value = 1;
for (i = 0; i < context_numBits; i++) {
context_data_val = context_data_val << 1 | value;
if (context_data_position === 15) {
context_data_position = 0;
context_data_string += f(context_data_val);
context_data_val = 0;
} else {
context_data_position++;
}
value = 0;
}
value = context_w.charCodeAt(0);
for (i = 0; i < 16; i++) {
context_data_val = context_data_val << 1 | value & 1;
if (context_data_position === 15) {
context_data_position = 0;
context_data_string += f(context_data_val);
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn === 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
delete context_dictionaryToCreate[context_w];
} else {
value = context_dictionary[context_w];
for (i = 0; i < context_numBits; i++) {
context_data_val = context_data_val << 1 | value & 1;
if (context_data_position === 15) {
context_data_position = 0;
context_data_string += f(context_data_val);
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn === 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
// Add wc to the dictionary.
context_dictionary[context_wc] = context_dictSize++;
context_w = String(context_c);
}
}
// Output the code for w.
if (context_w !== "") {
if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate, context_w)) {
if (context_w.charCodeAt(0) < 256) {
for (i = 0; i < context_numBits; i++) {
context_data_val = context_data_val << 1;
if (context_data_position === 15) {
context_data_position = 0;
context_data_string += f(context_data_val);
context_data_val = 0;
} else {
context_data_position++;
}
}
value = context_w.charCodeAt(0);
for (i = 0; i < 8; i++) {
context_data_val = context_data_val << 1 | value & 1;
if (context_data_position === 15) {
context_data_position = 0;
context_data_string += f(context_data_val);
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
} else {
value = 1;
for (i = 0; i < context_numBits; i++) {
context_data_val = context_data_val << 1 | value;
if (context_data_position === 15) {
context_data_position = 0;
context_data_string += f(context_data_val);
context_data_val = 0;
} else {
context_data_position++;
}
value = 0;
}
value = context_w.charCodeAt(0);
for (i = 0; i < 16; i++) {
context_data_val = context_data_val << 1 | value & 1;
if (context_data_position === 15) {
context_data_position = 0;
context_data_string += f(context_data_val);
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn === 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
delete context_dictionaryToCreate[context_w];
} else {
value = context_dictionary[context_w];
for (i = 0; i < context_numBits; i++) {
context_data_val = context_data_val << 1 | value & 1;
if (context_data_position === 15) {
context_data_position = 0;
context_data_string += f(context_data_val);
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn === 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
}
// Mark the end of the stream
value = 2;
for (i = 0; i < context_numBits; i++) {
context_data_val = context_data_val << 1 | value & 1;
if (context_data_position === 15) {
context_data_position = 0;
context_data_string += f(context_data_val);
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
// Flush the last char
while (true) {
context_data_val = context_data_val << 1;
if (context_data_position === 15) {
context_data_string += f(context_data_val);
break;
}
else context_data_position++;
}
function toBase64(input: string) {
var i = 0;
while (i < input.length * 2) {
if (i % 2 == 0) {
chr1 = input.charCodeAt(i / 2) >> 8;
chr2 = input.charCodeAt(i / 2) & 255;
if (i / 2 + 1 < input.length)
chr3 = input.charCodeAt(i / 2 + 1) >> 8;
else
chr3 = NaN;
} else {
chr1 = input.charCodeAt((i - 1) / 2) & 255;
if ((i + 1) / 2 < input.length) {
chr2 = input.charCodeAt((i + 1) / 2) >> 8;
chr3 = input.charCodeAt((i + 1) / 2) & 255;
} else
chr2 = chr3 = NaN;
}
i += 3;
enc1 = chr1 >> 2;
enc2 = (chr1 & 3) << 4 | chr2 >> 4;
enc3 = (chr2 & 15) << 2 | chr3 >> 6;
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
charset.charAt(enc1) + charset.charAt(enc2) +
charset.charAt(enc3) + charset.charAt(enc4);
}
return output;
}
//constants represent the state of the data, whether or not
//it has been compressed so that the process can be reversed
var compressed = toBase64("l" + context_data_string);
orig = toBase64("n" + orig);
//only use compressed version if it is indeed smaller,
//as lzstring compression actually lengthens short, or already
//highly compressed strings
if (compressed.length > orig.length) compressed = orig;
return "b" + compressed;
}
/*revert from compressed Base64 text to regular text*/
function revertFrom(input: string) {
if (input === null) {
console.log("Decompression error1: Input is null");
return "";
}
if (/^b/.test(input)) input = input.replace(/^b/, "");
else {
//console.log("Decompression error2: Input is not base64 compressed >>> " + input);
return null;
}
var output = "",
ol = 0,
output_ = 0,
chr1, chr2, chr3,
enc1, enc2, enc3, enc4,
i = 0, f = String.fromCharCode;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = charset.indexOf(input.charAt(i++));
enc2 = charset.indexOf(input.charAt(i++));
enc3 = charset.indexOf(input.charAt(i++));
enc4 = charset.indexOf(input.charAt(i++));
chr1 = enc1 << 2 | enc2 >> 4;
chr2 = (enc2 & 15) << 4 | enc3 >> 2;
chr3 = (enc3 & 3) << 6 | enc4;
if (ol % 2 == 0) {
output_ = chr1 << 8;
if (enc3 !== 64) {
output += f(output_ | chr2);
}
if (enc4 !== 64) {
output_ = chr3 << 8;
}
} else {
output = output + f(output_ | chr1);
if (enc3 !== 64) {
output_ = chr2 << 8;
}
if (enc4 !== 64) {
output += f(output_ | chr3);
}
}
ol += 3;
}
ol = null!; output_ = null!; chr1 = null!; chr2 = null!; chr3 = null!;
enc1 = null!; enc2 = null!; enc3 = null!; enc4 = null!;
//now decompress the data
if (/^l/.test(output)) var compressed = output.replace(/^l/, "");
else return output.replace(/^n/, "");
output = null!;
if (compressed === null) {
console.log("Decompression error2: Reverted from Base64 value is null");
return null;
}
if (compressed === "") {
console.log('Decompression error3: Reverted from Base64 value is "" (Empty string)');
return null;
}
var dictionary: any[] = [0, 1, 2, 3],
enlargeIn = 4,
dictSize = 4,
numBits = 3,
entry = "",
result = "",
w,
bits = 0,
resb,
maxpower = 4,
power = 1,
c: any,
data = { string: compressed, val: compressed.charCodeAt(0), position: 32768, index: 1 };
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position === 0) {
data.position = 32768;
data.val = data.string.charCodeAt(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
switch (bits) {
case 0:
bits = 0;
maxpower = Math.pow(2, 8);
power = 1;
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position === 0) {
data.position = 32768;
data.val = data.string.charCodeAt(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
c = f(bits);
break;
case 1:
bits = 0;
maxpower = Math.pow(2, 16);
power = 1;
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position === 0) {
data.position = 32768;
data.val = data.string.charCodeAt(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
c = f(bits);
break;
case 2:
return "";
}
dictionary[3] = c;
w = result = c;
while (true) {
if (data.index > data.string.length) {
console.log("Decompression error5");
return "";
}
bits = 0;
maxpower = Math.pow(2, numBits);
power = 1;
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position === 0) {
data.position = 32768;
data.val = data.string.charCodeAt(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
switch (c = bits) {
case 0:
bits = 0;
maxpower = Math.pow(2, 8);
power = 1;
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position === 0) {
data.position = 32768;
data.val = data.string.charCodeAt(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
dictionary[dictSize++] = f(bits);
c = dictSize - 1;
enlargeIn--;
break;
case 1:
bits = 0;
maxpower = Math.pow(2, 16);
power = 1;
while (power != maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position === 0) {
data.position = 32768;
data.val = data.string.charCodeAt(data.index++);
}
bits |= (resb > 0 ? 1 : 0) * power;
power <<= 1;
}
dictionary[dictSize++] = f(bits);
c = dictSize - 1;
enlargeIn--;
break;
case 2:
return result;
}
if (enlargeIn === 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
if (dictionary[c]) {
entry = dictionary[c];
} else {
if (c === dictSize && w) {
entry = w + w.charAt(0);
} else {
return null;
}
}
result += entry;
// Add w+entry[0] to the dictionary.
dictionary[dictSize++] = w + entry.charAt(0);
enlargeIn--;
w = entry;
if (enlargeIn === 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
}
}
type BigNumber = number | bigint | string | BigInteger;
interface BigInteger {
/**
* Returns the absolute value of a bigInt.
*/
abs(): BigInteger;
/**
* Performs addition.
*/
add(number: BigNumber): BigInteger;
/**
* Performs a comparison between two numbers. If the numbers are equal, it returns 0.
* If the first number is greater, it returns 1. If the first number is lesser, it returns -1.
*/
compare(number: BigNumber): number;
/**
* Performs a comparison between the absolute value of two numbers.
*/
compareAbs(number: BigNumber): number;
/**
* Alias for the compare method.
*/
compareTo(number: BigNumber): number;
/**
* Performs integer division, disregarding the remainder.
*/
divide(number: BigNumber): BigInteger;
/**
* Performs division and returns an object with two properties: quotient and remainder.
* The sign of the remainder will match the sign of the dividend.
*/
divmod(number: BigNumber): { quotient: BigInteger, remainder: BigInteger };
/**
* Alias for the equals method.
*/
eq(number: BigNumber): boolean;
/**
* Checks if two numbers are equal.
*/
equals(number: BigNumber): boolean;
/**
* Returns true if the number is negative, false otherwise.
* Returns false for 0 and true for -0.
*/
isNegative(): boolean;
/**
* Returns true if the number is odd, false otherwise.
*/
isOdd(): boolean;
/**
* Return true if the number is positive, false otherwise.
* Returns true for 0 and false for -0.
*/
isPositive(): boolean;
/**
* Returns true if the number is 1 or -1, false otherwise.
*/
isUnit(): boolean;
/**
* Return true if the number is 0 or -0, false otherwise.
*/
isZero(): boolean;
/**
* Alias for the subtract method.
*/
minus(number: BigNumber): BigInteger;
/**
* Performs division and returns the remainder, disregarding the quotient.
* The sign of the remainder will match the sign of the dividend.
*/
mod(number: BigNumber): BigInteger;
/**
* Takes the number to the power exp modulo mod.
*/
modPow(exp: BigNumber, mod: BigNumber): BigInteger;
/**
* Performs multiplication.
*/
multiply(number: BigNumber): BigInteger;
/**
* Reverses the sign of the number.
*/
negate(): BigInteger;
/**
* Alias for the notEquals method.
*/
neq(number: BigNumber): boolean;
/**
* Adds one to the number.
*/
next(): BigInteger;
/**
* Alias for the divide method.
*/
over(number: BigNumber): BigInteger;
/**
* Alias for the add method.
*/
plus(number: BigNumber): BigInteger;
/**
* Alias for the mod method.
*/
remainder(number: BigNumber): BigInteger;
/**
* Shifts the number right by n places in its binary representation.
* If a negative number is provided, it will shift left.
*
* Throws an error if number is outside of the range [-9007199254740992, 9007199254740992].
*/
shiftRight(number: BigNumber): BigInteger;
/**
* Squares the number.
*/
square(): BigInteger;
/**
* Performs subtraction.
*/
subtract(number: BigNumber): BigInteger;
/**
* Alias for the multiply method.
*/
times(number: BigNumber): BigInteger;
/**
* Converts a bigInt into a native Javascript number. Loses precision for numbers outside the range.
*/
toJSNumber(): number;
/**
* Converts a bigInt to a string.
*/
toString(radix?: number, alphabet?: string): string;
/**
* Converts a bigInt to a string. This method is called behind the scenes in JSON.stringify.
*/
toJSON(): string;
/**
* Converts a bigInt to a native Javascript number. This override allows you to use native
* arithmetic operators without explicit conversion.
*/
valueOf(): number;
_multiplyBySmall(v): BigInteger;
}
interface BaseArray {
value: number[];
isNegative: boolean;
}
interface BigInt extends BigInteger {
value: number[];
sign: boolean;
isSmall: false;
}
interface SmallInt extends BigInteger {
value: number;
sign: boolean;
isSmall: true;
}
var bigInt = (function (undefined) {
var BASE = 1e7,
LOG_BASE = 7,
MAX_INT = 9007199254740992,
MAX_INT_ARR = smallToArray(MAX_INT),
DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz";
function Integer(v, radix?, alphabet?, caseSensitive?): BigInteger {
if (typeof v === "undefined") return Integer[0];
if (typeof radix !== "undefined") return +radix === 10 && !alphabet ? parseValue(v) : parseBase(v, radix, alphabet, caseSensitive);
return parseValue(v);
}
function BigInteger(this, value, sign) {
this.value = value;
this.sign = sign;
this.isSmall = false;
}
BigInteger.prototype = Object.create(Integer.prototype);
function SmallInteger(this, value) {
this.value = value;
this.sign = value < 0;
this.isSmall = true;
}
SmallInteger.prototype = Object.create(Integer.prototype);
function isPrecise(n) {
return -MAX_INT < n && n < MAX_INT;
}
function smallToArray(n) { // For performance reasons doesn't reference BASE, need to change this function if BASE changes
if (n < 1e7)
return [n];
if (n < 1e14)
return [n % 1e7, Math.floor(n / 1e7)];
return [n % 1e7, Math.floor(n / 1e7) % 1e7, Math.floor(n / 1e14)];
}
function arrayToSmall(arr: number[]): number | number[] { // If BASE changes this function may need to change
trim(arr);
var length = arr.length;
if (length < 4 && compareAbs(arr, MAX_INT_ARR) < 0) {
switch (length) {
case 0: return 0;
case 1: return arr[0];
case 2: return arr[0] + arr[1] * BASE;
default: return arr[0] + (arr[1] + arr[2] * BASE) * BASE;
}
}
return arr;
}
function trim(v) {
var i = v.length;
while (v[--i] === 0);
v.length = i + 1;
}
function createArray(length) { // function shamelessly stolen from Yaffle's library https://github.com/Yaffle/BigInteger
var x = new Array(length);
var i = -1;
while (++i < length) {
x[i] = 0;
}
return x;
}
function truncate(n) {
if (n > 0) return Math.floor(n);
return Math.ceil(n);
}
function add(a, b) { // assumes a and b are arrays with a.length >= b.length
var l_a = a.length,
l_b = b.length,
r = new Array(l_a),
carry = 0,
base = BASE,
sum, i;
for (i = 0; i < l_b; i++) {
sum = a[i] + b[i] + carry;
carry = sum >= base ? 1 : 0;
r[i] = sum - carry * base;
}
while (i < l_a) {
sum = a[i] + carry;
carry = sum === base ? 1 : 0;
r[i++] = sum - carry * base;
}
if (carry > 0) r.push(carry);
return r;
}
function addAny(a, b) {
if (a.length >= b.length) return add(a, b);
return add(b, a);
}
function addSmall(a, carry) { // assumes a is array, carry is number with 0 <= carry < MAX_INT
var l = a.length,
r = new Array(l),
base = BASE,
sum, i;
for (i = 0; i < l; i++) {
sum = a[i] - base + carry;
carry = Math.floor(sum / base);
r[i] = sum - carry * base;
carry += 1;
}
while (carry > 0) {
r[i++] = carry % base;
carry = Math.floor(carry / base);
}
return r;
}
BigInteger.prototype.add = function (v) {
var n = parseValue(v);
if (this.sign !== n.sign) {
return this.subtract(n.negate());
}
var a = this.value, b = n.value;
if (n.isSmall) {
return new BigInteger(addSmall(a, Math.abs(b as number)), this.sign);
}
return new BigInteger(addAny(a, b), this.sign);
};
BigInteger.prototype.plus = BigInteger.prototype.add;
SmallInteger.prototype.add = function (v) {
var n = parseValue(v);
var a = this.value;
if (a < 0 !== n.sign) {
return this.subtract(n.negate());
}
var b = n.value;
if (n.isSmall) {
if (isPrecise(a + b)) return new SmallInteger(a + b);
b = smallToArray(Math.abs(b as number));
}
return new BigInteger(addSmall(b, Math.abs(a)), a < 0);
};
SmallInteger.prototype.plus = SmallInteger.prototype.add;
function subtract(a, b) { // assumes a and b are arrays with a >= b
var a_l = a.length,
b_l = b.length,
r = new Array(a_l),
borrow = 0,
base = BASE,
i, difference;
for (i = 0; i < b_l; i++) {
difference = a[i] - borrow - b[i];
if (difference < 0) {
difference += base;
borrow = 1;
} else borrow = 0;
r[i] = difference;
}
for (i = b_l; i < a_l; i++) {
difference = a[i] - borrow;
if (difference < 0) difference += base;
else {
r[i++] = difference;
break;
}
r[i] = difference;
}