did_utils/crypto/
traits.rs

1// Inspired from https://github.com/decentralized-identity/did-key.rs
2// We are desiging the application to support many curve algorithms.
3// This module will design an interface common to all curves, so that we can change the curve
4// without altering consuming modules.
5
6use super::errors::Error;
7
8/// The length of a 32-byte key material.
9pub const BYTES_LENGTH_32: usize = 32;
10
11#[allow(unused)]
12pub trait ToMultikey {
13    /// Converts keypair into its multikey string
14    fn to_multikey(&self) -> String;
15}
16
17/// A trait for types that hold key material bytes.
18pub trait KeyMaterial {
19    /// Returns the public key bytes as a slice.
20    ///
21    /// Returns a `Result` containing the public key bytes, or an `Error` if the operation fails.
22
23    fn public_key_bytes(&self) -> Result<[u8; BYTES_LENGTH_32], Error>;
24    /// Returns the secret key bytes as a slice.
25    ///
26    /// Returns a `Result` containing the secret key bytes, or an `Error` if the operation fails.
27    fn private_key_bytes(&self) -> Result<[u8; BYTES_LENGTH_32], Error>;
28}
29
30/// A trait for types that support deterministic key generation.
31pub trait Generate: KeyMaterial {
32    /// Generates a new random key.
33    ///
34    /// Returns a `Result` containing the new key, or an `Error` if the operation fails.
35    fn new() -> Result<Self, Error>
36    where
37        Self: Sized;
38
39    /// Generates a new key deterministically using the given seed.
40    ///
41    /// Returns a `Result` containing the new key, or an `Error` if the operation fails.
42    fn new_with_seed(seed: &[u8]) -> Result<Self, Error>
43    where
44        Self: Sized;
45
46    /// Generates a new instance from an existing public key.
47    ///
48    /// Returns a `Result` containing the new instance, or an `Error` if the operation fails.
49    fn from_public_key(public_key: &[u8; BYTES_LENGTH_32]) -> Result<Self, Error>
50    where
51        Self: Sized;
52
53    /// Generates a new instance from an existing secret key.
54    ///
55    /// Returns a `Result` containing the new instance, or an `Error` if the operation fails.
56    fn from_secret_key(private_key: &[u8; BYTES_LENGTH_32]) -> Result<Self, Error>
57    where
58        Self: Sized;
59}
60
61/// A trait for types that support creating and verifying digital signatures.
62pub trait CoreSign {
63    /// Performs a sign operation.
64    ///
65    /// Returns a `Result` containing the signature, or an `Error` if the operation fails.
66    fn sign(&self, payload: &[u8]) -> Result<Vec<u8>, Error>;
67
68    /// Performs a verify operation.
69    ///
70    /// Returns a `Result` containing `()`, or an `Error` if the operation fails.
71    fn verify(&self, payload: &[u8], signature: &[u8]) -> Result<(), Error>;
72}
73
74/// A trait for types that support ECDH key exchange operations.
75pub trait ECDH {
76    /// Performs a key exchange operation.
77    ///
78    /// Returns an `Option` containing the shared secret, or `None` if the operation fails.
79    fn key_exchange(&self, their_public: &Self) -> Option<Vec<u8>>;
80}
81
82/// A trait for converting a key to its public counterpart.
83pub trait ToPublic {
84    /// Converts the key to its public counterpart.
85    ///
86    /// This method returns a new instance of the key with any private information removed.
87    /// The returned key contains only the public key components.
88    fn to_public(&self) -> Self;
89}