Luhn or Verhoeff algorithm for credit card numbers
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The Luhn and Verhoeff algorithms are both widely used methods for validating the integrity of numerical identifiers, most notably credit card numbers. Their primary purpose is to catch common errors in a sequence of numbers, ensuring that the numbers were not entered incorrectly. While they might not be foolproof against deliberate fraud, they serve as an important line of defense against accidental errors.
Luhn Algorithm
Overview
The Luhn Algorithm, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula created by Hans Peter Luhn, a scientist at IBM. It is not only used for credit card numbers but also for various other numbering systems.
Steps
The Luhn Algorithm checks for validity in the following steps:
- Reverse the Order: Reverse the digits of the number.
- Double Every Other Digit: Starting from the first digit (now in reversed order), double every second digit. If doubling a digit results in a two-digit number (i.e., 10 or above), add the digits of the result together (e.g., 12 becomes 1 + 2 = 3).
- Sum all the digits: Add all individual digits together.
- Modulo 10 Check: If the total modulo 10 is 0 (i.e., it is divisible by 10), then the number is valid according to the Luhn check.
Example
Consider the card number: 4532 7581 2345 6789.
- Reverse the digits: 9876 5432 1857 2354
- Doubling every second digit: (12, 9, 82, 7, 62, 5, 42, 3, 22, 2, 12, 8, 52, 7, 32, 4)
- Results in: 2, 9, 16, 7, 12, 5, 8, 3, 4, 2, 2, 8, 10, 7, 6, 4
- Applying digit sum for numbers over 9: 2, 9, 7, 7, 3, 5, 8, 3, 4, 2, 2, 8, 1, 7, 6, 4
- Sum all values: 78
- Check modulo 10: 78 % 10 = 8 (Not valid according to Luhn.)
Key Points of Luhn's Algorithm
| Feature | Description |
| Type | Checksum algorithm |
| Operation | Modulus 10 (mod 10) operations |
| Errors Detected | Single-digit errors, simple transposition errors |
| Applications | Credit card numbers, IMEI numbers, other identifiers |
| Limitation | Not foolproof against all types of fraud |
Verhoeff Algorithm
Overview
The Verhoeff Algorithm is another checksum formula that was proposed by the Dutch mathematician Jacobus Verhoeff. It is more robust than Luhn’s algorithm, as it can detect more types of errors including transposition errors of adjacent digits.
Steps
The Verhoeff algorithm uses a dihedral group D5 for its error detection process, characterized by:
- A multiplication table,
- A permutation table,
- An inversion table.
- Use of Tables: The algorithm involves using pre-computed tables for multiplication and permutation.
- Calculate Checksum: A digit is appended to make the total divisible by 11, conforming to modular arithmetic involving the tables.
- Correction: The main advantage lies in higher error correction capability compared to Luhn's.
Technical Implementation
Let's explore each element further:
- Multiplication Table: A 10x10 table used to multiply digits.
- Permutation Table: Determines the position influence for each digit based on its position in the number.
- Inversion Table: Used to determine the check digit that makes the total verifiable as a modulo 11.
Example
Please note that due to complexity, an elaborate manual calculation is not presented here, but the steps involve using the above tables iteratively.
Key Points of Verhoeff's Algorithm
| Feature | Description |
| Type | Checksum algorithm with dihedral group D5 |
| Operation | Modular arithmetic with special tables |
| Errors Detected | Single-digit errors, transpositions, twin errors, and more complex mistakes |
| Applications | Primarily theoretical and less used practically compared to Luhn for credit cards |
| Limitation | More computationally intensive compared to Luhn |
Conclusion
Both algorithms serve a critical role in ensuring the integrity of numerical data entry processes. While Luhn is widely adopted for its simplicity and speed, Verhoeff offers a much higher error detection capability at the cost of computational complexity. Together, they illustrate the importance of checksum algorithms in maintaining data integrity in financial and telecommunication industries.

