386f21d7c099dd26925730bbc8f973008a30f855
[oweals/gnunet.git] / src / util / test_crypto_paillier.c
1 /*
2      This file is part of GNUnet.
3      (C) 2014 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19
20 */
21 /**
22  * @file util/test_crypto_paillier.c
23  * @brief testcase paillier crypto
24  * @author Florian Dold
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include <gcrypt.h>
29
30
31 int
32 main (int argc, char *argv[])
33 {
34   int ret;
35   gcry_mpi_t m1;
36   gcry_mpi_t m2;
37   gcry_mpi_t result;
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;
42   
43   struct GNUNET_CRYPTO_PaillierPublicKey public_key;
44   struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
45
46   GNUNET_CRYPTO_paillier_create (&public_key, &private_key);
47
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);
54
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);
57     return 1;
58   }
59   
60   GNUNET_CRYPTO_paillier_encrypt (&public_key, m2, &c2);
61
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);
64     return 1;
65   }
66   
67   GNUNET_CRYPTO_paillier_decrypt (&private_key, &public_key,
68                                   &c_result, hom_result);
69
70   if (0 != gcry_mpi_cmp(result, hom_result))
71     printf ("GNUNET_CRYPTO_paillier miscalculated!\n");
72     return 1;
73   
74   return 0;
75 }
76
77 /* end of test_crypto_paillier.c */