1use crate::error::{Result, SdkError};
7use amaters_core::{CipherBlob, Key};
8use std::path::Path;
9
10#[derive(Clone)]
15pub struct FheKeys {
16 #[cfg(feature = "fhe")]
17 _keys: tfhe::ClientKey,
18 #[cfg(not(feature = "fhe"))]
19 _placeholder: (),
20}
21
22impl FheKeys {
23 pub fn generate() -> Result<Self> {
27 #[cfg(feature = "fhe")]
28 {
29 Err(SdkError::Fhe(
31 "FHE key generation not yet implemented".to_string(),
32 ))
33 }
34 #[cfg(not(feature = "fhe"))]
35 {
36 Ok(Self { _placeholder: () })
37 }
38 }
39
40 pub fn load_from_file(_path: impl AsRef<Path>) -> Result<Self> {
42 #[cfg(feature = "fhe")]
43 {
44 Err(SdkError::Fhe(
46 "FHE key loading not yet implemented".to_string(),
47 ))
48 }
49 #[cfg(not(feature = "fhe"))]
50 {
51 Ok(Self { _placeholder: () })
52 }
53 }
54
55 pub fn save_to_file(&self, _path: impl AsRef<Path>) -> Result<()> {
57 #[cfg(feature = "fhe")]
58 {
59 Err(SdkError::Fhe(
61 "FHE key saving not yet implemented".to_string(),
62 ))
63 }
64 #[cfg(not(feature = "fhe"))]
65 {
66 Ok(())
67 }
68 }
69
70 #[cfg(feature = "serialization")]
72 pub fn to_bytes(&self) -> Result<Vec<u8>> {
73 #[cfg(feature = "fhe")]
74 {
75 Err(SdkError::Fhe(
77 "FHE key serialization not yet implemented".to_string(),
78 ))
79 }
80 #[cfg(not(feature = "fhe"))]
81 {
82 Ok(Vec::new())
83 }
84 }
85
86 #[cfg(feature = "serialization")]
88 pub fn from_bytes(_bytes: &[u8]) -> Result<Self> {
89 #[cfg(feature = "fhe")]
90 {
91 Err(SdkError::Fhe(
93 "FHE key deserialization not yet implemented".to_string(),
94 ))
95 }
96 #[cfg(not(feature = "fhe"))]
97 {
98 Ok(Self { _placeholder: () })
99 }
100 }
101}
102
103pub struct FheEncryptor {
105 keys: FheKeys,
106}
107
108impl FheEncryptor {
109 pub fn new() -> Result<Self> {
111 Ok(Self {
112 keys: FheKeys::generate()?,
113 })
114 }
115
116 pub fn with_keys(keys: FheKeys) -> Self {
118 Self { keys }
119 }
120
121 pub fn keys(&self) -> &FheKeys {
123 &self.keys
124 }
125
126 pub fn encrypt(&self, _plaintext: &[u8]) -> Result<CipherBlob> {
131 #[cfg(feature = "fhe")]
132 {
133 Err(SdkError::Fhe(
135 "FHE encryption not yet implemented".to_string(),
136 ))
137 }
138 #[cfg(not(feature = "fhe"))]
139 {
140 Ok(CipherBlob::new(_plaintext.to_vec()))
143 }
144 }
145
146 pub fn decrypt(&self, ciphertext: &CipherBlob) -> Result<Vec<u8>> {
151 #[cfg(feature = "fhe")]
152 {
153 Err(SdkError::Fhe(
155 "FHE decryption not yet implemented".to_string(),
156 ))
157 }
158 #[cfg(not(feature = "fhe"))]
159 {
160 Ok(ciphertext.to_vec())
163 }
164 }
165
166 pub fn encrypt_key(&self, _key: &Key) -> Result<CipherBlob> {
168 #[cfg(feature = "fhe")]
169 {
170 Err(SdkError::Fhe(
171 "FHE key encryption not yet implemented".to_string(),
172 ))
173 }
174 #[cfg(not(feature = "fhe"))]
175 {
176 Ok(CipherBlob::new(_key.to_vec()))
178 }
179 }
180
181 pub fn encrypt_batch(&self, plaintexts: &[&[u8]]) -> Result<Vec<CipherBlob>> {
183 plaintexts.iter().map(|p| self.encrypt(p)).collect()
184 }
185}
186
187impl Default for FheEncryptor {
188 fn default() -> Self {
189 Self::new().expect("failed to create default encryptor")
190 }
191}
192
193#[cfg(test)]
194mod tests {
195 use super::*;
196
197 #[test]
198 fn test_fhe_keys_stub() {
199 #[cfg(not(feature = "fhe"))]
201 {
202 let keys = FheKeys::generate().expect("generate keys");
203 let _saved = keys.save_to_file("/tmp/test_keys");
204 }
205 }
206
207 #[test]
208 fn test_encryptor_stub() {
209 #[cfg(not(feature = "fhe"))]
210 {
211 let encryptor = FheEncryptor::new().expect("create encryptor");
212 let plaintext = b"hello world";
213 let ciphertext = encryptor.encrypt(plaintext).expect("encrypt");
214 let decrypted = encryptor.decrypt(&ciphertext).expect("decrypt");
215
216 assert_eq!(decrypted, plaintext);
218 }
219 }
220
221 #[test]
222 fn test_batch_encrypt() {
223 #[cfg(not(feature = "fhe"))]
224 {
225 let encryptor = FheEncryptor::new().expect("create encryptor");
226 let data: Vec<&[u8]> = vec![b"one", b"two", b"three"];
227
228 let encrypted = encryptor.encrypt_batch(&data).expect("batch encrypt");
229 assert_eq!(encrypted.len(), 3);
230 }
231 }
232}