From f2a90a51ce3045e676ba105caf96d5037e03d4b4 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 26 Nov 2023 18:45:59 +0100 Subject: initial commit --- src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/lib.rs (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ce38f07 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,43 @@ +pub mod error; +pub mod hibe; +pub mod kem; + +use error::Result; + +/// A trait to provide byte-level access to objects. +pub trait ByteAccess { + /// Provides access to the bytes. + /// + /// Unlike [`AsRef`], there are no statements made about the performance of this operation. + /// This operation will allocate a fresh vector, and the byte representation may or may not + /// have to be computed first. + fn bytes(&self) -> Vec; + + /// Provide a short fingerprint of the bytes. + /// + /// This can be used to "summarize" long keys when displaying them, to still provide + /// distinguishing features but to not print out the whole key. + /// + /// By default, this method uses the first 16 bytes of the [`ByteAccess::bytes`] + /// representation, and formats them as a hex string. + fn fingerprint(&self) -> String { + hex::encode(&self.bytes()[..16]) + } +} + +/// A trait to mark objects that can map from an application-specific identity to a HIBE-specific +/// identity. +/// +/// A mapper can be implemented multiple times for a single struct, thereby providing multiple +/// (equivalent) ways to map. +pub trait Mapper { + fn map_identity(&self, input: F) -> Result>; +} + +/// [`Mapper`] is automatically implemented for functions and closures that match the signature of +/// [`Mapper::map_identity`]. +impl Result>> Mapper for F { + fn map_identity(&self, input: X) -> Result> { + self(input) + } +} -- cgit v1.2.3