Repository navigation
num%perf: divide Arith256 by u64 with schoolbook division - #138
Conversation
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
|
Note This pull request has no conflicts! 🎊 🎉 🎊 |
|
✅ Final review complete — no blockers (commit f9923a9) · triage: normal |
6ab7ae9 to
289b89c
Compare
thepastaclaw
left a comment
There was a problem hiding this comment.
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:
normalbygpt-6.2-sol(effort low) — This is a small, contained arithmetic-logic change inpkgs/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); agentphase1-reviewer,muse-spark-1.3-contributor— rust-quality (completed, effort xhigh); agentphase1-reviewer - Phase 1 model:
muse-spark-1.3-contributor— not quota-gated; passed overgemini-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; agentsol-verifier - Phase 2 reviewers:
gpt-6.2-sol— general (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— rust-quality (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— security-auditor (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— general (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— rust-quality (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— security-auditor (completed, effort high); agentphase2-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.
There was a problem hiding this comment.
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:
remstays belowrhs, so each quotient limb fits inu64, 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
- 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.(asblock_proof's comment atarith256.rs:216-217does). That also answers thepastaclaw's nitpick. - One row at the cast's bound (thepastaclaw's first suggestion). Give
div_matches_wide_divisorthe dividend as a case parameter and add a row whose remainder is carried at its largest, for example a dividend whose most significant limb isu64::MAX - 1and whose other limbs areu64::MAX, with divisoru64::MAX. Not a separate test. - 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.
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.
289b89c to
f9923a9
Compare
thepastaclaw
left a comment
There was a problem hiding this comment.
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:
normalbygpt-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); agentphase1-reviewer,muse-spark-1.3-contributor— rust-quality (completed, effort xhigh); agentphase1-reviewer - Phase 1 model:
muse-spark-1.3-contributor— not quota-gated; passed overgemini-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; agentsol-verifier - Phase 2 reviewers:
gpt-6.2-sol— general (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— rust-quality (completed, effort high); agentphase2-reviewer,gpt-6.2-sol— security-auditor (completed, effort high); agentphase2-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)
Motivation
Dividing an
Arith256by au64widened 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
u128instead, in four steps.next_work_requiredper 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
comments and concerns without said assistance. I assume authorship of the code submitted.
Agent disclosure
May be assisted by Opus 5.5, medium effort.