aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/test_crypto_paillier.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/test_crypto_paillier.c')
-rw-r--r--src/lib/util/test_crypto_paillier.c248
1 files changed, 248 insertions, 0 deletions
diff --git a/src/lib/util/test_crypto_paillier.c b/src/lib/util/test_crypto_paillier.c
new file mode 100644
index 000000000..412ce5c23
--- /dev/null
+++ b/src/lib/util/test_crypto_paillier.c
@@ -0,0 +1,248 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2014 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19
20 */
21/**
22 * @file util/test_crypto_paillier.c
23 * @brief testcase paillier crypto
24 * @author Christian Fuchs
25 * @author Florian Dold
26 */
27
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#include <gcrypt.h>
31
32
33static int
34test_crypto ()
35{
36 gcry_mpi_t plaintext;
37 gcry_mpi_t plaintext_result;
38 struct GNUNET_CRYPTO_PaillierCiphertext ciphertext;
39 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
40 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
41 int ret = 0;
42
43 GNUNET_CRYPTO_paillier_create (&public_key,
44 &private_key);
45 GNUNET_assert (NULL != (plaintext = gcry_mpi_new (0)));
46 GNUNET_assert (NULL != (plaintext_result = gcry_mpi_new (0)));
47 gcry_mpi_randomize (plaintext,
48 GNUNET_CRYPTO_PAILLIER_BITS / 2,
49 GCRY_WEAK_RANDOM);
50
51 GNUNET_CRYPTO_paillier_encrypt (&public_key,
52 plaintext,
53 0 /* 0 hom ops */,
54 &ciphertext);
55 GNUNET_CRYPTO_paillier_decrypt (&private_key,
56 &public_key,
57 &ciphertext,
58 plaintext_result);
59 if (0 != gcry_mpi_cmp (plaintext,
60 plaintext_result))
61 {
62 fprintf (stderr,
63 "Paillier decryption failed with plaintext of size %u\n",
64 gcry_mpi_get_nbits (plaintext));
65 gcry_log_debugmpi ("\n",
66 plaintext);
67 gcry_log_debugmpi ("\n",
68 plaintext_result);
69 ret = 1;
70 }
71 gcry_mpi_release (plaintext);
72 gcry_mpi_release (plaintext_result);
73 return ret;
74}
75
76
77static int
78test_hom_simple (unsigned int a,
79 unsigned int b)
80{
81 gcry_mpi_t m1;
82 gcry_mpi_t m2;
83 gcry_mpi_t result;
84 gcry_mpi_t hom_result;
85 struct GNUNET_CRYPTO_PaillierCiphertext c1;
86 struct GNUNET_CRYPTO_PaillierCiphertext c2;
87 struct GNUNET_CRYPTO_PaillierCiphertext c_result;
88 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
89 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
90 int ret = 0;
91
92 GNUNET_CRYPTO_paillier_create (&public_key,
93 &private_key);
94
95 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
96 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
97 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
98 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
99 m1 = gcry_mpi_set_ui (m1, a);
100 m2 = gcry_mpi_set_ui (m2, b);
101 gcry_mpi_add (result,
102 m1,
103 m2);
104 GNUNET_CRYPTO_paillier_encrypt (&public_key,
105 m1,
106 2,
107 &c1);
108 GNUNET_CRYPTO_paillier_encrypt (&public_key,
109 m2,
110 2,
111 &c2);
112 GNUNET_CRYPTO_paillier_hom_add (&public_key,
113 &c1,
114 &c2,
115 &c_result);
116 GNUNET_CRYPTO_paillier_decrypt (&private_key,
117 &public_key,
118 &c_result,
119 hom_result);
120 if (0 != gcry_mpi_cmp (result, hom_result))
121 {
122 fprintf (stderr,
123 "GNUNET_CRYPTO_paillier failed simple math!\n");
124 gcry_log_debugmpi ("got ", hom_result);
125 gcry_log_debugmpi ("wanted ", result);
126 ret = 1;
127 }
128 gcry_mpi_release (m1);
129 gcry_mpi_release (m2);
130 gcry_mpi_release (result);
131 gcry_mpi_release (hom_result);
132 return ret;
133}
134
135
136static int
137test_hom ()
138{
139 int ret;
140 gcry_mpi_t m1;
141 gcry_mpi_t m2;
142 gcry_mpi_t result;
143 gcry_mpi_t hom_result;
144 struct GNUNET_CRYPTO_PaillierCiphertext c1;
145 struct GNUNET_CRYPTO_PaillierCiphertext c2;
146 struct GNUNET_CRYPTO_PaillierCiphertext c_result;
147 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
148 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
149
150 GNUNET_CRYPTO_paillier_create (&public_key,
151 &private_key);
152
153 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
154 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
155 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
156 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
157 m1 = gcry_mpi_set_ui (m1, 1);
158 /* m1 = m1 * 2 ^ (GCPB - 3) */
159 gcry_mpi_mul_2exp (m1,
160 m1,
161 GNUNET_CRYPTO_PAILLIER_BITS - 3);
162 m2 = gcry_mpi_set_ui (m2, 15);
163 /* m1 = m1 * 2 ^ (GCPB / 2) */
164 gcry_mpi_mul_2exp (m2,
165 m2,
166 GNUNET_CRYPTO_PAILLIER_BITS / 2);
167 gcry_mpi_add (result,
168 m1,
169 m2);
170
171 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
172 m1,
173 2,
174 &c1)))
175 {
176 fprintf (stderr,
177 "GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n",
178 ret);
179 ret = 1;
180 goto out;
181 }
182 if (2 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
183 m2,
184 2,
185 &c2)))
186 {
187 fprintf (stderr,
188 "GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 2 allowed operation, got %d!\n",
189 ret);
190 ret = 1;
191 goto out;
192 }
193
194 if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add (&public_key,
195 &c1,
196 &c2,
197 &c_result)))
198 {
199 fprintf (stderr,
200 "GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n",
201 ret);
202 ret = 1;
203 goto out;
204 }
205
206 GNUNET_CRYPTO_paillier_decrypt (&private_key,
207 &public_key,
208 &c_result,
209 hom_result);
210
211 if (0 != gcry_mpi_cmp (result, hom_result))
212 {
213 fprintf (stderr,
214 "GNUNET_CRYPTO_paillier miscalculated with large numbers!\n");
215 gcry_log_debugmpi ("got", hom_result);
216 gcry_log_debugmpi ("wanted", result);
217 ret = 1;
218 }
219out:
220 gcry_mpi_release (m1);
221 gcry_mpi_release (m2);
222 gcry_mpi_release (result);
223 gcry_mpi_release (hom_result);
224 return ret;
225}
226
227
228int
229main (int argc,
230 char *argv[])
231{
232 int ret;
233
234 ret = test_crypto ();
235 if (0 != ret)
236 return ret;
237 ret = test_hom_simple (2, 4);
238 if (0 != ret)
239 return ret;
240 ret = test_hom_simple (13, 17);
241 if (0 != ret)
242 return ret;
243 ret = test_hom ();
244 return ret;
245}
246
247
248/* end of test_crypto_paillier.c */