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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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;
41 GNUNET_CRYPTO_paillier_create (&public_key,
43 GNUNET_assert (NULL != (plaintext = gcry_mpi_new (0)));
44 GNUNET_assert (NULL != (plaintext_result = gcry_mpi_new (0)));
45 gcry_mpi_randomize (plaintext,
46 GNUNET_CRYPTO_PAILLIER_BITS / 2,
49 GNUNET_CRYPTO_paillier_encrypt (&public_key,
53 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",
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;
88 GNUNET_CRYPTO_paillier_create (&public_key,
91 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
92 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
93 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
94 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
95 m1 = gcry_mpi_set_ui (m1, a);
96 m2 = gcry_mpi_set_ui (m2, b);
100 GNUNET_CRYPTO_paillier_encrypt (&public_key,
104 GNUNET_CRYPTO_paillier_encrypt (&public_key,
108 GNUNET_CRYPTO_paillier_hom_add (&public_key,
112 GNUNET_CRYPTO_paillier_decrypt (&private_key,
116 if (0 != gcry_mpi_cmp (result, hom_result))
119 "GNUNET_CRYPTO_paillier failed simple math!\n");
120 gcry_log_debugmpi ("got ", hom_result);
121 gcry_log_debugmpi ("wanted ", result);
135 gcry_mpi_t hom_result;
136 struct GNUNET_CRYPTO_PaillierCiphertext c1;
137 struct GNUNET_CRYPTO_PaillierCiphertext c2;
138 struct GNUNET_CRYPTO_PaillierCiphertext c_result;
139 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
140 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
142 GNUNET_CRYPTO_paillier_create (&public_key,
145 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
146 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
147 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
148 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
149 m1 = gcry_mpi_set_ui (m1, 1);
150 /* m1 = m1 * 2 ^ (GCPB - 3) */
151 gcry_mpi_mul_2exp (m1,
153 GNUNET_CRYPTO_PAILLIER_BITS - 3);
154 m2 = gcry_mpi_set_ui (m2, 15);
155 /* m1 = m1 * 2 ^ (GCPB / 2) */
156 gcry_mpi_mul_2exp (m2,
158 GNUNET_CRYPTO_PAILLIER_BITS / 2);
159 gcry_mpi_add (result,
163 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
169 "GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n",
173 if (2 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
179 "GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 2 allowed operation, got %d!\n",
184 if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add (&public_key,
190 "GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n",
195 GNUNET_CRYPTO_paillier_decrypt (&private_key,
200 if (0 != gcry_mpi_cmp (result, hom_result))
203 "GNUNET_CRYPTO_paillier miscalculated with large numbers!\n");
204 gcry_log_debugmpi ("got", hom_result);
205 gcry_log_debugmpi ("wanted", result);
217 ret = test_crypto ();
220 ret = test_hom_simple (2,4);
223 ret = test_hom_simple (13,17);
230 /* end of test_crypto_paillier.c */