-
Notifications
You must be signed in to change notification settings - Fork 24
Add an ML-KEM implementation #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1809696
Add ML-KEM implementation
bifurcation 60c4890
Pre-compute constants using const fn
bifurcation 9fe6e2f
Use constant-time selection in decapsulation
bifurcation d7ceaf9
Make determinstic feature non-default
bifurcation 20089d7
Remove redundant .gitignore
bifurcation 3bad718
Add workspace-level Cargo.toml
bifurcation 4e5404e
Convert from generic-array to hybrid-array
bifurcation 48e9e2f
Use released version of hybrid-array
bifurcation d0945d4
Use standard KEM interfaces
bifurcation 4d83d5d
Cargo clippy cleanup
bifurcation File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[workspace] | ||
resolver = "2" | ||
members = [ | ||
"ml-kem", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
name = "ml-kem" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[features] | ||
default = [] | ||
deterministic = [] # Expose deterministic generation and encapsulation functions | ||
|
||
[dependencies] | ||
const-default = "1.0.0" | ||
crypto-common = { version = "0.1.6", features = ["getrandom"] } | ||
generic-array = { version = "1.0.0", features = ["const-default"] } | ||
hybrid-array = { version = "0.2.0-rc.6" } | ||
sha3 = "0.10.8" | ||
|
||
[dev-dependencies] | ||
criterion = "0.5.1" | ||
hex = "0.4.3" | ||
hex-literal = "0.4.1" | ||
rand = "0.8.5" | ||
|
||
[profile.bench] | ||
debug = true | ||
|
||
[[bench]] | ||
name = "mlkem" | ||
harness = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use crypto_common::rand_core::CryptoRngCore; | ||
use hybrid_array::{Array, ArraySize}; | ||
use ml_kem::*; | ||
|
||
pub fn rand<L: ArraySize>(rng: &mut impl CryptoRngCore) -> Array<u8, L> { | ||
let mut val = Array::<u8, L>::default(); | ||
rng.fill_bytes(&mut val); | ||
val | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut rng = rand::thread_rng(); | ||
let d: B32 = rand(&mut rng); | ||
let z: B32 = rand(&mut rng); | ||
let m: B32 = rand(&mut rng); | ||
|
||
let (dk, ek) = MlKem768::generate_deterministic(&d, &z); | ||
let dk_bytes = dk.as_bytes(); | ||
let ek_bytes = ek.as_bytes(); | ||
let (ct, _sk) = ek.encapsulate(&mut rng).unwrap(); | ||
|
||
// Key generation | ||
c.bench_function("keygen", |b| { | ||
b.iter(|| { | ||
let (dk, ek) = <MlKem768 as KemCore>::generate_deterministic(&d, &z); | ||
let _dk_bytes = dk.as_bytes(); | ||
let _ek_bytes = ek.as_bytes(); | ||
}) | ||
}); | ||
|
||
// Encapsulation | ||
c.bench_function("encapsulate", |b| { | ||
b.iter(|| { | ||
let ek = <MlKem768 as KemCore>::EncapsulationKey::from_bytes(&ek_bytes); | ||
ek.encapsulate_deterministic(&m).unwrap(); | ||
}) | ||
}); | ||
|
||
// Decapsulation | ||
c.bench_function("decapsulate", |b| { | ||
b.iter(|| { | ||
let dk = <MlKem768 as KemCore>::DecapsulationKey::from_bytes(&dk_bytes); | ||
dk.decapsulate(&ct).unwrap(); | ||
}) | ||
}); | ||
|
||
// Round trip | ||
c.bench_function("round_trip", |b| { | ||
b.iter(|| { | ||
let (dk, ek) = <MlKem768 as KemCore>::generate_deterministic(&d, &z); | ||
let (ct, _sk) = ek.encapsulate(&mut rng).unwrap(); | ||
dk.decapsulate(&ct).unwrap(); | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.