Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust 1.74 #16416

Draft
wants to merge 5 commits into
base: dw/use-latest-master-proof-systems
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions nix/rust.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ let
# override stdenv.targetPlatform here, if neccesary
};
toolchainHashes = {
"1.72" = "sha256-dxE7lmCFWlq0nl/wKcmYvpP9zqQbBitAQgZ1zx9Ooik=";
"nightly-2023-09-01" =
"sha256-zek9JAnRaoX8V0U2Y5ssXVe9tvoQ0ERGXfUCUGYdrMA=";
"1.74" = "sha256-PjvuouwTsYfNKW5Vi5Ye7y+lL7SsWGBxCtBOOm2z14c=";
# copy the placeholder line with the correct toolchain name when adding a new toolchain
# That is,
# 1. Put the correct version name;
Expand All @@ -19,7 +17,7 @@ let
# error: hash mismatch in fixed-output derivation '/nix/store/XXXXX'
# specified: sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
# got: sha256-Q9UgzzvxLi4x9aWUJTn+/5EXekC98ODRU1TwhUs9RnY=
"placeholder" = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
# "placeholder" = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
};
# rust-toolchain.toml -> { rustc, cargo, rust-analyzer, ... }
rustChannelFromToolchainFileOf = file:
Expand Down
2 changes: 1 addition & 1 deletion src/lib/crypto/kimchi_bindings/stubs/rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# 4. figure out the hashes of the (now obsolete) docker images used in CI rules that are failing, grep for these hashes and replace them with the new hashes

[toolchain]
channel = "1.72"
channel = "1.74"
15 changes: 8 additions & 7 deletions src/lib/crypto/kimchi_bindings/stubs/src/caml/caml_pointer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
macro_rules! impl_caml_pointer {
($name: ident => $typ: ty) => {
#[derive(std::fmt::Debug, Clone, ::ocaml_gen::CustomType)]
pub struct $name(pub ::std::rc::Rc<$typ>);
pub struct $name(pub ::std::rc::Rc<std::cell::UnsafeCell<$typ>>);
Copy link
Member

Choose a reason for hiding this comment

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

Note as it is important for the Caml binding: UnsafeCell is implemented as:

#[lang = "unsafe_cell"]
#[stable(feature = "rust1", since = "1.0.0")]
#[repr(transparent)]
#[rustc_pub_transparent]
pub struct UnsafeCell<T: ?Sized> {
    value: T,
}

Copy link
Member

Choose a reason for hiding this comment

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

I guess we're adding a new indirection? We might lose some performances?

Copy link
Member Author

@martyall martyall Dec 13, 2024

Choose a reason for hiding this comment

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

Note as it is important for the Caml binding: UnsafeCell is implemented as:

The nightly tests were passing with this change, does this mean that that the ocaml bindings lib was able to resolve this on its own or is it just coincidence because maybe the runtime rep is the same

Copy link
Member Author

@martyall martyall Dec 15, 2024

Choose a reason for hiding this comment

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

I guess we're adding a new indirection? We might lose some performances?

My intuition is that this UnsafeCell should be optimized away. We can discuss


impl $name {
extern "C" fn caml_pointer_finalize(v: ocaml::Raw) {
Expand Down Expand Up @@ -32,15 +32,17 @@ macro_rules! impl_caml_pointer {

impl $name {
pub fn create(x: $typ) -> $name {
$name(::std::rc::Rc::new(x))
$name(::std::rc::Rc::new(std::cell::UnsafeCell::new(x)))
}
}

impl ::std::ops::Deref for $name {
type Target = $typ;

fn deref(&self) -> &Self::Target {
&*self.0
unsafe {
martyall marked this conversation as resolved.
Show resolved Hide resolved
&*self.0.get()
}
}
}

Expand All @@ -49,16 +51,15 @@ macro_rules! impl_caml_pointer {
unsafe {
// Wholely unsafe, Batman!
// We would use [`get_mut_unchecked`] here, but it is nightly-only.
// Instead, we get coerce our constant pointer to a mutable
// pointer, in the knowledge that
// Instead, we use UnsafeCell in the knowledge that
// * all of our mutations called from OCaml are blocking, so
// we won't have multiple live mutable references live
// we won't have multiple live mutable references
// simultaneously, and
// * the underlying pointer is in the correct state to be
// mutable, since we can call [`get_mut_unchecked`] in
// nightly, or can call [`get_mut`] and unwrap if this is
// the only live reference.
&mut *(((&*self.0) as *const Self::Target) as *mut Self::Target)
Copy link
Member Author

Choose a reason for hiding this comment

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

1.74 throws a compiler error here. There might be some other way to get it to stop complaining, but I think this is equivalent without the illegal cast

&mut *self.0.get()
Copy link
Member

Choose a reason for hiding this comment

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

Note: implementation of get.

    #[inline(always)]
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_const_stable(feature = "const_unsafecell_get", since = "1.32.0")]
    #[rustc_never_returns_null_ptr]
    pub const fn get(&self) -> *mut T {
        // We can just cast the pointer from `UnsafeCell<T>` to `T` because of
        // #[repr(transparent)]. This exploits std's special status, there is
        // no guarantee for user code that this will work in future versions of the compiler!
        self as *const UnsafeCell<T> as *const T as *mut T
    }

Copy link
Member

Choose a reason for hiding this comment

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

Nit: cargo fmt reformats this line. It seems we do not run in the Mina CI clippy nor fmt.

Copy link
Member Author

@martyall martyall Dec 13, 2024

Choose a reason for hiding this comment

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

Note: implementation of get.

This makes me think someone was trying to get around using UnsafeCell when they wrote this the first time. I would hope that this would be optimized away

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn caml_pasta_fp_plonk_proof_create(
.collect()
};

let witness: Vec<Vec<_>> = witness.iter().map(|x| (*x.0).clone()).collect();
let witness: Vec<Vec<_>> = witness.iter().map(|x| (**x).clone()).collect();
let witness: [Vec<_>; COLUMNS] = witness
.try_into()
.map_err(|_| ocaml::Error::Message("the witness should be a column of 15 vectors"))?;
Expand Down Expand Up @@ -140,7 +140,7 @@ pub fn caml_pasta_fp_plonk_proof_create_and_verify(
.collect()
};

let witness: Vec<Vec<_>> = witness.iter().map(|x| (*x.0).clone()).collect();
let witness: Vec<Vec<_>> = witness.iter().map(|x| (**x).clone()).collect();
let witness: [Vec<_>; COLUMNS] = witness
.try_into()
.map_err(|_| ocaml::Error::Message("the witness should be a column of 15 vectors"))?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn caml_pasta_fq_plonk_proof_create(
.collect()
};

let witness: Vec<Vec<_>> = witness.iter().map(|x| (*x.0).clone()).collect();
let witness: Vec<Vec<_>> = witness.iter().map(|x| (**x).clone()).collect();
let witness: [Vec<_>; COLUMNS] = witness
.try_into()
.expect("the witness should be a column of 15 vectors");
Expand Down
2 changes: 1 addition & 1 deletion src/lib/crypto/kimchi_bindings/wasm/rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# This should stay in line with the versions in
# - kimchi_bindings/js/node_js/build.sh
# - kimchi_bindings/js/web/build.sh
channel = "nightly-2023-09-01" # roughly matches 1.72
channel = "1.74"
1 change: 0 additions & 1 deletion src/lib/crypto/kimchi_bindings/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(get_mut_unchecked)]
//! The Marlin_plonk_stubs crate exports some functionalities
//! and structures from the following the Rust crates to OCaml:
//!
Expand Down