Skip to content

Commit

Permalink
fix: replaced empty tuple error types with Infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSherbinin committed Aug 29, 2024
1 parent 23d8c1d commit ce72e1e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
16 changes: 12 additions & 4 deletions dhkem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ rand_core = "0.6.4"

# optional dependencies
elliptic-curve = { version = "0.13.8", optional = true, default-features = false }
k256 = { version = "0.13.3", optional = true, default-features = false, features = ["arithmetic"] }
p256 = { version = "0.13.2", optional = true, default-features = false, features = ["arithmetic"] }
p384 = { version = "0.13.0", optional = true, default-features = false, features = ["arithmetic"] }
p521 = { version = "0.13.3", optional = true, default-features = false, features = ["arithmetic"] }
k256 = { version = "0.13.3", optional = true, default-features = false, features = [
"arithmetic",
] }
p256 = { version = "0.13.2", optional = true, default-features = false, features = [
"arithmetic",
] }
p384 = { version = "0.13.0", optional = true, default-features = false, features = [
"arithmetic",
] }
p521 = { version = "0.13.3", optional = true, default-features = false, features = [
"arithmetic",
] }
x25519 = { version = "2.0.1", package = "x25519-dalek", optional = true, default-features = false }
zeroize = { version = "1.8.1", optional = true, default-features = false }

Expand Down
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

0 comments on commit ce72e1e

Please sign in to comment.