Skip to content

Add Luhn / Luhn Mod N checksum codec#26

Merged
dhondta merged 2 commits intomainfrom
copilot/add-luhn-encoding
Mar 22, 2026
Merged

Add Luhn / Luhn Mod N checksum codec#26
dhondta merged 2 commits intomainfrom
copilot/add-luhn-encoding

Conversation

Copy link
Contributor

Copilot AI commented Mar 22, 2026

Checklist

  • No unrelated changes
  • Codec is new (not already implemented)
  • Tests included (if cannot be automated with tests/test_generated)
  • Documentation (included in the right page in docs/pages/enc)

Description

Implements the Luhn Mod N algorithm — a checksum scheme used to validate credit card numbers, IMEI numbers, and similar identifiers. Encoding appends a check character; decoding verifies it and strips it.

  • src/codext/checksums/luhn.py: Factory-based codec (_luhn_encode(n) / _luhn_decode(n)) parametrized via the add() pattern capture group. Doubling step uses the Mod N formula d = code*2; result = d % N + d // N. Raises LuhnEncodeError / LuhnDecodeError on invalid input/checksum in strict mode.
  • src/codext/checksums/__init__.py: Wires in the new module.

Supported names: luhn, luhn-10, luhn10 (standard mod-10, digits 0-9); luhn-<N> / luhn<N> for N=2..36 using the first N characters of 0-9A-Z.

import codext

codext.encode("7992739871", "luhn")    # → '79927398713'  (classic Luhn test vector)
codext.decode("79927398713", "luhn")   # → '7992739871'

codext.encode("DEADBEEF", "luhn-16")  # → 'DEADBEEFC'
codext.decode("DEADBEEFC", "luhn-16") # → 'DEADBEEF'

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • bitcoinwiki.org
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Add new encoding: luhn</issue_title>
<issue_description>### Description

The Luhn algorithm, also known as the Luhn formula, “modulus 10”, or “mod 10” algorithm, is a simple checksum formula used to validate identification numbers. The algorithm – developed by IBM engineer Hans Peter Luhn – can be used to validate credit card numbers, International Mobile Equipment Identity (IMEI) numbers, and other numerical data. Businesses frequently rely on the Luhn algorithm to increase the accuracy and reliability of their data entry processes, especially when handling sensitive information such as payment data.

Source: Stripe

Reference

https://bitcoinwiki.org/wiki/luhn-mod-n-algorithm</issue_description>

<agent_instructions>Implement this checksum algorithm according to the guideline in docs/pages/howto.md.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: dhondta <9108102+dhondta@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dhondta/python-codext/sessions/2ceb2d9a-a14b-4de4-8712-b44d0df3e8b0
Copilot AI changed the title [WIP] Add new encoding: luhn Add Luhn / Luhn Mod N checksum codec Mar 22, 2026
Copilot AI requested a review from dhondta March 22, 2026 16:04
@dhondta dhondta marked this pull request as ready for review March 22, 2026 16:15
@dhondta dhondta merged commit d84e909 into main Mar 22, 2026
10 checks passed
@dhondta dhondta deleted the copilot/add-luhn-encoding branch March 22, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add new encoding: luhn

2 participants