1use std::error::Error;
2use std::fmt;
3
4pub enum CryptoAPIError {
5 PolynomialSizeError {
6 size_1: usize,
7 size_2: usize,
8 description: String,
9 },
10 NoNoiseInCiphertext {
11 var: f64,
12 description: String,
13 },
14 DimensionError {
15 dim_1: usize,
16 dim_2: usize,
17 description: String,
18 },
19 InvalidEncoderError {
20 nb_bit_precision: usize,
21 delta: f64,
22 description: String,
23 },
24 MessageOutsideIntervalError {
25 message: f64,
26 o: f64,
27 delta: f64,
28 description: String,
29 },
30 MessageTooBigError {
31 message: f64,
32 delta: f64,
33 description: String,
34 },
35 DeltaError {
36 delta_1: f64,
37 delta_2: f64,
38 description: String,
39 },
40 PaddingError {
41 p_1: usize,
42 p_2: usize,
43 description: String,
44 },
45 NotEnoughPaddingError {
46 p: usize,
47 min_p: usize,
48 description: String,
49 },
50 IndexError {
51 nb_ct: usize,
52 n: usize,
53 description: String,
54 },
55 ConstantMaximumError {
56 cst: f64,
57 max: f64,
58 description: String,
59 },
60 ZeroInIntervalError {
61 o: f64,
62 delta: f64,
63 description: String,
64 },
65 NbCTError {
66 nb_ct1: usize,
67 nb_ct2: usize,
68 description: String,
69 },
70 PrecisionError {
71 description: String,
72 },
73 MinMaxError {
74 min: f64,
75 max: f64,
76 description: String,
77 },
78 RadiusError {
79 radius: f64,
80 description: String,
81 },
82 MonomialError {
83 polynomial_size: usize,
84 monomial: usize,
85 description: String,
86 },
87 NotPowerOfTwoError {
88 polynomial_size: usize,
89 description: String,
90 },
91 ZeroCiphertextsInStructureError {
92 nb_ciphertexts: usize,
93 description: String,
94 },
95 WrongSizeError {
96 size: usize,
97 description: String,
98 },
99 NotEnoughValidEncoderError {
100 nb_valid_encoders: usize,
101 nb_actions: usize,
102 description: String,
103 },
104 LweToRlweError {
105 dimension: usize,
106 polynomial_size: usize,
107 description: String,
108 },
109}
110impl fmt::Display for CryptoAPIError {
111 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112 match self {
113 CryptoAPIError::PolynomialSizeError { description, .. } => {
114 writeln!(f, "\n{}", description)
115 }
116 CryptoAPIError::NoNoiseInCiphertext { description, .. } => {
117 writeln!(f, "\n{}", description)
118 }
119 CryptoAPIError::DimensionError { description, .. } => writeln!(f, "\n{}", description),
120 CryptoAPIError::NotEnoughPaddingError { description, .. } => {
121 writeln!(f, "\n{}", description)
122 }
123 CryptoAPIError::InvalidEncoderError { description, .. } => {
124 writeln!(f, "\n{}", description)
125 }
126 CryptoAPIError::MessageOutsideIntervalError { description, .. } => {
127 writeln!(f, "\n{}", description)
128 }
129 CryptoAPIError::MessageTooBigError { description, .. } => {
130 writeln!(f, "\n{}", description)
131 }
132 CryptoAPIError::DeltaError { description, .. } => writeln!(f, "\n{}", description),
133 CryptoAPIError::PaddingError { description, .. } => writeln!(f, "\n{}", description),
134 CryptoAPIError::IndexError { description, .. } => writeln!(f, "\n{}", description),
135 CryptoAPIError::ConstantMaximumError { description, .. } => {
136 writeln!(f, "\n{}", description)
137 }
138 CryptoAPIError::ZeroInIntervalError { description, .. } => {
139 writeln!(f, "\n{}", description)
140 }
141 CryptoAPIError::NbCTError { description, .. } => writeln!(f, "\n{}", description),
142 CryptoAPIError::PrecisionError { description } => writeln!(f, "\n{}", description),
143 CryptoAPIError::MinMaxError { description, .. } => writeln!(f, "\n{}", description),
144 CryptoAPIError::RadiusError { description, .. } => writeln!(f, "\n{}", description),
145 CryptoAPIError::MonomialError { description, .. } => writeln!(f, "\n{}", description),
146 CryptoAPIError::NotPowerOfTwoError { description, .. } => {
147 writeln!(f, "\n{}", description)
148 }
149 CryptoAPIError::ZeroCiphertextsInStructureError { description, .. } => {
150 writeln!(f, "\n{}", description)
151 }
152 CryptoAPIError::WrongSizeError { description, .. } => writeln!(f, "\n{}", description),
153 CryptoAPIError::NotEnoughValidEncoderError { description, .. } => {
154 writeln!(f, "\n{}", description)
155 }
156 CryptoAPIError::LweToRlweError { description, .. } => writeln!(f, "\n{}", description),
157 }
158 }
159}
160
161impl fmt::Debug for CryptoAPIError {
162 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
163 match self {
164 CryptoAPIError::PolynomialSizeError { description, .. } => {
165 writeln!(f, "\n{}", description)
166 }
167 CryptoAPIError::NoNoiseInCiphertext { description, .. } => {
168 writeln!(f, "\n{}", description)
169 }
170 CryptoAPIError::DimensionError { description, .. } => writeln!(f, "\n{}", description),
171 CryptoAPIError::NotEnoughPaddingError { description, .. } => {
172 writeln!(f, "\n{}", description)
173 }
174 CryptoAPIError::InvalidEncoderError { description, .. } => {
175 writeln!(f, "\n{}", description)
176 }
177 CryptoAPIError::MessageOutsideIntervalError { description, .. } => {
178 writeln!(f, "\n{}", description)
179 }
180 CryptoAPIError::MessageTooBigError { description, .. } => {
181 writeln!(f, "\n{}", description)
182 }
183 CryptoAPIError::DeltaError { description, .. } => writeln!(f, "\n{}", description),
184 CryptoAPIError::PaddingError { description, .. } => writeln!(f, "\n{}", description),
185 CryptoAPIError::IndexError { description, .. } => writeln!(f, "\n{}", description),
186 CryptoAPIError::ConstantMaximumError { description, .. } => {
187 writeln!(f, "\n{}", description)
188 }
189 CryptoAPIError::ZeroInIntervalError { description, .. } => {
190 writeln!(f, "\n{}", description)
191 }
192 CryptoAPIError::NbCTError { description, .. } => writeln!(f, "\n{}", description),
193 CryptoAPIError::PrecisionError { description } => writeln!(f, "\n{}", description),
194 CryptoAPIError::MinMaxError { description, .. } => writeln!(f, "\n{}", description),
195 CryptoAPIError::RadiusError { description, .. } => writeln!(f, "\n{}", description),
196 CryptoAPIError::MonomialError { description, .. } => writeln!(f, "\n{}", description),
197 CryptoAPIError::NotPowerOfTwoError { description, .. } => {
198 writeln!(f, "\n{}", description)
199 }
200 CryptoAPIError::ZeroCiphertextsInStructureError { description, .. } => {
201 writeln!(f, "\n{}", description)
202 }
203 CryptoAPIError::WrongSizeError { description, .. } => writeln!(f, "\n{}", description),
204 CryptoAPIError::NotEnoughValidEncoderError { description, .. } => {
205 writeln!(f, "\n{}", description)
206 }
207 CryptoAPIError::LweToRlweError { description, .. } => writeln!(f, "\n{}", description),
208 }
209 }
210}
211
212impl Error for CryptoAPIError {
213 fn description(&self) -> &str {
214 match self {
215 CryptoAPIError::PolynomialSizeError { description, .. } => description,
216 CryptoAPIError::NoNoiseInCiphertext { description, .. } => description,
217 CryptoAPIError::DimensionError { description, .. } => description,
218 CryptoAPIError::NotEnoughPaddingError { description, .. } => description,
219 CryptoAPIError::InvalidEncoderError { description, .. } => description,
220 CryptoAPIError::MessageOutsideIntervalError { description, .. } => description,
221 CryptoAPIError::MessageTooBigError { description, .. } => description,
222 CryptoAPIError::DeltaError { description, .. } => description,
223 CryptoAPIError::PaddingError { description, .. } => description,
224 CryptoAPIError::IndexError { description, .. } => description,
225 CryptoAPIError::ConstantMaximumError { description, .. } => description,
226 CryptoAPIError::ZeroInIntervalError { description, .. } => description,
227 CryptoAPIError::NbCTError { description, .. } => description,
228 CryptoAPIError::PrecisionError { description } => description,
229 CryptoAPIError::MinMaxError { description, .. } => description,
230 CryptoAPIError::RadiusError { description, .. } => description,
231 CryptoAPIError::MonomialError { description, .. } => description,
232 CryptoAPIError::NotPowerOfTwoError { description, .. } => description,
233 CryptoAPIError::ZeroCiphertextsInStructureError { description, .. } => description,
234 CryptoAPIError::WrongSizeError { description, .. } => description,
235 CryptoAPIError::NotEnoughValidEncoderError { description, .. } => description,
236 CryptoAPIError::LweToRlweError { description, .. } => description,
237 }
238 }
239}
240
241#[macro_export]
242macro_rules! PolynomialSizeError {
243 ($size_1: expr, $size_2: expr) => {
244 CryptoAPIError::PolynomialSizeError {
245 size_1: $size_1,
246 size_2: $size_2,
247 description: format!(
248 "{}: {} != {} \n{:#?}\n ",
249 "Different polynomial sizes: ".red().bold(),
250 $size_1,
251 $size_2,
252 Backtrace::new()
253 ),
254 };
255 };
256}
257
258#[macro_export]
259macro_rules! NoNoiseInCiphertext {
260 ($var: expr) => {
261 CryptoAPIError::NoNoiseInCiphertext {
262 var: $var,
263 description: format!(
264 "{} {} {} \n{:#?}\n ",
265 "The integer representation has not enough precision to represent error samples from the normal law of variance".red().bold(),
266 $var,
267 "so the ciphertext does not contain any noise!\n{:#?}\n",
268 Backtrace::new()
269 ),
270 };
271 };
272}
273
274#[macro_export]
275macro_rules! DimensionError {
276 ($dim_1: expr, $dim_2:expr) => {
277 CryptoAPIError::DimensionError {
278 dim_1: $dim_1,
279 dim_2: $dim_2,
280 description: format!(
281 "{}: {} != {}\n{:#?}\n",
282 "Different dimensions".red().bold(),
283 $dim_1,
284 $dim_2,
285 Backtrace::new()
286 ),
287 };
288 };
289}
290
291#[macro_export]
292macro_rules! InvalidEncoderError {
293 ($nb_bit_precision: expr, $delta: expr) => {
294 CryptoAPIError::InvalidEncoderError {
295 nb_bit_precision: $nb_bit_precision,
296 delta: $delta,
297 description: format!(
298 "{}: nb_bit_precision = {}, delta = {}\n{:#?}\n",
299 "Invalid Encoder".red().bold(),
300 $nb_bit_precision,
301 $delta,
302 Backtrace::new()
303 ),
304 };
305 };
306}
307
308#[macro_export]
309macro_rules! MessageOutsideIntervalError {
310 ($message: expr, $o: expr, $delta: expr) => {
311 CryptoAPIError::MessageOutsideIntervalError {
312 message: $message,
313 o: $o,
314 delta: $delta,
315 description: format!(
316 "The message {} is {} [{}, {}] defined by o = {} and delta = {}\n{:#?}\n",
317 "outside the interval".red().bold(),
318 $message,
319 $o,
320 $o + $delta,
321 $o,
322 $delta,
323 Backtrace::new()
324 ),
325 };
326 };
327}
328
329#[macro_export]
330macro_rules! MessageTooBigError {
331 ($message: expr, $delta: expr) => {
332 CryptoAPIError::MessageTooBigError {
333 message: $message,
334 delta: $delta,
335 description: format!(
336 "The absolute value of the message {} is {} = {}\n{:#?}\n",
337 "bigger than delta".red().bold(),
338 $message.abs(),
339 $delta,
340 Backtrace::new()
341 ),
342 };
343 };
344}
345
346#[macro_export]
347macro_rules! DeltaError {
348 ($delta_1: expr, $delta_2: expr) => {
349 CryptoAPIError::DeltaError {
350 delta_1: $delta_1,
351 delta_2: $delta_2,
352 description: format!(
353 "{} : {} != {}\n{:#?}\n",
354 "Deltas should be the same".red().bold(),
355 $delta_1,
356 $delta_2,
357 Backtrace::new()
358 ),
359 };
360 };
361}
362
363#[macro_export]
364macro_rules! PaddingError {
365 ($p_1: expr, $p_2: expr) => {
366 CryptoAPIError::PaddingError {
367 p_1: $p_1,
368 p_2: $p_2,
369 description: format!(
370 "{}: {} != {}\n{:#?}\n",
371 "Number of bits of padding should be the same".red().bold(),
372 $p_1,
373 $p_2,
374 Backtrace::new()
375 ),
376 };
377 };
378}
379
380#[macro_export]
381macro_rules! NotEnoughPaddingError {
382 ($p: expr, $min_p: expr) => {
383 CryptoAPIError::NotEnoughPaddingError {
384 p: $p,
385 min_p: $min_p,
386 description: format!(
387 "{} we need at least {} bits of padding, and we only have {}\n{:#?}\n",
388 "Not enough padding:".red().bold(),
389 $min_p,
390 $p,
391 Backtrace::new()
392 ),
393 };
394 };
395}
396
397#[macro_export]
398macro_rules! IndexError {
399 ($nb_ct: expr, $n: expr) => {
400 CryptoAPIError::IndexError {
401 nb_ct: $nb_ct,
402 n: $n,
403 description: format!(
404 "{}: number of ciphertexts = {} <= index = {}\n{:#?}\n",
405 "Can't access the ciphertext".red().bold(),
406 $nb_ct,
407 $n,
408 Backtrace::new()
409 ),
410 };
411 };
412}
413
414#[macro_export]
415macro_rules! ConstantMaximumError {
416 ($cst: expr, $max: expr) => {
417 CryptoAPIError::ConstantMaximumError {
418 cst: $cst,
419 max: $max,
420 description: format!(
421 "Absolute value of the constant (= {}) is {} (= {})\n{:#?}\n",
422 $cst,
423 "bigger than the maximum".red().bold(),
424 $max,
425 Backtrace::new()
426 ),
427 };
428 };
429}
430
431#[macro_export]
432macro_rules! ZeroInIntervalError {
433 ($o: expr, $delta: expr) => {
434 CryptoAPIError::ZeroInIntervalError {
435 o: $o,
436 delta: $delta,
437 description: format!(
438 "{} = [{},{}] with o = {}, delta = {}\n{:#?}\n",
439 "Zero should be in the input interval".red().bold(),
440 $o,
441 $o + $delta,
442 $o,
443 $delta,
444 Backtrace::new()
445 ),
446 };
447 };
448}
449
450#[macro_export]
451macro_rules! NbCTError {
452 ($nb_ct1: expr, $nb_ct2: expr) => {
453 CryptoAPIError::NbCTError {
454 nb_ct1: $nb_ct1,
455 nb_ct2: $nb_ct2,
456 description: format!(
457 "{} : {} != {}\n{:#?}\n",
458 "The number of constants and the number of ciphertexts must be the same"
459 .red()
460 .bold(),
461 $nb_ct1,
462 $nb_ct2,
463 Backtrace::new()
464 ),
465 };
466 };
467}
468
469#[macro_export]
470macro_rules! PrecisionError {
471 () => {
472 CryptoAPIError::PrecisionError {
473 description: format!(
474 "{}\n{:?}\n",
475 "Number of bit for precision == 0".red().bold(),
476 Backtrace::new()
477 ),
478 };
479 };
480}
481
482#[macro_export]
483macro_rules! MinMaxError {
484 ($min: expr, $max: expr) => {
485 CryptoAPIError::MinMaxError {
486 min: $min,
487 max: $max,
488 description: format!(
489 "min (= {}) <= max (= {})\n{:#?}\n",
490 $min,
491 $max,
492 Backtrace::new()
493 ),
494 };
495 };
496}
497
498#[macro_export]
499macro_rules! RadiusError {
500 ($radius: expr) => {
501 CryptoAPIError::RadiusError {
502 radius: $radius,
503 description: format!(
504 "{}: {}\n{:#?}\n",
505 "Invalid radius".red().bold(),
506 $radius,
507 Backtrace::new()
508 ),
509 };
510 };
511}
512
513#[macro_export]
514macro_rules! MonomialError {
515 ($polynomial_size: expr, $monomial: expr) => {
516 CryptoAPIError::MonomialError {
517 polynomial_size: $polynomial_size,
518 monomial: $monomial,
519 description: format!(
520 "{}: polynomial_size (= {}) <= monomial index (= {})\n{:#?}\n",
521 "Can't access the monomial coefficient".red().bold(),
522 $polynomial_size,
523 $monomial,
524 Backtrace::new()
525 ),
526 };
527 };
528}
529
530#[macro_export]
531macro_rules! NotPowerOfTwoError {
532 ($polynomial_size: expr) => {
533 CryptoAPIError::NotPowerOfTwoError {
534 polynomial_size: $polynomial_size,
535 description: format!(
536 "polynomial_size (= {}) {}\n{:#?}\n",
537 $polynomial_size,
538 "must be a power of 2".red().bold(),
539 Backtrace::new()
540 ),
541 };
542 };
543}
544
545#[macro_export]
546macro_rules! ZeroCiphertextsInStructureError {
547 ($nb_ciphertexts: expr) => {
548 CryptoAPIError::ZeroCiphertextsInStructureError {
549 nb_ciphertexts: $nb_ciphertexts,
550 description: format!(
551 "There should be {}: nb_ciphertexts provided is {}\n{:#?}\n",
552 "at least one ciphertext in the structure".red().bold(),
553 $nb_ciphertexts,
554 Backtrace::new()
555 ),
556 };
557 };
558}
559
560#[macro_export]
561macro_rules! WrongSizeError {
562 ($size: expr) => {
563 CryptoAPIError::WrongSizeError {
564 size: $size,
565 description: format!(
566 "The {}: {}\n{:#?}\n",
567 "size is wrong".red().bold(),
568 $size,
569 Backtrace::new()
570 ),
571 };
572 };
573}
574
575#[macro_export]
576macro_rules! NotEnoughValidEncoderError {
577 ($nb_valid_encoders: expr,$nb_actions:expr) => {
578 CryptoAPIError::NotEnoughValidEncoderError {
579 nb_valid_encoders: $nb_valid_encoders,
580 nb_actions: $nb_actions,
581 description: format!(
582 "There are only {} {} but it was asked to work on {}\n{:#?}\n",
583 $nb_valid_encoders,
584 "valid encoders".red().bold(),
585 $nb_actions,
586 Backtrace::new()
587 ),
588 };
589 };
590}
591
592#[macro_export]
593macro_rules! LweToRlweError {
594 ($dimension: expr,$polynomial_size:expr) => {
595 CryptoAPIError::LweToRlweError {
596 dimension: $dimension,
597 polynomial_size: $polynomial_size,
598 description: format!(
599 "{} with dimension = {} {} with polynomial_size = {}\n{:#?}\n",
600 "Can't cast a Lwe".red().bold(),
601 $dimension,
602 "into a Rlwe".red().bold(),
603 $polynomial_size,
604 Backtrace::new()
605 ),
606 };
607 };
608}