aboutsummaryrefslogtreecommitdiff
path: root/brandt.c
diff options
context:
space:
mode:
authorMarkus Teich <markus.teich@stusta.mhn.de>2016-09-08 20:09:22 +0200
committerMarkus Teich <markus.teich@stusta.mhn.de>2016-09-08 20:09:22 +0200
commitde5ea20b5557870119332842ce3a31d608e0e66c (patch)
tree31f73d416b9b240941f18d5ec53121e809ab62d7 /brandt.c
parent14546eccb205aa844657d2c350c83f6be1289b67 (diff)
downloadlibbrandt-de5ea20b5557870119332842ce3a31d608e0e66c.tar.gz
libbrandt-de5ea20b5557870119332842ce3a31d608e0e66c.zip
private firstprice auction working in test_brandt
Diffstat (limited to 'brandt.c')
-rw-r--r--brandt.c123
1 files changed, 62 insertions, 61 deletions
diff --git a/brandt.c b/brandt.c
index 1c42d41..b6d3466 100644
--- a/brandt.c
+++ b/brandt.c
@@ -78,8 +78,8 @@ BRANDT_bidder_start (struct BRANDT_Auction *auction,
78 } 78 }
79 79
80 weprintf("broadcasting msg_init %p from bidder %d", buf, i); 80 weprintf("broadcasting msg_init %p from bidder %d", buf, i);
81 if (0 == auction->bcast (auction->closure, buf, buflen)) 81 auction->bcast (auction->closure, buf, buflen);
82 gcry_mpi_set_bit (auction->round_progress, auction->i); 82 gcry_mpi_set_bit (auction->round_progress, auction->i);
83 free (buf); 83 free (buf);
84} 84}
85 85
@@ -294,87 +294,88 @@ BRANDT_destroy (struct BRANDT_Auction *auction)
294 294
295 295
296static void 296static void
297report_outcome (struct BRANDT_Auction *ad,
298 enum auction_type atype,
299 enum outcome_type outcome)
300{
301 struct BRANDT_Result *res;
302 uint16_t reslen = 0;
303
304 if (!handler_res[atype][outcome] ||
305 !(res = handler_res[atype][outcome] (ad, &reslen)))
306 ad->result (ad->closure, NULL, 0);
307 else
308 ad->result (ad->closure, res, reslen);
309}
310
311
312static void
297advance_round (struct BRANDT_Auction *ad, 313advance_round (struct BRANDT_Auction *ad,
298 enum auction_type atype, 314 enum auction_type atype,
299 enum outcome_type outcome) 315 enum outcome_type outcome)
300{ 316{
301 struct BRANDT_Result *res;
302 uint16_t reslen = 0;
303 unsigned char *buf; 317 unsigned char *buf;
304 size_t buflen; 318 size_t buflen;
305 319
306 /* if we did not got the current round message from all participants, don't 320 if (!ad->seller_mode && msg_decrypt == ad->cur_round)
307 * advance to the next round yet. Early return, since we have to wait for
308 * the other participants messages. In the msg_decrypt round as a bidder we
309 * only need the one message from the seller. */
310 for (uint16_t i = 0; i < ad->n; i++)
311 if (!gcry_mpi_test_bit (ad->round_progress, i) &&
312 (ad->seller_mode || msg_decrypt != ad->cur_round))
313 return;
314
315 /* Do not advance to the next round if we are the seller and got all
316 * msg_decrypt messages from the bidders. We need to broadcast our combined
317 * msg_decrypt message back to the sellers first */
318 if (!ad->seller_mode || msg_decrypt != ad->cur_round)
319 { 321 {
320 /* clear old round progress and advance to next one */ 322 /* we are a bidder and successfully parsed the msg_decrypt from the
321 gcry_mpi_clear_highbit (ad->round_progress, 0); 323 * seller => we can determine the auction result */
322 ad->cur_round++; 324 report_outcome (ad, atype, outcome);
323 325 return;
324 /* prepare next round. */
325 if (handler_prep[atype][outcome][ad->cur_round])
326 handler_prep[atype][outcome][ad->cur_round] (ad);
327
328 /** \todo: setup round timeout trigger */
329 if (ad->seller_mode)
330 {
331 }
332 } 326 }
333 327
334 /* send next message if we are not done yet */ 328 /* only continue if the round is complete */
335 if ((!ad->seller_mode && msg_last > ad->cur_round) || 329 for (uint16_t i = 0; i < ad->n; i++)
336 (ad->seller_mode && msg_decrypt == ad->cur_round && 330 if (!gcry_mpi_test_bit (ad->round_progress, i))
337 gcry_mpi_test_bit (ad->round_progress, 0))) 331 return;
332
333 if (ad->seller_mode && msg_decrypt == ad->cur_round)
338 { 334 {
335 /* all bidders msg_decrypt received, broadcast combined msg_decrypt */
339 if (!handler_out[atype][outcome][ad->cur_round] || 336 if (!handler_out[atype][outcome][ad->cur_round] ||
340 !(buf = handler_out[atype][outcome][ad->cur_round](ad, &buflen))) 337 !(buf = handler_out[atype][outcome][ad->cur_round](ad, &buflen)))
341 { 338 {
342 weprintf ("failed to create message %d buffer", ad->cur_round); 339 weprintf ("failed to create msg %d buffer as seller",
340 ad->cur_round);
343 return; 341 return;
344 } 342 }
343 ad->bcast (ad->closure, buf, buflen);
345 344
346 /* msg_decrypt unicast to seller or broadcast from seller. 345 /* now we are done as seller and can determine the outcome */
347 * All other messages are broadcasted */ 346 report_outcome (ad, atype, outcome);
348 if (msg_decrypt == ad->cur_round && !ad->seller_mode) 347 return;
349 {
350 if (0 == ad->ucast (ad->closure, buf, buflen))
351 gcry_mpi_set_bit (ad->round_progress, ad->i);
352 }
353 else
354 {
355 if (!ad->seller_mode && 0 == ad->bcast (ad->closure, buf, buflen))
356 gcry_mpi_set_bit (ad->round_progress, ad->i);
357 }
358 } 348 }
359 349
360 if (msg_last == ad->cur_round || 350 /* round complete, advance to next one */
361 (msg_decrypt == ad->cur_round && ad->seller_mode && 351 gcry_mpi_clear_highbit (ad->round_progress, 0);
362 gcry_mpi_test_bit (ad->round_progress, 0))) 352 ad->cur_round++;
353
354 /* prepare next round. */
355 if (handler_prep[atype][outcome][ad->cur_round])
356 handler_prep[atype][outcome][ad->cur_round] (ad);
357
358 if (ad->seller_mode)
363 { 359 {
364 /* done with all rounds, determine outcome here */ 360 /** \todo: setup round timeout trigger */
365 if (!handler_res[atype][outcome] || 361 /* seller does not send regular messages */
366 !(res = handler_res[atype][outcome] (ad, &reslen))) 362 return;
367 { 363 }
368 weprintf ("failed to determine result");
369 /** \todo: call result with null pointer here? */
370 return;
371 }
372 364
373 ad->result (ad->closure, res, reslen); 365 /* create next message buffer */
374 /* seller still needs to broadcast the decryption to all bidders */ 366 if (!handler_out[atype][outcome][ad->cur_round] ||
375 if (!ad->seller_mode) 367 !(buf = handler_out[atype][outcome][ad->cur_round](ad, &buflen)))
376 return; 368 {
369 weprintf ("failed to create msg %d buffer as bidder", ad->cur_round);
370 return;
377 } 371 }
372
373 /* msg_decrypt unicast to seller. All other messages are broadcasted */
374 if (msg_decrypt == ad->cur_round)
375 ad->ucast (ad->closure, buf, buflen);
376 else
377 ad->bcast (ad->closure, buf, buflen);
378 gcry_mpi_set_bit (ad->round_progress, ad->i);
378} 379}
379 380
380 381