Skip to content

num%perf: divide Arith256 by u64 with schoolbook division - #138

Merged
PastaPastaPasta merged 1 commit into
dashpay:developfrom
ZocoLini:perf/num-div-u64
Nov 19, 2026
Merged

PastaPastaPasta merged 1 commit into
dashpay:developfrom
ZocoLini:perf/num-div-u64

Conversation

@ZocoLini

@ZocoLini ZocoLini commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Dividing an Arith256 by a u64 widened the divisor and ran the bit-serial 256-bit division, one iteration per quotient bit. Dash's DGW retarget does about 24 of these per header, which made it the main cost of header sync in debug builds.

Part of dashpay/rust-dashcore#1431.

Additional Information

This divides limb by limb with u128 instead, in four steps. next_work_required per header goes from 168 to 6 µs in debug and from 7.8 to 0.95 µs in release.

How Has This Been Tested?

On an Apple M5, a debug rust-dashcore testnet sync from genesis drops from 467 to 246 s with this change alone.

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional tests
  • I have made corresponding changes to the documentation
  • I did not use agentic assistance (i.e. LLMs, AI) except as disclosed and can answer questions,
    comments and concerns without said assistance. I assume authorship of the code submitted.
  • I have assigned this pull request to a milestone (for repository code-owners and collaborators only)

Agent disclosure

May be assisted by Opus 5.5, medium effort.

@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown

Note

This pull request has no conflicts! 🎊 🎉 🎊

@thepastaclaw

thepastaclaw commented Nov 18, 2026 •

Copy link
Copy Markdown

✅ Final review complete — no blockers (commit f9923a9) · triage: normal

@kwvg kwvg added this to the 0.4 milestone Nov 18, 2026

@thepastaclaw thepastaclaw 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.

Final validation — Phase 1 + Phase 2

The four-limb division preserves Dash Core’s integer quotient for nonzero divisors and the SDK’s documented zero-divisor behavior. No blockers were found; the review suggests a boundary test, a short comment explaining the narrowing cast, and commands to reproduce the DGW timings. The current CI shows passing build, test, and lint checks, including macOS; the automated review check remains pending.

🟡 2 suggestion(s) | 💬 1 nitpick(s)

Approved: nothing here blocks the merge. The non-blocking findings are optional: fix them forward, in this PR or a follow-up, without waiting for another review round.

Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: muse-spark-1.3-contributor (agent: phase1-reviewer, role: rust-quality); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 4: gpt-6.2-sol (agent: phase2-reviewer, role: rust-quality); reviewer 5: gpt-6.2-sol (agent: phase2-reviewer, role: security-auditor); reviewer 6: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 7: gpt-6.2-sol (agent: phase2-reviewer, role: rust-quality); reviewer 8: gpt-6.2-sol (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: normal by gpt-6.2-sol (effort low) — This is a small, contained arithmetic-logic change in pkgs/num/src/arith256.rs, but it changes division behavior used in difficulty retarget calculations, so correctness warrants ordinary review rather than a trivial or low-effort classification.
  • Phase 1 reviewers: muse-spark-1.3-contributor — general (completed, effort xhigh); agent phase1-reviewer, muse-spark-1.3-contributor — rust-quality (completed, effort xhigh); agent phase1-reviewer
  • Phase 1 model: muse-spark-1.3-contributor — not quota-gated; passed over gemini-3.8-flash-high (antigravity quota lookup failed), glm-5.3-flash (not used above high effort; tier asks max)
  • Second Phase-2 pass: on this first full review every Phase-2 reviewer also ran a second, independent time, and the verifier weighed both sets
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-sol — general (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — rust-quality (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — security-auditor (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — general (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — rust-quality (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — security-auditor (completed, effort high); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.

In `pkgs/num/src/arith256.rs`:
- [SUGGESTION] pkgs/num/src/arith256.rs:394-400: Test the maximum carried remainder and quotient limb
  The scalar division test compares one fixed dividend with the wide implementation for three divisors. It exercises ordinary remainder propagation but does not pin the upper boundary of the new loop. Using the existing `from_array` helper, divide the big-endian limbs `[u64::MAX - 1, u64::MAX, u64::MAX, u64::MAX]` by `u64::MAX` and assert the quotient is `[0, u64::MAX, u64::MAX, u64::MAX]`. The first step leaves `rhs - 1` as the remainder, and each later step carries that same remainder while emitting the largest possible quotient limb.
- [SUGGESTION] pkgs/num/src/arith256.rs:389-390: Provide commands to reproduce the DGW timings
  The PR reports before-and-after DGW timings but does not give the commands, the rust-dashcore revision, or how each SDK revision was selected. The SDK’s existing benchmark in `pkgs/num/bench/main.rs:18–23` passes an `Arith256` divisor, so it does not exercise this overload. Add the commands and dependency setup for the debug and release comparisons to the PR’s “How Has This Been Tested?” section.
- [NITPICK] pkgs/num/src/arith256.rs:402: Document why the quotient limb fits in `u64`
  The cast is exact: `rem` starts at zero and each modulo update keeps it below `rhs`, so appending a limb yields `current < rhs * 2^64`. State that invariant next to the cast so readers can distinguish intentional narrowing from silent truncation.

Comment thread pkgs/num/src/arith256.rs Resolved
Comment thread pkgs/num/src/arith256.rs Resolved
Comment thread pkgs/num/src/arith256.rs Resolved

@PastaPastaPasta PastaPastaPasta left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Steward review at 289b89c: two small code changes and the description, then I merge. The maintainer is away, and the steward reviews and merges in his place (#131).

The division is correct. An independent review checked that the new Div<u64> gives exactly what self / Self::from_u64(rhs) gave:

  • by the argument: rem stays below rhs, so each quotient limb fits in u64, and the limbs are read from the most significant one down;
  • by a local check over about 2.4 million dividend and divisor pairs, covering zero, edge and random values, with overflow checks on. The check fails if .rev() is removed.

It also confirmed as_chunks is within the 1.89 MSRV, and every check passes.

Changes

  1. The comment (arith256.rs:389-390). Describing how the other division works is the kind of comment the maintainer removes as drift risk (#86). Replace it with the invariant that makes the cast exact, in one line, for example // rem < rhs, so each quotient limb fits in u64. (as block_proof's comment at arith256.rs:216-217 does). That also answers thepastaclaw's nitpick.
  2. One row at the cast's bound (thepastaclaw's first suggestion). Give div_matches_wide_divisor the dividend as a case parameter and add a row whose remainder is carried at its largest, for example a dividend whose most significant limb is u64::MAX - 1 and whose other limbs are u64::MAX, with divisor u64::MAX. Not a separate test.
  3. The description goes into the merge commit, so it uses the repository's template: Motivation, Additional Information, Breaking Changes, How Has This Been Tested? (the commands you ran), the checklist, and the Agent disclosure section if any AI tool was used. Your current text fits under Motivation and Additional Information. A description edit needs no push.

Amend the commit and force-push; this repository keeps one commit per change rather than fix-up commits.

Not asked for: commands to reproduce the timings (thepastaclaw's second suggestion), or a benchmark. The timings are motivation, and the test shows the result is unchanged.


🤖 Posted autonomously by Claude (claude-opus-5-5) on behalf of pasta.

@github-actions github-actions Bot added the status:behind This pull request can be rebased on the base branch. label Nov 18, 2026
Dividing by a `u64` ran the bit-serial 256-bit division, one step per
quotient bit, and DGW does about 24 per header. Dividing limb by limb
with `u128` takes four steps instead.

@thepastaclaw thepastaclaw 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.

Re-review — Final validation — Phase 1 + Phase 2

The schoolbook Arith256 / u64 implementation preserves the established zero-divisor behavior and is supported by the new maximum-boundary regression case. The available CI checks are green, with only the documented reproducibility suggestion remaining unresolved.

🟡 1 suggestion(s)

Approved: nothing here blocks the merge. The non-blocking findings are optional: fix them forward, in this PR or a follow-up, without waiting for another review round.

Still open from earlier rounds (1)

  • 🟡 Suggestion: Provide commands to reproduce the DGW timings — pkgs/num/src/arith256.rs:389-390 (thread)
Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: muse-spark-1.3-contributor (agent: phase1-reviewer, role: rust-quality); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 4: gpt-6.2-sol (agent: phase2-reviewer, role: rust-quality); reviewer 5: gpt-6.2-sol (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: normal by gpt-6.2-sol (effort low) — This changes the arithmetic implementation of Arith256 division, a core logic path whose correctness requires validating quotient and remainder behavior across edge cases, but the contained 27-line diff is not both large and critical under the defined criteria.
  • Phase 1 reviewers: muse-spark-1.3-contributor — general (completed, effort xhigh); agent phase1-reviewer, muse-spark-1.3-contributor — rust-quality (completed, effort xhigh); agent phase1-reviewer
  • Phase 1 model: muse-spark-1.3-contributor — not quota-gated; passed over gemini-3.8-flash-high (antigravity quota lookup failed), glm-5.3-flash (not used above high effort; tier asks max)
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-sol — general (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — rust-quality (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — security-auditor (completed, effort high); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.

In `pkgs/num/src/arith256.rs`:
- [SUGGESTION] pkgs/num/src/arith256.rs:389-390: Provide commands to reproduce the DGW timings
  (carried; thread: https://github.com/dashpay/base-sdk/pull/138#discussion_r4618902872)

@PastaPastaPasta
PastaPastaPasta merged commit 84e9b68 into dashpay:develop Nov 19, 2026
37 checks passed
@PastaPastaPasta

Copy link
Copy Markdown
Member

@ZocoLini Merged into develop as 84e9b68.


🤖 Posted autonomously by Claude (claude-opus-5-5) on behalf of pasta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:behind This pull request can be rebased on the base branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants