Skip to content

Verify retained NAF ScriptNum witnesses - #353

Open
brenorb wants to merge 1 commit into
solving-bitcoin:mainfrom
brenorb:research/scriptnum-naf-witnesses
Open

brenorb wants to merge 1 commit into
solving-bitcoin:mainfrom
brenorb:research/scriptnum-naf-witnesses

Conversation

@brenorb

@brenorb brenorb commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds verify_scriptnum_naf(width), a table-free verifier for a canonical nonnegative ScriptNum and its retained non-adjacent-form (NAF) opening.

  • accepts width 1–32 with exact signed digits {-1,0,1};
  • enforces canonical raw encodings, nonnegative scalar claims, isolated nonzero digits, and exact scalar = Σ digit[i]·2^i;
  • retains the original scalar and digits for a downstream consumer;
  • adds 12 focused correctness/adversarial tests, two metrics configurations, primary-source provenance, catalog evidence, NR-112, and OP-067.

Measured complete leaves

Each NAF leaf includes validation, exact comparisons of every retained item, and terminal OP_TRUE. The scalar-only baseline checks the same canonical nonnegative value and retained scalar, without asking for a decomposition. Witness byte counts include scalar/digit item counts and CompactSize lengths, but not Taproot framing or transaction context.

Metric NAF width 9, scalar 175 NAF width 16, scalar 21845 Scalar-only baseline
Script bytes 447 791 14
Data-witness bytes 17 28 4
Maximum canonical witness bytes 18 28 4 for these claims
Complete data items 10 17 1
Incremental hint items, all at entry 9 16 0
Serialized hint bytes 13 24 0
Combined stack peak 14 21 4
Static non-push opcodes 347 613 8
Executed non-push opcodes 286 493 8

The NAF side validates and retains a digit stream; the plain side does not. No scalar-compression or scalar-multiplication win is claimed. NR-112 records the baseline tradeoff and rejected source-depth variants. OP-067 tracks a consuming schedule.

Review corrections

  • A maximum-scalar counterexample, 2^31−1 = -1 + 2^31, exposed intermediate overflow. Negative reconstruction now forms 2a−1 as a+(a−1), with an explicit regression test.
  • Canonical witness maxima respect non-adjacent digit spacing: 18 and 28 bytes, not 22 and 36.
  • The scalar alias fixture is now an equal-value nonminimal encoding of 173.
  • Follow-up static review confirmed all three arithmetic branches and maximum-scalar reconstruction. The final catalog consistency issue is corrected.

Evidence and gates

  • locally-reproduced; execution unclassified.
  • Focused coverage includes all 81 four-digit ternary vectors, standard NAF for every value 0–255, max scalar, aliases at every input position with minimality disabled, invalid spacing, missing/extra items, unrelated caller state, and an over-limit stack mutation.
  • python3 tools/kb.py validate: 204 records, 784 configurations.
  • Required local non-field test command passed: 1,692 tests across 25 suites.
  • All three GitHub checks pass on c1c00f1293bf2fa866ea2d6c48d0240beffecfbe, including locked non-field tests and pinned repository Core comparisons.
  • Independent follow-up review found no remaining reconstruction defect.

No new-fragment Bitcoin Core differential, relay-policy, deployability, or cryptographic-security claim is made. Repository Core harness checks cover existing fixtures rather than differentially validating this primitive.

@brenorb
brenorb force-pushed the research/scriptnum-naf-witnesses branch from 5abe5ae to 9d07b3e Compare November 13, 2026 14:05
@brenorb
brenorb force-pushed the research/scriptnum-naf-witnesses branch from 9d07b3e to c1c00f1 Compare November 13, 2026 14:08
@brenorb
brenorb marked this pull request as ready for review November 13, 2026 15:17
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T15:23:54.977593Z c1c00f1 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c1c00f1293

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +71 to +72
The maximum canonical NAF data witnesses at these fixed widths are 18 and 28
bytes respectively: one item-count byte, three bytes for the scalar claim, and

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Include scalar sign padding in the width-16 witness maximum

For a width-16 opening, the valid NAF [0,1] repeated eight times represents 43690, whose canonical ScriptNum encoding is aa aa 00: three payload bytes because of sign padding. Under the stated serialization boundary, its data witness occupies 1 + (1 + 3) + 16 + 8 = 29 bytes. The published 28-byte maximum, also copied into the knowledge page and catalog's witness_bytes_max, therefore understates the supported-domain bound. Keep 28 for the measured 21845 fixture, but update the maximum to 29 and cover this sign-boundary case.

AGENTS.md reference: AGENTS.md:L55-L56

Useful? React with 👍 / 👎.

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.

1 participant