Skip to content

Commit 502ff6b

Browse files
fix: replaced empty tuple error types with Infallible (#59)
1 parent 1f2941d commit 502ff6b

4 files changed

Lines changed: 23 additions & 26 deletions

File tree

dhkem/src/ecdh_kem.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Generic Elliptic Curve Diffie-Hellman KEM adapter.
22
33
use crate::{DhDecapsulator, DhEncapsulator, DhKem};
4-
use core::marker::PhantomData;
4+
use core::{convert::Infallible, marker::PhantomData};
55
use elliptic_curve::{
66
ecdh::{EphemeralSecret, SharedSecret},
77
CurveArithmetic, PublicKey,
@@ -19,7 +19,7 @@ impl<C> Encapsulate<PublicKey<C>, SharedSecret<C>> for DhEncapsulator<PublicKey<
1919
where
2020
C: CurveArithmetic,
2121
{
22-
type Error = ();
22+
type Error = Infallible;
2323

2424
fn encapsulate(
2525
&self,
@@ -38,7 +38,7 @@ impl<C> Decapsulate<PublicKey<C>, SharedSecret<C>> for DhDecapsulator<EphemeralS
3838
where
3939
C: CurveArithmetic,
4040
{
41-
type Error = ();
41+
type Error = Infallible;
4242

4343
fn decapsulate(&self, encapsulated_key: &PublicKey<C>) -> Result<SharedSecret<C>, Self::Error> {
4444
let ss = self.0.diffie_hellman(encapsulated_key);

dhkem/src/x25519_kem.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{DhDecapsulator, DhEncapsulator, DhKem};
2+
use core::convert::Infallible;
23
use kem::{Decapsulate, Encapsulate};
34
use rand_core::CryptoRngCore;
45
use x25519::{PublicKey, ReusableSecret, SharedSecret};
@@ -9,7 +10,7 @@ use x25519::{PublicKey, ReusableSecret, SharedSecret};
910
pub struct X25519Kem;
1011

1112
impl Encapsulate<PublicKey, SharedSecret> for DhEncapsulator<PublicKey> {
12-
type Error = ();
13+
type Error = Infallible;
1314

1415
fn encapsulate(
1516
&self,
@@ -25,7 +26,7 @@ impl Encapsulate<PublicKey, SharedSecret> for DhEncapsulator<PublicKey> {
2526
}
2627

2728
impl Decapsulate<PublicKey, SharedSecret> for DhDecapsulator<ReusableSecret> {
28-
type Error = ();
29+
type Error = Infallible;
2930

3031
fn decapsulate(&self, encapsulated_key: &PublicKey) -> Result<SharedSecret, Self::Error> {
3132
let ss = self.0.diffie_hellman(encapsulated_key);

ml-kem/src/kem.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::convert::Infallible;
12
use core::marker::PhantomData;
23
use hybrid_array::typenum::U32;
34
use rand_core::CryptoRngCore;
@@ -85,12 +86,12 @@ impl<P> ::kem::Decapsulate<EncodedCiphertext<P>, SharedKey> for DecapsulationKey
8586
where
8687
P: KemParams,
8788
{
88-
// Decapsulation is infallible
89-
// XXX(RLB): Maybe we should reflect decryption failure as an error?
90-
// TODO(RLB) Make Infallible
91-
type Error = ();
89+
type Error = Infallible;
9290

93-
fn decapsulate(&self, encapsulated_key: &EncodedCiphertext<P>) -> Result<SharedKey, ()> {
91+
fn decapsulate(
92+
&self,
93+
encapsulated_key: &EncodedCiphertext<P>,
94+
) -> Result<SharedKey, Self::Error> {
9495
let mp = self.dk_pke.decrypt(encapsulated_key);
9596
let (Kp, rp) = G(&[&mp, &self.ek.h]);
9697
let Kbar = J(&[self.z.as_slice(), encapsulated_key.as_ref()]);
@@ -187,9 +188,7 @@ impl<P> ::kem::Encapsulate<EncodedCiphertext<P>, SharedKey> for EncapsulationKey
187188
where
188189
P: KemParams,
189190
{
190-
// TODO(RLB) Make Infallible
191-
// TODO(RLB) Swap the order of the
192-
type Error = ();
191+
type Error = Infallible;
193192

194193
fn encapsulate(
195194
&self,
@@ -205,8 +204,7 @@ impl<P> crate::EncapsulateDeterministic<EncodedCiphertext<P>, SharedKey> for Enc
205204
where
206205
P: KemParams,
207206
{
208-
// TODO(RLB) Make Infallible
209-
type Error = ();
207+
type Error = Infallible;
210208

211209
fn encapsulate_deterministic(
212210
&self,

x-wing/src/lib.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
//! assert_eq!(ss_sender, ss_receiver);
2828
//! ```
2929
30+
use core::convert::Infallible;
31+
3032
use kem::{Decapsulate, Encapsulate};
3133
use ml_kem::array::ArrayN;
3234
use ml_kem::{kem, EncodedSizeUser, KemCore, MlKem768, MlKem768Params, B32};
@@ -73,7 +75,7 @@ pub struct EncapsulationKey {
7375
}
7476

7577
impl Encapsulate<Ciphertext, SharedSecret> for EncapsulationKey {
76-
type Error = ();
78+
type Error = Infallible;
7779

7880
fn encapsulate(
7981
&self,
@@ -133,7 +135,7 @@ pub struct DecapsulationKey {
133135
}
134136

135137
impl Decapsulate<Ciphertext, SharedSecret> for DecapsulationKey {
136-
type Error = ();
138+
type Error = Infallible;
137139

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

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

353355
for test_vector in test_vectors {
354-
run_test(test_vector)?;
356+
run_test(test_vector);
355357
}
356-
357-
Ok(())
358358
}
359359

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

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

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

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

373-
let ss = sk.decapsulate(&ct)?;
373+
let ss = sk.decapsulate(&ct).unwrap();
374374
assert_eq!(ss, test_vector.ss);
375-
376-
Ok(())
377375
}
378376

379377
#[test]

0 commit comments

Comments
 (0)