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

fix: replaced empty tuple error types with Infallible #59

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions dhkem/src/ecdh_kem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Generic Elliptic Curve Diffie-Hellman KEM adapter.

use crate::{DhDecapsulator, DhEncapsulator, DhKem};
use core::marker::PhantomData;
use core::{convert::Infallible, marker::PhantomData};
use elliptic_curve::{
ecdh::{EphemeralSecret, SharedSecret},
CurveArithmetic, PublicKey,
Expand All @@ -19,7 +19,7 @@ impl<C> Encapsulate<PublicKey<C>, SharedSecret<C>> for DhEncapsulator<PublicKey<
where
C: CurveArithmetic,
{
type Error = ();
type Error = Infallible;

fn encapsulate(
&self,
Expand All @@ -38,7 +38,7 @@ impl<C> Decapsulate<PublicKey<C>, SharedSecret<C>> for DhDecapsulator<EphemeralS
where
C: CurveArithmetic,
{
type Error = ();
type Error = Infallible;

fn decapsulate(&self, encapsulated_key: &PublicKey<C>) -> Result<SharedSecret<C>, Self::Error> {
let ss = self.0.diffie_hellman(encapsulated_key);
Expand Down
5 changes: 3 additions & 2 deletions dhkem/src/x25519_kem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{DhDecapsulator, DhEncapsulator, DhKem};
use core::convert::Infallible;
use kem::{Decapsulate, Encapsulate};
use rand_core::CryptoRngCore;
use x25519::{PublicKey, ReusableSecret, SharedSecret};
Expand All @@ -9,7 +10,7 @@ use x25519::{PublicKey, ReusableSecret, SharedSecret};
pub struct X25519Kem;

impl Encapsulate<PublicKey, SharedSecret> for DhEncapsulator<PublicKey> {
type Error = ();
type Error = Infallible;

fn encapsulate(
&self,
Expand All @@ -25,7 +26,7 @@ impl Encapsulate<PublicKey, SharedSecret> for DhEncapsulator<PublicKey> {
}

impl Decapsulate<PublicKey, SharedSecret> for DhDecapsulator<ReusableSecret> {
type Error = ();
type Error = Infallible;

fn decapsulate(&self, encapsulated_key: &PublicKey) -> Result<SharedSecret, Self::Error> {
let ss = self.0.diffie_hellman(encapsulated_key);
Expand Down
18 changes: 8 additions & 10 deletions ml-kem/src/kem.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::convert::Infallible;
use core::marker::PhantomData;
use hybrid_array::typenum::U32;
use rand_core::CryptoRngCore;
Expand Down Expand Up @@ -85,12 +86,12 @@ impl<P> ::kem::Decapsulate<EncodedCiphertext<P>, SharedKey> for DecapsulationKey
where
P: KemParams,
{
// Decapsulation is infallible
// XXX(RLB): Maybe we should reflect decryption failure as an error?
// TODO(RLB) Make Infallible
type Error = ();
type Error = Infallible;

fn decapsulate(&self, encapsulated_key: &EncodedCiphertext<P>) -> Result<SharedKey, ()> {
fn decapsulate(
&self,
encapsulated_key: &EncodedCiphertext<P>,
) -> Result<SharedKey, Self::Error> {
let mp = self.dk_pke.decrypt(encapsulated_key);
let (Kp, rp) = G(&[&mp, &self.ek.h]);
let Kbar = J(&[self.z.as_slice(), encapsulated_key.as_ref()]);
Expand Down Expand Up @@ -187,9 +188,7 @@ impl<P> ::kem::Encapsulate<EncodedCiphertext<P>, SharedKey> for EncapsulationKey
where
P: KemParams,
{
// TODO(RLB) Make Infallible
// TODO(RLB) Swap the order of the
type Error = ();
type Error = Infallible;

fn encapsulate(
&self,
Expand All @@ -205,8 +204,7 @@ impl<P> crate::EncapsulateDeterministic<EncodedCiphertext<P>, SharedKey> for Enc
where
P: KemParams,
{
// TODO(RLB) Make Infallible
type Error = ();
type Error = Infallible;

fn encapsulate_deterministic(
&self,
Expand Down
20 changes: 9 additions & 11 deletions x-wing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
//! assert_eq!(ss_sender, ss_receiver);
//! ```

use core::convert::Infallible;

use kem::{Decapsulate, Encapsulate};
use ml_kem::array::ArrayN;
use ml_kem::{kem, EncodedSizeUser, KemCore, MlKem768, MlKem768Params, B32};
Expand Down Expand Up @@ -73,7 +75,7 @@ pub struct EncapsulationKey {
}

impl Encapsulate<Ciphertext, SharedSecret> for EncapsulationKey {
type Error = ();
type Error = Infallible;

fn encapsulate(
&self,
Expand Down Expand Up @@ -133,7 +135,7 @@ pub struct DecapsulationKey {
}

impl Decapsulate<Ciphertext, SharedSecret> for DecapsulationKey {
type Error = ();
type Error = Infallible;

fn decapsulate(&self, ct: &Ciphertext) -> Result<SharedSecret, Self::Error> {
let (sk_m, sk_x, _pk_m, pk_x) = self.expand_key();
Expand Down Expand Up @@ -346,34 +348,30 @@ mod tests {

/// Test with test vectors from: https://github.com/dconnolly/draft-connolly-cfrg-xwing-kem/blob/main/spec/test-vectors.json
#[test]
fn rfc_test_vectors() -> Result<(), ()> {
fn rfc_test_vectors() {
let test_vectors =
serde_json::from_str::<Vec<TestVector>>(include_str!("test-vectors.json")).unwrap();

for test_vector in test_vectors {
run_test(test_vector)?;
run_test(test_vector);
}

Ok(())
}

fn run_test(test_vector: TestVector) -> Result<(), ()> {
fn run_test(test_vector: TestVector) {
let mut seed = SeedRng::new(test_vector.seed);
let (sk, pk) = generate_key_pair(&mut seed);

assert_eq!(sk.as_bytes().to_vec(), test_vector.sk);
assert_eq!(pk.as_bytes().to_vec(), test_vector.pk);

let mut eseed = SeedRng::new(test_vector.eseed);
let (ct, ss) = pk.encapsulate(&mut eseed)?;
let (ct, ss) = pk.encapsulate(&mut eseed).unwrap();

assert_eq!(ss, test_vector.ss);
assert_eq!(ct.as_bytes().to_vec(), test_vector.ct);

let ss = sk.decapsulate(&ct)?;
let ss = sk.decapsulate(&ct).unwrap();
assert_eq!(ss, test_vector.ss);

Ok(())
}

#[test]
Expand Down