-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaillier.h
266 lines (239 loc) · 7.29 KB
/
paillier.h
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
/**
* @file paillier.h
*
* @date Created on: Aug 25, 2012
* @author Camille Vuillaume
* @copyright Camille Vuillaume, 2012
* @defgroup Paillier Paillier cryptosystem
*
* This file is part of Paillier-GMP.
*
* Paillier-GMP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* Paillier-GMP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Paillier-GMP. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PAILLIER_H_
#define PAILLIER_H_
#include <stdio.h>
#include <gmp.h>
/** Private key
*
* @ingroup Paillier
*
* In addition to the usual private key elements, the structure contains:
* - CRT parameters for accelerating exponentiations during decryption
* - The modular inverse n^{-1} mod 2^len for accelerating the calculation of L
*/
typedef struct {
mp_bitcnt_t len; /**< bit length of n */
mpz_t lambda; /**< least common multiple of p and q */
mpz_t mu; /**< Modular inverse */
mpz_t p2; /**< square of prime number p */
mpz_t q2; /**< square of prime number q */
mpz_t p2invq2; /**< CRT parameter p^{-2} mod q^2 */
mpz_t ninv; /**< modular inverse n^{-1} mod 2^len */
mpz_t n; /**< n=p*q */
} paillier_private_key;
/** Public key
*
* @ingroup Paillier
*
* The generator is 1+n. This is fine in view of security because Class[g,n] is random self-reducible over g,
* therefore the security of the cryptosystem does not depend on the choice of g.
*/
typedef struct {
mp_bitcnt_t len; /**< bit length of n */
mpz_t n; /**< modulus n */
mpz_t n2;
} paillier_public_key;
/** Memory allocation for public key
*
* @ingroup Paillier
* @param[in] pub input public key
*/
void paillier_public_init(paillier_public_key *pub);
/** Memory allocation for private key
*
* @ingroup Paillier
* @param[in] priv input private key
*/
void paillier_private_init(paillier_private_key *priv);
/** Free memory for public key
*
* @ingroup Paillier
* @param[in] pub input public key
*/
void paillier_public_clear(paillier_public_key *pub);
/** Free memory for private key
*
* @ingroup Paillier
* @param[in] priv input private key
*/
void paillier_private_clear(paillier_private_key *priv);
/** Output public key to stdio stream
*
* @ingroup Paillier
* @param[out] fp output stream
* @param[in] pub input public key
*/
int paillier_public_out_str(FILE *fp, paillier_public_key *pub);
/** Output private key to stdio stream
*
* @ingroup Paillier
* @param[out] fp output stream
* @param[in] priv input private key
*/
int paillier_private_out_str(FILE *fp, paillier_private_key *priv);
/** Input public key from stdio stream
*
* @ingroup Paillier
* @param[out] pub output public key
* @param[in] fp input stream
*/
int paillier_public_in_str(paillier_public_key *pub, FILE *fp);
/** Input private key from stdio stream
*
* @ingroup Paillier
* @param[out] priv output private key
* @param[in] fp input stream
*/
int paillier_private_in_str(paillier_private_key *priv, FILE *fp);
/** Key generation
*
* @ingroup Paillier
* @param[out] pub output public key
* @param[out] priv output private key
* @param[in] len input bit length of public modulus
* @return 0 if no error
*/
int paillier_keygen(
paillier_public_key *pub,
paillier_private_key *priv,
mp_bitcnt_t len);
/** Key generation to stdio stream
*
* @ingroup Paillier
* @param[out] public_key output stream for public key
* @param[out] private_key output stream for private key
* @param[in] len input bit length of public modulus
* @return 0 if no error
*/
int paillier_keygen_str(
FILE *public_key,
FILE *private_key,
int len);
/** Encrypt
*
* @ingroup Paillier
* @param[out] ciphertext output ciphertext c=g^m*r^n mod n^2
* @param[in] plaintext input plaintext m
* @param[in] pub input public key
* @return 0 if no error
*/
int paillier_encrypt(
mpz_t ciphertext,
mpz_t plaintext,
paillier_public_key *pub);
/** Encrypt from stdio stream
*
* @ingroup Paillier
* @param[out] ciphertext output stream for ciphertext c=g^m*r^n mod n^2
* @param[in] plaintext input stream for plaintext m
* @param[in] public_key input stream for public key
* @return 0 if no error
*/
int paillier_encrypt_str(
FILE *ciphertext,
FILE *plaintext,
FILE *public_key);
/** Decrypt
*
* @ingroup Paillier
* @param[out] plaintext output plaintext m
* @param[in] ciphertext input ciphertext
* @param[in] priv input private key
* @return 0 if no error
*/
int paillier_decrypt(
mpz_t plaintext,
mpz_t ciphertext,
paillier_private_key *priv);
/** Decrypt from stdio stream
*
* @ingroup Paillier
* @param[out] plaintext output stream for plaintext m
* @param[in] ciphertext input stream for ciphertext c
* @param[in] private_key input stream for private key
* @return 0 if no error
*/
int paillier_decrypt_str(
FILE *ciphertext,
FILE *plaintext,
FILE *private_key);
/** Homomorphically add two plaintexts
*
* @ingroup Paillier
* @param[out] ciphertext3 output ciphertext corresponding to the homomorphic addition of the two plaintexts
* @param[in] ciphertext1 input first ciphertext corresponding to a plaintext to be homomorphically added
* @param[in] ciphertext2 input second ciphertext corresponding to a plaintext to be homomorphically added
* @param[in] pub input public key
* @return 0 if no error
*/
int paillier_homomorphic_add(
mpz_t ciphertext3,
mpz_t ciphertext1,
mpz_t ciphertext2,
paillier_public_key *pub);
/** Homomorphically add two plaintexts from stdio stream
*
* @ingroup Paillier
* @param[out] ciphertext3 output stream for result of homomorphic addition
* @param[in] ciphertext1 input stream for first ciphertext c1
* @param[in] ciphertext2 input stream for second ciphertext c2
* @param[in] public_key input stream for public key
* @return 0 if no error
*/
int paillier_homomorphic_add_str(
FILE *ciphertext3,
FILE *ciphertext1,
FILE *ciphertext2,
FILE *public_key);
/** Homomorphically multiply a plaintext with a constant
*
* @ingroup Paillier
* @param[out] ciphertext2 output ciphertext corresponding to the homomorphic multiplication of the plaintext with the constant
* @param[in] ciphertext1 input ciphertext corresponding to a plaintext to be homomorphically multiplied
* @param[in] constant input constant to be homomorphically multiplied
* @param[in] pub input public key
* @return 0 if no error
*/
int paillier_homomorphic_multc(
mpz_t ciphertext2,
mpz_t ciphertext1,
mpz_t constant,
paillier_public_key *pub);
/** Homomorphically multiply a plaintext with a constant from stdio stream
*
* @ingroup Paillier
* @param[out] ciphertext2 output stream for result of homomorphic multiplication
* @param[in] ciphertext1 input stream for ciphertext c
* @param[in] constant input stream for constant k
* @param[in] public_key input stream for public key
* @return 0 if no error
*/
int paillier_homomorphic_multc_str(
FILE *ciphertext2,
FILE *ciphertext1,
FILE *plaintext,
FILE *public_key);
#endif /* PAILLIER_H_ */