Skip to content

Add Rust bindings#1650

Open
Rixxc wants to merge 7 commits intopq-code-package:mainfrom
Rixxc:rust-bindings-v2
Open

Add Rust bindings#1650
Rixxc wants to merge 7 commits intopq-code-package:mainfrom
Rixxc:rust-bindings-v2

Conversation

@Rixxc
Copy link
Copy Markdown

@Rixxc Rixxc commented Apr 14, 2026

This PR proposes a Rust binding for mlkem-native.

Some notes about the implementation:

  • The bindings implement the KEM trait from the RustCrypto kem crate, which makes it a easy drop in replacement for the RustCrypto ml-kem crate
  • Rust compiles the mlkem-native with avx2 support if the target architecture supports it
  • acvp_mlkem.rs implements the acvp test binary using the Rust API, which allows the binding to be tested against the acvp test vectors
  • The implementation performs a key check when deserializing the encapsulation and decapsulation keys. This is a bit redundant as the checks are also performed on every encap and decap operation by the mlkem-native library. I chose to do this as the trait definition of the kem crate do not allow for those operations to fail, which means the only option to handle those errors is to panic. But the keys are checked upon construction this case should never be triggered with a safe program.

Please let me know what you think about this approach.

Rixxc added 3 commits April 14, 2026 16:43
Signed-off-by: Aaron Kaiser <a_kaiser@posteo.de>
Signed-off-by: Aaron Kaiser <a_kaiser@posteo.de>
Signed-off-by: Aaron Kaiser <a_kaiser@posteo.de>
@Rixxc Rixxc force-pushed the rust-bindings-v2 branch 2 times, most recently from 978ec03 to 3587f8e Compare April 16, 2026 08:30
Signed-off-by: Aaron Kaiser <a_kaiser@posteo.de>
@Rixxc Rixxc force-pushed the rust-bindings-v2 branch from 3587f8e to 9056a4c Compare April 16, 2026 08:30
@Rixxc Rixxc marked this pull request as ready for review April 16, 2026 08:31
@Rixxc Rixxc requested a review from a team as a code owner April 16, 2026 08:31
Comment thread Cargo.lock Outdated
# It is not intended for manual editing.
version = 4

[[package]]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@Rixxc Can you comment on what all these packages are needed for?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

There are only a few dependencies in that that are actually used at build- and runtime. The dependencies are:

Runtime Dependencies

  • kem: which contains interface definition that the mlkem-native crate implements. This is the same interface that the ml-kem crate from RustCrypto uses. This allows for the mlkem-native crate to be used in generic code that expects objects that implement this interface and can be used as an easy drop-in replacement for the already existing and quit popular ml-kem crate.
  • crypto-common: Is a dependency from kem
  • hybrid-array: Is needed to implement the kem interface
  • typenum: pulled in by hybrid-array
  • rand_core: This crate contains interface definitions for RNGs, similar to the kem crate

Build Dependencies

  • cc: Is needed to compile the C code of mlkem-native during build time of this crate
  • find-msvc-tools: pulled in by cc, needed to compile the C code under Windows
  • shlex: pulled in by cc, used to escape strings for a POSIX shell

Other Dependencies

The rest of the dependencies are just dev dependencies. They are pulled in due to the getrandom feature of of the kem create. They are only used in the examples in the mlkem-native create.

Comment thread bindings/rust/mlkem-native/Cargo.toml Outdated
[package]
name = "mlkem-native"
version = "1.1.0-rc.1"
edition = "2021"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Wrong year?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The edition field is used by the compiler. It specifies a set of features that the Rust compiler has to support to be able to compile the project.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've updated the edition to the latest one

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've updated the edition to the latest one

Rixxc added 2 commits April 17, 2026 09:54
…kem-native bindings automatically

Signed-off-by: Aaron Kaiser <a_kaiser@posteo.de>
…ix and exclude Cargo.lock from copyright and license check

Signed-off-by: Aaron Kaiser <a_kaiser@posteo.de>
@Rixxc Rixxc force-pushed the rust-bindings-v2 branch from 14f2a3f to 8409886 Compare April 17, 2026 07:55
…t, add metadata to package and update the rust edition

Signed-off-by: Aaron Kaiser <a_kaiser@posteo.de>
@Rixxc
Copy link
Copy Markdown
Author

Rixxc commented Apr 17, 2026

I had to move the package definition to the root of the project as otherwise we most likely will encounter issues when trying to publish the crate

@Rixxc
Copy link
Copy Markdown
Author

Rixxc commented Apr 17, 2026

Shall I squash the commits before this PR gets merged? Also is the crate name of mlkem-native fine or should it rather be mlkem-native-rs?

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