Which digits count, the 40-bit scatter, and the EAN-8 case
Note: this page is about retail JAN/EAN codes. The Datach also reads a Bandai-proprietary card format that works by different rules — see 4. Bandai's barcode.
日本語 | English | Français | Español | Italiano | 한국어
The 13 digits of an EAN-13 code are numbered d1 to d13 from the left. For
4902773230126 that means d1=4, d2=9, and so on to d13=6.
Each digit is a value 0–9; its bits are written b0 (least significant) through b3.
So d6=7 gives b0=1, b1=1, b2=1, b3=0.
The first important fact. Only d3 to d12 feed the character and stat calculation.
The two leading digits (the country prefix) and the trailing check digit have no effect on the
result. So 4902773230126 and 4502773230128
(leading digits changed to 45, check digit recomputed) produce exactly the same result.
Players had worked out experimentally that the country prefix "did nothing"; reading the
program confirms it is implemented that way.
The check digit being unused by the calculation does not make it free to set arbitrarily. The reader in front of the calculation does verify it, and a sequence that fails that check errors out there. That is why the example above has its check digit recomputed after the prefix change: a code with only the check digit altered will not be read in the first place. Precisely put, the check digit takes no part in computing the result but is used to validate the scan. This came to light while chasing down mismatches in the recorded scans; the story is on the notes page.
The ten digits d3–d12 are each split into four bits (b0–b3). The resulting 40 bits are redistributed one bit at a time into five internal registers R0–R4, according to a fixed table.
This is what made the system so hard to figure out from the outside. Information from neighbouring digits is flung to distant bit positions, so no amount of experimenting digit by digit ever makes the pattern come out evenly.
The complete table. Columns are the source bit; each cell is the destination register and bit position.
| Digit | b0 goes to | b1 goes to | b2 goes to | b3 goes to |
|---|---|---|---|---|
| d3 | R3 bit7 | R2 bit4 | R4 bit4 | R3 bit4 |
| d4 | R0 bit5 | R2 bit0 | R3 bit6 | R3 bit3 |
| d5 | R1 bit1 | R3 bit0 | R0 bit3 | R4 bit0 |
| d6 | R1 bit3 | R1 bit5 | R0 bit0 | R4 bit3 |
| d7 | R0 bit7 | R0 bit1 | R1 bit7 | R3 bit2 |
| d8 | R1 bit4 | R2 bit3 | R4 bit5 | R2 bit2 |
| d9 | R2 bit7 | R1 bit0 | R2 bit6 | R4 bit6 |
| d10 | R0 bit2 | R4 bit1 | R3 bit1 | R3 bit5 |
| d11 | R1 bit2 | R2 bit1 | R0 bit4 | R4 bit2 |
| d12 | R0 bit6 | R4 bit7 | R1 bit6 | R2 bit5 |
Forty bits map onto forty bits with nothing left over: the mapping is a bijection. No information is discarded, only rearranged.
R0–R4 are then treated as a single 40-bit stream, consumed from the front (the most significant bit of R0). The consumption divides evenly into exactly 40 bits:
| Bits read | Used for | Detail |
|---|---|---|
| 2 | Item slot or character slot | Page 2 |
| 8 | Weighted draw for the character | |
| 3 | Special move level | Page 3 |
| 9 × 3 | HP, BP, DP (nine bits each) |
The 8-digit codes found on cigarettes and small goods can be read too. The game takes the eight digits o1–o8 and folds them back on themselves to build ten effective digits, then runs exactly the same process as for EAN-13. The effective ten are:
[o3, o4, o5, o6, o7, o8, o7, o6, o5, o4]
So an EAN-8 code carries only six digits of information (o3–o8), and o4–o7 are each used twice. A curious consequence is that the EAN-8 check digit (o8) does take part in the calculation, unlike the EAN-13 case. So on an 8-digit code, mistyping o8 alone surfaces as a shifted stat — which is exactly the property that cracked the mismatched records open.