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 Christian Fuchs
25 * @author Florian Dold
28 #include "gnunet_util_lib.h"
33 main (int argc, char *argv[])
39 gcry_mpi_t hom_result;
40 struct GNUNET_CRYPTO_PaillierCiphertext c1;
41 struct GNUNET_CRYPTO_PaillierCiphertext c2;
42 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 (0)));
49 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
50 GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
51 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
52 //gcry_mpi_randomize (m1, GNUNET_CRYPTO_PAILLIER_BITS-2, GCRY_WEAK_RANDOM);
53 m1 = gcry_mpi_set_ui(m1,1);
54 gcry_mpi_mul_2exp(m1,m1,GNUNET_CRYPTO_PAILLIER_BITS-2);
55 //gcry_mpi_randomize (m2, GNUNET_CRYPTO_PAILLIER_BITS-2, GCRY_WEAK_RANDOM);
56 m2 = gcry_mpi_set_ui(m2,1);
57 gcry_mpi_mul_2exp(m2,m2,GNUNET_CRYPTO_PAILLIER_BITS-2);
58 gcry_mpi_add(result,m1,m2);
60 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key, m1, &c1))){
61 printf ("GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n", ret);
64 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key, m2, &c2))){
65 printf ("GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 1 allowed operation, got %d!\n", ret);
69 GNUNET_CRYPTO_paillier_encrypt (&public_key, m2, &c2);
71 if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add (&public_key, &c1,&c2, &c_result))){
72 printf ("GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n", ret);
76 GNUNET_CRYPTO_paillier_decrypt (&private_key, &public_key,
77 &c_result, hom_result);
79 gcry_log_debugmpi("\n", hom_result);
80 gcry_log_debugmpi("\n", result);
81 if (0 != gcry_mpi_cmp(result, hom_result)){
82 printf ("GNUNET_CRYPTO_paillier miscalculated!\n");
89 /* end of test_crypto_paillier.c */