Track bare-symbol values across a modulus instead of skipping

A line like `x = <formula> mod N` can't be solved for x (it's just a
name for "whatever this reduces to"), so the chain handler gave up on
any modular equation once a bare symbol was involved. That silently
passed over real errors in exactly the shape a CRT reconstruction
produces: `x = 2*77*2+... mod 231` followed by a contradicting
`x ≡ 57+66+126 mod 231` (the latter reduces to a different value mod
231) went unflagged.

Now, when exactly one side of the chain is a bare single letter and
every other side is a plain number, its reduced value is tracked in
known_vars the same way solved variables already are - catching the
mismatch as a contradiction without ever needing to solve for x.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
boyreau 2026-11-08 12:39:32 +01:00
commit 41c98fc1ab

View file

@ -284,11 +284,38 @@ def verify_claim(line):
shown = ', '.join(str(r) for r in red)
return ('wrong', f"{shown} mod {mod}" if mod is not None else shown)
# --- symbolic fallback: one unknown, e.g. "2x + 3 = 7" ----------
# Skipped when the chain carries a modulus: solving `x ≡ 2 mod 5` as
# the equation x = 2 would drop the modulus and report a verification
# that never happened.
# --- bare symbol across a modulus, e.g. "x = 2*77*2+... mod 231" --
# `x` can't be solved for (it's just a name standing for "whatever
# this reduces to"), but when exactly one side is a bare single
# letter and every other side is a plain number, the modular VALUE
# it's being defined as can still be tracked across lines the same
# way known_vars already tracks solved variables. This is what
# catches e.g. a later `x ≡ <different formula> mod 231` that
# reduces to a different value - a real contradiction, even though
# `x` itself is never pinned to one absolute number.
if mod is not None:
bare = [i for i, p in enumerate(parts) if re.fullmatch(r'[a-zA-Z]', p)]
numeric = {}
for i, p in enumerate(parts):
if i in bare:
continue
try:
numeric[i] = eval_expr(p) % mod
except Exception:
pass
if len(bare) == 1 and len(numeric) == len(parts) - 1 and numeric:
var = parts[bare[0]]
reduced = list(numeric.values())
if not all(v == reduced[0] for v in reduced):
shown = ', '.join(str(v) for v in reduced)
return ('wrong', f"{shown} mod {mod}")
value = reduced[0]
prev = known_vars.get(var)
if prev is not None and prev != value:
return ('wrong',
f"{var} = {value} contredit {var} = {prev} (mod {mod})")
known_vars[var] = value
return ('ok', None)
return ('skip', None)
# Prose sentences must not reach sympy: `parse_expr` with implicit
# multiplication turns every letter into a symbol, which is both