Mathematics•Foundational Algorithms
Multiprecision Integer Arithmetic
How arbitrary-size integers are represented and operated on, and why the choice of multiplication algorithm sets the ceiling for everything built above it.
The layer everything else assumes is free
Multiprecision integers are stored as arrays of fixed-size digits called limbs, in a base matched to the machine word. Addition is linear in the operand length; multiplication is not, and the crossover points between schoolbook, Karatsuba, Toom–Cook and FFT-based multiplication determine the practical cost of every higher-level algorithm. Division and modular reduction are the awkward operations, and are usually restructured to avoid them.
Learning objectives
- Describe the limb representation of a multiprecision integer and its sign handling.
- State the asymptotic costs of the principal multiplication algorithms.
- Explain why crossover points must be measured rather than assumed.
- Choose between Montgomery, Barrett and plain reduction for a modular workload.
- Identify the base ring appropriate to a given computation.
Section 01Representation
A non-negative multiprecision integer is an array of limbs (a0, …, an−1) in base B, together with a length and a sign. The value is
The base B is chosen as 232 or 264 so that a limb fits a machine register and carries can be detected cheaply. Two conventions compete for negative numbers: sign-magnitude, which keeps a separate sign flag, and two's-complement extension. Sign-magnitude dominates in number-theoretic libraries because the algorithms overwhelmingly operate on magnitudes and branch on sign at a higher level.
Every routine must return a normalised result: no leading zero limbs, and zero represented canonically with length 0 and positive sign. Non-normalised values are the classic source of comparison bugs, because two representations of the same integer then compare unequal.
Section 02Addition, subtraction and comparison
Addition and subtraction are linear. The only subtlety is carry and borrow propagation, which in the worst case ripples the full length of the operand but on random inputs terminates almost immediately.
- Set carry ← 0 and i ← 0.
- While i < max(m, n): set t ← ai + bi + carry (absent limbs read as 0).
- Set ci ← t mod B and carry ← ⌊t / B⌋. On a machine this is a single add-with-carry.
- Increment i and repeat.
- If carry ≠ 0, append it as the leading limb.
- Normalise and return.
Comparison is by length first, then by limbs from the most significant downward. Because the values are normalised, the length comparison is exact and usually decides immediately.
Section 03Multiplication and its crossovers
Multiplication is where algorithm choice matters. Four families are used in practice, and a serious library implements all of them with measured thresholds.
| Method | Cost | Idea | Typical regime |
|---|---|---|---|
| Schoolbook | O(n2) | Direct convolution of limb arrays | Up to roughly 20–40 limbs |
| Karatsuba | O(n1.585) | Split in two; three half-size products instead of four | Tens to a few hundred limbs |
| Toom–Cook (3-way and up) | O(n1.465) for 3-way | Split in k parts; evaluate, multiply pointwise, interpolate | Hundreds to thousands of limbs |
| Schönhage–Strassen / FFT | O(n log n log log n) | Multiplication as cyclic convolution via number-theoretic transform | Many thousands of limbs and above |
Published crossover points are not portable. Cache size, register width, multiplier latency and compiler behaviour all move them. A library that hard-codes another machine's thresholds can be several times slower than one that tunes them at build time. Treat crossovers as measured constants, never as inherited ones.
Section 04Division and modular reduction
Division is the expensive primitive. The classical algorithm processes one quotient limb at a time, estimating each from the leading limbs of the running remainder and correcting a bounded number of times. Correctness of the estimate depends on normalisation: the divisor is first scaled so that its leading limb has its top bit set, which bounds the estimation error to at most two.
Because division is costly, modular arithmetic avoids it wherever a modulus is reused.
Plain reduction
Divide and take the remainder. Correct, simple, and the right choice when the modulus changes on every operation.
Barrett reduction
Precompute a scaled reciprocal of the modulus; replace division by two multiplications and a correction. Suits a fixed modulus with mixed operations.
Montgomery reduction
Work in a transformed residue domain where reduction is exact shifting. Dominant for long chains of modular multiplications, such as modular exponentiation.
Montgomery form costs a conversion in and out. It wins when many multiplications happen between conversions — exponentiation is the canonical case — and loses when a single reduction is needed. It also requires an odd modulus.
Section 05Choosing the base ring
A computation should be carried out in the smallest ring in which it is valid. Working in a larger ring than necessary is the most common source of avoidable cost.
| Ring | Exact? | Cost driver | Typical use |
|---|---|---|---|
| ℤ (integers) | Yes | Operand growth | Resultants, HNF, exact linear algebra |
| ℚ (rationals) | Yes | GCD of numerator and denominator at every step | Avoid where possible — clear denominators and work in ℤ |
| ℤ/nℤ | Yes | Modular reduction | Modular algorithms, CRT reconstruction |
| Fq (finite field) | Yes | Field arithmetic | Polynomial factorisation, curve point counting |
| ℝ, ℂ (floating point) | No | Precision management and error tracking | Regulators, root finding, LLL with floating Gram–Schmidt |
Naive computation over ℚ invokes a GCD at every arithmetic step and the intermediate expressions grow explosively. Standard practice is to clear denominators once, work entirely in ℤ, and divide out a single content factor at the end.
ReferenceFrequently asked questions
Why base 2<sup>64</sup> rather than a decimal base?
Because carry detection, multiplication and shifting then map to single machine instructions. Decimal bases are used only where decimal output is the dominant operation, which is rare in number-theoretic work.
Is FFT multiplication worth implementing?
Only if the workload genuinely reaches operands of many thousands of limbs. Below that, its large constant factor and precision management make it slower than Toom–Cook. Most applications should link a tuned library rather than implement this layer at all.
Does floating point have any legitimate place here?
Yes, but always with an error bound and an exact fallback. Floating-point Gram–Schmidt inside LLL is standard practice and is safe precisely because the reduction conditions are re-verified in exact arithmetic when the floating computation looks marginal.
NavigateContinue in this stream
Curated next steps from this page. The site also surfaces algorithmically related reading below.
ProvenanceSources and further reading
Knuth, The Art of Computer Programming, volume 2, remains the reference treatment of multiprecision arithmetic. Current implementations of record are GMP and the arithmetic cores of PARI/GP and FLINT.
This page is an original KEVOS explanatory article. It presents the underlying mathematics — definitions, algorithms, complexity results and selection criteria — in KEVOS editorial voice. No text is reproduced from any copyrighted source. Where numerical tables are relevant, KEVOS links to live authoritative databases rather than republishing static values.
Handbook application: from concept to controlled practice
Purpose. This expanded section turns the original page into a practical handbook. It preserves the supplied material and adds a repeatable way to apply, check and review Multiprecision Integer Arithmetic. It does not replace a contract, legislation, a controlled standard, competent engineering judgement or specialist advice.
The operating aim is to turn a compact mathematical statement into a usable chain of definitions, claims, examples and checks. Read the original explanation first, then use the workflow and checks below to convert knowledge into evidence.
Treat Multiprecision Integer Arithmetic as a network of definitions and implications, not as a list of formulas. The working vocabulary on this page—reduction, section, multiprecision, arithmetic, representation—should be made explicit before any proof or computation begins. Record the ambient set or structure, the permitted operations and the equality or equivalence relation in use. A compact theorem often changes meaning when the base field, finiteness condition, commutativity assumption or direction of an action changes.
For a proof, write the hypotheses as a checklist and mark the line at which each one is used. For a computation, state the representation of the input, the arithmetic model, the termination condition and the output invariant. For a classification problem, distinguish existence from uniqueness and distinguish an object from its representation. These separations prevent a correct local calculation from being mistaken for the general result.
A useful worked example should be small enough to inspect completely but rich enough to exercise the main mechanism. Compute the result in two ways where practical: symbolically and by substitution, structurally and numerically, or directly and through a normal form. Then include one near-miss example in which a hypothesis fails. The contrast explains why the theorem is shaped as it is and gives the reader a diagnostic pattern for later problems.
Verification is part of the mathematics. Check domains and codomains, substitute proposed solutions, test identity and zero cases, compare dimensions or cardinalities, and confirm that maps respect the required operations. In numerical work, report precision, conditioning and a residual rather than digits alone. In algorithmic work, separate mathematical correctness from implementation complexity and resource limits.
Step-by-step operating method
- Fix the setting. State the objects, ambient structure, notation and assumptions before manipulating symbols.
- Separate claims. Distinguish definitions, hypotheses, conclusions, equivalent conditions and consequences.
- Choose a method. Select proof, construction, calculation or algorithm according to the question actually asked.
- Work a small case. Use the smallest non-trivial example to expose the mechanism and test edge behaviour.
- Verify independently. Substitute back, check invariants, test boundary cases or use an alternative derivation.
Worked-example protocol
Illustrative method—not a source theorem. Start with a small admissible input and list the definitions it must satisfy. Carry out each transformation on a separate line, citing the property that permits it. Preserve exact values until approximation is necessary. At the end, verify the output against the original definition and one invariant such as dimension, degree, determinant, order, norm or residual. Then alter one hypothesis and observe which step ceases to be valid. This protocol creates a reusable example without inventing a theorem-specific numerical answer.
| Stage | Record | Quality check |
|---|---|---|
| Input | Objects, domain, notation, assumptions | Every symbol is defined |
| Method | Permitted operation or cited result at each step | All hypotheses hold |
| Output | Exact result and representation | Correct type, domain and form |
| Verification | Substitution, invariant or alternative derivation | Independent agreement |
| Boundary test | Zero, identity, degenerate or failed hypothesis | Scope is understood |
Common failure modes and recovery actions
1. Watch for
Using a theorem without checking every hypothesis.
Recovery: Return to the governing definition or requirement and restate the decision in one sentence.
2. Watch for
Treating a suggestive example as a proof of the general case.
Recovery: Separate evidence from assumption, assign an owner and set a date for validation.
3. Watch for
Changing notation or conventions part-way through an argument.
Recovery: Run a small counterexample, boundary test, pilot or independent check before proceeding.
4. Watch for
Hiding a division-by-zero, convergence, finiteness or commutativity assumption.
Recovery: Record the consequence, decision and rationale, then update the controlled baseline.
5. Watch for
Reporting a computed result without a residual, substitution or structural check.
Recovery: Escalate when the issue affects safety, compliance, acceptance, material value or an agreed tolerance.
Review checklist
- Can every symbol be traced to a definition or prior result?
- Which hypothesis does each major step use?
- Does the method cover zero, identity, degenerate and boundary cases?
- Can the conclusion be checked by a second representation or calculation?
- Are mandatory requirements distinguished from recommendations and illustrative values?
- Are sources, assumptions, units, dates and versions recorded closely enough to reproduce the decision?
- Have safety, legal, ethical, stakeholder and operational consequences been considered at the appropriate level?
- Is there a named owner and a trigger for review, escalation, change or retirement?
Questions for deeper application
What is the most important distinction a practitioner must preserve when applying Multiprecision Integer Arithmetic?
Answer with a fact or cited source where available. Where evidence is incomplete, record the assumption, consequence, responsible owner and next validation action.
Which assumption about reduction would change the result most if it proved false?
Answer with a fact or cited source where available. Where evidence is incomplete, record the assumption, consequence, responsible owner and next validation action.
What evidence would allow an independent reviewer to reproduce or challenge the conclusion?
Answer with a fact or cited source where available. Where evidence is incomplete, record the assumption, consequence, responsible owner and next validation action.
Which boundary, exception or failure case has not yet been tested?
Answer with a fact or cited source where available. Where evidence is incomplete, record the assumption, consequence, responsible owner and next validation action.
What must be handed over, monitored or reviewed after the immediate work is complete?
Answer with a fact or cited source where available. Where evidence is incomplete, record the assumption, consequence, responsible owner and next validation action.
Authoritative references and use notes
The sources below were selected as institutional or primary guidance for the broader practice. They support the handbook method; they do not imply that every statement or clause in a source applies to every project. Confirm the current edition, jurisdiction, contract and application before treating any requirement as mandatory.
- MIT OpenCourseWare — Number Theory I — Massachusetts Institute of Technology. Used for algebraic and analytic number theory. Accessed 2026-08-13.
- MIT OpenCourseWare — Algebra I — Massachusetts Institute of Technology. Used for groups, vector spaces, linear transformations and linear groups. Accessed 2026-08-13.
- Page ID
- KV-MATH-0002
- Taxonomy
- ENG-MATH — Engineering / Mathematics
- Collection
- COL-CANT-001
- Topic stream
- CANT-FOUNDATIONS
- Version
- 1.1.0 / content 2026.08
- Last reviewed
- 2026-08-06
