Skip to content

Commit e903760

Browse files
committed
bip93: Report an unused length instead of a failed checksum
When a string's checksum fails and its length is one its application never uses, parse_codex32 now raises the application's length error ("This input has 47 characters. A Bitcoin master-seed backup must have exactly 48, 54, 61, 67, 74, or 127 characters.") instead of "The checksum does not match." A wrong length is the clearer reason, and GUIs and API callers now get it too. The checksum is still verified before any application rule accepts the input; the length only picks the reason for a rejection. The CLI's own rewrite of this message is gone. Claude-Session: https://claude.ai/code/session_01Q1TNm1YeYW8K9W2eBvxmCD
1 parent c02d747 commit e903760

5 files changed

Lines changed: 16 additions & 28 deletions

File tree

‎docs/developer/api.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ absolute size bound. The application parser selects the checksum type from the
1919
generic encoded length and validates the common header before verifying the
2020
outer checksum. Only afterward may the HRP select a registered application
2121
profile. The parser then checks its application length and payload semantics.
22+
When the checksum fails at a length the application never uses, the parser
23+
reports that length instead of the checksum; this only chooses the reason for
24+
a rejection.
2225
An unregistered HRP crosses the boundary only as an opaque base `Secret` or
2326
`Share`, with normalized `hrp`, `profile is None`, and no invented payload meaning.
2427

25-
CLI input diagnostics preserve generic container, header, and length failures.
26-
After a checksum failure, the CLI may report an unsupported profile text length
27-
instead. This diagnostic does not validate the input or change parser ordering;
28-
alignment correction remains available. Frozen-prefix hints do not override a
29-
generic parse-length failure.
28+
CLI input diagnostics preserve generic container, header, and length failures;
29+
alignment correction remains available after a length failure. Frozen-prefix
30+
hints do not override a generic parse-length failure.
3031

3132
### Specification-to-code map
3233

‎src/codex32/_cli_input.py‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
CodexError,
2828
DuplicateShareIndex,
2929
ExistingTargetIndex,
30-
InvalidChecksum,
31-
InvalidLength,
3230
)
3331
from codex32.profiles import Profile, _optional_profile_rules, _profile_rules
3432
from codex32.profiles.ms32 import (
@@ -300,18 +298,7 @@ def _parse(value: str, profiles: tuple[Profile, ...] | None) -> Artifact:
300298
try:
301299
artifact = parse_codex32(value)
302300
except CodexError as error:
303-
message = str(error)
304-
if isinstance(error, InvalidChecksum):
305-
# Parsing has already checked the container and generic length.
306-
# Explain an unsupported profile length without validating input
307-
# or changing the parser's checksum-before-profile boundary.
308-
try:
309-
_profile_rules(value[: value.rfind("1")]).validate_text_length(len(value))
310-
except InvalidLength as length_error:
311-
message = str(length_error)
312-
except CodexError:
313-
pass
314-
raise InputError(message) from error
301+
raise InputError(str(error)) from error
315302
if profiles is not None and artifact.profile not in profiles:
316303
allowed = " or ".join(_profile_rules(profile).label for profile in profiles)
317304
raise InputError(f"This command accepts only {allowed} input.")

‎src/codex32/bip93.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,12 @@ def _decode_codex32(text: str) -> tuple[str, _ProfileRules | None, tuple[int, ..
102102
checksum = _checksum_for_encoded_length(hrp, len(encoded))
103103
body = tuple(encoded[: -checksum.length])
104104
Header._from_symbols(body[:6])
105+
profile_rules = _optional_profile_rules(hrp)
105106
if not bech32_verify_checksum(hrp, encoded, checksum):
107+
if profile_rules is not None:
108+
# A length the application never uses is the clearer reason.
109+
profile_rules.validate_text_length(len(text))
106110
raise InvalidChecksum("The checksum does not match.")
107-
profile_rules = _optional_profile_rules(hrp)
108111
if profile_rules is not None:
109112
profile_rules.validate_text_length(len(text))
110113
profile_rules.validate_payload_length(len(body) - 6)

‎tests/test_cli_lengths.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"This input has 95 characters. A Bitcoin master-seed backup must have "
2222
"exactly 48, 54, 61, 67, 74, or 127 characters."
2323
),
24-
InvalidChecksum,
24+
InvalidLength,
2525
),
2626
),
2727
)

‎tests/test_profiles.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ def test_pr2258_supported_size_vectors(seed_hex: str, text: str) -> None:
7676

7777
@pytest.mark.parametrize("text", FORMERLY_VALID_UNSUPPORTED_MS)
7878
def test_pr2258_rejects_formerly_valid_unsupported_sizes(text: str) -> None:
79-
expected = InvalidChecksum if text in FORMERLY_VALID_UNSUPPORTED_MS[-2:] else InvalidLength
80-
with pytest.raises(expected):
79+
with pytest.raises(InvalidLength):
8180
parse_codex32(text)
8281

8382

@@ -138,17 +137,15 @@ def test_common_header_is_validated_before_checksum() -> None:
138137
("hrp", "payload_length"),
139138
(("ms", 26), ("cl", 52), ("bip39_12w", 27), ("bip39_24w", 53)),
140139
)
141-
def test_checksum_precedes_profile_length(hrp: str, payload_length: int) -> None:
140+
def test_unused_profile_length_is_reported_instead_of_checksum(hrp: str, payload_length: int) -> None:
142141
valid = _oracle_encode(hrp, "0tests" + "q" * payload_length)
143-
with pytest.raises(InvalidChecksum):
142+
with pytest.raises(InvalidLength, match=f"This input has {len(valid) - 1} characters"):
144143
parse_codex32(valid[:-1])
145144

146145

147-
def test_generic_length_and_checksum_precede_ms_length() -> None:
146+
def test_generic_length_precedes_ms_length() -> None:
148147
with pytest.raises(InvalidLength, match="at least 21"):
149148
parse_codex32("ms1")
150-
with pytest.raises(InvalidChecksum):
151-
parse_codex32("ms10tests" + "q" * 40)
152149

153150

154151
def test_valid_profile_length_still_reports_checksum_failure() -> None:

0 commit comments

Comments
 (0)