aaefd5ac0e59855b0a21a41d2a3076df5078d8d9
[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 Christian Fuchs
25  * @author Florian Dold
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include <gcrypt.h>
30
31
32 int
33 main (int argc, char *argv[])
34 {
35   int ret;
36   gcry_mpi_t m1;
37   gcry_mpi_t m2;
38   gcry_mpi_t result;
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;
45   
46   GNUNET_CRYPTO_paillier_create (&public_key, &private_key);
47
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);
59
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);
62     return 1;
63   }
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);
66     return 1;
67   }
68   
69   GNUNET_CRYPTO_paillier_encrypt (&public_key, m2, &c2);
70
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);
73     return 1;
74   }
75   
76   GNUNET_CRYPTO_paillier_decrypt (&private_key, &public_key,
77                                   &c_result, hom_result);
78   
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");
83     return 1;
84   }
85   
86   return 0;
87 }
88
89 /* end of test_crypto_paillier.c */