-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbigint.lua
More file actions
1458 lines (1298 loc) · 35.9 KB
/
bigint.lua
File metadata and controls
1458 lines (1298 loc) · 35.9 KB
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
local function bigint_module()
local bigint = {};
--##### FORWARD DECLARATIONS #####--
local bigint_mt;
local bigint_digits;
local bigint_comparatorMap;
local bigint_rstrip;
local bigint_ensureBigInt;
local bigint_ensureInt;
local bigint_ensureString;
local bigint_ensureBool;
local bigint_ensureArray;
local table_reverse;
local table_copy;
--##### MODULE FUNCTIONS #####--
local math_floor = math.floor;
local math_ceil = math.ceil;
local table_concat = table.concat;
local string_sub = string.sub;
local string_byte = string.byte;
local string_char = string.char;
local string_format = string.format;
local string_dump = string.dump;
local unpack = unpack or table.unpack;
local getmetatable = getmetatable;
local setmetatable = setmetatable;
local tonumber = tonumber;
local type = type;
local loadstring = loadstring or load;
--##### CONSTRUCTORS #####--
function bigint.New()
local self = {
sign = 0,
bytes = {},
mutable = false
};
self = setmetatable(self, bigint_mt);
return self;
end
function bigint.Construct(value, base)
local valueType = type(value);
if valueType == "string" then
return bigint.FromString(value, base);
elseif valueType == "number" then
return bigint.FromNumber(value);
elseif valueType == "table" then
if bigint.IsBigInt(value) then
return value;
else
return bigint.FromArray(value);
end
else
error("cannot construct bigint from type: " .. type(value));
end
end
-- parse integer from a string
function bigint.FromString(value, base)
value = bigint_ensureString(value);
local self = bigint.New();
self.sign = 1;
local digitsStart = 1;
local digitsEnd = #value;
if string_byte(value, digitsStart) == 0x2d then -- "-"
self.sign = -1;
digitsStart = digitsStart + 1;
end
if string_byte(value, digitsStart) == 0x30 then -- "0"
digitsStart = digitsStart + 1;
local prefix = string_byte(value, digitsStart);
if (base == nil or base == 16) and prefix == 0x78 then -- "x"
base = 16;
digitsStart = digitsStart + 1;
elseif (base == nil or base == 2) and prefix == 0x62 then -- "b"
base = 2;
digitsStart = digitsStart + 1;
end
end
while string_byte(value, digitsStart) == 0x30 do -- "0"
digitsStart = digitsStart + 1;
end
if digitsStart > digitsEnd then
self.sign = 0;
return self;
end
base = bigint_ensureInt(base, 2, 36, 10);
if base == 2 or base == 16 then
-- fast bin/hex parser
local width = 8;
if base == 16 then
width = 2;
end
local i = 1;
for j = digitsEnd, digitsStart, -width do
if j - width + 1 <= digitsStart then
self.bytes[i] = tonumber(string_sub(value, digitsStart, j), base);
else
self.bytes[i] = tonumber(string_sub(value, j - width + 1, j), base);
end
i = i + 1;
end
return self;
else
-- general parser
local carry = 0;
for i = digitsStart, digitsEnd, 1 do
-- multiply by base
local carry = 0;
local j = 1;
while self.bytes[j] ~= nil or carry ~= 0 do
local product = (self.bytes[j] or 0) * base + carry;
self.bytes[j] = product % 256;
carry = math_floor(product / 256);
j = j + 1;
end
-- add digit
j = 1;
carry = tonumber(string_sub(value, i, i), base);
while carry ~= 0 do
local sum = (self.bytes[j] or 0) + carry;
self.bytes[j] = sum % 256;
carry = math_floor(sum / 256);
j = j + 1;
end
end
return self;
end
end
function bigint.FromNumber(value)
value = bigint_ensureInt(value);
local self = bigint.New();
if value < 0 then
value = -value;
self.sign = -1;
elseif value > 0 then
self.sign = 1;
end
local i = 1;
while value > 0 do
self.bytes[i] = value % 256;
i = i + 1;
value = math_floor(value / 256);
end
return self;
end
function bigint.FromArray(array, littleEndian)
array = bigint_ensureArray(array);
littleEndian = bigint_ensureBool(littleEndian, false);
local self = bigint.New();
self.bytes = table_copy(array);
if not littleEndian then
table_reverse(self.bytes);
end
self.sign = 1;
bigint_rstrip(self);
return self;
end
function bigint.FromBytes(bytes, littleEndian)
bytes = bigint_ensureString(bytes);
littleEndian = bigint_ensureBool(littleEndian, false);
local self = bigint.New();
self.bytes = {string_byte(bytes, 1, #bytes)};
if not littleEndian then
table_reverse(self.bytes);
end
self.sign = 1;
bigint_rstrip(self);
return self;
end
function bigint.IsBigInt(obj)
return getmetatable(obj) == bigint_mt;
end
--##### ARITHMETIC OPERATORS #####--
function bigint:Unm()
if self.sign == 0 then
return self;
else
local this = self:CopyIfImmutable();
this.sign = -this.sign;
return this;
end
end
function bigint:Abs()
if self.sign >= 0 then
return self;
else
local this = self:CopyIfImmutable();
this.sign = 1;
return this;
end
end
function bigint:SetSign(sign)
sign = bigint_ensureInt(sign, -1, 1);
if self.sign == 0 or sign == self.sign then
return self;
elseif sign == 0 then
return bigint.Zero;
elseif sign == -1 or sign == 1 then
local this = self:CopyIfImmutable();
this.sign = sign;
return this;
end
error("invalid sign");
end
function bigint:Add(other)
other = bigint_ensureBigInt(other);
-- addition of 0
if other.sign == 0 then
return self;
elseif self.sign == 0 then
return other;
end
-- determine sign, operation, and order of operands
local subtract = false;
local swapOrder = false;
local changeSign = false;
if self.sign == other.sign then
if #self.bytes < #other.bytes then
swapOrder = true;
end
else
local ucomp = self:CompareU(other);
if ucomp == 0 then
return bigint.Zero;
end
subtract = true;
if ucomp == -1 then
swapOrder = true;
changeSign = true;
end
end
-- perform operation
local this = self:CopyIfImmutable();
local byteCount;
local otherByteCount;
local bytes;
local otherBytes;
if swapOrder then
bytes = other.bytes;
otherBytes = self.bytes;
else
bytes = self.bytes;
otherBytes = other.bytes;
end
byteCount = #bytes;
otherByteCount = #otherBytes;
local carry = 0;
for i = 1, byteCount, 1 do
local byte = bytes[i];
local otherByte = otherBytes[i] or 0;
if subtract then
otherByte = -otherByte;
end
local sum = byte + otherByte + carry;
if not subtract and sum >= 256 then
this.bytes[i] = sum - 256;
carry = 1;
elseif subtract and sum < 0 then
this.bytes[i] = sum + 256;
carry = -1;
else
this.bytes[i] = sum;
carry = 0;
end
-- end loop as soon as possible
if i >= otherByteCount and carry == 0 then
if swapOrder then
-- just need to copy remaining bytes
for j = i + 1, byteCount, 1 do
this.bytes[j] = bytes[j];
end
end
break;
end
end
if carry > 0 then
this.bytes[byteCount + 1] = carry
end
if subtract then
bigint_rstrip(this);
end
if changeSign then
this.sign = -this.sign;
end
return this;
end
function bigint:Sub(other)
other = bigint_ensureBigInt(other);
return self:Add(other:Unm());
end
function bigint:Mul(other)
other = bigint_ensureBigInt(other);
-- multiplication by 0
if self.sign == 0 or other.sign == 0 then
return bigint.Zero;
end
-- multiplication by 1
if self:IsOne() then
if self.sign == -1 then
return other:Unm();
else
return other;
end
end
if other:IsOne() then
if other.sign == -1 then
return self:Unm();
else
return self;
end
end
-- general multiplication
local carry = 0;
local this = self:CopyIfImmutable();
local bytes = this.bytes;
local otherBytes = other.bytes;
local byteCount = #bytes;
local otherByteCount = #otherBytes;
if otherByteCount > byteCount then
-- swap order so that number with more bytes comes first
bytes = other.bytes;
otherBytes = this.bytes;
local tmp = byteCount;
byteCount = otherByteCount;
otherByteCount = tmp;
end
local shift = 0;
local result = {};
local carry = 0;
for i = 1, otherByteCount, 1 do
if otherBytes[i] == 0 then
if result[i] == nil then
result[i] = 0;
end
else
-- multiply each byte
local j = 1;
while j <= byteCount do
local resultIdx = i + j - 1;
local product = bytes[j] * otherBytes[i] + carry + (result[resultIdx] or 0);
-- add product to result
result[resultIdx] = product % 256;
carry = math_floor(product / 256);
j = j + 1;
end
-- finish adding carry
while carry ~= 0 do
local resultIdx = i + j - 1;
local sum = (result[resultIdx] or 0) + carry;
result[resultIdx] = sum % 256;
carry = math_floor(sum / 256);
j = j + 1;
end
end
end
table_copy(this.bytes, result);
this.sign = this.sign * other.sign;
return this;
end
function bigint:DivWithRemainder(other, ignoreRemainder)
other = bigint_ensureBigInt(other);
ignoreRemainder = bigint_ensureBool(ignoreRemainder, false);
-- division of/by 0
if self.sign == 0 then
return self, self;
elseif other.sign == 0 then
return other, other;
end
-- division by 1
if other:IsOne() then
if other.sign == -1 then
return self:Unm(), bigint.Zero;
else
return self, bigint.Zero;
end
end
-- division by bigger number or self
local ucomp = self:CompareU(other);
if ucomp == -1 then
if self.sign == other.sign then
return bigint.Zero, self;
else
if ignoreRemainder then
return bigint.Zero, self;
else
return bigint.Zero, self:Add(other);
end
end
elseif ucomp == 0 then
if self.sign == other.sign then
return bigint.One, bigint.Zero;
else
return bigint.NegOne, bigint.Zero;
end
end
-- general division
local this = self:CopyIfImmutable();
local another = other:CopyIfImmutable();
local byteCount = #this.bytes;
local otherByteCount = #another.bytes;
local resultIdx = 1;
local result = {};
local divIdx = byteCount - otherByteCount + 1;
while divIdx >= 1 do
local factor = 0;
repeat
-- check if divisor is smaller
local foundFactor = false;
local sliceSize = otherByteCount;
if divIdx + sliceSize <= byteCount and this.bytes[divIdx + sliceSize] ~= 0 then
sliceSize = sliceSize + 1;
end
for i = sliceSize, 1, -1 do
local byte = this.bytes[divIdx + i - 1] or 0;
local otherByte = another.bytes[i] or 0;
if otherByte < byte then
foundFactor = false;
break;
elseif otherByte > byte then
foundFactor = true;
break;
end
end
-- subtract divisor
if not foundFactor then
factor = factor + 1;
local carry = 0;
local i = 1;
while i <= sliceSize or carry ~= 0 do
local j = divIdx + i - 1;
local diff = (this.bytes[j] or 0) - (another.bytes[i] or 0) + carry;
if diff < 0 then
carry = -1;
diff = diff + 256;
else
carry = 0;
end
this.bytes[j] = diff;
i = i + 1;
end
end
until foundFactor;
-- set digit
result[resultIdx] = factor;
resultIdx = resultIdx + 1;
divIdx = divIdx - 1;
end
local sign = this.sign;
local otherSign = another.sign;
local divSign = sign * otherSign;
bigint_rstrip(this);
-- if remainder is negative, add divisor to make it positive
if not ignoreRemainder then
if sign == -otherSign and this.sign ~= 0 then
this = this:Add(another);
end
end
table_reverse(result);
table_copy(another.bytes, result);
another.sign = divSign;
bigint_rstrip(another);
if this.sign ~= 0 and otherSign == -1 then
this.sign = -1;
end
return another, this;
end
function bigint:Div(other)
local quotient, remainder = self:DivWithRemainder(other, true);
return quotient;
end
function bigint:Mod(other)
local quotient, remainder = self:DivWithRemainder(other);
return remainder;
end
function bigint:Pow(other)
other = bigint_ensureBigInt(other);
if other.sign == 0 then
return bigint.One;
elseif other.sign == -1 then
return bigint.Zero;
elseif other:IsOne() then
return self;
end
local sign = self.sign;
if sign == -1 and other:IsEven() then
sign = 1;
end
-- fast exponent if self is a power of 2
local power = self:ExactLog2();
if power ~= nil then
-- assumes other isn't so big that precision becomes an issue
local shift = (other:ToNumber() - 1) * power;
return self:Shl(shift):SetSign(sign);
end
-- multiply by self repeatedly
local this = self:Copy();
this.mutable = true;
local another = other:CopyIfImmutable():Abs():Add(bigint.NegOne);
local otherMutable = another.mutable;
another.mutable = true;
while another.sign ~= 0 do
this = this:Mul(self);
another = another:Add(bigint.NegOne);
end
this.mutable = false;
another.mutable = otherMutable;
this.sign = sign;
return this;
end
-- calculate log2 by finding highest 1 bit
function bigint:Log2()
if self.sign == 0 then
return nil;
end
local byteCount = #self.bytes;
local byte = self.bytes[byteCount];
local bitNum = (byteCount - 1) * 8;
while byte >= 1 do
bitNum = bitNum + 1;
byte = byte / 2;
end
return bigint.FromNumber(bitNum - 1);
end
-- return log2 if it's an integer, else nil
function bigint:ExactLog2()
if self.sign == 0 then
return nil;
end
local i = 1;
local power = 0;
local foundOne = false;
while self.bytes[i] ~= nil do
local byte = self.bytes[i];
for _ = 1, 8, 1 do
if byte % 2 < 1 then
if not foundOne then
power = power + 1;
end
else
if foundOne then
return nil;
end
foundOne = true;
end
byte = byte / 2;
end
i = i + 1;
end
return power;
end
--##### BITWISE OPERATORS #####--
function bigint:Shr(n)
n = bigint_ensureInt(n);
if self.sign == 0 or n == 0 then
return self;
end
if n < 0 then
return self:Shl(-n);
end
-- shift whole bytes
local shiftBytes = math_floor(n / 8);
local byteCount = #self.bytes;
if shiftBytes >= byteCount then
return bigint.Zero;
end
local this = self:CopyIfImmutable();
for i = shiftBytes + 1, byteCount, 1 do
this.bytes[i - shiftBytes] = this.bytes[i];
end
for i = byteCount - shiftBytes + 1, byteCount, 1 do
this.bytes[i] = nil;
end
byteCount = byteCount - shiftBytes;
-- shift bits
local shiftBits = n % 8;
if shiftBits == 0 then
return this;
end
local shiftNum = 2 ^ shiftBits;
local unshiftNum = 2 ^ (8 - shiftBits);
for i = 1, byteCount, 1 do
local overflow = this.bytes[i] % shiftNum;
this.bytes[i] = math_floor(this.bytes[i] / shiftNum);
if i ~= 1 then
this.bytes[i - 1] = this.bytes[i - 1] + overflow * unshiftNum;
end
end
-- strip zero
if this.bytes[byteCount] == 0 then
this.bytes[byteCount] = nil;
if byteCount == 1 then
this.sign = 0;
end
end
return this;
end
function bigint:Shl(n)
n = bigint_ensureInt(n);
if self.sign == 0 or n == 0 then
return self;
end
if n < 0 then
return self:Shr(-n);
end
-- shift whole bytes
local shiftBytes = math_floor(n / 8);
local byteCount = #self.bytes;
local this = self:CopyIfImmutable();
for i = byteCount + 1, byteCount + shiftBytes, 1 do
this.bytes[i] = 0;
end
for i = byteCount + shiftBytes, shiftBytes + 1, -1 do
this.bytes[i] = this.bytes[i - shiftBytes];
end
for i = shiftBytes, 1, -1 do
this.bytes[i] = 0;
end
byteCount = byteCount + shiftBytes;
-- shift bits
local shiftBits = n % 8;
if shiftBits == 0 then
return this;
end
local shiftNum = 2 ^ shiftBits;
local unshiftNum = 2 ^ (8 - shiftBits);
for i = byteCount, shiftBytes + 1, -1 do
local overflow = math_floor(this.bytes[i] / unshiftNum);
this.bytes[i] = (this.bytes[i] * shiftNum) % 256;
if overflow ~= 0 then
this.bytes[i + 1] = (this.bytes[i + 1] or 0) + overflow;
end
end
return this;
end
function bigint:Bor(other)
other = bigint_ensureBigInt(other);
if other.sign == 0 then
return self;
end
if self.sign == 0 then
return other:Abs();
end
if self:CompareU(other) == 0 then
return self;
end
local this;
local count = #self.bytes;
local otherCount = #other.bytes;
if otherCount > count then
count = otherCount;
end
for i = 1, count, 1 do
local result = 0;
local bit = 1;
local byte = (this or self).bytes[i];
local origByte = byte;
byte = byte or 0;
local otherByte = other.bytes[i] or 0;
for _ = 1, 8, 1 do
if (byte % 2) >= 1 or (otherByte % 2) >= 1 then
result = result + bit;
end
byte = byte / 2;
otherByte = otherByte / 2;
bit = bit * 2;
end
if result ~= origByte then
if this == nil then
-- lazy copy
this = self:CopyIfImmutable();
end
this.bytes[i] = result;
end
end
return this or self;
end
function bigint:Band(other)
other = bigint_ensureBigInt(other);
if self.sign == 0 then
return self;
end
if other.sign == 0 then
return other:Abs();
end
if self:CompareU(other) == 0 then
return self;
end
local this;
local count = #self.bytes;
local otherCount = #other.bytes;
if otherCount > count then
count = otherCount;
end
for i = 1, count, 1 do
local result = 0;
local bit = 1;
local byte = (this or self).bytes[i];
byte = byte or 0;
local origByte = byte;
local otherByte = other.bytes[i] or 0;
for _ = 1, 8, 1 do
if (byte % 2) >= 1 and (otherByte % 2) >= 1 then
result = result + bit;
end
byte = byte / 2;
otherByte = otherByte / 2;
bit = bit * 2;
end
if result ~= origByte then
if this == nil then
-- lazy copy
this = self:CopyIfImmutable();
end
this.bytes[i] = result;
end
end
if this == nil then
this = self;
else
bigint_rstrip(this);
end
return this;
end
function bigint:Bxor(other)
other = bigint_ensureBigInt(other);
if other.sign == 0 then
return self;
end
if self.sign == 0 then
return other:Abs();
end
if self:CompareU(other) == 0 then
return bigint.Zero;
end
local this = self:CopyIfImmutable();
local count = #self.bytes;
local otherCount = #other.bytes;
if otherCount > count then
count = otherCount;
end
for i = 1, count, 1 do
local result = 0;
local bit = 1;
local byte = this.bytes[i];
local origByte = byte;
byte = byte or 0;
local otherByte = other.bytes[i] or 0;
for _ = 1, 8, 1 do
if ((byte % 2) >= 1) ~= ((otherByte % 2) >= 1) then
result = result + bit;
end
byte = byte / 2;
otherByte = otherByte / 2;
bit = bit * 2;
end
this.bytes[i] = result;
end
bigint_rstrip(this);
return this;
end
function bigint:Bnot(size)
local this = self:CopyIfImmutable();
local byteCount = #self.bytes;
size = bigint_ensureInt(size, 1, nil, byteCount);
if this.sign == 0 then
this.bytes[1] = 0xff;
this.sign = 1;
return this;
end
for i = 1, byteCount, 1 do
local result = 0;
local bit = 1;
local byte = this.bytes[i];
for _ = 1, 8, 1 do
if (byte % 2) < 1 then
result = result + bit;
end
byte = byte / 2;
bit = bit * 2;
end
this.bytes[i] = result;
end
for i = byteCount + 1, size, 1 do
this.bytes[i] = 0xff;
end
bigint_rstrip(this);
return this;
end
function bigint:SetBits(...)
local arg = {...};
local count = #arg;
if count == 0 then
return self;
end
local this;
local byteCount = #self.bytes;
for i = 1, count, 1 do
local bit = bigint_ensureInt(arg[i], 1);
bit = bit - 1;
local byteNum = math_floor(bit / 8) + 1;
if byteNum > byteCount then
if this == nil then
-- lazy copy
this = self:CopyIfImmutable();
if this.sign == 0 then
this.sign = 1;
end
end
for j = byteCount + 1, byteNum, 1 do
this.bytes[j] = 0;
end
byteCount = byteNum;
end
local byte = (this or self).bytes[byteNum];
local bitNum = 2 ^ (bit % 8);
if (byte / bitNum) % 2 < 1 then
if this == nil then
-- lazy copy
this = self:CopyIfImmutable();
if this.sign == 0 then
this.sign = 1;
end
end
this.bytes[byteNum] = byte + bitNum;
end
end
return this or self;
end
function bigint:UnsetBits(...)
local arg = {...};
if self.sign == 0 then
return self;
end
local count = #arg;
if count == 0 then
return self;
end
local this;
for i = 1, count, 1 do
local bit = bigint_ensureInt(arg[i], 1);
bit = bit - 1;
local byteNum = math_floor(bit / 8) + 1;
local byte = (this or self).bytes[byteNum];
if byte ~= nil then
local bitNum = 2 ^ (bit % 8);
if (byte / bitNum) % 2 >= 1 then
if this == nil then
-- lazy copy
this = self:CopyIfImmutable();
end
this.bytes[byteNum] = byte - bitNum;
end
end
end
if this == nil then
this = self;
else
bigint_rstrip(this);
end
return this;
end
function bigint:GetBit(i)
i = bigint_ensureInt(i, 1);
if i < 1 then
return nil;
end
if self.sign == 0 then
return 0;
end
i = i - 1;
local byteNum = math_floor(i / 8) + 1;
local byte = self.bytes[byteNum];
if byte == nil or byte == 0 then
return 0;
end
local bitNum = i % 8;
return math_floor(byte / (2 ^ bitNum)) % 2;
end
-- convert 2's complement unsigned number to signed
function bigint:CastSigned(size)
local byteCount = #self.bytes;
size = bigint_ensureInt(size, 1, nil, byteCount);
if self.sign == 0 then
return self;
end
if byteCount > size then
error("twos complement overflow");
end
if self.sign == 1 and (self.bytes[size] or 0) > 0x7f then
local this = self:CopyIfImmutable();
local mutable = this.mutable;
this.mutable = true;
this = this:Bnot(size):Add(bigint.One);
this.sign = -1;
this.mutable = mutable;
return this;
end
return self;
end
-- convert 2's complement signed number to unsigned
function bigint:CastUnsigned(size)
local byteCount = #self.bytes;
size = bigint_ensureInt(size, 1, nil, byteCount);
if self.sign == 0 then