Residue number system
A residue number system or residue numeral system is a numeral system representing integers by their values modulo several pairwise coprime integers called the moduli. This representation is allowed by the Chinese remainder theorem, which asserts that, if is the product of the moduli, there is, in an interval of length, exactly one integer having any given set of modular values.
Using a residue numeral system for arithmetic operations is also called multi-modular arithmetic.
Multi-modular arithmetic is widely used for computation with large integers, typically in linear algebra, because it provides faster computation than with the usual numeral systems, even when the time for converting between numeral systems is taken into account. Other applications of multi-modular arithmetic include polynomial greatest common divisor, Gröbner basis computation and cryptography.
Definition
A residue numeral system is defined by a set of integerscalled the moduli, which are generally supposed to be pairwise coprime. Residue number systems have been defined for non-coprime moduli, but are not commonly used because of worse properties.
An integer is represented in the residue numeral system by the family of its remainders
under Euclidean division by the moduli. That is
and
for every
Let be the product of all the. Two integers whose difference is a multiple of have the same representation in the residue numeral system defined by the s. More precisely, the Chinese remainder theorem asserts that each of the different sets of possible residues represents exactly one residue class modulo. That is, each set of residues represents exactly one integer in the interval. For signed numbers, the dynamic range is
.
Arithmetic operations
For adding, subtracting and multiplying numbers represented in a residue number system, it suffices to perform the same modular operation on each pair of residues. More precisely, ifis the list of moduli, the sum of the integers and, respectively represented by the residues and is the integer represented by such that
for . Subtraction and multiplication are defined similarly.
For a succession of operations, it is not necessary to apply the modulo operation at each step. It may be applied at the end of the computation, or, during the computation, for avoiding overflow of hardware operations.
However, operations such as magnitude comparison, sign computation, overflow detection, scaling, and division are difficult to perform in a residue number system.
Comparison
If two integers are equal, then all their residues are equal. Conversely, if all residues are equal, then the two integers are equal or differ by a multiple of. It follows that testing equality is easy.At the opposite, testing inequalities is difficult and, usually, requires to convert integers to the standard representation. As a consequence, this representation of numbers is not suitable for algorithms using inequality tests, such as Euclidean division and Euclidean algorithm.
Division
Division in residue numeral systems is problematic. On the other hand, if is coprime with thencan be easily calculated by
where is multiplicative inverse of modulo, and is multiplicative inverse of modulo.