-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongArithLib.pas
713 lines (618 loc) · 20.5 KB
/
LongArithLib.pas
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
{*******************************************************}
{ }
{ Delphi Long Arithmetic For RSA }
{ Encryption Component Library }
{ }
{ Copyright (c) 2019 CryptoProsper }
{ Created by Konstantin Shulga }
{ Date created 17/03/2019 }
{ Latest update date 30/04/2019 }
{ }
{*******************************************************}
unit LongArithLib;
interface
uses
SysUtils;
const
MaxLongNumLen = 10000; // Maximum array dimension
Base = 10000; // "Number System"
NumOfDigits = 4; // Number of digits in one cell
FLNumGreaterSLNum = 1; // Indicates that the first number
// is greater than the second
FLNumLessSLNum = -1; // Indicates that the first number
// is less than the second
FLNumEqualSLNum = 0; // Indicates that the first number
// is equal to the second
Quotient = 'Quotient'; // Indicates that you need to get
// the whole part of the division
Remainder = 'Remainder'; // Indicates that you need to get
// the remainder of the division
type
{ TNumericPartLongNum is an array storing the numeric part
of the "long number". The number is stored in 10,000
"number systems" (Base). One digit of the number holds
a maximum of 4 digits (NumOfDigits). In the zero element
of the array is stored the length of the "long number" }
TNumericPartLongNum = array[0..MaxLongNumLen] of Word;
{ TLongNum is a record that stores a "long number".
The first field of the record is the
"long number" sign (Sign). The second field
of the record is the numeric part
of the "long number" (LongNum) }
TLongNum = record
Sign : ShortInt;
NumericPLN : TNumericPartLongNum;
end;
{ Converts a "long number" from a string to an array of numbers
with Base(10.000) number system. Numbers are placed
in reverse order (from low-order to high-order) }
function ConvertStrToLongNum(LongNum: String): TLongNum;
{ Converts a "long number" from an array of numbers to a string }
function ConvertLongNumToStr(LongNum: TLongNum): String;
{ Returns the largest length of two "long numbers"
without sign }
function GetMaxLenOfLongNum(const FirstLongNum,
SecLongNum: TLongNum): Word;
{ Compares the modular parts of two "long numbers".
If the first number is greater than the second,
then returns FLNumGreaterSLNum.
If the first long number is less than the second,
then it returns FLNumLessSLNum.
If the first long number is equal to the second,
then returns FLNumEqualSLNum }
function CompareModulesLongNums(FirstLongNum,
SecLongNum: String): ShortInt;
{ Compares two "long numbers" with the sign.
If the first number is greater than the second,
then returns FLNumGreaterSLNum.
If the first long number is less than the second,
then it returns FLNumLessSLNum.
If the first long number is equal to the second,
then returns FLNumEqualSLNum }
function CompareLongNums(FirstLongNum,
SecLongNum: String): ShortInt;
{ Adds to the first "long number" the second,
taking into account the sign
and the discharge overflow }
function AddLongNums(FirstLongNum,
SecLongNum: String): String;
{ Subtracts from the "first long number" the second,
taking into account the sign }
function SubLongNums(FirstLongNum,
SecLongNum: String): String;
{ Multiplies the first "long number" by the second,
taking into account the sign
and overflow of the discharge }
function MultLongNums(const FirstLongNum,
SecLongNum: String): String;
{ Calculates the integer part of dividing
the "long number" into a "short" one,
taking into account the sign }
function GetIntPartOfDivLNumBySNum(const LongNum: String;
ShortNum: LongInt): String;
{ Calculates the remainder of dividing
a "long number" into a "short" one,
taking into account the sign }
function GetRemOfDivLNumBySNum(const LongNum: String;
const ShortNum: LongInt): String;
{ Finds the integer part and remainder of the division
of two "long numbers" using a binary search,
taking into account the sign. ReturnParam indicates
that it is necessary to return:
the integer part of the division (Quotient)
or the remainder (Remainder) }
function GetQuotOrResBinSearch(FirstLongNum,
SecLongNum, ReturnParam: String): String;
{ Divides the first "long number" by the second "long number",
taking into account the sign. ReturnParam indicates
that it is necessary to return:
the integer part of the division (Quotient)
or the remainder (Remainder) }
function DivideLongNumByLongNum(FirstLongNum,
SecLongNum, ReturnParam: String): String;
{ Raises the "long number" to the power,
taking into account the sign using multiplication.
"Power" can not be negative }
function RaiseLNumToPower(const Number: String;
const Power: Byte): String;
{ Converts a "long number" from a decimal number system
to a binary one by dividing the number by two.
"Number" can not be negative }
function ConvertDecToBin(Number: String): String;
{ Builds a number modulo using the fast exponentiation algorithm }
function PowerNumModulo(const Number, Power, Module: String): String;
{ Gets the multiplicative inverse of the value Number modulo }
function GetMultInverseMod(const Number, Module: String): String;
implementation
function ConvertStrToLongNum;
var
NewItemPos, ErrorCode : Integer;
TmpStr : String;
I : Word;
// NewItemPos - pointer to the first digit of the new number
begin
if LongNum[1] = '-' then
begin
Result.Sign := -1;
Delete(LongNum, 1, 1);
end
else
Result.Sign := 1;
FillChar(Result.NumericPLN, SizeOf(Result.NumericPLN), 0);
I := 0;
NewItemPos := Length(LongNum) - NumOfDigits + 1;
{ While the pointer to the beginning of the new number
is not negative: copy the NumOfDigits characters
from the string LongNum and convert the copied string
into a number }
while NewItemPos > 0 do
begin
Inc(I);
TmpStr := Copy(LongNum, NewItemPos, NumOfDigits);
Val(TmpStr, Result.NumericPLN[I], ErrorCode);
Dec(NewItemPos, NumOfDigits);
end;
{ If there are senior digits of the number
(the number of digits is less than 4),
then copy the remaining digits
and convert them to the number }
if NewItemPos + NumOfDigits > 1 then
begin
Inc(I);
TmpStr := Copy(LongNum, 1, NewItemPos + NumOfDigits - 1);
Val(TmpStr, Result.NumericPLN[I], ErrorCode);
end;
Result.NumericPLN[0] := I;
end;
function ConvertLongNumToStr;
var
I : Word;
begin
Result := '';
if LongNum.Sign = -1 then
Result := Result + '-';
for I := LongNum.NumericPLN[0] downto 1 do
begin
if I <> LongNum.NumericPLN[0] then
begin
if LongNum.NumericPLN[I] < 10 then
Result := Result + '000' + IntToStr(LongNum.NumericPLN[I])
else if (LongNum. NumericPLN[I] >= 10) and
(LongNum.NumericPLN[I] < 100) then
Result := Result + '00' + IntToStr(LongNum.NumericPLN[I])
else if (LongNum.NumericPLN[I] >= 100) and
(LongNum.NumericPLN[I] < 1000) then
Result := Result + '0' + IntToStr(LongNum.NumericPLN[I])
else
Result := Result + IntToStr(LongNum.NumericPLN[I]);
end
else
Result := Result + IntToStr(LongNum.NumericPLN[I]);
end;
end;
function GetMaxLenOfLongNum;
begin
if FirstLongNum.NumericPLN[0] > SecLongNum.NumericPLN[0] then
Result := FirstLongNum.NumericPLN[0]
else
Result := SecLongNum.NumericPLN[0];
end;
function CompareModulesLongNums;
var
Len : Word;
FLNum, SLNum : TLongNum;
begin
FLNum := ConvertStrToLongNum(FirstLongNum);
SLNum := ConvertStrToLongNum(SecLongNum);
Len := GetMaxLenOfLongNum(FLNum, SLNum);
while (Len > 0) and
(FLNum.NumericPLN[Len] = SLNum.NumericPLN[Len]) do
Dec(Len);
if Len = 0 then
Result := FLNumEqualSLNum
else if FLNum.NumericPLN[Len] > SLNum.NumericPLN[Len] then
Result := FLNumGreaterSLNum
else
Result := FLNumLessSLNum;
end;
function CompareLongNums;
begin
if (FirstLongNum[1] = '-') and (SecLongNum[1] <> '-') then
Result := FLNumLessSLNum
else if (FirstLongNum[1] <> '-') and (SecLongNum[1] = '-') then
Result := FLNumGreaterSLNum
else if (FirstLongNum[1] = '-') and (SecLongNum[1] = '-') then
begin
Delete(FirstLongNum, 1, 1);
Delete(SecLongNum, 1, 1);
Result := -1 * CompareModulesLongNums(FirstLongNum, SecLongNum);
end
else
Result := CompareModulesLongNums(FirstLongNum, SecLongNum);
end;
function AddLongNums;
var
Len, Carry, I : Word;
FLNum, SLNum, ResLNum : TLongNum;
begin
FLNum := ConvertStrToLongNum(FirstLongNum);
SLNum := ConvertStrToLongNum(SecLongNum);
Len := GetMaxLenOfLongNum(FLNum, SLNum);
Carry := 0;
{ The addition of two "long numbers" with the sign.
At discharge overflow, transfer to
the next discharge is made }
if (FLNum.Sign = -1) and (SLNum.Sign = 1) then
begin
Delete(FirstLongNum, 1, 1);
Result := SubLongNums(SecLongNum, FirstLongNum);
end
else if (FLNum.Sign = 1) and (SLNum.Sign = -1) then
begin
Delete(SecLongNum, 1, 1);
Result := SubLongNums(FirstLongNum, SecLongNum);
end
else
begin
if (FLNum.Sign = -1) and (SLNum.Sign = -1) then
ResLNum.Sign := -1
else
ResLNum.Sign := 1;
for I := 1 to Len do
begin
Carry := Carry + FLNum.NumericPLN[I] + SLNum.NumericPLN[I];
ResLNum.NumericPLN[I] := Carry mod Base;
Carry := Carry div Base;
end;
if Carry > 0 then
begin
Inc(Len);
ResLNum.NumericPLN[Len] := Carry;
end;
ResLNum.NumericPLN[0] := Len;
Result := ConvertLongNumToStr(ResLNum);
end;
end;
function SubLongNums;
var
Len, Tmp, I : Word;
Carry : Integer;
FLNum, SLNum, ResLNum : TLongNum;
begin
FLNum := ConvertStrToLongNum(FirstLongNum);
SLNum := ConvertStrToLongNum(SecLongNum);
Len := GetMaxLenOfLongNum(FLNum, SLNum);
Carry := 0;
{ Subtracting from the first "long number" of the second,
taking into account the signs. If, when subtracting
successive digits of a number, a negative number
is obtained, then we borrow from the left digit "Base" }
if (FLNum.Sign = -1) and (SLNum.Sign = -1) then
begin
Delete(SecLongNum, 1, 1);
Result := AddLongNums(FirstLongNum, SecLongNum);
end
else if (FLNum.Sign = 1) and (SLNum.Sign = -1) then
begin
Delete(SecLongNum, 1, 1);
Result := AddlongNums(FirstLongNum, SecLongNum);
end
else if (FLNum.Sign = -1) and (SLNum.Sign = 1) then
begin
Delete(FirstLongNum, 1, 1);
Result := '-' + AddlongNums(FirstLongNum, SecLongNum);
end
else
begin
if CompareModulesLongNums(FirstLongNum, SecLongNum) = FLNumLessSLNum then
begin
ResLNum.Sign := -1;
for I := 1 to Len do
begin
Tmp := FLNum.NumericPLN[I];
FLNum.NumericPLN[I] := SLNum.NumericPLN[I];
SLNum.NumericPLN[I] := Tmp;
end;
end
else
ResLNum.Sign := 1;
for I := 1 to Len do
begin
Carry := Carry + FLNum.NumericPLN[I] -
SLNum.NumericPLN[I] + Base;
ResLNum.NumericPLN[I] := Carry mod Base;
if Carry < Base then
Carry := -1
else
Carry := 0;
end;
while (ResLNum.NumericPLN[Len] = 0) and (Len > 1) do
Dec(Len);
ResLNum.NumericPLN[0] := Len;
Result := ConvertLongNumToStr(ResLNum);
end;
end;
function MultLongNums;
var
I, J, K : Word;
Carry : LongWord;
FLNum, SLNum, ResLNum : TLongNum;
begin
FLNum := ConvertStrToLongNum(FirstLongNum);
SLNum := ConvertStrToLongNum(SecLongNum);
FillChar(ResLNum.NumericPLN, SizeOf(ResLNum.NumericPLN), 0);
{ Multiplication of two "long numbers"
taking into account the sign
and overflow of the discharge }
if (FLNum.Sign = -1) and (SLNum.Sign = -1) then
ResLNum.Sign := 1
else if (FLNum.Sign = 1) and (SLNum.Sign = 1) then
ResLNum.Sign := 1
else
ResLNum.Sign := -1;
for I := 1 to FLNum.NumericPLN[0] do
begin
for J := 1 to SLNum.NumericPLN[0] do
begin
Carry := FLNum.NumericPLN[I] * SLNum.NumericPLN[J];
K := I + J - 1;
while Carry > 0 do
begin
Carry := Carry + ResLNum.NumericPLN[K];
ResLNum.NumericPLN[K] := Carry mod Base;
Carry := Carry div Base;
if K > ResLNum.NumericPLN[0] then
ResLNum.NumericPLN[0] := K;
Inc(K);
end;
end;
end;
Result := ConvertLongNumToStr(ResLNum);
end;
function GetIntPartOfDivLNumBySNum;
var
ResLen, I, K : Word;
LNum, ResLNum : TLongNum;
TmpNum : Int64;
IsContinue : Boolean;
begin
FillChar(ResLNum.NumericPLN, SizeOf(ResLNum.NumericPLN), 0);
LNum := ConvertStrToLongNum(LongNum);
IsContinue := False;
TmpNum := 0;
ResLen := 0;
K := 0;
{ Calculate the integer part of dividing the "long number"
into a short one, taking into account the sign }
if (LNum.Sign = -1) and (ShortNum < 0) then
ResLNum.Sign := 1
else if (LNum.Sign = -1) and (ShortNum > 0) then
ResLNum.Sign := -1
else if (ShortNum < 0) and (LNum.Sign = 1) then
ResLNum.Sign := -1
else if CompareModulesLongNums(LongNum,
IntToStr(ShortNum)) = FLNumLessSLNum then
ResLNum.Sign := 1;
for I := LNum.NumericPLN[0] downto 1 do
begin
TmpNum := TmpNum * Base + LNum.NumericPLN[I];
if (TmpNum < Abs(ShortNum)) and (K = 0) and (I > 1) then
IsContinue := True;
if not IsContinue then
begin
K := 1;
Inc(ResLen);
ResLNum.NumericPLN[I] := TmpNum div Abs(ShortNum);
TmpNum := TmpNum mod Abs(ShortNum);
end;
IsContinue := False;
end;
ResLNum.NumericPLN[0] := ResLen;
Result := ConvertLongNumToStr(ResLNum);
end;
function GetRemOfDivLNumBySNum;
var
I : Word;
LNum : TLongNum;
begin
if CompareModulesLongNums(LongNum, IntToStr(ShortNum)) = -1 then
Result := LongNum
else
begin
Result := '0';
LNum := ConvertStrToLongNum(LongNum);
for I := LNum.NumericPLN[0] downto 1 do
Result := IntToStr((StrToInt(Result) *
Base + LNum.NumericPLN[I]) mod ShortNum);
end;
end;
function GetQuotOrResBinSearch;
var
LeftBorder, RightBorder, CentralEl, EstimatedQuot : String;
IsFindQuotient : Boolean;
begin
IsFindQuotient := False;
Result := '';
if CompareModulesLongNums(FirstLongNum, SecLongNum) = FLNumLessSLNum then
begin
if ReturnParam = Quotient then
Result := '0'
else if ReturnParam = Remainder then
Result := FirstLongNum;
end
else
begin
if (FirstLongNum[1] = '-') and (SecLongNum[1] <> '-')
and (ReturnParam = Quotient) then
begin
Delete(FirstLongNum, 1, 1);
Result := '-';
end
else if (FirstLongNum[1] <> '-') and (SecLongNum[1] = '-')
and (ReturnParam = Quotient) then
begin
Delete(SecLongNum, 1, 1);
Result := '-';
end
else if (FirstLongNum[1] = '-') and (SecLongNum[1] = '-')
and (ReturnParam = Quotient) then
begin
Delete(FirstLongNum, 1, 1);
Delete(SecLongNum, 1, 1);
Result := '';
end
else if (FirstLongNum[1] = '-') and (SecLongNum[1] <> '-')
and (ReturnParam = Remainder) then
begin
Delete(FirstLongNum, 1, 1);
Result := '-';
end
else if (FirstLongNum[1] <> '-') and (SecLongNum[1] = '-')
and (ReturnParam = Remainder) then
begin
Delete(SecLongNum, 1, 1);
Result := '';
end
else if (FirstLongNum[1] = '-') and (SecLongNum[1] = '-')
and (ReturnParam = Remainder) then
begin
Delete(FirstLongNum, 1, 1);
Delete(SecLongNum, 1, 1);
Result := '-';
end;
LeftBorder := '1';
RightBorder := FirstLongNum;
{ Binary search for the required private }
while (CompareModulesLongNums(SubLongNums(RightBorder, '1'),
LeftBorder) = 1) and not IsFindQuotient do
begin
CentralEl := GetIntPartOfDivLNumBySNum(AddLongNums(LeftBorder,
RightBorder), 2);
EstimatedQuot := MultLongNums(CentralEl, SecLongNum);
case CompareModulesLongNums(FirstLongNum, EstimatedQuot) of
FLNumGreaterSLNum : LeftBorder := CentralEl;
FLNumEqualSLNum : IsFindQuotient := True;
FLNumLessSLNum : RightBorder := CentralEl;
end;
end;
if ReturnParam = Quotient then
Result := Result + GetIntPartOfDivLNumBySNum(AddLongNums(RightBorder,
LeftBorder), 2)
else if ReturnParam = Remainder then
Result := Result + SubLongNums(FirstLongNum,
MultLongNums(GetIntPartOfDivLNumBySNum(AddLongNums(RightBorder,
LeftBorder), 2), SecLongNum))
else
Result := 'Dividing error!';
end;
end;
function DivideLongNumByLongNum;
begin
if (SecLongNum = '1') and (ReturnParam = Quotient) then
Result := FirstLongNum
else if (SecLongNum = '-1') and (ReturnParam = Quotient) then
Result := '-' + FirstLongNum
else if (SecLongNum = '1') or (SecLongNum = '-1')
and (ReturnParam = Remainder) then
Result := '0'
else if SecLongNum = '0' then
Result := 'Division error by zero!'
else
Result := GetQuotOrResBinSearch(FirstLongNum,
SecLongNum, ReturnParam);
end;
function RaiseLNumToPower;
var
I : LongWord;
begin
Result := '1';
I := 1;
if (Power = 0) and (Number[1] <> '-') then
Result := '1'
else if (Power = 0) and (Number[1] = '-') then
Result := '-1'
else if Power = 1 then
Result := Number
else
begin
while I <= Power do
begin
Result := MultLongNums(Result, Number);
Inc(I);
end;
end;
end;
function ConvertDecToBin;
var
I : Integer;
TmpStr: String;
begin
if Number[1] = '-' then
Result := 'The number can not be negative!'
else
begin
TmpStr := '';
{ Converts a number from the decimal number system
to the binary number system by dividing it by two }
while (Number <> '0') and (Number <> '1') do
begin
TmpStr := TmpStr + GetRemOfDivLNumBySNum(Number, 2);
Number := GetIntPartOfDivLNumBySNum(Number, 2);
end;
TmpStr := TmpStr + Number;
SetLength(Result, Length(TmpStr));
for I := 1 to Length(TmpStr) do
Result[I] := TmpStr[Length(TmpStr)-I+1];
end;
end;
function PowerNumModulo;
var
PowerInBinRep : String;
I : Word;
begin
PowerInBinRep := ConvertDecToBin(Power);
Result := '1';
for I := 1 to Length(PowerInBinRep) - 1 do
begin
Result := DivideLongNumByLongNum(RaiseLNumToPower(MultLongNums(Result,
RaiseLNumToPower(Number, StrToInt(PowerInBinRep[I]))),
2), Module, Remainder);
end;
Result := DivideLongNumByLongNum(MultLongNums(Result,
RaiseLNumToPower(Number,
StrToInt(PowerInBinRep[Length(PowerInBinRep)]))),
Module, Remainder);
end;
function GetMultInverseMod;
var
T, R, NewT, NewR, Q, Tmp : String;
IsInvertable : Boolean;
begin
T := '0';
R := Module;
NewT := '1';
NewR := Number;
IsInvertable := True;
while NewR <> '0' do
begin
Q := DivideLongNumByLongNum(R, NewR, Quotient);
Tmp := SubLongNums(T, MultLongNums(Q, NewT));
T := NewT;
NewT := Tmp;
Tmp := SubLongNums(R, MultLongNums(Q, NewR));
R := NewR;
NewR := Tmp;
end;
if CompareModulesLongNums(R, '1') = FLNumGreaterSLNum then
begin
Result := 'Number is not invertable!';
IsInvertable := False
end;
if IsInvertable then
begin
if CompareLongNums(T, '0') = FLNumLessSLNum then
T := AddLongNums(T, Module);
Result := T;
end;
end;
end.