Skip to content

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

Draft
brian-pane wants to merge 2 commits into
ctz:mainfrom
brian-pane:vartime
Draft

Constant-time impl for part (but not all) of PosInt::invert_vartime#153
brian-pane wants to merge 2 commits into
ctz: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 (f4eff21) with main (410dd9e)

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 (410dd9e) to head (f4eff21).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #153   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files         184      184           
  Lines       50802    50813   +11     
=======================================
+ Hits        50684    50695   +11     
  Misses        118      118           

☔ 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.

@brian-pane brian-pane marked this pull request as draft May 15, 2026 15:40
@brian-pane
Copy link
Copy Markdown
Contributor Author

Now that I have a ctgrind working in my test environment, I'll iterate some more on this to see if I can remove the remaining variable-time operations.

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