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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/test_crypto_paillier.c
23 * @brief testcase paillier crypto
24 * @author Christian Fuchs
25 * @author Florian Dold
28 #include "gnunet_util_lib.h"
36 gcry_mpi_t plaintext_result;
37 struct GNUNET_CRYPTO_PaillierCiphertext ciphertext;
38 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
39 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
42 GNUNET_CRYPTO_paillier_create (&public_key,
44 GNUNET_assert (NULL != (plaintext = gcry_mpi_new (0)));
45 GNUNET_assert (NULL != (plaintext_result = gcry_mpi_new (0)));
46 gcry_mpi_randomize (plaintext,
47 GNUNET_CRYPTO_PAILLIER_BITS / 2,
50 GNUNET_CRYPTO_paillier_encrypt (&public_key,
54 GNUNET_CRYPTO_paillier_decrypt (&private_key,
58 if (0 != gcry_mpi_cmp (plaintext,
62 "Paillier decryption failed with plaintext of size %u\n",
63 gcry_mpi_get_nbits (plaintext));
64 gcry_log_debugmpi ("\n",
66 gcry_log_debugmpi ("\n",
70 gcry_mpi_release (plaintext);
71 gcry_mpi_release (plaintext_result);
77 test_hom_simple (unsigned int a,
83 gcry_mpi_t hom_result;
84 struct GNUNET_CRYPTO_PaillierCiphertext c1;
85 struct GNUNET_CRYPTO_PaillierCiphertext c2;
86 struct GNUNET_CRYPTO_PaillierCiphertext c_result;
87 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
88 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
91 GNUNET_CRYPTO_paillier_create (&public_key,
94 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
95 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
96 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
97 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
98 m1 = gcry_mpi_set_ui (m1, a);
99 m2 = gcry_mpi_set_ui (m2, b);
100 gcry_mpi_add (result,
103 GNUNET_CRYPTO_paillier_encrypt (&public_key,
107 GNUNET_CRYPTO_paillier_encrypt (&public_key,
111 GNUNET_CRYPTO_paillier_hom_add (&public_key,
115 GNUNET_CRYPTO_paillier_decrypt (&private_key,
119 if (0 != gcry_mpi_cmp (result, hom_result))
122 "GNUNET_CRYPTO_paillier failed simple math!\n");
123 gcry_log_debugmpi ("got ", hom_result);
124 gcry_log_debugmpi ("wanted ", result);
127 gcry_mpi_release (m1);
128 gcry_mpi_release (m2);
129 gcry_mpi_release (result);
130 gcry_mpi_release (hom_result);
142 gcry_mpi_t hom_result;
143 struct GNUNET_CRYPTO_PaillierCiphertext c1;
144 struct GNUNET_CRYPTO_PaillierCiphertext c2;
145 struct GNUNET_CRYPTO_PaillierCiphertext c_result;
146 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
147 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
149 GNUNET_CRYPTO_paillier_create (&public_key,
152 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
153 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
154 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
155 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
156 m1 = gcry_mpi_set_ui (m1, 1);
157 /* m1 = m1 * 2 ^ (GCPB - 3) */
158 gcry_mpi_mul_2exp (m1,
160 GNUNET_CRYPTO_PAILLIER_BITS - 3);
161 m2 = gcry_mpi_set_ui (m2, 15);
162 /* m1 = m1 * 2 ^ (GCPB / 2) */
163 gcry_mpi_mul_2exp (m2,
165 GNUNET_CRYPTO_PAILLIER_BITS / 2);
166 gcry_mpi_add (result,
170 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
176 "GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n",
181 if (2 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
187 "GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 2 allowed operation, got %d!\n",
193 if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add (&public_key,
199 "GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n",
205 GNUNET_CRYPTO_paillier_decrypt (&private_key,
210 if (0 != gcry_mpi_cmp (result, hom_result))
213 "GNUNET_CRYPTO_paillier miscalculated with large numbers!\n");
214 gcry_log_debugmpi ("got", hom_result);
215 gcry_log_debugmpi ("wanted", result);
219 gcry_mpi_release (m1);
220 gcry_mpi_release (m2);
221 gcry_mpi_release (result);
222 gcry_mpi_release (hom_result);
232 ret = test_crypto ();
235 ret = test_hom_simple (2,4);
238 ret = test_hom_simple (13,17);
245 /* end of test_crypto_paillier.c */