2 This file is part of GNUnet.
3 (C) 2014 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/test_crypto_paillier.c
23 * @brief testcase paillier crypto
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
32 main (int argc, char *argv[])
38 gcry_mpi_t hom_result;
39 struct GNUNET_CRYPTO_PaillierCiphertext c1;
40 struct GNUNET_CRYPTO_PaillierCiphertext c2;
41 struct GNUNET_CRYPTO_PaillierCiphertext c_result;
43 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
44 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
46 GNUNET_CRYPTO_paillier_create (&public_key, &private_key);
48 GNUNET_assert (NULL != (m1 = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS-1)));
49 GNUNET_assert (NULL != (m2 = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS-1)));
50 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS)));
51 gcry_mpi_randomize (m1, GNUNET_CRYPTO_PAILLIER_BITS-1, GCRY_WEAK_RANDOM);
52 gcry_mpi_randomize (m2, GNUNET_CRYPTO_PAILLIER_BITS-1, GCRY_WEAK_RANDOM);
53 gcry_mpi_add(result,m1,m2);
55 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key, m1, &c1))){
56 printf ("GNUNET_CRYPTO_paillier_encrypt failed, should return 1 allowed operation, got %d!\n", ret);
60 GNUNET_CRYPTO_paillier_encrypt (&public_key, m2, &c2);
62 if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add (&public_key, &c1,&c2, &c_result))){
63 printf ("GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n", ret);
67 GNUNET_CRYPTO_paillier_decrypt (&private_key, &public_key,
68 &c_result, hom_result);
70 if (0 != gcry_mpi_cmp(result, hom_result))
71 printf ("GNUNET_CRYPTO_paillier miscalculated!\n");
77 /* end of test_crypto_paillier.c */