fhe_traits/
lib.rs

1#![crate_name = "fhe_traits"]
2#![crate_type = "lib"]
3#![warn(missing_docs, unused_imports)]
4
5//! Traits for Fully Homomorphic Encryption
6
7use std::sync::Arc;
8
9use rand::{CryptoRng, RngCore};
10
11/// The homomorphic encryption parameters.
12pub trait FheParameters {}
13
14/// Indicates that an object is parametrized.
15pub trait FheParametrized {
16    /// The type of the FHE parameters.
17    type Parameters: FheParameters;
18}
19
20/// Indicates that Self parameters can be switched.
21pub trait FheParametersSwitchable<S: FheParametrized>
22where
23    Self: FheParametrized,
24{
25    /// The type of error returned.
26    type Error;
27
28    /// Attempt to switch the underlying parameters using the associated
29    /// switcher.
30    fn switch_parameters(&mut self, switcher: &S) -> Result<(), Self::Error>;
31}
32
33/// Encoding used when encoding a [`FhePlaintext`].
34pub trait FhePlaintextEncoding {}
35
36/// A plaintext which will encode one (or more) value(s).
37pub trait FhePlaintext
38where
39    Self: Sized + FheParametrized,
40{
41    /// The type of the encoding.
42    type Encoding: FhePlaintextEncoding;
43}
44
45/// Encode a value using a specified encoding.
46pub trait FheEncoder<V>
47where
48    Self: FhePlaintext,
49{
50    /// The type of error returned.
51    type Error;
52
53    /// Attempt to encode a value using a specified encoding.
54    fn try_encode(
55        value: V,
56        encoding: Self::Encoding,
57        par: &Arc<Self::Parameters>,
58    ) -> Result<Self, Self::Error>;
59}
60
61/// Encode a value using a specified encoding.
62pub trait FheEncoderVariableTime<V>
63where
64    Self: FhePlaintext,
65{
66    /// The type of error returned.
67    type Error;
68
69    /// Attempt to encode a value using a specified encoding.
70    /// # Safety
71    /// This encoding runs in variable time and may leak information about the
72    /// value.
73    unsafe fn try_encode_vt(
74        value: V,
75        encoding: Self::Encoding,
76        par: &Arc<Self::Parameters>,
77    ) -> Result<Self, Self::Error>;
78}
79
80/// Decode the value in the plaintext with the specified (optional) encoding.
81pub trait FheDecoder<P: FhePlaintext>
82where
83    Self: Sized,
84{
85    /// The type of error returned.
86    type Error;
87
88    /// Attempt to decode a [`FhePlaintext`] into a value, using an (optional)
89    /// encoding.
90    fn try_decode<O>(pt: &P, encoding: O) -> Result<Self, Self::Error>
91    where
92        O: Into<Option<P::Encoding>>;
93}
94
95/// A ciphertext which will encrypt a plaintext.
96pub trait FheCiphertext
97where
98    Self: Sized + Serialize + FheParametrized + DeserializeParametrized,
99{
100}
101
102/// Encrypt a plaintext into a ciphertext.
103pub trait FheEncrypter<
104    P: FhePlaintext<Parameters = Self::Parameters>,
105    C: FheCiphertext<Parameters = Self::Parameters>,
106>: FheParametrized
107{
108    /// The type of error returned.
109    type Error;
110
111    /// Try to encrypt an [`FhePlaintext`] into an [`FheCiphertext`].
112    fn try_encrypt<R: RngCore + CryptoRng>(&self, pt: &P, rng: &mut R) -> Result<C, Self::Error>;
113}
114
115/// Decrypt a ciphertext into a plaintext
116pub trait FheDecrypter<
117    P: FhePlaintext<Parameters = Self::Parameters>,
118    C: FheCiphertext<Parameters = Self::Parameters>,
119>: FheParametrized
120{
121    /// The type of error returned.
122    type Error;
123
124    /// Try to decrypt an [`FheCiphertext`] into an [`FhePlaintext`].
125    fn try_decrypt(&self, ct: &C) -> Result<P, Self::Error>;
126}
127
128/// Serialization.
129pub trait Serialize {
130    /// Serialize `Self` into a vector of bytes.
131    fn to_bytes(&self) -> Vec<u8>;
132}
133
134/// Deserialization of a parametrized value.
135pub trait DeserializeParametrized
136where
137    Self: Sized,
138    Self: FheParametrized,
139{
140    /// The type of error returned.
141    type Error;
142
143    /// Attempt to deserialize from a vector of bytes
144    fn from_bytes(bytes: &[u8], par: &Arc<Self::Parameters>) -> Result<Self, Self::Error>;
145}
146
147/// Deserialization setting an explicit context.
148pub trait DeserializeWithContext
149where
150    Self: Sized,
151{
152    /// The type of error returned.
153    type Error;
154
155    /// The type of context.
156    type Context;
157
158    /// Attempt to deserialize from a vector of bytes
159    fn from_bytes(bytes: &[u8], ctx: &Arc<Self::Context>) -> Result<Self, Self::Error>;
160}
161
162/// Deserialization without context.
163pub trait Deserialize
164where
165    Self: Sized,
166{
167    /// The type of error returned.
168    type Error;
169
170    /// Attempt to deserialize from a vector of bytes
171    fn try_deserialize(bytes: &[u8]) -> Result<Self, Self::Error>;
172}