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.
17 * @file util/test_crypto_paillier.c
18 * @brief testcase paillier crypto
19 * @author Christian Fuchs
20 * @author Florian Dold
23 #include "gnunet_util_lib.h"
31 gcry_mpi_t plaintext_result;
32 struct GNUNET_CRYPTO_PaillierCiphertext ciphertext;
33 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
34 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
37 GNUNET_CRYPTO_paillier_create (&public_key,
39 GNUNET_assert (NULL != (plaintext = gcry_mpi_new (0)));
40 GNUNET_assert (NULL != (plaintext_result = gcry_mpi_new (0)));
41 gcry_mpi_randomize (plaintext,
42 GNUNET_CRYPTO_PAILLIER_BITS / 2,
45 GNUNET_CRYPTO_paillier_encrypt (&public_key,
49 GNUNET_CRYPTO_paillier_decrypt (&private_key,
53 if (0 != gcry_mpi_cmp (plaintext,
57 "Paillier decryption failed with plaintext of size %u\n",
58 gcry_mpi_get_nbits (plaintext));
59 gcry_log_debugmpi ("\n",
61 gcry_log_debugmpi ("\n",
65 gcry_mpi_release (plaintext);
66 gcry_mpi_release (plaintext_result);
72 test_hom_simple (unsigned int a,
78 gcry_mpi_t hom_result;
79 struct GNUNET_CRYPTO_PaillierCiphertext c1;
80 struct GNUNET_CRYPTO_PaillierCiphertext c2;
81 struct GNUNET_CRYPTO_PaillierCiphertext c_result;
82 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
83 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
86 GNUNET_CRYPTO_paillier_create (&public_key,
89 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
90 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
91 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
92 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
93 m1 = gcry_mpi_set_ui (m1, a);
94 m2 = gcry_mpi_set_ui (m2, b);
98 GNUNET_CRYPTO_paillier_encrypt (&public_key,
102 GNUNET_CRYPTO_paillier_encrypt (&public_key,
106 GNUNET_CRYPTO_paillier_hom_add (&public_key,
110 GNUNET_CRYPTO_paillier_decrypt (&private_key,
114 if (0 != gcry_mpi_cmp (result, hom_result))
117 "GNUNET_CRYPTO_paillier failed simple math!\n");
118 gcry_log_debugmpi ("got ", hom_result);
119 gcry_log_debugmpi ("wanted ", result);
122 gcry_mpi_release (m1);
123 gcry_mpi_release (m2);
124 gcry_mpi_release (result);
125 gcry_mpi_release (hom_result);
137 gcry_mpi_t hom_result;
138 struct GNUNET_CRYPTO_PaillierCiphertext c1;
139 struct GNUNET_CRYPTO_PaillierCiphertext c2;
140 struct GNUNET_CRYPTO_PaillierCiphertext c_result;
141 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
142 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
144 GNUNET_CRYPTO_paillier_create (&public_key,
147 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
148 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
149 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
150 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
151 m1 = gcry_mpi_set_ui (m1, 1);
152 /* m1 = m1 * 2 ^ (GCPB - 3) */
153 gcry_mpi_mul_2exp (m1,
155 GNUNET_CRYPTO_PAILLIER_BITS - 3);
156 m2 = gcry_mpi_set_ui (m2, 15);
157 /* m1 = m1 * 2 ^ (GCPB / 2) */
158 gcry_mpi_mul_2exp (m2,
160 GNUNET_CRYPTO_PAILLIER_BITS / 2);
161 gcry_mpi_add (result,
165 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
171 "GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n",
176 if (2 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key,
182 "GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 2 allowed operation, got %d!\n",
188 if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add (&public_key,
194 "GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n",
200 GNUNET_CRYPTO_paillier_decrypt (&private_key,
205 if (0 != gcry_mpi_cmp (result, hom_result))
208 "GNUNET_CRYPTO_paillier miscalculated with large numbers!\n");
209 gcry_log_debugmpi ("got", hom_result);
210 gcry_log_debugmpi ("wanted", result);
214 gcry_mpi_release (m1);
215 gcry_mpi_release (m2);
216 gcry_mpi_release (result);
217 gcry_mpi_release (hom_result);
227 ret = test_crypto ();
230 ret = test_hom_simple (2,4);
233 ret = test_hom_simple (13,17);
240 /* end of test_crypto_paillier.c */