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"
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;
41 GNUNET_CRYPTO_paillier_create (&public_key, &private_key);
43 GNUNET_assert (NULL != (plaintext = gcry_mpi_new (0)));
44 GNUNET_assert (NULL != (plaintext_result = gcry_mpi_new (0)));
46 gcry_mpi_randomize (plaintext, GNUNET_CRYPTO_PAILLIER_BITS / 2, GCRY_WEAK_RANDOM);
48 GNUNET_CRYPTO_paillier_encrypt (&public_key, plaintext, 0, &ciphertext);
50 GNUNET_CRYPTO_paillier_decrypt (&private_key, &public_key,
51 &ciphertext, plaintext_result);
53 if (0 != gcry_mpi_cmp (plaintext, plaintext_result))
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);
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;
77 GNUNET_CRYPTO_paillier_create (&public_key, &private_key);
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);
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);
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);
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);
105 GNUNET_CRYPTO_paillier_decrypt (&private_key, &public_key,
106 &c_result, hom_result);
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");
120 main (int argc, char *argv[])
123 ret = test_crypto ();
130 /* end of test_crypto_paillier.c */