paillier_common/traits.rs
1//! Abstract operations exposed by the library.
2
3/// Secure generation of fresh key pairs.
4pub trait KeyGeneration<KP> {
5 /// Generate fresh key pair with currently recommended security level (2048 bit modulus).
6 fn keypair() -> KP {
7 Self::keypair_with_modulus_size(2048)
8 }
9 fn keypair_safe_primes() -> KP {
10 Self::keypair_safe_primes_with_modulus_size(2048)
11 }
12 /// Generate fresh key pair with security level specified as the `bit_length` of the modulus.
13 ///
14 /// Currently recommended security level is a minimum of 2048 bits.
15 fn keypair_with_modulus_size(big_length: usize) -> KP;
16 fn keypair_safe_primes_with_modulus_size(big_length: usize) -> KP;
17}
18
19pub trait PrecomputeRandomness<EK, R, PR> {
20 fn precompute(ek: EK, r: R) -> PR;
21}
22
23/// Encryption of plaintext.
24pub trait Encrypt<EK, PT, CT> {
25 /// Encrypt plaintext `m` under key `ek` into a ciphertext.
26 fn encrypt(ek: &EK, m: PT) -> CT;
27}
28
29pub trait EncryptWithChosenRandomness<EK, PT, R, CT> {
30 fn encrypt_with_chosen_randomness(ek: &EK, m: PT, r: R) -> CT;
31}
32
33/// Decryption of ciphertext.
34pub trait Decrypt<DK, CT, PT> {
35 /// Decrypt ciphertext `c` using key `dk` into a plaintext.
36 fn decrypt(ek: &DK, c: CT) -> PT;
37}
38
39/// Opening of ciphertext.
40///
41/// Unlike decryption this also returns the randomness used.
42pub trait Open<DK, CT, PT, R> {
43 /// Open ciphertext `c` using key `dk` into a plaintext and a randomness.
44 fn open(dk: &DK, c: CT) -> (PT, R);
45}
46
47/// Addition of two ciphertexts.
48pub trait Add<EK, CT1, CT2, CT> {
49 /// Homomorphically combine ciphertexts `c1` and `c2` to obtain a ciphertext containing
50 /// the sum of the two underlying plaintexts, reduced modulus `n` from `ek`.
51 fn add(ek: &EK, c1: CT1, c2: CT2) -> CT;
52}
53
54/// Multiplication of ciphertext with plaintext.
55pub trait Mul<EK, CT1, PT2, CT> {
56 /// Homomorphically combine ciphertext `c1` and plaintext `m2` to obtain a ciphertext
57 /// containing the multiplication of the (underlying) plaintexts, reduced modulus `n` from `ek`.
58 fn mul(ek: &EK, c1: CT1, m2: PT2) -> CT;
59}
60
61/// Rerandomisation of ciphertext.
62pub trait Rerandomize<EK, CT1, CT> {
63 /// Rerandomise ciphertext `c` to hide any history of which homomorphic operations were
64 /// used to compute it, making it look exactly like a fresh encryption of the same plaintext.
65 fn rerandomize(ek: &EK, c: CT1) -> CT;
66}