User:Bob K31416/IBAN

Modulo operation on IBAN
The modulo operation finds the remainder of division of one number (the dividend) by another (the divisor). For the case of an IBAN sanity check, the dividend D is the IBAN after it has been converted to an integer, the divisor is 97, and the remainder is denoted D mod 97. It is often not practical to perform this calculation directly because of the large size of the integer D and the requirement that the arithmetic precision has to be exact. So the calculation is done with programming languages or software packages that support arbitrary-precision arithmetic.

The following is a possible algorithm for calculating D mod 97:


 * 1. Starting from the leftmost digit of D, construct a number using the first 9 digits and call it N.


 * 2. Calculate N mod 97.


 * 3. Construct an integer of 9 digits from the above result (2) followed by the next 7 digits of D. If there weren't 7 digits or more remaining in D but at least one, then construct an integer of less than 9 digits from the above result (2) followed by the remaining digits of D.


 * 3. Repeat steps 2–3 until all the digits of D have been processed.


 * 4. The result of the last calculation in (2) will be D mod 97 = N mod 97.

Example of IBAN check digit test
In this example, the above algorithm for D mod 97 will be applied to 321428291 2345698 7654321 61182 mod 97. (For clarity, the digits are colour-coded.) If the result is one, the IBAN corresponding to D passes the check digit test.


 * a. Construct N from the first 9 digits of D.
 * N = 321428291.


 * b. Calculate N mod 97 = 70.


 * c. Construct a new 9 digit N  from the above result (b) followed by the next 7 digits of D.
 * N = 70 2345698


 * d. Calculate N mod 97 = 70 2345698 mod 97 = 29.


 * e. Construct a new 9 digit N  from the above result (d) followed by the next 7 digits of D.
 * N = 29 7654321


 * f. Calculate N mod 97 = 42 2345698 mod 97 = 24.


 * g. Construct a new N from the above result (f) followed by the remaining 5 digits of D.
 * N = 24 61182


 * h. Calculate N mod 97 = 24 61182 mod 97 = 1.


 * i. From (h), the final result is D mod 97 = 1.

For reasons of precision, the use of integers instead of floating point numbers is recommended. If the number is too long for the software implementation of integers (a (signed) integer of 32 bits or 64 bits represents a maximum of 9 or 18 digits), then the calculation can be split up into consecutive remainder calculations on integers with a maximum length of 9 or 18 digits.

The following example shows the required steps:

1. The remainder of the division of 510007547061111462 by 97 = 1.

2. Calculate the modulo 97 of the first 9 digits of the number.

3. modulo 97 of 510007547 = 74.

4. Construct the next integer of 9 digits from the remainder followed by the next 7 or 8 digits of the number. Calculate modulo 97.

5. modulo 97 of 74 0611114 = 12.

6. Repeat step 4–5 until all the digits of the number have been processed.

7. modulo 97 of 12 62 = 1.

The Javascript program published by UN CEFACT TBG5 which uses a piece-wise approach. The code in this program calculates D mod 97 as follows:

The large integer D is decomposed into its decimal digits di (i = 1, 2, ... n ) which are ordered from right to left. For example, d1 is the rightmost digit of D and dn is the leftmost. Then D mod 97 is calculated using
 * $$ D \, \bmod\,97 = \left( {\sum\limits_{i = 1}^n {d_i \times a_i } } \right)\bmod\,97 \,, $$

where the integers ai are the n elements of an array defined by
 * $$a_i = {10^{(i-1)} } \bmod\,97\,, \qquad  i = 1, 2, ..., n\,. $$

The program generates the array ai by first setting a1 = 1, since
 * $$a_1 = 10^{0} \,\bmod\,97\, = 1\,,$$

and then calculating the remaining elements using the recursive relation
 * $$ a_{i + 1} = \left( {a_i \times 10} \right)\bmod\,97\,.$$

Note in the above expression for D mod 97, that the integers di and ai, and the summation of their products, are much smaller than the integer D. Thus, they are much more amenable to modulo operation calculations that require exact precision arithmetic by a computer that has limited precision integers.

Example of IBAN check digit test
In this example, the above relationship for D mod 97 will be applied to 32142829 12345698765432 1611 82 mod 97. (For clarification, the digits are colour-coded as before.) If the result is one, the IBAN corresponding to D passes the check digit test.

In the table below:


 * Column i are numbers corresponding to the digit positions, counting from right to left.
 * Column di are the values of the digits.
 * Column ai are the progressive values calculated as ai = (ai -1 x 10) mod 97 with the initial item a1 = 1 due to the definition ai = 10(i -1) mod 97.

The values ai are independent of the IBAN being checked, i.e., they form a constant array for all check digit tests on IBANs with a certain length. The value 27 is highlighted to show the propagation of the values of ai from one row to the next.

The final two rows conclude the computation, by checking that the remainder of the sum of the values in column di × ai when divided by 97 equals one, i.e. that
 * $$ D \, \bmod\,97 = \left( {\sum\limits_{i = 1}^{28} {d_i \times a_i } } \right)\bmod\,97 = 1 \,. $$

Note that 4560 (binary 0001 0001 1101 0000) is the largest number appearing in this example, which can easily be accommodated by a 16-bit computer.