Skip to content

feat(u4): add checked bit-to-nibble packer - #75

Merged
RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-bits-to-nibble
Sep 28, 2026
Merged

RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-bits-to-nibble

Conversation

@brenorb

@brenorb brenorb commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add a checked inverse for the existing u4 big-endian bit decomposition
  • pack four numeric bits into one nibble with an explicit stack contract
  • record focused correctness and reproducible size metrics

Verification

  • cargo test --locked arithmetic::u4::bits::tests::inverse_ -- --nocapture
  • cargo test --locked --test primitive_metrics
  • python3 tools/kb.py validate
  • cargo fmt --check
  • git diff --check

The full repository test suite was intentionally not run.

@brenorb
brenorb force-pushed the feat/u4-bits-to-nibble branch from d3ac1b1 to db961b9 Compare September 11, 2026 14:21

Copy link
Copy Markdown
Contributor

Review recommendation: fix before merging.

Correct the contradictory stack contract before merging. The actual input order is bit0 | bit1 | bit2 | bit3, with bit3 on top; the added comment reverses the written order while also claiming bit3 is on top. The implementation tests pass. Add an active focused metric check for the new measurements.

September 11 local validation (unchanged PR head): python3 tools/kb.py validate passed; 3 selected library tests passed. These are targeted local checks, not a full-suite or complete-spend validation. Results using a stack-limit-disabled execution helper remain research-unlimited.

Integration: rebase on current main, resolve any catalog/NR/OP ID collisions, and run the affected correctness tests and focused metric checks.

Reviewed commit: db961b9f56aa086935765afe10022fcc36a6a5df.

@brenorb
brenorb force-pushed the feat/u4-bits-to-nibble branch from db961b9 to d46cf24 Compare September 18, 2026 23:07
@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Updated in place in commit d46cf24.

  • Corrected the stack contract to preserved | bit0 | bit1 | bit2 | bit3, with bit3 on top; the implementation remains unchanged and reconstructs bit0 + 2*bit1 + 4*bit2 + 8*bit3.
  • Replaced the vacuous malformed-input assertion with checks for both -1 and 2 in each of the four positions, asserting the range-check error.
  • Added explicit InvalidStackOperation checks for 0–3 available inputs, checked and unchecked exhaustive round trips for all 16 nibbles, and a splitter-to-packer round trip proving main/altstack sentinel preservation.
  • Added the active u4_bits_to_nibble_metrics_are_current check covering script bytes, witness minimum/maximum, strict combined stack peak, and static non-push opcode count.
  • Rebased on current main and preserved the current u4 catalog/documentation state.

Validation: 24 affected bits tests passed; cargo test --locked --test primitive_metrics passed with 106 tests passing and 1 ignored; cargo fmt --all -- --check; python3 tools/kb.py validate; and git diff --cached --check all passed.

Copy link
Copy Markdown
Contributor

Review of d46cf245e82a99e24bd86a5d21ff531b3a661f51:

The explicit stack layout, checked/unchecked cases, short-stack tests, and active metric improve the proposed inverse adapter. Resolve the shared catalog/metric additions without dropping existing entries, then rerun the inverse-packer and round-trip tests plus its named metric check.

Current integration conflicts: knowledge/catalog.json, tests/primitive_metrics.rs. These must be resolved and the integrated result validated before merging.

Validation scope: source/diff and existing CI review; no new full-repository or field-arithmetic test run was requested.

brenorb and others added 2 commits September 28, 2026 01:38
Resolve the shared-file conflicts against origin/main (3d1001f):

- knowledge/catalog.json: keep main's u4 stack_contract (including the
  per-nibble popcount sentence) and append the PR's inverse-packer sentence;
  the PR's bits-to-nibble-checked configuration and metric test link are kept.
  All 117 main records and 283 main configurations survive; the merged file
  has 117 records and 284 configurations with no duplicate ids.
- tests/primitive_metrics.rs: keep both main's aes_key_profile_metrics_are_current
  and the PR's u4_bits_to_nibble_metrics_are_current (135 #[test] fns =
  134 main + 1 PR; every main and PR metric key occurrence preserved).
- src/arithmetic/u4/mod.rs: take main's sorted module list, a strict superset
  of the PR's (the PR only reordered declarations).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The round-trip test only exercised nibble 9 (0b1001), a bit-palindrome, so
a packer with reversed (LSB-first) weights still passed it. Round-trip every
nibble through the checked big-endian altstack splitter, for both checked
and unchecked packing, while keeping the surrounding main/alt sentinels.
With the weight-reversal mutant the test now fails at nibble 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@brenorb

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 5d8b653fb0928f111e622e3221622232cb0a149c (merge e6f59af8b01dc4353e0dd027833b9af7ce441a3a of current main 3d1001fc plus one test follow-up). The packer implementation, its stack layout, and its metric fixture are unchanged.

  • Conflicts. Against the old main (e9d5a66), the conflicts were exactly knowledge/catalog.json and tests/primitive_metrics.rs. Main has since moved to 3d1001fc, which also brought a conflict in src/arithmetic/u4/mod.rs. I resolved all three:

    • knowledge/catalog.json: I kept main's u4 stack_contract, including its per-nibble popcount sentence, and appended the PR's inverse-packer sentence. I compared the id sets on each side. Records: main 117, PR 101, merged 117. Configurations: main 283, PR 245, merged 284. Every main id and every PR id is in the merged file, there are no duplicates, and the only new configuration is bits-to-nibble-checked.
    • tests/primitive_metrics.rs: I kept both main's aes_key_profile_metrics_are_current and the PR's u4_bits_to_nibble_metrics_are_current. The file now has 135 #[test] fns (134 from main plus 1 from the PR). Every main and PR metric-key occurrence survived: 973 in total, which is main's 963 plus the PR's 5 rows in metrics() and 5 rows in the dedicated test.
    • src/arithmetic/u4/mod.rs: I took main's sorted list. It has 47 declarations and contains all 37 of the PR's; the PR only reordered them.
    • The PR's rustfmt-only edits to src/ciphers/aes/mod.rs and src/arithmetic/u4/sum.rs are already on main, so the merged tree differs from main only in the six packer files.
  • Checked rejection. inverse_rejects_non_bit_inputs passes. It rejects -1 and 2 at each of the four bit positions with Verify. I tested two mutants, each removing the range check at one position:

    • bit0 check removed: the test fails with left: None, right: Some(Verify).
    • bit3 check removed: the test fails with the same assertion.

    Both mutants were reverted. The valid controls are inverse_packs_big_endian_bits over all 16 nibbles.

  • Non-minimal bit encodings. I ran a temporary probe with witness items 00, 80, 0000, 0100 and 010000 at each position. The interpreter rejects them all with MinimalData, but only because its numeric-minimality option is on by default. With that option off, all of them are accepted and read as their numeric value 0 or 1. The 0 <= x < 2 check constrains the numeric value, not the byte encoding. The output nibble is always minimal because it comes from OP_ADD. This matches the README ("constrained to the numeric range") and the catalog security note ("byte-unique ScriptNum encoding remains a protocol obligation"). A byte-canonical bit check was not added in this PR.

  • Short stacks. inverse_rejects_short_stacks passes: 0–3 available items each fail with InvalidStackOperation. The four-item cases in the exhaustive packing test are the succeeding control.

  • Round trip (follow-up commit). The old round-trip test used only nibble 9 (0b1001). Because 9 is a bit-palindrome, a mutant with reversed weights (8*bit0 + 4*bit1 + 2*bit2 + bit3) still passed it; only the exhaustive packing test caught that mutant. The round trip now covers all 16 nibbles through main's u4_nibbles_to_be_bits_toaltstack(1, true), for both checked and unchecked packing, and keeps the main/alt sentinels 99 and 42. Under the same mutant it fails with round trip failed for nibble 1, checked true. The mutant was reverted, and I updated the README test description to match.

  • Metric. u4_bits_to_nibble_metrics_are_current passes with no fixture change. The fragment is 41 policy-produced bytes with 33 static non-push opcodes, uses a 5–9-byte serialized witness of 4 data items and 0 hint items, and peaks at 7 combined stack items under the strict limit.

Validation on the clean committed HEAD:

  • cargo test --locked --lib arithmetic::u4::bits: 24 passed, including the 4 inverse tests.
  • cargo test --locked --test primitive_metrics u4_bits_to_nibble: 1 passed.
  • cargo test --locked --test primitive_metrics u4: 55 passed.
  • python3 tools/kb.py validate: 117 records / 284 configurations.
  • python3 -m unittest discover -s tools -p 'test_*.py': 49 OK.
  • cargo test --locked --no-run, cargo fmt --all -- --check and the conflict-marker scan are clean.

Field tests were not run. Runs use compiler 124b561ed75ac3ec4c6ad99207d8dcdd3bc67180 and interpreter a09e87af444034698697f0a2267e755cf72f9aed in a tapscript context. The evidence is local only, and the deployment class stays unclassified; I am not claiming any Bitcoin Core or relay-policy validation.

@RobinLinus
RobinLinus merged commit 8cadeec into solving-bitcoin:main Sep 28, 2026
3 checks passed
@brenorb
brenorb deleted the feat/u4-bits-to-nibble 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