summaryrefslogtreecommitdiff
path: root/src/util/test_crypto_paillier.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_crypto_paillier.c')
-rw-r--r--src/util/test_crypto_paillier.c305
1 files changed, 153 insertions, 152 deletions
diff --git a/src/util/test_crypto_paillier.c b/src/util/test_crypto_paillier.c
index d19696372..c4555fd10 100644
--- a/src/util/test_crypto_paillier.c
+++ b/src/util/test_crypto_paillier.c
@@ -11,13 +11,13 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 19
20*/ 20 */
21/** 21/**
22 * @file util/test_crypto_paillier.c 22 * @file util/test_crypto_paillier.c
23 * @brief testcase paillier crypto 23 * @brief testcase paillier crypto
@@ -30,7 +30,7 @@
30 30
31 31
32static int 32static int
33test_crypto () 33test_crypto()
34{ 34{
35 gcry_mpi_t plaintext; 35 gcry_mpi_t plaintext;
36 gcry_mpi_t plaintext_result; 36 gcry_mpi_t plaintext_result;
@@ -39,43 +39,43 @@ test_crypto ()
39 struct GNUNET_CRYPTO_PaillierPrivateKey private_key; 39 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
40 int ret = 0; 40 int ret = 0;
41 41
42 GNUNET_CRYPTO_paillier_create (&public_key, 42 GNUNET_CRYPTO_paillier_create(&public_key,
43 &private_key); 43 &private_key);
44 GNUNET_assert (NULL != (plaintext = gcry_mpi_new (0))); 44 GNUNET_assert(NULL != (plaintext = gcry_mpi_new(0)));
45 GNUNET_assert (NULL != (plaintext_result = gcry_mpi_new (0))); 45 GNUNET_assert(NULL != (plaintext_result = gcry_mpi_new(0)));
46 gcry_mpi_randomize (plaintext, 46 gcry_mpi_randomize(plaintext,
47 GNUNET_CRYPTO_PAILLIER_BITS / 2, 47 GNUNET_CRYPTO_PAILLIER_BITS / 2,
48 GCRY_WEAK_RANDOM); 48 GCRY_WEAK_RANDOM);
49 49
50 GNUNET_CRYPTO_paillier_encrypt (&public_key, 50 GNUNET_CRYPTO_paillier_encrypt(&public_key,
51 plaintext, 51 plaintext,
52 0 /* 0 hom ops */, 52 0 /* 0 hom ops */,
53 &ciphertext); 53 &ciphertext);
54 GNUNET_CRYPTO_paillier_decrypt (&private_key, 54 GNUNET_CRYPTO_paillier_decrypt(&private_key,
55 &public_key, 55 &public_key,
56 &ciphertext, 56 &ciphertext,
57 plaintext_result); 57 plaintext_result);
58 if (0 != gcry_mpi_cmp (plaintext, 58 if (0 != gcry_mpi_cmp(plaintext,
59 plaintext_result)) 59 plaintext_result))
60 { 60 {
61 fprintf (stderr, 61 fprintf(stderr,
62 "Paillier decryption failed with plaintext of size %u\n", 62 "Paillier decryption failed with plaintext of size %u\n",
63 gcry_mpi_get_nbits (plaintext)); 63 gcry_mpi_get_nbits(plaintext));
64 gcry_log_debugmpi ("\n", 64 gcry_log_debugmpi("\n",
65 plaintext); 65 plaintext);
66 gcry_log_debugmpi ("\n", 66 gcry_log_debugmpi("\n",
67 plaintext_result); 67 plaintext_result);
68 ret = 1; 68 ret = 1;
69 } 69 }
70 gcry_mpi_release (plaintext); 70 gcry_mpi_release(plaintext);
71 gcry_mpi_release (plaintext_result); 71 gcry_mpi_release(plaintext_result);
72 return ret; 72 return ret;
73} 73}
74 74
75 75
76static int 76static int
77test_hom_simple (unsigned int a, 77test_hom_simple(unsigned int a,
78 unsigned int b) 78 unsigned int b)
79{ 79{
80 gcry_mpi_t m1; 80 gcry_mpi_t m1;
81 gcry_mpi_t m2; 81 gcry_mpi_t m2;
@@ -88,52 +88,52 @@ test_hom_simple (unsigned int a,
88 struct GNUNET_CRYPTO_PaillierPrivateKey private_key; 88 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
89 int ret = 0; 89 int ret = 0;
90 90
91 GNUNET_CRYPTO_paillier_create (&public_key, 91 GNUNET_CRYPTO_paillier_create(&public_key,
92 &private_key); 92 &private_key);
93 93
94 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0))); 94 GNUNET_assert(NULL != (m1 = gcry_mpi_new(0)));
95 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0))); 95 GNUNET_assert(NULL != (m2 = gcry_mpi_new(0)));
96 GNUNET_assert (NULL != (result = gcry_mpi_new (0))); 96 GNUNET_assert(NULL != (result = gcry_mpi_new(0)));
97 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0))); 97 GNUNET_assert(NULL != (hom_result = gcry_mpi_new(0)));
98 m1 = gcry_mpi_set_ui (m1, a); 98 m1 = gcry_mpi_set_ui(m1, a);
99 m2 = gcry_mpi_set_ui (m2, b); 99 m2 = gcry_mpi_set_ui(m2, b);
100 gcry_mpi_add (result, 100 gcry_mpi_add(result,
101 m1, 101 m1,
102 m2); 102 m2);
103 GNUNET_CRYPTO_paillier_encrypt (&public_key, 103 GNUNET_CRYPTO_paillier_encrypt(&public_key,
104 m1, 104 m1,
105 2, 105 2,
106 &c1); 106 &c1);
107 GNUNET_CRYPTO_paillier_encrypt (&public_key, 107 GNUNET_CRYPTO_paillier_encrypt(&public_key,
108 m2, 108 m2,
109 2, 109 2,
110 &c2); 110 &c2);
111 GNUNET_CRYPTO_paillier_hom_add (&public_key, 111 GNUNET_CRYPTO_paillier_hom_add(&public_key,
112 &c1, 112 &c1,
113 &c2, 113 &c2,
114 &c_result); 114 &c_result);
115 GNUNET_CRYPTO_paillier_decrypt (&private_key, 115 GNUNET_CRYPTO_paillier_decrypt(&private_key,
116 &public_key, 116 &public_key,
117 &c_result, 117 &c_result,
118 hom_result); 118 hom_result);
119 if (0 != gcry_mpi_cmp (result, hom_result)) 119 if (0 != gcry_mpi_cmp(result, hom_result))
120 { 120 {
121 fprintf (stderr, 121 fprintf(stderr,
122 "GNUNET_CRYPTO_paillier failed simple math!\n"); 122 "GNUNET_CRYPTO_paillier failed simple math!\n");
123 gcry_log_debugmpi ("got ", hom_result); 123 gcry_log_debugmpi("got ", hom_result);
124 gcry_log_debugmpi ("wanted ", result); 124 gcry_log_debugmpi("wanted ", result);
125 ret = 1; 125 ret = 1;
126 } 126 }
127 gcry_mpi_release (m1); 127 gcry_mpi_release(m1);
128 gcry_mpi_release (m2); 128 gcry_mpi_release(m2);
129 gcry_mpi_release (result); 129 gcry_mpi_release(result);
130 gcry_mpi_release (hom_result); 130 gcry_mpi_release(hom_result);
131 return ret; 131 return ret;
132} 132}
133 133
134 134
135static int 135static int
136test_hom () 136test_hom()
137{ 137{
138 int ret; 138 int ret;
139 gcry_mpi_t m1; 139 gcry_mpi_t m1;
@@ -146,99 +146,100 @@ test_hom ()
146 struct GNUNET_CRYPTO_PaillierPublicKey public_key; 146 struct GNUNET_CRYPTO_PaillierPublicKey public_key;
147 struct GNUNET_CRYPTO_PaillierPrivateKey private_key; 147 struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
148 148
149 GNUNET_CRYPTO_paillier_create (&public_key, 149 GNUNET_CRYPTO_paillier_create(&public_key,
150 &private_key); 150 &private_key);
151 151
152 GNUNET_assert (NULL != (m1 = gcry_mpi_new (0))); 152 GNUNET_assert(NULL != (m1 = gcry_mpi_new(0)));
153 GNUNET_assert (NULL != (m2 = gcry_mpi_new (0))); 153 GNUNET_assert(NULL != (m2 = gcry_mpi_new(0)));
154 GNUNET_assert (NULL != (result = gcry_mpi_new (0))); 154 GNUNET_assert(NULL != (result = gcry_mpi_new(0)));
155 GNUNET_assert (NULL != (hom_result = gcry_mpi_new (0))); 155 GNUNET_assert(NULL != (hom_result = gcry_mpi_new(0)));
156 m1 = gcry_mpi_set_ui (m1, 1); 156 m1 = gcry_mpi_set_ui(m1, 1);
157 /* m1 = m1 * 2 ^ (GCPB - 3) */ 157 /* m1 = m1 * 2 ^ (GCPB - 3) */
158 gcry_mpi_mul_2exp (m1, 158 gcry_mpi_mul_2exp(m1,
159 m1, 159 m1,
160 GNUNET_CRYPTO_PAILLIER_BITS - 3); 160 GNUNET_CRYPTO_PAILLIER_BITS - 3);
161 m2 = gcry_mpi_set_ui (m2, 15); 161 m2 = gcry_mpi_set_ui(m2, 15);
162 /* m1 = m1 * 2 ^ (GCPB / 2) */ 162 /* m1 = m1 * 2 ^ (GCPB / 2) */
163 gcry_mpi_mul_2exp (m2, 163 gcry_mpi_mul_2exp(m2,
164 m2, 164 m2,
165 GNUNET_CRYPTO_PAILLIER_BITS / 2); 165 GNUNET_CRYPTO_PAILLIER_BITS / 2);
166 gcry_mpi_add (result, 166 gcry_mpi_add(result,
167 m1, 167 m1,
168 m2); 168 m2);
169 169
170 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key, 170 if (1 != (ret = GNUNET_CRYPTO_paillier_encrypt(&public_key,
171 m1, 171 m1,
172 2, 172 2,
173 &c1))) 173 &c1)))
174 { 174 {
175 fprintf (stderr, 175 fprintf(stderr,
176 "GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n", 176 "GNUNET_CRYPTO_paillier_encrypt 1 failed, should return 1 allowed operation, got %d!\n",
177 ret); 177 ret);
178 ret = 1; 178 ret = 1;
179 goto out; 179 goto out;
180 } 180 }
181 if (2 != (ret = GNUNET_CRYPTO_paillier_encrypt (&public_key, 181 if (2 != (ret = GNUNET_CRYPTO_paillier_encrypt(&public_key,
182 m2, 182 m2,
183 2, 183 2,
184 &c2))) 184 &c2)))
185 { 185 {
186 fprintf (stderr, 186 fprintf(stderr,
187 "GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 2 allowed operation, got %d!\n", 187 "GNUNET_CRYPTO_paillier_encrypt 2 failed, should return 2 allowed operation, got %d!\n",
188 ret); 188 ret);
189 ret = 1; 189 ret = 1;
190 goto out; 190 goto out;
191 } 191 }
192 192
193 if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add (&public_key, 193 if (0 != (ret = GNUNET_CRYPTO_paillier_hom_add(&public_key,
194 &c1, 194 &c1,
195 &c2, 195 &c2,
196 &c_result))) 196 &c_result)))
197 { 197 {
198 fprintf (stderr, 198 fprintf(stderr,
199 "GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n", 199 "GNUNET_CRYPTO_paillier_hom_add failed, expected 0 remaining operations, got %d!\n",
200 ret); 200 ret);
201 ret = 1; 201 ret = 1;
202 goto out; 202 goto out;
203 } 203 }
204 204
205 GNUNET_CRYPTO_paillier_decrypt (&private_key, 205 GNUNET_CRYPTO_paillier_decrypt(&private_key,
206 &public_key, 206 &public_key,
207 &c_result, 207 &c_result,
208 hom_result); 208 hom_result);
209 209
210 if (0 != gcry_mpi_cmp (result, hom_result)) 210 if (0 != gcry_mpi_cmp(result, hom_result))
211 { 211 {
212 fprintf (stderr, 212 fprintf(stderr,
213 "GNUNET_CRYPTO_paillier miscalculated with large numbers!\n"); 213 "GNUNET_CRYPTO_paillier miscalculated with large numbers!\n");
214 gcry_log_debugmpi ("got", hom_result); 214 gcry_log_debugmpi("got", hom_result);
215 gcry_log_debugmpi ("wanted", result); 215 gcry_log_debugmpi("wanted", result);
216 ret = 1; 216 ret = 1;
217 } 217 }
218out: 218out:
219 gcry_mpi_release (m1); 219 gcry_mpi_release(m1);
220 gcry_mpi_release (m2); 220 gcry_mpi_release(m2);
221 gcry_mpi_release (result); 221 gcry_mpi_release(result);
222 gcry_mpi_release (hom_result); 222 gcry_mpi_release(hom_result);
223 return ret; 223 return ret;
224} 224}
225 225
226 226
227int 227int
228main (int argc, 228main(int argc,
229 char *argv[]) 229 char *argv[])
230{ 230{
231 int ret; 231 int ret;
232 ret = test_crypto (); 232
233 ret = test_crypto();
233 if (0 != ret) 234 if (0 != ret)
234 return ret; 235 return ret;
235 ret = test_hom_simple (2,4); 236 ret = test_hom_simple(2, 4);
236 if (0 != ret) 237 if (0 != ret)
237 return ret; 238 return ret;
238 ret = test_hom_simple (13,17); 239 ret = test_hom_simple(13, 17);
239 if (0 != ret) 240 if (0 != ret)
240 return ret; 241 return ret;
241 ret = test_hom (); 242 ret = test_hom();
242 return ret; 243 return ret;
243} 244}
244 245