Skip to content

feat(u4): document word stack transfers - #49

Merged
RobinLinus merged 7 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-stack-transfer-boundary
Sep 28, 2026
Merged

RobinLinus merged 7 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-stack-transfer-boundary

Conversation

@brenorb

@brenorb brenorb commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • document the existing u4_copy_u32_from(0) and u4_move_u32_from(0) contracts
  • add deterministic order-preservation coverage for copy and destructive move
  • add measured README and catalog records for both eight-nibble transfers

Validation

  • cargo fmt --all -- --check
  • cargo test --locked arithmetic::u4::stack::tests::copy_and_move_u32_preserve_nibble_order
  • python3 tools/kb.py validate
  • git diff --check

The full repository test suite was intentionally not run; validation is scoped to the changed primitive as requested.

Copy link
Copy Markdown
Contributor

Review: changes needed before merge.

Fold the existing word-transfer documentation/tests into the shared stack/router package, with consistent preservation and ordering assertions.

Historical local validation (September 11, at this unchanged PR head): python3 tools/kb.py validate passed; 2 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.

The new metric markers are only added to the ignored aggregate metric collection; add an active, focused metric check for these documented configurations. Rebase the documentation and catalog on current main, preserving the existing configurations, and rerun only the affected checks.

Reviewed commit: 09a75bd862f36b8654f0827a1756d1796fa779f1.

@brenorb
brenorb force-pushed the feat/u4-stack-transfer-boundary branch from 09a75bd to 6b1ac69 Compare September 18, 2026 19:24
@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Updated in place and rebased onto current main. Consolidated the shared word-transfer coverage requested by #37: raw-item/address documentation, preservation/order tests at depths 0 and 8, strict 992/993 copy and 999/1000 move boundaries, and active copy/move metrics with runtime witnesses and strict stack peaks. Removed the old ignored duplicate metric entries.

Checks: cargo test --locked --test primitive_metrics (106 passed, 1 ignored); focused U4 stack tests (16 passed); python3 tools/kb.py validate (101 records, 246 configurations); cargo fmt --all -- --check; git diff --cached --check.

@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Consolidated #48 into this PR. Added explicit one-way and roundtrip ordering tests, zero-item and both-direction underflow checks, and active u4_toaltstack(4)/u4_fromaltstack(4) metrics. The docs now state that each one-way transfer reverses the moved group, while a complete roundtrip preserves order and older stack state; the representative witness is 8 bytes with a 9-byte maximum. Required checks passed: cargo fmt --all -- --check, python3 tools/kb.py validate, git diff --cached --check, focused transport tests, and cargo test --locked --test primitive_metrics (107 passed, 1 ignored).

@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Consolidated #50 into this PR. Added the staged verifier’s raw-byte contract, sentinel preservation, mismatch coverage at all eight positions, canonical/noncanonical encoding behavior, and both main/altstack underflow checks. Added an active metric check for the 29-byte, 33-byte-witness, 17-item-peak, 23-non-push-opcode verifier boundary; staging and terminal predicates are excluded from fragment size. Required checks passed: cargo fmt --all -- --check, python3 tools/kb.py validate, git diff --cached --check, focused verifier tests, and cargo test --locked --test primitive_metrics (108 passed, 1 ignored).

@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Consolidated #53 into this PR. Added active lifecycle metrics for the 16-item triangular and 17-item linear offset tables, exact schedule tests, sentinel-preservation tests, matching-cleanup coverage, wrong-width failure, leftover-width behavior, and explicit over-cleanup consumption. Documentation now distinguishes offset tables from complete Boolean lookup memory and records caller-owned depth/cleanup obligations. Required checks passed: cargo fmt --all -- --check, python3 tools/kb.py validate, git diff --cached --check, focused lookup tests, and cargo test --locked --test primitive_metrics (109 passed, 1 ignored).

@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Added the remaining #56 regression to this consolidated package.\n\nThe new test supplies exactly 16 runtime items and verifies that both and fail with . It deliberately does not carry over #56's stale stack/metric probes; the existing active #49 metrics remain authoritative.\n\nChecks:\n- cargo fmt --all -- --check\n- python3 tools/kb.py validate\n- git diff --cached --check\n- cargo test --locked --lib arithmetic::u4::stack::tests — 24 passed\n- cargo test --locked --test primitive_metrics u4_word_transfer_metrics_are_current — passed

@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Correction to the previous note: added the remaining #56 regression to this consolidated package. The new test supplies exactly 16 runtime items and verifies that both u4_copy_u32_from(9) and u4_move_u32_from(9) fail with InvalidStackOperation. It deliberately does not carry over #56's stale stack/metric probes; the existing active #49 metrics remain authoritative.\n\nChecks:\n- cargo fmt --all -- --check\n- python3 tools/kb.py validate\n- git diff --cached --check\n- cargo test --locked --lib arithmetic::u4::stack::tests — 24 passed\n- cargo test --locked --test primitive_metrics u4_word_transfer_metrics_are_current — passed

Copy link
Copy Markdown
Contributor

Review of 328c8be0fa6a775f10c1518c4d62d4e456e0e78f:

The consolidated transfer/lookup coverage addresses the earlier packaging request. While resolving the stack.rs conflict, retain the newly merged #239 checked/unchecked pair-packing and range-error tests as well as this PR's transfer tests. Merge catalog/metric additions without dropping existing rows, then run the focused stack/lookup checks.

Current integration conflicts: knowledge/catalog.json, src/arithmetic/u4/stack.rs, 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.

…licts

Robin Linus requested integrating current main while retaining solving-bitcoin#239's
checked/unchecked pair-packing and range-error tests on stack.rs
alongside this PR's word-transfer and lookup-lifecycle documentation
and tests.

- knowledge/catalog.json: combined the two independent stack_contract
  sentence additions (main's popcount wording, this PR's transfer/
  lookup wording); kept every record and configuration from both
  sides (117 records, 292 configurations; main's 283 configs + this
  PR's 9 new ones).
- src/arithmetic/u4/mod.rs: re-sorted the module list, keeping every
  `pub mod` from both sides.
- src/arithmetic/u4/stack.rs: kept both `use` imports (this PR's
  execute_script_with_inputs_strict and main's bitcoin_scriptexec::
  ExecError, the latter needed by solving-bitcoin#239's tests). All 24 tests in the
  module (main's 13 plus this PR's 11 new transfer tests) survive;
  the PR's now-superseded `packs_all_checked_nibble_pairs` is
  correctly dropped in favor of main's renamed/expanded
  `packs_all_nibble_pairs`, per instruction to keep main's
  pair-packing functions unchanged.
- tests/primitive_metrics.rs: both sides had appended new tests at
  the same insertion point; spliced this PR's 4 new u4 metric tests
  ahead of main's 4 new u32 metric tests, preserving all fns from
  both sides (206 main + 4 new = 210 total, verified by name-set
  comparison).

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

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 6f006d70a858741756c6ba88ede04b88c694eaa2, integrating current main (3d1001fc, which had moved from the reviewed baseline via #244's 27-PR integration) while preserving #239's checked/unchecked pair-packing and range-error tests alongside this PR's transfer and lookup coverage.

  • Conflicts. The actual integration conflicted on four files, one more than listed in the review: knowledge/catalog.json, src/arithmetic/u4/mod.rs, src/arithmetic/u4/stack.rs, tests/primitive_metrics.rs. The extra file (mod.rs) conflicted only because other PRs merged into main between the review and now (equality, msb, mul, odd_inverse, threshold, transition_count, trichotomy, cyclic_equality) added pub mod lines interleaved with this PR's own additions; resolved by taking the sorted union of both sides' module declarations.

  • test(u4): strengthen pair-to-byte coverage #239 tests preserved on stack.rs (13 fns already on main): test_repeat, test_number_to_nibble, test_hex_to_nibble, packs_all_nibble_pairs (the renamed/expanded successor to this PR's now-superseded packs_all_checked_nibble_pairs), checked_pair_rejects_malformed_nibbles_and_preserves_state, checked_triplet_packs_boundaries_and_preserves_state, unchecked_triplet_requires_the_caller_invariant, checked_quad_packs_boundaries_and_preserves_state, unchecked_pair_requires_the_caller_invariant, splits_all_checked_bytes, checked_split_rejects_malformed_bytes_and_preserves_altstack, unchecked_split_requires_the_caller_invariant, canonical_nibble_boundary_rejects_aliases_and_out_of_range_values. u4_pair_to_u8 itself is untouched — main's rotating range-check implementation is kept exactly as-is; only this PR's doc comments on other functions (u4_toaltstack, u4_fromaltstack, u4_copy_u32_from, u4_move_u32_from, u4_u32_verify_from_altstack) were carried forward.

  • This PR's transfer/lookup tests preserved (11 in stack.rs + 3 in logic.rs): altstack_transport_documents_one_way_order_and_roundtrip, altstack_transport_zero_is_a_noop, altstack_transport_rejects_missing_items_in_both_directions, altstack_word_verifier_accepts_matching_words_and_preserves_sentinels, altstack_word_verifier_rejects_a_mismatch_at_each_position, altstack_word_verifier_compares_raw_encodings, altstack_word_verifier_rejects_missing_main_or_alt_items, copy_and_move_u32_preserve_nibble_order, copy_and_move_u32_at_depth_eight_preserve_every_word, copy_and_move_u32_reject_out_of_bounds_source_depth, word_transfer_stack_boundaries_are_strict; and in logic.rs: lookup_tables_preserve_their_index_schedule, lookup_lifecycle_preserves_sentinels_and_matching_cleanup, lookup_cleanup_boundaries_are_explicit. All 24 tests in stack.rs's test module (13 + 11) and all 3 in logic.rs are present and pass; name-set comparison confirms merged == main ∪ pr in every case except the one intentional drop noted above (per the instruction to keep main's pair-packing functions unchanged).

  • knowledge/catalog.json. Kept every record and configuration from both sides: main had 117 records / 283 configurations, this PR added 9 new configurations (verify-u32-from-altstack, half-lookup-setup, half-lookup-cleanup, full-lookup-setup, full-lookup-cleanup, copy-u32-from, move-u32-from, altstack-transport-4, altstack-transport-4-return) under the existing 117-record set; merged file has 117 records / 292 configurations = main's 283 + this PR's 9, with main ⊆ merged and pr ⊆ merged verified by Python set comparison (record ids and (record, config) id pairs) and no duplicate ids anywhere. The single conflicting field, stack_contract prose on the shared u4 record, was resolved by concatenating both sides' independent sentence additions (main's per-nibble-popcount sentence, this PR's altstack-transport/staged-word-verifier/lookup sentences) rather than dropping either.

  • tests/primitive_metrics.rs. Both sides had appended new tests at the identical insertion point right after ed25519_packed_decoder_metrics_are_current. Resolved by placing this PR's 4 new tests (u4_word_transfer_metrics_are_current, u4_altstack_transport_metrics_are_current, u4_lookup_lifecycle_metrics_are_current, u4_staged_word_verifier_metrics_are_current) immediately ahead of main's 4 new tests (u32_reverse_byte_adapter_metrics_are_current, u32_equality_metrics_are_current, u32_fixed_rotation_metrics_are_current, u32_byte_planes_metrics_are_current), then the shared tail unchanged. Name-set comparison: main had 206 #[test] fns, this PR's branch had 172 (it diverged from an earlier main), union is 210, and the merged file has exactly 210 with no loss and no duplicates.

  • Negative-test meaningfulness (both reverted before commit; git diff was empty afterward):

    • Transfer-order mutant: changed u4_toaltstack(n) to move n.saturating_sub(1) items (leaving one limb behind on the altstack) instead of n. Under this mutant, altstack_transport_documents_one_way_order_and_roundtrip FAILED with main-to-alt order changed: ... Error: EqualVerify, and altstack_transport_rejects_missing_items_in_both_directions FAILED on assertion failed: matches!(to_alt.error, Some(InvalidStackOperation)). The valid control (altstack_transport_zero_is_a_noop, a 0-item transfer unaffected by the mutant) still passed, so the rejection isn't vacuous.
    • test(u4): strengthen pair-to-byte coverage #239 range-check mutant: removed the second OP_WITHIN range check from u4_pair_to_u8's checked branch (dropping validation of the high limb while keeping the low check). Under this mutant, checked_pair_rejects_malformed_nibbles_and_preserves_state FAILED with assertion left == right failed / left: None / right: Some(Verify) (two of its four malformed-input cases, (-1,0) and (16,0), now pass through unchecked). This confirms the merge preserved a live, load-bearing range check rather than a vacuous one.
  • Validation on the committed HEAD (clean git status --porcelain):

    • cargo test --locked --lib arithmetic::u4:: -- --skip fields:: → 216 passed; 0 failed
    • cargo test --locked --test primitive_metrics -- u4_word_transfer_metrics_are_current u4_altstack_transport_metrics_are_current u4_lookup_lifecycle_metrics_are_current u4_staged_word_verifier_metrics_are_current --skip fields:: → 4 passed; 0 failed (no metric fixture drift; no UPDATE_PRIMITIVE_METRICS refresh was needed)
    • python3 tools/kb.py validate → knowledge base valid: 117 records, 292 configurations
    • python3 -m unittest discover -s tools -p 'test_*.py' → Ran 49 tests ... OK (run because tools/ changed as part of main's Integrate 27 reviewed PRs with corrected validation and Taproot preflight #244 integration)
    • cargo fmt --all -- --check → clean
    • cargo test --locked --no-run → whole crate + every test binary compiled successfully

Evidence remains local only (locally-reproduced per AGENTS.md evidence levels; deployment class unclassified for the mutated-vs-reverted comparisons, which never ran against Bitcoin Core or relay policy). No Core or policy validation is claimed anywhere in this change.

Pushed to feat/u4-stack-transfer-boundary at 6f006d70a858741756c6ba88ede04b88c694eaa2 (fast-forward from 328c8be0); gh pr view 49 --repo solving-bitcoin/bitcoin-scripts --json headRefOid,mergeable reports that SHA and MERGEABLE.

@RobinLinus
RobinLinus merged commit 3219a39 into solving-bitcoin:main Sep 28, 2026
3 checks passed
@brenorb
brenorb deleted the feat/u4-stack-transfer-boundary 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