1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use paillier::*;
pub use paillier::EncodedCiphertext;
use serde::{Deserialize, Serialize};

pub use keys::{KeyPair, UserEncryptionKey};

pub mod keys;

#[derive(Debug, Deserialize, Serialize)]
pub struct UserCipher {
    pub recipient: String,
    pub cipher: EncodedCiphertext<u64>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct UserVecCipher {
    pub recipient: String,
    pub cipher: EncodedCiphertext<Vec<u64>>,
}

pub fn load_key_pair(json_keys: &str) -> KeyPair {
    let key_pair: KeyPair = serde_json::from_str(json_keys).unwrap();
    key_pair
}

pub fn load_encryption_key(json_key: &str) -> EncryptionKey {
    let encryption_key: EncryptionKey = serde_json::from_str(json_key).unwrap();
    encryption_key
}

pub fn encrypt(ek: &EncryptionKey, num: u64) -> EncodedCiphertext<u64> {
    Paillier::encrypt(ek, num)
}

pub fn encrypt_with_minimal_key(ek: &MinimalEncryptionKey, num: u64) -> EncodedCiphertext<u64> {
    let full_ek = EncryptionKey {
        n: ek.n.clone(),
        nn: ek.n.clone() * ek.n.clone(),
    };
    Paillier::encrypt(&full_ek, num)
}

pub fn encrypt_vec(ek: &EncryptionKey, nums: &Vec<u64>) -> EncodedCiphertext<Vec<u64>> {
    Paillier::encrypt(ek, &*nums.clone())
}

pub fn decrypt(dk: &DecryptionKey, ct: &EncodedCiphertext<u64>) -> u64 {
    Paillier::decrypt(dk, ct)
}

pub fn decrypt_vec(dk: &DecryptionKey, ct: &EncodedCiphertext<Vec<u64>>) -> Vec<u64> {
    Paillier::decrypt(dk, ct)
}

pub fn add_ciphers(ek: &EncryptionKey, a: &EncodedCiphertext<u64>, b: &EncodedCiphertext<u64>) -> EncodedCiphertext<u64> {
    Paillier::add(ek, a, b)
}

pub fn add_vec_ciphers(ek: &EncryptionKey, a: &EncodedCiphertext<Vec<u64>>, b: &EncodedCiphertext<Vec<u64>>) -> EncodedCiphertext<Vec<u64>> {
    Paillier::add(ek, a, b)
}

pub fn add_all_vec(ek: &EncryptionKey, ciphers: &[EncodedCiphertext<Vec<u64>>]) -> EncodedCiphertext<Vec<u64>> {
    let ciphers_owned = ciphers.to_owned();
    let start = ciphers_owned[0].clone();
    let result = ciphers_owned[1..]
        .iter()
        .fold(start, |sum, item| add_vec_ciphers(ek, &sum, item));
    result
}

pub fn add_all(ek: &EncryptionKey, ciphers: &[EncodedCiphertext<u64>]) -> EncodedCiphertext<u64> {
    let ciphers_owned = ciphers.to_owned();
    let start = ciphers_owned[0].clone();
    let result = ciphers_owned[1..]
        .iter()
        .fold(start, |sum, item| add_ciphers(ek, &sum, item));
    result
}


pub fn encrypt_with_json_keys(num: u64, eks: &[String]) -> Vec<UserCipher> {
    let user_keys: Vec<UserEncryptionKey> = eks.
        iter()
        .map(|ek| serde_json::from_str(ek).unwrap())
        .collect();
    encrypt_with_keys(num, &user_keys)
}

pub fn encrypt_with_keys(num: u64, eks: &[UserEncryptionKey]) -> Vec<UserCipher> {
    let ciphers = eks
        .iter()
        .map(|ek| {
            let ct = encrypt(&ek.decryption_key, num);
            UserCipher {
                recipient: ek.user_id.clone(),
                cipher: ct,
            }
        }).collect();
    ciphers
}

pub fn encrypt_vec_with_json_keys(nums: &Vec<u64>, eks: &[String]) -> Vec<UserVecCipher> {
    let user_keys: Vec<UserEncryptionKey> = eks.
        iter()
        .map(|ek| serde_json::from_str(ek).unwrap())
        .collect();
    encrypt_vec_with_keys(nums, &user_keys)
}

pub fn encrypt_vec_with_keys(nums: &Vec<u64>, eks: &[UserEncryptionKey]) -> Vec<UserVecCipher> {
    let ciphers = eks
        .iter()
        .map(|ek| {
            let ct = encrypt_vec(&ek.decryption_key, nums);
            UserVecCipher {
                recipient: ek.user_id.clone(),
                cipher: ct,
            }
        }).collect();
    ciphers
}

#[cfg(test)]
mod tests {
    use paillier::MinimalEncryptionKey;

    use crate::UserEncryptionKey;

    #[test]
    fn encrypt_decrypt() {
        let key_pair = super::KeyPair::new();

        let initial = 20;
        let encrypted = super::encrypt(&key_pair.ek, initial);
        println!("{:?}", serde_json::to_string(&encrypted).unwrap());
        let decrypted = super::decrypt(&key_pair.dk, &encrypted);
        assert_eq!(initial, decrypted);
    }

    #[test]
    #[should_panic]
    fn decryption_fails() {
        let key_pair = super::KeyPair::new();

        let initial = 20;
        let encrypted = super::encrypt(&key_pair.ek, initial);

        let key_pair_2 = super::KeyPair::new();
        let decrypted = super::decrypt(&key_pair_2.dk, &encrypted);
    }

    #[test]
    fn add() {
        let key_pair = super::KeyPair::new();

        let a = super::encrypt(&key_pair.ek, 20);
        let b = super::encrypt(&key_pair.ek, 30);
        let sum = super::add_ciphers(&key_pair.ek, &a, &b);

        let decrypted = super::decrypt(&key_pair.dk, &sum);
        assert_eq!(50, decrypted);
    }

    #[test]
    fn add_all() {
        let key_pair = super::KeyPair::new();

        let a = super::encrypt(&key_pair.ek, 10);
        let b = super::encrypt(&key_pair.ek, 10);
        let c = super::encrypt(&key_pair.ek, 10);
        let d = super::encrypt(&key_pair.ek, 10);
        let e = super::encrypt(&key_pair.ek, 10);
        let sum = super::add_all(&key_pair.ek, &[a, b, c, d, e]);

        let decrypted = super::decrypt(&key_pair.dk, &sum);
        assert_eq!(50, decrypted);
    }

    #[test]
    fn encrypt_decrypt_vec() {
        let key_pair = super::KeyPair::new();

        let initial: Vec<u64> = vec![20, 30, 40];
        let encrypted = super::encrypt_vec(&key_pair.ek, &initial);
        let decrypted = super::decrypt_vec(&key_pair.dk, &encrypted);
        assert_eq!(initial, decrypted);
    }

    #[test]
    fn add_vec() {
        let key_pair = super::KeyPair::new();

        let a = super::encrypt_vec(&key_pair.ek, &vec![10, 20, 30]);
        let b = super::encrypt_vec(&key_pair.ek, &vec![40, 50, 60]);

        let sum = super::add_vec_ciphers(&key_pair.ek, &a, &b);

        let decrypted = super::decrypt_vec(&key_pair.dk, &sum);
        assert_eq!(vec![50, 70, 90], decrypted);
    }

    #[test]
    fn add_all_vec() {
        let key_pair = super::KeyPair::new();
        let a = super::encrypt_vec(&key_pair.ek, &vec![10, 20, 30]);
        let b = super::encrypt_vec(&key_pair.ek, &vec![40, 50, 60]);
        let c = super::encrypt_vec(&key_pair.ek, &vec![40, 50, 60]);

        let sum = super::add_all_vec(&key_pair.ek, &[a, b, c]);
        assert_eq!(vec![90, 120, 150], super::decrypt_vec(&key_pair.dk, &sum));
    }

    #[test]
    fn encrypt_with_keys() {
        let key_pair_1 = super::KeyPair::new();
        let key_pair_2 = super::KeyPair::new();
        let key_pair_3 = super::KeyPair::new();

        let key_pairs = &[key_pair_1, key_pair_2, key_pair_3];

        let mut user_keys: Vec<UserEncryptionKey> = Vec::new();
        for (index, key_pair) in key_pairs.iter().enumerate() {
            user_keys.push(
                UserEncryptionKey {
                    user_id: format!("user_{}", index),
                    decryption_key: key_pair.ek.clone(),
                }
            );
        }
        let encrypted = super::encrypt_with_keys(20, &user_keys);
        assert_eq!(3, encrypted.len());
        println!("{:?}", serde_json::to_string(&encrypted).unwrap());

        let decrypted = super::decrypt(&key_pairs[0].dk, &encrypted[0].cipher);
        assert_eq!(20, decrypted);


        let json_keys = user_keys
            .iter()
            .map(|user_key| serde_json::to_string(&user_key).unwrap())
            .collect::<Vec<String>>();
        let encrypted = super::encrypt_with_json_keys(20, &json_keys);
        assert_eq!(3, encrypted.len());
        let decrypted = super::decrypt(&key_pairs[0].dk, &encrypted[0].cipher);
        assert_eq!(20, decrypted);
    }

    #[test]
    fn encrypt_vec_with_keys() {
        let key_pair_1 = super::KeyPair::new();
        let key_pair_2 = super::KeyPair::new();
        let key_pair_3 = super::KeyPair::new();

        let key_pairs = &[key_pair_1, key_pair_2, key_pair_3];

        let mut user_keys: Vec<UserEncryptionKey> = Vec::new();
        for (index, key_pair) in key_pairs.iter().enumerate() {
            user_keys.push(
                UserEncryptionKey {
                    user_id: format!("user_{}", index),
                    decryption_key: key_pair.ek.clone(),
                }
            );
        }
        let encrypted = super::encrypt_vec_with_keys(&vec![20, 30, 40], &user_keys);
        assert_eq!(3, encrypted.len());

        let decrypted = super::decrypt_vec(&key_pairs[0].dk, &encrypted[0].cipher);
        assert_eq!(vec![20, 30, 40], decrypted);
    }

    #[test]
    fn encrypt_with_minimal_key() {
        let key_pair_1 = super::KeyPair::new();

        let minimal_key = MinimalEncryptionKey {
            n: key_pair_1.ek.n.clone(),
        };
        let encrypted = super::encrypt_with_minimal_key(&minimal_key, 20);
        let decrypted = super::decrypt(&key_pair_1.dk, &encrypted);
        assert_eq!(20, decrypted);
    }
}