// Copyright 2023 Cryspen Sarl // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0
//! This module contains the trait and related errors for a KEM that takes array references as //! arguments and writes to outputs to mutable array references.
/// A Key Encapsulation Mechanismd (KEM). This trait is the most low-level and mostly used in the /// implementation of other, more usabe APIs on top. pubtrait Kem< const EK_LEN: usize, const DK_LEN: usize, const CT_LEN: usize, const SS_LEN: usize, const RAND_KEYGEN_LEN: usize, const RAND_ENCAPS_LEN: usize,
>
{ /// Generate a pair of encapsulation and decapsulation keys. /// It is the responsibility of the caller to ensure that the `rand` argument is actually /// random. fn keygen(
ek: &mut [u8; EK_LEN],
dk: &mut [u8; DK_LEN],
rand: &[u8; RAND_KEYGEN_LEN],
) -> Result<(), KeyGenError>;
/// Encapsulate a shared secret towards a given encapsulation key. /// It is the responsibility of the caller to ensure that the `rand` argument is actually /// random. fn encaps(
ct: &mut [u8; CT_LEN],
ss: &mut [u8; SS_LEN],
ek: &[u8; EK_LEN],
rand: &[u8; RAND_ENCAPS_LEN],
) -> Result<(), EncapsError>;
/// Error generating key with provided randomness #[derive(Debug)] pubenum KeyGenError { /// Error generating key with provided randomness
InvalidRandomness,
/// An unknown error occurred
Unknown,
}
/// Error indicating that encapsulating failed #[derive(Debug)] pubenum EncapsError { /// Encapsulation key is invalid
InvalidEncapsKey,
/// Error encapsulating key with provided randomness
InvalidRandomness,
/// An unknown error occurred
Unknown,
}
/// Error indicating that decapsulating failed #[derive(Debug)] pubenum DecapsError { /// Ciphertext key is invalid
InvalidCiphertext,
/// Decapsulation key is invalid
InvalidDecapsKey,
/// An unknown error occurred
Unknown,
}
impl core::fmt::Display for KeyGenError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let text = matchself {
KeyGenError::InvalidRandomness => "error generating key with provided randomness",
KeyGenError::Unknown => "an unknown error occurred",
};
f.write_str(text)
}
}
impl core::fmt::Display for EncapsError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let text = matchself {
EncapsError::InvalidEncapsKey => "encapsulation key is invalid",
EncapsError::InvalidRandomness => "error generating key with provided randomness",
EncapsError::Unknown => "an unknown error occurred",
};
f.write_str(text)
}
}
impl core::fmt::Display for DecapsError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let text = matchself {
DecapsError::InvalidDecapsKey => "encapsulation key is invalid",
DecapsError::InvalidCiphertext => "ciphertext is invalid",
DecapsError::Unknown => "an unknown error occurred",
};
f.write_str(text)
}
}
#[cfg(feature = "error-in-core")] /// Here we implement the Error trait. This has only been added to core relatively recently, so we /// are hiding that behind a feature. mod error_in_core { impl core::error::Error forsuper::EncapsError {} impl core::error::Error forsuper::DecapsError {} impl core::error::Error forsuper::KeyGenError {}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-09-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.