aboutsummaryrefslogtreecommitdiff
path: root/src/scalarproduct
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/scalarproduct
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/scalarproduct')
-rw-r--r--src/scalarproduct/gnunet-scalarproduct.c441
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct-ecc.h9
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct-ecc_alice.c978
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c902
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct.h12
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct_alice.c1134
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct_bob.c1110
-rw-r--r--src/scalarproduct/scalarproduct.h17
-rw-r--r--src/scalarproduct/scalarproduct_api.c405
-rw-r--r--src/scalarproduct/test_ecc_scalarproduct.c176
10 files changed, 2629 insertions, 2555 deletions
diff --git a/src/scalarproduct/gnunet-scalarproduct.c b/src/scalarproduct/gnunet-scalarproduct.c
index fa0e1ccda..76320dbba 100644
--- a/src/scalarproduct/gnunet-scalarproduct.c
+++ b/src/scalarproduct/gnunet-scalarproduct.c
@@ -33,7 +33,8 @@
33#include "gnunet_protocols.h" 33#include "gnunet_protocols.h"
34#include "scalarproduct.h" 34#include "scalarproduct.h"
35 35
36#define LOG(kind, ...) GNUNET_log_from(kind, "gnunet-scalarproduct", __VA_ARGS__) 36#define LOG(kind, ...) GNUNET_log_from (kind, "gnunet-scalarproduct", \
37 __VA_ARGS__)
37 38
38 39
39/** 40/**
@@ -79,44 +80,44 @@ static struct GNUNET_SCALARPRODUCT_ComputationHandle *computation;
79 * @param status if our job was successfully processed 80 * @param status if our job was successfully processed
80 */ 81 */
81static void 82static void
82responder_callback(void *cls, 83responder_callback (void *cls,
83 enum GNUNET_SCALARPRODUCT_ResponseStatus status) 84 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
84{ 85{
85 switch (status) 86 switch (status)
86 { 87 {
87 case GNUNET_SCALARPRODUCT_STATUS_SUCCESS: 88 case GNUNET_SCALARPRODUCT_STATUS_SUCCESS:
88 ret = 0; 89 ret = 0;
89 LOG(GNUNET_ERROR_TYPE_INFO, 90 LOG (GNUNET_ERROR_TYPE_INFO,
90 "Session %s concluded.\n", 91 "Session %s concluded.\n",
91 GNUNET_h2s(&session_key)); 92 GNUNET_h2s (&session_key));
92 break; 93 break;
93 94
94 case GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE: 95 case GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE:
95 LOG(GNUNET_ERROR_TYPE_ERROR, 96 LOG (GNUNET_ERROR_TYPE_ERROR,
96 "Session %s failed: invalid response\n", 97 "Session %s failed: invalid response\n",
97 GNUNET_h2s(&session_key)); 98 GNUNET_h2s (&session_key));
98 break; 99 break;
99 100
100 case GNUNET_SCALARPRODUCT_STATUS_FAILURE: 101 case GNUNET_SCALARPRODUCT_STATUS_FAILURE:
101 LOG(GNUNET_ERROR_TYPE_ERROR, 102 LOG (GNUNET_ERROR_TYPE_ERROR,
102 "Session %s failed: service failure\n", 103 "Session %s failed: service failure\n",
103 GNUNET_h2s(&session_key)); 104 GNUNET_h2s (&session_key));
104 break; 105 break;
105 106
106 case GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED: 107 case GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED:
107 LOG(GNUNET_ERROR_TYPE_ERROR, 108 LOG (GNUNET_ERROR_TYPE_ERROR,
108 "Session %s failed: service disconnect!\n", 109 "Session %s failed: service disconnect!\n",
109 GNUNET_h2s(&session_key)); 110 GNUNET_h2s (&session_key));
110 break; 111 break;
111 112
112 default: 113 default:
113 LOG(GNUNET_ERROR_TYPE_ERROR, 114 LOG (GNUNET_ERROR_TYPE_ERROR,
114 "Session %s failed: return code %d\n", 115 "Session %s failed: return code %d\n",
115 GNUNET_h2s(&session_key), 116 GNUNET_h2s (&session_key),
116 status); 117 status);
117 } 118 }
118 computation = NULL; 119 computation = NULL;
119 GNUNET_SCHEDULER_shutdown(); 120 GNUNET_SCHEDULER_shutdown ();
120} 121}
121 122
122 123
@@ -128,60 +129,60 @@ responder_callback(void *cls,
128 * @param result the result in gnu/gcry MPI format 129 * @param result the result in gnu/gcry MPI format
129 */ 130 */
130static void 131static void
131requester_callback(void *cls, 132requester_callback (void *cls,
132 enum GNUNET_SCALARPRODUCT_ResponseStatus status, 133 enum GNUNET_SCALARPRODUCT_ResponseStatus status,
133 gcry_mpi_t result) 134 gcry_mpi_t result)
134{ 135{
135 unsigned char *buf; 136 unsigned char *buf;
136 gcry_error_t rc; 137 gcry_error_t rc;
137 138
138 switch (status) 139 switch (status)
140 {
141 case GNUNET_SCALARPRODUCT_STATUS_SUCCESS:
142 if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
139 { 143 {
140 case GNUNET_SCALARPRODUCT_STATUS_SUCCESS: 144 ret = 0;
141 if (0 == (rc = gcry_mpi_aprint(GCRYMPI_FMT_HEX, &buf, NULL, result))) 145 fprintf (stdout,
142 { 146 "%s\n",
143 ret = 0; 147 buf);
144 fprintf(stdout, 148 fflush (stdout);
145 "%s\n",
146 buf);
147 fflush(stdout);
148 }
149 else
150 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR,
151 "gcry_mpi_aprint",
152 rc);
153 break;
154
155 case GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE:
156 LOG(GNUNET_ERROR_TYPE_ERROR,
157 "Session %s with peer %s failed: invalid response received\n",
158 GNUNET_h2s(&session_key),
159 GNUNET_i2s(&peer_id));
160 break;
161
162 case GNUNET_SCALARPRODUCT_STATUS_FAILURE:
163 LOG(GNUNET_ERROR_TYPE_ERROR,
164 "Session %s with peer %s failed: API failure\n",
165 GNUNET_h2s(&session_key),
166 GNUNET_i2s(&peer_id));
167 break;
168
169 case GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED:
170 LOG(GNUNET_ERROR_TYPE_ERROR,
171 "Session %s with peer %s was disconnected from service.\n",
172 GNUNET_h2s(&session_key),
173 GNUNET_i2s(&peer_id));
174 break;
175
176 default:
177 LOG(GNUNET_ERROR_TYPE_ERROR,
178 "Session %s with peer %s failed: return code %d\n",
179 GNUNET_h2s(&session_key),
180 GNUNET_i2s(&peer_id),
181 status);
182 } 149 }
150 else
151 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
152 "gcry_mpi_aprint",
153 rc);
154 break;
155
156 case GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE:
157 LOG (GNUNET_ERROR_TYPE_ERROR,
158 "Session %s with peer %s failed: invalid response received\n",
159 GNUNET_h2s (&session_key),
160 GNUNET_i2s (&peer_id));
161 break;
162
163 case GNUNET_SCALARPRODUCT_STATUS_FAILURE:
164 LOG (GNUNET_ERROR_TYPE_ERROR,
165 "Session %s with peer %s failed: API failure\n",
166 GNUNET_h2s (&session_key),
167 GNUNET_i2s (&peer_id));
168 break;
169
170 case GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED:
171 LOG (GNUNET_ERROR_TYPE_ERROR,
172 "Session %s with peer %s was disconnected from service.\n",
173 GNUNET_h2s (&session_key),
174 GNUNET_i2s (&peer_id));
175 break;
176
177 default:
178 LOG (GNUNET_ERROR_TYPE_ERROR,
179 "Session %s with peer %s failed: return code %d\n",
180 GNUNET_h2s (&session_key),
181 GNUNET_i2s (&peer_id),
182 status);
183 }
183 computation = NULL; 184 computation = NULL;
184 GNUNET_SCHEDULER_shutdown(); 185 GNUNET_SCHEDULER_shutdown ();
185} 186}
186 187
187 188
@@ -192,13 +193,13 @@ requester_callback(void *cls,
192 * @param tc unused 193 * @param tc unused
193 */ 194 */
194static void 195static void
195shutdown_task(void *cls) 196shutdown_task (void *cls)
196{ 197{
197 if (NULL != computation) 198 if (NULL != computation)
198 { 199 {
199 GNUNET_SCALARPRODUCT_cancel(computation); 200 GNUNET_SCALARPRODUCT_cancel (computation);
200 ret = 1; /* aborted */ 201 ret = 1; /* aborted */
201 } 202 }
202} 203}
203 204
204 205
@@ -211,10 +212,10 @@ shutdown_task(void *cls)
211 * @param cfg configuration 212 * @param cfg configuration
212 */ 213 */
213static void 214static void
214run(void *cls, 215run (void *cls,
215 char *const *args, 216 char *const *args,
216 const char *cfgfile, 217 const char *cfgfile,
217 const struct GNUNET_CONFIGURATION_Handle *cfg) 218 const struct GNUNET_CONFIGURATION_Handle *cfg)
218{ 219{
219 char *begin = input_elements; 220 char *begin = input_elements;
220 char *end; 221 char *end;
@@ -223,120 +224,125 @@ run(void *cls,
223 uint32_t element_count = 0; 224 uint32_t element_count = 0;
224 225
225 if (NULL == input_elements) 226 if (NULL == input_elements)
226 { 227 {
227 LOG(GNUNET_ERROR_TYPE_ERROR, 228 LOG (GNUNET_ERROR_TYPE_ERROR,
228 _("You must specify at least one message ID to check!\n")); 229 _ ("You must specify at least one message ID to check!\n"));
229 return; 230 return;
230 } 231 }
231 if ((NULL == input_session_key) || 232 if ((NULL == input_session_key) ||
232 (0 == strlen(input_session_key))) 233 (0 == strlen (input_session_key)))
233 { 234 {
234 LOG(GNUNET_ERROR_TYPE_ERROR, 235 LOG (GNUNET_ERROR_TYPE_ERROR,
235 _("This program needs a session identifier for comparing vectors.\n")); 236 _ (
236 return; 237 "This program needs a session identifier for comparing vectors.\n"));
237 } 238 return;
238 GNUNET_CRYPTO_hash(input_session_key, 239 }
239 strlen(input_session_key), 240 GNUNET_CRYPTO_hash (input_session_key,
240 &session_key); 241 strlen (input_session_key),
242 &session_key);
241 if ((NULL != input_peer_id) && 243 if ((NULL != input_peer_id) &&
242 (GNUNET_OK != 244 (GNUNET_OK !=
243 GNUNET_CRYPTO_eddsa_public_key_from_string(input_peer_id, 245 GNUNET_CRYPTO_eddsa_public_key_from_string (input_peer_id,
244 strlen(input_peer_id), 246 strlen (input_peer_id),
245 &peer_id.public_key))) 247 &peer_id.public_key)))
246 { 248 {
247 LOG(GNUNET_ERROR_TYPE_ERROR, 249 LOG (GNUNET_ERROR_TYPE_ERROR,
248 _("Tried to set initiator mode, as peer ID was given. " 250 _ ("Tried to set initiator mode, as peer ID was given. "
249 "However, `%s' is not a valid peer identifier.\n"), 251 "However, `%s' is not a valid peer identifier.\n"),
250 input_peer_id); 252 input_peer_id);
251 return; 253 return;
252 } 254 }
253 if (('\'' == *begin) && 255 if (('\'' == *begin) &&
254 ('\'' == begin[strlen(begin) - 1])) 256 ('\'' == begin[strlen (begin) - 1]))
255 { 257 {
256 begin[strlen(begin) - 1] = '\0'; 258 begin[strlen (begin) - 1] = '\0';
257 if (strlen(begin) > 0) 259 if (strlen (begin) > 0)
258 begin++; 260 begin++;
259 } 261 }
260 for (end = begin; 0 != *end; end++) 262 for (end = begin; 0 != *end; end++)
261 if (*end == ';') 263 if (*end == ';')
262 element_count++; 264 element_count++;
263 if (0 == element_count) 265 if (0 == element_count)
264 { 266 {
265 LOG(GNUNET_ERROR_TYPE_ERROR, 267 LOG (GNUNET_ERROR_TYPE_ERROR,
266 _("Need elements to compute the scalarproduct, got none.\n")); 268 _ ("Need elements to compute the scalarproduct, got none.\n"));
267 return; 269 return;
268 } 270 }
269 271
270 elements = GNUNET_malloc(sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count); 272 elements = GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element)
273 * element_count);
271 274
272 for (i = 0; i < element_count; i++) 275 for (i = 0; i < element_count; i++)
276 {
277 struct GNUNET_SCALARPRODUCT_Element element;
278 char*separator = NULL;
279
280 /* get the length of the current key,value; tupel */
281 for (end = begin; *end != ';'; end++)
282 if (*end == ',')
283 separator = end;
284
285 /* final element */
286 if ((NULL == separator) ||
287 (begin == separator) ||
288 (separator == end - 1))
289 {
290 LOG (GNUNET_ERROR_TYPE_ERROR,
291 _ ("Malformed input, could not parse `%s'\n"),
292 begin);
293 GNUNET_free (elements);
294 return;
295 }
296 *separator = 0;
297 /* read the element's key */
298 GNUNET_CRYPTO_hash (begin,
299 strlen (begin),
300 &element.key);
301
302 /* read the element's value */
303 if (1 !=
304 sscanf (separator + 1,
305 "%" SCNd64 ";",
306 &element.value))
273 { 307 {
274 struct GNUNET_SCALARPRODUCT_Element element; 308 LOG (GNUNET_ERROR_TYPE_ERROR,
275 char* separator = NULL; 309 _ ("Could not convert `%s' to int64_t.\n"),
276 310 begin);
277 /* get the length of the current key,value; tupel */ 311 GNUNET_free (elements);
278 for (end = begin; *end != ';'; end++) 312 return;
279 if (*end == ',')
280 separator = end;
281
282 /* final element */
283 if ((NULL == separator) ||
284 (begin == separator) ||
285 (separator == end - 1))
286 {
287 LOG(GNUNET_ERROR_TYPE_ERROR,
288 _("Malformed input, could not parse `%s'\n"),
289 begin);
290 GNUNET_free(elements);
291 return;
292 }
293 *separator = 0;
294 /* read the element's key */
295 GNUNET_CRYPTO_hash(begin,
296 strlen(begin),
297 &element.key);
298
299 /* read the element's value */
300 if (1 !=
301 sscanf(separator + 1,
302 "%" SCNd64 ";",
303 &element.value))
304 {
305 LOG(GNUNET_ERROR_TYPE_ERROR,
306 _("Could not convert `%s' to int64_t.\n"),
307 begin);
308 GNUNET_free(elements);
309 return;
310 }
311 element.value = GNUNET_htonll(element.value);
312 elements[i] = element;
313 begin = end + 1;
314 } 313 }
314 element.value = GNUNET_htonll (element.value);
315 elements[i] = element;
316 begin = end + 1;
317 }
315 318
316 if (((NULL != input_peer_id) && 319 if (((NULL != input_peer_id) &&
317 (NULL == (computation 320 (NULL == (computation
318 = GNUNET_SCALARPRODUCT_start_computation(cfg, 321 = GNUNET_SCALARPRODUCT_start_computation (cfg,
319 &session_key, 322 &session_key,
320 &peer_id, 323 &peer_id,
321 elements, element_count, 324 elements,
322 &requester_callback, 325 element_count,
323 NULL)))) || 326 &requester_callback,
327 NULL)))) ||
324 ((NULL == input_peer_id) && 328 ((NULL == input_peer_id) &&
325 (NULL == (computation 329 (NULL == (computation
326 = GNUNET_SCALARPRODUCT_accept_computation(cfg, 330 = GNUNET_SCALARPRODUCT_accept_computation (cfg,
327 &session_key, 331 &session_key,
328 elements, element_count, 332 elements,
329 &responder_callback, 333 element_count,
330 NULL))))) 334 &
331 { 335 responder_callback,
332 fprintf(stderr, 336 NULL)))))
333 _("Failed to initiate computation, were all keys unique?\n")); 337 {
334 GNUNET_free(elements); 338 fprintf (stderr,
335 return; 339 _ ("Failed to initiate computation, were all keys unique?\n"));
336 } 340 GNUNET_free (elements);
337 GNUNET_free(elements); 341 return;
338 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, 342 }
339 NULL); 343 GNUNET_free (elements);
344 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
345 NULL);
340 ret = 0; 346 ret = 0;
341} 347}
342 348
@@ -349,42 +355,47 @@ run(void *cls,
349 * @return 0 ok, 1 on error 355 * @return 0 ok, 1 on error
350 */ 356 */
351int 357int
352main(int argc, char *const *argv) 358main (int argc, char *const *argv)
353{ 359{
354 struct GNUNET_GETOPT_CommandLineOption options[] = { 360 struct GNUNET_GETOPT_CommandLineOption options[] = {
355 GNUNET_GETOPT_option_string('e', 361 GNUNET_GETOPT_option_string ('e',
356 "elements", 362 "elements",
357 "\"key1,val1;key2,val2;...,keyn,valn;\"", 363 "\"key1,val1;key2,val2;...,keyn,valn;\"",
358 gettext_noop("A comma separated list of elements to compare as vector with our remote peer."), 364 gettext_noop (
359 &input_elements), 365 "A comma separated list of elements to compare as vector with our remote peer."),
360 366 &input_elements),
361 GNUNET_GETOPT_option_string('e', 367
362 "elements", 368 GNUNET_GETOPT_option_string ('e',
363 "\"key1,val1;key2,val2;...,keyn,valn;\"", 369 "elements",
364 gettext_noop("A comma separated list of elements to compare as vector with our remote peer."), 370 "\"key1,val1;key2,val2;...,keyn,valn;\"",
365 &input_elements), 371 gettext_noop (
366 372 "A comma separated list of elements to compare as vector with our remote peer."),
367 GNUNET_GETOPT_option_string('p', 373 &input_elements),
368 "peer", 374
369 "PEERID", 375 GNUNET_GETOPT_option_string ('p',
370 gettext_noop("[Optional] peer to calculate our scalarproduct with. If this parameter is not given, the service will wait for a remote peer to compute the request."), 376 "peer",
371 &input_peer_id), 377 "PEERID",
372 378 gettext_noop (
373 GNUNET_GETOPT_option_string('k', 379 "[Optional] peer to calculate our scalarproduct with. If this parameter is not given, the service will wait for a remote peer to compute the request."),
374 "key", 380 &input_peer_id),
375 "TRANSACTION_ID", 381
376 gettext_noop("Transaction ID shared with peer."), 382 GNUNET_GETOPT_option_string ('k',
377 &input_session_key), 383 "key",
384 "TRANSACTION_ID",
385 gettext_noop (
386 "Transaction ID shared with peer."),
387 &input_session_key),
378 388
379 GNUNET_GETOPT_OPTION_END 389 GNUNET_GETOPT_OPTION_END
380 }; 390 };
381 391
382 return (GNUNET_OK == 392 return (GNUNET_OK ==
383 GNUNET_PROGRAM_run(argc, 393 GNUNET_PROGRAM_run (argc,
384 argv, 394 argv,
385 "gnunet-scalarproduct", 395 "gnunet-scalarproduct",
386 gettext_noop("Calculate the Vectorproduct with a GNUnet peer."), 396 gettext_noop (
387 options, &run, NULL)) ? ret : 1; 397 "Calculate the Vectorproduct with a GNUnet peer."),
398 options, &run, NULL)) ? ret : 1;
388} 399}
389 400
390/* end of gnunet-scalarproduct.c */ 401/* end of gnunet-scalarproduct.c */
diff --git a/src/scalarproduct/gnunet-service-scalarproduct-ecc.h b/src/scalarproduct/gnunet-service-scalarproduct-ecc.h
index 8dbf042b9..5c7a8d8f1 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct-ecc.h
+++ b/src/scalarproduct/gnunet-service-scalarproduct-ecc.h
@@ -37,7 +37,8 @@ GNUNET_NETWORK_STRUCT_BEGIN
37 * send a `struct AliceCryptodataMessage *`. Bob is not expected 37 * send a `struct AliceCryptodataMessage *`. Bob is not expected
38 * to respond via CADET in the meantime. 38 * to respond via CADET in the meantime.
39 */ 39 */
40struct EccServiceRequestMessage { 40struct EccServiceRequestMessage
41{
41 /** 42 /**
42 * Type is #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION 43 * Type is #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION
43 */ 44 */
@@ -60,7 +61,8 @@ struct EccServiceRequestMessage {
60 * (after set intersection). Alice may send messages of this 61 * (after set intersection). Alice may send messages of this
61 * type repeatedly to transmit all values. 62 * type repeatedly to transmit all values.
62 */ 63 */
63struct EccAliceCryptodataMessage { 64struct EccAliceCryptodataMessage
65{
64 /** 66 /**
65 * Type is #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA 67 * Type is #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA
66 */ 68 */
@@ -84,7 +86,8 @@ struct EccAliceCryptodataMessage {
84 * conversation may be continued with `struct BobCryptodataMultipartMessage` 86 * conversation may be continued with `struct BobCryptodataMultipartMessage`
85 * messages afterwards. 87 * messages afterwards.
86 */ 88 */
87struct EccBobCryptodataMessage { 89struct EccBobCryptodataMessage
90{
88 /** 91 /**
89 * GNUNET message header with type 92 * GNUNET message header with type
90 * #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA. 93 * #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA.
diff --git a/src/scalarproduct/gnunet-service-scalarproduct-ecc_alice.c b/src/scalarproduct/gnunet-service-scalarproduct-ecc_alice.c
index 9671c46f6..20ab292cf 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct-ecc_alice.c
+++ b/src/scalarproduct/gnunet-service-scalarproduct-ecc_alice.c
@@ -37,7 +37,7 @@
37#include "gnunet-service-scalarproduct-ecc.h" 37#include "gnunet-service-scalarproduct-ecc.h"
38 38
39#define LOG(kind, ...) \ 39#define LOG(kind, ...) \
40 GNUNET_log_from(kind, "scalarproduct-alice", __VA_ARGS__) 40 GNUNET_log_from (kind, "scalarproduct-alice", __VA_ARGS__)
41 41
42/** 42/**
43 * Maximum allowed result value for the scalarproduct computation. 43 * Maximum allowed result value for the scalarproduct computation.
@@ -57,7 +57,8 @@
57/** 57/**
58 * An encrypted element key-value pair. 58 * An encrypted element key-value pair.
59 */ 59 */
60struct MpiElement { 60struct MpiElement
61{
61 /** 62 /**
62 * Key used to identify matching pairs of values to multiply. 63 * Key used to identify matching pairs of values to multiply.
63 * Points into an existing data structure, to avoid copying 64 * Points into an existing data structure, to avoid copying
@@ -76,7 +77,8 @@ struct MpiElement {
76 * A scalarproduct session which tracks 77 * A scalarproduct session which tracks
77 * a request form the client to our final response. 78 * a request form the client to our final response.
78 */ 79 */
79struct AliceServiceSession { 80struct AliceServiceSession
81{
80 /** 82 /**
81 * (hopefully) unique transaction ID 83 * (hopefully) unique transaction ID
82 */ 84 */
@@ -210,11 +212,11 @@ static struct GNUNET_CADET_Handle *my_cadet;
210 * @return #GNUNET_OK (continue to iterate) 212 * @return #GNUNET_OK (continue to iterate)
211 */ 213 */
212static int 214static int
213free_element_cb(void *cls, const struct GNUNET_HashCode *key, void *value) 215free_element_cb (void *cls, const struct GNUNET_HashCode *key, void *value)
214{ 216{
215 struct GNUNET_SCALARPRODUCT_Element *e = value; 217 struct GNUNET_SCALARPRODUCT_Element *e = value;
216 218
217 GNUNET_free(e); 219 GNUNET_free (e);
218 return GNUNET_OK; 220 return GNUNET_OK;
219} 221}
220 222
@@ -225,7 +227,7 @@ free_element_cb(void *cls, const struct GNUNET_HashCode *key, void *value)
225 * @param s the session to free elements from 227 * @param s the session to free elements from
226 */ 228 */
227static void 229static void
228destroy_service_session(struct AliceServiceSession *s) 230destroy_service_session (struct AliceServiceSession *s)
229{ 231{
230 unsigned int i; 232 unsigned int i;
231 233
@@ -233,54 +235,54 @@ destroy_service_session(struct AliceServiceSession *s)
233 return; 235 return;
234 s->in_destroy = GNUNET_YES; 236 s->in_destroy = GNUNET_YES;
235 if (NULL != s->client) 237 if (NULL != s->client)
236 { 238 {
237 struct GNUNET_SERVICE_Client *c = s->client; 239 struct GNUNET_SERVICE_Client *c = s->client;
238 240
239 s->client = NULL; 241 s->client = NULL;
240 GNUNET_SERVICE_client_drop(c); 242 GNUNET_SERVICE_client_drop (c);
241 } 243 }
242 if (NULL != s->channel) 244 if (NULL != s->channel)
243 { 245 {
244 GNUNET_CADET_channel_destroy(s->channel); 246 GNUNET_CADET_channel_destroy (s->channel);
245 s->channel = NULL; 247 s->channel = NULL;
246 } 248 }
247 if (NULL != s->intersected_elements) 249 if (NULL != s->intersected_elements)
248 { 250 {
249 GNUNET_CONTAINER_multihashmap_iterate(s->intersected_elements, 251 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
250 &free_element_cb, 252 &free_element_cb,
251 s); 253 s);
252 GNUNET_CONTAINER_multihashmap_destroy(s->intersected_elements); 254 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
253 s->intersected_elements = NULL; 255 s->intersected_elements = NULL;
254 } 256 }
255 if (NULL != s->intersection_listen) 257 if (NULL != s->intersection_listen)
256 { 258 {
257 GNUNET_SET_listen_cancel(s->intersection_listen); 259 GNUNET_SET_listen_cancel (s->intersection_listen);
258 s->intersection_listen = NULL; 260 s->intersection_listen = NULL;
259 } 261 }
260 if (NULL != s->intersection_op) 262 if (NULL != s->intersection_op)
261 { 263 {
262 LOG(GNUNET_ERROR_TYPE_DEBUG, "Set intersection, op still ongoing!\n"); 264 LOG (GNUNET_ERROR_TYPE_DEBUG, "Set intersection, op still ongoing!\n");
263 GNUNET_SET_operation_cancel(s->intersection_op); 265 GNUNET_SET_operation_cancel (s->intersection_op);
264 s->intersection_op = NULL; 266 s->intersection_op = NULL;
265 } 267 }
266 if (NULL != s->intersection_set) 268 if (NULL != s->intersection_set)
267 { 269 {
268 GNUNET_SET_destroy(s->intersection_set); 270 GNUNET_SET_destroy (s->intersection_set);
269 s->intersection_set = NULL; 271 s->intersection_set = NULL;
270 } 272 }
271 if (NULL != s->sorted_elements) 273 if (NULL != s->sorted_elements)
272 { 274 {
273 for (i = 0; i < s->used_element_count; i++) 275 for (i = 0; i < s->used_element_count; i++)
274 gcry_mpi_release(s->sorted_elements[i].value); 276 gcry_mpi_release (s->sorted_elements[i].value);
275 GNUNET_free(s->sorted_elements); 277 GNUNET_free (s->sorted_elements);
276 s->sorted_elements = NULL; 278 s->sorted_elements = NULL;
277 } 279 }
278 if (NULL != s->product) 280 if (NULL != s->product)
279 { 281 {
280 gcry_mpi_release(s->product); 282 gcry_mpi_release (s->product);
281 s->product = NULL; 283 s->product = NULL;
282 } 284 }
283 GNUNET_free(s); 285 GNUNET_free (s);
284} 286}
285 287
286 288
@@ -291,22 +293,22 @@ destroy_service_session(struct AliceServiceSession *s)
291 * @param session the associated client session to fail or succeed 293 * @param session the associated client session to fail or succeed
292 */ 294 */
293static void 295static void
294prepare_client_end_notification(struct AliceServiceSession *session) 296prepare_client_end_notification (struct AliceServiceSession *session)
295{ 297{
296 struct ClientResponseMessage *msg; 298 struct ClientResponseMessage *msg;
297 struct GNUNET_MQ_Envelope *e; 299 struct GNUNET_MQ_Envelope *e;
298 300
299 if (NULL == session->client_mq) 301 if (NULL == session->client_mq)
300 return; /* no client left to be notified */ 302 return; /* no client left to be notified */
301 GNUNET_log( 303 GNUNET_log (
302 GNUNET_ERROR_TYPE_DEBUG, 304 GNUNET_ERROR_TYPE_DEBUG,
303 "Sending session-end notification with status %d to client for session %s\n", 305 "Sending session-end notification with status %d to client for session %s\n",
304 session->status, 306 session->status,
305 GNUNET_h2s(&session->session_id)); 307 GNUNET_h2s (&session->session_id));
306 e = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT); 308 e = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
307 msg->product_length = htonl(0); 309 msg->product_length = htonl (0);
308 msg->status = htonl(session->status); 310 msg->status = htonl (session->status);
309 GNUNET_MQ_send(session->client_mq, e); 311 GNUNET_MQ_send (session->client_mq, e);
310} 312}
311 313
312 314
@@ -317,7 +319,7 @@ prepare_client_end_notification(struct AliceServiceSession *session)
317 * @param s the session associated with our client. 319 * @param s the session associated with our client.
318 */ 320 */
319static void 321static void
320transmit_client_response(struct AliceServiceSession *s) 322transmit_client_response (struct AliceServiceSession *s)
321{ 323{
322 struct ClientResponseMessage *msg; 324 struct ClientResponseMessage *msg;
323 struct GNUNET_MQ_Envelope *e; 325 struct GNUNET_MQ_Envelope *e;
@@ -329,56 +331,56 @@ transmit_client_response(struct AliceServiceSession *s)
329 gcry_mpi_t value; 331 gcry_mpi_t value;
330 332
331 if (NULL == s->product) 333 if (NULL == s->product)
332 { 334 {
333 GNUNET_break(0); 335 GNUNET_break (0);
334 prepare_client_end_notification(s); 336 prepare_client_end_notification (s);
335 return; 337 return;
336 } 338 }
337 value = gcry_mpi_new(0); 339 value = gcry_mpi_new (0);
338 sign = gcry_mpi_cmp_ui(s->product, 0); 340 sign = gcry_mpi_cmp_ui (s->product, 0);
339 if (0 > sign) 341 if (0 > sign)
340 { 342 {
341 range = -1; 343 range = -1;
342 gcry_mpi_sub(value, value, s->product); 344 gcry_mpi_sub (value, value, s->product);
343 } 345 }
344 else if (0 < sign) 346 else if (0 < sign)
345 { 347 {
346 range = 1; 348 range = 1;
347 gcry_mpi_add(value, value, s->product); 349 gcry_mpi_add (value, value, s->product);
348 } 350 }
349 else 351 else
350 { 352 {
351 /* result is exactly zero */ 353 /* result is exactly zero */
352 range = 0; 354 range = 0;
353 } 355 }
354 gcry_mpi_release(s->product); 356 gcry_mpi_release (s->product);
355 s->product = NULL; 357 s->product = NULL;
356 358
357 if ((0 != range) && (0 != (rc = gcry_mpi_aprint(GCRYMPI_FMT_STD, 359 if ((0 != range) && (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD,
358 &product_exported, 360 &product_exported,
359 &product_length, 361 &product_length,
360 value)))) 362 value))))
361 { 363 {
362 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 364 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
363 prepare_client_end_notification(s); 365 prepare_client_end_notification (s);
364 return; 366 return;
365 } 367 }
366 gcry_mpi_release(value); 368 gcry_mpi_release (value);
367 e = GNUNET_MQ_msg_extra(msg, 369 e = GNUNET_MQ_msg_extra (msg,
368 product_length, 370 product_length,
369 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT); 371 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
370 msg->status = htonl(GNUNET_SCALARPRODUCT_STATUS_SUCCESS); 372 msg->status = htonl (GNUNET_SCALARPRODUCT_STATUS_SUCCESS);
371 msg->range = htonl(range); 373 msg->range = htonl (range);
372 msg->product_length = htonl(product_length); 374 msg->product_length = htonl (product_length);
373 if (NULL != product_exported) 375 if (NULL != product_exported)
374 { 376 {
375 GNUNET_memcpy(&msg[1], product_exported, product_length); 377 GNUNET_memcpy (&msg[1], product_exported, product_length);
376 GNUNET_free(product_exported); 378 GNUNET_free (product_exported);
377 } 379 }
378 GNUNET_MQ_send(s->client_mq, e); 380 GNUNET_MQ_send (s->client_mq, e);
379 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Sent result to client, session %s has ended!\n", 382 "Sent result to client, session %s has ended!\n",
381 GNUNET_h2s(&s->session_id)); 383 GNUNET_h2s (&s->session_id));
382} 384}
383 385
384 386
@@ -392,21 +394,21 @@ transmit_client_response(struct AliceServiceSession *s)
392 * @param channel connection to the other end (henceforth invalid) 394 * @param channel connection to the other end (henceforth invalid)
393 */ 395 */
394static void 396static void
395cb_channel_destruction(void *cls, const struct GNUNET_CADET_Channel *channel) 397cb_channel_destruction (void *cls, const struct GNUNET_CADET_Channel *channel)
396{ 398{
397 struct AliceServiceSession *s = cls; 399 struct AliceServiceSession *s = cls;
398 400
399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Peer disconnected, terminating session %s with peer %s\n", 402 "Peer disconnected, terminating session %s with peer %s\n",
401 GNUNET_h2s(&s->session_id), 403 GNUNET_h2s (&s->session_id),
402 GNUNET_i2s(&s->peer)); 404 GNUNET_i2s (&s->peer));
403 s->channel = NULL; 405 s->channel = NULL;
404 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status) 406 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
405 { 407 {
406 /* We didn't get an answer yet, fail with error */ 408 /* We didn't get an answer yet, fail with error */
407 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 409 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
408 prepare_client_end_notification(s); 410 prepare_client_end_notification (s);
409 } 411 }
410} 412}
411 413
412 414
@@ -419,9 +421,9 @@ cb_channel_destruction(void *cls, const struct GNUNET_CADET_Channel *channel)
419 * @return product as MPI, never NULL 421 * @return product as MPI, never NULL
420 */ 422 */
421static gcry_mpi_t 423static gcry_mpi_t
422compute_scalar_product(struct AliceServiceSession *session, 424compute_scalar_product (struct AliceServiceSession *session,
423 gcry_mpi_point_t prod_g_i_b_i, 425 gcry_mpi_point_t prod_g_i_b_i,
424 gcry_mpi_point_t prod_h_i_b_i) 426 gcry_mpi_point_t prod_h_i_b_i)
425{ 427{
426 gcry_mpi_point_t g_i_b_i_a_inv; 428 gcry_mpi_point_t g_i_b_i_a_inv;
427 gcry_mpi_point_t g_ai_bi; 429 gcry_mpi_point_t g_ai_bi;
@@ -429,28 +431,28 @@ compute_scalar_product(struct AliceServiceSession *session,
429 gcry_mpi_t ret; 431 gcry_mpi_t ret;
430 432
431 g_i_b_i_a_inv = 433 g_i_b_i_a_inv =
432 GNUNET_CRYPTO_ecc_pmul_mpi(edc, prod_g_i_b_i, my_privkey_inv); 434 GNUNET_CRYPTO_ecc_pmul_mpi (edc, prod_g_i_b_i, my_privkey_inv);
433 g_ai_bi = GNUNET_CRYPTO_ecc_add(edc, g_i_b_i_a_inv, prod_h_i_b_i); 435 g_ai_bi = GNUNET_CRYPTO_ecc_add (edc, g_i_b_i_a_inv, prod_h_i_b_i);
434 gcry_mpi_point_release(g_i_b_i_a_inv); 436 gcry_mpi_point_release (g_i_b_i_a_inv);
435 ai_bi = GNUNET_CRYPTO_ecc_dlog(edc, g_ai_bi); 437 ai_bi = GNUNET_CRYPTO_ecc_dlog (edc, g_ai_bi);
436 gcry_mpi_point_release(g_ai_bi); 438 gcry_mpi_point_release (g_ai_bi);
437 if (INT_MAX == ai_bi) 439 if (INT_MAX == ai_bi)
438 { 440 {
439 /* result too big */ 441 /* result too big */
440 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
441 "Scalar product result out of range\n"); 443 "Scalar product result out of range\n");
442 return NULL; 444 return NULL;
443 } 445 }
444 ret = gcry_mpi_new(0); 446 ret = gcry_mpi_new (0);
445 if (ai_bi > 0) 447 if (ai_bi > 0)
446 { 448 {
447 gcry_mpi_set_ui(ret, ai_bi); 449 gcry_mpi_set_ui (ret, ai_bi);
448 } 450 }
449 else 451 else
450 { 452 {
451 gcry_mpi_set_ui(ret, -ai_bi); 453 gcry_mpi_set_ui (ret, -ai_bi);
452 gcry_mpi_neg(ret, ret); 454 gcry_mpi_neg (ret, ret);
453 } 455 }
454 return ret; 456 return ret;
455} 457}
456 458
@@ -463,46 +465,46 @@ compute_scalar_product(struct AliceServiceSession *session,
463 * @param msg the actual message 465 * @param msg the actual message
464 */ 466 */
465static void 467static void
466handle_bobs_cryptodata_message(void *cls, 468handle_bobs_cryptodata_message (void *cls,
467 const struct EccBobCryptodataMessage *msg) 469 const struct EccBobCryptodataMessage *msg)
468{ 470{
469 struct AliceServiceSession *s = cls; 471 struct AliceServiceSession *s = cls;
470 gcry_mpi_point_t prod_g_i_b_i; 472 gcry_mpi_point_t prod_g_i_b_i;
471 gcry_mpi_point_t prod_h_i_b_i; 473 gcry_mpi_point_t prod_h_i_b_i;
472 uint32_t contained; 474 uint32_t contained;
473 475
474 contained = ntohl(msg->contained_element_count); 476 contained = ntohl (msg->contained_element_count);
475 if (2 != contained) 477 if (2 != contained)
476 { 478 {
477 GNUNET_break_op(0); 479 GNUNET_break_op (0);
478 destroy_service_session(s); 480 destroy_service_session (s);
479 return; 481 return;
480 } 482 }
481 if (NULL == s->sorted_elements) 483 if (NULL == s->sorted_elements)
482 { 484 {
483 /* we're not ready yet, how can Bob be? */ 485 /* we're not ready yet, how can Bob be? */
484 GNUNET_break_op(0); 486 GNUNET_break_op (0);
485 destroy_service_session(s); 487 destroy_service_session (s);
486 return; 488 return;
487 } 489 }
488 if (s->total != s->client_received_element_count) 490 if (s->total != s->client_received_element_count)
489 { 491 {
490 /* we're not ready yet, how can Bob be? */ 492 /* we're not ready yet, how can Bob be? */
491 GNUNET_break_op(0); 493 GNUNET_break_op (0);
492 destroy_service_session(s); 494 destroy_service_session (s);
493 return; 495 return;
494 } 496 }
495 497
496 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "Received %u crypto values from Bob\n", 499 "Received %u crypto values from Bob\n",
498 (unsigned int)contained); 500 (unsigned int) contained);
499 GNUNET_CADET_receive_done(s->channel); 501 GNUNET_CADET_receive_done (s->channel);
500 prod_g_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point(edc, &msg->prod_g_i_b_i); 502 prod_g_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point (edc, &msg->prod_g_i_b_i);
501 prod_h_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point(edc, &msg->prod_h_i_b_i); 503 prod_h_i_b_i = GNUNET_CRYPTO_ecc_bin_to_point (edc, &msg->prod_h_i_b_i);
502 s->product = compute_scalar_product(s, prod_g_i_b_i, prod_h_i_b_i); 504 s->product = compute_scalar_product (s, prod_g_i_b_i, prod_h_i_b_i);
503 gcry_mpi_point_release(prod_g_i_b_i); 505 gcry_mpi_point_release (prod_g_i_b_i);
504 gcry_mpi_point_release(prod_h_i_b_i); 506 gcry_mpi_point_release (prod_h_i_b_i);
505 transmit_client_response(s); 507 transmit_client_response (s);
506} 508}
507 509
508 510
@@ -515,19 +517,19 @@ handle_bobs_cryptodata_message(void *cls,
515 * @param value the `struct GNUNET_SCALARPRODUCT_Element *` 517 * @param value the `struct GNUNET_SCALARPRODUCT_Element *`
516 */ 518 */
517static int 519static int
518copy_element_cb(void *cls, const struct GNUNET_HashCode *key, void *value) 520copy_element_cb (void *cls, const struct GNUNET_HashCode *key, void *value)
519{ 521{
520 struct AliceServiceSession *s = cls; 522 struct AliceServiceSession *s = cls;
521 struct GNUNET_SCALARPRODUCT_Element *e = value; 523 struct GNUNET_SCALARPRODUCT_Element *e = value;
522 gcry_mpi_t mval; 524 gcry_mpi_t mval;
523 int64_t val; 525 int64_t val;
524 526
525 mval = gcry_mpi_new(0); 527 mval = gcry_mpi_new (0);
526 val = (int64_t)GNUNET_ntohll(e->value); 528 val = (int64_t) GNUNET_ntohll (e->value);
527 if (0 > val) 529 if (0 > val)
528 gcry_mpi_sub_ui(mval, mval, -val); 530 gcry_mpi_sub_ui (mval, mval, -val);
529 else 531 else
530 gcry_mpi_add_ui(mval, mval, val); 532 gcry_mpi_add_ui (mval, mval, val);
531 s->sorted_elements[s->used_element_count].value = mval; 533 s->sorted_elements[s->used_element_count].value = mval;
532 s->sorted_elements[s->used_element_count].key = &e->key; 534 s->sorted_elements[s->used_element_count].key = &e->key;
533 s->used_element_count++; 535 s->used_element_count++;
@@ -543,12 +545,12 @@ copy_element_cb(void *cls, const struct GNUNET_HashCode *key, void *value)
543 * @return -1 for a < b, 0 for a=b, 1 for a > b. 545 * @return -1 for a < b, 0 for a=b, 1 for a > b.
544 */ 546 */
545static int 547static int
546element_cmp(const void *a, const void *b) 548element_cmp (const void *a, const void *b)
547{ 549{
548 const struct MpiElement *ma = a; 550 const struct MpiElement *ma = a;
549 const struct MpiElement *mb = b; 551 const struct MpiElement *mb = b;
550 552
551 return GNUNET_CRYPTO_hash_cmp(ma->key, mb->key); 553 return GNUNET_CRYPTO_hash_cmp (ma->key, mb->key);
552} 554}
553 555
554 556
@@ -557,9 +559,9 @@ element_cmp(const void *a, const void *b)
557 * message 559 * message
558 */ 560 */
559#define ELEMENT_CAPACITY \ 561#define ELEMENT_CAPACITY \
560 ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - \ 562 ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 \
561 sizeof(struct EccAliceCryptodataMessage)) / \ 563 - sizeof(struct EccAliceCryptodataMessage)) \
562 sizeof(struct GNUNET_CRYPTO_EccPoint)) 564 / sizeof(struct GNUNET_CRYPTO_EccPoint))
563 565
564 566
565/** 567/**
@@ -569,7 +571,7 @@ element_cmp(const void *a, const void *b)
569 * @param s the associated service session 571 * @param s the associated service session
570 */ 572 */
571static void 573static void
572send_alices_cryptodata_message(struct AliceServiceSession *s) 574send_alices_cryptodata_message (struct AliceServiceSession *s)
573{ 575{
574 struct EccAliceCryptodataMessage *msg; 576 struct EccAliceCryptodataMessage *msg;
575 struct GNUNET_MQ_Envelope *e; 577 struct GNUNET_MQ_Envelope *e;
@@ -580,64 +582,64 @@ send_alices_cryptodata_message(struct AliceServiceSession *s)
580 unsigned int off; 582 unsigned int off;
581 unsigned int todo_count; 583 unsigned int todo_count;
582 584
583 s->sorted_elements = GNUNET_new_array(GNUNET_CONTAINER_multihashmap_size( 585 s->sorted_elements = GNUNET_new_array (GNUNET_CONTAINER_multihashmap_size (
584 s->intersected_elements), 586 s->intersected_elements),
585 struct MpiElement); 587 struct MpiElement);
586 s->used_element_count = 0; 588 s->used_element_count = 0;
587 GNUNET_CONTAINER_multihashmap_iterate(s->intersected_elements, 589 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
588 &copy_element_cb, 590 &copy_element_cb,
589 s); 591 s);
590 LOG(GNUNET_ERROR_TYPE_DEBUG, 592 LOG (GNUNET_ERROR_TYPE_DEBUG,
591 "Finished intersection, %d items remain\n", 593 "Finished intersection, %d items remain\n",
592 s->used_element_count); 594 s->used_element_count);
593 qsort(s->sorted_elements, 595 qsort (s->sorted_elements,
594 s->used_element_count, 596 s->used_element_count,
595 sizeof(struct MpiElement), 597 sizeof(struct MpiElement),
596 &element_cmp); 598 &element_cmp);
597 off = 0; 599 off = 0;
598 while (off < s->used_element_count) 600 while (off < s->used_element_count)
601 {
602 todo_count = s->used_element_count - off;
603 if (todo_count > ELEMENT_CAPACITY)
604 todo_count = ELEMENT_CAPACITY;
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "Sending %u/%u crypto values to Bob\n",
607 (unsigned int) todo_count,
608 (unsigned int) s->used_element_count);
609
610 e =
611 GNUNET_MQ_msg_extra (msg,
612 todo_count * 2
613 * sizeof(struct GNUNET_CRYPTO_EccPoint),
614 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA);
615 msg->contained_element_count = htonl (todo_count);
616 payload = (struct GNUNET_CRYPTO_EccPoint *) &msg[1];
617 r_ia = gcry_mpi_new (0);
618 r_ia_ai = gcry_mpi_new (0);
619 for (i = off; i < off + todo_count; i++)
599 { 620 {
600 todo_count = s->used_element_count - off; 621 gcry_mpi_t r_i;
601 if (todo_count > ELEMENT_CAPACITY) 622 gcry_mpi_point_t g_i;
602 todo_count = ELEMENT_CAPACITY; 623 gcry_mpi_point_t h_i;
603 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 624
604 "Sending %u/%u crypto values to Bob\n", 625 r_i = GNUNET_CRYPTO_ecc_random_mod_n (edc);
605 (unsigned int)todo_count, 626 g_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc, r_i);
606 (unsigned int)s->used_element_count); 627 /* r_ia = r_i * a */
607 628 gcry_mpi_mul (r_ia, r_i, my_privkey);
608 e = 629 gcry_mpi_release (r_i);
609 GNUNET_MQ_msg_extra(msg, 630 /* r_ia_ai = r_ia + a_i */
610 todo_count * 2 * 631 gcry_mpi_add (r_ia_ai, r_ia, s->sorted_elements[i].value);
611 sizeof(struct GNUNET_CRYPTO_EccPoint), 632 h_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc, r_ia_ai);
612 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA); 633 GNUNET_CRYPTO_ecc_point_to_bin (edc, g_i, &payload[(i - off) * 2]);
613 msg->contained_element_count = htonl(todo_count); 634 GNUNET_CRYPTO_ecc_point_to_bin (edc, h_i, &payload[(i - off) * 2 + 1]);
614 payload = (struct GNUNET_CRYPTO_EccPoint *)&msg[1]; 635 gcry_mpi_point_release (g_i);
615 r_ia = gcry_mpi_new(0); 636 gcry_mpi_point_release (h_i);
616 r_ia_ai = gcry_mpi_new(0);
617 for (i = off; i < off + todo_count; i++)
618 {
619 gcry_mpi_t r_i;
620 gcry_mpi_point_t g_i;
621 gcry_mpi_point_t h_i;
622
623 r_i = GNUNET_CRYPTO_ecc_random_mod_n(edc);
624 g_i = GNUNET_CRYPTO_ecc_dexp_mpi(edc, r_i);
625 /* r_ia = r_i * a */
626 gcry_mpi_mul(r_ia, r_i, my_privkey);
627 gcry_mpi_release(r_i);
628 /* r_ia_ai = r_ia + a_i */
629 gcry_mpi_add(r_ia_ai, r_ia, s->sorted_elements[i].value);
630 h_i = GNUNET_CRYPTO_ecc_dexp_mpi(edc, r_ia_ai);
631 GNUNET_CRYPTO_ecc_point_to_bin(edc, g_i, &payload[(i - off) * 2]);
632 GNUNET_CRYPTO_ecc_point_to_bin(edc, h_i, &payload[(i - off) * 2 + 1]);
633 gcry_mpi_point_release(g_i);
634 gcry_mpi_point_release(h_i);
635 }
636 gcry_mpi_release(r_ia);
637 gcry_mpi_release(r_ia_ai);
638 off += todo_count;
639 GNUNET_MQ_send(s->cadet_mq, e);
640 } 637 }
638 gcry_mpi_release (r_ia);
639 gcry_mpi_release (r_ia_ai);
640 off += todo_count;
641 GNUNET_MQ_send (s->cadet_mq, e);
642 }
641} 643}
642 644
643 645
@@ -652,70 +654,70 @@ send_alices_cryptodata_message(struct AliceServiceSession *s)
652 * @param status what has happened with the set intersection? 654 * @param status what has happened with the set intersection?
653 */ 655 */
654static void 656static void
655cb_intersection_element_removed(void *cls, 657cb_intersection_element_removed (void *cls,
656 const struct GNUNET_SET_Element *element, 658 const struct GNUNET_SET_Element *element,
657 uint64_t current_size, 659 uint64_t current_size,
658 enum GNUNET_SET_Status status) 660 enum GNUNET_SET_Status status)
659{ 661{
660 struct AliceServiceSession *s = cls; 662 struct AliceServiceSession *s = cls;
661 struct GNUNET_SCALARPRODUCT_Element *se; 663 struct GNUNET_SCALARPRODUCT_Element *se;
662 664
663 switch (status) 665 switch (status)
666 {
667 case GNUNET_SET_STATUS_OK:
668 /* this element has been removed from the set */
669 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
670 element->data);
671 GNUNET_assert (NULL != se);
672 LOG (GNUNET_ERROR_TYPE_DEBUG,
673 "Intersection removed element with key %s and value %lld\n",
674 GNUNET_h2s (&se->key),
675 (long long) GNUNET_ntohll (se->value));
676 GNUNET_assert (
677 GNUNET_YES ==
678 GNUNET_CONTAINER_multihashmap_remove (s->intersected_elements,
679 element->data,
680 se));
681 GNUNET_free (se);
682 return;
683
684 case GNUNET_SET_STATUS_DONE:
685 s->intersection_op = NULL;
686 if (NULL != s->intersection_set)
687 {
688 GNUNET_SET_destroy (s->intersection_set);
689 s->intersection_set = NULL;
690 }
691 send_alices_cryptodata_message (s);
692 return;
693
694 case GNUNET_SET_STATUS_HALF_DONE:
695 /* unexpected for intersection */
696 GNUNET_break (0);
697 return;
698
699 case GNUNET_SET_STATUS_FAILURE:
700 /* unhandled status code */
701 LOG (GNUNET_ERROR_TYPE_DEBUG, "Set intersection failed!\n");
702 if (NULL != s->intersection_listen)
664 { 703 {
665 case GNUNET_SET_STATUS_OK: 704 GNUNET_SET_listen_cancel (s->intersection_listen);
666 /* this element has been removed from the set */ 705 s->intersection_listen = NULL;
667 se = GNUNET_CONTAINER_multihashmap_get(s->intersected_elements,
668 element->data);
669 GNUNET_assert(NULL != se);
670 LOG(GNUNET_ERROR_TYPE_DEBUG,
671 "Intersection removed element with key %s and value %lld\n",
672 GNUNET_h2s(&se->key),
673 (long long)GNUNET_ntohll(se->value));
674 GNUNET_assert(
675 GNUNET_YES ==
676 GNUNET_CONTAINER_multihashmap_remove(s->intersected_elements,
677 element->data,
678 se));
679 GNUNET_free(se);
680 return;
681
682 case GNUNET_SET_STATUS_DONE:
683 s->intersection_op = NULL;
684 if (NULL != s->intersection_set)
685 {
686 GNUNET_SET_destroy(s->intersection_set);
687 s->intersection_set = NULL;
688 }
689 send_alices_cryptodata_message(s);
690 return;
691
692 case GNUNET_SET_STATUS_HALF_DONE:
693 /* unexpected for intersection */
694 GNUNET_break(0);
695 return;
696
697 case GNUNET_SET_STATUS_FAILURE:
698 /* unhandled status code */
699 LOG(GNUNET_ERROR_TYPE_DEBUG, "Set intersection failed!\n");
700 if (NULL != s->intersection_listen)
701 {
702 GNUNET_SET_listen_cancel(s->intersection_listen);
703 s->intersection_listen = NULL;
704 }
705 s->intersection_op = NULL;
706 if (NULL != s->intersection_set)
707 {
708 GNUNET_SET_destroy(s->intersection_set);
709 s->intersection_set = NULL;
710 }
711 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
712 prepare_client_end_notification(s);
713 return;
714
715 default:
716 GNUNET_break(0);
717 return;
718 } 706 }
707 s->intersection_op = NULL;
708 if (NULL != s->intersection_set)
709 {
710 GNUNET_SET_destroy (s->intersection_set);
711 s->intersection_set = NULL;
712 }
713 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
714 prepare_client_end_notification (s);
715 return;
716
717 default:
718 GNUNET_break (0);
719 return;
720 }
719} 721}
720 722
721 723
@@ -735,40 +737,40 @@ cb_intersection_element_removed(void *cls,
735 * message. It's necessary to specify the timeout. 737 * message. It's necessary to specify the timeout.
736 */ 738 */
737static void 739static void
738cb_intersection_request_alice(void *cls, 740cb_intersection_request_alice (void *cls,
739 const struct GNUNET_PeerIdentity *other_peer, 741 const struct GNUNET_PeerIdentity *other_peer,
740 const struct GNUNET_MessageHeader *context_msg, 742 const struct GNUNET_MessageHeader *context_msg,
741 struct GNUNET_SET_Request *request) 743 struct GNUNET_SET_Request *request)
742{ 744{
743 struct AliceServiceSession *s = cls; 745 struct AliceServiceSession *s = cls;
744 746
745 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746 "Received intersection request from %s!\n", 748 "Received intersection request from %s!\n",
747 GNUNET_i2s(other_peer)); 749 GNUNET_i2s (other_peer));
748 if (0 != GNUNET_memcmp(other_peer, &s->peer)) 750 if (0 != GNUNET_memcmp (other_peer, &s->peer))
749 { 751 {
750 GNUNET_break_op(0); 752 GNUNET_break_op (0);
751 return; 753 return;
752 } 754 }
753 s->intersection_op = GNUNET_SET_accept(request, 755 s->intersection_op = GNUNET_SET_accept (request,
754 GNUNET_SET_RESULT_REMOVED, 756 GNUNET_SET_RESULT_REMOVED,
755 (struct GNUNET_SET_Option[]){ { 0 } }, 757 (struct GNUNET_SET_Option[]){ { 0 } },
756 &cb_intersection_element_removed, 758 &cb_intersection_element_removed,
757 s); 759 s);
758 if (NULL == s->intersection_op) 760 if (NULL == s->intersection_op)
759 { 761 {
760 GNUNET_break(0); 762 GNUNET_break (0);
761 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 763 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
762 prepare_client_end_notification(s); 764 prepare_client_end_notification (s);
763 return; 765 return;
764 } 766 }
765 if (GNUNET_OK != GNUNET_SET_commit(s->intersection_op, s->intersection_set)) 767 if (GNUNET_OK != GNUNET_SET_commit (s->intersection_op, s->intersection_set))
766 { 768 {
767 GNUNET_break(0); 769 GNUNET_break (0);
768 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 770 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
769 prepare_client_end_notification(s); 771 prepare_client_end_notification (s);
770 return; 772 return;
771 } 773 }
772} 774}
773 775
774 776
@@ -778,58 +780,58 @@ cb_intersection_request_alice(void *cls,
778 * @param session the service session context 780 * @param session the service session context
779 */ 781 */
780static void 782static void
781client_request_complete_alice(struct AliceServiceSession *s) 783client_request_complete_alice (struct AliceServiceSession *s)
782{ 784{
783 struct GNUNET_MQ_MessageHandler cadet_handlers[] = 785 struct GNUNET_MQ_MessageHandler cadet_handlers[] =
784 { GNUNET_MQ_hd_fixed_size(bobs_cryptodata_message, 786 { GNUNET_MQ_hd_fixed_size (bobs_cryptodata_message,
785 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA, 787 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA,
786 struct EccBobCryptodataMessage, 788 struct EccBobCryptodataMessage,
787 s), 789 s),
788 GNUNET_MQ_handler_end() }; 790 GNUNET_MQ_handler_end () };
789 struct EccServiceRequestMessage *msg; 791 struct EccServiceRequestMessage *msg;
790 struct GNUNET_MQ_Envelope *e; 792 struct GNUNET_MQ_Envelope *e;
791 struct GNUNET_HashCode set_sid; 793 struct GNUNET_HashCode set_sid;
792 794
793 GNUNET_CRYPTO_hash(&s->session_id, 795 GNUNET_CRYPTO_hash (&s->session_id,
794 sizeof(struct GNUNET_HashCode), 796 sizeof(struct GNUNET_HashCode),
795 &set_sid); 797 &set_sid);
796 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 "Creating new channel for session with key %s.\n", 799 "Creating new channel for session with key %s.\n",
798 GNUNET_h2s(&s->session_id)); 800 GNUNET_h2s (&s->session_id));
799 s->channel = GNUNET_CADET_channel_create(my_cadet, 801 s->channel = GNUNET_CADET_channel_create (my_cadet,
800 s, 802 s,
801 &s->peer, 803 &s->peer,
802 &s->session_id, 804 &s->session_id,
803 NULL, 805 NULL,
804 &cb_channel_destruction, 806 &cb_channel_destruction,
805 cadet_handlers); 807 cadet_handlers);
806 if (NULL == s->channel) 808 if (NULL == s->channel)
807 { 809 {
808 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 810 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
809 prepare_client_end_notification(s); 811 prepare_client_end_notification (s);
810 return; 812 return;
811 } 813 }
812 s->cadet_mq = GNUNET_CADET_get_mq(s->channel); 814 s->cadet_mq = GNUNET_CADET_get_mq (s->channel);
813 s->intersection_listen = GNUNET_SET_listen(cfg, 815 s->intersection_listen = GNUNET_SET_listen (cfg,
814 GNUNET_SET_OPERATION_INTERSECTION, 816 GNUNET_SET_OPERATION_INTERSECTION,
815 &set_sid, 817 &set_sid,
816 &cb_intersection_request_alice, 818 &cb_intersection_request_alice,
817 s); 819 s);
818 if (NULL == s->intersection_listen) 820 if (NULL == s->intersection_listen)
819 { 821 {
820 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 822 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
821 GNUNET_CADET_channel_destroy(s->channel); 823 GNUNET_CADET_channel_destroy (s->channel);
822 s->channel = NULL; 824 s->channel = NULL;
823 prepare_client_end_notification(s); 825 prepare_client_end_notification (s);
824 return; 826 return;
825 } 827 }
826 828
827 e = 829 e =
828 GNUNET_MQ_msg(msg, 830 GNUNET_MQ_msg (msg,
829 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION); 831 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION);
830 GNUNET_MQ_env_set_options(e, GNUNET_MQ_PRIO_CRITICAL_CONTROL); 832 GNUNET_MQ_env_set_options (e, GNUNET_MQ_PRIO_CRITICAL_CONTROL);
831 msg->session_id = s->session_id; 833 msg->session_id = s->session_id;
832 GNUNET_MQ_send(s->cadet_mq, e); 834 GNUNET_MQ_send (s->cadet_mq, e);
833} 835}
834 836
835 837
@@ -842,7 +844,7 @@ client_request_complete_alice(struct AliceServiceSession *s)
842 * @return #GNUNET_OK if @a msg is well-formed 844 * @return #GNUNET_OK if @a msg is well-formed
843 */ 845 */
844static int 846static int
845check_alice_client_message_multipart( 847check_alice_client_message_multipart (
846 void *cls, 848 void *cls,
847 const struct ComputationBobCryptodataMultipartMessage *msg) 849 const struct ComputationBobCryptodataMultipartMessage *msg)
848{ 850{
@@ -850,18 +852,18 @@ check_alice_client_message_multipart(
850 uint32_t contained_count; 852 uint32_t contained_count;
851 uint16_t msize; 853 uint16_t msize;
852 854
853 msize = ntohs(msg->header.size); 855 msize = ntohs (msg->header.size);
854 contained_count = ntohl(msg->element_count_contained); 856 contained_count = ntohl (msg->element_count_contained);
855 if ((msize != 857 if ((msize !=
856 (sizeof(struct ComputationBobCryptodataMultipartMessage) + 858 (sizeof(struct ComputationBobCryptodataMultipartMessage)
857 contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element))) || 859 + contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element))) ||
858 (0 == contained_count) || 860 (0 == contained_count) ||
859 (s->total == s->client_received_element_count) || 861 (s->total == s->client_received_element_count) ||
860 (s->total < s->client_received_element_count + contained_count)) 862 (s->total < s->client_received_element_count + contained_count))
861 { 863 {
862 GNUNET_break_op(0); 864 GNUNET_break_op (0);
863 return GNUNET_SYSERR; 865 return GNUNET_SYSERR;
864 } 866 }
865 return GNUNET_OK; 867 return GNUNET_OK;
866} 868}
867 869
@@ -874,7 +876,7 @@ check_alice_client_message_multipart(
874 * @param msg the actual message 876 * @param msg the actual message
875 */ 877 */
876static void 878static void
877handle_alice_client_message_multipart( 879handle_alice_client_message_multipart (
878 void *cls, 880 void *cls,
879 const struct ComputationBobCryptodataMultipartMessage *msg) 881 const struct ComputationBobCryptodataMultipartMessage *msg)
880{ 882{
@@ -884,41 +886,41 @@ handle_alice_client_message_multipart(
884 struct GNUNET_SET_Element set_elem; 886 struct GNUNET_SET_Element set_elem;
885 struct GNUNET_SCALARPRODUCT_Element *elem; 887 struct GNUNET_SCALARPRODUCT_Element *elem;
886 888
887 contained_count = ntohl(msg->element_count_contained); 889 contained_count = ntohl (msg->element_count_contained);
888 s->client_received_element_count += contained_count; 890 s->client_received_element_count += contained_count;
889 elements = (const struct GNUNET_SCALARPRODUCT_Element *)&msg[1]; 891 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
890 for (uint32_t i = 0; i < contained_count; i++) 892 for (uint32_t i = 0; i < contained_count; i++)
893 {
894 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
895 GNUNET_memcpy (elem,
896 &elements[i],
897 sizeof(struct GNUNET_SCALARPRODUCT_Element));
898 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put (
899 s->intersected_elements,
900 &elem->key,
901 elem,
902 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
891 { 903 {
892 elem = GNUNET_new(struct GNUNET_SCALARPRODUCT_Element); 904 GNUNET_break (0);
893 GNUNET_memcpy(elem, 905 GNUNET_free (elem);
894 &elements[i], 906 continue;
895 sizeof(struct GNUNET_SCALARPRODUCT_Element));
896 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(
897 s->intersected_elements,
898 &elem->key,
899 elem,
900 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
901 {
902 GNUNET_break(0);
903 GNUNET_free(elem);
904 continue;
905 }
906 set_elem.data = &elem->key;
907 set_elem.size = sizeof(elem->key);
908 set_elem.element_type = 0;
909 GNUNET_SET_add_element(s->intersection_set, &set_elem, NULL, NULL);
910 s->used_element_count++;
911 } 907 }
912 GNUNET_SERVICE_client_continue(s->client); 908 set_elem.data = &elem->key;
909 set_elem.size = sizeof(elem->key);
910 set_elem.element_type = 0;
911 GNUNET_SET_add_element (s->intersection_set, &set_elem, NULL, NULL);
912 s->used_element_count++;
913 }
914 GNUNET_SERVICE_client_continue (s->client);
913 if (s->total != s->client_received_element_count) 915 if (s->total != s->client_received_element_count)
914 { 916 {
915 /* more to come */ 917 /* more to come */
916 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Received client multipart data, waiting for more!\n"); 919 "Received client multipart data, waiting for more!\n");
918 return; 920 return;
919 } 921 }
920 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Launching computation\n"); 922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Launching computation\n");
921 client_request_complete_alice(s); 923 client_request_complete_alice (s);
922} 924}
923 925
924 926
@@ -931,8 +933,8 @@ handle_alice_client_message_multipart(
931 * @return #GNUNET_OK if @a msg is well-formed 933 * @return #GNUNET_OK if @a msg is well-formed
932 */ 934 */
933static int 935static int
934check_alice_client_message(void *cls, 936check_alice_client_message (void *cls,
935 const struct AliceComputationMessage *msg) 937 const struct AliceComputationMessage *msg)
936{ 938{
937 struct AliceServiceSession *s = cls; 939 struct AliceServiceSession *s = cls;
938 uint16_t msize; 940 uint16_t msize;
@@ -940,23 +942,23 @@ check_alice_client_message(void *cls,
940 uint32_t contained_count; 942 uint32_t contained_count;
941 943
942 if (NULL != s->intersected_elements) 944 if (NULL != s->intersected_elements)
943 { 945 {
944 /* only one concurrent session per client connection allowed, 946 /* only one concurrent session per client connection allowed,
945 simplifies logic a lot... */ 947 simplifies logic a lot... */
946 GNUNET_break(0); 948 GNUNET_break (0);
947 return GNUNET_SYSERR; 949 return GNUNET_SYSERR;
948 } 950 }
949 msize = ntohs(msg->header.size); 951 msize = ntohs (msg->header.size);
950 total_count = ntohl(msg->element_count_total); 952 total_count = ntohl (msg->element_count_total);
951 contained_count = ntohl(msg->element_count_contained); 953 contained_count = ntohl (msg->element_count_contained);
952 if ((0 == total_count) || (0 == contained_count) || 954 if ((0 == total_count) || (0 == contained_count) ||
953 (msize != 955 (msize !=
954 (sizeof(struct AliceComputationMessage) + 956 (sizeof(struct AliceComputationMessage)
955 contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element)))) 957 + contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element))))
956 { 958 {
957 GNUNET_break_op(0); 959 GNUNET_break_op (0);
958 return GNUNET_SYSERR; 960 return GNUNET_SYSERR;
959 } 961 }
960 return GNUNET_OK; 962 return GNUNET_OK;
961} 963}
962 964
@@ -969,8 +971,8 @@ check_alice_client_message(void *cls,
969 * @param msg the actual message 971 * @param msg the actual message
970 */ 972 */
971static void 973static void
972handle_alice_client_message(void *cls, 974handle_alice_client_message (void *cls,
973 const struct AliceComputationMessage *msg) 975 const struct AliceComputationMessage *msg)
974{ 976{
975 struct AliceServiceSession *s = cls; 977 struct AliceServiceSession *s = cls;
976 uint32_t contained_count; 978 uint32_t contained_count;
@@ -979,53 +981,53 @@ handle_alice_client_message(void *cls,
979 struct GNUNET_SET_Element set_elem; 981 struct GNUNET_SET_Element set_elem;
980 struct GNUNET_SCALARPRODUCT_Element *elem; 982 struct GNUNET_SCALARPRODUCT_Element *elem;
981 983
982 total_count = ntohl(msg->element_count_total); 984 total_count = ntohl (msg->element_count_total);
983 contained_count = ntohl(msg->element_count_contained); 985 contained_count = ntohl (msg->element_count_contained);
984 s->peer = msg->peer; 986 s->peer = msg->peer;
985 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE; 987 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
986 s->total = total_count; 988 s->total = total_count;
987 s->client_received_element_count = contained_count; 989 s->client_received_element_count = contained_count;
988 s->session_id = msg->session_key; 990 s->session_id = msg->session_key;
989 elements = (const struct GNUNET_SCALARPRODUCT_Element *)&msg[1]; 991 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
990 s->intersected_elements = 992 s->intersected_elements =
991 GNUNET_CONTAINER_multihashmap_create(s->total, GNUNET_YES); 993 GNUNET_CONTAINER_multihashmap_create (s->total, GNUNET_YES);
992 s->intersection_set = 994 s->intersection_set =
993 GNUNET_SET_create(cfg, GNUNET_SET_OPERATION_INTERSECTION); 995 GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_INTERSECTION);
994 for (uint32_t i = 0; i < contained_count; i++) 996 for (uint32_t i = 0; i < contained_count; i++)
997 {
998 if (0 == GNUNET_ntohll (elements[i].value))
999 continue;
1000 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1001 GNUNET_memcpy (elem,
1002 &elements[i],
1003 sizeof(struct GNUNET_SCALARPRODUCT_Element));
1004 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put (
1005 s->intersected_elements,
1006 &elem->key,
1007 elem,
1008 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
995 { 1009 {
996 if (0 == GNUNET_ntohll(elements[i].value)) 1010 /* element with same key encountered twice! */
997 continue; 1011 GNUNET_break (0);
998 elem = GNUNET_new(struct GNUNET_SCALARPRODUCT_Element); 1012 GNUNET_free (elem);
999 GNUNET_memcpy(elem, 1013 continue;
1000 &elements[i],
1001 sizeof(struct GNUNET_SCALARPRODUCT_Element));
1002 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(
1003 s->intersected_elements,
1004 &elem->key,
1005 elem,
1006 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1007 {
1008 /* element with same key encountered twice! */
1009 GNUNET_break(0);
1010 GNUNET_free(elem);
1011 continue;
1012 }
1013 set_elem.data = &elem->key;
1014 set_elem.size = sizeof(elem->key);
1015 set_elem.element_type = 0;
1016 GNUNET_SET_add_element(s->intersection_set, &set_elem, NULL, NULL);
1017 s->used_element_count++;
1018 } 1014 }
1019 GNUNET_SERVICE_client_continue(s->client); 1015 set_elem.data = &elem->key;
1016 set_elem.size = sizeof(elem->key);
1017 set_elem.element_type = 0;
1018 GNUNET_SET_add_element (s->intersection_set, &set_elem, NULL, NULL);
1019 s->used_element_count++;
1020 }
1021 GNUNET_SERVICE_client_continue (s->client);
1020 if (s->total != s->client_received_element_count) 1022 if (s->total != s->client_received_element_count)
1021 { 1023 {
1022 /* wait for multipart msg */ 1024 /* wait for multipart msg */
1023 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "Received partial client request, waiting for more!\n"); 1026 "Received partial client request, waiting for more!\n");
1025 return; 1027 return;
1026 } 1028 }
1027 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Launching computation\n"); 1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Launching computation\n");
1028 client_request_complete_alice(s); 1030 client_request_complete_alice (s);
1029} 1031}
1030 1032
1031 1033
@@ -1036,20 +1038,20 @@ handle_alice_client_message(void *cls,
1036 * @param tc unused 1038 * @param tc unused
1037 */ 1039 */
1038static void 1040static void
1039shutdown_task(void *cls) 1041shutdown_task (void *cls)
1040{ 1042{
1041 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Shutting down, initiating cleanup.\n"); 1043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down, initiating cleanup.\n");
1042 // FIXME: we have to cut our connections to CADET first! 1044 // FIXME: we have to cut our connections to CADET first!
1043 if (NULL != my_cadet) 1045 if (NULL != my_cadet)
1044 { 1046 {
1045 GNUNET_CADET_disconnect(my_cadet); 1047 GNUNET_CADET_disconnect (my_cadet);
1046 my_cadet = NULL; 1048 my_cadet = NULL;
1047 } 1049 }
1048 if (NULL != edc) 1050 if (NULL != edc)
1049 { 1051 {
1050 GNUNET_CRYPTO_ecc_dlog_release(edc); 1052 GNUNET_CRYPTO_ecc_dlog_release (edc);
1051 edc = NULL; 1053 edc = NULL;
1052 } 1054 }
1053} 1055}
1054 1056
1055 1057
@@ -1064,13 +1066,13 @@ shutdown_task(void *cls)
1064 * @return our `struct AliceServiceSession` 1066 * @return our `struct AliceServiceSession`
1065 */ 1067 */
1066static void * 1068static void *
1067client_connect_cb(void *cls, 1069client_connect_cb (void *cls,
1068 struct GNUNET_SERVICE_Client *client, 1070 struct GNUNET_SERVICE_Client *client,
1069 struct GNUNET_MQ_Handle *mq) 1071 struct GNUNET_MQ_Handle *mq)
1070{ 1072{
1071 struct AliceServiceSession *s; 1073 struct AliceServiceSession *s;
1072 1074
1073 s = GNUNET_new(struct AliceServiceSession); 1075 s = GNUNET_new (struct AliceServiceSession);
1074 s->client = client; 1076 s->client = client;
1075 s->client_mq = mq; 1077 s->client_mq = mq;
1076 return s; 1078 return s;
@@ -1088,18 +1090,18 @@ client_connect_cb(void *cls,
1088 * @param app_cls our `struct AliceServiceSession` 1090 * @param app_cls our `struct AliceServiceSession`
1089 */ 1091 */
1090static void 1092static void
1091client_disconnect_cb(void *cls, 1093client_disconnect_cb (void *cls,
1092 struct GNUNET_SERVICE_Client *client, 1094 struct GNUNET_SERVICE_Client *client,
1093 void *app_cls) 1095 void *app_cls)
1094{ 1096{
1095 struct AliceServiceSession *s = app_cls; 1097 struct AliceServiceSession *s = app_cls;
1096 1098
1097 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "Client %p disconnected from us.\n", 1100 "Client %p disconnected from us.\n",
1099 client); 1101 client);
1100 s->client = NULL; 1102 s->client = NULL;
1101 s->client_mq = NULL; 1103 s->client_mq = NULL;
1102 destroy_service_session(s); 1104 destroy_service_session (s);
1103} 1105}
1104 1106
1105 1107
@@ -1111,45 +1113,45 @@ client_disconnect_cb(void *cls,
1111 * @param service the initialized service 1113 * @param service the initialized service
1112 */ 1114 */
1113static void 1115static void
1114run(void *cls, 1116run (void *cls,
1115 const struct GNUNET_CONFIGURATION_Handle *c, 1117 const struct GNUNET_CONFIGURATION_Handle *c,
1116 struct GNUNET_SERVICE_Handle *service) 1118 struct GNUNET_SERVICE_Handle *service)
1117{ 1119{
1118 cfg = c; 1120 cfg = c;
1119 edc = GNUNET_CRYPTO_ecc_dlog_prepare(MAX_RESULT, MAX_RAM); 1121 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_RESULT, MAX_RAM);
1120 /* Select a random 'a' value for Alice */ 1122 /* Select a random 'a' value for Alice */
1121 GNUNET_CRYPTO_ecc_rnd_mpi(edc, &my_privkey, &my_privkey_inv); 1123 GNUNET_CRYPTO_ecc_rnd_mpi (edc, &my_privkey, &my_privkey_inv);
1122 my_cadet = GNUNET_CADET_connect(cfg); 1124 my_cadet = GNUNET_CADET_connect (cfg);
1123 if (NULL == my_cadet) 1125 if (NULL == my_cadet)
1124 { 1126 {
1125 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Connect to CADET failed\n")); 1127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Connect to CADET failed\n"));
1126 GNUNET_SCHEDULER_shutdown(); 1128 GNUNET_SCHEDULER_shutdown ();
1127 return; 1129 return;
1128 } 1130 }
1129 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL); 1131 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
1130} 1132}
1131 1133
1132 1134
1133/** 1135/**
1134 * Define "main" method using service macro. 1136 * Define "main" method using service macro.
1135 */ 1137 */
1136GNUNET_SERVICE_MAIN( 1138GNUNET_SERVICE_MAIN (
1137 "scalarproduct-alice", 1139 "scalarproduct-alice",
1138 GNUNET_SERVICE_OPTION_NONE, 1140 GNUNET_SERVICE_OPTION_NONE,
1139 &run, 1141 &run,
1140 &client_connect_cb, 1142 &client_connect_cb,
1141 &client_disconnect_cb, 1143 &client_disconnect_cb,
1142 NULL, 1144 NULL,
1143 GNUNET_MQ_hd_var_size(alice_client_message, 1145 GNUNET_MQ_hd_var_size (alice_client_message,
1144 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE, 1146 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE,
1145 struct AliceComputationMessage, 1147 struct AliceComputationMessage,
1146 NULL), 1148 NULL),
1147 GNUNET_MQ_hd_var_size( 1149 GNUNET_MQ_hd_var_size (
1148 alice_client_message_multipart, 1150 alice_client_message_multipart,
1149 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE, 1151 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE,
1150 struct ComputationBobCryptodataMultipartMessage, 1152 struct ComputationBobCryptodataMultipartMessage,
1151 NULL), 1153 NULL),
1152 GNUNET_MQ_handler_end()); 1154 GNUNET_MQ_handler_end ());
1153 1155
1154 1156
1155/* end of gnunet-service-scalarproduct-ecc_alice.c */ 1157/* end of gnunet-service-scalarproduct-ecc_alice.c */
diff --git a/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c b/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c
index fc2d1d8c5..2da79f845 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c
+++ b/src/scalarproduct/gnunet-service-scalarproduct-ecc_bob.c
@@ -36,13 +36,14 @@
36#include "scalarproduct.h" 36#include "scalarproduct.h"
37#include "gnunet-service-scalarproduct-ecc.h" 37#include "gnunet-service-scalarproduct-ecc.h"
38 38
39#define LOG(kind, ...) GNUNET_log_from(kind, "scalarproduct-bob", __VA_ARGS__) 39#define LOG(kind, ...) GNUNET_log_from (kind, "scalarproduct-bob", __VA_ARGS__)
40 40
41 41
42/** 42/**
43 * An encrypted element key-value pair. 43 * An encrypted element key-value pair.
44 */ 44 */
45struct MpiElement { 45struct MpiElement
46{
46 /** 47 /**
47 * Key used to identify matching pairs of values to multiply. 48 * Key used to identify matching pairs of values to multiply.
48 * Points into an existing data structure, to avoid copying 49 * Points into an existing data structure, to avoid copying
@@ -61,7 +62,8 @@ struct MpiElement {
61 * A scalarproduct session which tracks an offer for a 62 * A scalarproduct session which tracks an offer for a
62 * multiplication service by a local client. 63 * multiplication service by a local client.
63 */ 64 */
64struct BobServiceSession { 65struct BobServiceSession
66{
65 /** 67 /**
66 * The client this request is related to. 68 * The client this request is related to.
67 */ 69 */
@@ -192,13 +194,13 @@ static struct GNUNET_CRYPTO_EccDlogContext *edc;
192 * @param value the value to free 194 * @param value the value to free
193 */ 195 */
194static int 196static int
195free_element_cb(void *cls, 197free_element_cb (void *cls,
196 const struct GNUNET_HashCode *key, 198 const struct GNUNET_HashCode *key,
197 void *value) 199 void *value)
198{ 200{
199 struct GNUNET_SCALARPRODUCT_Element *element = value; 201 struct GNUNET_SCALARPRODUCT_Element *element = value;
200 202
201 GNUNET_free(element); 203 GNUNET_free (element);
202 return GNUNET_OK; 204 return GNUNET_OK;
203} 205}
204 206
@@ -209,7 +211,7 @@ free_element_cb(void *cls,
209 * @param session the session to free elements from 211 * @param session the session to free elements from
210 */ 212 */
211static void 213static void
212destroy_service_session(struct BobServiceSession *s) 214destroy_service_session (struct BobServiceSession *s)
213{ 215{
214 unsigned int i; 216 unsigned int i;
215 217
@@ -217,58 +219,58 @@ destroy_service_session(struct BobServiceSession *s)
217 return; 219 return;
218 s->in_destroy = GNUNET_YES; 220 s->in_destroy = GNUNET_YES;
219 if (NULL != s->client) 221 if (NULL != s->client)
220 { 222 {
221 struct GNUNET_SERVICE_Client *c = s->client; 223 struct GNUNET_SERVICE_Client *c = s->client;
222 224
223 s->client = NULL; 225 s->client = NULL;
224 GNUNET_SERVICE_client_drop(c); 226 GNUNET_SERVICE_client_drop (c);
225 } 227 }
226 if (NULL != s->intersected_elements) 228 if (NULL != s->intersected_elements)
227 { 229 {
228 GNUNET_CONTAINER_multihashmap_iterate(s->intersected_elements, 230 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
229 &free_element_cb, 231 &free_element_cb,
230 NULL); 232 NULL);
231 GNUNET_CONTAINER_multihashmap_destroy(s->intersected_elements); 233 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
232 s->intersected_elements = NULL; 234 s->intersected_elements = NULL;
233 } 235 }
234 if (NULL != s->intersection_op) 236 if (NULL != s->intersection_op)
235 { 237 {
236 GNUNET_SET_operation_cancel(s->intersection_op); 238 GNUNET_SET_operation_cancel (s->intersection_op);
237 s->intersection_op = NULL; 239 s->intersection_op = NULL;
238 } 240 }
239 if (NULL != s->intersection_set) 241 if (NULL != s->intersection_set)
240 { 242 {
241 GNUNET_SET_destroy(s->intersection_set); 243 GNUNET_SET_destroy (s->intersection_set);
242 s->intersection_set = NULL; 244 s->intersection_set = NULL;
243 } 245 }
244 if (NULL != s->sorted_elements) 246 if (NULL != s->sorted_elements)
245 { 247 {
246 for (i = 0; i < s->used_element_count; i++) 248 for (i = 0; i < s->used_element_count; i++)
247 gcry_mpi_release(s->sorted_elements[i].value); 249 gcry_mpi_release (s->sorted_elements[i].value);
248 GNUNET_free(s->sorted_elements); 250 GNUNET_free (s->sorted_elements);
249 s->sorted_elements = NULL; 251 s->sorted_elements = NULL;
250 } 252 }
251 if (NULL != s->prod_g_i_b_i) 253 if (NULL != s->prod_g_i_b_i)
252 { 254 {
253 gcry_mpi_point_release(s->prod_g_i_b_i); 255 gcry_mpi_point_release (s->prod_g_i_b_i);
254 s->prod_g_i_b_i = NULL; 256 s->prod_g_i_b_i = NULL;
255 } 257 }
256 if (NULL != s->prod_h_i_b_i) 258 if (NULL != s->prod_h_i_b_i)
257 { 259 {
258 gcry_mpi_point_release(s->prod_h_i_b_i); 260 gcry_mpi_point_release (s->prod_h_i_b_i);
259 s->prod_h_i_b_i = NULL; 261 s->prod_h_i_b_i = NULL;
260 } 262 }
261 if (NULL != s->port) 263 if (NULL != s->port)
262 { 264 {
263 GNUNET_CADET_close_port(s->port); 265 GNUNET_CADET_close_port (s->port);
264 s->port = NULL; 266 s->port = NULL;
265 } 267 }
266 if (NULL != s->channel) 268 if (NULL != s->channel)
267 { 269 {
268 GNUNET_CADET_channel_destroy(s->channel); 270 GNUNET_CADET_channel_destroy (s->channel);
269 s->channel = NULL; 271 s->channel = NULL;
270 } 272 }
271 GNUNET_free(s); 273 GNUNET_free (s);
272} 274}
273 275
274 276
@@ -280,24 +282,24 @@ destroy_service_session(struct BobServiceSession *s)
280 * @param session the associated client session to fail or succeed 282 * @param session the associated client session to fail or succeed
281 */ 283 */
282static void 284static void
283prepare_client_end_notification(struct BobServiceSession *session) 285prepare_client_end_notification (struct BobServiceSession *session)
284{ 286{
285 struct ClientResponseMessage *msg; 287 struct ClientResponseMessage *msg;
286 struct GNUNET_MQ_Envelope *e; 288 struct GNUNET_MQ_Envelope *e;
287 289
288 if (NULL == session->client_mq) 290 if (NULL == session->client_mq)
289 return; /* no client left to be notified */ 291 return; /* no client left to be notified */
290 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Sending session-end notification with status %d to client for session %s\n", 293 "Sending session-end notification with status %d to client for session %s\n",
292 session->status, 294 session->status,
293 GNUNET_h2s(&session->session_id)); 295 GNUNET_h2s (&session->session_id));
294 e = GNUNET_MQ_msg(msg, 296 e = GNUNET_MQ_msg (msg,
295 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT); 297 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
296 msg->range = 0; 298 msg->range = 0;
297 msg->product_length = htonl(0); 299 msg->product_length = htonl (0);
298 msg->status = htonl(session->status); 300 msg->status = htonl (session->status);
299 GNUNET_MQ_send(session->client_mq, 301 GNUNET_MQ_send (session->client_mq,
300 e); 302 e);
301} 303}
302 304
303 305
@@ -313,22 +315,22 @@ prepare_client_end_notification(struct BobServiceSession *session)
313 * with the channel is stored 315 * with the channel is stored
314 */ 316 */
315static void 317static void
316cb_channel_destruction(void *cls, 318cb_channel_destruction (void *cls,
317 const struct GNUNET_CADET_Channel *channel) 319 const struct GNUNET_CADET_Channel *channel)
318{ 320{
319 struct BobServiceSession *s = cls; 321 struct BobServiceSession *s = cls;
320 322
321 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Peer disconnected, terminating session %s with peer %s\n", 324 "Peer disconnected, terminating session %s with peer %s\n",
323 GNUNET_h2s(&s->session_id), 325 GNUNET_h2s (&s->session_id),
324 GNUNET_i2s(&s->peer)); 326 GNUNET_i2s (&s->peer));
325 s->channel = NULL; 327 s->channel = NULL;
326 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status) 328 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
327 { 329 {
328 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 330 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
329 prepare_client_end_notification(s); 331 prepare_client_end_notification (s);
330 } 332 }
331 destroy_service_session(s); 333 destroy_service_session (s);
332} 334}
333 335
334 336
@@ -337,12 +339,12 @@ cb_channel_destruction(void *cls,
337 * the client that we are finished. 339 * the client that we are finished.
338 */ 340 */
339static void 341static void
340bob_cadet_done_cb(void *cls) 342bob_cadet_done_cb (void *cls)
341{ 343{
342 struct BobServiceSession *session = cls; 344 struct BobServiceSession *session = cls;
343 345
344 session->status = GNUNET_SCALARPRODUCT_STATUS_SUCCESS; 346 session->status = GNUNET_SCALARPRODUCT_STATUS_SUCCESS;
345 prepare_client_end_notification(session); 347 prepare_client_end_notification (session);
346} 348}
347 349
348 350
@@ -352,29 +354,29 @@ bob_cadet_done_cb(void *cls)
352 * @param s the associated requesting session with Alice 354 * @param s the associated requesting session with Alice
353 */ 355 */
354static void 356static void
355transmit_bobs_cryptodata_message(struct BobServiceSession *s) 357transmit_bobs_cryptodata_message (struct BobServiceSession *s)
356{ 358{
357 struct EccBobCryptodataMessage *msg; 359 struct EccBobCryptodataMessage *msg;
358 struct GNUNET_MQ_Envelope *e; 360 struct GNUNET_MQ_Envelope *e;
359 361
360 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Sending response to Alice\n"); 363 "Sending response to Alice\n");
362 e = GNUNET_MQ_msg(msg, 364 e = GNUNET_MQ_msg (msg,
363 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA); 365 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_BOB_CRYPTODATA);
364 msg->contained_element_count = htonl(2); 366 msg->contained_element_count = htonl (2);
365 if (NULL != s->prod_g_i_b_i) 367 if (NULL != s->prod_g_i_b_i)
366 GNUNET_CRYPTO_ecc_point_to_bin(edc, 368 GNUNET_CRYPTO_ecc_point_to_bin (edc,
367 s->prod_g_i_b_i, 369 s->prod_g_i_b_i,
368 &msg->prod_g_i_b_i); 370 &msg->prod_g_i_b_i);
369 if (NULL != s->prod_h_i_b_i) 371 if (NULL != s->prod_h_i_b_i)
370 GNUNET_CRYPTO_ecc_point_to_bin(edc, 372 GNUNET_CRYPTO_ecc_point_to_bin (edc,
371 s->prod_h_i_b_i, 373 s->prod_h_i_b_i,
372 &msg->prod_h_i_b_i); 374 &msg->prod_h_i_b_i);
373 GNUNET_MQ_notify_sent(e, 375 GNUNET_MQ_notify_sent (e,
374 &bob_cadet_done_cb, 376 &bob_cadet_done_cb,
375 s); 377 s);
376 GNUNET_MQ_send(s->cadet_mq, 378 GNUNET_MQ_send (s->cadet_mq,
377 e); 379 e);
378} 380}
379 381
380 382
@@ -388,21 +390,21 @@ transmit_bobs_cryptodata_message(struct BobServiceSession *s)
388 * TODO: code duplication with Alice! 390 * TODO: code duplication with Alice!
389 */ 391 */
390static int 392static int
391copy_element_cb(void *cls, 393copy_element_cb (void *cls,
392 const struct GNUNET_HashCode *key, 394 const struct GNUNET_HashCode *key,
393 void *value) 395 void *value)
394{ 396{
395 struct BobServiceSession *s = cls; 397 struct BobServiceSession *s = cls;
396 struct GNUNET_SCALARPRODUCT_Element *e = value; 398 struct GNUNET_SCALARPRODUCT_Element *e = value;
397 gcry_mpi_t mval; 399 gcry_mpi_t mval;
398 int64_t val; 400 int64_t val;
399 401
400 mval = gcry_mpi_new(0); 402 mval = gcry_mpi_new (0);
401 val = (int64_t)GNUNET_ntohll(e->value); 403 val = (int64_t) GNUNET_ntohll (e->value);
402 if (0 > val) 404 if (0 > val)
403 gcry_mpi_sub_ui(mval, mval, -val); 405 gcry_mpi_sub_ui (mval, mval, -val);
404 else 406 else
405 gcry_mpi_add_ui(mval, mval, val); 407 gcry_mpi_add_ui (mval, mval, val);
406 s->sorted_elements [s->used_element_count].value = mval; 408 s->sorted_elements [s->used_element_count].value = mval;
407 s->sorted_elements [s->used_element_count].key = &e->key; 409 s->sorted_elements [s->used_element_count].key = &e->key;
408 s->used_element_count++; 410 s->used_element_count++;
@@ -419,14 +421,14 @@ copy_element_cb(void *cls,
419 * TODO: code duplication with Alice! 421 * TODO: code duplication with Alice!
420 */ 422 */
421static int 423static int
422element_cmp(const void *a, 424element_cmp (const void *a,
423 const void *b) 425 const void *b)
424{ 426{
425 const struct MpiElement *ma = a; 427 const struct MpiElement *ma = a;
426 const struct MpiElement *mb = b; 428 const struct MpiElement *mb = b;
427 429
428 return GNUNET_CRYPTO_hash_cmp(ma->key, 430 return GNUNET_CRYPTO_hash_cmp (ma->key,
429 mb->key); 431 mb->key);
430} 432}
431 433
432 434
@@ -440,8 +442,8 @@ element_cmp(const void *a,
440 * #GNUNET_SYSERR to close it (signal serious error) 442 * #GNUNET_SYSERR to close it (signal serious error)
441 */ 443 */
442static int 444static int
443check_alices_cryptodata_message(void *cls, 445check_alices_cryptodata_message (void *cls,
444 const struct EccAliceCryptodataMessage *msg) 446 const struct EccAliceCryptodataMessage *msg)
445{ 447{
446 struct BobServiceSession *s = cls; 448 struct BobServiceSession *s = cls;
447 uint32_t contained_elements; 449 uint32_t contained_elements;
@@ -449,26 +451,26 @@ check_alices_cryptodata_message(void *cls,
449 uint16_t msize; 451 uint16_t msize;
450 unsigned int max; 452 unsigned int max;
451 453
452 msize = ntohs(msg->header.size); 454 msize = ntohs (msg->header.size);
453 if (msize <= sizeof(struct EccAliceCryptodataMessage)) 455 if (msize <= sizeof(struct EccAliceCryptodataMessage))
454 { 456 {
455 GNUNET_break_op(0); 457 GNUNET_break_op (0);
456 return GNUNET_SYSERR; 458 return GNUNET_SYSERR;
457 } 459 }
458 contained_elements = ntohl(msg->contained_element_count); 460 contained_elements = ntohl (msg->contained_element_count);
459 /* Our intersection may still be ongoing, but this is nevertheless 461 /* Our intersection may still be ongoing, but this is nevertheless
460 an upper bound on the required array size */ 462 an upper bound on the required array size */
461 max = GNUNET_CONTAINER_multihashmap_size(s->intersected_elements); 463 max = GNUNET_CONTAINER_multihashmap_size (s->intersected_elements);
462 msg_length = sizeof(struct EccAliceCryptodataMessage) 464 msg_length = sizeof(struct EccAliceCryptodataMessage)
463 + contained_elements * sizeof(struct GNUNET_CRYPTO_EccPoint) * 2; 465 + contained_elements * sizeof(struct GNUNET_CRYPTO_EccPoint) * 2;
464 if ((msize != msg_length) || 466 if ((msize != msg_length) ||
465 (0 == contained_elements) || 467 (0 == contained_elements) ||
466 (contained_elements > UINT16_MAX) || 468 (contained_elements > UINT16_MAX) ||
467 (max < contained_elements + s->cadet_received_element_count)) 469 (max < contained_elements + s->cadet_received_element_count))
468 { 470 {
469 GNUNET_break_op(0); 471 GNUNET_break_op (0);
470 return GNUNET_SYSERR; 472 return GNUNET_SYSERR;
471 } 473 }
472 return GNUNET_OK; 474 return GNUNET_OK;
473} 475}
474 476
@@ -481,8 +483,8 @@ check_alices_cryptodata_message(void *cls,
481 * @param msg the actual message 483 * @param msg the actual message
482 */ 484 */
483static void 485static void
484handle_alices_cryptodata_message(void *cls, 486handle_alices_cryptodata_message (void *cls,
485 const struct EccAliceCryptodataMessage *msg) 487 const struct EccAliceCryptodataMessage *msg)
486{ 488{
487 struct BobServiceSession *s = cls; 489 struct BobServiceSession *s = cls;
488 const struct GNUNET_CRYPTO_EccPoint *payload; 490 const struct GNUNET_CRYPTO_EccPoint *payload;
@@ -496,77 +498,78 @@ handle_alices_cryptodata_message(void *cls,
496 gcry_mpi_point_t g_i_b_i; 498 gcry_mpi_point_t g_i_b_i;
497 gcry_mpi_point_t h_i_b_i; 499 gcry_mpi_point_t h_i_b_i;
498 500
499 contained_elements = ntohl(msg->contained_element_count); 501 contained_elements = ntohl (msg->contained_element_count);
500 max = GNUNET_CONTAINER_multihashmap_size(s->intersected_elements); 502 max = GNUNET_CONTAINER_multihashmap_size (s->intersected_elements);
501 /* sort our vector for the computation */ 503 /* sort our vector for the computation */
502 if (NULL == s->sorted_elements) 504 if (NULL == s->sorted_elements)
503 { 505 {
504 s->sorted_elements 506 s->sorted_elements
505 = GNUNET_new_array(GNUNET_CONTAINER_multihashmap_size(s->intersected_elements), 507 = GNUNET_new_array (GNUNET_CONTAINER_multihashmap_size (
506 struct MpiElement); 508 s->intersected_elements),
507 s->used_element_count = 0; 509 struct MpiElement);
508 GNUNET_CONTAINER_multihashmap_iterate(s->intersected_elements, 510 s->used_element_count = 0;
509 &copy_element_cb, 511 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
510 s); 512 &copy_element_cb,
511 qsort(s->sorted_elements, 513 s);
512 s->used_element_count, 514 qsort (s->sorted_elements,
513 sizeof(struct MpiElement), 515 s->used_element_count,
514 &element_cmp); 516 sizeof(struct MpiElement),
515 } 517 &element_cmp);
516 518 }
517 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 519
518 "Received %u crypto values from Alice\n", 520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 (unsigned int)contained_elements); 521 "Received %u crypto values from Alice\n",
520 payload = (const struct GNUNET_CRYPTO_EccPoint *)&msg[1]; 522 (unsigned int) contained_elements);
523 payload = (const struct GNUNET_CRYPTO_EccPoint *) &msg[1];
521 524
522 for (i = 0; i < contained_elements; i++) 525 for (i = 0; i < contained_elements; i++)
526 {
527 b_i = &s->sorted_elements[i + s->cadet_received_element_count];
528 g_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
529 &payload[i * 2]);
530 g_i_b_i = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
531 g_i,
532 b_i->value);
533 gcry_mpi_point_release (g_i);
534 h_i = GNUNET_CRYPTO_ecc_bin_to_point (edc,
535 &payload[i * 2 + 1]);
536 h_i_b_i = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
537 h_i,
538 b_i->value);
539 gcry_mpi_point_release (h_i);
540 if (0 == i + s->cadet_received_element_count)
541 {
542 /* first iteration, nothing to add */
543 s->prod_g_i_b_i = g_i_b_i;
544 s->prod_h_i_b_i = h_i_b_i;
545 }
546 else
523 { 547 {
524 b_i = &s->sorted_elements[i + s->cadet_received_element_count]; 548 /* further iterations, cummulate resulting value */
525 g_i = GNUNET_CRYPTO_ecc_bin_to_point(edc, 549 tmp = GNUNET_CRYPTO_ecc_add (edc,
526 &payload[i * 2]); 550 s->prod_g_i_b_i,
527 g_i_b_i = GNUNET_CRYPTO_ecc_pmul_mpi(edc, 551 g_i_b_i);
528 g_i, 552 gcry_mpi_point_release (s->prod_g_i_b_i);
529 b_i->value); 553 gcry_mpi_point_release (g_i_b_i);
530 gcry_mpi_point_release(g_i); 554 s->prod_g_i_b_i = tmp;
531 h_i = GNUNET_CRYPTO_ecc_bin_to_point(edc, 555 tmp = GNUNET_CRYPTO_ecc_add (edc,
532 &payload[i * 2 + 1]); 556 s->prod_h_i_b_i,
533 h_i_b_i = GNUNET_CRYPTO_ecc_pmul_mpi(edc, 557 h_i_b_i);
534 h_i, 558 gcry_mpi_point_release (s->prod_h_i_b_i);
535 b_i->value); 559 gcry_mpi_point_release (h_i_b_i);
536 gcry_mpi_point_release(h_i); 560 s->prod_h_i_b_i = tmp;
537 if (0 == i + s->cadet_received_element_count)
538 {
539 /* first iteration, nothing to add */
540 s->prod_g_i_b_i = g_i_b_i;
541 s->prod_h_i_b_i = h_i_b_i;
542 }
543 else
544 {
545 /* further iterations, cummulate resulting value */
546 tmp = GNUNET_CRYPTO_ecc_add(edc,
547 s->prod_g_i_b_i,
548 g_i_b_i);
549 gcry_mpi_point_release(s->prod_g_i_b_i);
550 gcry_mpi_point_release(g_i_b_i);
551 s->prod_g_i_b_i = tmp;
552 tmp = GNUNET_CRYPTO_ecc_add(edc,
553 s->prod_h_i_b_i,
554 h_i_b_i);
555 gcry_mpi_point_release(s->prod_h_i_b_i);
556 gcry_mpi_point_release(h_i_b_i);
557 s->prod_h_i_b_i = tmp;
558 }
559 } 561 }
562 }
560 s->cadet_received_element_count += contained_elements; 563 s->cadet_received_element_count += contained_elements;
561 if ((s->cadet_received_element_count == max) && 564 if ((s->cadet_received_element_count == max) &&
562 (NULL == s->intersection_op)) 565 (NULL == s->intersection_op))
563 { 566 {
564 /* intersection has finished also on our side, and 567 /* intersection has finished also on our side, and
565 we got the full set, so we can proceed with the 568 we got the full set, so we can proceed with the
566 CADET response(s) */ 569 CADET response(s) */
567 transmit_bobs_cryptodata_message(s); 570 transmit_bobs_cryptodata_message (s);
568 } 571 }
569 GNUNET_CADET_receive_done(s->channel); 572 GNUNET_CADET_receive_done (s->channel);
570} 573}
571 574
572 575
@@ -580,71 +583,72 @@ handle_alices_cryptodata_message(void *cls,
580 * @param status what has happened with the set intersection? 583 * @param status what has happened with the set intersection?
581 */ 584 */
582static void 585static void
583cb_intersection_element_removed(void *cls, 586cb_intersection_element_removed (void *cls,
584 const struct GNUNET_SET_Element *element, 587 const struct GNUNET_SET_Element *element,
585 uint64_t current_size, 588 uint64_t current_size,
586 enum GNUNET_SET_Status status) 589 enum GNUNET_SET_Status status)
587{ 590{
588 struct BobServiceSession *s = cls; 591 struct BobServiceSession *s = cls;
589 struct GNUNET_SCALARPRODUCT_Element *se; 592 struct GNUNET_SCALARPRODUCT_Element *se;
590 593
591 switch (status) 594 switch (status)
595 {
596 case GNUNET_SET_STATUS_OK:
597 /* this element has been removed from the set */
598 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
599 element->data);
600 GNUNET_assert (NULL != se);
601 LOG (GNUNET_ERROR_TYPE_DEBUG,
602 "Removed element with key %s and value %lld\n",
603 GNUNET_h2s (&se->key),
604 (long long) GNUNET_ntohll (se->value));
605 GNUNET_assert (GNUNET_YES ==
606 GNUNET_CONTAINER_multihashmap_remove (
607 s->intersected_elements,
608 element->data,
609 se));
610 GNUNET_free (se);
611 return;
612
613 case GNUNET_SET_STATUS_DONE:
614 s->intersection_op = NULL;
615 GNUNET_break (NULL == s->intersection_set);
616 GNUNET_CADET_receive_done (s->channel);
617 LOG (GNUNET_ERROR_TYPE_DEBUG,
618 "Finished intersection, %d items remain\n",
619 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements));
620 if (s->client_received_element_count ==
621 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements))
592 { 622 {
593 case GNUNET_SET_STATUS_OK: 623 /* CADET transmission from Alice is also already done,
594 /* this element has been removed from the set */ 624 start with our own reply */
595 se = GNUNET_CONTAINER_multihashmap_get(s->intersected_elements, 625 transmit_bobs_cryptodata_message (s);
596 element->data);
597 GNUNET_assert(NULL != se);
598 LOG(GNUNET_ERROR_TYPE_DEBUG,
599 "Removed element with key %s and value %lld\n",
600 GNUNET_h2s(&se->key),
601 (long long)GNUNET_ntohll(se->value));
602 GNUNET_assert(GNUNET_YES ==
603 GNUNET_CONTAINER_multihashmap_remove(s->intersected_elements,
604 element->data,
605 se));
606 GNUNET_free(se);
607 return;
608
609 case GNUNET_SET_STATUS_DONE:
610 s->intersection_op = NULL;
611 GNUNET_break(NULL == s->intersection_set);
612 GNUNET_CADET_receive_done(s->channel);
613 LOG(GNUNET_ERROR_TYPE_DEBUG,
614 "Finished intersection, %d items remain\n",
615 GNUNET_CONTAINER_multihashmap_size(s->intersected_elements));
616 if (s->client_received_element_count ==
617 GNUNET_CONTAINER_multihashmap_size(s->intersected_elements))
618 {
619 /* CADET transmission from Alice is also already done,
620 start with our own reply */
621 transmit_bobs_cryptodata_message(s);
622 }
623 return;
624
625 case GNUNET_SET_STATUS_HALF_DONE:
626 /* unexpected for intersection */
627 GNUNET_break(0);
628 return;
629
630 case GNUNET_SET_STATUS_FAILURE:
631 /* unhandled status code */
632 LOG(GNUNET_ERROR_TYPE_DEBUG,
633 "Set intersection failed!\n");
634 s->intersection_op = NULL;
635 if (NULL != s->intersection_set)
636 {
637 GNUNET_SET_destroy(s->intersection_set);
638 s->intersection_set = NULL;
639 }
640 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
641 prepare_client_end_notification(s);
642 return;
643
644 default:
645 GNUNET_break(0);
646 return;
647 } 626 }
627 return;
628
629 case GNUNET_SET_STATUS_HALF_DONE:
630 /* unexpected for intersection */
631 GNUNET_break (0);
632 return;
633
634 case GNUNET_SET_STATUS_FAILURE:
635 /* unhandled status code */
636 LOG (GNUNET_ERROR_TYPE_DEBUG,
637 "Set intersection failed!\n");
638 s->intersection_op = NULL;
639 if (NULL != s->intersection_set)
640 {
641 GNUNET_SET_destroy (s->intersection_set);
642 s->intersection_set = NULL;
643 }
644 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
645 prepare_client_end_notification (s);
646 return;
647
648 default:
649 GNUNET_break (0);
650 return;
651 }
648} 652}
649 653
650 654
@@ -655,36 +659,36 @@ cb_intersection_element_removed(void *cls,
655 * @param s client session to start intersection for 659 * @param s client session to start intersection for
656 */ 660 */
657static void 661static void
658start_intersection(struct BobServiceSession *s) 662start_intersection (struct BobServiceSession *s)
659{ 663{
660 struct GNUNET_HashCode set_sid; 664 struct GNUNET_HashCode set_sid;
661 665
662 GNUNET_CRYPTO_hash(&s->session_id, 666 GNUNET_CRYPTO_hash (&s->session_id,
663 sizeof(struct GNUNET_HashCode), 667 sizeof(struct GNUNET_HashCode),
664 &set_sid); 668 &set_sid);
665 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "Got session with key %s and %u elements, starting intersection.\n", 670 "Got session with key %s and %u elements, starting intersection.\n",
667 GNUNET_h2s(&s->session_id), 671 GNUNET_h2s (&s->session_id),
668 (unsigned int)s->total); 672 (unsigned int) s->total);
669 673
670 s->intersection_op 674 s->intersection_op
671 = GNUNET_SET_prepare(&s->peer, 675 = GNUNET_SET_prepare (&s->peer,
672 &set_sid, 676 &set_sid,
673 NULL, 677 NULL,
674 GNUNET_SET_RESULT_REMOVED, 678 GNUNET_SET_RESULT_REMOVED,
675 (struct GNUNET_SET_Option[]) { { 0 } }, 679 (struct GNUNET_SET_Option[]) { { 0 } },
676 &cb_intersection_element_removed, 680 &cb_intersection_element_removed,
677 s); 681 s);
678 if (GNUNET_OK != 682 if (GNUNET_OK !=
679 GNUNET_SET_commit(s->intersection_op, 683 GNUNET_SET_commit (s->intersection_op,
680 s->intersection_set)) 684 s->intersection_set))
681 { 685 {
682 GNUNET_break(0); 686 GNUNET_break (0);
683 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 687 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
684 prepare_client_end_notification(s); 688 prepare_client_end_notification (s);
685 return; 689 return;
686 } 690 }
687 GNUNET_SET_destroy(s->intersection_set); 691 GNUNET_SET_destroy (s->intersection_set);
688 s->intersection_set = NULL; 692 s->intersection_set = NULL;
689} 693}
690 694
@@ -696,21 +700,21 @@ start_intersection(struct BobServiceSession *s)
696 * @param msg the actual message 700 * @param msg the actual message
697 */ 701 */
698static void 702static void
699handle_alices_computation_request(void *cls, 703handle_alices_computation_request (void *cls,
700 const struct EccServiceRequestMessage *msg) 704 const struct EccServiceRequestMessage *msg)
701{ 705{
702 struct BobServiceSession *s = cls; 706 struct BobServiceSession *s = cls;
703 707
704 s->session_id = msg->session_id; // ?? 708 s->session_id = msg->session_id; // ??
705 if (s->client_received_element_count < s->total) 709 if (s->client_received_element_count < s->total)
706 { 710 {
707 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Alice ready, still waiting for Bob client data!\n"); 712 "Alice ready, still waiting for Bob client data!\n");
709 return; 713 return;
710 } 714 }
711 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Both ready, launching intersection!\n"); 716 "Both ready, launching intersection!\n");
713 start_intersection(s); 717 start_intersection (s);
714} 718}
715 719
716 720
@@ -725,20 +729,20 @@ handle_alices_computation_request(void *cls,
725 * @return session associated with the channel 729 * @return session associated with the channel
726 */ 730 */
727static void * 731static void *
728cb_channel_incoming(void *cls, 732cb_channel_incoming (void *cls,
729 struct GNUNET_CADET_Channel *channel, 733 struct GNUNET_CADET_Channel *channel,
730 const struct GNUNET_PeerIdentity *initiator) 734 const struct GNUNET_PeerIdentity *initiator)
731{ 735{
732 struct BobServiceSession *s = cls; 736 struct BobServiceSession *s = cls;
733 737
734 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 "New incoming channel from peer %s.\n", 739 "New incoming channel from peer %s.\n",
736 GNUNET_i2s(initiator)); 740 GNUNET_i2s (initiator));
737 GNUNET_CADET_close_port(s->port); 741 GNUNET_CADET_close_port (s->port);
738 s->port = NULL; 742 s->port = NULL;
739 s->peer = *initiator; 743 s->peer = *initiator;
740 s->channel = channel; 744 s->channel = channel;
741 s->cadet_mq = GNUNET_CADET_get_mq(s->channel); 745 s->cadet_mq = GNUNET_CADET_get_mq (s->channel);
742 return s; 746 return s;
743} 747}
744 748
@@ -751,25 +755,28 @@ cb_channel_incoming(void *cls,
751 * @return #GNUNET_OK if @a msg is well-formed 755 * @return #GNUNET_OK if @a msg is well-formed
752 */ 756 */
753static int 757static int
754check_bob_client_message_multipart(void *cls, 758check_bob_client_message_multipart (void *cls,
755 const struct ComputationBobCryptodataMultipartMessage *msg) 759 const struct
760 ComputationBobCryptodataMultipartMessage *
761 msg)
756{ 762{
757 struct BobServiceSession *s = cls; 763 struct BobServiceSession *s = cls;
758 uint32_t contained_count; 764 uint32_t contained_count;
759 uint16_t msize; 765 uint16_t msize;
760 766
761 msize = ntohs(msg->header.size); 767 msize = ntohs (msg->header.size);
762 contained_count = ntohl(msg->element_count_contained); 768 contained_count = ntohl (msg->element_count_contained);
763 if ((msize != (sizeof(struct ComputationBobCryptodataMultipartMessage) + 769 if ((msize != (sizeof(struct ComputationBobCryptodataMultipartMessage)
764 contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element))) || 770 + contained_count * sizeof(struct
771 GNUNET_SCALARPRODUCT_Element))) ||
765 (0 == contained_count) || 772 (0 == contained_count) ||
766 (UINT16_MAX < contained_count) || 773 (UINT16_MAX < contained_count) ||
767 (s->total == s->client_received_element_count) || 774 (s->total == s->client_received_element_count) ||
768 (s->total < s->client_received_element_count + contained_count)) 775 (s->total < s->client_received_element_count + contained_count))
769 { 776 {
770 GNUNET_break(0); 777 GNUNET_break (0);
771 return GNUNET_SYSERR; 778 return GNUNET_SYSERR;
772 } 779 }
773 return GNUNET_OK; 780 return GNUNET_OK;
774} 781}
775 782
@@ -782,8 +789,10 @@ check_bob_client_message_multipart(void *cls,
782 * @param msg the actual message 789 * @param msg the actual message
783 */ 790 */
784static void 791static void
785handle_bob_client_message_multipart(void *cls, 792handle_bob_client_message_multipart (void *cls,
786 const struct ComputationBobCryptodataMultipartMessage *msg) 793 const struct
794 ComputationBobCryptodataMultipartMessage *
795 msg)
787{ 796{
788 struct BobServiceSession *s = cls; 797 struct BobServiceSession *s = cls;
789 uint32_t contained_count; 798 uint32_t contained_count;
@@ -791,50 +800,50 @@ handle_bob_client_message_multipart(void *cls,
791 struct GNUNET_SET_Element set_elem; 800 struct GNUNET_SET_Element set_elem;
792 struct GNUNET_SCALARPRODUCT_Element *elem; 801 struct GNUNET_SCALARPRODUCT_Element *elem;
793 802
794 contained_count = ntohl(msg->element_count_contained); 803 contained_count = ntohl (msg->element_count_contained);
795 elements = (const struct GNUNET_SCALARPRODUCT_Element *)&msg[1]; 804 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
796 for (uint32_t i = 0; i < contained_count; i++) 805 for (uint32_t i = 0; i < contained_count; i++)
806 {
807 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
808 GNUNET_memcpy (elem,
809 &elements[i],
810 sizeof(struct GNUNET_SCALARPRODUCT_Element));
811 if (GNUNET_SYSERR ==
812 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
813 &elem->key,
814 elem,
815 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
797 { 816 {
798 elem = GNUNET_new(struct GNUNET_SCALARPRODUCT_Element); 817 GNUNET_break (0);
799 GNUNET_memcpy(elem, 818 GNUNET_free (elem);
800 &elements[i], 819 continue;
801 sizeof(struct GNUNET_SCALARPRODUCT_Element));
802 if (GNUNET_SYSERR ==
803 GNUNET_CONTAINER_multihashmap_put(s->intersected_elements,
804 &elem->key,
805 elem,
806 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
807 {
808 GNUNET_break(0);
809 GNUNET_free(elem);
810 continue;
811 }
812 set_elem.data = &elem->key;
813 set_elem.size = sizeof(elem->key);
814 set_elem.element_type = 0;
815 GNUNET_SET_add_element(s->intersection_set,
816 &set_elem,
817 NULL, NULL);
818 } 820 }
821 set_elem.data = &elem->key;
822 set_elem.size = sizeof(elem->key);
823 set_elem.element_type = 0;
824 GNUNET_SET_add_element (s->intersection_set,
825 &set_elem,
826 NULL, NULL);
827 }
819 s->client_received_element_count += contained_count; 828 s->client_received_element_count += contained_count;
820 GNUNET_SERVICE_client_continue(s->client); 829 GNUNET_SERVICE_client_continue (s->client);
821 if (s->total != s->client_received_element_count) 830 if (s->total != s->client_received_element_count)
822 { 831 {
823 /* more to come */ 832 /* more to come */
824 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
825 "Request still partial, waiting for more client data!\n"); 834 "Request still partial, waiting for more client data!\n");
826 return; 835 return;
827 } 836 }
828 if (NULL == s->channel) 837 if (NULL == s->channel)
829 { 838 {
830 /* no Alice waiting for this request, wait for Alice */ 839 /* no Alice waiting for this request, wait for Alice */
831 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "Client ready, still waiting for Alice!\n"); 841 "Client ready, still waiting for Alice!\n");
833 return; 842 return;
834 } 843 }
835 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836 "Both ready, launching intersection!\n"); 845 "Both ready, launching intersection!\n");
837 start_intersection(s); 846 start_intersection (s);
838} 847}
839 848
840 849
@@ -847,8 +856,8 @@ handle_bob_client_message_multipart(void *cls,
847 * @return #GNUNET_OK if @a msg is well-formed 856 * @return #GNUNET_OK if @a msg is well-formed
848 */ 857 */
849static int 858static int
850check_bob_client_message(void *cls, 859check_bob_client_message (void *cls,
851 const struct BobComputationMessage *msg) 860 const struct BobComputationMessage *msg)
852{ 861{
853 struct BobServiceSession *s = cls; 862 struct BobServiceSession *s = cls;
854 uint32_t contained_count; 863 uint32_t contained_count;
@@ -856,22 +865,23 @@ check_bob_client_message(void *cls,
856 uint16_t msize; 865 uint16_t msize;
857 866
858 if (GNUNET_SCALARPRODUCT_STATUS_INIT != s->status) 867 if (GNUNET_SCALARPRODUCT_STATUS_INIT != s->status)
859 { 868 {
860 GNUNET_break(0); 869 GNUNET_break (0);
861 return GNUNET_SYSERR; 870 return GNUNET_SYSERR;
862 } 871 }
863 msize = ntohs(msg->header.size); 872 msize = ntohs (msg->header.size);
864 total_count = ntohl(msg->element_count_total); 873 total_count = ntohl (msg->element_count_total);
865 contained_count = ntohl(msg->element_count_contained); 874 contained_count = ntohl (msg->element_count_contained);
866 if ((0 == total_count) || 875 if ((0 == total_count) ||
867 (0 == contained_count) || 876 (0 == contained_count) ||
868 (UINT16_MAX < contained_count) || 877 (UINT16_MAX < contained_count) ||
869 (msize != (sizeof(struct BobComputationMessage) + 878 (msize != (sizeof(struct BobComputationMessage)
870 contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element)))) 879 + contained_count * sizeof(struct
871 { 880 GNUNET_SCALARPRODUCT_Element))))
872 GNUNET_break_op(0); 881 {
873 return GNUNET_SYSERR; 882 GNUNET_break_op (0);
874 } 883 return GNUNET_SYSERR;
884 }
875 return GNUNET_OK; 885 return GNUNET_OK;
876} 886}
877 887
@@ -885,20 +895,20 @@ check_bob_client_message(void *cls,
885 * @param msg the actual message 895 * @param msg the actual message
886 */ 896 */
887static void 897static void
888handle_bob_client_message(void *cls, 898handle_bob_client_message (void *cls,
889 const struct BobComputationMessage *msg) 899 const struct BobComputationMessage *msg)
890{ 900{
891 struct BobServiceSession *s = cls; 901 struct BobServiceSession *s = cls;
892 struct GNUNET_MQ_MessageHandler cadet_handlers[] = { 902 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
893 GNUNET_MQ_hd_fixed_size(alices_computation_request, 903 GNUNET_MQ_hd_fixed_size (alices_computation_request,
894 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION, 904 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_SESSION_INITIALIZATION,
895 struct EccServiceRequestMessage, 905 struct EccServiceRequestMessage,
896 s), 906 s),
897 GNUNET_MQ_hd_var_size(alices_cryptodata_message, 907 GNUNET_MQ_hd_var_size (alices_cryptodata_message,
898 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA, 908 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ECC_ALICE_CRYPTODATA,
899 struct EccAliceCryptodataMessage, 909 struct EccAliceCryptodataMessage,
900 s), 910 s),
901 GNUNET_MQ_handler_end() 911 GNUNET_MQ_handler_end ()
902 }; 912 };
903 uint32_t contained_count; 913 uint32_t contained_count;
904 uint32_t total_count; 914 uint32_t total_count;
@@ -906,63 +916,63 @@ handle_bob_client_message(void *cls,
906 struct GNUNET_SET_Element set_elem; 916 struct GNUNET_SET_Element set_elem;
907 struct GNUNET_SCALARPRODUCT_Element *elem; 917 struct GNUNET_SCALARPRODUCT_Element *elem;
908 918
909 total_count = ntohl(msg->element_count_total); 919 total_count = ntohl (msg->element_count_total);
910 contained_count = ntohl(msg->element_count_contained); 920 contained_count = ntohl (msg->element_count_contained);
911 921
912 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE; 922 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
913 s->total = total_count; 923 s->total = total_count;
914 s->client_received_element_count = contained_count; 924 s->client_received_element_count = contained_count;
915 s->session_id = msg->session_key; 925 s->session_id = msg->session_key;
916 elements = (const struct GNUNET_SCALARPRODUCT_Element *)&msg[1]; 926 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
917 s->intersected_elements 927 s->intersected_elements
918 = GNUNET_CONTAINER_multihashmap_create(s->total, 928 = GNUNET_CONTAINER_multihashmap_create (s->total,
919 GNUNET_YES); 929 GNUNET_YES);
920 s->intersection_set 930 s->intersection_set
921 = GNUNET_SET_create(cfg, 931 = GNUNET_SET_create (cfg,
922 GNUNET_SET_OPERATION_INTERSECTION); 932 GNUNET_SET_OPERATION_INTERSECTION);
923 for (uint32_t i = 0; i < contained_count; i++) 933 for (uint32_t i = 0; i < contained_count; i++)
934 {
935 if (0 == GNUNET_ntohll (elements[i].value))
936 continue;
937 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
938 GNUNET_memcpy (elem,
939 &elements[i],
940 sizeof(struct GNUNET_SCALARPRODUCT_Element));
941 if (GNUNET_SYSERR ==
942 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
943 &elem->key,
944 elem,
945 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
924 { 946 {
925 if (0 == GNUNET_ntohll(elements[i].value)) 947 GNUNET_break (0);
926 continue; 948 GNUNET_free (elem);
927 elem = GNUNET_new(struct GNUNET_SCALARPRODUCT_Element); 949 continue;
928 GNUNET_memcpy(elem,
929 &elements[i],
930 sizeof(struct GNUNET_SCALARPRODUCT_Element));
931 if (GNUNET_SYSERR ==
932 GNUNET_CONTAINER_multihashmap_put(s->intersected_elements,
933 &elem->key,
934 elem,
935 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
936 {
937 GNUNET_break(0);
938 GNUNET_free(elem);
939 continue;
940 }
941 set_elem.data = &elem->key;
942 set_elem.size = sizeof(elem->key);
943 set_elem.element_type = 0;
944 GNUNET_SET_add_element(s->intersection_set,
945 &set_elem,
946 NULL, NULL);
947 s->used_element_count++;
948 } 950 }
949 GNUNET_SERVICE_client_continue(s->client); 951 set_elem.data = &elem->key;
950 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 952 set_elem.size = sizeof(elem->key);
951 "Received client request, opening port %s!\n", 953 set_elem.element_type = 0;
952 GNUNET_h2s(&msg->session_key)); 954 GNUNET_SET_add_element (s->intersection_set,
953 s->port = GNUNET_CADET_open_port(my_cadet, 955 &set_elem,
954 &msg->session_key, 956 NULL, NULL);
955 &cb_channel_incoming, 957 s->used_element_count++;
956 s, 958 }
957 NULL, 959 GNUNET_SERVICE_client_continue (s->client);
958 &cb_channel_destruction, 960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 cadet_handlers); 961 "Received client request, opening port %s!\n",
962 GNUNET_h2s (&msg->session_key));
963 s->port = GNUNET_CADET_open_port (my_cadet,
964 &msg->session_key,
965 &cb_channel_incoming,
966 s,
967 NULL,
968 &cb_channel_destruction,
969 cadet_handlers);
960 if (NULL == s->port) 970 if (NULL == s->port)
961 { 971 {
962 GNUNET_break(0); 972 GNUNET_break (0);
963 GNUNET_SERVICE_client_drop(s->client); 973 GNUNET_SERVICE_client_drop (s->client);
964 return; 974 return;
965 } 975 }
966} 976}
967 977
968 978
@@ -972,21 +982,21 @@ handle_bob_client_message(void *cls,
972 * @param cls unused 982 * @param cls unused
973 */ 983 */
974static void 984static void
975shutdown_task(void *cls) 985shutdown_task (void *cls)
976{ 986{
977 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Shutting down, initiating cleanup.\n"); 988 "Shutting down, initiating cleanup.\n");
979 // FIXME: we have to cut our connections to CADET first! 989 // FIXME: we have to cut our connections to CADET first!
980 if (NULL != my_cadet) 990 if (NULL != my_cadet)
981 { 991 {
982 GNUNET_CADET_disconnect(my_cadet); 992 GNUNET_CADET_disconnect (my_cadet);
983 my_cadet = NULL; 993 my_cadet = NULL;
984 } 994 }
985 if (NULL != edc) 995 if (NULL != edc)
986 { 996 {
987 GNUNET_CRYPTO_ecc_dlog_release(edc); 997 GNUNET_CRYPTO_ecc_dlog_release (edc);
988 edc = NULL; 998 edc = NULL;
989 } 999 }
990} 1000}
991 1001
992 1002
@@ -1001,13 +1011,13 @@ shutdown_task(void *cls)
1001 * @return our `struct BobServiceSession` 1011 * @return our `struct BobServiceSession`
1002 */ 1012 */
1003static void * 1013static void *
1004client_connect_cb(void *cls, 1014client_connect_cb (void *cls,
1005 struct GNUNET_SERVICE_Client *client, 1015 struct GNUNET_SERVICE_Client *client,
1006 struct GNUNET_MQ_Handle *mq) 1016 struct GNUNET_MQ_Handle *mq)
1007{ 1017{
1008 struct BobServiceSession *s; 1018 struct BobServiceSession *s;
1009 1019
1010 s = GNUNET_new(struct BobServiceSession); 1020 s = GNUNET_new (struct BobServiceSession);
1011 s->client = client; 1021 s->client = client;
1012 s->client_mq = mq; 1022 s->client_mq = mq;
1013 return s; 1023 return s;
@@ -1025,16 +1035,16 @@ client_connect_cb(void *cls,
1025 * @param app_cls our `struct BobServiceSession` 1035 * @param app_cls our `struct BobServiceSession`
1026 */ 1036 */
1027static void 1037static void
1028client_disconnect_cb(void *cls, 1038client_disconnect_cb (void *cls,
1029 struct GNUNET_SERVICE_Client *client, 1039 struct GNUNET_SERVICE_Client *client,
1030 void *app_cls) 1040 void *app_cls)
1031{ 1041{
1032 struct BobServiceSession *s = app_cls; 1042 struct BobServiceSession *s = app_cls;
1033 1043
1034 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 "Client disconnected from us.\n"); 1045 "Client disconnected from us.\n");
1036 s->client = NULL; 1046 s->client = NULL;
1037 destroy_service_session(s); 1047 destroy_service_session (s);
1038} 1048}
1039 1049
1040 1050
@@ -1046,24 +1056,24 @@ client_disconnect_cb(void *cls,
1046 * @param service the initialized service 1056 * @param service the initialized service
1047 */ 1057 */
1048static void 1058static void
1049run(void *cls, 1059run (void *cls,
1050 const struct GNUNET_CONFIGURATION_Handle *c, 1060 const struct GNUNET_CONFIGURATION_Handle *c,
1051 struct GNUNET_SERVICE_Handle *service) 1061 struct GNUNET_SERVICE_Handle *service)
1052{ 1062{
1053 cfg = c; 1063 cfg = c;
1054 /* We don't really do DLOG, so we can setup with very minimal resources */ 1064 /* We don't really do DLOG, so we can setup with very minimal resources */
1055 edc = GNUNET_CRYPTO_ecc_dlog_prepare(4 /* max value */, 1065 edc = GNUNET_CRYPTO_ecc_dlog_prepare (4 /* max value */,
1056 2 /* RAM */); 1066 2 /* RAM */);
1057 my_cadet = GNUNET_CADET_connect(cfg); 1067 my_cadet = GNUNET_CADET_connect (cfg);
1058 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, 1068 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1059 NULL); 1069 NULL);
1060 if (NULL == my_cadet) 1070 if (NULL == my_cadet)
1061 { 1071 {
1062 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1072 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1063 _("Connect to CADET failed\n")); 1073 _ ("Connect to CADET failed\n"));
1064 GNUNET_SCHEDULER_shutdown(); 1074 GNUNET_SCHEDULER_shutdown ();
1065 return; 1075 return;
1066 } 1076 }
1067} 1077}
1068 1078
1069 1079
@@ -1077,15 +1087,15 @@ GNUNET_SERVICE_MAIN
1077 &client_connect_cb, 1087 &client_connect_cb,
1078 &client_disconnect_cb, 1088 &client_disconnect_cb,
1079 NULL, 1089 NULL,
1080 GNUNET_MQ_hd_var_size(bob_client_message, 1090 GNUNET_MQ_hd_var_size (bob_client_message,
1081 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB, 1091 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB,
1082 struct BobComputationMessage, 1092 struct BobComputationMessage,
1083 NULL), 1093 NULL),
1084 GNUNET_MQ_hd_var_size(bob_client_message_multipart, 1094 GNUNET_MQ_hd_var_size (bob_client_message_multipart,
1085 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB, 1095 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB,
1086 struct ComputationBobCryptodataMultipartMessage, 1096 struct ComputationBobCryptodataMultipartMessage,
1087 NULL), 1097 NULL),
1088 GNUNET_MQ_handler_end()); 1098 GNUNET_MQ_handler_end ());
1089 1099
1090 1100
1091/* end of gnunet-service-scalarproduct-ecc_bob.c */ 1101/* end of gnunet-service-scalarproduct-ecc_bob.c */
diff --git a/src/scalarproduct/gnunet-service-scalarproduct.h b/src/scalarproduct/gnunet-service-scalarproduct.h
index d795badf1..f2592bacf 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct.h
+++ b/src/scalarproduct/gnunet-service-scalarproduct.h
@@ -37,7 +37,8 @@ GNUNET_NETWORK_STRUCT_BEGIN
37 * send a `struct AliceCryptodataMessage *`. Bob is not expected 37 * send a `struct AliceCryptodataMessage *`. Bob is not expected
38 * to respond via CADET in the meantime. 38 * to respond via CADET in the meantime.
39 */ 39 */
40struct ServiceRequestMessage { 40struct ServiceRequestMessage
41{
41 /** 42 /**
42 * Type is #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION 43 * Type is #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION
43 */ 44 */
@@ -65,7 +66,8 @@ struct ServiceRequestMessage {
65 * (after set intersection). Alice may send messages of this 66 * (after set intersection). Alice may send messages of this
66 * type repeatedly to transmit all values. 67 * type repeatedly to transmit all values.
67 */ 68 */
68struct AliceCryptodataMessage { 69struct AliceCryptodataMessage
70{
69 /** 71 /**
70 * Type is #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA 72 * Type is #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA
71 */ 73 */
@@ -89,7 +91,8 @@ struct AliceCryptodataMessage {
89 * conversation may be continued with `struct BobCryptodataMultipartMessage` 91 * conversation may be continued with `struct BobCryptodataMultipartMessage`
90 * messages afterwards. 92 * messages afterwards.
91 */ 93 */
92struct BobCryptodataMessage { 94struct BobCryptodataMessage
95{
93 /** 96 /**
94 * GNUNET message header with type 97 * GNUNET message header with type
95 * #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA. 98 * #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA.
@@ -114,7 +117,8 @@ struct BobCryptodataMessage {
114 * Once all k-values have been transmitted, Bob is finished and 117 * Once all k-values have been transmitted, Bob is finished and
115 * Alice can transmit the final result to the client. 118 * Alice can transmit the final result to the client.
116 */ 119 */
117struct BobCryptodataMultipartMessage { 120struct BobCryptodataMultipartMessage
121{
118 /** 122 /**
119 * GNUNET message header 123 * GNUNET message header
120 */ 124 */
diff --git a/src/scalarproduct/gnunet-service-scalarproduct_alice.c b/src/scalarproduct/gnunet-service-scalarproduct_alice.c
index 6e823dca4..66b01c89e 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct_alice.c
+++ b/src/scalarproduct/gnunet-service-scalarproduct_alice.c
@@ -37,12 +37,13 @@
37#include "gnunet-service-scalarproduct.h" 37#include "gnunet-service-scalarproduct.h"
38 38
39#define LOG(kind, ...) \ 39#define LOG(kind, ...) \
40 GNUNET_log_from(kind, "scalarproduct-alice", __VA_ARGS__) 40 GNUNET_log_from (kind, "scalarproduct-alice", __VA_ARGS__)
41 41
42/** 42/**
43 * An encrypted element key-value pair. 43 * An encrypted element key-value pair.
44 */ 44 */
45struct MpiElement { 45struct MpiElement
46{
46 /** 47 /**
47 * Key used to identify matching pairs of values to multiply. 48 * Key used to identify matching pairs of values to multiply.
48 * Points into an existing data structure, to avoid copying 49 * Points into an existing data structure, to avoid copying
@@ -61,7 +62,8 @@ struct MpiElement {
61 * A scalarproduct session which tracks 62 * A scalarproduct session which tracks
62 * a request form the client to our final response. 63 * a request form the client to our final response.
63 */ 64 */
64struct AliceServiceSession { 65struct AliceServiceSession
66{
65 /** 67 /**
66 * (hopefully) unique transaction ID 68 * (hopefully) unique transaction ID
67 */ 69 */
@@ -221,11 +223,11 @@ static struct GNUNET_CADET_Handle *my_cadet;
221 * @return #GNUNET_OK (continue to iterate) 223 * @return #GNUNET_OK (continue to iterate)
222 */ 224 */
223static int 225static int
224free_element_cb(void *cls, const struct GNUNET_HashCode *key, void *value) 226free_element_cb (void *cls, const struct GNUNET_HashCode *key, void *value)
225{ 227{
226 struct GNUNET_SCALARPRODUCT_Element *e = value; 228 struct GNUNET_SCALARPRODUCT_Element *e = value;
227 229
228 GNUNET_free(e); 230 GNUNET_free (e);
229 return GNUNET_OK; 231 return GNUNET_OK;
230} 232}
231 233
@@ -236,69 +238,69 @@ free_element_cb(void *cls, const struct GNUNET_HashCode *key, void *value)
236 * @param s the session to free elements from 238 * @param s the session to free elements from
237 */ 239 */
238static void 240static void
239destroy_service_session(struct AliceServiceSession *s) 241destroy_service_session (struct AliceServiceSession *s)
240{ 242{
241 if (GNUNET_YES == s->in_destroy) 243 if (GNUNET_YES == s->in_destroy)
242 return; 244 return;
243 s->in_destroy = GNUNET_YES; 245 s->in_destroy = GNUNET_YES;
244 if (NULL != s->client) 246 if (NULL != s->client)
245 { 247 {
246 struct GNUNET_SERVICE_Client *c = s->client; 248 struct GNUNET_SERVICE_Client *c = s->client;
247 249
248 s->client = NULL; 250 s->client = NULL;
249 GNUNET_SERVICE_client_drop(c); 251 GNUNET_SERVICE_client_drop (c);
250 } 252 }
251 if (NULL != s->channel) 253 if (NULL != s->channel)
252 { 254 {
253 GNUNET_CADET_channel_destroy(s->channel); 255 GNUNET_CADET_channel_destroy (s->channel);
254 s->channel = NULL; 256 s->channel = NULL;
255 } 257 }
256 if (NULL != s->intersected_elements) 258 if (NULL != s->intersected_elements)
257 { 259 {
258 GNUNET_CONTAINER_multihashmap_iterate(s->intersected_elements, 260 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
259 &free_element_cb, 261 &free_element_cb,
260 s); 262 s);
261 GNUNET_CONTAINER_multihashmap_destroy(s->intersected_elements); 263 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
262 s->intersected_elements = NULL; 264 s->intersected_elements = NULL;
263 } 265 }
264 if (NULL != s->intersection_listen) 266 if (NULL != s->intersection_listen)
265 { 267 {
266 GNUNET_SET_listen_cancel(s->intersection_listen); 268 GNUNET_SET_listen_cancel (s->intersection_listen);
267 s->intersection_listen = NULL; 269 s->intersection_listen = NULL;
268 } 270 }
269 if (NULL != s->intersection_op) 271 if (NULL != s->intersection_op)
270 { 272 {
271 GNUNET_SET_operation_cancel(s->intersection_op); 273 GNUNET_SET_operation_cancel (s->intersection_op);
272 s->intersection_op = NULL; 274 s->intersection_op = NULL;
273 } 275 }
274 if (NULL != s->intersection_set) 276 if (NULL != s->intersection_set)
275 { 277 {
276 GNUNET_SET_destroy(s->intersection_set); 278 GNUNET_SET_destroy (s->intersection_set);
277 s->intersection_set = NULL; 279 s->intersection_set = NULL;
278 } 280 }
279 if (NULL != s->sorted_elements) 281 if (NULL != s->sorted_elements)
280 { 282 {
281 for (unsigned int i = 0; i < s->used_element_count; i++) 283 for (unsigned int i = 0; i < s->used_element_count; i++)
282 gcry_mpi_release(s->sorted_elements[i].value); 284 gcry_mpi_release (s->sorted_elements[i].value);
283 GNUNET_free(s->sorted_elements); 285 GNUNET_free (s->sorted_elements);
284 s->sorted_elements = NULL; 286 s->sorted_elements = NULL;
285 } 287 }
286 if (NULL != s->r) 288 if (NULL != s->r)
287 { 289 {
288 GNUNET_free(s->r); 290 GNUNET_free (s->r);
289 s->r = NULL; 291 s->r = NULL;
290 } 292 }
291 if (NULL != s->r_prime) 293 if (NULL != s->r_prime)
292 { 294 {
293 GNUNET_free(s->r_prime); 295 GNUNET_free (s->r_prime);
294 s->r_prime = NULL; 296 s->r_prime = NULL;
295 } 297 }
296 if (NULL != s->product) 298 if (NULL != s->product)
297 { 299 {
298 gcry_mpi_release(s->product); 300 gcry_mpi_release (s->product);
299 s->product = NULL; 301 s->product = NULL;
300 } 302 }
301 GNUNET_free(s); 303 GNUNET_free (s);
302} 304}
303 305
304 306
@@ -309,22 +311,22 @@ destroy_service_session(struct AliceServiceSession *s)
309 * @param session the associated client session to fail or succeed 311 * @param session the associated client session to fail or succeed
310 */ 312 */
311static void 313static void
312prepare_client_end_notification(struct AliceServiceSession *session) 314prepare_client_end_notification (struct AliceServiceSession *session)
313{ 315{
314 struct ClientResponseMessage *msg; 316 struct ClientResponseMessage *msg;
315 struct GNUNET_MQ_Envelope *e; 317 struct GNUNET_MQ_Envelope *e;
316 318
317 if (NULL == session->client_mq) 319 if (NULL == session->client_mq)
318 return; /* no client left to be notified */ 320 return; /* no client left to be notified */
319 GNUNET_log( 321 GNUNET_log (
320 GNUNET_ERROR_TYPE_DEBUG, 322 GNUNET_ERROR_TYPE_DEBUG,
321 "Sending session-end notification with status %d to client for session %s\n", 323 "Sending session-end notification with status %d to client for session %s\n",
322 session->status, 324 session->status,
323 GNUNET_h2s(&session->session_id)); 325 GNUNET_h2s (&session->session_id));
324 e = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT); 326 e = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
325 msg->product_length = htonl(0); 327 msg->product_length = htonl (0);
326 msg->status = htonl(session->status); 328 msg->status = htonl (session->status);
327 GNUNET_MQ_send(session->client_mq, e); 329 GNUNET_MQ_send (session->client_mq, e);
328} 330}
329 331
330 332
@@ -335,7 +337,7 @@ prepare_client_end_notification(struct AliceServiceSession *session)
335 * @param s the session associated with our client. 337 * @param s the session associated with our client.
336 */ 338 */
337static void 339static void
338transmit_client_response(struct AliceServiceSession *s) 340transmit_client_response (struct AliceServiceSession *s)
339{ 341{
340 struct ClientResponseMessage *msg; 342 struct ClientResponseMessage *msg;
341 struct GNUNET_MQ_Envelope *e; 343 struct GNUNET_MQ_Envelope *e;
@@ -347,56 +349,56 @@ transmit_client_response(struct AliceServiceSession *s)
347 gcry_mpi_t value; 349 gcry_mpi_t value;
348 350
349 if (NULL == s->product) 351 if (NULL == s->product)
350 { 352 {
351 GNUNET_break(0); 353 GNUNET_break (0);
352 prepare_client_end_notification(s); 354 prepare_client_end_notification (s);
353 return; 355 return;
354 } 356 }
355 value = gcry_mpi_new(0); 357 value = gcry_mpi_new (0);
356 sign = gcry_mpi_cmp_ui(s->product, 0); 358 sign = gcry_mpi_cmp_ui (s->product, 0);
357 if (0 > sign) 359 if (0 > sign)
358 { 360 {
359 range = -1; 361 range = -1;
360 gcry_mpi_sub(value, value, s->product); 362 gcry_mpi_sub (value, value, s->product);
361 } 363 }
362 else if (0 < sign) 364 else if (0 < sign)
363 { 365 {
364 range = 1; 366 range = 1;
365 gcry_mpi_add(value, value, s->product); 367 gcry_mpi_add (value, value, s->product);
366 } 368 }
367 else 369 else
368 { 370 {
369 /* result is exactly zero */ 371 /* result is exactly zero */
370 range = 0; 372 range = 0;
371 } 373 }
372 gcry_mpi_release(s->product); 374 gcry_mpi_release (s->product);
373 s->product = NULL; 375 s->product = NULL;
374 376
375 if ((0 != range) && (0 != (rc = gcry_mpi_aprint(GCRYMPI_FMT_STD, 377 if ((0 != range) && (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_STD,
376 &product_exported, 378 &product_exported,
377 &product_length, 379 &product_length,
378 value)))) 380 value))))
379 { 381 {
380 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 382 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
381 prepare_client_end_notification(s); 383 prepare_client_end_notification (s);
382 return; 384 return;
383 } 385 }
384 gcry_mpi_release(value); 386 gcry_mpi_release (value);
385 e = GNUNET_MQ_msg_extra(msg, 387 e = GNUNET_MQ_msg_extra (msg,
386 product_length, 388 product_length,
387 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT); 389 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
388 msg->status = htonl(GNUNET_SCALARPRODUCT_STATUS_SUCCESS); 390 msg->status = htonl (GNUNET_SCALARPRODUCT_STATUS_SUCCESS);
389 msg->range = htonl(range); 391 msg->range = htonl (range);
390 msg->product_length = htonl(product_length); 392 msg->product_length = htonl (product_length);
391 if (NULL != product_exported) 393 if (NULL != product_exported)
392 { 394 {
393 GNUNET_memcpy(&msg[1], product_exported, product_length); 395 GNUNET_memcpy (&msg[1], product_exported, product_length);
394 GNUNET_free(product_exported); 396 GNUNET_free (product_exported);
395 } 397 }
396 GNUNET_MQ_send(s->client_mq, e); 398 GNUNET_MQ_send (s->client_mq, e);
397 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "Sent result to client, session %s has ended!\n", 400 "Sent result to client, session %s has ended!\n",
399 GNUNET_h2s(&s->session_id)); 401 GNUNET_h2s (&s->session_id));
400} 402}
401 403
402 404
@@ -410,20 +412,20 @@ transmit_client_response(struct AliceServiceSession *s)
410 * @param channel connection to the other end (henceforth invalid) 412 * @param channel connection to the other end (henceforth invalid)
411 */ 413 */
412static void 414static void
413cb_channel_destruction(void *cls, const struct GNUNET_CADET_Channel *channel) 415cb_channel_destruction (void *cls, const struct GNUNET_CADET_Channel *channel)
414{ 416{
415 struct AliceServiceSession *s = cls; 417 struct AliceServiceSession *s = cls;
416 418
417 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Peer disconnected, terminating session %s with peer %s\n", 420 "Peer disconnected, terminating session %s with peer %s\n",
419 GNUNET_h2s(&s->session_id), 421 GNUNET_h2s (&s->session_id),
420 GNUNET_i2s(&s->peer)); 422 GNUNET_i2s (&s->peer));
421 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status) 423 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
422 { 424 {
423 /* We didn't get an answer yet, fail with error */ 425 /* We didn't get an answer yet, fail with error */
424 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 426 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
425 prepare_client_end_notification(s); 427 prepare_client_end_notification (s);
426 } 428 }
427 s->channel = NULL; 429 s->channel = NULL;
428} 430}
429 431
@@ -436,21 +438,21 @@ cb_channel_destruction(void *cls, const struct GNUNET_CADET_Channel *channel)
436 * @return an MPI value containing the calculated sum, never NULL 438 * @return an MPI value containing the calculated sum, never NULL
437 */ 439 */
438static gcry_mpi_t 440static gcry_mpi_t
439compute_square_sum_mpi_elements(const struct MpiElement *vector, 441compute_square_sum_mpi_elements (const struct MpiElement *vector,
440 uint32_t length) 442 uint32_t length)
441{ 443{
442 gcry_mpi_t elem; 444 gcry_mpi_t elem;
443 gcry_mpi_t sum; 445 gcry_mpi_t sum;
444 uint32_t i; 446 uint32_t i;
445 447
446 GNUNET_assert(NULL != (sum = gcry_mpi_new(0))); 448 GNUNET_assert (NULL != (sum = gcry_mpi_new (0)));
447 GNUNET_assert(NULL != (elem = gcry_mpi_new(0))); 449 GNUNET_assert (NULL != (elem = gcry_mpi_new (0)));
448 for (i = 0; i < length; i++) 450 for (i = 0; i < length; i++)
449 { 451 {
450 gcry_mpi_mul(elem, vector[i].value, vector[i].value); 452 gcry_mpi_mul (elem, vector[i].value, vector[i].value);
451 gcry_mpi_add(sum, sum, elem); 453 gcry_mpi_add (sum, sum, elem);
452 } 454 }
453 gcry_mpi_release(elem); 455 gcry_mpi_release (elem);
454 return sum; 456 return sum;
455} 457}
456 458
@@ -463,20 +465,20 @@ compute_square_sum_mpi_elements(const struct MpiElement *vector,
463 * @return an MPI value containing the calculated sum, never NULL 465 * @return an MPI value containing the calculated sum, never NULL
464 */ 466 */
465static gcry_mpi_t 467static gcry_mpi_t
466compute_square_sum(const gcry_mpi_t *vector, uint32_t length) 468compute_square_sum (const gcry_mpi_t *vector, uint32_t length)
467{ 469{
468 gcry_mpi_t elem; 470 gcry_mpi_t elem;
469 gcry_mpi_t sum; 471 gcry_mpi_t sum;
470 uint32_t i; 472 uint32_t i;
471 473
472 GNUNET_assert(NULL != (sum = gcry_mpi_new(0))); 474 GNUNET_assert (NULL != (sum = gcry_mpi_new (0)));
473 GNUNET_assert(NULL != (elem = gcry_mpi_new(0))); 475 GNUNET_assert (NULL != (elem = gcry_mpi_new (0)));
474 for (i = 0; i < length; i++) 476 for (i = 0; i < length; i++)
475 { 477 {
476 gcry_mpi_mul(elem, vector[i], vector[i]); 478 gcry_mpi_mul (elem, vector[i], vector[i]);
477 gcry_mpi_add(sum, sum, elem); 479 gcry_mpi_add (sum, sum, elem);
478 } 480 }
479 gcry_mpi_release(elem); 481 gcry_mpi_release (elem);
480 return sum; 482 return sum;
481} 483}
482 484
@@ -488,7 +490,7 @@ compute_square_sum(const gcry_mpi_t *vector, uint32_t length)
488 * @return product as MPI, never NULL 490 * @return product as MPI, never NULL
489 */ 491 */
490static gcry_mpi_t 492static gcry_mpi_t
491compute_scalar_product(struct AliceServiceSession *session) 493compute_scalar_product (struct AliceServiceSession *session)
492{ 494{
493 uint32_t count; 495 uint32_t count;
494 gcry_mpi_t t; 496 gcry_mpi_t t;
@@ -508,80 +510,80 @@ compute_scalar_product(struct AliceServiceSession *session)
508 // from the E(a_pi)(+)E(-b_pi-r_pi) and E(a_qi)(+)E(-r_qi) twice each, 510 // from the E(a_pi)(+)E(-b_pi-r_pi) and E(a_qi)(+)E(-r_qi) twice each,
509 // the result is E((S + a_pi) + (S -b_pi-r_pi)) and E(S + a_qi + S - r_qi) 511 // the result is E((S + a_pi) + (S -b_pi-r_pi)) and E(S + a_qi + S - r_qi)
510 for (i = 0; i < count; i++) 512 for (i = 0; i < count; i++)
511 { 513 {
512 r[i] = gcry_mpi_new(0); 514 r[i] = gcry_mpi_new (0);
513 GNUNET_CRYPTO_paillier_decrypt(&my_privkey, 515 GNUNET_CRYPTO_paillier_decrypt (&my_privkey,
514 &my_pubkey, 516 &my_pubkey,
515 &session->r[i], 517 &session->r[i],
516 r[i]); 518 r[i]);
517 gcry_mpi_sub(r[i], r[i], my_offset); 519 gcry_mpi_sub (r[i], r[i], my_offset);
518 gcry_mpi_sub(r[i], r[i], my_offset); 520 gcry_mpi_sub (r[i], r[i], my_offset);
519 r_prime[i] = gcry_mpi_new(0); 521 r_prime[i] = gcry_mpi_new (0);
520 GNUNET_CRYPTO_paillier_decrypt(&my_privkey, 522 GNUNET_CRYPTO_paillier_decrypt (&my_privkey,
521 &my_pubkey, 523 &my_pubkey,
522 &session->r_prime[i], 524 &session->r_prime[i],
523 r_prime[i]); 525 r_prime[i]);
524 gcry_mpi_sub(r_prime[i], r_prime[i], my_offset); 526 gcry_mpi_sub (r_prime[i], r_prime[i], my_offset);
525 gcry_mpi_sub(r_prime[i], r_prime[i], my_offset); 527 gcry_mpi_sub (r_prime[i], r_prime[i], my_offset);
526 } 528 }
527 529
528 // calculate t = sum(ai) 530 // calculate t = sum(ai)
529 t = compute_square_sum_mpi_elements(session->sorted_elements, count); 531 t = compute_square_sum_mpi_elements (session->sorted_elements, count);
530 // calculate U 532 // calculate U
531 u = gcry_mpi_new(0); 533 u = gcry_mpi_new (0);
532 tmp = compute_square_sum(r, count); 534 tmp = compute_square_sum (r, count);
533 gcry_mpi_sub(u, u, tmp); 535 gcry_mpi_sub (u, u, tmp);
534 gcry_mpi_release(tmp); 536 gcry_mpi_release (tmp);
535 537
536 //calculate U' 538 // calculate U'
537 u_prime = gcry_mpi_new(0); 539 u_prime = gcry_mpi_new (0);
538 tmp = compute_square_sum(r_prime, count); 540 tmp = compute_square_sum (r_prime, count);
539 gcry_mpi_sub(u_prime, u_prime, tmp); 541 gcry_mpi_sub (u_prime, u_prime, tmp);
540 542
541 GNUNET_assert(p = gcry_mpi_new(0)); 543 GNUNET_assert (p = gcry_mpi_new (0));
542 GNUNET_assert(p_prime = gcry_mpi_new(0)); 544 GNUNET_assert (p_prime = gcry_mpi_new (0));
543 GNUNET_assert(s = gcry_mpi_new(0)); 545 GNUNET_assert (s = gcry_mpi_new (0));
544 GNUNET_assert(s_prime = gcry_mpi_new(0)); 546 GNUNET_assert (s_prime = gcry_mpi_new (0));
545 547
546 // compute P 548 // compute P
547 GNUNET_CRYPTO_paillier_decrypt(&my_privkey, &my_pubkey, &session->s, s); 549 GNUNET_CRYPTO_paillier_decrypt (&my_privkey, &my_pubkey, &session->s, s);
548 GNUNET_CRYPTO_paillier_decrypt(&my_privkey, 550 GNUNET_CRYPTO_paillier_decrypt (&my_privkey,
549 &my_pubkey, 551 &my_pubkey,
550 &session->s_prime, 552 &session->s_prime,
551 s_prime); 553 s_prime);
552 554
553 // compute P 555 // compute P
554 gcry_mpi_add(p, s, t); 556 gcry_mpi_add (p, s, t);
555 gcry_mpi_add(p, p, u); 557 gcry_mpi_add (p, p, u);
556 558
557 // compute P' 559 // compute P'
558 gcry_mpi_add(p_prime, s_prime, t); 560 gcry_mpi_add (p_prime, s_prime, t);
559 gcry_mpi_add(p_prime, p_prime, u_prime); 561 gcry_mpi_add (p_prime, p_prime, u_prime);
560 562
561 gcry_mpi_release(t); 563 gcry_mpi_release (t);
562 gcry_mpi_release(u); 564 gcry_mpi_release (u);
563 gcry_mpi_release(u_prime); 565 gcry_mpi_release (u_prime);
564 gcry_mpi_release(s); 566 gcry_mpi_release (s);
565 gcry_mpi_release(s_prime); 567 gcry_mpi_release (s_prime);
566 568
567 // compute product 569 // compute product
568 gcry_mpi_sub(p, p, p_prime); 570 gcry_mpi_sub (p, p, p_prime);
569 gcry_mpi_release(p_prime); 571 gcry_mpi_release (p_prime);
570 tmp = gcry_mpi_set_ui(tmp, 2); 572 tmp = gcry_mpi_set_ui (tmp, 2);
571 gcry_mpi_div(p, NULL, p, tmp, 0); 573 gcry_mpi_div (p, NULL, p, tmp, 0);
572 574
573 gcry_mpi_release(tmp); 575 gcry_mpi_release (tmp);
574 for (i = 0; i < count; i++) 576 for (i = 0; i < count; i++)
575 { 577 {
576 gcry_mpi_release(session->sorted_elements[i].value); 578 gcry_mpi_release (session->sorted_elements[i].value);
577 gcry_mpi_release(r[i]); 579 gcry_mpi_release (r[i]);
578 gcry_mpi_release(r_prime[i]); 580 gcry_mpi_release (r_prime[i]);
579 } 581 }
580 GNUNET_free(session->sorted_elements); 582 GNUNET_free (session->sorted_elements);
581 session->sorted_elements = NULL; 583 session->sorted_elements = NULL;
582 GNUNET_free(session->r); 584 GNUNET_free (session->r);
583 session->r = NULL; 585 session->r = NULL;
584 GNUNET_free(session->r_prime); 586 GNUNET_free (session->r_prime);
585 session->r_prime = NULL; 587 session->r_prime = NULL;
586 588
587 return p; 589 return p;
@@ -598,7 +600,7 @@ compute_scalar_product(struct AliceServiceSession *session)
598 * #GNUNET_SYSERR to close it (signal serious error) 600 * #GNUNET_SYSERR to close it (signal serious error)
599 */ 601 */
600static int 602static int
601check_bobs_cryptodata_multipart( 603check_bobs_cryptodata_multipart (
602 void *cls, 604 void *cls,
603 const struct BobCryptodataMultipartMessage *msg) 605 const struct BobCryptodataMultipartMessage *msg)
604{ 606{
@@ -607,17 +609,17 @@ check_bobs_cryptodata_multipart(
607 size_t msg_size; 609 size_t msg_size;
608 size_t required_size; 610 size_t required_size;
609 611
610 msg_size = ntohs(msg->header.size); 612 msg_size = ntohs (msg->header.size);
611 contained = ntohl(msg->contained_element_count); 613 contained = ntohl (msg->contained_element_count);
612 required_size = 614 required_size =
613 sizeof(struct BobCryptodataMultipartMessage) + 615 sizeof(struct BobCryptodataMultipartMessage)
614 2 * contained * sizeof(struct GNUNET_CRYPTO_PaillierCiphertext); 616 + 2 * contained * sizeof(struct GNUNET_CRYPTO_PaillierCiphertext);
615 if ((required_size != msg_size) || 617 if ((required_size != msg_size) ||
616 (s->cadet_received_element_count + contained > s->used_element_count)) 618 (s->cadet_received_element_count + contained > s->used_element_count))
617 { 619 {
618 GNUNET_break(0); 620 GNUNET_break (0);
619 return GNUNET_SYSERR; 621 return GNUNET_SYSERR;
620 } 622 }
621 return GNUNET_OK; 623 return GNUNET_OK;
622} 624}
623 625
@@ -629,7 +631,7 @@ check_bobs_cryptodata_multipart(
629 * @param msg the actual message 631 * @param msg the actual message
630 */ 632 */
631static void 633static void
632handle_bobs_cryptodata_multipart( 634handle_bobs_cryptodata_multipart (
633 void *cls, 635 void *cls,
634 const struct BobCryptodataMultipartMessage *msg) 636 const struct BobCryptodataMultipartMessage *msg)
635{ 637{
@@ -638,29 +640,29 @@ handle_bobs_cryptodata_multipart(
638 size_t i; 640 size_t i;
639 uint32_t contained; 641 uint32_t contained;
640 642
641 contained = ntohl(msg->contained_element_count); 643 contained = ntohl (msg->contained_element_count);
642 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643 "Received %u additional crypto values from Bob\n", 645 "Received %u additional crypto values from Bob\n",
644 (unsigned int)contained); 646 (unsigned int) contained);
645 647
646 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *)&msg[1]; 648 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
647 /* Convert each k[][perm] to its MPI_value */ 649 /* Convert each k[][perm] to its MPI_value */
648 for (i = 0; i < contained; i++) 650 for (i = 0; i < contained; i++)
649 { 651 {
650 GNUNET_memcpy(&s->r[s->cadet_received_element_count + i], 652 GNUNET_memcpy (&s->r[s->cadet_received_element_count + i],
651 &payload[2 * i], 653 &payload[2 * i],
652 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)); 654 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
653 GNUNET_memcpy(&s->r_prime[s->cadet_received_element_count + i], 655 GNUNET_memcpy (&s->r_prime[s->cadet_received_element_count + i],
654 &payload[2 * i], 656 &payload[2 * i],
655 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)); 657 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
656 } 658 }
657 s->cadet_received_element_count += contained; 659 s->cadet_received_element_count += contained;
658 GNUNET_CADET_receive_done(s->channel); 660 GNUNET_CADET_receive_done (s->channel);
659 if (s->cadet_received_element_count != s->used_element_count) 661 if (s->cadet_received_element_count != s->used_element_count)
660 return; /* more to come */ 662 return; /* more to come */
661 663
662 s->product = compute_scalar_product(s); 664 s->product = compute_scalar_product (s);
663 transmit_client_response(s); 665 transmit_client_response (s);
664} 666}
665 667
666 668
@@ -674,38 +676,38 @@ handle_bobs_cryptodata_multipart(
674 * #GNUNET_SYSERR to close it (we are done) 676 * #GNUNET_SYSERR to close it (we are done)
675 */ 677 */
676static int 678static int
677check_bobs_cryptodata_message(void *cls, 679check_bobs_cryptodata_message (void *cls,
678 const struct BobCryptodataMessage *msg) 680 const struct BobCryptodataMessage *msg)
679{ 681{
680 struct AliceServiceSession *s = cls; 682 struct AliceServiceSession *s = cls;
681 uint32_t contained; 683 uint32_t contained;
682 uint16_t msg_size; 684 uint16_t msg_size;
683 size_t required_size; 685 size_t required_size;
684 686
685 msg_size = ntohs(msg->header.size); 687 msg_size = ntohs (msg->header.size);
686 contained = ntohl(msg->contained_element_count); 688 contained = ntohl (msg->contained_element_count);
687 required_size = 689 required_size =
688 sizeof(struct BobCryptodataMessage) + 690 sizeof(struct BobCryptodataMessage)
689 2 * contained * sizeof(struct GNUNET_CRYPTO_PaillierCiphertext) + 691 + 2 * contained * sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)
690 2 * sizeof(struct GNUNET_CRYPTO_PaillierCiphertext); 692 + 2 * sizeof(struct GNUNET_CRYPTO_PaillierCiphertext);
691 if ((msg_size != required_size) || (contained > UINT16_MAX) || 693 if ((msg_size != required_size) || (contained > UINT16_MAX) ||
692 (s->used_element_count < contained)) 694 (s->used_element_count < contained))
693 { 695 {
694 GNUNET_break_op(0); 696 GNUNET_break_op (0);
695 return GNUNET_SYSERR; 697 return GNUNET_SYSERR;
696 } 698 }
697 if (NULL == s->sorted_elements) 699 if (NULL == s->sorted_elements)
698 { 700 {
699 /* we're not ready yet, how can Bob be? */ 701 /* we're not ready yet, how can Bob be? */
700 GNUNET_break_op(0); 702 GNUNET_break_op (0);
701 return GNUNET_SYSERR; 703 return GNUNET_SYSERR;
702 } 704 }
703 if (s->total != s->client_received_element_count) 705 if (s->total != s->client_received_element_count)
704 { 706 {
705 /* we're not ready yet, how can Bob be? */ 707 /* we're not ready yet, how can Bob be? */
706 GNUNET_break_op(0); 708 GNUNET_break_op (0);
707 return GNUNET_SYSERR; 709 return GNUNET_SYSERR;
708 } 710 }
709 return GNUNET_OK; 711 return GNUNET_OK;
710} 712}
711 713
@@ -718,50 +720,50 @@ check_bobs_cryptodata_message(void *cls,
718 * @param msg the actual message 720 * @param msg the actual message
719 */ 721 */
720static void 722static void
721handle_bobs_cryptodata_message(void *cls, 723handle_bobs_cryptodata_message (void *cls,
722 const struct BobCryptodataMessage *msg) 724 const struct BobCryptodataMessage *msg)
723{ 725{
724 struct AliceServiceSession *s = cls; 726 struct AliceServiceSession *s = cls;
725 const struct GNUNET_CRYPTO_PaillierCiphertext *payload; 727 const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
726 uint32_t i; 728 uint32_t i;
727 uint32_t contained; 729 uint32_t contained;
728 730
729 contained = ntohl(msg->contained_element_count); 731 contained = ntohl (msg->contained_element_count);
730 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
731 "Received %u crypto values from Bob\n", 733 "Received %u crypto values from Bob\n",
732 (unsigned int)contained); 734 (unsigned int) contained);
733 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *)&msg[1]; 735 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
734 GNUNET_memcpy(&s->s, 736 GNUNET_memcpy (&s->s,
735 &payload[0], 737 &payload[0],
736 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)); 738 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
737 GNUNET_memcpy(&s->s_prime, 739 GNUNET_memcpy (&s->s_prime,
738 &payload[1], 740 &payload[1],
739 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)); 741 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
740 payload = &payload[2]; 742 payload = &payload[2];
741 743
742 s->r = GNUNET_new_array(s->used_element_count, 744 s->r = GNUNET_new_array (s->used_element_count,
743 struct GNUNET_CRYPTO_PaillierCiphertext); 745 struct GNUNET_CRYPTO_PaillierCiphertext);
744 s->r_prime = GNUNET_new_array(s->used_element_count, 746 s->r_prime = GNUNET_new_array (s->used_element_count,
745 struct GNUNET_CRYPTO_PaillierCiphertext); 747 struct GNUNET_CRYPTO_PaillierCiphertext);
746 for (i = 0; i < contained; i++) 748 for (i = 0; i < contained; i++)
747 { 749 {
748 GNUNET_memcpy(&s->r[i], 750 GNUNET_memcpy (&s->r[i],
749 &payload[2 * i], 751 &payload[2 * i],
750 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)); 752 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
751 GNUNET_memcpy(&s->r_prime[i], 753 GNUNET_memcpy (&s->r_prime[i],
752 &payload[2 * i + 1], 754 &payload[2 * i + 1],
753 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)); 755 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
754 } 756 }
755 s->cadet_received_element_count = contained; 757 s->cadet_received_element_count = contained;
756 GNUNET_CADET_receive_done(s->channel); 758 GNUNET_CADET_receive_done (s->channel);
757 759
758 if (s->cadet_received_element_count != s->used_element_count) 760 if (s->cadet_received_element_count != s->used_element_count)
759 { 761 {
760 /* More to come */ 762 /* More to come */
761 return; 763 return;
762 } 764 }
763 s->product = compute_scalar_product(s); 765 s->product = compute_scalar_product (s);
764 transmit_client_response(s); 766 transmit_client_response (s);
765} 767}
766 768
767 769
@@ -774,19 +776,19 @@ handle_bobs_cryptodata_message(void *cls,
774 * @param value the `struct GNUNET_SCALARPRODUCT_Element *` 776 * @param value the `struct GNUNET_SCALARPRODUCT_Element *`
775 */ 777 */
776static int 778static int
777copy_element_cb(void *cls, const struct GNUNET_HashCode *key, void *value) 779copy_element_cb (void *cls, const struct GNUNET_HashCode *key, void *value)
778{ 780{
779 struct AliceServiceSession *s = cls; 781 struct AliceServiceSession *s = cls;
780 struct GNUNET_SCALARPRODUCT_Element *e = value; 782 struct GNUNET_SCALARPRODUCT_Element *e = value;
781 gcry_mpi_t mval; 783 gcry_mpi_t mval;
782 int64_t val; 784 int64_t val;
783 785
784 mval = gcry_mpi_new(0); 786 mval = gcry_mpi_new (0);
785 val = (int64_t)GNUNET_ntohll(e->value); 787 val = (int64_t) GNUNET_ntohll (e->value);
786 if (0 > val) 788 if (0 > val)
787 gcry_mpi_sub_ui(mval, mval, -val); 789 gcry_mpi_sub_ui (mval, mval, -val);
788 else 790 else
789 gcry_mpi_add_ui(mval, mval, val); 791 gcry_mpi_add_ui (mval, mval, val);
790 s->sorted_elements[s->used_element_count].value = mval; 792 s->sorted_elements[s->used_element_count].value = mval;
791 s->sorted_elements[s->used_element_count].key = &e->key; 793 s->sorted_elements[s->used_element_count].key = &e->key;
792 s->used_element_count++; 794 s->used_element_count++;
@@ -802,12 +804,12 @@ copy_element_cb(void *cls, const struct GNUNET_HashCode *key, void *value)
802 * @return -1 for a < b, 0 for a=b, 1 for a > b. 804 * @return -1 for a < b, 0 for a=b, 1 for a > b.
803 */ 805 */
804static int 806static int
805element_cmp(const void *a, const void *b) 807element_cmp (const void *a, const void *b)
806{ 808{
807 const struct MpiElement *ma = a; 809 const struct MpiElement *ma = a;
808 const struct MpiElement *mb = b; 810 const struct MpiElement *mb = b;
809 811
810 return GNUNET_CRYPTO_hash_cmp(ma->key, mb->key); 812 return GNUNET_CRYPTO_hash_cmp (ma->key, mb->key);
811} 813}
812 814
813 815
@@ -816,9 +818,9 @@ element_cmp(const void *a, const void *b)
816 * message 818 * message
817 */ 819 */
818#define ELEMENT_CAPACITY \ 820#define ELEMENT_CAPACITY \
819 ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - \ 821 ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 \
820 sizeof(struct AliceCryptodataMessage)) / \ 822 - sizeof(struct AliceCryptodataMessage)) \
821 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)) 823 / sizeof(struct GNUNET_CRYPTO_PaillierCiphertext))
822 824
823 825
824/** 826/**
@@ -828,7 +830,7 @@ element_cmp(const void *a, const void *b)
828 * @param s the associated service session 830 * @param s the associated service session
829 */ 831 */
830static void 832static void
831send_alices_cryptodata_message(struct AliceServiceSession *s) 833send_alices_cryptodata_message (struct AliceServiceSession *s)
832{ 834{
833 struct AliceCryptodataMessage *msg; 835 struct AliceCryptodataMessage *msg;
834 struct GNUNET_MQ_Envelope *e; 836 struct GNUNET_MQ_Envelope *e;
@@ -838,50 +840,50 @@ send_alices_cryptodata_message(struct AliceServiceSession *s)
838 gcry_mpi_t a; 840 gcry_mpi_t a;
839 uint32_t off; 841 uint32_t off;
840 842
841 s->sorted_elements = GNUNET_malloc( 843 s->sorted_elements = GNUNET_malloc (
842 GNUNET_CONTAINER_multihashmap_size(s->intersected_elements) * 844 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements)
843 sizeof(struct MpiElement)); 845 * sizeof(struct MpiElement));
844 s->used_element_count = 0; 846 s->used_element_count = 0;
845 GNUNET_CONTAINER_multihashmap_iterate(s->intersected_elements, 847 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
846 &copy_element_cb, 848 &copy_element_cb,
847 s); 849 s);
848 LOG(GNUNET_ERROR_TYPE_DEBUG, 850 LOG (GNUNET_ERROR_TYPE_DEBUG,
849 "Finished intersection, %d items remain\n", 851 "Finished intersection, %d items remain\n",
850 s->used_element_count); 852 s->used_element_count);
851 qsort(s->sorted_elements, 853 qsort (s->sorted_elements,
852 s->used_element_count, 854 s->used_element_count,
853 sizeof(struct MpiElement), 855 sizeof(struct MpiElement),
854 &element_cmp); 856 &element_cmp);
855 off = 0; 857 off = 0;
856 while (off < s->used_element_count) 858 while (off < s->used_element_count)
859 {
860 todo_count = s->used_element_count - off;
861 if (todo_count > ELEMENT_CAPACITY)
862 todo_count = ELEMENT_CAPACITY;
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Sending %u/%u crypto values to Bob\n",
865 (unsigned int) todo_count,
866 (unsigned int) s->used_element_count);
867
868 e =
869 GNUNET_MQ_msg_extra (msg,
870 todo_count
871 * sizeof(struct GNUNET_CRYPTO_PaillierCiphertext),
872 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA);
873 msg->contained_element_count = htonl (todo_count);
874 payload = (struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
875 a = gcry_mpi_new (0);
876 for (i = off; i < off + todo_count; i++)
857 { 877 {
858 todo_count = s->used_element_count - off; 878 gcry_mpi_add (a, s->sorted_elements[i].value, my_offset);
859 if (todo_count > ELEMENT_CAPACITY) 879 GNUNET_assert (
860 todo_count = ELEMENT_CAPACITY; 880 3 ==
861 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 881 GNUNET_CRYPTO_paillier_encrypt (&my_pubkey, a, 3, &payload[i - off]));
862 "Sending %u/%u crypto values to Bob\n",
863 (unsigned int)todo_count,
864 (unsigned int)s->used_element_count);
865
866 e =
867 GNUNET_MQ_msg_extra(msg,
868 todo_count *
869 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext),
870 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA);
871 msg->contained_element_count = htonl(todo_count);
872 payload = (struct GNUNET_CRYPTO_PaillierCiphertext *)&msg[1];
873 a = gcry_mpi_new(0);
874 for (i = off; i < off + todo_count; i++)
875 {
876 gcry_mpi_add(a, s->sorted_elements[i].value, my_offset);
877 GNUNET_assert(
878 3 ==
879 GNUNET_CRYPTO_paillier_encrypt(&my_pubkey, a, 3, &payload[i - off]));
880 }
881 gcry_mpi_release(a);
882 off += todo_count;
883 GNUNET_MQ_send(s->cadet_mq, e);
884 } 882 }
883 gcry_mpi_release (a);
884 off += todo_count;
885 GNUNET_MQ_send (s->cadet_mq, e);
886 }
885} 887}
886 888
887 889
@@ -896,70 +898,70 @@ send_alices_cryptodata_message(struct AliceServiceSession *s)
896 * @param status what has happened with the set intersection? 898 * @param status what has happened with the set intersection?
897 */ 899 */
898static void 900static void
899cb_intersection_element_removed(void *cls, 901cb_intersection_element_removed (void *cls,
900 const struct GNUNET_SET_Element *element, 902 const struct GNUNET_SET_Element *element,
901 uint64_t current_size, 903 uint64_t current_size,
902 enum GNUNET_SET_Status status) 904 enum GNUNET_SET_Status status)
903{ 905{
904 struct AliceServiceSession *s = cls; 906 struct AliceServiceSession *s = cls;
905 struct GNUNET_SCALARPRODUCT_Element *se; 907 struct GNUNET_SCALARPRODUCT_Element *se;
906 908
907 switch (status) 909 switch (status)
910 {
911 case GNUNET_SET_STATUS_OK:
912 /* this element has been removed from the set */
913 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
914 element->data);
915 GNUNET_assert (NULL != se);
916 LOG (GNUNET_ERROR_TYPE_DEBUG,
917 "Intersection removed element with key %s and value %lld\n",
918 GNUNET_h2s (&se->key),
919 (long long) GNUNET_ntohll (se->value));
920 GNUNET_assert (
921 GNUNET_YES ==
922 GNUNET_CONTAINER_multihashmap_remove (s->intersected_elements,
923 element->data,
924 se));
925 GNUNET_free (se);
926 return;
927
928 case GNUNET_SET_STATUS_DONE:
929 s->intersection_op = NULL;
930 if (NULL != s->intersection_set)
908 { 931 {
909 case GNUNET_SET_STATUS_OK: 932 GNUNET_SET_destroy (s->intersection_set);
910 /* this element has been removed from the set */ 933 s->intersection_set = NULL;
911 se = GNUNET_CONTAINER_multihashmap_get(s->intersected_elements,
912 element->data);
913 GNUNET_assert(NULL != se);
914 LOG(GNUNET_ERROR_TYPE_DEBUG,
915 "Intersection removed element with key %s and value %lld\n",
916 GNUNET_h2s(&se->key),
917 (long long)GNUNET_ntohll(se->value));
918 GNUNET_assert(
919 GNUNET_YES ==
920 GNUNET_CONTAINER_multihashmap_remove(s->intersected_elements,
921 element->data,
922 se));
923 GNUNET_free(se);
924 return;
925
926 case GNUNET_SET_STATUS_DONE:
927 s->intersection_op = NULL;
928 if (NULL != s->intersection_set)
929 {
930 GNUNET_SET_destroy(s->intersection_set);
931 s->intersection_set = NULL;
932 }
933 send_alices_cryptodata_message(s);
934 return;
935
936 case GNUNET_SET_STATUS_HALF_DONE:
937 /* unexpected for intersection */
938 GNUNET_break(0);
939 return;
940
941 case GNUNET_SET_STATUS_FAILURE:
942 /* unhandled status code */
943 LOG(GNUNET_ERROR_TYPE_DEBUG, "Set intersection failed!\n");
944 if (NULL != s->intersection_listen)
945 {
946 GNUNET_SET_listen_cancel(s->intersection_listen);
947 s->intersection_listen = NULL;
948 }
949 s->intersection_op = NULL;
950 if (NULL != s->intersection_set)
951 {
952 GNUNET_SET_destroy(s->intersection_set);
953 s->intersection_set = NULL;
954 }
955 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
956 prepare_client_end_notification(s);
957 return;
958
959 default:
960 GNUNET_break(0);
961 return;
962 } 934 }
935 send_alices_cryptodata_message (s);
936 return;
937
938 case GNUNET_SET_STATUS_HALF_DONE:
939 /* unexpected for intersection */
940 GNUNET_break (0);
941 return;
942
943 case GNUNET_SET_STATUS_FAILURE:
944 /* unhandled status code */
945 LOG (GNUNET_ERROR_TYPE_DEBUG, "Set intersection failed!\n");
946 if (NULL != s->intersection_listen)
947 {
948 GNUNET_SET_listen_cancel (s->intersection_listen);
949 s->intersection_listen = NULL;
950 }
951 s->intersection_op = NULL;
952 if (NULL != s->intersection_set)
953 {
954 GNUNET_SET_destroy (s->intersection_set);
955 s->intersection_set = NULL;
956 }
957 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
958 prepare_client_end_notification (s);
959 return;
960
961 default:
962 GNUNET_break (0);
963 return;
964 }
963} 965}
964 966
965 967
@@ -979,37 +981,37 @@ cb_intersection_element_removed(void *cls,
979 * message. It's necessary to specify the timeout. 981 * message. It's necessary to specify the timeout.
980 */ 982 */
981static void 983static void
982cb_intersection_request_alice(void *cls, 984cb_intersection_request_alice (void *cls,
983 const struct GNUNET_PeerIdentity *other_peer, 985 const struct GNUNET_PeerIdentity *other_peer,
984 const struct GNUNET_MessageHeader *context_msg, 986 const struct GNUNET_MessageHeader *context_msg,
985 struct GNUNET_SET_Request *request) 987 struct GNUNET_SET_Request *request)
986{ 988{
987 struct AliceServiceSession *s = cls; 989 struct AliceServiceSession *s = cls;
988 990
989 if (0 != GNUNET_memcmp(other_peer, &s->peer)) 991 if (0 != GNUNET_memcmp (other_peer, &s->peer))
990 { 992 {
991 GNUNET_break_op(0); 993 GNUNET_break_op (0);
992 return; 994 return;
993 } 995 }
994 s->intersection_op = GNUNET_SET_accept(request, 996 s->intersection_op = GNUNET_SET_accept (request,
995 GNUNET_SET_RESULT_REMOVED, 997 GNUNET_SET_RESULT_REMOVED,
996 (struct GNUNET_SET_Option[]){ { 0 } }, 998 (struct GNUNET_SET_Option[]){ { 0 } },
997 &cb_intersection_element_removed, 999 &cb_intersection_element_removed,
998 s); 1000 s);
999 if (NULL == s->intersection_op) 1001 if (NULL == s->intersection_op)
1000 { 1002 {
1001 GNUNET_break(0); 1003 GNUNET_break (0);
1002 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 1004 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1003 prepare_client_end_notification(s); 1005 prepare_client_end_notification (s);
1004 return; 1006 return;
1005 } 1007 }
1006 if (GNUNET_OK != GNUNET_SET_commit(s->intersection_op, s->intersection_set)) 1008 if (GNUNET_OK != GNUNET_SET_commit (s->intersection_op, s->intersection_set))
1007 { 1009 {
1008 GNUNET_break(0); 1010 GNUNET_break (0);
1009 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 1011 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1010 prepare_client_end_notification(s); 1012 prepare_client_end_notification (s);
1011 return; 1013 return;
1012 } 1014 }
1013} 1015}
1014 1016
1015 1017
@@ -1019,58 +1021,58 @@ cb_intersection_request_alice(void *cls,
1019 * @param session the service session context 1021 * @param session the service session context
1020 */ 1022 */
1021static void 1023static void
1022client_request_complete_alice(struct AliceServiceSession *s) 1024client_request_complete_alice (struct AliceServiceSession *s)
1023{ 1025{
1024 struct GNUNET_MQ_MessageHandler cadet_handlers[] = 1026 struct GNUNET_MQ_MessageHandler cadet_handlers[] =
1025 { GNUNET_MQ_hd_var_size(bobs_cryptodata_message, 1027 { GNUNET_MQ_hd_var_size (bobs_cryptodata_message,
1026 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA, 1028 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA,
1027 struct BobCryptodataMessage, 1029 struct BobCryptodataMessage,
1028 s), 1030 s),
1029 GNUNET_MQ_hd_var_size( 1031 GNUNET_MQ_hd_var_size (
1030 bobs_cryptodata_multipart, 1032 bobs_cryptodata_multipart,
1031 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART, 1033 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART,
1032 struct BobCryptodataMultipartMessage, 1034 struct BobCryptodataMultipartMessage,
1033 s), 1035 s),
1034 GNUNET_MQ_handler_end() }; 1036 GNUNET_MQ_handler_end () };
1035 struct ServiceRequestMessage *msg; 1037 struct ServiceRequestMessage *msg;
1036 struct GNUNET_MQ_Envelope *e; 1038 struct GNUNET_MQ_Envelope *e;
1037 1039
1038 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039 "Creating new channel for session with key %s.\n", 1041 "Creating new channel for session with key %s.\n",
1040 GNUNET_h2s(&s->session_id)); 1042 GNUNET_h2s (&s->session_id));
1041 s->channel = GNUNET_CADET_channel_create(my_cadet, 1043 s->channel = GNUNET_CADET_channel_create (my_cadet,
1042 s, 1044 s,
1043 &s->peer, 1045 &s->peer,
1044 &s->session_id, 1046 &s->session_id,
1045 NULL, 1047 NULL,
1046 &cb_channel_destruction, 1048 &cb_channel_destruction,
1047 cadet_handlers); 1049 cadet_handlers);
1048 if (NULL == s->channel) 1050 if (NULL == s->channel)
1049 { 1051 {
1050 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 1052 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1051 prepare_client_end_notification(s); 1053 prepare_client_end_notification (s);
1052 return; 1054 return;
1053 } 1055 }
1054 s->cadet_mq = GNUNET_CADET_get_mq(s->channel); 1056 s->cadet_mq = GNUNET_CADET_get_mq (s->channel);
1055 s->intersection_listen = GNUNET_SET_listen(cfg, 1057 s->intersection_listen = GNUNET_SET_listen (cfg,
1056 GNUNET_SET_OPERATION_INTERSECTION, 1058 GNUNET_SET_OPERATION_INTERSECTION,
1057 &s->session_id, 1059 &s->session_id,
1058 &cb_intersection_request_alice, 1060 &cb_intersection_request_alice,
1059 s); 1061 s);
1060 if (NULL == s->intersection_listen) 1062 if (NULL == s->intersection_listen)
1061 { 1063 {
1062 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 1064 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
1063 GNUNET_CADET_channel_destroy(s->channel); 1065 GNUNET_CADET_channel_destroy (s->channel);
1064 s->channel = NULL; 1066 s->channel = NULL;
1065 prepare_client_end_notification(s); 1067 prepare_client_end_notification (s);
1066 return; 1068 return;
1067 } 1069 }
1068 1070
1069 e = GNUNET_MQ_msg(msg, 1071 e = GNUNET_MQ_msg (msg,
1070 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION); 1072 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION);
1071 msg->session_id = s->session_id; 1073 msg->session_id = s->session_id;
1072 msg->public_key = my_pubkey; 1074 msg->public_key = my_pubkey;
1073 GNUNET_MQ_send(s->cadet_mq, e); 1075 GNUNET_MQ_send (s->cadet_mq, e);
1074} 1076}
1075 1077
1076 1078
@@ -1083,7 +1085,7 @@ client_request_complete_alice(struct AliceServiceSession *s)
1083 * @return #GNUNET_OK if @a msg is well-formed 1085 * @return #GNUNET_OK if @a msg is well-formed
1084 */ 1086 */
1085static int 1087static int
1086check_alice_client_message_multipart( 1088check_alice_client_message_multipart (
1087 void *cls, 1089 void *cls,
1088 const struct ComputationBobCryptodataMultipartMessage *msg) 1090 const struct ComputationBobCryptodataMultipartMessage *msg)
1089{ 1091{
@@ -1091,18 +1093,18 @@ check_alice_client_message_multipart(
1091 uint32_t contained_count; 1093 uint32_t contained_count;
1092 uint16_t msize; 1094 uint16_t msize;
1093 1095
1094 msize = ntohs(msg->header.size); 1096 msize = ntohs (msg->header.size);
1095 contained_count = ntohl(msg->element_count_contained); 1097 contained_count = ntohl (msg->element_count_contained);
1096 if ((msize != 1098 if ((msize !=
1097 (sizeof(struct ComputationBobCryptodataMultipartMessage) + 1099 (sizeof(struct ComputationBobCryptodataMultipartMessage)
1098 contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element))) || 1100 + contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element))) ||
1099 (0 == contained_count) || 1101 (0 == contained_count) ||
1100 (s->total == s->client_received_element_count) || 1102 (s->total == s->client_received_element_count) ||
1101 (s->total < s->client_received_element_count + contained_count)) 1103 (s->total < s->client_received_element_count + contained_count))
1102 { 1104 {
1103 GNUNET_break_op(0); 1105 GNUNET_break_op (0);
1104 return GNUNET_SYSERR; 1106 return GNUNET_SYSERR;
1105 } 1107 }
1106 return GNUNET_OK; 1108 return GNUNET_OK;
1107} 1109}
1108 1110
@@ -1115,7 +1117,7 @@ check_alice_client_message_multipart(
1115 * @param msg the actual message 1117 * @param msg the actual message
1116 */ 1118 */
1117static void 1119static void
1118handle_alice_client_message_multipart( 1120handle_alice_client_message_multipart (
1119 void *cls, 1121 void *cls,
1120 const struct ComputationBobCryptodataMultipartMessage *msg) 1122 const struct ComputationBobCryptodataMultipartMessage *msg)
1121{ 1123{
@@ -1125,38 +1127,38 @@ handle_alice_client_message_multipart(
1125 struct GNUNET_SET_Element set_elem; 1127 struct GNUNET_SET_Element set_elem;
1126 struct GNUNET_SCALARPRODUCT_Element *elem; 1128 struct GNUNET_SCALARPRODUCT_Element *elem;
1127 1129
1128 contained_count = ntohl(msg->element_count_contained); 1130 contained_count = ntohl (msg->element_count_contained);
1129 s->client_received_element_count += contained_count; 1131 s->client_received_element_count += contained_count;
1130 elements = (const struct GNUNET_SCALARPRODUCT_Element *)&msg[1]; 1132 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1131 for (uint32_t i = 0; i < contained_count; i++) 1133 for (uint32_t i = 0; i < contained_count; i++)
1134 {
1135 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1136 GNUNET_memcpy (elem,
1137 &elements[i],
1138 sizeof(struct GNUNET_SCALARPRODUCT_Element));
1139 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put (
1140 s->intersected_elements,
1141 &elem->key,
1142 elem,
1143 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1132 { 1144 {
1133 elem = GNUNET_new(struct GNUNET_SCALARPRODUCT_Element); 1145 GNUNET_break (0);
1134 GNUNET_memcpy(elem, 1146 GNUNET_free (elem);
1135 &elements[i], 1147 continue;
1136 sizeof(struct GNUNET_SCALARPRODUCT_Element));
1137 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(
1138 s->intersected_elements,
1139 &elem->key,
1140 elem,
1141 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1142 {
1143 GNUNET_break(0);
1144 GNUNET_free(elem);
1145 continue;
1146 }
1147 set_elem.data = &elem->key;
1148 set_elem.size = sizeof(elem->key);
1149 set_elem.element_type = 0;
1150 GNUNET_SET_add_element(s->intersection_set, &set_elem, NULL, NULL);
1151 s->used_element_count++;
1152 } 1148 }
1153 GNUNET_SERVICE_client_continue(s->client); 1149 set_elem.data = &elem->key;
1150 set_elem.size = sizeof(elem->key);
1151 set_elem.element_type = 0;
1152 GNUNET_SET_add_element (s->intersection_set, &set_elem, NULL, NULL);
1153 s->used_element_count++;
1154 }
1155 GNUNET_SERVICE_client_continue (s->client);
1154 if (s->total != s->client_received_element_count) 1156 if (s->total != s->client_received_element_count)
1155 { 1157 {
1156 /* more to come */ 1158 /* more to come */
1157 return; 1159 return;
1158 } 1160 }
1159 client_request_complete_alice(s); 1161 client_request_complete_alice (s);
1160} 1162}
1161 1163
1162 1164
@@ -1169,8 +1171,8 @@ handle_alice_client_message_multipart(
1169 * @return #GNUNET_OK if @a msg is well-formed 1171 * @return #GNUNET_OK if @a msg is well-formed
1170 */ 1172 */
1171static int 1173static int
1172check_alice_client_message(void *cls, 1174check_alice_client_message (void *cls,
1173 const struct AliceComputationMessage *msg) 1175 const struct AliceComputationMessage *msg)
1174{ 1176{
1175 struct AliceServiceSession *s = cls; 1177 struct AliceServiceSession *s = cls;
1176 uint16_t msize; 1178 uint16_t msize;
@@ -1178,23 +1180,23 @@ check_alice_client_message(void *cls,
1178 uint32_t contained_count; 1180 uint32_t contained_count;
1179 1181
1180 if (NULL != s->intersected_elements) 1182 if (NULL != s->intersected_elements)
1181 { 1183 {
1182 /* only one concurrent session per client connection allowed, 1184 /* only one concurrent session per client connection allowed,
1183 simplifies logic a lot... */ 1185 simplifies logic a lot... */
1184 GNUNET_break(0); 1186 GNUNET_break (0);
1185 return GNUNET_SYSERR; 1187 return GNUNET_SYSERR;
1186 } 1188 }
1187 msize = ntohs(msg->header.size); 1189 msize = ntohs (msg->header.size);
1188 total_count = ntohl(msg->element_count_total); 1190 total_count = ntohl (msg->element_count_total);
1189 contained_count = ntohl(msg->element_count_contained); 1191 contained_count = ntohl (msg->element_count_contained);
1190 if ((0 == total_count) || (0 == contained_count) || 1192 if ((0 == total_count) || (0 == contained_count) ||
1191 (msize != 1193 (msize !=
1192 (sizeof(struct AliceComputationMessage) + 1194 (sizeof(struct AliceComputationMessage)
1193 contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element)))) 1195 + contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element))))
1194 { 1196 {
1195 GNUNET_break_op(0); 1197 GNUNET_break_op (0);
1196 return GNUNET_SYSERR; 1198 return GNUNET_SYSERR;
1197 } 1199 }
1198 return GNUNET_OK; 1200 return GNUNET_OK;
1199} 1201}
1200 1202
@@ -1207,8 +1209,8 @@ check_alice_client_message(void *cls,
1207 * @param msg the actual message 1209 * @param msg the actual message
1208 */ 1210 */
1209static void 1211static void
1210handle_alice_client_message(void *cls, 1212handle_alice_client_message (void *cls,
1211 const struct AliceComputationMessage *msg) 1213 const struct AliceComputationMessage *msg)
1212{ 1214{
1213 struct AliceServiceSession *s = cls; 1215 struct AliceServiceSession *s = cls;
1214 uint32_t contained_count; 1216 uint32_t contained_count;
@@ -1217,51 +1219,51 @@ handle_alice_client_message(void *cls,
1217 struct GNUNET_SET_Element set_elem; 1219 struct GNUNET_SET_Element set_elem;
1218 struct GNUNET_SCALARPRODUCT_Element *elem; 1220 struct GNUNET_SCALARPRODUCT_Element *elem;
1219 1221
1220 total_count = ntohl(msg->element_count_total); 1222 total_count = ntohl (msg->element_count_total);
1221 contained_count = ntohl(msg->element_count_contained); 1223 contained_count = ntohl (msg->element_count_contained);
1222 s->peer = msg->peer; 1224 s->peer = msg->peer;
1223 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE; 1225 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1224 s->total = total_count; 1226 s->total = total_count;
1225 s->client_received_element_count = contained_count; 1227 s->client_received_element_count = contained_count;
1226 s->session_id = msg->session_key; 1228 s->session_id = msg->session_key;
1227 elements = (const struct GNUNET_SCALARPRODUCT_Element *)&msg[1]; 1229 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1228 s->intersected_elements = 1230 s->intersected_elements =
1229 GNUNET_CONTAINER_multihashmap_create(s->total, GNUNET_YES); 1231 GNUNET_CONTAINER_multihashmap_create (s->total, GNUNET_YES);
1230 s->intersection_set = 1232 s->intersection_set =
1231 GNUNET_SET_create(cfg, GNUNET_SET_OPERATION_INTERSECTION); 1233 GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_INTERSECTION);
1232 1234
1233 for (uint32_t i = 0; i < contained_count; i++) 1235 for (uint32_t i = 0; i < contained_count; i++)
1236 {
1237 if (0 == GNUNET_ntohll (elements[i].value))
1238 continue;
1239 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1240 GNUNET_memcpy (elem,
1241 &elements[i],
1242 sizeof(struct GNUNET_SCALARPRODUCT_Element));
1243 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put (
1244 s->intersected_elements,
1245 &elem->key,
1246 elem,
1247 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1234 { 1248 {
1235 if (0 == GNUNET_ntohll(elements[i].value)) 1249 /* element with same key encountered twice! */
1236 continue; 1250 GNUNET_break (0);
1237 elem = GNUNET_new(struct GNUNET_SCALARPRODUCT_Element); 1251 GNUNET_free (elem);
1238 GNUNET_memcpy(elem, 1252 continue;
1239 &elements[i],
1240 sizeof(struct GNUNET_SCALARPRODUCT_Element));
1241 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(
1242 s->intersected_elements,
1243 &elem->key,
1244 elem,
1245 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1246 {
1247 /* element with same key encountered twice! */
1248 GNUNET_break(0);
1249 GNUNET_free(elem);
1250 continue;
1251 }
1252 set_elem.data = &elem->key;
1253 set_elem.size = sizeof(elem->key);
1254 set_elem.element_type = 0;
1255 GNUNET_SET_add_element(s->intersection_set, &set_elem, NULL, NULL);
1256 s->used_element_count++;
1257 } 1253 }
1258 GNUNET_SERVICE_client_continue(s->client); 1254 set_elem.data = &elem->key;
1255 set_elem.size = sizeof(elem->key);
1256 set_elem.element_type = 0;
1257 GNUNET_SET_add_element (s->intersection_set, &set_elem, NULL, NULL);
1258 s->used_element_count++;
1259 }
1260 GNUNET_SERVICE_client_continue (s->client);
1259 if (s->total != s->client_received_element_count) 1261 if (s->total != s->client_received_element_count)
1260 { 1262 {
1261 /* wait for multipart msg */ 1263 /* wait for multipart msg */
1262 return; 1264 return;
1263 } 1265 }
1264 client_request_complete_alice(s); 1266 client_request_complete_alice (s);
1265} 1267}
1266 1268
1267 1269
@@ -1271,15 +1273,15 @@ handle_alice_client_message(void *cls,
1271 * @param cls unused 1273 * @param cls unused
1272 */ 1274 */
1273static void 1275static void
1274shutdown_task(void *cls) 1276shutdown_task (void *cls)
1275{ 1277{
1276 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Shutting down, initiating cleanup.\n"); 1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down, initiating cleanup.\n");
1277 // FIXME: we have to cut our connections to CADET first! 1279 // FIXME: we have to cut our connections to CADET first!
1278 if (NULL != my_cadet) 1280 if (NULL != my_cadet)
1279 { 1281 {
1280 GNUNET_CADET_disconnect(my_cadet); 1282 GNUNET_CADET_disconnect (my_cadet);
1281 my_cadet = NULL; 1283 my_cadet = NULL;
1282 } 1284 }
1283} 1285}
1284 1286
1285 1287
@@ -1294,13 +1296,13 @@ shutdown_task(void *cls)
1294 * @return our `struct AliceServiceSession` 1296 * @return our `struct AliceServiceSession`
1295 */ 1297 */
1296static void * 1298static void *
1297client_connect_cb(void *cls, 1299client_connect_cb (void *cls,
1298 struct GNUNET_SERVICE_Client *client, 1300 struct GNUNET_SERVICE_Client *client,
1299 struct GNUNET_MQ_Handle *mq) 1301 struct GNUNET_MQ_Handle *mq)
1300{ 1302{
1301 struct AliceServiceSession *s; 1303 struct AliceServiceSession *s;
1302 1304
1303 s = GNUNET_new(struct AliceServiceSession); 1305 s = GNUNET_new (struct AliceServiceSession);
1304 s->client = client; 1306 s->client = client;
1305 s->client_mq = mq; 1307 s->client_mq = mq;
1306 return s; 1308 return s;
@@ -1318,18 +1320,18 @@ client_connect_cb(void *cls,
1318 * @param app_cls our `struct AliceServiceSession` 1320 * @param app_cls our `struct AliceServiceSession`
1319 */ 1321 */
1320static void 1322static void
1321client_disconnect_cb(void *cls, 1323client_disconnect_cb (void *cls,
1322 struct GNUNET_SERVICE_Client *client, 1324 struct GNUNET_SERVICE_Client *client,
1323 void *app_cls) 1325 void *app_cls)
1324{ 1326{
1325 struct AliceServiceSession *s = app_cls; 1327 struct AliceServiceSession *s = app_cls;
1326 1328
1327 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1328 "Client %p disconnected from us.\n", 1330 "Client %p disconnected from us.\n",
1329 client); 1331 client);
1330 s->client = NULL; 1332 s->client = NULL;
1331 s->client_mq = NULL; 1333 s->client_mq = NULL;
1332 destroy_service_session(s); 1334 destroy_service_session (s);
1333} 1335}
1334 1336
1335 1337
@@ -1341,49 +1343,49 @@ client_disconnect_cb(void *cls,
1341 * @param service the initialized service 1343 * @param service the initialized service
1342 */ 1344 */
1343static void 1345static void
1344run(void *cls, 1346run (void *cls,
1345 const struct GNUNET_CONFIGURATION_Handle *c, 1347 const struct GNUNET_CONFIGURATION_Handle *c,
1346 struct GNUNET_SERVICE_Handle *service) 1348 struct GNUNET_SERVICE_Handle *service)
1347{ 1349{
1348 cfg = c; 1350 cfg = c;
1349 /* 1351 /*
1350 offset has to be sufficiently small to allow computation of: 1352 offset has to be sufficiently small to allow computation of:
1351 m1+m2 mod n == (S + a) + (S + b) mod n, 1353 m1+m2 mod n == (S + a) + (S + b) mod n,
1352 if we have more complex operations, this factor needs to be lowered */ 1354 if we have more complex operations, this factor needs to be lowered */
1353 my_offset = gcry_mpi_new(GNUNET_CRYPTO_PAILLIER_BITS / 3); 1355 my_offset = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS / 3);
1354 gcry_mpi_set_bit(my_offset, GNUNET_CRYPTO_PAILLIER_BITS / 3); 1356 gcry_mpi_set_bit (my_offset, GNUNET_CRYPTO_PAILLIER_BITS / 3);
1355 GNUNET_CRYPTO_paillier_create(&my_pubkey, &my_privkey); 1357 GNUNET_CRYPTO_paillier_create (&my_pubkey, &my_privkey);
1356 my_cadet = GNUNET_CADET_connect(cfg); 1358 my_cadet = GNUNET_CADET_connect (cfg);
1357 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL); 1359 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
1358 if (NULL == my_cadet) 1360 if (NULL == my_cadet)
1359 { 1361 {
1360 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Connect to CADET failed\n")); 1362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Connect to CADET failed\n"));
1361 GNUNET_SCHEDULER_shutdown(); 1363 GNUNET_SCHEDULER_shutdown ();
1362 return; 1364 return;
1363 } 1365 }
1364} 1366}
1365 1367
1366 1368
1367/** 1369/**
1368 * Define "main" method using service macro. 1370 * Define "main" method using service macro.
1369 */ 1371 */
1370GNUNET_SERVICE_MAIN( 1372GNUNET_SERVICE_MAIN (
1371 "scalarproduct-alice", 1373 "scalarproduct-alice",
1372 GNUNET_SERVICE_OPTION_NONE, 1374 GNUNET_SERVICE_OPTION_NONE,
1373 &run, 1375 &run,
1374 &client_connect_cb, 1376 &client_connect_cb,
1375 &client_disconnect_cb, 1377 &client_disconnect_cb,
1376 NULL, 1378 NULL,
1377 GNUNET_MQ_hd_var_size(alice_client_message, 1379 GNUNET_MQ_hd_var_size (alice_client_message,
1378 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE, 1380 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE,
1379 struct AliceComputationMessage, 1381 struct AliceComputationMessage,
1380 NULL), 1382 NULL),
1381 GNUNET_MQ_hd_var_size( 1383 GNUNET_MQ_hd_var_size (
1382 alice_client_message_multipart, 1384 alice_client_message_multipart,
1383 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE, 1385 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE,
1384 struct ComputationBobCryptodataMultipartMessage, 1386 struct ComputationBobCryptodataMultipartMessage,
1385 NULL), 1387 NULL),
1386 GNUNET_MQ_handler_end()); 1388 GNUNET_MQ_handler_end ());
1387 1389
1388 1390
1389/* end of gnunet-service-scalarproduct_alice.c */ 1391/* end of gnunet-service-scalarproduct_alice.c */
diff --git a/src/scalarproduct/gnunet-service-scalarproduct_bob.c b/src/scalarproduct/gnunet-service-scalarproduct_bob.c
index f787cdd2e..23d59a36a 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct_bob.c
+++ b/src/scalarproduct/gnunet-service-scalarproduct_bob.c
@@ -36,13 +36,14 @@
36#include "scalarproduct.h" 36#include "scalarproduct.h"
37#include "gnunet-service-scalarproduct.h" 37#include "gnunet-service-scalarproduct.h"
38 38
39#define LOG(kind, ...) GNUNET_log_from(kind, "scalarproduct-bob", __VA_ARGS__) 39#define LOG(kind, ...) GNUNET_log_from (kind, "scalarproduct-bob", __VA_ARGS__)
40 40
41 41
42/** 42/**
43 * An encrypted element key-value pair. 43 * An encrypted element key-value pair.
44 */ 44 */
45struct MpiElement { 45struct MpiElement
46{
46 /** 47 /**
47 * Key used to identify matching pairs of values to multiply. 48 * Key used to identify matching pairs of values to multiply.
48 * Points into an existing data structure, to avoid copying 49 * Points into an existing data structure, to avoid copying
@@ -61,7 +62,8 @@ struct MpiElement {
61 * A scalarproduct session which tracks an offer for a 62 * A scalarproduct session which tracks an offer for a
62 * multiplication service by a local client. 63 * multiplication service by a local client.
63 */ 64 */
64struct BobServiceSession { 65struct BobServiceSession
66{
65 /** 67 /**
66 * (hopefully) unique transaction ID 68 * (hopefully) unique transaction ID
67 */ 69 */
@@ -235,13 +237,13 @@ static struct GNUNET_CADET_Handle *my_cadet;
235 * @param value the value to free 237 * @param value the value to free
236 */ 238 */
237static int 239static int
238free_element_cb(void *cls, 240free_element_cb (void *cls,
239 const struct GNUNET_HashCode *key, 241 const struct GNUNET_HashCode *key,
240 void *value) 242 void *value)
241{ 243{
242 struct GNUNET_SCALARPRODUCT_Element *element = value; 244 struct GNUNET_SCALARPRODUCT_Element *element = value;
243 245
244 GNUNET_free(element); 246 GNUNET_free (element);
245 return GNUNET_OK; 247 return GNUNET_OK;
246} 248}
247 249
@@ -252,7 +254,7 @@ free_element_cb(void *cls,
252 * @param session the session to free elements from 254 * @param session the session to free elements from
253 */ 255 */
254static void 256static void
255destroy_service_session(struct BobServiceSession *s) 257destroy_service_session (struct BobServiceSession *s)
256{ 258{
257 unsigned int i; 259 unsigned int i;
258 260
@@ -260,63 +262,63 @@ destroy_service_session(struct BobServiceSession *s)
260 return; 262 return;
261 s->in_destroy = GNUNET_YES; 263 s->in_destroy = GNUNET_YES;
262 if (NULL != s->client) 264 if (NULL != s->client)
263 { 265 {
264 struct GNUNET_SERVICE_Client *c = s->client; 266 struct GNUNET_SERVICE_Client *c = s->client;
265 267
266 s->client = NULL; 268 s->client = NULL;
267 GNUNET_SERVICE_client_drop(c); 269 GNUNET_SERVICE_client_drop (c);
268 } 270 }
269 if (NULL != s->intersected_elements) 271 if (NULL != s->intersected_elements)
270 { 272 {
271 GNUNET_CONTAINER_multihashmap_iterate(s->intersected_elements, 273 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
272 &free_element_cb, 274 &free_element_cb,
273 NULL); 275 NULL);
274 GNUNET_CONTAINER_multihashmap_destroy(s->intersected_elements); 276 GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
275 s->intersected_elements = NULL; 277 s->intersected_elements = NULL;
276 } 278 }
277 if (NULL != s->intersection_op) 279 if (NULL != s->intersection_op)
278 { 280 {
279 GNUNET_SET_operation_cancel(s->intersection_op); 281 GNUNET_SET_operation_cancel (s->intersection_op);
280 s->intersection_op = NULL; 282 s->intersection_op = NULL;
281 } 283 }
282 if (NULL != s->intersection_set) 284 if (NULL != s->intersection_set)
283 { 285 {
284 GNUNET_SET_destroy(s->intersection_set); 286 GNUNET_SET_destroy (s->intersection_set);
285 s->intersection_set = NULL; 287 s->intersection_set = NULL;
286 } 288 }
287 if (NULL != s->e_a) 289 if (NULL != s->e_a)
288 { 290 {
289 GNUNET_free(s->e_a); 291 GNUNET_free (s->e_a);
290 s->e_a = NULL; 292 s->e_a = NULL;
291 } 293 }
292 if (NULL != s->sorted_elements) 294 if (NULL != s->sorted_elements)
293 { 295 {
294 for (i = 0; i < s->used_element_count; i++) 296 for (i = 0; i < s->used_element_count; i++)
295 gcry_mpi_release(s->sorted_elements[i].value); 297 gcry_mpi_release (s->sorted_elements[i].value);
296 GNUNET_free(s->sorted_elements); 298 GNUNET_free (s->sorted_elements);
297 s->sorted_elements = NULL; 299 s->sorted_elements = NULL;
298 } 300 }
299 if (NULL != s->r) 301 if (NULL != s->r)
300 { 302 {
301 GNUNET_free(s->r); 303 GNUNET_free (s->r);
302 s->r = NULL; 304 s->r = NULL;
303 } 305 }
304 if (NULL != s->r_prime) 306 if (NULL != s->r_prime)
305 { 307 {
306 GNUNET_free(s->r_prime); 308 GNUNET_free (s->r_prime);
307 s->r_prime = NULL; 309 s->r_prime = NULL;
308 } 310 }
309 if (NULL != s->port) 311 if (NULL != s->port)
310 { 312 {
311 GNUNET_CADET_close_port(s->port); 313 GNUNET_CADET_close_port (s->port);
312 s->port = NULL; 314 s->port = NULL;
313 } 315 }
314 if (NULL != s->channel) 316 if (NULL != s->channel)
315 { 317 {
316 GNUNET_CADET_channel_destroy(s->channel); 318 GNUNET_CADET_channel_destroy (s->channel);
317 s->channel = NULL; 319 s->channel = NULL;
318 } 320 }
319 GNUNET_free(s); 321 GNUNET_free (s);
320} 322}
321 323
322 324
@@ -328,24 +330,24 @@ destroy_service_session(struct BobServiceSession *s)
328 * @param session the associated client session to fail or succeed 330 * @param session the associated client session to fail or succeed
329 */ 331 */
330static void 332static void
331prepare_client_end_notification(struct BobServiceSession *session) 333prepare_client_end_notification (struct BobServiceSession *session)
332{ 334{
333 struct ClientResponseMessage *msg; 335 struct ClientResponseMessage *msg;
334 struct GNUNET_MQ_Envelope *e; 336 struct GNUNET_MQ_Envelope *e;
335 337
336 if (NULL == session->client_mq) 338 if (NULL == session->client_mq)
337 return; /* no client left to be notified */ 339 return; /* no client left to be notified */
338 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "Sending session-end notification with status %d to client for session %s\n", 341 "Sending session-end notification with status %d to client for session %s\n",
340 session->status, 342 session->status,
341 GNUNET_h2s(&session->session_id)); 343 GNUNET_h2s (&session->session_id));
342 e = GNUNET_MQ_msg(msg, 344 e = GNUNET_MQ_msg (msg,
343 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT); 345 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
344 msg->range = 0; 346 msg->range = 0;
345 msg->product_length = htonl(0); 347 msg->product_length = htonl (0);
346 msg->status = htonl(session->status); 348 msg->status = htonl (session->status);
347 GNUNET_MQ_send(session->client_mq, 349 GNUNET_MQ_send (session->client_mq,
348 e); 350 e);
349} 351}
350 352
351 353
@@ -359,22 +361,22 @@ prepare_client_end_notification(struct BobServiceSession *session)
359 * @param channel connection to the other end (henceforth invalid) 361 * @param channel connection to the other end (henceforth invalid)
360 */ 362 */
361static void 363static void
362cb_channel_destruction(void *cls, 364cb_channel_destruction (void *cls,
363 const struct GNUNET_CADET_Channel *channel) 365 const struct GNUNET_CADET_Channel *channel)
364{ 366{
365 struct BobServiceSession *s = cls; 367 struct BobServiceSession *s = cls;
366 368
367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 "Peer disconnected, terminating session %s with peer %s\n", 370 "Peer disconnected, terminating session %s with peer %s\n",
369 GNUNET_h2s(&s->session_id), 371 GNUNET_h2s (&s->session_id),
370 GNUNET_i2s(&s->peer)); 372 GNUNET_i2s (&s->peer));
371 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status) 373 if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
372 { 374 {
373 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 375 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
374 prepare_client_end_notification(s); 376 prepare_client_end_notification (s);
375 } 377 }
376 s->channel = NULL; 378 s->channel = NULL;
377 destroy_service_session(s); 379 destroy_service_session (s);
378} 380}
379 381
380 382
@@ -383,19 +385,22 @@ cb_channel_destruction(void *cls,
383 * the client that we are finished. 385 * the client that we are finished.
384 */ 386 */
385static void 387static void
386bob_cadet_done_cb(void *cls) 388bob_cadet_done_cb (void *cls)
387{ 389{
388 struct BobServiceSession *session = cls; 390 struct BobServiceSession *session = cls;
389 391
390 session->status = GNUNET_SCALARPRODUCT_STATUS_SUCCESS; 392 session->status = GNUNET_SCALARPRODUCT_STATUS_SUCCESS;
391 prepare_client_end_notification(session); 393 prepare_client_end_notification (session);
392} 394}
393 395
394 396
395/** 397/**
396 * Maximum count of elements we can put into a multipart message 398 * Maximum count of elements we can put into a multipart message
397 */ 399 */
398#define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof(struct BobCryptodataMultipartMessage)) / sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)) 400#define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 \
401 - sizeof(struct BobCryptodataMultipartMessage)) \
402 / sizeof(struct \
403 GNUNET_CRYPTO_PaillierCiphertext))
399 404
400 405
401/** 406/**
@@ -405,7 +410,7 @@ bob_cadet_done_cb(void *cls)
405 * @param s the associated service session 410 * @param s the associated service session
406 */ 411 */
407static void 412static void
408transmit_bobs_cryptodata_message_multipart(struct BobServiceSession *s) 413transmit_bobs_cryptodata_message_multipart (struct BobServiceSession *s)
409{ 414{
410 struct GNUNET_CRYPTO_PaillierCiphertext *payload; 415 struct GNUNET_CRYPTO_PaillierCiphertext *payload;
411 struct BobCryptodataMultipartMessage *msg; 416 struct BobCryptodataMultipartMessage *msg;
@@ -415,39 +420,42 @@ transmit_bobs_cryptodata_message_multipart(struct BobServiceSession *s)
415 uint32_t todo_count; 420 uint32_t todo_count;
416 421
417 while (s->cadet_transmitted_element_count != s->used_element_count) 422 while (s->cadet_transmitted_element_count != s->used_element_count)
423 {
424 todo_count = s->used_element_count - s->cadet_transmitted_element_count;
425 if (todo_count > ELEMENT_CAPACITY / 2)
426 todo_count = ELEMENT_CAPACITY / 2;
427
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Sending %u additional crypto values to Alice\n",
430 (unsigned int) todo_count);
431 e = GNUNET_MQ_msg_extra (msg,
432 todo_count * sizeof(struct
433 GNUNET_CRYPTO_PaillierCiphertext)
434 * 2,
435 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART);
436 msg->contained_element_count = htonl (todo_count);
437 payload = (struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
438 for (i = s->cadet_transmitted_element_count, j = 0; i <
439 s->cadet_transmitted_element_count + todo_count; i++)
418 { 440 {
419 todo_count = s->used_element_count - s->cadet_transmitted_element_count; 441 // r[i][p] and r[i][q]
420 if (todo_count > ELEMENT_CAPACITY / 2) 442 GNUNET_memcpy (&payload[j++],
421 todo_count = ELEMENT_CAPACITY / 2; 443 &s->r[i],
422 444 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
423 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 445 GNUNET_memcpy (&payload[j++],
424 "Sending %u additional crypto values to Alice\n", 446 &s->r_prime[i],
425 (unsigned int)todo_count); 447 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
426 e = GNUNET_MQ_msg_extra(msg,
427 todo_count * sizeof(struct GNUNET_CRYPTO_PaillierCiphertext) * 2,
428 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART);
429 msg->contained_element_count = htonl(todo_count);
430 payload = (struct GNUNET_CRYPTO_PaillierCiphertext *)&msg[1];
431 for (i = s->cadet_transmitted_element_count, j = 0; i < s->cadet_transmitted_element_count + todo_count; i++)
432 {
433 //r[i][p] and r[i][q]
434 GNUNET_memcpy(&payload[j++],
435 &s->r[i],
436 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
437 GNUNET_memcpy(&payload[j++],
438 &s->r_prime[i],
439 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
440 }
441 s->cadet_transmitted_element_count += todo_count;
442 if (s->cadet_transmitted_element_count == s->used_element_count)
443 GNUNET_MQ_notify_sent(e,
444 &bob_cadet_done_cb,
445 s);
446 GNUNET_MQ_send(s->cadet_mq,
447 e);
448 } 448 }
449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 449 s->cadet_transmitted_element_count += todo_count;
450 "All values queued for Alice, Bob is done\n"); 450 if (s->cadet_transmitted_element_count == s->used_element_count)
451 GNUNET_MQ_notify_sent (e,
452 &bob_cadet_done_cb,
453 s);
454 GNUNET_MQ_send (s->cadet_mq,
455 e);
456 }
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "All values queued for Alice, Bob is done\n");
451} 459}
452 460
453 461
@@ -463,7 +471,7 @@ transmit_bobs_cryptodata_message_multipart(struct BobServiceSession *s)
463 * @param s the associated requesting session with Alice 471 * @param s the associated requesting session with Alice
464 */ 472 */
465static void 473static void
466transmit_bobs_cryptodata_message(struct BobServiceSession *s) 474transmit_bobs_cryptodata_message (struct BobServiceSession *s)
467{ 475{
468 struct BobCryptodataMessage *msg; 476 struct BobCryptodataMessage *msg;
469 struct GNUNET_MQ_Envelope *e; 477 struct GNUNET_MQ_Envelope *e;
@@ -471,49 +479,50 @@ transmit_bobs_cryptodata_message(struct BobServiceSession *s)
471 unsigned int i; 479 unsigned int i;
472 480
473 s->cadet_transmitted_element_count 481 s->cadet_transmitted_element_count
474 = ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof(struct BobCryptodataMessage)) 482 = ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof(struct
483 BobCryptodataMessage))
475 / sizeof(struct GNUNET_CRYPTO_PaillierCiphertext) / 2) - 1; 484 / sizeof(struct GNUNET_CRYPTO_PaillierCiphertext) / 2) - 1;
476 if (s->cadet_transmitted_element_count > s->used_element_count) 485 if (s->cadet_transmitted_element_count > s->used_element_count)
477 s->cadet_transmitted_element_count = s->used_element_count; 486 s->cadet_transmitted_element_count = s->used_element_count;
478 487
479 e = GNUNET_MQ_msg_extra(msg, 488 e = GNUNET_MQ_msg_extra (msg,
480 (2 + s->cadet_transmitted_element_count * 2) 489 (2 + s->cadet_transmitted_element_count * 2)
481 * sizeof(struct GNUNET_CRYPTO_PaillierCiphertext), 490 * sizeof(struct GNUNET_CRYPTO_PaillierCiphertext),
482 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA); 491 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA);
483 msg->contained_element_count = htonl(s->cadet_transmitted_element_count); 492 msg->contained_element_count = htonl (s->cadet_transmitted_element_count);
484 493
485 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486 "Sending %u/%u crypto values to Alice\n", 495 "Sending %u/%u crypto values to Alice\n",
487 (unsigned int)s->cadet_transmitted_element_count, 496 (unsigned int) s->cadet_transmitted_element_count,
488 (unsigned int)s->used_element_count); 497 (unsigned int) s->used_element_count);
489 498
490 payload = (struct GNUNET_CRYPTO_PaillierCiphertext *)&msg[1]; 499 payload = (struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
491 GNUNET_memcpy(&payload[0], 500 GNUNET_memcpy (&payload[0],
492 &s->s, 501 &s->s,
493 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)); 502 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
494 GNUNET_memcpy(&payload[1], 503 GNUNET_memcpy (&payload[1],
495 &s->s_prime, 504 &s->s_prime,
496 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)); 505 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
497 506
498 payload = &payload[2]; 507 payload = &payload[2];
499 // convert k[][] 508 // convert k[][]
500 for (i = 0; i < s->cadet_transmitted_element_count; i++) 509 for (i = 0; i < s->cadet_transmitted_element_count; i++)
501 { 510 {
502 //k[i][p] and k[i][q] 511 // k[i][p] and k[i][q]
503 GNUNET_memcpy(&payload[i * 2], 512 GNUNET_memcpy (&payload[i * 2],
504 &s->r[i], 513 &s->r[i],
505 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)); 514 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
506 GNUNET_memcpy(&payload[i * 2 + 1], 515 GNUNET_memcpy (&payload[i * 2 + 1],
507 &s->r_prime[i], 516 &s->r_prime[i],
508 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)); 517 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext));
509 } 518 }
510 if (s->cadet_transmitted_element_count == s->used_element_count) 519 if (s->cadet_transmitted_element_count == s->used_element_count)
511 GNUNET_MQ_notify_sent(e, 520 GNUNET_MQ_notify_sent (e,
512 &bob_cadet_done_cb, 521 &bob_cadet_done_cb,
513 s); 522 s);
514 GNUNET_MQ_send(s->cadet_mq, 523 GNUNET_MQ_send (s->cadet_mq,
515 e); 524 e);
516 transmit_bobs_cryptodata_message_multipart(s); 525 transmit_bobs_cryptodata_message_multipart (s);
517} 526}
518#undef ELEMENT_CAPACITY 527#undef ELEMENT_CAPACITY
519 528
@@ -527,21 +536,21 @@ transmit_bobs_cryptodata_message(struct BobServiceSession *s)
527 * TODO: code duplication with Alice! 536 * TODO: code duplication with Alice!
528 */ 537 */
529static gcry_mpi_t 538static gcry_mpi_t
530compute_square_sum(const gcry_mpi_t *vector, 539compute_square_sum (const gcry_mpi_t *vector,
531 uint32_t length) 540 uint32_t length)
532{ 541{
533 gcry_mpi_t elem; 542 gcry_mpi_t elem;
534 gcry_mpi_t sum; 543 gcry_mpi_t sum;
535 uint32_t i; 544 uint32_t i;
536 545
537 GNUNET_assert(NULL != (sum = gcry_mpi_new(0))); 546 GNUNET_assert (NULL != (sum = gcry_mpi_new (0)));
538 GNUNET_assert(NULL != (elem = gcry_mpi_new(0))); 547 GNUNET_assert (NULL != (elem = gcry_mpi_new (0)));
539 for (i = 0; i < length; i++) 548 for (i = 0; i < length; i++)
540 { 549 {
541 gcry_mpi_mul(elem, vector[i], vector[i]); 550 gcry_mpi_mul (elem, vector[i], vector[i]);
542 gcry_mpi_add(sum, sum, elem); 551 gcry_mpi_add (sum, sum, elem);
543 } 552 }
544 gcry_mpi_release(elem); 553 gcry_mpi_release (elem);
545 return sum; 554 return sum;
546} 555}
547 556
@@ -557,7 +566,7 @@ compute_square_sum(const gcry_mpi_t *vector,
557 * @return #GNUNET_OK on success 566 * @return #GNUNET_OK on success
558 */ 567 */
559static int 568static int
560compute_service_response(struct BobServiceSession *session) 569compute_service_response (struct BobServiceSession *session)
561{ 570{
562 uint32_t i; 571 uint32_t i;
563 unsigned int *p; 572 unsigned int *p;
@@ -573,125 +582,126 @@ compute_service_response(struct BobServiceSession *session)
573 count = session->used_element_count; 582 count = session->used_element_count;
574 a = session->e_a; 583 a = session->e_a;
575 b = session->sorted_elements; 584 b = session->sorted_elements;
576 q = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, 585 q = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
577 count); 586 count);
578 p = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, 587 p = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
579 count); 588 count);
580 rand = GNUNET_malloc(sizeof(gcry_mpi_t) * count); 589 rand = GNUNET_malloc (sizeof(gcry_mpi_t) * count);
581 for (i = 0; i < count; i++) 590 for (i = 0; i < count; i++)
582 GNUNET_assert(NULL != (rand[i] = gcry_mpi_new(0))); 591 GNUNET_assert (NULL != (rand[i] = gcry_mpi_new (0)));
583 r = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_PaillierCiphertext) * count); 592 r = GNUNET_malloc (sizeof(struct GNUNET_CRYPTO_PaillierCiphertext) * count);
584 r_prime = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_PaillierCiphertext) * count); 593 r_prime = GNUNET_malloc (sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)
594 * count);
585 595
586 for (i = 0; i < count; i++) 596 for (i = 0; i < count; i++)
587 { 597 {
588 int32_t svalue; 598 int32_t svalue;
589 599
590 svalue = (int32_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 600 svalue = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
591 UINT32_MAX); 601 UINT32_MAX);
592 // long to gcry_mpi_t 602 // long to gcry_mpi_t
593 if (svalue < 0) 603 if (svalue < 0)
594 gcry_mpi_sub_ui(rand[i], 604 gcry_mpi_sub_ui (rand[i],
595 rand[i], 605 rand[i],
596 -svalue); 606 -svalue);
597 else 607 else
598 rand[i] = gcry_mpi_set_ui(rand[i], svalue); 608 rand[i] = gcry_mpi_set_ui (rand[i], svalue);
599 } 609 }
600 610
601 tmp = gcry_mpi_new(0); 611 tmp = gcry_mpi_new (0);
602 // encrypt the element 612 // encrypt the element
603 // for the sake of readability I decided to have dedicated permutation 613 // for the sake of readability I decided to have dedicated permutation
604 // vectors, which get rid of all the lookups in p/q. 614 // vectors, which get rid of all the lookups in p/q.
605 // however, ap/aq are not absolutely necessary but are just abstraction 615 // however, ap/aq are not absolutely necessary but are just abstraction
606 // Calculate Kp = E(S + a_pi) (+) E(S - r_pi - b_pi) 616 // Calculate Kp = E(S + a_pi) (+) E(S - r_pi - b_pi)
607 for (i = 0; i < count; i++) 617 for (i = 0; i < count; i++)
608 { 618 {
609 // E(S - r_pi - b_pi) 619 // E(S - r_pi - b_pi)
610 gcry_mpi_sub(tmp, my_offset, rand[p[i]]); 620 gcry_mpi_sub (tmp, my_offset, rand[p[i]]);
611 gcry_mpi_sub(tmp, tmp, b[p[i]].value); 621 gcry_mpi_sub (tmp, tmp, b[p[i]].value);
612 GNUNET_assert(2 == 622 GNUNET_assert (2 ==
613 GNUNET_CRYPTO_paillier_encrypt(&session->remote_pubkey, 623 GNUNET_CRYPTO_paillier_encrypt (&session->remote_pubkey,
614 tmp, 624 tmp,
615 2, 625 2,
616 &r[i])); 626 &r[i]));
617 627
618 // E(S - r_pi - b_pi) * E(S + a_pi) == E(2*S + a - r - b) 628 // E(S - r_pi - b_pi) * E(S + a_pi) == E(2*S + a - r - b)
619 if (GNUNET_OK != 629 if (GNUNET_OK !=
620 GNUNET_CRYPTO_paillier_hom_add(&session->remote_pubkey, 630 GNUNET_CRYPTO_paillier_hom_add (&session->remote_pubkey,
621 &r[i], 631 &r[i],
622 &a[p[i]], 632 &a[p[i]],
623 &r[i])) 633 &r[i]))
624 { 634 {
625 GNUNET_break_op(0); 635 GNUNET_break_op (0);
626 goto error_cleanup; 636 goto error_cleanup;
627 }
628 } 637 }
638 }
629 639
630 // Calculate Kq = E(S + a_qi) (+) E(S - r_qi) 640 // Calculate Kq = E(S + a_qi) (+) E(S - r_qi)
631 for (i = 0; i < count; i++) 641 for (i = 0; i < count; i++)
632 { 642 {
633 // E(S - r_qi) 643 // E(S - r_qi)
634 gcry_mpi_sub(tmp, my_offset, rand[q[i]]); 644 gcry_mpi_sub (tmp, my_offset, rand[q[i]]);
635 GNUNET_assert(2 == 645 GNUNET_assert (2 ==
636 GNUNET_CRYPTO_paillier_encrypt(&session->remote_pubkey, 646 GNUNET_CRYPTO_paillier_encrypt (&session->remote_pubkey,
637 tmp, 647 tmp,
638 2, 648 2,
639 &r_prime[i])); 649 &r_prime[i]));
640 650
641 // E(S - r_qi) * E(S + a_qi) == E(2*S + a_qi - r_qi) 651 // E(S - r_qi) * E(S + a_qi) == E(2*S + a_qi - r_qi)
642 if (GNUNET_OK != 652 if (GNUNET_OK !=
643 GNUNET_CRYPTO_paillier_hom_add(&session->remote_pubkey, 653 GNUNET_CRYPTO_paillier_hom_add (&session->remote_pubkey,
644 &r_prime[i], 654 &r_prime[i],
645 &a[q[i]], 655 &a[q[i]],
646 &r_prime[i])) 656 &r_prime[i]))
647 { 657 {
648 GNUNET_break_op(0); 658 GNUNET_break_op (0);
649 goto error_cleanup; 659 goto error_cleanup;
650 }
651 } 660 }
652 gcry_mpi_release(tmp); 661 }
662 gcry_mpi_release (tmp);
653 663
654 // Calculate S' = E(SUM( r_i^2 )) 664 // Calculate S' = E(SUM( r_i^2 ))
655 tmp = compute_square_sum(rand, count); 665 tmp = compute_square_sum (rand, count);
656 GNUNET_assert(1 == 666 GNUNET_assert (1 ==
657 GNUNET_CRYPTO_paillier_encrypt(&session->remote_pubkey, 667 GNUNET_CRYPTO_paillier_encrypt (&session->remote_pubkey,
658 tmp, 668 tmp,
659 1, 669 1,
660 &session->s_prime)); 670 &session->s_prime));
661 gcry_mpi_release(tmp); 671 gcry_mpi_release (tmp);
662 672
663 // Calculate S = E(SUM( (r_i + b_i)^2 )) 673 // Calculate S = E(SUM( (r_i + b_i)^2 ))
664 for (i = 0; i < count; i++) 674 for (i = 0; i < count; i++)
665 gcry_mpi_add(rand[i], rand[i], b[i].value); 675 gcry_mpi_add (rand[i], rand[i], b[i].value);
666 tmp = compute_square_sum(rand, count); 676 tmp = compute_square_sum (rand, count);
667 GNUNET_assert(1 == 677 GNUNET_assert (1 ==
668 GNUNET_CRYPTO_paillier_encrypt(&session->remote_pubkey, 678 GNUNET_CRYPTO_paillier_encrypt (&session->remote_pubkey,
669 tmp, 679 tmp,
670 1, 680 1,
671 &session->s)); 681 &session->s));
672 gcry_mpi_release(tmp); 682 gcry_mpi_release (tmp);
673 683
674 session->r = r; 684 session->r = r;
675 session->r_prime = r_prime; 685 session->r_prime = r_prime;
676 686
677 for (i = 0; i < count; i++) 687 for (i = 0; i < count; i++)
678 gcry_mpi_release(rand[i]); 688 gcry_mpi_release (rand[i]);
679 GNUNET_free(session->e_a); 689 GNUNET_free (session->e_a);
680 session->e_a = NULL; 690 session->e_a = NULL;
681 GNUNET_free(p); 691 GNUNET_free (p);
682 GNUNET_free(q); 692 GNUNET_free (q);
683 GNUNET_free(rand); 693 GNUNET_free (rand);
684 return GNUNET_OK; 694 return GNUNET_OK;
685 695
686error_cleanup: 696error_cleanup:
687 GNUNET_free(r); 697 GNUNET_free (r);
688 GNUNET_free(r_prime); 698 GNUNET_free (r_prime);
689 gcry_mpi_release(tmp); 699 gcry_mpi_release (tmp);
690 GNUNET_free(p); 700 GNUNET_free (p);
691 GNUNET_free(q); 701 GNUNET_free (q);
692 for (i = 0; i < count; i++) 702 for (i = 0; i < count; i++)
693 gcry_mpi_release(rand[i]); 703 gcry_mpi_release (rand[i]);
694 GNUNET_free(rand); 704 GNUNET_free (rand);
695 return GNUNET_SYSERR; 705 return GNUNET_SYSERR;
696} 706}
697 707
@@ -706,21 +716,21 @@ error_cleanup:
706 * TODO: code duplication with Alice! 716 * TODO: code duplication with Alice!
707 */ 717 */
708static int 718static int
709copy_element_cb(void *cls, 719copy_element_cb (void *cls,
710 const struct GNUNET_HashCode *key, 720 const struct GNUNET_HashCode *key,
711 void *value) 721 void *value)
712{ 722{
713 struct BobServiceSession *s = cls; 723 struct BobServiceSession *s = cls;
714 struct GNUNET_SCALARPRODUCT_Element *e = value; 724 struct GNUNET_SCALARPRODUCT_Element *e = value;
715 gcry_mpi_t mval; 725 gcry_mpi_t mval;
716 int64_t val; 726 int64_t val;
717 727
718 mval = gcry_mpi_new(0); 728 mval = gcry_mpi_new (0);
719 val = (int64_t)GNUNET_ntohll(e->value); 729 val = (int64_t) GNUNET_ntohll (e->value);
720 if (0 > val) 730 if (0 > val)
721 gcry_mpi_sub_ui(mval, mval, -val); 731 gcry_mpi_sub_ui (mval, mval, -val);
722 else 732 else
723 gcry_mpi_add_ui(mval, mval, val); 733 gcry_mpi_add_ui (mval, mval, val);
724 s->sorted_elements [s->used_element_count].value = mval; 734 s->sorted_elements [s->used_element_count].value = mval;
725 s->sorted_elements [s->used_element_count].key = &e->key; 735 s->sorted_elements [s->used_element_count].key = &e->key;
726 s->used_element_count++; 736 s->used_element_count++;
@@ -737,14 +747,14 @@ copy_element_cb(void *cls,
737 * TODO: code duplication with Alice! 747 * TODO: code duplication with Alice!
738 */ 748 */
739static int 749static int
740element_cmp(const void *a, 750element_cmp (const void *a,
741 const void *b) 751 const void *b)
742{ 752{
743 const struct MpiElement *ma = a; 753 const struct MpiElement *ma = a;
744 const struct MpiElement *mb = b; 754 const struct MpiElement *mb = b;
745 755
746 return GNUNET_CRYPTO_hash_cmp(ma->key, 756 return GNUNET_CRYPTO_hash_cmp (ma->key,
747 mb->key); 757 mb->key);
748} 758}
749 759
750 760
@@ -756,33 +766,34 @@ element_cmp(const void *a,
756 * @param s session to transmit reply for. 766 * @param s session to transmit reply for.
757 */ 767 */
758static void 768static void
759transmit_cryptographic_reply(struct BobServiceSession *s) 769transmit_cryptographic_reply (struct BobServiceSession *s)
760{ 770{
761 struct GNUNET_CADET_Channel *channel; 771 struct GNUNET_CADET_Channel *channel;
762 772
763 /* TODO: code duplication with Alice! */ 773 /* TODO: code duplication with Alice! */
764 LOG(GNUNET_ERROR_TYPE_DEBUG, 774 LOG (GNUNET_ERROR_TYPE_DEBUG,
765 "Received everything, building reply for Alice\n"); 775 "Received everything, building reply for Alice\n");
766 s->sorted_elements 776 s->sorted_elements
767 = GNUNET_malloc(GNUNET_CONTAINER_multihashmap_size(s->intersected_elements) * 777 = GNUNET_malloc (GNUNET_CONTAINER_multihashmap_size (
768 sizeof(struct MpiElement)); 778 s->intersected_elements)
779 * sizeof(struct MpiElement));
769 s->used_element_count = 0; 780 s->used_element_count = 0;
770 GNUNET_CONTAINER_multihashmap_iterate(s->intersected_elements, 781 GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
771 &copy_element_cb, 782 &copy_element_cb,
772 s); 783 s);
773 qsort(s->sorted_elements, 784 qsort (s->sorted_elements,
774 s->used_element_count, 785 s->used_element_count,
775 sizeof(struct MpiElement), 786 sizeof(struct MpiElement),
776 &element_cmp); 787 &element_cmp);
777 if (GNUNET_OK != 788 if (GNUNET_OK !=
778 compute_service_response(s)) 789 compute_service_response (s))
779 { 790 {
780 channel = s->channel; 791 channel = s->channel;
781 s->channel = NULL; 792 s->channel = NULL;
782 GNUNET_CADET_channel_destroy(channel); 793 GNUNET_CADET_channel_destroy (channel);
783 return; 794 return;
784 } 795 }
785 transmit_bobs_cryptodata_message(s); 796 transmit_bobs_cryptodata_message (s);
786} 797}
787 798
788 799
@@ -796,8 +807,8 @@ transmit_cryptographic_reply(struct BobServiceSession *s)
796 * #GNUNET_SYSERR to close it (signal serious error) 807 * #GNUNET_SYSERR to close it (signal serious error)
797 */ 808 */
798static int 809static int
799check_alices_cryptodata_message(void *cls, 810check_alices_cryptodata_message (void *cls,
800 const struct AliceCryptodataMessage *msg) 811 const struct AliceCryptodataMessage *msg)
801{ 812{
802 struct BobServiceSession *s = cls; 813 struct BobServiceSession *s = cls;
803 uint32_t contained_elements; 814 uint32_t contained_elements;
@@ -805,21 +816,22 @@ check_alices_cryptodata_message(void *cls,
805 uint16_t msize; 816 uint16_t msize;
806 unsigned int max; 817 unsigned int max;
807 818
808 msize = ntohs(msg->header.size); 819 msize = ntohs (msg->header.size);
809 contained_elements = ntohl(msg->contained_element_count); 820 contained_elements = ntohl (msg->contained_element_count);
810 /* Our intersection may still be ongoing, but this is nevertheless 821 /* Our intersection may still be ongoing, but this is nevertheless
811 an upper bound on the required array size */ 822 an upper bound on the required array size */
812 max = GNUNET_CONTAINER_multihashmap_size(s->intersected_elements); 823 max = GNUNET_CONTAINER_multihashmap_size (s->intersected_elements);
813 msg_length = sizeof(struct AliceCryptodataMessage) 824 msg_length = sizeof(struct AliceCryptodataMessage)
814 + contained_elements * sizeof(struct GNUNET_CRYPTO_PaillierCiphertext); 825 + contained_elements * sizeof(struct
826 GNUNET_CRYPTO_PaillierCiphertext);
815 if ((msize != msg_length) || 827 if ((msize != msg_length) ||
816 (0 == contained_elements) || 828 (0 == contained_elements) ||
817 (contained_elements > UINT16_MAX) || 829 (contained_elements > UINT16_MAX) ||
818 (max < contained_elements + s->cadet_received_element_count)) 830 (max < contained_elements + s->cadet_received_element_count))
819 { 831 {
820 GNUNET_break_op(0); 832 GNUNET_break_op (0);
821 return GNUNET_SYSERR; 833 return GNUNET_SYSERR;
822 } 834 }
823 return GNUNET_OK; 835 return GNUNET_OK;
824} 836}
825 837
@@ -832,40 +844,41 @@ check_alices_cryptodata_message(void *cls,
832 * @param msg the actual message 844 * @param msg the actual message
833 */ 845 */
834static void 846static void
835handle_alices_cryptodata_message(void *cls, 847handle_alices_cryptodata_message (void *cls,
836 const struct AliceCryptodataMessage *msg) 848 const struct AliceCryptodataMessage *msg)
837{ 849{
838 struct BobServiceSession *s = cls; 850 struct BobServiceSession *s = cls;
839 const struct GNUNET_CRYPTO_PaillierCiphertext *payload; 851 const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
840 uint32_t contained_elements; 852 uint32_t contained_elements;
841 unsigned int max; 853 unsigned int max;
842 854
843 contained_elements = ntohl(msg->contained_element_count); 855 contained_elements = ntohl (msg->contained_element_count);
844 /* Our intersection may still be ongoing, but this is nevertheless 856 /* Our intersection may still be ongoing, but this is nevertheless
845 an upper bound on the required array size */ 857 an upper bound on the required array size */
846 max = GNUNET_CONTAINER_multihashmap_size(s->intersected_elements); 858 max = GNUNET_CONTAINER_multihashmap_size (s->intersected_elements);
847 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Received %u crypto values from Alice\n", 860 "Received %u crypto values from Alice\n",
849 (unsigned int)contained_elements); 861 (unsigned int) contained_elements);
850 862
851 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *)&msg[1]; 863 payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
852 if (NULL == s->e_a) 864 if (NULL == s->e_a)
853 s->e_a = GNUNET_new_array(max, 865 s->e_a = GNUNET_new_array (max,
854 struct GNUNET_CRYPTO_PaillierCiphertext); 866 struct GNUNET_CRYPTO_PaillierCiphertext);
855 GNUNET_memcpy(&s->e_a[s->cadet_received_element_count], 867 GNUNET_memcpy (&s->e_a[s->cadet_received_element_count],
856 payload, 868 payload,
857 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext) * contained_elements); 869 sizeof(struct GNUNET_CRYPTO_PaillierCiphertext)
870 * contained_elements);
858 s->cadet_received_element_count += contained_elements; 871 s->cadet_received_element_count += contained_elements;
859 872
860 if ((s->cadet_received_element_count == max) && 873 if ((s->cadet_received_element_count == max) &&
861 (NULL == s->intersection_op)) 874 (NULL == s->intersection_op))
862 { 875 {
863 /* intersection has finished also on our side, and 876 /* intersection has finished also on our side, and
864 we got the full set, so we can proceed with the 877 we got the full set, so we can proceed with the
865 CADET response(s) */ 878 CADET response(s) */
866 transmit_cryptographic_reply(s); 879 transmit_cryptographic_reply (s);
867 } 880 }
868 GNUNET_CADET_receive_done(s->channel); 881 GNUNET_CADET_receive_done (s->channel);
869} 882}
870 883
871 884
@@ -879,71 +892,72 @@ handle_alices_cryptodata_message(void *cls,
879 * @param status what has happened with the set intersection? 892 * @param status what has happened with the set intersection?
880 */ 893 */
881static void 894static void
882cb_intersection_element_removed(void *cls, 895cb_intersection_element_removed (void *cls,
883 const struct GNUNET_SET_Element *element, 896 const struct GNUNET_SET_Element *element,
884 uint64_t current_size, 897 uint64_t current_size,
885 enum GNUNET_SET_Status status) 898 enum GNUNET_SET_Status status)
886{ 899{
887 struct BobServiceSession *s = cls; 900 struct BobServiceSession *s = cls;
888 struct GNUNET_SCALARPRODUCT_Element *se; 901 struct GNUNET_SCALARPRODUCT_Element *se;
889 902
890 switch (status) 903 switch (status)
904 {
905 case GNUNET_SET_STATUS_OK:
906 /* this element has been removed from the set */
907 se = GNUNET_CONTAINER_multihashmap_get (s->intersected_elements,
908 element->data);
909 GNUNET_assert (NULL != se);
910 LOG (GNUNET_ERROR_TYPE_DEBUG,
911 "Removed element with key %s and value %lld\n",
912 GNUNET_h2s (&se->key),
913 (long long) GNUNET_ntohll (se->value));
914 GNUNET_assert (GNUNET_YES ==
915 GNUNET_CONTAINER_multihashmap_remove (
916 s->intersected_elements,
917 element->data,
918 se));
919 GNUNET_free (se);
920 return;
921
922 case GNUNET_SET_STATUS_DONE:
923 s->intersection_op = NULL;
924 GNUNET_break (NULL == s->intersection_set);
925 GNUNET_CADET_receive_done (s->channel);
926 LOG (GNUNET_ERROR_TYPE_DEBUG,
927 "Finished intersection, %d items remain\n",
928 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements));
929 if (s->client_received_element_count ==
930 GNUNET_CONTAINER_multihashmap_size (s->intersected_elements))
891 { 931 {
892 case GNUNET_SET_STATUS_OK: 932 /* CADET transmission from Alice is also already done,
893 /* this element has been removed from the set */ 933 start with our own reply */
894 se = GNUNET_CONTAINER_multihashmap_get(s->intersected_elements, 934 transmit_cryptographic_reply (s);
895 element->data);
896 GNUNET_assert(NULL != se);
897 LOG(GNUNET_ERROR_TYPE_DEBUG,
898 "Removed element with key %s and value %lld\n",
899 GNUNET_h2s(&se->key),
900 (long long)GNUNET_ntohll(se->value));
901 GNUNET_assert(GNUNET_YES ==
902 GNUNET_CONTAINER_multihashmap_remove(s->intersected_elements,
903 element->data,
904 se));
905 GNUNET_free(se);
906 return;
907
908 case GNUNET_SET_STATUS_DONE:
909 s->intersection_op = NULL;
910 GNUNET_break(NULL == s->intersection_set);
911 GNUNET_CADET_receive_done(s->channel);
912 LOG(GNUNET_ERROR_TYPE_DEBUG,
913 "Finished intersection, %d items remain\n",
914 GNUNET_CONTAINER_multihashmap_size(s->intersected_elements));
915 if (s->client_received_element_count ==
916 GNUNET_CONTAINER_multihashmap_size(s->intersected_elements))
917 {
918 /* CADET transmission from Alice is also already done,
919 start with our own reply */
920 transmit_cryptographic_reply(s);
921 }
922 return;
923
924 case GNUNET_SET_STATUS_HALF_DONE:
925 /* unexpected for intersection */
926 GNUNET_break(0);
927 return;
928
929 case GNUNET_SET_STATUS_FAILURE:
930 /* unhandled status code */
931 LOG(GNUNET_ERROR_TYPE_DEBUG,
932 "Set intersection failed!\n");
933 s->intersection_op = NULL;
934 if (NULL != s->intersection_set)
935 {
936 GNUNET_SET_destroy(s->intersection_set);
937 s->intersection_set = NULL;
938 }
939 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
940 prepare_client_end_notification(s);
941 return;
942
943 default:
944 GNUNET_break(0);
945 return;
946 } 935 }
936 return;
937
938 case GNUNET_SET_STATUS_HALF_DONE:
939 /* unexpected for intersection */
940 GNUNET_break (0);
941 return;
942
943 case GNUNET_SET_STATUS_FAILURE:
944 /* unhandled status code */
945 LOG (GNUNET_ERROR_TYPE_DEBUG,
946 "Set intersection failed!\n");
947 s->intersection_op = NULL;
948 if (NULL != s->intersection_set)
949 {
950 GNUNET_SET_destroy (s->intersection_set);
951 s->intersection_set = NULL;
952 }
953 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
954 prepare_client_end_notification (s);
955 return;
956
957 default:
958 GNUNET_break (0);
959 return;
960 }
947} 961}
948 962
949 963
@@ -954,31 +968,31 @@ cb_intersection_element_removed(void *cls,
954 * @param s client session to start intersection for 968 * @param s client session to start intersection for
955 */ 969 */
956static void 970static void
957start_intersection(struct BobServiceSession *s) 971start_intersection (struct BobServiceSession *s)
958{ 972{
959 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
960 "Got session with key %s and %u elements, starting intersection.\n", 974 "Got session with key %s and %u elements, starting intersection.\n",
961 GNUNET_h2s(&s->session_id), 975 GNUNET_h2s (&s->session_id),
962 (unsigned int)s->total); 976 (unsigned int) s->total);
963 977
964 s->intersection_op 978 s->intersection_op
965 = GNUNET_SET_prepare(&s->peer, 979 = GNUNET_SET_prepare (&s->peer,
966 &s->session_id, 980 &s->session_id,
967 NULL, 981 NULL,
968 GNUNET_SET_RESULT_REMOVED, 982 GNUNET_SET_RESULT_REMOVED,
969 (struct GNUNET_SET_Option[]) { { 0 } }, 983 (struct GNUNET_SET_Option[]) { { 0 } },
970 &cb_intersection_element_removed, 984 &cb_intersection_element_removed,
971 s); 985 s);
972 if (GNUNET_OK != 986 if (GNUNET_OK !=
973 GNUNET_SET_commit(s->intersection_op, 987 GNUNET_SET_commit (s->intersection_op,
974 s->intersection_set)) 988 s->intersection_set))
975 { 989 {
976 GNUNET_break(0); 990 GNUNET_break (0);
977 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE; 991 s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
978 prepare_client_end_notification(s); 992 prepare_client_end_notification (s);
979 return; 993 return;
980 } 994 }
981 GNUNET_SET_destroy(s->intersection_set); 995 GNUNET_SET_destroy (s->intersection_set);
982 s->intersection_set = NULL; 996 s->intersection_set = NULL;
983} 997}
984 998
@@ -990,15 +1004,15 @@ start_intersection(struct BobServiceSession *s)
990 * @param msg the actual message 1004 * @param msg the actual message
991 */ 1005 */
992static void 1006static void
993handle_alices_computation_request(void *cls, 1007handle_alices_computation_request (void *cls,
994 const struct ServiceRequestMessage *msg) 1008 const struct ServiceRequestMessage *msg)
995{ 1009{
996 struct BobServiceSession *s = cls; 1010 struct BobServiceSession *s = cls;
997 1011
998 s->session_id = msg->session_id; // ?? 1012 s->session_id = msg->session_id; // ??
999 s->remote_pubkey = msg->public_key; 1013 s->remote_pubkey = msg->public_key;
1000 if (s->client_received_element_count == s->total) 1014 if (s->client_received_element_count == s->total)
1001 start_intersection(s); 1015 start_intersection (s);
1002} 1016}
1003 1017
1004 1018
@@ -1013,20 +1027,20 @@ handle_alices_computation_request(void *cls,
1013 * @return session associated with the channel 1027 * @return session associated with the channel
1014 */ 1028 */
1015static void * 1029static void *
1016cb_channel_incoming(void *cls, 1030cb_channel_incoming (void *cls,
1017 struct GNUNET_CADET_Channel *channel, 1031 struct GNUNET_CADET_Channel *channel,
1018 const struct GNUNET_PeerIdentity *initiator) 1032 const struct GNUNET_PeerIdentity *initiator)
1019{ 1033{
1020 struct BobServiceSession *s = cls; 1034 struct BobServiceSession *s = cls;
1021 1035
1022 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1023 "New incoming channel from peer %s.\n", 1037 "New incoming channel from peer %s.\n",
1024 GNUNET_i2s(initiator)); 1038 GNUNET_i2s (initiator));
1025 GNUNET_CADET_close_port(s->port); 1039 GNUNET_CADET_close_port (s->port);
1026 s->port = NULL; 1040 s->port = NULL;
1027 s->channel = channel; 1041 s->channel = channel;
1028 s->peer = *initiator; 1042 s->peer = *initiator;
1029 s->cadet_mq = GNUNET_CADET_get_mq(s->channel); 1043 s->cadet_mq = GNUNET_CADET_get_mq (s->channel);
1030 return s; 1044 return s;
1031} 1045}
1032 1046
@@ -1039,25 +1053,28 @@ cb_channel_incoming(void *cls,
1039 * @return #GNUNET_OK if @a msg is well-formed 1053 * @return #GNUNET_OK if @a msg is well-formed
1040 */ 1054 */
1041static int 1055static int
1042check_bob_client_message_multipart(void *cls, 1056check_bob_client_message_multipart (void *cls,
1043 const struct ComputationBobCryptodataMultipartMessage *msg) 1057 const struct
1058 ComputationBobCryptodataMultipartMessage *
1059 msg)
1044{ 1060{
1045 struct BobServiceSession *s = cls; 1061 struct BobServiceSession *s = cls;
1046 uint32_t contained_count; 1062 uint32_t contained_count;
1047 uint16_t msize; 1063 uint16_t msize;
1048 1064
1049 msize = ntohs(msg->header.size); 1065 msize = ntohs (msg->header.size);
1050 contained_count = ntohl(msg->element_count_contained); 1066 contained_count = ntohl (msg->element_count_contained);
1051 if ((msize != (sizeof(struct ComputationBobCryptodataMultipartMessage) + 1067 if ((msize != (sizeof(struct ComputationBobCryptodataMultipartMessage)
1052 contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element))) || 1068 + contained_count * sizeof(struct
1069 GNUNET_SCALARPRODUCT_Element))) ||
1053 (0 == contained_count) || 1070 (0 == contained_count) ||
1054 (UINT16_MAX < contained_count) || 1071 (UINT16_MAX < contained_count) ||
1055 (s->total == s->client_received_element_count) || 1072 (s->total == s->client_received_element_count) ||
1056 (s->total < s->client_received_element_count + contained_count)) 1073 (s->total < s->client_received_element_count + contained_count))
1057 { 1074 {
1058 GNUNET_break(0); 1075 GNUNET_break (0);
1059 return GNUNET_SYSERR; 1076 return GNUNET_SYSERR;
1060 } 1077 }
1061 return GNUNET_OK; 1078 return GNUNET_OK;
1062} 1079}
1063 1080
@@ -1070,8 +1087,10 @@ check_bob_client_message_multipart(void *cls,
1070 * @param msg the actual message 1087 * @param msg the actual message
1071 */ 1088 */
1072static void 1089static void
1073handle_bob_client_message_multipart(void *cls, 1090handle_bob_client_message_multipart (void *cls,
1074 const struct ComputationBobCryptodataMultipartMessage *msg) 1091 const struct
1092 ComputationBobCryptodataMultipartMessage *
1093 msg)
1075{ 1094{
1076 struct BobServiceSession *s = cls; 1095 struct BobServiceSession *s = cls;
1077 uint32_t contained_count; 1096 uint32_t contained_count;
@@ -1079,44 +1098,44 @@ handle_bob_client_message_multipart(void *cls,
1079 struct GNUNET_SET_Element set_elem; 1098 struct GNUNET_SET_Element set_elem;
1080 struct GNUNET_SCALARPRODUCT_Element *elem; 1099 struct GNUNET_SCALARPRODUCT_Element *elem;
1081 1100
1082 contained_count = ntohl(msg->element_count_contained); 1101 contained_count = ntohl (msg->element_count_contained);
1083 elements = (const struct GNUNET_SCALARPRODUCT_Element *)&msg[1]; 1102 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1084 for (uint32_t i = 0; i < contained_count; i++) 1103 for (uint32_t i = 0; i < contained_count; i++)
1104 {
1105 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1106 GNUNET_memcpy (elem,
1107 &elements[i],
1108 sizeof(struct GNUNET_SCALARPRODUCT_Element));
1109 if (GNUNET_SYSERR ==
1110 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1111 &elem->key,
1112 elem,
1113 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1085 { 1114 {
1086 elem = GNUNET_new(struct GNUNET_SCALARPRODUCT_Element); 1115 GNUNET_break (0);
1087 GNUNET_memcpy(elem, 1116 GNUNET_free (elem);
1088 &elements[i], 1117 continue;
1089 sizeof(struct GNUNET_SCALARPRODUCT_Element));
1090 if (GNUNET_SYSERR ==
1091 GNUNET_CONTAINER_multihashmap_put(s->intersected_elements,
1092 &elem->key,
1093 elem,
1094 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1095 {
1096 GNUNET_break(0);
1097 GNUNET_free(elem);
1098 continue;
1099 }
1100 set_elem.data = &elem->key;
1101 set_elem.size = sizeof(elem->key);
1102 set_elem.element_type = 0;
1103 GNUNET_SET_add_element(s->intersection_set,
1104 &set_elem,
1105 NULL, NULL);
1106 } 1118 }
1119 set_elem.data = &elem->key;
1120 set_elem.size = sizeof(elem->key);
1121 set_elem.element_type = 0;
1122 GNUNET_SET_add_element (s->intersection_set,
1123 &set_elem,
1124 NULL, NULL);
1125 }
1107 s->client_received_element_count += contained_count; 1126 s->client_received_element_count += contained_count;
1108 GNUNET_SERVICE_client_continue(s->client); 1127 GNUNET_SERVICE_client_continue (s->client);
1109 if (s->total != s->client_received_element_count) 1128 if (s->total != s->client_received_element_count)
1110 { 1129 {
1111 /* more to come */ 1130 /* more to come */
1112 return; 1131 return;
1113 } 1132 }
1114 if (NULL == s->channel) 1133 if (NULL == s->channel)
1115 { 1134 {
1116 /* no Alice waiting for this request, wait for Alice */ 1135 /* no Alice waiting for this request, wait for Alice */
1117 return; 1136 return;
1118 } 1137 }
1119 start_intersection(s); 1138 start_intersection (s);
1120} 1139}
1121 1140
1122 1141
@@ -1129,8 +1148,8 @@ handle_bob_client_message_multipart(void *cls,
1129 * @return #GNUNET_OK if @a msg is well-formed 1148 * @return #GNUNET_OK if @a msg is well-formed
1130 */ 1149 */
1131static int 1150static int
1132check_bob_client_message(void *cls, 1151check_bob_client_message (void *cls,
1133 const struct BobComputationMessage *msg) 1152 const struct BobComputationMessage *msg)
1134{ 1153{
1135 struct BobServiceSession *s = cls; 1154 struct BobServiceSession *s = cls;
1136 uint32_t contained_count; 1155 uint32_t contained_count;
@@ -1138,22 +1157,23 @@ check_bob_client_message(void *cls,
1138 uint16_t msize; 1157 uint16_t msize;
1139 1158
1140 if (GNUNET_SCALARPRODUCT_STATUS_INIT != s->status) 1159 if (GNUNET_SCALARPRODUCT_STATUS_INIT != s->status)
1141 { 1160 {
1142 GNUNET_break(0); 1161 GNUNET_break (0);
1143 return GNUNET_SYSERR; 1162 return GNUNET_SYSERR;
1144 } 1163 }
1145 msize = ntohs(msg->header.size); 1164 msize = ntohs (msg->header.size);
1146 total_count = ntohl(msg->element_count_total); 1165 total_count = ntohl (msg->element_count_total);
1147 contained_count = ntohl(msg->element_count_contained); 1166 contained_count = ntohl (msg->element_count_contained);
1148 if ((0 == total_count) || 1167 if ((0 == total_count) ||
1149 (0 == contained_count) || 1168 (0 == contained_count) ||
1150 (UINT16_MAX < contained_count) || 1169 (UINT16_MAX < contained_count) ||
1151 (msize != (sizeof(struct BobComputationMessage) + 1170 (msize != (sizeof(struct BobComputationMessage)
1152 contained_count * sizeof(struct GNUNET_SCALARPRODUCT_Element)))) 1171 + contained_count * sizeof(struct
1153 { 1172 GNUNET_SCALARPRODUCT_Element))))
1154 GNUNET_break_op(0); 1173 {
1155 return GNUNET_SYSERR; 1174 GNUNET_break_op (0);
1156 } 1175 return GNUNET_SYSERR;
1176 }
1157 return GNUNET_OK; 1177 return GNUNET_OK;
1158} 1178}
1159 1179
@@ -1167,20 +1187,20 @@ check_bob_client_message(void *cls,
1167 * @param msg the actual message 1187 * @param msg the actual message
1168 */ 1188 */
1169static void 1189static void
1170handle_bob_client_message(void *cls, 1190handle_bob_client_message (void *cls,
1171 const struct BobComputationMessage *msg) 1191 const struct BobComputationMessage *msg)
1172{ 1192{
1173 struct BobServiceSession *s = cls; 1193 struct BobServiceSession *s = cls;
1174 struct GNUNET_MQ_MessageHandler cadet_handlers[] = { 1194 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
1175 GNUNET_MQ_hd_fixed_size(alices_computation_request, 1195 GNUNET_MQ_hd_fixed_size (alices_computation_request,
1176 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION, 1196 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION,
1177 struct ServiceRequestMessage, 1197 struct ServiceRequestMessage,
1178 NULL), 1198 NULL),
1179 GNUNET_MQ_hd_var_size(alices_cryptodata_message, 1199 GNUNET_MQ_hd_var_size (alices_cryptodata_message,
1180 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA, 1200 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA,
1181 struct AliceCryptodataMessage, 1201 struct AliceCryptodataMessage,
1182 NULL), 1202 NULL),
1183 GNUNET_MQ_handler_end() 1203 GNUNET_MQ_handler_end ()
1184 }; 1204 };
1185 uint32_t contained_count; 1205 uint32_t contained_count;
1186 uint32_t total_count; 1206 uint32_t total_count;
@@ -1188,61 +1208,61 @@ handle_bob_client_message(void *cls,
1188 struct GNUNET_SET_Element set_elem; 1208 struct GNUNET_SET_Element set_elem;
1189 struct GNUNET_SCALARPRODUCT_Element *elem; 1209 struct GNUNET_SCALARPRODUCT_Element *elem;
1190 1210
1191 total_count = ntohl(msg->element_count_total); 1211 total_count = ntohl (msg->element_count_total);
1192 contained_count = ntohl(msg->element_count_contained); 1212 contained_count = ntohl (msg->element_count_contained);
1193 1213
1194 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE; 1214 s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
1195 s->total = total_count; 1215 s->total = total_count;
1196 s->client_received_element_count = contained_count; 1216 s->client_received_element_count = contained_count;
1197 s->session_id = msg->session_key; 1217 s->session_id = msg->session_key;
1198 elements = (const struct GNUNET_SCALARPRODUCT_Element *)&msg[1]; 1218 elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
1199 s->intersected_elements 1219 s->intersected_elements
1200 = GNUNET_CONTAINER_multihashmap_create(s->total, 1220 = GNUNET_CONTAINER_multihashmap_create (s->total,
1201 GNUNET_YES); 1221 GNUNET_YES);
1202 s->intersection_set 1222 s->intersection_set
1203 = GNUNET_SET_create(cfg, 1223 = GNUNET_SET_create (cfg,
1204 GNUNET_SET_OPERATION_INTERSECTION); 1224 GNUNET_SET_OPERATION_INTERSECTION);
1205 for (uint32_t i = 0; i < contained_count; i++) 1225 for (uint32_t i = 0; i < contained_count; i++)
1226 {
1227 if (0 == GNUNET_ntohll (elements[i].value))
1228 continue;
1229 elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
1230 GNUNET_memcpy (elem,
1231 &elements[i],
1232 sizeof(struct GNUNET_SCALARPRODUCT_Element));
1233 if (GNUNET_SYSERR ==
1234 GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
1235 &elem->key,
1236 elem,
1237 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1206 { 1238 {
1207 if (0 == GNUNET_ntohll(elements[i].value)) 1239 GNUNET_break (0);
1208 continue; 1240 GNUNET_free (elem);
1209 elem = GNUNET_new(struct GNUNET_SCALARPRODUCT_Element); 1241 continue;
1210 GNUNET_memcpy(elem,
1211 &elements[i],
1212 sizeof(struct GNUNET_SCALARPRODUCT_Element));
1213 if (GNUNET_SYSERR ==
1214 GNUNET_CONTAINER_multihashmap_put(s->intersected_elements,
1215 &elem->key,
1216 elem,
1217 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1218 {
1219 GNUNET_break(0);
1220 GNUNET_free(elem);
1221 continue;
1222 }
1223 set_elem.data = &elem->key;
1224 set_elem.size = sizeof(elem->key);
1225 set_elem.element_type = 0;
1226 GNUNET_SET_add_element(s->intersection_set,
1227 &set_elem,
1228 NULL, NULL);
1229 s->used_element_count++;
1230 } 1242 }
1231 GNUNET_SERVICE_client_continue(s->client); 1243 set_elem.data = &elem->key;
1244 set_elem.size = sizeof(elem->key);
1245 set_elem.element_type = 0;
1246 GNUNET_SET_add_element (s->intersection_set,
1247 &set_elem,
1248 NULL, NULL);
1249 s->used_element_count++;
1250 }
1251 GNUNET_SERVICE_client_continue (s->client);
1232 /* We're ready, open the port */ 1252 /* We're ready, open the port */
1233 s->port = GNUNET_CADET_open_port(my_cadet, 1253 s->port = GNUNET_CADET_open_port (my_cadet,
1234 &msg->session_key, 1254 &msg->session_key,
1235 &cb_channel_incoming, 1255 &cb_channel_incoming,
1236 s, 1256 s,
1237 NULL, 1257 NULL,
1238 &cb_channel_destruction, 1258 &cb_channel_destruction,
1239 cadet_handlers); 1259 cadet_handlers);
1240 if (NULL == s->port) 1260 if (NULL == s->port)
1241 { 1261 {
1242 GNUNET_break(0); 1262 GNUNET_break (0);
1243 GNUNET_SERVICE_client_drop(s->client); 1263 GNUNET_SERVICE_client_drop (s->client);
1244 return; 1264 return;
1245 } 1265 }
1246} 1266}
1247 1267
1248 1268
@@ -1252,16 +1272,16 @@ handle_bob_client_message(void *cls,
1252 * @param cls unused 1272 * @param cls unused
1253 */ 1273 */
1254static void 1274static void
1255shutdown_task(void *cls) 1275shutdown_task (void *cls)
1256{ 1276{
1257 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 "Shutting down, initiating cleanup.\n"); 1278 "Shutting down, initiating cleanup.\n");
1259 // FIXME: we have to cut our connections to CADET first! 1279 // FIXME: we have to cut our connections to CADET first!
1260 if (NULL != my_cadet) 1280 if (NULL != my_cadet)
1261 { 1281 {
1262 GNUNET_CADET_disconnect(my_cadet); 1282 GNUNET_CADET_disconnect (my_cadet);
1263 my_cadet = NULL; 1283 my_cadet = NULL;
1264 } 1284 }
1265} 1285}
1266 1286
1267 1287
@@ -1276,13 +1296,13 @@ shutdown_task(void *cls)
1276 * @return our `struct BobServiceSession` 1296 * @return our `struct BobServiceSession`
1277 */ 1297 */
1278static void * 1298static void *
1279client_connect_cb(void *cls, 1299client_connect_cb (void *cls,
1280 struct GNUNET_SERVICE_Client *client, 1300 struct GNUNET_SERVICE_Client *client,
1281 struct GNUNET_MQ_Handle *mq) 1301 struct GNUNET_MQ_Handle *mq)
1282{ 1302{
1283 struct BobServiceSession *s; 1303 struct BobServiceSession *s;
1284 1304
1285 s = GNUNET_new(struct BobServiceSession); 1305 s = GNUNET_new (struct BobServiceSession);
1286 s->client = client; 1306 s->client = client;
1287 s->client_mq = mq; 1307 s->client_mq = mq;
1288 return s; 1308 return s;
@@ -1300,16 +1320,16 @@ client_connect_cb(void *cls,
1300 * @param app_cls our `struct BobServiceSession` 1320 * @param app_cls our `struct BobServiceSession`
1301 */ 1321 */
1302static void 1322static void
1303client_disconnect_cb(void *cls, 1323client_disconnect_cb (void *cls,
1304 struct GNUNET_SERVICE_Client *client, 1324 struct GNUNET_SERVICE_Client *client,
1305 void *app_cls) 1325 void *app_cls)
1306{ 1326{
1307 struct BobServiceSession *s = app_cls; 1327 struct BobServiceSession *s = app_cls;
1308 1328
1309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 "Client disconnected from us.\n"); 1330 "Client disconnected from us.\n");
1311 s->client = NULL; 1331 s->client = NULL;
1312 destroy_service_session(s); 1332 destroy_service_session (s);
1313} 1333}
1314 1334
1315 1335
@@ -1321,31 +1341,31 @@ client_disconnect_cb(void *cls,
1321 * @param service the initialized service 1341 * @param service the initialized service
1322 */ 1342 */
1323static void 1343static void
1324run(void *cls, 1344run (void *cls,
1325 const struct GNUNET_CONFIGURATION_Handle *c, 1345 const struct GNUNET_CONFIGURATION_Handle *c,
1326 struct GNUNET_SERVICE_Handle *service) 1346 struct GNUNET_SERVICE_Handle *service)
1327{ 1347{
1328 cfg = c; 1348 cfg = c;
1329 /* 1349 /*
1330 offset has to be sufficiently small to allow computation of: 1350 offset has to be sufficiently small to allow computation of:
1331 m1+m2 mod n == (S + a) + (S + b) mod n, 1351 m1+m2 mod n == (S + a) + (S + b) mod n,
1332 if we have more complex operations, this factor needs to be lowered */ 1352 if we have more complex operations, this factor needs to be lowered */
1333 my_offset = gcry_mpi_new(GNUNET_CRYPTO_PAILLIER_BITS / 3); 1353 my_offset = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS / 3);
1334 gcry_mpi_set_bit(my_offset, 1354 gcry_mpi_set_bit (my_offset,
1335 GNUNET_CRYPTO_PAILLIER_BITS / 3); 1355 GNUNET_CRYPTO_PAILLIER_BITS / 3);
1336 1356
1337 GNUNET_CRYPTO_paillier_create(&my_pubkey, 1357 GNUNET_CRYPTO_paillier_create (&my_pubkey,
1338 &my_privkey); 1358 &my_privkey);
1339 my_cadet = GNUNET_CADET_connect(cfg); 1359 my_cadet = GNUNET_CADET_connect (cfg);
1340 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, 1360 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1341 NULL); 1361 NULL);
1342 if (NULL == my_cadet) 1362 if (NULL == my_cadet)
1343 { 1363 {
1344 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1364 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1345 _("Connect to CADET failed\n")); 1365 _ ("Connect to CADET failed\n"));
1346 GNUNET_SCHEDULER_shutdown(); 1366 GNUNET_SCHEDULER_shutdown ();
1347 return; 1367 return;
1348 } 1368 }
1349} 1369}
1350 1370
1351 1371
@@ -1359,15 +1379,15 @@ GNUNET_SERVICE_MAIN
1359 &client_connect_cb, 1379 &client_connect_cb,
1360 &client_disconnect_cb, 1380 &client_disconnect_cb,
1361 NULL, 1381 NULL,
1362 GNUNET_MQ_hd_var_size(bob_client_message, 1382 GNUNET_MQ_hd_var_size (bob_client_message,
1363 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB, 1383 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB,
1364 struct BobComputationMessage, 1384 struct BobComputationMessage,
1365 NULL), 1385 NULL),
1366 GNUNET_MQ_hd_var_size(bob_client_message_multipart, 1386 GNUNET_MQ_hd_var_size (bob_client_message_multipart,
1367 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB, 1387 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB,
1368 struct ComputationBobCryptodataMultipartMessage, 1388 struct ComputationBobCryptodataMultipartMessage,
1369 NULL), 1389 NULL),
1370 GNUNET_MQ_handler_end()); 1390 GNUNET_MQ_handler_end ());
1371 1391
1372 1392
1373/* end of gnunet-service-scalarproduct_bob.c */ 1393/* end of gnunet-service-scalarproduct_bob.c */
diff --git a/src/scalarproduct/scalarproduct.h b/src/scalarproduct/scalarproduct.h
index a3154e587..f2311cda0 100644
--- a/src/scalarproduct/scalarproduct.h
+++ b/src/scalarproduct/scalarproduct.h
@@ -32,14 +32,18 @@ GNUNET_NETWORK_STRUCT_BEGIN
32 * a failure of the command 'cmd' with the message given 32 * a failure of the command 'cmd' with the message given
33 * by gcry_strerror(rc). 33 * by gcry_strerror(rc).
34 */ 34 */
35#define LOG_GCRY(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while (0) 35#define LOG_GCRY(level, cmd, rc) do { LOG (level, _ ( \
36 "`%s' failed at %s:%d with error: %s\n"), \
37 cmd, __FILE__, __LINE__, \
38 gcry_strerror (rc)); } while (0)
36 39
37 40
38/** 41/**
39 * Message type passed from client to service 42 * Message type passed from client to service
40 * to initiate a request or responder role 43 * to initiate a request or responder role
41 */ 44 */
42struct AliceComputationMessage { 45struct AliceComputationMessage
46{
43 /** 47 /**
44 * GNUNET message header with type 48 * GNUNET message header with type
45 * #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE 49 * #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE
@@ -81,7 +85,8 @@ struct AliceComputationMessage {
81 * Message type passed from client to service 85 * Message type passed from client to service
82 * to initiate a request or responder role 86 * to initiate a request or responder role
83 */ 87 */
84struct BobComputationMessage { 88struct BobComputationMessage
89{
85 /** 90 /**
86 * GNUNET message header with type 91 * GNUNET message header with type
87 * #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB 92 * #GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB
@@ -117,7 +122,8 @@ struct BobComputationMessage {
117/** 122/**
118 * multipart messages following `struct ComputationMessage` 123 * multipart messages following `struct ComputationMessage`
119 */ 124 */
120struct ComputationBobCryptodataMultipartMessage { 125struct ComputationBobCryptodataMultipartMessage
126{
121 /** 127 /**
122 * GNUNET message header 128 * GNUNET message header
123 */ 129 */
@@ -138,7 +144,8 @@ struct ComputationBobCryptodataMultipartMessage {
138 * Message type passed from service client 144 * Message type passed from service client
139 * to finalize a session as requester or responder 145 * to finalize a session as requester or responder
140 */ 146 */
141struct ClientResponseMessage { 147struct ClientResponseMessage
148{
142 /** 149 /**
143 * GNUNET message header 150 * GNUNET message header
144 */ 151 */
diff --git a/src/scalarproduct/scalarproduct_api.c b/src/scalarproduct/scalarproduct_api.c
index 5fd810317..b2a90c222 100644
--- a/src/scalarproduct/scalarproduct_api.c
+++ b/src/scalarproduct/scalarproduct_api.c
@@ -31,7 +31,7 @@
31#include "gnunet_protocols.h" 31#include "gnunet_protocols.h"
32#include "scalarproduct.h" 32#include "scalarproduct.h"
33 33
34#define LOG(kind, ...) GNUNET_log_from(kind, "scalarproduct-api", __VA_ARGS__) 34#define LOG(kind, ...) GNUNET_log_from (kind, "scalarproduct-api", __VA_ARGS__)
35 35
36 36
37/** 37/**
@@ -42,15 +42,21 @@
42 * @param status processing status code 42 * @param status processing status code
43 */ 43 */
44typedef void 44typedef void
45(*GNUNET_SCALARPRODUCT_ResponseMessageHandler) (struct GNUNET_SCALARPRODUCT_ComputationHandle *h, 45(*GNUNET_SCALARPRODUCT_ResponseMessageHandler) (struct
46 const struct ClientResponseMessage *msg, 46 GNUNET_SCALARPRODUCT_ComputationHandle
47 enum GNUNET_SCALARPRODUCT_ResponseStatus status); 47 *h,
48 const struct
49 ClientResponseMessage *msg,
50 enum
51 GNUNET_SCALARPRODUCT_ResponseStatus
52 status);
48 53
49 54
50/** 55/**
51 * A handle returned for each computation 56 * A handle returned for each computation
52 */ 57 */
53struct GNUNET_SCALARPRODUCT_ComputationHandle { 58struct GNUNET_SCALARPRODUCT_ComputationHandle
59{
54 /** 60 /**
55 * Our configuration. 61 * Our configuration.
56 */ 62 */
@@ -98,15 +104,15 @@ struct GNUNET_SCALARPRODUCT_ComputationHandle {
98 * @return #GNUNET_OK if @a message is well-formed 104 * @return #GNUNET_OK if @a message is well-formed
99 */ 105 */
100static int 106static int
101check_response(void *cls, 107check_response (void *cls,
102 const struct ClientResponseMessage *message) 108 const struct ClientResponseMessage *message)
103{ 109{
104 if (ntohs(message->header.size) != 110 if (ntohs (message->header.size) !=
105 ntohl(message->product_length) + sizeof(struct ClientResponseMessage)) 111 ntohl (message->product_length) + sizeof(struct ClientResponseMessage))
106 { 112 {
107 GNUNET_break(0); 113 GNUNET_break (0);
108 return GNUNET_SYSERR; 114 return GNUNET_SYSERR;
109 } 115 }
110 return GNUNET_OK; 116 return GNUNET_OK;
111} 117}
112 118
@@ -121,14 +127,14 @@ check_response(void *cls,
121 * @param status the condition the request was terminated with (eg: disconnect) 127 * @param status the condition the request was terminated with (eg: disconnect)
122 */ 128 */
123static void 129static void
124process_status_message(struct GNUNET_SCALARPRODUCT_ComputationHandle *h, 130process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
125 const struct ClientResponseMessage *msg, 131 const struct ClientResponseMessage *msg,
126 enum GNUNET_SCALARPRODUCT_ResponseStatus status) 132 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
127{ 133{
128 if (NULL != h->cont_status) 134 if (NULL != h->cont_status)
129 h->cont_status(h->cont_cls, 135 h->cont_status (h->cont_cls,
130 status); 136 status);
131 GNUNET_SCALARPRODUCT_cancel(h); 137 GNUNET_SCALARPRODUCT_cancel (h);
132} 138}
133 139
134 140
@@ -141,16 +147,16 @@ process_status_message(struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
141 * @param msg Pointer to the data received in response 147 * @param msg Pointer to the data received in response
142 */ 148 */
143static void 149static void
144handle_response(void *cls, 150handle_response (void *cls,
145 const struct ClientResponseMessage *message) 151 const struct ClientResponseMessage *message)
146{ 152{
147 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls; 153 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
148 enum GNUNET_SCALARPRODUCT_ResponseStatus status; 154 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
149 155
150 status = (enum GNUNET_SCALARPRODUCT_ResponseStatus)ntohl(message->status); 156 status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status);
151 h->response_proc(h, 157 h->response_proc (h,
152 message, 158 message,
153 status); 159 status);
154} 160}
155 161
156 162
@@ -162,28 +168,28 @@ handle_response(void *cls,
162 * @return #GNUNET_OK if all keys are unique 168 * @return #GNUNET_OK if all keys are unique
163 */ 169 */
164static int 170static int
165check_unique(const struct GNUNET_SCALARPRODUCT_Element *elements, 171check_unique (const struct GNUNET_SCALARPRODUCT_Element *elements,
166 uint32_t element_count) 172 uint32_t element_count)
167{ 173{
168 struct GNUNET_CONTAINER_MultiHashMap *map; 174 struct GNUNET_CONTAINER_MultiHashMap *map;
169 uint32_t i; 175 uint32_t i;
170 int ok; 176 int ok;
171 177
172 ok = GNUNET_OK; 178 ok = GNUNET_OK;
173 map = GNUNET_CONTAINER_multihashmap_create(2 * element_count, 179 map = GNUNET_CONTAINER_multihashmap_create (2 * element_count,
174 GNUNET_YES); 180 GNUNET_YES);
175 for (i = 0; i < element_count; i++) 181 for (i = 0; i < element_count; i++)
176 if (GNUNET_OK != 182 if (GNUNET_OK !=
177 GNUNET_CONTAINER_multihashmap_put(map, 183 GNUNET_CONTAINER_multihashmap_put (map,
178 &elements[i].key, 184 &elements[i].key,
179 map, 185 map,
180 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 186 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
181 { 187 {
182 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183 _("Keys given to SCALARPRODUCT not unique!\n")); 189 _ ("Keys given to SCALARPRODUCT not unique!\n"));
184 ok = GNUNET_SYSERR; 190 ok = GNUNET_SYSERR;
185 } 191 }
186 GNUNET_CONTAINER_multihashmap_destroy(map); 192 GNUNET_CONTAINER_multihashmap_destroy (map);
187 return ok; 193 return ok;
188} 194}
189 195
@@ -196,16 +202,16 @@ check_unique(const struct GNUNET_SCALARPRODUCT_Element *elements,
196 * @param error error code 202 * @param error error code
197 */ 203 */
198static void 204static void
199mq_error_handler(void *cls, 205mq_error_handler (void *cls,
200 enum GNUNET_MQ_Error error) 206 enum GNUNET_MQ_Error error)
201{ 207{
202 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls; 208 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
203 209
204 LOG(GNUNET_ERROR_TYPE_INFO, 210 LOG (GNUNET_ERROR_TYPE_INFO,
205 "Disconnected from SCALARPRODUCT service.\n"); 211 "Disconnected from SCALARPRODUCT service.\n");
206 h->response_proc(h, 212 h->response_proc (h,
207 NULL, 213 NULL,
208 GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED); 214 GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED);
209} 215}
210 216
211 217
@@ -221,21 +227,25 @@ mq_error_handler(void *cls,
221 * @return a new handle for this computation 227 * @return a new handle for this computation
222 */ 228 */
223struct GNUNET_SCALARPRODUCT_ComputationHandle * 229struct GNUNET_SCALARPRODUCT_ComputationHandle *
224GNUNET_SCALARPRODUCT_accept_computation(const struct GNUNET_CONFIGURATION_Handle *cfg, 230GNUNET_SCALARPRODUCT_accept_computation (const struct
225 const struct GNUNET_HashCode *session_key, 231 GNUNET_CONFIGURATION_Handle *cfg,
226 const struct GNUNET_SCALARPRODUCT_Element *elements, 232 const struct
227 uint32_t element_count, 233 GNUNET_HashCode *session_key,
228 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont, 234 const struct
229 void *cont_cls) 235 GNUNET_SCALARPRODUCT_Element *elements,
236 uint32_t element_count,
237 GNUNET_SCALARPRODUCT_ContinuationWithStatus
238 cont,
239 void *cont_cls)
230{ 240{
231 struct GNUNET_SCALARPRODUCT_ComputationHandle *h 241 struct GNUNET_SCALARPRODUCT_ComputationHandle *h
232 = GNUNET_new(struct GNUNET_SCALARPRODUCT_ComputationHandle); 242 = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
233 struct GNUNET_MQ_MessageHandler handlers[] = { 243 struct GNUNET_MQ_MessageHandler handlers[] = {
234 GNUNET_MQ_hd_var_size(response, 244 GNUNET_MQ_hd_var_size (response,
235 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT, 245 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
236 struct ClientResponseMessage, 246 struct ClientResponseMessage,
237 h), 247 h),
238 GNUNET_MQ_handler_end() 248 GNUNET_MQ_handler_end ()
239 }; 249 };
240 struct GNUNET_MQ_Envelope *env; 250 struct GNUNET_MQ_Envelope *env;
241 struct BobComputationMessage *msg; 251 struct BobComputationMessage *msg;
@@ -246,61 +256,62 @@ GNUNET_SCALARPRODUCT_accept_computation(const struct GNUNET_CONFIGURATION_Handle
246 uint32_t element_count_transfered; 256 uint32_t element_count_transfered;
247 257
248 258
249 if (GNUNET_SYSERR == check_unique(elements, 259 if (GNUNET_SYSERR == check_unique (elements,
250 element_count)) 260 element_count))
251 return NULL; 261 return NULL;
252 h->cont_status = cont; 262 h->cont_status = cont;
253 h->cont_cls = cont_cls; 263 h->cont_cls = cont_cls;
254 h->response_proc = &process_status_message; 264 h->response_proc = &process_status_message;
255 h->cfg = cfg; 265 h->cfg = cfg;
256 h->key = *session_key; 266 h->key = *session_key;
257 h->mq = GNUNET_CLIENT_connect(cfg, 267 h->mq = GNUNET_CLIENT_connect (cfg,
258 "scalarproduct-bob", 268 "scalarproduct-bob",
259 handlers, 269 handlers,
260 &mq_error_handler, 270 &mq_error_handler,
261 h); 271 h);
262 if (NULL == h->mq) 272 if (NULL == h->mq)
263 { 273 {
264 /* scalarproduct configuration error */ 274 /* scalarproduct configuration error */
265 GNUNET_break(0); 275 GNUNET_break (0);
266 GNUNET_free(h); 276 GNUNET_free (h);
267 return NULL; 277 return NULL;
268 } 278 }
269 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(struct BobComputationMessage)) 279 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(struct
280 BobComputationMessage))
270 / sizeof(struct GNUNET_SCALARPRODUCT_Element); 281 / sizeof(struct GNUNET_SCALARPRODUCT_Element);
271 todo = GNUNET_MIN(possible, 282 todo = GNUNET_MIN (possible,
272 element_count); 283 element_count);
273 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); 284 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element);
274 env = GNUNET_MQ_msg_extra(msg, 285 env = GNUNET_MQ_msg_extra (msg,
275 size, 286 size,
276 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB); 287 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
277 msg->element_count_total = htonl(element_count); 288 msg->element_count_total = htonl (element_count);
278 msg->element_count_contained = htonl(todo); 289 msg->element_count_contained = htonl (todo);
279 msg->session_key = *session_key; 290 msg->session_key = *session_key;
280 GNUNET_memcpy(&msg[1], 291 GNUNET_memcpy (&msg[1],
281 elements, 292 elements,
282 size); 293 size);
283 element_count_transfered = todo; 294 element_count_transfered = todo;
284 GNUNET_MQ_send(h->mq, 295 GNUNET_MQ_send (h->mq,
285 env); 296 env);
286 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(*mmsg)) 297 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(*mmsg))
287 / sizeof(struct GNUNET_SCALARPRODUCT_Element); 298 / sizeof(struct GNUNET_SCALARPRODUCT_Element);
288 while (element_count_transfered < element_count) 299 while (element_count_transfered < element_count)
289 { 300 {
290 todo = GNUNET_MIN(possible, 301 todo = GNUNET_MIN (possible,
291 element_count - element_count_transfered); 302 element_count - element_count_transfered);
292 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); 303 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element);
293 env = GNUNET_MQ_msg_extra(mmsg, 304 env = GNUNET_MQ_msg_extra (mmsg,
294 size, 305 size,
295 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB); 306 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB);
296 mmsg->element_count_contained = htonl(todo); 307 mmsg->element_count_contained = htonl (todo);
297 GNUNET_memcpy(&mmsg[1], 308 GNUNET_memcpy (&mmsg[1],
298 &elements[element_count_transfered], 309 &elements[element_count_transfered],
299 size); 310 size);
300 element_count_transfered += todo; 311 element_count_transfered += todo;
301 GNUNET_MQ_send(h->mq, 312 GNUNET_MQ_send (h->mq,
302 env); 313 env);
303 } 314 }
304 return h; 315 return h;
305} 316}
306 317
@@ -315,9 +326,9 @@ GNUNET_SCALARPRODUCT_accept_computation(const struct GNUNET_CONFIGURATION_Handle
315 * @param status the condition the request was terminated with (eg: disconnect) 326 * @param status the condition the request was terminated with (eg: disconnect)
316 */ 327 */
317static void 328static void
318process_result_message(struct GNUNET_SCALARPRODUCT_ComputationHandle *h, 329process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
319 const struct ClientResponseMessage *msg, 330 const struct ClientResponseMessage *msg,
320 enum GNUNET_SCALARPRODUCT_ResponseStatus status) 331 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
321{ 332{
322 uint32_t product_len; 333 uint32_t product_len;
323 gcry_mpi_t result = NULL; 334 gcry_mpi_t result = NULL;
@@ -326,42 +337,42 @@ process_result_message(struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
326 size_t rsize; 337 size_t rsize;
327 338
328 if (GNUNET_SCALARPRODUCT_STATUS_SUCCESS == status) 339 if (GNUNET_SCALARPRODUCT_STATUS_SUCCESS == status)
340 {
341 result = gcry_mpi_new (0);
342
343 product_len = ntohl (msg->product_length);
344 if (0 < product_len)
329 { 345 {
330 result = gcry_mpi_new(0); 346 rsize = 0;
331 347 if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD,
332 product_len = ntohl(msg->product_length); 348 &msg[1],
333 if (0 < product_len) 349 product_len,
334 { 350 &rsize)))
335 rsize = 0; 351 {
336 if (0 != (rc = gcry_mpi_scan(&num, GCRYMPI_FMT_STD, 352 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
337 &msg[1], 353 "gcry_mpi_scan",
338 product_len, 354 rc);
339 &rsize))) 355 gcry_mpi_release (result);
340 { 356 result = NULL;
341 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, 357 status = GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE;
342 "gcry_mpi_scan", 358 }
343 rc); 359 else
344 gcry_mpi_release(result); 360 {
345 result = NULL; 361 if (0 < (int32_t) ntohl (msg->range))
346 status = GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE; 362 gcry_mpi_add (result, result, num);
347 } 363 else
348 else 364 gcry_mpi_sub (result, result, num);
349 { 365 gcry_mpi_release (num);
350 if (0 < (int32_t)ntohl(msg->range)) 366 }
351 gcry_mpi_add(result, result, num);
352 else
353 gcry_mpi_sub(result, result, num);
354 gcry_mpi_release(num);
355 }
356 }
357 } 367 }
368 }
358 if (NULL != h->cont_datum) 369 if (NULL != h->cont_datum)
359 h->cont_datum(h->cont_cls, 370 h->cont_datum (h->cont_cls,
360 status, 371 status,
361 result); 372 result);
362 if (NULL != result) 373 if (NULL != result)
363 gcry_mpi_release(result); 374 gcry_mpi_release (result);
364 GNUNET_SCALARPRODUCT_cancel(h); 375 GNUNET_SCALARPRODUCT_cancel (h);
365} 376}
366 377
367 378
@@ -378,22 +389,25 @@ process_result_message(struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
378 * @return a new handle for this computation 389 * @return a new handle for this computation
379 */ 390 */
380struct GNUNET_SCALARPRODUCT_ComputationHandle * 391struct GNUNET_SCALARPRODUCT_ComputationHandle *
381GNUNET_SCALARPRODUCT_start_computation(const struct GNUNET_CONFIGURATION_Handle *cfg, 392GNUNET_SCALARPRODUCT_start_computation (const struct
382 const struct GNUNET_HashCode *session_key, 393 GNUNET_CONFIGURATION_Handle *cfg,
383 const struct GNUNET_PeerIdentity *peer, 394 const struct
384 const struct GNUNET_SCALARPRODUCT_Element *elements, 395 GNUNET_HashCode *session_key,
385 uint32_t element_count, 396 const struct GNUNET_PeerIdentity *peer,
386 GNUNET_SCALARPRODUCT_DatumProcessor cont, 397 const struct
387 void *cont_cls) 398 GNUNET_SCALARPRODUCT_Element *elements,
399 uint32_t element_count,
400 GNUNET_SCALARPRODUCT_DatumProcessor cont,
401 void *cont_cls)
388{ 402{
389 struct GNUNET_SCALARPRODUCT_ComputationHandle *h 403 struct GNUNET_SCALARPRODUCT_ComputationHandle *h
390 = GNUNET_new(struct GNUNET_SCALARPRODUCT_ComputationHandle); 404 = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
391 struct GNUNET_MQ_MessageHandler handlers[] = { 405 struct GNUNET_MQ_MessageHandler handlers[] = {
392 GNUNET_MQ_hd_var_size(response, 406 GNUNET_MQ_hd_var_size (response,
393 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT, 407 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
394 struct ClientResponseMessage, 408 struct ClientResponseMessage,
395 h), 409 h),
396 GNUNET_MQ_handler_end() 410 GNUNET_MQ_handler_end ()
397 }; 411 };
398 struct GNUNET_MQ_Envelope *env; 412 struct GNUNET_MQ_Envelope *env;
399 struct AliceComputationMessage *msg; 413 struct AliceComputationMessage *msg;
@@ -403,64 +417,65 @@ GNUNET_SCALARPRODUCT_start_computation(const struct GNUNET_CONFIGURATION_Handle
403 uint16_t todo; 417 uint16_t todo;
404 uint32_t element_count_transfered; 418 uint32_t element_count_transfered;
405 419
406 if (GNUNET_SYSERR == check_unique(elements, 420 if (GNUNET_SYSERR == check_unique (elements,
407 element_count)) 421 element_count))
408 return NULL; 422 return NULL;
409 h->mq = GNUNET_CLIENT_connect(cfg, 423 h->mq = GNUNET_CLIENT_connect (cfg,
410 "scalarproduct-alice", 424 "scalarproduct-alice",
411 handlers, 425 handlers,
412 &mq_error_handler, 426 &mq_error_handler,
413 h); 427 h);
414 if (NULL == h->mq) 428 if (NULL == h->mq)
415 { 429 {
416 /* missconfigured scalarproduct service */ 430 /* missconfigured scalarproduct service */
417 GNUNET_break(0); 431 GNUNET_break (0);
418 GNUNET_free(h); 432 GNUNET_free (h);
419 return NULL; 433 return NULL;
420 } 434 }
421 h->cont_datum = cont; 435 h->cont_datum = cont;
422 h->cont_cls = cont_cls; 436 h->cont_cls = cont_cls;
423 h->response_proc = &process_result_message; 437 h->response_proc = &process_result_message;
424 h->cfg = cfg; 438 h->cfg = cfg;
425 h->key = *session_key; 439 h->key = *session_key;
426 440
427 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(struct AliceComputationMessage)) 441 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(struct
442 AliceComputationMessage))
428 / sizeof(struct GNUNET_SCALARPRODUCT_Element); 443 / sizeof(struct GNUNET_SCALARPRODUCT_Element);
429 todo = GNUNET_MIN(possible, 444 todo = GNUNET_MIN (possible,
430 element_count); 445 element_count);
431 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); 446 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element);
432 env = GNUNET_MQ_msg_extra(msg, 447 env = GNUNET_MQ_msg_extra (msg,
433 size, 448 size,
434 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE); 449 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
435 msg->element_count_total = htonl(element_count); 450 msg->element_count_total = htonl (element_count);
436 msg->element_count_contained = htonl(todo); 451 msg->element_count_contained = htonl (todo);
437 msg->reserved = htonl(0); 452 msg->reserved = htonl (0);
438 msg->peer = *peer; 453 msg->peer = *peer;
439 msg->session_key = *session_key; 454 msg->session_key = *session_key;
440 GNUNET_memcpy(&msg[1], 455 GNUNET_memcpy (&msg[1],
441 elements, 456 elements,
442 size); 457 size);
443 GNUNET_MQ_send(h->mq, 458 GNUNET_MQ_send (h->mq,
444 env); 459 env);
445 element_count_transfered = todo; 460 element_count_transfered = todo;
446 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(*mmsg)) 461 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(*mmsg))
447 / sizeof(struct GNUNET_SCALARPRODUCT_Element); 462 / sizeof(struct GNUNET_SCALARPRODUCT_Element);
448 while (element_count_transfered < element_count) 463 while (element_count_transfered < element_count)
449 { 464 {
450 todo = GNUNET_MIN(possible, 465 todo = GNUNET_MIN (possible,
451 element_count - element_count_transfered); 466 element_count - element_count_transfered);
452 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element); 467 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element);
453 env = GNUNET_MQ_msg_extra(mmsg, 468 env = GNUNET_MQ_msg_extra (mmsg,
454 size, 469 size,
455 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE); 470 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE);
456 mmsg->element_count_contained = htonl(todo); 471 mmsg->element_count_contained = htonl (todo);
457 GNUNET_memcpy(&mmsg[1], 472 GNUNET_memcpy (&mmsg[1],
458 &elements[element_count_transfered], 473 &elements[element_count_transfered],
459 size); 474 size);
460 element_count_transfered += todo; 475 element_count_transfered += todo;
461 GNUNET_MQ_send(h->mq, 476 GNUNET_MQ_send (h->mq,
462 env); 477 env);
463 } 478 }
464 return h; 479 return h;
465} 480}
466 481
@@ -472,14 +487,14 @@ GNUNET_SCALARPRODUCT_start_computation(const struct GNUNET_CONFIGURATION_Handle
472 * @param h computation handle to terminate 487 * @param h computation handle to terminate
473 */ 488 */
474void 489void
475GNUNET_SCALARPRODUCT_cancel(struct GNUNET_SCALARPRODUCT_ComputationHandle *h) 490GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
476{ 491{
477 if (NULL != h->mq) 492 if (NULL != h->mq)
478 { 493 {
479 GNUNET_MQ_destroy(h->mq); 494 GNUNET_MQ_destroy (h->mq);
480 h->mq = NULL; 495 h->mq = NULL;
481 } 496 }
482 GNUNET_free(h); 497 GNUNET_free (h);
483} 498}
484 499
485 500
diff --git a/src/scalarproduct/test_ecc_scalarproduct.c b/src/scalarproduct/test_ecc_scalarproduct.c
index 543ac4c7e..1117f0811 100644
--- a/src/scalarproduct/test_ecc_scalarproduct.c
+++ b/src/scalarproduct/test_ecc_scalarproduct.c
@@ -41,8 +41,8 @@ static struct GNUNET_CRYPTO_EccDlogContext *edc;
41 * @return avec * bvec 41 * @return avec * bvec
42 */ 42 */
43static int 43static int
44test_sp(const unsigned int *avec, 44test_sp (const unsigned int *avec,
45 const unsigned int *bvec) 45 const unsigned int *bvec)
46{ 46{
47 unsigned int len; 47 unsigned int len;
48 unsigned int i; 48 unsigned int i;
@@ -67,109 +67,109 @@ test_sp(const unsigned int *avec,
67 return 0; 67 return 0;
68 68
69 /* Alice */ 69 /* Alice */
70 GNUNET_CRYPTO_ecc_rnd_mpi(edc, 70 GNUNET_CRYPTO_ecc_rnd_mpi (edc,
71 &a, &a_inv); 71 &a, &a_inv);
72 g = GNUNET_new_array(len, 72 g = GNUNET_new_array (len,
73 gcry_mpi_point_t); 73 gcry_mpi_point_t);
74 h = GNUNET_new_array(len, 74 h = GNUNET_new_array (len,
75 gcry_mpi_point_t); 75 gcry_mpi_point_t);
76 ria = gcry_mpi_new(0); 76 ria = gcry_mpi_new (0);
77 tmp = gcry_mpi_new(0); 77 tmp = gcry_mpi_new (0);
78 for (i = 0; i < len; i++) 78 for (i = 0; i < len; i++)
79 { 79 {
80 ri = GNUNET_CRYPTO_ecc_random_mod_n(edc); 80 ri = GNUNET_CRYPTO_ecc_random_mod_n (edc);
81 g[i] = GNUNET_CRYPTO_ecc_dexp_mpi(edc, 81 g[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
82 ri); 82 ri);
83 /* ria = ri * a */ 83 /* ria = ri * a */
84 gcry_mpi_mul(ria, 84 gcry_mpi_mul (ria,
85 ri, 85 ri,
86 a); 86 a);
87 /* tmp = ria + avec[i] */ 87 /* tmp = ria + avec[i] */
88 gcry_mpi_add_ui(tmp, 88 gcry_mpi_add_ui (tmp,
89 ria, 89 ria,
90 avec[i]); 90 avec[i]);
91 h[i] = GNUNET_CRYPTO_ecc_dexp_mpi(edc, 91 h[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
92 tmp); 92 tmp);
93 } 93 }
94 gcry_mpi_release(ria); 94 gcry_mpi_release (ria);
95 gcry_mpi_release(tmp); 95 gcry_mpi_release (tmp);
96 96
97 /* Bob */ 97 /* Bob */
98 val = gcry_mpi_new(0); 98 val = gcry_mpi_new (0);
99 gcry_mpi_set_ui(val, bvec[0]); 99 gcry_mpi_set_ui (val, bvec[0]);
100 pg = GNUNET_CRYPTO_ecc_pmul_mpi(edc, 100 pg = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
101 g[0], 101 g[0],
102 val); 102 val);
103 ph = GNUNET_CRYPTO_ecc_pmul_mpi(edc, 103 ph = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
104 h[0], 104 h[0],
105 val); 105 val);
106 for (i = 1; i < len; i++) 106 for (i = 1; i < len; i++)
107 { 107 {
108 gcry_mpi_point_t m; 108 gcry_mpi_point_t m;
109 gcry_mpi_point_t tmp; 109 gcry_mpi_point_t tmp;
110 110
111 gcry_mpi_set_ui(val, bvec[i]); 111 gcry_mpi_set_ui (val, bvec[i]);
112 m = GNUNET_CRYPTO_ecc_pmul_mpi(edc, 112 m = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
113 g[i], 113 g[i],
114 val); 114 val);
115 tmp = GNUNET_CRYPTO_ecc_add(edc, 115 tmp = GNUNET_CRYPTO_ecc_add (edc,
116 m, 116 m,
117 pg); 117 pg);
118 gcry_mpi_point_release(m); 118 gcry_mpi_point_release (m);
119 gcry_mpi_point_release(pg); 119 gcry_mpi_point_release (pg);
120 gcry_mpi_point_release(g[i]); 120 gcry_mpi_point_release (g[i]);
121 pg = tmp; 121 pg = tmp;
122 122
123 m = GNUNET_CRYPTO_ecc_pmul_mpi(edc, 123 m = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
124 h[i], 124 h[i],
125 val); 125 val);
126 tmp = GNUNET_CRYPTO_ecc_add(edc, 126 tmp = GNUNET_CRYPTO_ecc_add (edc,
127 m, 127 m,
128 ph); 128 ph);
129 gcry_mpi_point_release(m); 129 gcry_mpi_point_release (m);
130 gcry_mpi_point_release(ph); 130 gcry_mpi_point_release (ph);
131 gcry_mpi_point_release(h[i]); 131 gcry_mpi_point_release (h[i]);
132 ph = tmp; 132 ph = tmp;
133 } 133 }
134 gcry_mpi_release(val); 134 gcry_mpi_release (val);
135 GNUNET_free(g); 135 GNUNET_free (g);
136 GNUNET_free(h); 136 GNUNET_free (h);
137 137
138 /* Alice */ 138 /* Alice */
139 pgi = GNUNET_CRYPTO_ecc_pmul_mpi(edc, 139 pgi = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
140 pg, 140 pg,
141 a_inv); 141 a_inv);
142 gsp = GNUNET_CRYPTO_ecc_add(edc, 142 gsp = GNUNET_CRYPTO_ecc_add (edc,
143 pgi, 143 pgi,
144 ph); 144 ph);
145 gcry_mpi_point_release(pgi); 145 gcry_mpi_point_release (pgi);
146 gcry_mpi_point_release(ph); 146 gcry_mpi_point_release (ph);
147 sp = GNUNET_CRYPTO_ecc_dlog(edc, 147 sp = GNUNET_CRYPTO_ecc_dlog (edc,
148 gsp); 148 gsp);
149 gcry_mpi_point_release(gsp); 149 gcry_mpi_point_release (gsp);
150 return sp; 150 return sp;
151} 151}
152 152
153 153
154int 154int
155main(int argc, char *argv[]) 155main (int argc, char *argv[])
156{ 156{
157 static unsigned int v11[] = { 1, 1, 0 }; 157 static unsigned int v11[] = { 1, 1, 0 };
158 static unsigned int v22[] = { 2, 2, 0 }; 158 static unsigned int v22[] = { 2, 2, 0 };
159 static unsigned int v35[] = { 3, 5, 0 }; 159 static unsigned int v35[] = { 3, 5, 0 };
160 static unsigned int v24[] = { 2, 4, 0 }; 160 static unsigned int v24[] = { 2, 4, 0 };
161 161
162 GNUNET_log_setup("test-ecc-scalarproduct", 162 GNUNET_log_setup ("test-ecc-scalarproduct",
163 "WARNING", 163 "WARNING",
164 NULL); 164 NULL);
165 edc = GNUNET_CRYPTO_ecc_dlog_prepare(128, 128); 165 edc = GNUNET_CRYPTO_ecc_dlog_prepare (128, 128);
166 GNUNET_assert(2 == test_sp(v11, v11)); 166 GNUNET_assert (2 == test_sp (v11, v11));
167 GNUNET_assert(4 == test_sp(v22, v11)); 167 GNUNET_assert (4 == test_sp (v22, v11));
168 GNUNET_assert(8 == test_sp(v35, v11)); 168 GNUNET_assert (8 == test_sp (v35, v11));
169 GNUNET_assert(26 == test_sp(v35, v24)); 169 GNUNET_assert (26 == test_sp (v35, v24));
170 GNUNET_assert(26 == test_sp(v24, v35)); 170 GNUNET_assert (26 == test_sp (v24, v35));
171 GNUNET_assert(16 == test_sp(v22, v35)); 171 GNUNET_assert (16 == test_sp (v22, v35));
172 GNUNET_CRYPTO_ecc_dlog_release(edc); 172 GNUNET_CRYPTO_ecc_dlog_release (edc);
173 return 0; 173 return 0;
174} 174}
175 175