Skip to content

Add canonical Adler-32 state with deferred reduction - #252

Merged
RobinLinus merged 4 commits into
solving-bitcoin:mainfrom
brenorb:codex/adler32-delayed-reduction
Oct 22, 2026
Merged

RobinLinus merged 4 commits into
solving-bitcoin:mainfrom
brenorb:codex/adler32-delayed-reduction

Conversation

@brenorb

@brenorb brenorb commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

The new arithmetic::checksums::adler32::adler32_state(n) fragment consumes 0..=997 canonical byte-valued ScriptNums and returns both Adler residues while preserving caller main/alt stacks. It validates inputs before allocating accumulators, then delays modular reduction using proven four-byte ScriptNum bounds. The original schedule rejects 996/997 inputs at the first 1,001-item state; the new schedule accepts 997 at exactly 1,000 without increasing script bytes.

At the same 32-input/two-output boundary, it costs 636 fragment / 645 complete test-leaf bytes versus 740/749 for prefix-bounded streaming, with 97 serialized data-witness bytes, 32 ordinary data items, zero hints, and peak 35. The 997-input fragment is 19,122 bytes versus the streaming baseline's explicitly unoptimized 41,253 bytes. Raw/final fragment and leaf hashes, independent compiler-policy flags, witness hashes, pins, source-file bindings and boundaries are recorded for all 31 comparison rows.

The shared contracts cover three checksum schedules plus canonical/numeric u32 bit adapters: hostile inputs at every position, permitted aliases, asymmetric ordering, short inputs, both caller stacks, exact resource boundaries, original/repaired scheduling controls, deliberate mutations of compiled validation guards and both terminal predicates. Independent zlib supplies 555 deterministic result vectors. Current reproduction compares every measurement and all seven catalog configurations, excluding only whole-file source_sha256 metadata. A separate full-clone audit checks all 17 historical blobs, including the original measurement code and test, against immutable source commit 250f23861a87ee10b2a7e1ea3de07dcf8569e820. The historical report and measurements are unchanged. Regression coverage permits unrelated source changes while detecting an actually compiled arithmetic/bytecode mutation.

RFC 1950 already specifies delayed Adler reduction; this contribution is its ScriptNum/stack specialization. Adler is a non-cryptographic corruption checksum, with a documented three-byte collision. It does not authenticate data or return packed network-order bytes. Evidence is locally-reproduced, deployment unclassified; no Bitcoin Core or relay-policy acceptance is claimed. NR-087 records the rejected schedule and dominated baseline, OP-043 defines the remaining complete-spend criterion. All 135 current main catalog records and envelope fields are preserved; the combined tree contains 136 records and 435 configurations.

The branch includes current main (adda14fe4895b4d783e6c41197f3e59553945bac) with both checksum modules, every existing catalog record and every metric test preserved.

Validation on delivered head 8190f6762df355a50e887f859b8cbbd99516e041:

  • CARGO_PROFILE_TEST_OPT_LEVEL=1 cargo test --offline --locked -- --skip fields::: 1,247 passed, 25 ignored across 19 suites; no metric refresh.
  • All ten Adler/sibling contract and metric suites also passed separately: 236 passed, 1 ignored.
  • KB validation (136 records / 435 configurations), formatting and all 54 Python harness tests passed.
  • Independent zlib oracle reproduces the recorded vectors byte-for-byte.
  • Temporarily changing the actual source's initial A from 1 to 2 makes the full artifact check fail; restoring the original implementation passes. The historical metrics file remains byte-for-byte unchanged.

Rust tests use CARGO_PROFILE_TEST_OPT_LEVEL=1 with debug assertions and overflow checks retained. Script compilation uses the centralized bounded policy. No field-arithmetic tests were run.

Copy link
Copy Markdown
Contributor

Review of 2eb71c86cef756fcd229c3c2c71b5fcfba975bc1: please fix the artifact test's integration contract before merge.

The combined review tree passes the Adler arithmetic, malformed-witness, caller-stack, and regression tests, but report_and_catalog_bind_every_artifact_and_policy_boundary fails at the full measure::report(...) == report comparison. The report hashes whole shared files (src/arithmetic/mod.rs, src/arithmetic/checksums/mod.rs, tests/primitive_metrics.rs, and src/arithmetic/u32/stack.rs). Adding the independently reviewed CRC module/other primitives, or the selection PR's documentation correction, changes those hashes even though the Adler implementation, baseline, measurement code and measured bytecode are unchanged.

Separate immutable historical-source verification from current measurement reproduction. Continue checking the pinned commit's blobs against the historical hashes, and compare current policy-produced bytecode, witnesses, outputs, stack peaks, options and dependency pins against the recorded measurements. Current shared-file hashes should not be required to equal a historical standalone tree, and the test file's self-hash needs the same treatment. Do not simply refresh historical measurements to hide the difference.

Please demonstrate that the artifact check still catches a real arithmetic/bytecode mutation but permits adding an unrelated module or metric test. I am excluding this PR from integration #263 until that is addressed.

RobinLinus pushed a commit that referenced this pull request Oct 16, 2026
…ibility fixes

Exclude Adler-32 PR #252 pending reproducible artifact validation fixes.
Preserve CRC and Adler modules, all existing catalog records and metric tests. Assign Adler negative result NR-087 and open question OP-043 to avoid IDs already used on main. Historical Adler metrics and source bindings remain unchanged.

brenorb commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review, @RobinLinus! 🙂 This is addressed in ddeccda, and I merged the current main in 8190f67 so the shared-file integration case is covered too.

Historical provenance and current reproduction now have separate checks. The historical audit still verifies all 17 pinned blobs, including the original test and measurement code. Current reproduction compares every report field except source_sha256, so bytecode, witnesses, outputs, stack peaks, compiler/interpreter pins and execution options must still match. The historical metrics.json and measurements are unchanged.

The regression test permits unrelated shared-file hash changes and rejects a genuinely compiled/executed arithmetic mutation. I also temporarily changed the actual source's initial accumulator from 1 to 2: the full artifact check failed as expected, then passed with the original implementation restored. All nine Adler contract tests pass on the combined tree.

The merge preserves both CRC and Adler modules, all existing catalog entries and metric tests. Adler's documentation IDs are now NR-087 / OP-043 to avoid collisions on main. KB validation (136 records / 435 configurations), formatting and all 54 Python tests also pass.

Thanks again for catching the integration issue!

@brenorb

brenorb commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Hi @RobinLinus, a quick follow-up on the fix in ddeccda / 8190f67. I re-checked it end to end against the real artifact test, not only the synthetic regression test 🙂

On the PR head 8190f67, which already contains current main (including #263 and #264):

  • All 9 Adler contract tests pass, including historical_source_blobs_match_recorded_hashes, which still checks all 17 pinned blobs at the recorded revision. All three CI jobs are green.
  • Unrelated changes are allowed. In a scratch tree I added a new sibling module (pub mod parity; in src/arithmetic/checksums/mod.rs), another module declaration in src/arithmetic/mod.rs, a doc edit in src/arithmetic/u32/stack.rs, a new #[test] in tests/primitive_metrics.rs, and an edit to tests/adler32_contract.rs itself. With all of those in place, the real report_and_catalog_bind_every_artifact_and_policy_boundary still passes, and so does the new metric test.
  • A real mutation is caught. With those unrelated edits still applied, I changed the actual generator's initial accumulator in adler32_state from 1 0 to 2 0. The same test then fails at the reproduced-measurements comparison (tests/adler32_contract.rs:438).

Only source_sha256 is excluded from the current-tree comparison, and it is still verified against the historical blobs. Bytecode, witnesses, outputs, stack peaks, options and pins must all match. metrics.json was not refreshed. The scratch edits were discarded, so nothing new was pushed.

I think this is ready for another look whenever you have time. Thanks again for the thorough review!

Copy link
Copy Markdown
Contributor

The earlier shared-source hash blocker is resolved. All nine Adler-32 contract tests passed locally, including historical source verification, malformed inputs, exact stack frontiers, and a compiled arithmetic mutation. The focused metric also passes on the combined branch. Approved for integration via #289; its Adler research identifiers become NR-109/OP-065 to avoid the sorting collision, without changing the historical artifact.

@RobinLinus
RobinLinus merged commit 9498803 into solving-bitcoin:main Oct 22, 2026
3 checks passed
@brenorb
brenorb deleted the codex/adler32-delayed-reduction branch October 28, 2026 02:57
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.

2 participants