Skip to content

Constant-time impl for part (but not all) of PosInt::invert_vartime#153

Open
brian-pane wants to merge 2 commits intoctz:mainfrom
brian-pane:vartime
Open

Constant-time impl for part (but not all) of PosInt::invert_vartime#153
brian-pane wants to merge 2 commits intoctz:mainfrom
brian-pane:vartime

Conversation

@brian-pane
Copy link
Copy Markdown
Contributor

No description provided.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 6, 2026

Merging this PR will not alter performance

✅ 155 untouched benchmarks


Comparing brian-pane:vartime (9ab190f) with main (3353ba5)

Open in CodSpeed

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (3353ba5) to head (9ab190f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #153      +/-   ##
==========================================
- Coverage   99.76%   99.76%   -0.01%     
==========================================
  Files         184      184              
  Lines       50832    50843      +11     
==========================================
+ Hits        50714    50723       +9     
- Misses        118      120       +2     

☔ View full report in Codecov by Sentry.
📢 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.

@brian-pane brian-pane marked this pull request as draft March 10, 2026 00:02
@brian-pane brian-pane marked this pull request as ready for review March 10, 2026 00:38
@brian-pane
Copy link
Copy Markdown
Contributor Author

The performance regression reported by CodSpeed seems to be unrelated. It happens only in the libcrux ML-KEM test, and based on my reading of the code and CodSpeed's flamegraphs, that test doesn't appear to have any dependency on PosInt::invert_vartime.

@brian-pane
Copy link
Copy Markdown
Contributor Author

The performance regression reported by CodSpeed seems to be unrelated. It happens only in the libcrux ML-KEM test, and based on my reading of the code and CodSpeed's flamegraphs, that test doesn't appear to have any dependency on PosInt::invert_vartime.

I just rebased, and the CodSpeed test is passing now.

Comment on lines +243 to +244
let mask = mask_bit << 63;
let mask = mask | mask.saturating_sub(1);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

On x86_64, rustc+llvm generates a cmov operation for the saturating_sub.
On Aarch64, it generates a csel operation. (Compiler Explorer)

As far as I can tell, both those instructions should run in constant time, independent of their inputs. But if there's a better idiom for "sign-extend this bit to fill 64 bits," I'm happy to switch to that.

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