Skip to content

Latest commit

 

History

History

arithmetic

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Arithmetic experiments

This directory develops an exact arithmetic baseline before any zk-friendly replacement is admitted. The current experiment compares x87 result bits, exception-status bits, and comparison condition codes against Berkeley SoftFloat across basic arithmetic and the dominant store/round/conversion/ branch boundaries. A separate local probe executes the exact __ftol2 body extracted from a verified retail image. Both are deterministic counterexample searches, not proofs of arithmetic equivalence.

Pinned SoftFloat oracle

The experiment uses Berkeley SoftFloat Release 3e from the ucb-bar/berkeley-softfloat-3 repository at commit:

f74b1e48110ac3a27dd49b787d164e55e42d81d1

That commit is dated 26 January 2018 and titled Release 3e. SoftFloat source is not vendored into zkTH06. Its checkout stays under the ignored repos/ directory, and the runner exports the pinned Git object into a temporary directory before compiling. Working-tree modifications and build products are therefore not probe inputs.

The upstream COPYING.txt is a three-clause BSD-style license with SHA-256:

145ea96b4a4a04a1a7738d2a2bf9e830f861971e69606187b018d9e8fc0b95c7

The runner additionally checks hashes for the selected platform header, public API header, and state implementation. It compiles only the 37 upstream translation units needed by this experiment, using the 8086 specialization.

Compared semantics

softfloat_probe.c first evaluates add, sub, mul, div, and sqrt in two ways:

  1. inline x87 instructions under control word 0x027f; and
  2. SoftFloat extF80_* operations with round-to-nearest-even and extF80_roundingPrecision = 64.

SoftFloat documents 64 here as precision equivalent to float64_t; that is the 53-bit significand setting corresponding to x87 precision-control value 10. Results retain the extended exponent range. The comparison checks the raw 16-bit sign/exponent and 64-bit significand after an x87 fstpt store.

The same probe then checks five instruction boundaries represented heavily in the audited executable:

  • fstp to binary32 and binary64;
  • frndint; and
  • fistp to signed 32-bit and 64-bit integers.

Each boundary is run under both 0x027f (nearest-even) and 0x0e7f (toward-zero with the same PC53 precision and exception masks). The latter is the control word produced when the audited D3DX F2IBegin helpers set the RC bits. SoftFloat uses softfloat_round_near_even and softfloat_round_minMag, respectively.

For every tuple, the probe also compares x87 status bits 0--5: invalid, denormal operand, divide-by-zero, overflow, underflow, and inexact. SoftFloat's five IEEE-style flags map directly to all except denormal operand. That sixth bit is derived separately from the instruction and operand class. This is instruction-specific: frndint can signal it, while fistp and fstp do not; invalid square root and zero-divide also take priority for the tested arithmetic forms. An earlier naive "any subnormal input" rule failed immediately on a subnormal dividend divided by zero, so it was not retained. The per-instruction rules are checked against Intel's Instruction Set Reference, Volume 2A and AMD's x87 instruction manual.

Finally, the probe mirrors the only comparison forms found in mapped game code: fcomp m32fp and fcomp m64fp. It compares C0, C1, C2, and C3 together with exception bits 0--5. The SoftFloat relation is built from signaling equality and less-than so every NaN produces x87's unordered relation and invalid exception. Fixed cases include infinities, quiet and signaling NaNs, signed zeros, and combinations in which invalid takes priority over a subnormal operand. Random comparison cases remain canonical and finite.

Two deterministic input classes are used for every operation:

  • finite binary32 bit patterns converted exactly to extFloat80_t; and
  • canonical finite extended values with the explicit integer bit consistent with the exponent and the low eleven significand bits zero, including boundary-heavy sampling across the full extended exponent range.

Fixed cases cover signed zero, subnormal boundaries, normal boundaries, values around one, signed integer-conversion limits, and the largest finite values. Random inputs exclude NaN and infinity encodings, although operations such as zero divided by zero and square root of a negative finite value can still produce a NaN result. Comparison-only fixed cases additionally cover canonical infinities and both NaN classes.

Reproduction

Create the ignored upstream checkout once:

git clone https://github.com/ucb-bar/berkeley-softfloat-3.git \
  repos/berkeley-softfloat-3
git -C repos/berkeley-softfloat-3 checkout --detach \
  f74b1e48110ac3a27dd49b787d164e55e42d81d1

Then run one million pseudorandom cases per operation and input class:

python3 tools/run_softfloat_probe.py --cases 1000000

The runner requires an x86-64 host, Git, and a C11 compiler. It records the pinned revision, probe hash, architecture, CPU model, and compiler identity. Compilation and execution take place in a temporary directory. This external-source hardware experiment is intentionally not part of the lightweight repository CI; CI checks the runner's Python syntax but does not fetch SoftFloat or compile the C probe.

On the initial AMD EPYC 9654 host with GCC 11.4.0, probe SHA-256 b062a031b5866b7e86514db17d162ecbdf9398b0a5d0c5530dfcf6c4889ffe71, the command produced:

Family binary32-derived tuples canonical PC53 ext80 tuples Mismatches
five basic operations 5,000,798 5,001,314 0
five boundaries, two RC profiles 10,000,140 10,000,330 0

It also matched 2,000,110 fcomp m32fp and 2,000,110 fcomp m64fp condition/status tuples. The relation totals were 2,001,320 greater, 1,998,808 less, 16 equal, and 76 unordered. The deliberately small equal/unordered counts come from fixed cases; random pairs almost never compare equal and exclude NaNs.

In total, 34,002,802 result/condition/exception tuples matched. The run observed each of the six exception bits in the basic-operation campaign:

Campaign Invalid Denormal Divide-by-zero Overflow Underflow Inexact
basic 1,001,228 88,466 56 255,746 265,276 7,547,221
boundaries 3,150,469 20,789 0 1,815,764 1,853,992 10,475,625
comparisons 76 29,682 0 0 0 0

Counts overlap when one operation sets multiple flags; divide-by-zero does not apply to the selected boundary instructions. The measured run used the deterministic seed embedded in the probe and completed in 3.6 seconds. These results are specific to that processor, compiler, source revision, probe hash, selected operations, and input generator.

Exact __ftol2 extraction experiment

The static audit identifies 77 calls from mapped game functions to the helper at virtual address 0x0045ba78. The instruction immediately before each call is x87: 60 fld, seven fmul, four fdiv, two fadd, two fsubp, and two fsubr. A bounded straight-line use scan observes EAX at all 77 sites and EDX at none; 75 sites observe all of EAX and two observe only AL. The scan stops at branches or later calls and leaves EDX live at 42 stopping points, so this is a bounded syntactic fact rather than whole-control-flow dead-result analysis or a dynamic range proof.

run_ftol2_probe.py verifies the retail executable hash, resolves the helper through the PE section table, and checks the 117-byte body against SHA-256 5333b186c02836974c6f792303aeb2c00d856316b93ccbbe65f51def6ae661b4. It places those bytes only in a temporary directory and links ftol2_harness.S as a freestanding i386 process. No helper byte, executable fragment, or generated object is retained or redistributed.

Run the local experiment with:

python3 tools/run_ftol2_probe.py \
  local/original-th06/東方紅魔郷.exe --cases 1000000

The local runner requires GNU as, GNU ld, and Linux i386 execution support; it does not require 32-bit libc. Its PE addressing and dyadic decoder have a proprietary-input-free unit test:

python3 tests/test_ftol2_probe.py

The independent Python model interprets each canonical finite ext80 input as an exact dyadic rational and truncates it toward zero. Inputs are limited to the signed-i64 representable domain and combine exact binary32-derived values, canonical PC53 extended values, signed/subnormal/integer boundaries, and the exact -2^63 endpoint. On the initial host, all 1,000,014 complete EDX:EAX results matched and every call returned the x87 stack to empty; 908,273 runs left the inexact status bit set.

A second campaign exercised the observation made by ECL variable dispatch: exact quarter-step boundaries around -10025..-10001, random PC53 values densely covering that interval, infinities, both NaN classes, very large finite values, a pseudo-denormal, and an unnormal. All 1,000,154 classifications matched: 3,084 inputs selected one of the 25 variable IDs, and nine exceptional inputs raised invalid without spuriously selecting an ID. All nine invalid executions returned the x87 integer-indefinite value 0x8000000000000000, whose low EAX half is zero. The combined run executed the exact helper 2,000,168 times with no mismatch.

The first campaign closes neither the out-of-range/NaN helper semantics nor a call-site range proof, and the second remains finite counterexample search. However, the ECL caller does not observe an arbitrary integer: it observes only which, if any, of 25 sentinel IDs was selected. A replacement for that site can therefore prove exact classifier agreement without first imposing a global signed-32-bit range on every non-sentinel input. That narrower theorem still needs raw-ext80/helper semantics and code binding. Other retained arithmetic consumers may continue to require conventional range invariants. Until their site-specific premises are proved, the exact extracted helper remains the oracle.

Address-level obligation ledger

obligations-v1.json turns the aggregate census into a reviewable proof-work queue. It records all 244 comparison addresses and all 77 mapped __ftol2 call addresses, their mapped owner/offset, the minimum semantic classification needed by the current Lean models, and conservative open-status fields. It deliberately excludes executable bytes and full disassembly operand strings.

arithmetic_obligations.py accepts only the pinned executable/mapping/implemented hashes, invokes the same audit, and seals canonical JSON with a content digest. The tracked ledger also records the generator and audit-tool hashes. A local owner of the retail image can check byte-for-byte reproduction with:

python3 tools/arithmetic_obligations.py \
  local/original-th06/東方紅魔郷.exe \
  --mapping repos/th06/config/mapping.csv \
  --implemented repos/th06/config/implemented.csv \
  --check arithmetic/obligations-v1.json

Public CI only runs the proprietary-input-free structural/digest test. The ledger makes no reachability or refinement claim: every site is still marked unclassified, and every conversion range remains unproved. The schema, discharge discipline, and corrected two-site AL result are detailed in docs/arithmetic-obligations.md.

Further version-1 artifacts begin classifying that queue without modifying its conservative base status:

  • ftol2-source-candidates-v1.json assigns all 77 calls a source-expression candidate and semantic sink at authoritative source revision cc475a0bc3fef38683b0f02224c87ddba0a021d9. It identifies 68 presentation/audio calls as candidates for omission after noninterference, and nine gameplay calls as retain candidates. These are manual disassembly/source alignments, not debug-line or compiler proofs.
  • ecl-var-dispatch-v1.json verifies 17 critical instruction signatures and extracts the 25 unique in-function jump targets used by GetVar/GetVarFloat. It contains no executable bytes or complete operand strings. Its static decode is not yet a verified decoder, reachability, helper-refinement, or guest-binding theorem.
  • ftol2-helper-v1.json checks all 37 instructions in the 117-byte helper and records its masked-invalid path. Under the still-open architectural premise that masked-invalid fistp m64int stores integer- indefinite, the path returns EDX:EAX 0x80000000:00000000; Lean checks the low-EAX projection, but not the decoder, x87 premise, or code binding.
  • ecl-player-write-v1.json independently walks all 8,384 subroutine instructions extracted from the seven hash-pinned retail ECL files. Among 1,844 candidate output operations, none names a player position or other readonly ID. All 80 direct increment/decrement operations that bypass the modeled type guard target four local-variable IDs. The artifact checks 28 retail instruction signatures and 17 source anchors, but archive extraction, runtime decoding/handler correspondence, immutability, non-ECL writer completeness, and guest binding remain open.
  • player-position-v1.json checks 95 instruction signatures across eight mapped functions, 21 pinned source anchors, five binary32 constants, four character-speed records, and four comparison contracts. Its total Lean clamp model proves that either infinity is clamped into 16..432, whereas NaN survives the retail branch sequence. This narrows the player invariant to candidate-not-NaN, complete-writer, scheduling, arithmetic/code-binding, and guest-refinement obligations; it does not discharge them.
  • player-state-enclosing-v1.json checks 55 additional instruction roles across Player::AddedCallback, Player::OnUpdate, both timer helpers, and the movement call; it also pins 30 source anchors. For the full-speed/no-bomb/no-hit/no-time-stop-write profile, it records the post-spawn anchor, timer/state recurrence, callback ordering, and the fail-closed writer boundary, while directly sealing the preceding position/speed/bounds artifact. It remains static correspondence evidence, not a verified decoder, reachability/writer-completeness proof, or Rust refinement theorem.
  • item-score-v1.json checks 77 instruction signatures across the four difficulty profiles, the five-entry difficulty table, all eight retained score conversions, their repeated binary32 field loads, and the final gameplay-score addition. It now seals the player-position artifact as an input. The associated Lean model proves conditional finite geometry and total finite/infinity/NaN score bounds, not their binary bindings.
  • item-score-corpus-v1.json records a debug- breakpoint experiment over 2,081 point-item collections in four Linux replay runs. All observed values were finite and inside the candidate collection interval, but this remains reconstruction-only counterexample search.
  • first-item-v1.json composes the first-wave and Item-score audits with 21 source anchors for random-drop cadence, Item allocation/motion/AABB collection, retained feedback, and collision-ANM reclamation. It records the exact Normal Reimu-A frame-219 spawn/frame-249 collection contract and its finite-prefix noninterference assumptions; it is static correspondence evidence rather than a binary/refinement proof.

These artifacts have proprietary-input-free structural tests in lightweight CI. Local owners can reproduce them with:

python3 tools/ftol2_source_candidates.py \
  --source-root repos/th06 \
  --check arithmetic/ftol2-source-candidates-v1.json
python3 tools/ecl_var_dispatch_audit.py \
  local/original-th06/東方紅魔郷.exe \
  --mapping repos/th06/config/mapping.csv \
  --check arithmetic/ecl-var-dispatch-v1.json
python3 tools/ftol2_helper_audit.py \
  local/original-th06/東方紅魔郷.exe \
  --check arithmetic/ftol2-helper-v1.json
python3 tools/ecl_player_write_audit.py \
  local/original-th06/東方紅魔郷.exe \
  local/original-th06/紅魔郷ST.DAT \
  --mapping repos/th06/config/mapping.csv \
  --check arithmetic/ecl-player-write-v1.json
python3 tools/player_position_audit.py \
  local/original-th06/東方紅魔郷.exe \
  --mapping repos/th06/config/mapping.csv \
  --source-root repos/th06 \
  --check arithmetic/player-position-v1.json
python3 tools/player_state_enclosing_audit.py \
  local/original-th06/東方紅魔郷.exe \
  --mapping repos/th06/config/mapping.csv \
  --source-root repos/th06 \
  --check arithmetic/player-state-enclosing-v1.json
python3 tools/item_score_audit.py \
  local/original-th06/東方紅魔郷.exe \
  --mapping repos/th06/config/mapping.csv \
  --check arithmetic/item-score-v1.json
python3 tools/first_item_audit.py \
  local/original-th06/東方紅魔郷.exe \
  --source-root repos/th06 \
  --check arithmetic/first-item-v1.json

The corpus report additionally requires a debug build, GDB, the four pinned replays (including the local-only world-record input), and verified retail data. Its generator prints the exact replay, runner, source, tool, and data- manifest identities into the sealed report; public CI validates the report but does not execute GDB or require commercial inputs.

make -C reference/build config=debug -j2
python3 tools/run_item_score_probe.py \
  --runner reference/th06 \
  --data-directory local/original-th06 \
  --check arithmetic/item-score-corpus-v1.json \
  replays/samples/th6_ud000134.rpy \
  replays/samples/th6_ud000232.rpy \
  replays/samples/th6_ud002677.rpy \
  replays/samples/th6_udLuRB.rpy

Soundness boundary

The experiment supports a candidate implementation choice; it does not close the arithmetic proof. In particular:

  • Berkeley SoftFloat is an independently implemented executable oracle here, not a formally verified theorem;
  • basic/store operations compare only the six exception bits; comparisons add C0/C1/C2/C3, while TOP/tag state, instruction/data pointers, stack faults, trap behavior, and arbitrary NaN payload/noncanonical encodings remain outside the SoftFloat probe;
  • binary32-derived values are first converted exactly and then loaded as extended values, so the status comparison does not model a preceding fld m32fp denormal-operand event;
  • remainder and fisttp remain unmodeled; the extracted __ftol2 experiment covers its complete register result and stack balance only for canonical finite, signed-i64-representable PC53 inputs; a second experiment tests only ECL sentinel classification on focused exceptional and boundary inputs, not universal exceptional semantics or a reachable-state theorem;
  • x87 transcendental instructions are outside SoftFloat's operation set and still require a pinned processor/emulator profile or a separate equivalence proof;
  • the experiment does not prove the game's loader-established control word, dynamic reachability, original instruction extraction, or connection to a future Lean definition and zkVM guest.

The address binding work queue is now explicit, but none of its entries is discharged. Source/sink candidates narrow it to nine retained calls and 68 potential omissions; every omission still needs noninterference. The immediate arithmetic goals are an exact ECL helper-to-classifier theorem, the point-item player-candidate/writer/scheduling and collision/helper quotient for eight score calls, complete result-use evidence, load/remainder semantics, and an exact transcendental profile. The total score model no longer assumes finite item coordinates, and the clamp model no longer assumes the pre-clamp candidate is finite. The fixed retail ECL scan removes one concrete alias-writer family from the data-level queue but not its extraction/decoder/handler bindings or non-ECL completeness. Their architectural and address bindings remain open. The exact implementation remains the fallback whenever a refinement theorem cannot be proved.