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

Add real CI for ML-KEM #5

Merged
merged 11 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
56 changes: 26 additions & 30 deletions .github/workflows/ml-kem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,30 @@ jobs:
with:
msrv: 1.74.0

# TODO
# no_std:
# needs: set-msrv
# runs-on: ubuntu-latest
# strategy:
# matrix:
# rust:
# - ${{needs.set-msrv.outputs.msrv}}
# - stable
# target:
# - thumbv7em-none-eabi
# - wasm32-unknown-unknown
# steps:
# - uses: actions/checkout@v4
# - uses: RustCrypto/actions/cargo-cache@master
# - uses: dtolnay/rust-toolchain@master
# with:
# toolchain: ${{ matrix.rust }}
# targets: ${{ matrix.target }}
# - run: cargo build --no-default-features --target ${{ matrix.target }}
no_std:
needs: set-msrv
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- ${{needs.set-msrv.outputs.msrv}}
- stable
target:
- thumbv7em-none-eabi
- wasm32-unknown-unknown
steps:
- uses: actions/checkout@v4
- uses: RustCrypto/actions/cargo-cache@master
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
- run: cargo build --no-default-features --target ${{ matrix.target }}

# TODO
# minimal-versions:
# uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
# with:
# working-directory: ${{ github.workflow }}
minimal-versions:
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
with:
working-directory: ${{ github.workflow }}

test:
needs: set-msrv
Expand All @@ -64,11 +62,9 @@ jobs:
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- run: cargo build --all-features
# TODO(tarcieri): remove cargo build, run cargo test
#- run: cargo test --no-default-features
#- run: cargo test
#- run: cargo test --all-features
- run: cargo test --no-default-features
- run: cargo test
- run: cargo test --all-features

# TODO(tarcieri): miri
# miri:
Expand Down
4 changes: 1 addition & 3 deletions ml-kem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ 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" }
rand_core = "0.6.4"
sha3 = "0.10.8"
tarcieri marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion ml-kem/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]

use crypto_common::rand_core::CryptoRngCore;
use hybrid_array::{Array, ArraySize};
use rand_core::CryptoRngCore;
use sha3::{
digest::{ExtendableOutput, Update, XofReader},
Digest, Sha3_256, Sha3_512, Shake128, Shake256,
Expand Down
2 changes: 1 addition & 1 deletion ml-kem/src/kem.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::marker::PhantomData;
use crypto_common::rand_core::CryptoRngCore;
use hybrid_array::typenum::U32;
use rand_core::CryptoRngCore;

use crate::crypto::{rand, G, H, J};
use crate::param::{DecapsulationKeySize, EncapsulationKeySize, EncodedCiphertext, KemParams};
Expand Down
21 changes: 18 additions & 3 deletions ml-kem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ pub mod kem;
mod param;

use core::fmt::Debug;
use crypto_common::rand_core::CryptoRngCore;
use hybrid_array::{
typenum::{U10, U11, U2, U3, U4, U5},
Array,
};
use rand_core::CryptoRngCore;

#[cfg(feature = "deterministic")]
pub use util::B32;
Expand Down Expand Up @@ -132,10 +132,25 @@ pub trait KemCore {
type CiphertextSize: ArraySize;

/// A decapsulation key for this KEM
type DecapsulationKey: Decapsulate<Ciphertext<Self>, SharedKey<Self>> + Debug + PartialEq;
type DecapsulationKey: Decapsulate<Ciphertext<Self>, SharedKey<Self>>
+ EncodedSizeUser
+ Debug
+ PartialEq;

#[cfg(not(feature = "deterministic"))]
/// An encapsulation key for this KEM
bifurcation marked this conversation as resolved.
Show resolved Hide resolved
type EncapsulationKey: Encapsulate<Ciphertext<Self>, SharedKey<Self>> + Debug + PartialEq;
type EncapsulationKey: Encapsulate<Ciphertext<Self>, SharedKey<Self>>
+ EncodedSizeUser
+ Debug
+ PartialEq;

#[cfg(feature = "deterministic")]
/// An encapsulation key for this KEM
bifurcation marked this conversation as resolved.
Show resolved Hide resolved
type EncapsulationKey: Encapsulate<Ciphertext<Self>, SharedKey<Self>>
+ EncapsulateDeterministic<Ciphertext<Self>, SharedKey<Self>>
+ EncodedSizeUser
+ Debug
+ PartialEq;

/// Generate a new (decapsulation, encapsulation) key pair
fn generate(rng: &mut impl CryptoRngCore) -> (Self::DecapsulationKey, Self::EncapsulationKey);
Expand Down
12 changes: 6 additions & 6 deletions ml-kem/tests/vectors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(feature = "deterministic")]

use generic_array::GenericArray;
use hybrid_array::Array;
use ml_kem::*;

pub struct GenerateVector {
Expand All @@ -12,8 +12,8 @@ pub struct GenerateVector {

impl GenerateVector {
pub fn verify<K: KemCore>(&self) {
let d = GenericArray::from_slice(&self.d);
let z = GenericArray::from_slice(&self.z);
let d = Array::from_slice(&self.d);
let z = Array::from_slice(&self.z);
let (dk, ek) = K::generate_deterministic(d, z);
assert_eq!(dk.as_bytes().as_slice(), self.dk);
assert_eq!(ek.as_bytes().as_slice(), self.ek);
Expand All @@ -35,10 +35,10 @@ pub struct EncapsulateVector {

impl EncapsulateVector {
pub fn verify<K: KemCore>(&self) {
let m = GenericArray::from_slice(&self.m);
let m = Array::from_slice(&self.m);
let ek_bytes = Encoded::<K::EncapsulationKey>::from_slice(self.ek);
let ek = K::EncapsulationKey::from_bytes(ek_bytes);
let (k, c) = ek.encapsulate_deterministic(m);
let (c, k) = ek.encapsulate_deterministic(m).unwrap();
assert_eq!(k.as_slice(), &self.k);
assert_eq!(c.as_slice(), self.c);
}
Expand All @@ -56,7 +56,7 @@ impl DecapsulateVector {
let dk = K::DecapsulationKey::from_bytes(dk_bytes);

let c_bytes = Ciphertext::<K>::from_slice(self.c);
let k = dk.decapsulate(c_bytes);
let k = dk.decapsulate(c_bytes).unwrap();
assert_eq!(k.as_slice(), &self.k);
}
}
Loading