Skip to content

feat: make rust const#2080

Merged
JasonGross merged 3 commits into
mit-plv:masterfrom
Daniel-Aaron-Bloom:rust-const
Jun 3, 2025
Merged

feat: make rust const#2080
JasonGross merged 3 commits into
mit-plv:masterfrom
Daniel-Aaron-Bloom:rust-const

Conversation

@Daniel-Aaron-Bloom

Copy link
Copy Markdown
Contributor

&mut is const-stable as of rust 1.83. As this is a pretty big jump in MSRV, it should probably be a minor version bump.

Unfortunately using Index and IndexMut in const functions isn't const, except on built-in arrays. However you can get around this with an internal helper type (IndexConst in this PR) doing the array indexing. This does make the Index and IndexMut impls internally redundant, but they're harmless and removing them would break downstream crates, which would be a major version bump.

At some point in the distant future when const traits are stabilized, IndexConst can go away and the original code will just work with essentially no change to public API. But that's probably a few years away.

Comment thread fiat-rust/Cargo.toml
Comment thread fiat-rust/Cargo.toml Outdated

@JasonGross JasonGross left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you!

let x23: u128 = (((arg1[0]) as u128) * ((arg2[2]) as u128));
let x24: u128 = (((arg1[0]) as u128) * ((arg2[1]) as u128));
let x25: u128 = (((arg1[0]) as u128) * ((arg2[0]) as u128));
pub const fn fiat_25519_carry_mul(mut out1: &mut fiat_25519_tight_field_element, arg1: &fiat_25519_loose_field_element, arg2: &fiat_25519_loose_field_element) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do we need mut out1 now? And will this break any downstream crates?

@Daniel-Aaron-Bloom Daniel-Aaron-Bloom Apr 24, 2025

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.

The short answer is "no, everything is fine". mut in only part of the internal signature of out1 not the signature of fiat_25519_carry_mul.

The reason why out1 need to be mut is a quirk of how rust mutable borrowing works. There are multiple indexing operations into out1, which means multiple IndexConsts are constructed. IndexConst take ownership of whatever is passed in, which is fine when dealing with & which are Copy, but &mut is not Copy. To get around this we borrow out1 and pass that in instead. The issue is if we used &out1 to borrow, then we couldn't get &mut out because once you're non-mut you're stuck that way. So instead we use &mut out1, but for that to work the local variable out1 must be mutable.

As state above though, this doesn't impact callers because it's only the local variable out1 which is being changed to be mutable. It's the same as putting let mut out1 = out1; as the first line and shadowing the variable for the whole function.

@@ -87,6 +101,23 @@ Module Rust.
(["";
"#![allow(unused_parens)]";
"#![allow(non_camel_case_types)]";
"";
"struct IndexConst<T: ?Sized>(T);";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It might be worth adding a Rust comment here about why this type exists (basically just the contents of the PR message)

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.

Done.

Daniel-Aaron-Bloom and others added 3 commits June 3, 2025 14:32
Co-authored-by: Jason Gross <jasongross9@gmail.com>
@JasonGross JasonGross merged commit 0891903 into mit-plv:master Jun 3, 2025
14 of 15 checks passed
@Daniel-Aaron-Bloom Daniel-Aaron-Bloom deleted the rust-const branch June 4, 2025 00:21
@tarcieri tarcieri mentioned this pull request Sep 3, 2025
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.

2 participants