Skip to main content

Module checkdigit

Module checkdigit 

Source
Expand description

Check-digit algorithms for securities identifiers.

A check digit is a redundant character appended to an identifier so that a single mistyped or transposed character is detected rather than silently accepted. Five of the identifiers in this crate carry one, and each computes it differently — the scan direction, the letter expansion, and the treatment of two-digit products are all load-bearing and easy to get subtly wrong.

Each function takes the identifier body — the identifier without its check digit(s) — validates that body’s own length and character set defensively, and returns the check digit(s) the governing standard prescribes. A parser verifies a supplied check digit by recomputing it with the matching function and comparing; it never trusts the digit it was given.

§The algorithms

luhn_checksum   Luhn mod-10 over a pure-digit string. Right-to-left,
                rightmost digit weight 2, alternating 2,1,2,...; a
                weighted product of 10 or more is folded to its digit
                sum (equivalently, p - 9).

isin (ISO 6166) Each body character is first expanded — a digit stays a
                digit; a letter becomes the two-digit number 10 + (c -
                'A') — and a Luhn mod-10 is taken over the resulting
                digit string. Parity is assigned AFTER expansion.

cusip (X9.6)    Modulus-10 "double add double". Left-to-right, 1-indexed:
                odd positions weight 1, even positions weight 2. Each
                weighted product is folded to floor(p/10) + (p mod 10).

sedol           Fixed weight vector [1,3,1,7,3,9] applied left-to-right.
                Unlike the others, weighted products are NOT folded.

lei (ISO 7064)  MOD 97-10: expand the body followed by the literal "00",
                read as one integer M; the check digits are 98 - (M mod
                97). The modulus is taken by a streaming recurrence — no
                wide integer is ever formed.

figi (X9.145)   Modulus-10 double add double, but right-to-left with the
                RIGHTMOST character at weight 1 (not 2); every decimal
                digit of each weighted product is summed.

Every example below is a real, well-known instrument whose check digit was recomputed by hand.

§References

  • ISO 6166 — International Securities Identification Number (ISIN).
  • ISO/IEC 7064 — Check character systems (the MOD 97-10 system).
  • ANSI X9.6 — CUSIP, CUSIP Global Services.
  • ANSI X9.145 / Object Management Group — Financial Instrument Global Identifier (FIGI), the OpenFIGI specification.
  • London Stock Exchange — SEDOL Masterfile service description.

Functions§

cusip_check_digit
Computes the CUSIP check digit (ANSI X9.6) of an 8-character body.
figi_check_digit
Computes the FIGI check digit (ANSI X9.145) of an 11-character body.
isin_check_digit
Computes the ISIN check digit (ISO 6166) of an 11-character body.
lei_check_digits
Computes the two LEI check digits (ISO 7064 MOD 97-10) of an 18-character body.
luhn_checksum
Computes the Luhn mod-10 checksum digit of a pure-digit string.
sedol_check_digit
Computes the SEDOL check digit of a 6-character body.