Sign in Join
Wiki copytemporary

BigInteger: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: BigInteger

OldNewLine
1+There are several Pascal libraries for BigInteger (and Big Decimals, like 1.23) functionality. The C-based [[gmp|GMP library]] can also be used in Free Pascal, it's not listed here.
2+
3+## fptlsbigint.pas in fcl-hash package in FPC Trunk
4+
5+- Refactored from Ultibo Big Integer unit Copyright (C) 2018 - SoftOz Pty Ltd.
6+- Source Code: <https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/fcl-hash/src/fptlsbigint.pas>
7+
8+Inspired by AXTLS - \crypto\bigint.c - Copyright (c) 2007, Cameron Rich\
9+Reference: Handbook of Applied Cryptography (Chapter 14) - <http://cacr.uwaterloo.ca/hac/about/chap14.pdf>
10+
11+## TForge
12+
13+- By: Sergey Kasandrov
14+- Homepage: <https://github.com/Alexey-T/TForge-Pascal>
15+
16+Modern cryptographic library for Delphi and Lazarus/FPC implemented as a set of runtime packages. Project development was stopped, the last found snapshot v0.76 was put to GitHub.
17+
18+## Big Decimal Math
19+
20+- By: Benito van der Zander
21+- Homepage: <https://benibela.de/sources_en.html#bigdecimalmath>
22+
23+This unit provides a BigDecimal-Type which stores arbitrary precision (BCD) decimal numbers and can be used like any native numeric type.
24+
25+## FNX
26+
27+- By: Marcel Martin
28+- Homepage: <https://www.ellipsa.eu/public/fnx/fnx.html>
29+
30+Library of multi-precision numbers (currently, only big integers) written for Free Pascal and Linux. The assembler code that can be enabled with a compiler directive, is for x86-64 processors only.
31+
32+## MPArith
33+
34+- By: Wolfgang Ehrhardt
35+- Homepage: <https://github.com/Alexey-T/Wolfgang_Ehrhardt_codes/tree/master/Misc/MPArith>
36+
37+Project development was stopped in about 2018. The last found snapshot was put to GitHub.
38+
39+Comment by forum user [MathMan](https://forum.lazarus.freepascal.org/index.php?action=profile;u=55215):
40+
41+- Functionality
42+ - supports 16/32/64 bit, little- & big-endian
43+ - pure functional implementation
44+ - dynamic sizing
45+ - solid error handling (function results or exceptions)
46+ - extensive set of functions - arith, bitops, comparisons, type conversion, base conversion
47+ - extensive number theory - gcd, modinv, mulmod, pow, powmod, sqrt+remainder, n-root+remainder, and a lot more
48+
49+- Algorithms
50+ - basecase multiplication only 33% slower than best known pure Pascal
51+ - efficient multiplication: Karatsuba, Toom3
52+ - efficient squaring: Karatsuba, Toom3
53+ - efficient division: Burnikel-Ziegler
54+ - div&cong base conversion (both ways)
55+ - efficient GCD
56+ - efficient modular ops: based on Montgomery & Barrett reductions
57+ - efficient roots: quadratically converging
58+
59+- Issues
60+ - default thresholds & missing tool for auto detection
61+ - Burnikel-Ziegler efficiency degrades on large sizes
62+ - no class implementation
63+
64+- Nice things
65+ - simple to activate under FPC
66+ - accompanied by a good manual
67+ - extensive test coverage
68+ - there is also rationals, reals & complex available with large set of functions
69+
70+## BigInt
71+
72+- By: Franco Milani
73+- Homepage: <http://spazioinwind.libero.it/frm/software>
74+
75+Though this is a Pascal source \>90% are coded as x86-32 Win32 assembler routines.
76+
77+## HugeInt
78+
79+- By: David J Butler
80+- Homepage: <https://github.com/fundamentalslib/fundamentals5>
81+
82+Part of a much larger library but the unit can be used self-contained.
83+
84+## LongMathForPascal
85+
86+- By: Zsolt Szakaly
87+- Homepage: <https://github.com/zsoltszakaly/longmathforpascal>
88+
89+A quite recent contribution (2021) - unfortunately it does not support any efficient algorithms and becomes slow very fast.
90+
91+## GInt
92+
93+- By: Walied Othman
94+- Homepage: <https://github.com/SnakeDoctor/FGInt>
95+
96+Again part of a larger library but can be used self-contained.
97+
98+## GNURZ
99+
100+- By: Alexander Staidl
101+- Homepage: <https://github.com/afriess/GNURZ>
102+
103+Development was stopped in about 2009 year. Almost all identifiers are in German. Additional unit written in Assembler exists, but it's not required.
104+
105+## BigIntegers for Delphi
106+
107+- By: Rudy Velthuis
108+- Homepage: <https://github.com/rvelthuis/DelphiBigNumbers>
109+
110+Code can be adapted for Free Pascal. How-to from [Rudy's website](http://www.rvelthuis.de/programs/bigintegers.html):
111+
112+ In {$mode delphi}, it is possible to compile the units. FreePascal doesn’t seem to use segmented unit names (yet), so you’ll have to rename some of the used units and qualifiers like System.Math or System.SysUtils to simply Math or SysUtils. At first, you’ll have to {$DEFINE PUREPASCAL}, because making the assembler work satisfactorily could be quite a job. It seems to work alright, if you set {$asmmode intel}, but it is a little finicky. Stuff like LEA EAX,[EDX + EDI*CLimbSize] seems to cause problems, but [EDX + CLimbSize*EDI] works. That is a bit of a problem for me, because I most of the time use the former variety. But I guess that, with enough time on your hands, you can make it work.
113+
114+ Some of the tricky routines, e.g. ToString, which uses RecursiveToString, which in its turn needs an exact alignment of bytes to get a usable string, don’t work, but in the meantime — until you solved the problem — you can e.g. use (or wrap or rename) .ToStringClassic(10) instead. I guess the tricky routines just need some work and some debugging to make them work under FreePascal. I only tried a quick and dirty test, did not do any extensive tests, but it is obviously possible to make it work. Just don’t give up immediately.
115+
116+ Types like TArray don’t seem to work. And there is no way you can define an array of BigInteger before BigInteger is defined, so you will have to define a type BigIntegerArray = array of BigInteger inside the BigInteger record, as a nested type. You will meet other, similar problems. Another was the fact that there is no DivMod() function for UInt64 types, so you will have to either use the one for Longint or use div and mod separately. It could be that the change I made there is one of the causes for the failure of RecursiveToString. This needs some investigation.
117+
118+ There will be other, simple problems and it is well possible that there are a few not-so-simple problems. But it looks as if BigIntegers can be used in FreePascal.
119+
120+Comment by forum user [MathMan](https://forum.lazarus.freepascal.org/index.php?action=profile;u=55215):
121+
122+- Functionality
123+ - supports 32/64 bit, little- & big-endian
124+ - class implementation with operators & functional replicants
125+ - dynamic sizing and immutability
126+ - late binding
127+ - solid error handling
128+ - solid set of operators - arith, bitops, comparisons, expl & impl conversions
129+ - some number theory - gcd, modinv, mulmod, pow, powmod, sqrt+remainder, n-root+remainder
130+- Algorithms
131+ - basecase multiplication only 33% slower than best known pure Pascal
132+ - efficient multiplication: Karatsuba, Toom3
133+ - efficient squaring: Karatsuba
134+ - efficient division: Burnikel-Ziegler
135+ - div&cong base conversion (from binary to arb base only)
136+ - euclidean GCD
137+- Issues
138+ - tricky to convert to FPC
139+ - memory management degrades efficient algs on larger sizes (speed reduced by 5)
140+ - default thresholds & missing tool for auto detection
141+ - sqr-schoolbook = mul-schoolbook
142+ - missing efficient GCD algorithm
143+ - missing efficient mod algorithms (Montgomery, Barrett)
144+ - the way asm-enhancements are integrated could be improved (subjective)
145+ - testing depth & breadth could be improved (subjective)
146+- Nice things
147+ - there is also BigDecimals, BigRationals & BigFloats
148+
149+## Multi-Word-Int
150+
151+- By: Mark Taylor
152+- Homepage: <https://github.com/ad1mt/Multi-Word-Int> or <https://mark-taylor.me.uk/index.php?page=Software>
153+
154+Designed to be:
155+
156+- easy to use, requires minimal changes to existing code
157+- written entirely in Pascal, no assembly or C language, so portable and reliable
158+- works in both 64bit and 32bit environments
159+- compact code base, but has limited maths functions
160+- reasonably fast (although not as fast as MPArith which is around 10x faster)
161+
162+NB this is a recent new development, and should be considered beta code.
163+
164+## See Also
165+
166+- [[Portal:SciTech]]
167+
168+---
169+*From the Free Pascal wiki, page "BigInteger", as it stood on 2023-11-18 after a change by WayneSherman, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
170+Category: Comparisons
171+Category: Libraries
172+Category: Mathematics

Back to history