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 Oct 15, 2024
1 parent 1fe2e42 commit 55afe8f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
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 55afe8f

Please sign in to comment.