cupcake/
traits.rs

1// Copyright (c) Facebook, Inc. and its affiliates.
2//
3// This source code is licensed under the MIT license found in the
4// LICENSE file in the root directory of this source tree.
5/// Trait for key generation
6pub trait KeyGeneration<PK,SK>: EncryptionOfZeros<PK, SK>{
7    /// Generate a (pk, sk) keypair
8    fn generate_keypair(&self) -> (PK, SK);
9
10    /// Generate a secret key
11    fn generate_key(&self) -> SK;
12}
13
14/// Trait for encryption of zeros.
15pub trait EncryptionOfZeros<CT, SK>{
16    /// Use the secret key to generate a fresh encryption of zero
17    fn encrypt_zero_sk(&self, sk: &SK) -> CT;
18
19    /// Generate a fresh encryption of the zero plaintext
20    fn encrypt_zero(&self, pk: &CT) -> CT;
21}
22
23/// Trait for symmetric key encryption.
24pub trait SKEncryption<CT, PT, SK>: KeyGeneration<CT, SK>{
25
26    /// Encrypt a given plaintext
27    fn encrypt_sk(&self, pt: &PT, sk: &SK) -> CT;
28
29    /// Decrypt a ciphertext
30    fn decrypt(&self, ct: &CT, sk: &SK) -> PT;
31}
32
33/// Trait for public key encryption.
34pub trait PKEncryption<CT, PT, SK>: SKEncryption<CT, PT, SK> {
35    /// Encrypt a given plaintext
36    fn encrypt(&self, pt: &PT, pk: &CT) -> CT;
37}
38
39/// Trait for adding a plaintext to a ciphertext.
40pub trait CipherPlainAddition<CT, PT>: {
41    /// Add a plaintext into a ciphertext.
42    fn add_plain_inplace(&self, ct1: &mut CT, pt: &PT);
43}
44
45
46/// Trait for additive homomorphic operations.
47pub trait AdditiveHomomorphicScheme<CT, SK>: EncryptionOfZeros<CT, SK> {
48    /// Add a ciphertext into another.
49    fn add_inplace(&self, ct1: &mut CT, ct2: &CT);
50
51    /// Rerandomize a ciphertext in-place. The resulting ciphertext will decrypt to the same
52    /// plaintext, while being unlinkable to the input ciphertext.
53    fn rerandomize(&self, ct: &mut CT, pk: &CT);
54}
55
56/// Trait for serialization.
57pub trait Serializable{
58    /// Serialize to a vector of bytes.
59    fn to_bytes(&self) -> Vec<u8>;
60
61    /// Deserialize from a vector of bytes.
62    fn from_bytes(bytes: &Vec<u8>) -> Self;
63}
64
65/// Number-theoretic transform (NTT) and fast polynomial multiplication based on NTT.
66pub trait NTT<T>: Clone {
67    fn is_ntt_form(&self) -> bool;
68
69    fn set_ntt_form(&mut self, value: bool);
70
71    fn forward_transform(&mut self);
72
73    fn inverse_transform(&mut self);
74}
75
76pub trait FastPolyMultiply<T>: NTT<T> {
77    fn multiply_fast(&self, other: &Self) -> Self;
78
79    fn coeffwise_multiply(&self, other: &Self) -> Self;
80}