This is a post in COSIC’s Guide to Crypto! In this series, researchers from COSIC will be informally writing about different concepts in cryptography. Our aim is to introduce the reader to these concepts with explanations, illustrations and examples; so let’s get started!
Introduction to Non-Positional Arithmetic
The decimal representation of numbers, or positional representation in base \(10\), is quite natural for us since it matches our number of fingers. It is also very convenient in every day’s life, especially because it gives us the ability to compare numbers at glance. This representation can be easily generalized in base \(b\), in which case the digits will range between \(0\) and \(b-1\) instead of \(0\) and \(9\). In particular, the binary representation used by computers is nothing but the positional representation in base \(2\) that is with digits being only \(0\) and \(1\).
However, despite its relative simplicity, it is not that easy to perform computations in such representation. A lot of people have painful memories of the hours spent in elementary school performing multiplications of numbers with \(3\), or even more, digits by hand. It is so tedious that it could even be used as a punishment in the same way than copying lines.
One of the reasons of this tediousness is that school-book multiplication has an asymptotic complexity which is quadratic. This means that if you want to multiply one \(n\) digits number with one \(m\) digits number you have to perform asymptotically \(m \times n\) operations on the digits, it is denoted \(\mathcal{O}(m\times n)\). This includes the \(m\times n\) multiplications of digits in the beginning and, assuming \(n < m\), the \(n-1\) additions of numbers with at most \(m+n\) digits in the end.
Fast multiplication algorithms
Now imagine what will happens if you aim at manipulating numbers with several hundreds of digits, which is something quite common in cryptography. As an example, the current recommendations for the RSA cryptosystem are to use keys of at least 3072 bits, that is 925 digits in decimal representation. Therefore a single multiplication between two such numbers would require \(3072^2 = 9.437.184\) elementary multiplications which is quite a lot, even for a computer. The emergence of computers in the 1950’s allowed us to reach numbers of size unthinkable in the past, hence it has been natural to seek for more efficient algorithms.
The first sub-quadratic multiplication algorithm was proposed in 1960 by Anatoly Karatsuba with an asymptotic complexity of \(\mathcal{O}(n^{\log_2(3)} \approx n^{1.585})\) instead of \(\mathcal{O}(n^2)\) previously [1]. With this algorithm, the above example now only requires around \(3072^{1.585} \approx 336.846 \) elementary multiplications. In 1971, Cooley and Tukey conjectured that the asymptotic optimal bound on the complexity of multiplication was \(\mathcal{O}(n\log n)\) while the best algorithm at the time had a complexity in \(\mathcal{O}(n\log n \log\log n)\). In 2019, this conjectured optimal bound has finally been reached by Harvey and Van Der Hoeven [2].
However these algorithms, although more efficient asymptotically, require very large numbers before being more efficient in practice. As a consequence, the most efficient algorithms cannot be used in practice, even in cryptography.
Residue Number System representation
In 1959, Garner proposed an alternative idea: decompose big numbers into several small ones on which the operations will be performed. In this way, instead of performing one big multiplication, one would perform several multiplications on small numbers which will be way cheaper.
To this end, he used the Chinese Remainder Theorem (CRT) so that a number will be represented by its remainders modulo several small numbers \(b_1,\ldots,b_k\) and called this representation the Residue Number System (RNS) [3]. In a nutshell, the CRT allows you to decompose numbers modulo \(B = b_1\times\ \cdots \times b_k\) into their remainders modulo each of the \(b_i\) and to perform operations (additions, subtractions and multiplications) directly on the remainders provided that the moduli \(b_i\) are pairwise coprime. The moduli \(\mathcal{B} = \{b_1,\ldots ,b_k\}\) form what we call the RNS basis.
As an example, consider the RNS basis \( \mathcal{B} = \{47,59,71 \}\), in this basis we have:
\( 1452 = (42 \mod 47, 36 \mod 59, 32 \mod 71)\) \( 3565 = (40 \mod 47, 25 \mod 59, 15 \mod 71). \)
We can now compute the sum of the two numbers:
\(42 + 40 = 82 = 35 \mod 47\)
\(36+25 = 61 = 2 \mod 59 \)
\(32 + 15 = 47 = 47 \mod 71\)
One can check that \(1452+3565 = 5017 = (35 \mod 47, 2 \mod 59, 47 \mod 71)\). From now on, I will omit the moduli in the representation and I will write instead \(5017 = (35, 2, 47)_\mathcal{B}\). Similarly, if one wants to compute the product of these two numbers one will find:
\(42 \times 40 = 1680 = 35 \mod 47\)
\(36 \times 25 = 900 = 15 \mod 59 \)
\(32 \times 15 = 480 = 54 \mod 71\)
And as expected \(1452 \times 3565 = 5.176.380 = (35, 15, 54)_\mathcal{B} \). However in this case we also have \( 57.422 = (35, 15, 54)_\mathcal{B} \), which means that \(5.176.380\) and \(57.422\) have the same representation in the basis \(\mathcal{B}\). This is because \( \mathcal{B} = \{47,59,71\}\) can only represent numbers modulo \(47\times 59\times 71 = 196.883 < 5.176.380 \) and since \(5.176.380 = 57.422 \mod 196.883\) both numbers have the same RNS representation. In order to be able to represent uniquely \(5.176.380\), one needs to increase the size of the number we can represent, for instance by adding \(73\) to the basis \(\mathcal{B}\) to form \( \mathcal{B}’ = \{47,59,71,73 \}\). Thus, in \(\mathcal{B}’\) we are able to represent numbers as big as \(196.883 \times 73 = 14.372.459\).
\( 1452 = (42, 36, 32, 65)_{\mathcal{B}’}\) and \( 3565 = (40, 25, 15, 61)_{\mathcal{B}’} \)
and since \(65\times 61 = 3965 = 23 \mod 73\), we have \(5.176.380 = (35, 15, 54, 23)_{\mathcal{B}’} \) while \(57.422 = (35, 15, 54, 44)_{\mathcal{B}’} \), so both numbers do have a distinct representation in the basis \( \mathcal{B}’\).
Advantages and Limitations of the RNS representation
Back to our RSA example, if we want to perform the product of two \(3072\) bits numbers in RNS, we need to have a basis large enough to represent numbers of \(6144\) bits. In practice the moduli of the RNS basis are chosen to fit in a machine word, for instance \(64\) bits, therefore we would need a basis of \(6144 \div 64 = 96\) moduli of 64 bits. So instead of performing one multiplication between two \(3072\) bits numbers one has to perform \(96\) multiplications of two \(64\) bits numbers, for a total of \(96\times 64^2 = 393.216\) elementary multiplications using schoolbook algorithm, or around \(96\times 64^{1.585} \approx 69.984\) with Karatsuba algorithm.
Note however that since the operations modulo each element of the basis are independent to each others, they can be performed in parallel. Therefore, in theory, the execution time can even be reduced to one multiplication of two \(64\) bits numbers. Note that the gain would be even more important with smaller moduli, for instance \(32\) bits.
However, although RNS representation is very efficient to perform elementary operations, it has also important drawbacks due to its non-positional nature. Indeed it is very easy to compare \( 1452\) and \( 3565\) since we only need to compare their most significant digit which is the right most one. However it is impossible to determine which one of \( (42, 36, 32, 65)_{\mathcal{B}’}\) and \( (40, 25, 15, 61)_{\mathcal{B}’} \) represents the biggest number directly from their representation. Because of this, it is impossible to perform any operation requiring a comparison directly in RNS, in particular one cannot perform non-exact divisions and thus represent, even approximately, numbers which are not integers in this representation.
Despite these limitations it is possible to perform arithmetic modulo a large prime number in RNS, this is why it is still heavily used in the implementation of cryptographic protocols such as RSA, ECDSA especially on embedded devices with constrained capacities such as smart cards. It also benefits to more recent cryptographic primitives using very large parameters such as Fully Homomorphic Encryption (FHE), but details on these kind of implementation will be the subject of another post.
References
[1] A. Karatsuba and Yu Ofman, Multiplication of Many-Digital Numbers by Automatic Computers. Doklady Akad. Nauk SSSR Vol. 145 (1962), p. 293–294
[2] David Harvey, Joris van der Hoeven. Integer multiplication in time O(n log n). 2019. hal-02070778
[3] H. L. Garner, “The Residue Number System,” in IRE Transactions on Electronic Computers, vol. EC-8, no. 2, pp. 140-147, June 1959.


