2 This file is part of GNUnet.
3 Copyright (C) 2014 GNUnet e.V.
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.
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.
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/>.
20 * @file util/test_crypto_paillier.c
21 * @brief testcase paillier crypto
22 * @author Christian Fuchs
23 * @author Florian Dold
26 #include "gnunet_util_lib.h"
34 gcry_mpi_t plaintext_result;
35 struct GNUNET_CRYPTO_PaillierCiphertext ciphertext;
36 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
37 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
40 GNUNET_CRYPTO_paillier_create (&public_key,
42 GNUNET_assert (NULL != (plaintext = gcry_mpi_new (0)));
43 GNUNET_assert (NULL != (plaintext_result = gcry_mpi_new (0)));
44 gcry_mpi_randomize (plaintext,
45 GNUNET_CRYPTO_PAILLIER_BITS / 2,
48 GNUNET_CRYPTO_paillier_encrypt (&public_key,
52 GNUNET_CRYPTO_paillier_decrypt (&private_key,
56 if (0 != gcry_mpi_cmp (plaintext,
60 "Paillier decryption failed with plaintext of size %u\n",
61 gcry_mpi_get_nbits (plaintext));
62 gcry_log_debugmpi ("\n",
64 gcry_log_debugmpi ("\n",
68 gcry_mpi_release (plaintext);
69 gcry_mpi_release (plaintext_result);
75 test_hom_simple (unsigned int a,
81 gcry_mpi_t hom_result;
82 struct GNUNET_CRYPTO_PaillierCiphertext c1;
83 struct GNUNET_CRYPTO_PaillierCiphertext c2;
84 struct GNUNET_CRYPTO_PaillierCiphertext c_result;
85 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
86 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
89 GNUNET_CRYPTO_paillier_create (&public_key,
92 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
93 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
94 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
95 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
96 m1 = gcry_mpi_set_ui (m1, a);
97 m2 = gcry_mpi_set_ui (m2, b);
101 GNUNET_CRYPTO_paillier_encrypt (&public_key,
105 GNUNET_CRYPTO_paillier_encrypt (&public_key,
109 GNUNET_CRYPTO_paillier_hom_add (&public_key,
113 GNUNET_CRYPTO_paillier_decrypt (&private_key,
117 if (0 != gcry_mpi_cmp (result, hom_result))
120 "GNUNET_CRYPTO_paillier failed simple math!\n");
121 gcry_log_debugmpi ("got ", hom_result);
122 gcry_log_debugmpi ("wanted ", result);
125 gcry_mpi_release (m1);
126 gcry_mpi_release (m2);
127 gcry_mpi_release (result);
128 gcry_mpi_release (hom_result);
140 gcry_mpi_t hom_result;
141 struct GNUNET_CRYPTO_PaillierCiphertext c1;
142 struct GNUNET_CRYPTO_PaillierCiphertext c2;
143 struct GNUNET_CRYPTO_PaillierCiphertext c_result;
144 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
145 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
147 GNUNET_CRYPTO_paillier_create (&public_key,
150 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
151 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
152 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
153 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
154 m1 = gcry_mpi_set_ui (m1, 1);
155 /* m1 = m1 * 2 ^ (GCPB - 3) */
156 gcry_mpi_mul_2exp (m1,
158 GNUNET_CRYPTO_PAILLIER_BITS - 3);
159 m2 = gcry_mpi_set_ui (m2, 15);
160 /* m1 = m1 * 2 ^ (GCPB / 2) */
161 gcry_mpi_mul_2exp (m2,
163 GNUNET_CRYPTO_PAILLIER_BITS / 2);
164 gcry_mpi_add (result,
168 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
174 "GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n",
179 if (2 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
185 "GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 2 allowed operation, got %d!\n",
191 if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add (&public_key,
197 "GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n",
203 GNUNET_CRYPTO_paillier_decrypt (&private_key,
208 if (0 != gcry_mpi_cmp (result, hom_result))
211 "GNUNET_CRYPTO_paillier miscalculated with large numbers!\n");
212 gcry_log_debugmpi ("got", hom_result);
213 gcry_log_debugmpi ("wanted", result);
217 gcry_mpi_release (m1);
218 gcry_mpi_release (m2);
219 gcry_mpi_release (result);
220 gcry_mpi_release (hom_result);
230 ret = test_crypto ();
233 ret = test_hom_simple (2,4);
236 ret = test_hom_simple (13,17);
243 /* end of test_crypto_paillier.c */