Caloogy Article
中文

Casting Out Nines and Check Digits

Date
May 4, 2025
Series
Cryptography
Reading
15 min
Topics
Check digitCasting out ninesModular arithmeticRing homomorphism

The one-sentence version

Replace an integer with its remainder and compute: the result is the same as "compute first, then take the remainder". This holds because the reduction map π:Z→Z/nZ\pi:\mathbb{Z}\to\mathbb{Z}/n\mathbb{Z} is a ring homomorphism. Casting out nines, barcodes, bank card numbers, the last digit of an ID number and IBANs all work the same way: add a "fingerprint" to the data, so that a copying error will most likely change it. Any error whose difference falls in the homomorphism's kernel nZn\mathbb{Z} leaves the fingerprint unchanged, and so goes undetected.

This article has three layers:

  1. The intuition: casting out nines, casting out elevens, and the errors they can and can't catch
  2. Real-world use: ISBNs, EAN/UPC barcodes, bank card Luhn, ID numbers and IBANs, with brief extensions to CRC and Verhoeff
  3. The mathematics: Z/nZ\mathbb{Z}/n\mathbb{Z}, ring homomorphisms, kernels and why "modulo a prime" catches more, with a worked example for each method

About the numbers in the examples

Every book number, card number, ID number and account number in this article is a public test number, an example from a standards document, or a made-up number, and doesn't correspond to any real person. All examples were checked with Python.


Layer 1: The Intuition

1.1 Casting out nines: checking arithmetic with "digit sums"

In primary school you compute 3847×5263847\times 526 and get 20235222023522. How can you quickly check it?

Casting out nines goes like this:

  1. Add up the digits of each number, repeating until one digit remains (you can also throw away any 99 along the way, which is what "casting out nines" means):
    • 38473847: 3+8+4+7=223+8+4+7=22, 2+2=42+2=4
    • 526526: 5+2+6=135+2+6=13, 1+3=41+3=4
  2. Do the same operation on these two "representatives": 4×4=164\times 4=16, 1+6=71+6=7
  3. Compute the digit sum of the answer: for 20235222023522, 2+0+2+3+5+2+2=162+0+2+3+5+2+2=16, 1+6=71+6=7
  4. Both sides are 77, so it passes the check.

Addition and subtraction work the same way:

Why can a digit sum represent a number? Because "the sum of the digits" and the original number have the same remainder when divided by 99. For example 3847÷9=4273847\div 9=427 remainder 44, exactly the digit sum 22→422\to 4. The reason is that 1010 leaves remainder 11 when divided by 99, so 100,1000,…100,1000,\dots also leave remainder 11, and each digit contributes to the remainder only by "its own size". Layer 3 makes this rigorous.

And "remainders" have a nice property: compute then reduce equals reduce then compute. So if the true answer leaves remainder 77 when divided by 99 but yours leaves 55, your answer must be wrong.

1.2 What casting out nines can and can't catch

Passing the check doesn't mean it's right

Casting out nines can only prove "wrong", never "right". Mismatched remainders mean there is definitely an error; matching remainders only mean it may be right.

What it catches: most single-digit mistakes. For example, writing 20235222023522 as 20235322023532 changes the digit sum to 17→8≠717\to 8\ne 7, which shows up at once.

What it misses:

Error type Example Why it's missed
Two digits swapped 20235222023522 written as 22035222203522 Same digits, same digit sum
00 written as 99 or vice versa 20235222023522 written as 29235222923522 The digit sum changes by exactly 99, the same after casting out
A digit off by a multiple of 99 20235222023522 written as 20325222032522 (90009000 too many) The difference is a multiple of 99
An extra or missing 00 20235222023522 written as 2023522020235220 A trailing 00 doesn't change the digit sum

To sum up: whenever the difference between the wrong and right answers is a multiple of 99, casting out nines can't see it. "Swapping" in the first row is the most common slip, and it falls exactly in this class: swapping adjacent digits a,ba,b gives a difference of 9×(a−b)×10k9\times(a-b)\times 10^k.

1.3 Casting out elevens: using "alternating sums" to fill the gaps

Using the remainder modulo 1111 instead fixes the swap problem. Because 1010 leaves remainder −1-1 modulo 1111 (that is, remainder 1010),

The remainder of a number modulo 1111 == the alternating sum, starting from the units digit, adding then subtracting

For example:

Now look at the two errors casting out nines just missed:

But casting out elevens has blind spots as well: it can't catch errors whose difference is a multiple of 1111, such as swapping two digits one position apart (both have the same sign, so the alternating sum is unchanged). So in practice the two are often used together. An error slips past both checks only if its difference is a multiple of both 99 and 1111, that is, a multiple of 9999.

No

Yes

Computation to check
a × b = c ?

Take each remainder
(digit sum / alternating sum)

Do the same operation on the remainders

Same as
c's remainder?

Definitely wrong

May be right
(misses a difference in 9ℤ)

1.4 From "checking arithmetic" to "checking numbers"

Check digits use this idea in reverse. Casting out nines checks "the result of a computation"; check digits check "a number being copied or typed":

  1. Define a "fingerprint function", such as "a weighted sum, reduced modulo 1111"
  2. When issuing a number, add one digit (the check digit) so that the whole number's fingerprint equals some fixed value, such as 00
  3. When someone copies or scans the number, recompute the fingerprint. If it is wrong, the number is wrong

The two most common errors in human copying are:

Designing a check digit means choosing the "modulus" and "weights" so that all of these two classes are caught. Below we look at how each scheme does on these two points.


Layer 2: Real-World Use

First a summary table, with each row explained afterwards:

Scheme Modulus Weights / rule Single-digit errors Adjacent swaps
Casting out nines 99 All 11 Misses 0↔90\leftrightarrow 9 Misses all
ISBN-10 1111 10,9,…,110,9,\dots,1 Catches all Catches all
ISBN-13 / EAN / UPC 1010 Alternating 1,31,3 Catches all Misses pairs differing by 55 (like 16↔6116\leftrightarrow 61)
Luhn (bank cards) 1010 Every other digit "doubled, then digit sum" Catches all Misses only 09↔9009\leftrightarrow 90
Chinese ID, ISO 7064 MOD 11-2 1111 2k mod 112^k \bmod 11 Catches all Catches all
IBAN, ISO 7064 MOD 97-10 9797 10k mod 9710^k \bmod 97 Catches all Catches all
Verhoeff Dihedral group D5D_5 Permutations Catches all Catches all

The pattern is obvious: schemes with a prime modulus (1111, 9797) do best. Layer 3 explains why.

2.1 ISBN-10: modulo 1111, where the check digit can be X

Book numbers before 2007 had 1010 digits a1a2⋯a10a_1a_2\cdots a_{10}, with the rule

10a1+9a2+8a3+⋯+2a9+1⋅a10≡0(mod11).10a_1+9a_2+8a_3+\cdots+2a_9+1\cdot a_{10}\equiv 0 \pmod{11}.

Because there are 1111 remainders modulo 1111, from 00 to 1010, the check digit a10a_{10} sometimes has to equal 1010, which is written as the Roman numeral X. That is why some older books have numbers ending in X.

Example: ISBN 0-306-40615-2 (an example commonly used by Wikipedia and other sources)

Digit 00 33 00 66 44 00 66 11 55 22
Weight 1010 99 88 77 66 55 44 33 22 11
Product 00 2727 00 4242 2424 00 2424 33 1010 22

The total is =132=12×11≡0(mod11)=132=12\times 11\equiv 0\pmod{11} ✓

An example with X: 0-8044-2957-X. The weighted sum of the first 99 digits is 0+72+0+28+24+10+36+15+14=1990+72+0+28+24+10+36+15+14=199, and 199≡1(mod11)199\equiv 1\pmod{11}. The check digit cc must satisfy 1+c≡01+c\equiv 0, so c=10c=10, written X.

Error detection: all single-digit errors and all swaps (not just adjacent ones) are caught.

2.2 ISBN-13, EAN-13, UPC: modulo 1010, weights 11 and 33

Supermarket barcodes and today's book numbers use this scheme. Reading left to right, the weights alternate 1,3,1,3,…1,3,1,3,\dots:

a1+3a2+a3+3a4+⋯+a13≡0(mod10).a_1+3a_2+a_3+3a_4+\cdots+a_{13}\equiv 0\pmod{10}.

(The 1212-digit UPC-A has weights 3,1,3,1,…3,1,3,1,\dots from the left, on the same principle.)

Example: ISBN-13 978-0-306-40615-7

Digit 99 77 88 00 33 00 66 44 00 66 11 55 77
Weight 11 33 11 33 11 33 11 33 11 33 11 33 11
Product 99 2121 88 00 33 00 66 1212 00 1818 11 1515 77

The total is =100≡0(mod10)=100\equiv 0\pmod{10} ✓. The UPC example 036000291452 passes the same way.

Error detection:

To stay compatible with barcode systems, ISBN-13 gave up ISBN-10's modulus 1111, so its error detection actually got weaker.

2.3 Bank card numbers: the Luhn algorithm

The last digit of Visa, Mastercard, UnionPay and other card numbers is a Luhn check digit (proposed by Hans Peter Luhn of IBM in 1954). The rule:

  1. From right to left, multiply the 2,4,6,…2,4,6,\dotsth digits by 22
  2. If the result has two digits, add them (equivalent to subtracting 99), for example 7×2=14→1+4=57\times 2=14\to 1+4=5
  3. Add all the digits; the total must be ≡0(mod10)\equiv 0\pmod{10}

Example: 4111 1111 1111 1111 (Visa's public test card number, commonly used in payment development, not a real card)

From right to left, the even positions (positions 1,3,5,…,151,3,5,\dots,15 from the left) are doubled:

The total is =8+14+8=30≡0(mod10)=8+14+8=30\equiv 0\pmod{10} ✓

If you mistakenly write the second-to-last digit as 22 (...1121), this digit is doubled and becomes 44. The total becomes 32≢032\not\equiv 0, and it is caught.

Error detection:

Luhn isn't a security measure

A check digit only guards against slips, not forgery. The algorithm is public and anyone can compute a valid check digit, so real security comes from the card networks' databases, CVVs and 3-D Secure.

2.4 Digit 18 of the Chinese resident ID: ISO 7064 MOD 11-2

In the 1818-digit ID number a1a2⋯a18a_1a_2\cdots a_{18}, the first 66 digits are a region code, the next 88 are the date of birth, the next 33 are a sequence code, and the last, a18a_{18}, is the check digit. The rule is

∑i=118ai⋅218−i≡1(mod11),\sum_{i=1}^{18} a_i\cdot 2^{18-i}\equiv 1\pmod{11},

where a18a_{18} is written as X if it needs to equal 1010. This is why some people's ID numbers end in X, for exactly the same reason as ISBN-10.

Reducing 218−i2^{18-i} modulo 1111, the weights of the first 1717 digits are

7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,7,\ 9,\ 10,\ 5,\ 8,\ 4,\ 2,\ 1,\ 6,\ 3,\ 7,\ 9,\ 10,\ 5,\ 8,\ 4,\ 2,

and the last digit has weight 20=12^0=1. In practice, first compute the weighted sum SS of the first 1717 digits, then look it up:

S mod 11S\bmod 11 00 11 22 33 44 55 66 77 88 99 1010
Check digit 11 00 X 99 88 77 66 55 44 33 22

(This table is the result of solving S+c≡1(mod11)S+c\equiv 1\pmod{11}.)

Example: 11010519491231002X. This is the example number from the national standard GB 11643—1999, and doesn't correspond to a real person.

Digit 11 11 00 11 00 55 11 99 44 99 11 22 33 11 00 00 22
Weight 77 99 1010 55 88 44 22 11 66 33 77 99 1010 55 88 44 22
Product 77 99 00 55 00 2020 22 99 2424 2727 77 1818 3030 55 00 00 44

S=167=15×11+2S=167=15\times 11+2, so S≡2S\equiv 2, and the table gives check digit X ✓. Verification: 167+10=177=16×11+1≡1167+10=177=16\times 11+1\equiv 1 ✓.

If the birthday 1231 is copied as 1213 (swapping the adjacent 3,13,1), the computed check digit becomes 99, which doesn't match X, and it is caught.

Error detection: all single-digit errors and all adjacent swaps are caught.

2.5 IBAN: modulo 9797

International bank account numbers (IBANs, used in Europe, the Middle East and elsewhere) have two check digits, under the rule ISO 7064 MOD 97-10:

  1. Move the first 44 characters (the country code and two check digits) to the end
  2. Replace letters with two-digit numbers: A=10, B=11, …, Z=35\mathrm{A}=10,\ \mathrm{B}=11,\ \dots,\ \mathrm{Z}=35
  3. The resulting large integer must be ≡1(mod97)\equiv 1\pmod{97}

Example: GB82 WEST 1234 5698 7654 32. This is a fictional example account commonly used in IBAN standards documents.

N=3214282912345698765432161182N=3214282912345698765432161182

321428291≡70(mod97)702345698≡29(mod97)297654321≡24(mod97)2461182≡1(mod97) ✓\begin{aligned} 321428291 &\equiv 70 \pmod{97}\\ 702345698 &\equiv 29 \pmod{97}\\ 297654321 &\equiv 24 \pmod{97}\\ 2461182 &\equiv 1 \pmod{97}\ \checkmark \end{aligned}

This segmented method also works because reduction is a ring homomorphism, as Layer 3 explains.

To generate the check digits, first compute N0N_0 with the check digits set to 00; the check digits are then 98−(N0 mod 97)98-(N_0\bmod 97). In this example the result is exactly 8282.

Why 9797: 9797 is the largest prime below 100100. Two check digits can represent at most 100100 values, 9797 uses nearly all of them, and being prime it detects the most errors. IBAN catches all single-character errors and all adjacent digit swaps, with an actual miss rate of about 1/97≈1%1/97\approx 1\%.

2.6 Extensions: CRC and Verhoeff

CRC (cyclic redundancy check): replacing "modulo a number" with "modulo a polynomial"

Ethernet frames, ZIP archives and PNG images all use CRC. It treats a bit string bm−1⋯b1b0b_{m-1}\cdots b_1b_0 as the polynomial ∑bixi\sum b_i x^i in F2[x]\mathbb{F}_2[x] (F2=Z/2Z\mathbb{F}_2=\mathbb{Z}/2\mathbb{Z}), and takes the remainder modulo a fixed generator polynomial g(x)g(x): F2[x] ⟶ F2[x]/(g(x)).\mathbb{F}_2[x]\ \longrightarrow\ \mathbb{F}_2[x]/(g(x)). This is also a ring homomorphism, with kernel the ideal (g)(g). An error is caught as long as the error polynomial e(x)e(x) isn't divisible by g(x)g(x). A good choice of gg (such as the degree-3232 polynomial of CRC-32) guarantees catching all consecutive errors (burst errors) of length ≤32\le 32. This is the same pattern as casting out nines, with Z\mathbb{Z} replaced by F2[x]\mathbb{F}_2[x].

The Verhoeff algorithm: solving both error classes at once with the non-commutative group D5D_5

The schemes above all compute weighted sums in the commutative ring Z/nZ\mathbb{Z}/n\mathbb{Z}. With a modulus of 1010, some errors are always missed: EAN misses 55 pairs, Luhn misses 11. It can be proved that a "weighted sum" in Z/10Z\mathbb{Z}/10\mathbb{Z} can't catch all single errors and all adjacent swaps at once.

Jacobus Verhoeff's 1969 approach: map the 1010 digits to the 1010 elements of the dihedral group D5D_5 (the 1010 symmetries of a regular pentagon: 55 rotations plus 55 reflections), together with a carefully designed permutation σ\sigma. The rule is σ1(a1)∗σ2(a2)∗⋯∗σn(an)=e,\sigma^{1}(a_1) * \sigma^{2}(a_2) * \cdots * \sigma^{n}(a_n) = e, where ∗* is multiplication in D5D_5 (not commutative). Because xy≠yxxy\ne yx, swapping adjacent digits generally changes the product, so a single decimal check digit is enough to catch all single errors and adjacent swaps.

Example: the Verhoeff check digit of the string 236 is 33. 2363 passes the check, while changing it to 2633 fails. India's Aadhaar identity number uses this algorithm.


Layer 3: The Mathematics

3.1 Congruence and Z/nZ\mathbb{Z}/n\mathbb{Z}

Fix a positive integer nn. If n∣a−bn\mid a-b, we say a≡b(modn)a\equiv b\pmod n. This is an equivalence relation, and the equivalence class of aa is written

aˉ=a+nZ={…,a−n, a, a+n, … }.\bar a = a+n\mathbb{Z}=\{\dots,a-n,\ a,\ a+n,\ \dots\}.

All the classes form Z/nZ={0ˉ,1ˉ,…,n−1‾}\mathbb{Z}/n\mathbb{Z}=\{\bar 0,\bar 1,\dots,\overline{n-1}\}, with operations defined by

aˉ+bˉ=a+b‾,aˉ bˉ=ab‾.\bar a+\bar b=\overline{a+b},\qquad \bar a\,\bar b=\overline{ab}.

Well-definedness

We need the operations above not to depend on the choice of representatives: if a≡a′a\equiv a' and b≡b′b\equiv b', then ab−a′b′=a(b−b′)+b′(a−a′)∈nZ,ab-a'b' = a(b-b')+b'(a-a')\in n\mathbb{Z}, because nZn\mathbb{Z} is an ideal: it is closed under addition and absorbs multiplication (r⋅nZ⊆nZr\cdot n\mathbb{Z}\subseteq n\mathbb{Z}). This is exactly why a quotient ring R/IR/I can define multiplication.

3.2 The reduction map is a ring homomorphism

Define

π:Z→Z/nZ,π(a)=aˉ.\pi:\mathbb{Z}\to\mathbb{Z}/n\mathbb{Z},\qquad \pi(a)=\bar a.

The operations defined in 3.1 give directly

π(a+b)=π(a)+π(b),π(ab)=π(a) π(b),π(1)=1ˉ,\pi(a+b)=\pi(a)+\pi(b),\qquad \pi(ab)=\pi(a)\,\pi(b),\qquad \pi(1)=\bar 1,

so π\pi is a (surjective) ring homomorphism. In words, "compute then reduce == reduce then compute", that is,

ab‾=aˉ bˉ.\overline{ab}=\bar a\,\bar b .

Casting out nines is the case n=9n=9: to check c=?abc\stackrel{?}{=}ab, just check whether cˉ=?aˉ bˉ\bar c\stackrel{?}{=}\bar a\,\bar b holds in Z/9Z\mathbb{Z}/9\mathbb{Z}. Z/9Z\mathbb{Z}/9\mathbb{Z} has only 99 elements, so very little computation is needed.

Mind the direction of the logic:

c=ab ⟹ π(c)=π(a)π(b),c=ab\ \Longrightarrow\ \pi(c)=\pi(a)\pi(b),

but the converse fails. So casting out nines can only refute, never confirm.

The example from 1.1 in the language of homomorphisms

π(3847)=4ˉ\pi(3847)=\bar 4, π(526)=4ˉ\pi(526)=\bar 4, 4ˉ⋅4ˉ=16‾=7ˉ\bar 4\cdot\bar 4=\overline{16}=\bar 7, π(2023522)=7ˉ\pi(2023522)=\bar 7. The two sides are equal, so the homomorphism doesn't refute the equation.

The segmented reduction for IBANs is also an application of the homomorphism property. Let N=A⋅10k+BN=A\cdot 10^k+B. Then

π(N)=π(A) π(10)k+π(B),\pi(N)=\pi(A)\,\pi(10)^k+\pi(B),

so you can first replace AA with its remainder rr and then reduce r⋅10k+Br\cdot 10^k+B, and the result is unchanged.

3.3 Why "10≡110\equiv 1" gives the digit sum

Let N=∑k=0mdk 10kN=\sum_{k=0}^{m} d_k\,10^k, where dk∈{0,…,9}d_k\in\{0,\dots,9\} are the digits. With the homomorphism π:Z→Z/9Z\pi:\mathbb{Z}\to\mathbb{Z}/9\mathbb{Z},

π(N)=∑k=0mπ(dk) π(10)k=∑k=0mdˉk⋅1ˉ k=∑kdk‾,\pi(N)=\sum_{k=0}^{m}\pi(d_k)\,\pi(10)^k=\sum_{k=0}^{m}\bar d_k\cdot\bar 1^{\,k}=\overline{\textstyle\sum_k d_k},

because 10=9+110=9+1, so π(10)=1ˉ\pi(10)=\bar 1. In other words,

N≡d0+d1+⋯+dm(mod9).N\equiv d_0+d_1+\cdots+d_m\pmod 9 .

The same goes modulo 33, because 10≡1(mod3)10\equiv 1\pmod 3. This is why "a number is divisible by 33 if its digit sum is".

Modulo 1111: 10=11−110=11-1, so π(10)=−1‾\pi(10)=\overline{-1}, and then

N≡∑k=0m(−1)kdk=d0−d1+d2−⋯(mod11),N\equiv\sum_{k=0}^{m}(-1)^k d_k = d_0-d_1+d_2-\cdots\pmod{11},

which is the alternating sum.

By hand

N=2023522N=2023522, where d0=2,d1=2,d2=5,d3=3,d4=2,d5=0,d6=2d_0=2, d_1=2, d_2=5, d_3=3, d_4=2, d_5=0, d_6=2: ∑kdk=16≡7(mod9),∑k(−1)kdk=2−2+5−3+2−0+2=6(mod11).\sum_k d_k=16\equiv 7\pmod 9,\qquad \sum_k(-1)^kd_k=2-2+5-3+2-0+2=6\pmod{11}. Indeed 2023522=224835×9+7=183956×11+62023522=224835\times 9+7=183956\times 11+6 ✓

3.4 The kernel is 9Z9\mathbb{Z}: which errors go undetected

ker⁡π={a∈Z: π(a)=0ˉ}=9Z.\ker\pi=\{a\in\mathbb{Z}:\ \pi(a)=\bar 0\}=9\mathbb{Z}.

Let the correct answer be cc, the written answer c′c', and the error e=c′−ce=c'-c. Then

π(c′)=π(c)  ⟺  π(e)=0ˉ  ⟺  e∈ker⁡π=9Z.\pi(c')=\pi(c)\iff \pi(e)=\bar 0\iff e\in\ker\pi=9\mathbb{Z}.

So an error goes undetected if and only if it falls in the kernel. This is the most basic property of a homomorphism: π(x)=π(y)  ⟺  x−y∈ker⁡π\pi(x)=\pi(y)\iff x-y\in\ker\pi. By the first isomorphism theorem, Z/ker⁡π≅Z/9Z\mathbb{Z}/\ker\pi\cong\mathbb{Z}/9\mathbb{Z}, and differences in the kernel are completely invisible in the image.

Going through the table in 1.2:

3.5 The general framework for weighted check digits, and why primes matter

Every "weighted sum" check digit can be written as

φ(a1,…,am)=∑i=1mwi ai ∈ Z/nZ,valid number  ⟺  φ=tˉ.\varphi(a_1,\dots,a_m)=\sum_{i=1}^{m} w_i\,a_i\ \in\ \mathbb{Z}/n\mathbb{Z},\qquad \text{valid number}\iff\varphi=\bar t .

φ\varphi is a module homomorphism (linear map) from (Z/nZ)m(\mathbb{Z}/n\mathbb{Z})^m to Z/nZ\mathbb{Z}/n\mathbb{Z}, so the analysis is the same as in 3.4: an error vector ee goes undetected if and only if e∈ker⁡φe\in\ker\varphi.

(1) Single-digit errors: digit ii changes from aa to bb, and φ\varphi changes by wi(b−a)w_i(b-a).

(2) Swapping digits i,ji,j: if they were a,ba,b, the change is

wi(b−a)+wj(a−b)=(wi−wj)(b−a).w_i(b-a)+w_j(a-b)=(w_i-w_j)(b-a).

Because digits satisfy 0≤a,b≤90\le a,b\le 9, when a≠ba\ne b we have b−a∈{±1,…,±9}b-a\in\{\pm1,\dots,\pm9\}.

Theorem (error detection modulo a prime)

Let n=pn=p be prime with p>9p>9 (for example 1111 or 9797).

  1. If every weight wi≢0(modp)w_i\not\equiv 0\pmod p, then all single-digit errors are caught.
  2. If wi≢wj(modp)w_i\not\equiv w_j\pmod p, then all swaps of digits i,ji,j are caught.

Proof: pp is prime, so Z/pZ\mathbb{Z}/p\mathbb{Z} is a field and has no zero divisors: xy=0⇒x=0xy=0\Rightarrow x=0 or y=0y=0. Since p>9p>9, 0<∣b−a∣≤9<p0<|b-a|\le 9<p, so b−a‾≠0ˉ\overline{b-a}\ne\bar 0, that is, b−a‾\overline{b-a} is invertible.

  • (1) wiw_i and b−ab-a are both invertible, so the product wi(b−a)≠0ˉw_i(b-a)\ne\bar 0.
  • (2) wi−wjw_i-w_j and b−ab-a are both invertible, so the product (wi−wj)(b−a)≠0ˉ(w_i-w_j)(b-a)\ne\bar 0. ■\blacksquare

Checking each in turn:

Counterexamples: when the modulus isn't prime, zero divisors cause misses

In one sentence

The point of check digit design is to make the weights wiw_i and the weight differences wi−wjw_i-w_j all units (invertible elements) in Z/nZ\mathbb{Z}/n\mathbb{Z}. When nn is prime, Z/nZ\mathbb{Z}/n\mathbb{Z} is a field and every nonzero element is a unit, so this is easy. When n=10n=10 there are zero divisors 22 and 55, which always leave holes.

3.6 Why Luhn misses only 09↔9009\leftrightarrow 90

Luhn isn't a pure weighted sum. Let the "double and take digit sum" function be

δ(x)={2x,0≤x≤4,2x−9,5≤x≤9,\delta(x)=\begin{cases}2x, & 0\le x\le 4,\\ 2x-9, & 5\le x\le 9,\end{cases}

with this table of values:

xx 00 11 22 33 44 55 66 77 88 99
δ(x)\delta(x) 00 22 44 66 88 11 33 55 77 99

(Note that δ(x)≡2x(mod9)\delta(x)\equiv 2x\pmod 9, because a digit sum doesn't change the remainder modulo 99; 3.3 is at work again.)

Single errors: in an undoubled position, the change is b−a≢0(mod10)b-a\not\equiv 0\pmod{10}; in a doubled position, the change is δ(b)−δ(a)\delta(b)-\delta(a). The table shows δ\delta is a permutation (injective) of {0,…,9}\{0,\dots,9\}, so δ(a)≠δ(b)\delta(a)\ne\delta(b) when a≠ba\ne b, and both lie between 00 and 99, so their difference can't be a multiple of 1010. Therefore all single errors are caught.

Adjacent swaps: of two adjacent digits, exactly one is doubled. Say the doubled digit was aa and the plain one bb; after swapping, the doubled digit is bb and the plain one aa. The total changes by

(δ(b)+a)−(δ(a)+b)=f(b)−f(a),f(x):=δ(x)−x.\big(\delta(b)+a\big)-\big(\delta(a)+b\big)=f(b)-f(a),\qquad f(x):=\delta(x)-x .

Computing f(x) mod 10f(x)\bmod 10:

xx 00 11 22 33 44 55 66 77 88 99
f(x)=δ(x)−xf(x)=\delta(x)-x 00 11 22 33 44 −4-4 −3-3 −2-2 −1-1 00
f(x) mod 10f(x)\bmod 10 00 11 22 33 44 66 77 88 99 00

In Z/10Z\mathbb{Z}/10\mathbb{Z}, ff has only one "collision": f(0)=f(9)=0f(0)=f(9)=0. So when a≠ba\ne b,

f(a)≡f(b)(mod10)  ⟺  {a,b}={0,9},f(a)\equiv f(b)\pmod{10}\iff\{a,b\}=\{0,9\},

that is, Luhn misses only the adjacent swap 09↔9009\leftrightarrow 90. Intuitively, 00 doubled is still 00, and 99 doubled is 18→918\to 9; both are fixed points of δ\delta, so the total is the same whichever digit is doubled.

By hand: the test card number 4111 1111 1111 11114111\ 1111\ 1111\ 1111

Number the positions i=1,…,16i=1,\dots,16 from right to left, doubling even ii: ∑i oddai=8⋅1=8,∑i evenδ(ai)=δ(4)+7 δ(1)=8+14=22,\sum_{i\ \text{odd}} a_i=8\cdot 1=8,\qquad \sum_{i\ \text{even}}\delta(a_i)=\delta(4)+7\,\delta(1)=8+14=22, for a total of 30≡0(mod10)30\equiv 0\pmod{10} ✓. A counterexample (made-up numbers): 4090 0000 0000 00034090\ 0000\ 0000\ 0003 and 4900 0000 0000 00034900\ 0000\ 0000\ 0003 both pass, because f(0)=f(9)f(0)=f(9).

3.7 The big picture

ring/module homomorphism π

kernel = n ℤ (or (g))

Yes: ℤ/pℤ is a field
nonzero elements are invertible

No: zero divisors
like 2·5=0 in ℤ/10ℤ

Switch to the non-commutative group D₅

ℤ (or (ℤ/nℤ)^m, 𝔽₂[x])

ℤ/nℤ (or 𝔽₂[x]/(g))

Error falls in the kernel
⇒ undetected

Is n prime?

Single errors + swaps
all caught
(ISBN-10, Chinese ID, IBAN)

Always some holes
(EAN misses difference 5, Luhn misses 09↔90)

Verhoeff: catches all

How it maps to abstract algebra

Algebra concept Role in this article
Ring homomorphism π:Z→Z/nZ\pi:\mathbb{Z}\to\mathbb{Z}/n\mathbb{Z} "Reduce then compute == compute then reduce", the basis of casting out nines
Kernel ker⁡π=nZ\ker\pi=n\mathbb{Z} is an ideal The undetected errors are exactly those falling in the kernel
Well-definedness of the quotient ring Z/nZ\mathbb{Z}/n\mathbb{Z} Segmented reduction, digit sums and alternating sums all work
Z/pZ\mathbb{Z}/p\mathbb{Z} is a field (pp prime) Nonzero elements are invertible with no zero divisors, so ISBN-10, Chinese ID and IBAN catch everything
Zero divisors (2ˉ⋅5ˉ=0ˉ\bar2\cdot\bar5=\bar0) EAN misses swaps with difference 55
Polynomial quotient ring F2[x]/(g)\mathbb{F}_2[x]/(g) CRC
Non-commutative group D5D_5 The Verhoeff check