libcrux_traits/ecdh/
arrayref.rs1use libcrux_secrets::U8;
6
7pub trait EcdhArrayref<const RAND_LEN: usize, const SECRET_LEN: usize, const PUBLIC_LEN: usize> {
10 fn generate_secret(
14 secret: &mut [U8; SECRET_LEN],
15 rand: &[U8; RAND_LEN],
16 ) -> Result<(), GenerateSecretError>;
17
18 fn secret_to_public(
20 public: &mut [u8; PUBLIC_LEN],
21 secret: &[U8; SECRET_LEN],
22 ) -> Result<(), SecretToPublicError>;
23
24 fn generate_pair(
27 public: &mut [u8; PUBLIC_LEN],
28 secret: &mut [U8; SECRET_LEN],
29 rand: &[U8; RAND_LEN],
30 ) -> Result<(), GenerateSecretError> {
31 Self::generate_secret(secret, rand)?;
32 Self::secret_to_public(public, secret).map_err(|_| GenerateSecretError::Unknown)
33 }
34
35 fn derive_ecdh(
42 derived: &mut [U8; PUBLIC_LEN],
43 public: &[u8; PUBLIC_LEN],
44 secret: &[U8; SECRET_LEN],
45 ) -> Result<(), DeriveError>;
46
47 fn validate_secret(secret: &[U8; SECRET_LEN]) -> Result<(), ValidateSecretError>;
49}
50
51#[derive(Debug)]
52pub enum GenerateSecretError {
54 InvalidRandomness,
56
57 Unknown,
59}
60
61#[derive(Debug)]
62pub enum SecretToPublicError {
64 InvalidSecret,
66 Unknown,
68}
69
70#[derive(Debug)]
71pub enum DeriveError {
73 InvalidPublic,
75 InvalidSecret,
77 Unknown,
79}
80
81#[derive(Debug)]
82pub enum ValidateSecretError {
84 InvalidSecret,
86 Unknown,
88}
89
90impl core::fmt::Display for GenerateSecretError {
91 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
92 let text = match self {
93 GenerateSecretError::InvalidRandomness => {
94 "error generating secret value with provided randomness"
95 }
96 GenerateSecretError::Unknown => "an unknown error occured",
97 };
98
99 f.write_str(text)
100 }
101}
102
103impl core::fmt::Display for SecretToPublicError {
104 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
105 let text = match self {
106 SecretToPublicError::InvalidSecret => "secret value is invalid",
107 SecretToPublicError::Unknown => "an unknown error occured",
108 };
109
110 f.write_str(text)
111 }
112}
113
114impl core::fmt::Display for DeriveError {
115 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
116 let text = match self {
117 DeriveError::InvalidPublic => "public value is invalid",
118 DeriveError::InvalidSecret => "secret value is invalid",
119 DeriveError::Unknown => "an unknown error occured",
120 };
121
122 f.write_str(text)
123 }
124}
125
126impl core::fmt::Display for ValidateSecretError {
127 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
128 let text = match self {
129 ValidateSecretError::InvalidSecret => "secret value is invalid",
130 ValidateSecretError::Unknown => "an unknown error occured",
131 };
132
133 f.write_str(text)
134 }
135}
136
137#[cfg(feature = "error-in-core")]
138mod error_in_core {
141 impl core::error::Error for super::GenerateSecretError {}
142 impl core::error::Error for super::SecretToPublicError {}
143 impl core::error::Error for super::DeriveError {}
144 impl core::error::Error for super::ValidateSecretError {}
145}