Skip to content

feat(u4): add canonical LSB projection - #113

Merged
RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-canonical-lsb
Sep 28, 2026
Merged

RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-canonical-lsb

Conversation

@brenorb

@brenorb brenorb commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add u4_nibbles_to_lsb_canonical() with minimal ScriptNum validation for every nibble
  • reuse the existing 16-item LSB table and preserve the original unchecked-range profile
  • document the 504-byte boundary and add reference, malformed-input, stack-preservation, and metric coverage

Verification

  • cargo test --locked arithmetic::u4::lsb::tests:: --lib
  • cargo test --locked --test primitive_metrics u4_lsb_canonical_metrics_are_current -- --exact
  • python3 tools/kb.py validate
  • cargo fmt --all -- --check
  • git diff --check

Copy link
Copy Markdown
Contributor

Review: changes required before merge.

The canonical wrapper retains a maximum batch size of 982, but its stronger validator adds one stack item to the peak. Strict local executions succeed at 981 and fail at 982 for otherwise valid inputs. Give the canonical variant its own maximum (981 without unrelated state), update composition/peak documentation, and add exact-boundary tests.

Evidence: locally-reproduced against the proposed implementation; local fragment execution only, deployment unclassified. This is not Bitcoin Core or complete-spend validation. Run the affected module tests and focused metric check after the fix.

Reviewed commit: 6ce7b1f94df418e590cb1a1ea60a9e6a01bffd15.

@brenorb
brenorb force-pushed the feat/u4-canonical-lsb branch from 6ce7b1f to 74a77ab Compare September 18, 2026 23:41
@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the review recommendation in rebased commit 74a77ab.

  • Added U4_LSB_CANONICAL_MAX_BATCH = 981; the ordinary numeric-range wrapper remains capped at 982.
  • Added an exact strict boundary regression: canonical 981 succeeds at a 1,000-item peak, canonical 982 and zero are rejected at generation time, and the ordinary 982 generation bound remains accepted.
  • Added the canonical LSB metric guard and synchronized README, comparison, primitive page, and dedicated arithmetic/u4-lsb catalog configuration.
  • Documented the composition rule n + 19 + unrelated_live_items <= 1000, counting both stacks, and kept deployment unclassified.
  • Rebased onto current main and kept the canonical configuration out of the general u4 catalog record.

Validation:

  • cargo test --locked --lib arithmetic::u4::lsb::tests — 6 passed
  • cargo test --locked --test primitive_metrics u4_lsb_canonical_metrics_are_current — passed
  • cargo fmt --all -- --check
  • python3 tools/kb.py validate
  • git diff --cached --check

Copy link
Copy Markdown
Contributor

Review of 74a77ab170798690800728ce5780b8f0c036c796:

The canonical maximum is now separately bounded at 981, with an executed strict frontier, addressing the earlier overflow. Integrate this with newly merged #236, preserving its range-only boundary tests and terminology. Rerun the focused LSB tests and both range-only/canonical metric checks.

Current integration conflicts: knowledge/catalog.json, knowledge/primitives/u4-lsb.md, src/arithmetic/u4/README.md, src/arithmetic/u4/lsb.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 06:25
…solving-bitcoin#236 and Core fixture solving-bitcoin#225

Resolve conflicts in knowledge/catalog.json, knowledge/primitives/u4-lsb.md,
src/arithmetic/u4/README.md, src/arithmetic/u4/lsb.rs and
src/arithmetic/u4/mod.rs (the last one is new relative to e9d5a66 because
main now also carries solving-bitcoin#244).

- lsb.rs: keep solving-bitcoin#236's range-checked wording, imports and its three tests
  (projects_all_nibble_least_significant_bits_in_order,
  rejects_out_of_range_nibbles_at_each_position,
  respects_stack_frontier_and_preserves_state); keep this PR's separate
  u4_nibbles_to_lsb_canonical API, its 981-input bound and its four tests.
  Document the canonical API's contract distinctly.
- mod.rs: take main's sorted module list (superset of the PR's).
- catalog: keep main's range-only summary/security wording and solving-bitcoin#225's Core
  complete-leaf configuration; add the PR's canonical-checked-batch32
  configuration, list the canonical tests and metric, and state that only
  the canonical API enforces canonical ScriptNum encoding.
- knowledge page / README: keep solving-bitcoin#236 and solving-bitcoin#225 text verbatim; add the canonical
  adapter as a separate API with its 1..=981 frontier and the note that the
  Core fixture does not validate it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ality check

canonical_projection_rejects_malformed_nibbles ran under the default local
helper, which enforces minimal ScriptNum decoding (require_minimal), so the
aliases [0x01, 0x00] and [0x80] were rejected with MinimalData by OP_WITHIN
before verify_canonical_nibble ran. With the canonicality check removed from
u4_nibbles_to_lsb_canonical the test still passed.

Run the rejection cases under the local TapscriptProfile::Consensus profile,
which decodes non-minimal numbers: add a valid control, a range-only control
showing u4_nibbles_to_lsb accepts both aliases there, and exact-error
assertions (EqualVerify for aliases, Verify for 256) for the canonical API at
every position. The original minimal-helper rejection check is retained.
With the canonicality check dropped, the test now fails with
"canonical API mishandled [1, 0] at 0" (left: None, right: Some(EqualVerify)).

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

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 6fa6631f0c8a56c47442edbbad2f2e77e64fbf15 (merge 31d49583e4082a1ddc1657d329f2e03e7eff8f87 of current main 3d1001fc plus one follow-up test fix). #236's range-only API, tests and wording, and #225's Core fixture and evidence text, are preserved. The canonical API is still a separate API.

  • Conflicts: Against current main (which now also includes Integrate 27 reviewed PRs with corrected validation and Taproot preflight #244), the merge conflicted in the four files you listed plus src/arithmetic/u4/mod.rs. Against the old e9d5a66, it conflicted in exactly your four files. Here is how each was resolved:
    • mod.rs: took main's sorted list. It has 47 lines and is a strict superset of the PR's 37.
    • lsb.rs: kept test(u4): cover checked LSB boundaries #236's module doc, u4_nibbles_to_lsb doc, test imports and assert_validation_error. Added a doc comment for u4_nibbles_to_lsb_canonical that states its canonical-encoding requirement and its 1..=981 bound.
    • knowledge/primitives/u4-lsb.md and src/arithmetic/u4/README.md: test(u4): cover checked LSB boundaries #236/test(u4): validate LSB projection under Core policy #225 text is kept verbatim ("range-checked", "Numeric range checking does not establish canonical or byte-unique ScriptNum encoding", the Core v30.3 fixture section). The canonical adapter is added as a separate API with its own paragraph and bullet. The page now says the Core complete-leaf fixture uses only the range-checked API and does not validate the canonical variant.
    • knowledge/catalog.json: kept main's summary/security wording and test(u4): validate LSB projection under Core policy #225's u4-lsb-0123456789abcdef configuration, and added canonical-checked-batch32. The tests list now names the four canonical tests and u4_lsb_canonical_metrics_are_current. security, stack_contract and limitations now say that only u4_nibbles_to_lsb_canonical enforces canonical ScriptNum encoding, and give the 982 (range-checked) and 981 (canonical) bounds.
    • Record-set check: main has 117 records / 283 configurations, the PR side has 101 / 245, and the merged file has 117 / 284. Main ⊆ merged and PR ⊆ merged, with no duplicate record or configuration ids. The only added configuration is arithmetic/u4-lsb/canonical-checked-batch32.
    • tests/primitive_metrics.rs merged automatically. It has 134 #[test] fns: main's 133 plus u4_lsb_canonical_metrics_are_current. Main ⊆ merged and PR ⊆ merged.
  • Tests preserved, by name:
  • Canonical frontier (mutant a): I raised U4_LSB_CANONICAL_MAX_BATCH to 982 (1_000 - 16 - 2, the earlier overflow). canonical_batch_boundary_is_one_item_smaller then fails at canonical max batch failed: Execution: tapscript, stack limit enforced; deployment unclassified. Error: StackSize. Reverted, the 981-input batch passes with a 1,000-item combined main+alt peak under execute_script_with_inputs_strict.
  • Canonicality check (mutant b): this exposed a vacuous test, fixed in the follow-up commit.
    • The problem: I replaced verify_canonical_nibble() with the range-only OP_DUP 0 16 OP_WITHIN OP_VERIFY, and the original canonical_projection_rejects_malformed_nibbles still passed. The default local helper enforces minimal numeric decoding, so [0x01, 0x00] and [0x80] were rejected by OP_WITHIN with MinimalData for both APIs before the canonicality check ran. [0x00, 0x01] (256) was rejected with Verify.
    • The fix: the rejection cases now run under the local TapscriptProfile::Consensus profile, which accepts non-minimal numbers, with the stack limit on. The test has a valid control that must succeed. A range-only control confirms u4_nibbles_to_lsb accepts both aliases there. At each of the 4 positions it checks exact errors for the canonical API: EqualVerify for the redundant-zero and negative-zero aliases, and Verify for 256. The original minimal-helper rejection check is kept.
    • Under the same mutant the test now fails with canonical API mishandled [1, 0] at 0 (left: None, right: Some(EqualVerify)). Both mutants are reverted and the tree is clean.
  • Metrics: unchanged, no refresh. Range-only u4_lsb_batch32: 440 bytes, 65 witness bytes, 50-item peak, 328 static non-push opcodes. Canonical u4_lsb_canonical_batch32: 504 bytes, 65 witness bytes, 51-item peak, 360 static non-push opcodes. Both have 32 data items and zero hints. The test(u4): validate LSB projection under Core policy #225 fixture is also unchanged: cargo run --locked --example core_validation_fixtures reproduces script SHA256 58fcbbe7…f708 and tapleaf 089b76bd…e160.

Validation on the clean committed HEAD, with CARGO_PROFILE_{DEV,TEST}_OPT_LEVEL=1:

  • cargo test --locked --lib arithmetic::u4::lsb: 7 passed.
  • cargo test --locked --test primitive_metrics u4_lsb: 2 passed (u4_lsb_metrics_are_current, u4_lsb_canonical_metrics_are_current).
  • python3 tools/kb.py validate: valid, 117 records / 284 configurations.
  • python3 -m unittest discover -s tools -p 'test_*.py': 49 OK.
  • cargo fmt --all -- --check passed, and cargo test --locked --no-run compiled.
  • No field tests were run.

Evidence for the canonical variant is locally-reproduced, and deployment is unclassified. No Bitcoin Core or relay-policy validation is claimed for it. The existing Core v30.3 evidence applies only to #225's range-checked complete leaf.

@RobinLinus
RobinLinus merged commit b7cadb0 into solving-bitcoin:main Sep 28, 2026
3 checks passed
@brenorb
brenorb deleted the feat/u4-canonical-lsb 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