Skip to content

feat(u4): add checked modulo-16 multiplication - #152

Merged
RobinLinus merged 2 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-mul-mod16
Sep 28, 2026
Merged

RobinLinus merged 2 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-mul-mod16

Conversation

@brenorb

@brenorb brenorb commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

This adds checked multiplication modulo 16 for two numeric u4 operands using a reusable 256-entry product table. Both operands are range-checked before forming the table index; the query consumes the operands and preserves the table beneath its result. Callers move or consume the result before table cleanup.

The representative query has 21 policy-produced script bytes, a 5-byte canonical serialized data-witness fixture (two operands, zero hints), a 261-item combined-stack peak, and 17 static non-push opcodes. Numeric range checks do not prove canonical byte encoding. Evidence remains locally-reproduced and deployment unclassified; no Bitcoin Core or relay-policy validation is claimed.

Integration preserves current main's catalog records, APIs, documentation, and active metric tests. Focused coverage includes all 256 operand pairs, malformed runtime inputs, surrounding state, and table preservation and cleanup.

Validation on the integrated tree: 6 focused multiplication tests passed, the named u4_mul_mod16_metrics_are_current test passed, KB validation passed (102 records / 257 configurations), and formatting and whitespace checks passed. The historical reproduction used interpreter 702544c9; current tests use a09e87af, with compiler 124b561e in both. These are local tapscript results with the stack limit enforced, not Bitcoin Core or relay-policy validation. The existing 21-byte query / 5-byte two-item data witness / 261-item metric peak / 17 static-opcode measurements are unchanged; zero hints. The separate all-table lifecycle test peaks at 516 combined items and uses script-constant operands.

Copy link
Copy Markdown
Contributor

Review: changes required before merge.

The product-table index calculation consumes operands and table entries instead of computing 16*a+b. The sequence OP_DUP OP_ADD OP_ADD OP_ADD OP_ADD lacks the further duplications needed for four doublings. A complete table-setup / 2 3 / multiply / compare-to-6 / table-cleanup wrapper fails. Correct the index construction and exhaust all 256 operand pairs while checking table preservation and cleanup.

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: 1a9c4c981d5369b8f69e04b884e5f7c36da232c6.

@brenorb

brenorb commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

Updated in commit 35a9e89 (branch feat/u4-mul-mod16).

The product-table index now computes 16*a+b by doubling a four times before adding b; it no longer consumes table entries. Tests cover all 256 operand pairs, reuse one resident table across all queries, verify cleanup, preserve surrounding main/alt-stack state, and reject out-of-range operands.

Refreshed metrics/docs: 21 fragment bytes, 17 static non-push opcodes, 261-item peak including the 256-item table. Documentation now describes numeric range checking and the required result-before-table-cleanup ordering.

Validation passed:

  • cargo test --locked --lib arithmetic::u4::mul::tests (4 passed)
  • cargo test --locked --test primitive_metrics (34 passed, 1 ignored)
  • cargo fmt --all -- --check
  • python3 tools/kb.py validate
  • git diff --cached --check

Copy link
Copy Markdown
Contributor

Review of 35a9e8972ccb38767f1b00b51f71405d5033f1df:

The index construction now performs four explicit doublings before adding the second operand, addressing the previous 16*a+b error. Preserve all 256 input-pair cases and full table-lifecycle checks. Rebase and rerun the multiplication library/metric checks and KB validation.

Current integration conflicts: knowledge/catalog.json, knowledge/comparisons/arithmetic.md, knowledge/primitives/index.md, src/arithmetic/u4/README.md, src/arithmetic/u4/mod.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.

# Conflicts:
#	knowledge/catalog.json
#	knowledge/comparisons/arithmetic.md
#	knowledge/primitives/index.md
#	src/arithmetic/u4/README.md
#	src/arithmetic/u4/mod.rs
#	tests/primitive_metrics.rs
@brenorb

brenorb commented Sep 26, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 06882588a3a29ac7dacc2997290d1c9671b06238, integrating current main while preserving its catalog records, module exports, documentation, and active metric functions.

  • Reproduced the original failure at the exact reviewed commit 1a9c4c981d5369b8f69e04b884e5f7c36da232c6: the original all-pairs test fails on witness [0,0,0] (expected result, a, b) with InvalidStackOperation at OP_2DROP. The current PR already contains four explicit doublings, which are preserved.
  • Retained all 256 runtime operand-pair checks through the existing shared strict success harness. Added direct checks of every resident table entry after the complete query sequence and before calling the real cleanup helper; all outputs are then checked in order, with a clean true terminal result.
  • Recreated the exact historical index sequence, including its trailing OP_SWAP, in a test-only mutation. It produces the same typed error and failing opcode, and the same shared success assertion catches it. The valid control uses the same complete leaf builder and witness.
  • Malformed inputs at both operand positions now require Verify for -1/16 and ScriptIntNumericOverflow for a five-byte number. Existing complete cleanup and preserved main/alt-stack regressions remain covered. Documentation now distinguishes numeric range checks from canonical encoding.

Validation on the integrated tree: 6 focused multiplication tests passed, the named u4_mul_mod16_metrics_are_current test passed, KB validation passed (102 records / 257 configurations), and formatting and whitespace checks passed. The historical reproduction used interpreter 702544c9; current tests use a09e87af, with compiler 124b561e in both. These are local tapscript results with the stack limit enforced, not Bitcoin Core or relay-policy validation. The existing 21-byte query / 5-byte two-item data witness / 261-item metric peak / 17 static-opcode measurements are unchanged; zero hints. The separate all-table lifecycle test peaks at 516 combined items and uses script-constant operands.

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