- log
[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 test_crypto ()
34 {
35   gcry_mpi_t plaintext;
36   gcry_mpi_t plaintext_result;
37   struct GNUNET_CRYPTO_PaillierCiphertext ciphertext;
38   struct GNUNET_CRYPTO_PaillierPublicKey public_key;
39   struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
40
41   GNUNET_CRYPTO_paillier_create (&public_key, &private_key);
42
43   GNUNET_assert (NULL != (plaintext = gcry_mpi_new (0)));
44   GNUNET_assert (NULL != (plaintext_result = gcry_mpi_new (0)));
45
46   gcry_mpi_randomize (plaintext, GNUNET_CRYPTO_PAILLIER_BITS / 2, GCRY_WEAK_RANDOM);
47
48   GNUNET_CRYPTO_paillier_encrypt (&public_key, plaintext, 0, &ciphertext);
49
50   GNUNET_CRYPTO_paillier_decrypt (&private_key, &public_key,
51                                   &ciphertext, plaintext_result);
52
53   if (0 != gcry_mpi_cmp (plaintext, plaintext_result))
54   {
55     printf ("paillier failed with plaintext of size %u\n", gcry_mpi_get_nbits (plaintext));
56     gcry_log_debugmpi("\n", plaintext);
57     gcry_log_debugmpi("\n", plaintext_result);
58     return 1;
59   }
60   return 0;
61 }
62
63 int
64 test_hom()
65 {
66   int ret;
67   gcry_mpi_t m1;
68   gcry_mpi_t m2;
69   gcry_mpi_t result;
70   gcry_mpi_t hom_result;
71   struct GNUNET_CRYPTO_PaillierCiphertext c1;
72   struct GNUNET_CRYPTO_PaillierCiphertext c2;
73   struct GNUNET_CRYPTO_PaillierCiphertext c_result;
74   struct GNUNET_CRYPTO_PaillierPublicKey public_key;
75   struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
76   
77   GNUNET_CRYPTO_paillier_create (&public_key, &private_key);
78
79   GNUNET_assert (NULL != (m1 = gcry_mpi_new (0)));
80   GNUNET_assert (NULL != (m2 = gcry_mpi_new (0)));
81   GNUNET_assert (NULL != (result = gcry_mpi_new (0)));
82   GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0)));
83   //gcry_mpi_randomize (m1, GNUNET_CRYPTO_PAILLIER_BITS-2, GCRY_WEAK_RANDOM);
84   m1 = gcry_mpi_set_ui(m1,1);
85   gcry_mpi_mul_2exp(m1,m1,GNUNET_CRYPTO_PAILLIER_BITS-3);
86   //gcry_mpi_randomize (m2, GNUNET_CRYPTO_PAILLIER_BITS-2, GCRY_WEAK_RANDOM);
87   m2 = gcry_mpi_set_ui(m2,1);
88   gcry_mpi_mul_2exp(m2,m2,GNUNET_CRYPTO_PAILLIER_BITS-3);
89   gcry_mpi_add(result,m1,m2);
90
91   if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key, m1, 2, &c1))){
92     printf ("GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n", ret);
93     return 1;
94   }
95   if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key, m2, 2, &c2))){
96     printf ("GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 1 allowed operation, got %d!\n", ret);
97     return 1;
98   }
99
100   if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add (&public_key, &c1,&c2, &c_result))){
101     printf ("GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n", ret);
102     return 1;
103   }
104   
105   GNUNET_CRYPTO_paillier_decrypt (&private_key, &public_key,
106                                   &c_result, hom_result);
107   
108   gcry_log_debugmpi("\n", hom_result);
109   gcry_log_debugmpi("\n", result);
110   if (0 != gcry_mpi_cmp(result, hom_result)){
111     printf ("GNUNET_CRYPTO_paillier miscalculated!\n");
112     return 1;
113   }
114   
115   return 0;
116 }
117
118
119 int
120 main (int argc, char *argv[])
121 {
122   int ret;
123   ret = test_crypto ();
124   if (0 != ret)
125     return ret;
126   ret = test_hom ();
127   return ret;
128 }
129
130 /* end of test_crypto_paillier.c */