Skip to content

Faster floor_sqrt, checked_sqrt implementation#1312

Open
andrewwhitehead wants to merge 5 commits into
RustCrypto:masterfrom
andrewwhitehead:fix/sqrt
Open

Faster floor_sqrt, checked_sqrt implementation#1312
andrewwhitehead wants to merge 5 commits into
RustCrypto:masterfrom
andrewwhitehead:fix/sqrt

Conversation

@andrewwhitehead

Copy link
Copy Markdown
Contributor

This re-applies the changes in #1258 with fixes for #1303. Changes to the BoxedUint proptests in #1310 also help to validate the results.

The sqrt logic is implemented on UintRef, meaning one version is used for both Uint and BoxedUint. This uses algorithm 1.12 in Modern Computer Arithmetic rather than 1.13, which scales better for large integers (approx O(2n)).

Addition and subtraction methods in UintRef are adjusted to accept an rhs smaller than self. This was inconsistently applied before.

checked_sqrt(_vartime) no longer performs an additional squaring, so its performance matches floor_sqrt.

Performance seems to be even better now, maybe due to bitlen inlining or other changes.

Sample benchmarks
sqrt/floor_sqrt, U256   time:   [136.87 ns 137.28 ns 137.74 ns]
                        change: [−79.375% −79.240% −79.049%] (p = 0.00 < 0.05)
                        Performance has improved.
sqrt/floor_sqrt, U512   time:   [297.86 ns 298.67 ns 299.63 ns]
                        change: [−77.808% −77.716% −77.608%] (p = 0.00 < 0.05)
                        Performance has improved.
sqrt/floor_sqrt_vartime, U256
                        time:   [67.951 ns 68.141 ns 68.360 ns]
                        change: [−82.235% −82.158% −82.067%] (p = 0.00 < 0.05)
                        Performance has improved.
sqrt/floor_sqrt, one Limb
                        time:   [45.111 ns 45.260 ns 45.440 ns]
                        change: [−68.637% −68.525% −68.397%] (p = 0.00 < 0.05)
                        Performance has improved.
sqrt/floor_sqrt_vartime, U256 one Limb
                        time:   [6.7108 ns 6.7672 ns 6.8396 ns]
                        change: [+17.539% +20.940% +23.453%] (p = 0.00 < 0.05)
                        Performance has regressed.
sqrt/checked_sqrt, U256 time:   [141.94 ns 142.42 ns 142.98 ns]
                        change: [−78.972% −78.899% −78.816%] (p = 0.00 < 0.05)
                        Performance has improved.
sqrt/checked_sqrt_vartime, U256
                        time:   [68.770 ns 69.016 ns 69.347 ns]
                        change: [−82.197% −82.130% −82.049%] (p = 0.00 < 0.05)
                        Performance has improved.

boxed_sqrt/floor_sqrt, 4096
                        time:   [2.4790 µs 2.4855 µs 2.4945 µs]
                        change: [−95.591% −95.581% −95.568%] (p = 0.00 < 0.05)
                        Performance has improved.
boxed_sqrt/floor_sqrt_vartime, 4096
                        time:   [2.2306 µs 2.2339 µs 2.2376 µs]
                        change: [−89.510% −89.493% −89.477%] (p = 0.00 < 0.05)
                        Performance has improved.
boxed_sqrt/checked_sqrt, 4096
                        time:   [2.4750 µs 2.4780 µs 2.4814 µs]
                        change: [−95.636% −95.628% −95.620%] (p = 0.00 < 0.05)
                        Performance has improved.
boxed_sqrt/checked_sqrt_vartime, 4096
                        time:   [2.2298 µs 2.2323 µs 2.2351 µs]
                        change: [−89.754% −89.732% −89.713%] (p = 0.00 < 0.05)
                        Performance has improved.

…n_vartime

Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.71795% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.22%. Comparing base (e25b054) to head (4333cb3).

Files with missing lines Patch % Lines
src/uint/ref_type/sqrt.rs 98.27% 3 Missing ⚠️
src/uint/sqrt.rs 96.87% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1312      +/-   ##
==========================================
+ Coverage   91.04%   91.22%   +0.18%     
==========================================
  Files         189      190       +1     
  Lines       22598    22850     +252     
==========================================
+ Hits        20574    20846     +272     
+ Misses       2024     2004      -20     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
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