diff --git a/brandt.c b/brandt.c index 5bf31ae..6c334b8 100644 --- a/brandt.c +++ b/brandt.c @@ -26,72 +26,6 @@ #include "util.h" -typedef int -(*msg_recv)(struct BRANDT_Auction *ad, - const unsigned char *buf, - size_t buflen, - uint16_t sender); - - -enum { - auction_firstPrice, - auction_mPlusFirstPrice, - auction_last -}; - - -enum { - outcome_private, - outcome_public, - outcome_last -}; - - -/** - * stores the function pointers to receive functions for each state. - * - * The first index denotes if a first price auction or a M+1st price auction is - * used. If it is 0, it is a first price auction, if it is 1, it is a M+1st - * price auction. - * - * The second index denotes if the outcome should be public or private. A value - * of 0 means a private outcome, while a value of 1 means public outcome. - */ -static msg_recv handler_in[auction_last][outcome_last][msg_last] = -{ - [auction_firstPrice] = - { - [outcome_private] = - { - [msg_init] = &smc_recv_keyshare, - [msg_bid] = &smc_recv_encrypted_bid, - [msg_outcome] = &fp_priv_recv_outcome, - [msg_decrypt] = &fp_priv_recv_decryption, - }, - [outcome_public] = - { - [msg_init] = &smc_recv_keyshare, - [msg_bid] = &smc_recv_encrypted_bid, - [msg_outcome] = &fp_pub_recv_outcome, - [msg_decrypt] = &fp_pub_recv_decryption, - } - }, - [auction_mPlusFirstPrice] = - { - [outcome_private] = - { - [msg_init] = &smc_recv_keyshare, - [msg_bid] = &smc_recv_encrypted_bid, - }, - [outcome_public] = - { - [msg_init] = &smc_recv_keyshare, - [msg_bid] = &smc_recv_encrypted_bid, - } - } -}; - - void BRANDT_init (struct GNUNET_CRYPTO_EccDlogContext *dlogctx) { diff --git a/crypto.h b/crypto.h index aecf850..af4b374 100644 --- a/crypto.h +++ b/crypto.h @@ -113,44 +113,141 @@ int smc_zkp_0og_check (const gcry_mpi_point_t y, const gcry_mpi_point_t beta, const struct proof_0og *proof); + /* --- Protocol implementation --- */ unsigned char *smc_gen_keyshare (struct BRANDT_Auction *ad, size_t *buflen); int smc_recv_keyshare (struct BRANDT_Auction *ad, - const unsigned char *buf, - size_t buflen, - uint16_t sender_index); + const unsigned char *buf, + size_t buflen, + uint16_t sender_index); unsigned char *smc_encrypt_bid (struct BRANDT_Auction *ad, size_t *buflen); int smc_recv_encrypted_bid (struct BRANDT_Auction *ad, - const unsigned char *buf, - size_t buflen, - uint16_t sender_index); + const unsigned char *buf, + size_t buflen, + uint16_t sender_index); -unsigned char *fp_priv_compute_outcome (struct BRANDT_Auction *ad, size_t *buflen); +unsigned char *fp_priv_compute_outcome (struct BRANDT_Auction *ad, + size_t *buflen); int fp_priv_recv_outcome (struct BRANDT_Auction *ad, - const unsigned char *buf, - size_t buflen, - uint16_t sender); + const unsigned char *buf, + size_t buflen, + uint16_t sender); -unsigned char *fp_priv_decrypt_outcome (struct BRANDT_Auction *ad, size_t *buflen); +unsigned char *fp_priv_decrypt_outcome (struct BRANDT_Auction *ad, + size_t *buflen); int fp_priv_recv_decryption (struct BRANDT_Auction *ad, - const unsigned char *buf, - size_t buflen, - uint16_t sender); + const unsigned char *buf, + size_t buflen, + uint16_t sender); -unsigned char *fp_pub_compute_outcome (struct BRANDT_Auction *ad, size_t *buflen); +unsigned char *fp_pub_compute_outcome (struct BRANDT_Auction *ad, + size_t *buflen); int fp_pub_recv_outcome (struct BRANDT_Auction *ad, - const unsigned char *buf, - size_t buflen, - uint16_t sender); + const unsigned char *buf, + size_t buflen, + uint16_t sender); -unsigned char *fp_pub_decrypt_outcome (struct BRANDT_Auction *ad, size_t *buflen); +unsigned char *fp_pub_decrypt_outcome (struct BRANDT_Auction *ad, + size_t *buflen); int fp_pub_recv_decryption (struct BRANDT_Auction *ad, - const unsigned char *buf, - size_t buflen, - uint16_t sender); + const unsigned char *buf, + size_t buflen, + uint16_t sender); int32_t fp_priv_determine_outcome (struct BRANDT_Auction *ad); + +/* --- Round dictionaries --- */ + +typedef int +(*msg_in)(struct BRANDT_Auction *ad, + const unsigned char *buf, + size_t buflen, + uint16_t sender); + + +typedef unsigned char * +(*msg_out)(struct BRANDT_Auction *ad, + size_t *buflen); + + +/** + * stores the function pointers to receive functions for each state. + * + * The first index denotes if a first price auction or a M+1st price auction is + * used. If it is 0, it is a first price auction, if it is 1, it is a M+1st + * price auction. + * + * The second index denotes if the outcome should be public or private. A value + * of 0 means a private outcome, while a value of 1 means public outcome. + */ +static const msg_in handler_in[auction_last][outcome_last][msg_last] = { + [auction_firstPrice] = { + [outcome_private] = { + [msg_init] = &smc_recv_keyshare, + [msg_bid] = &smc_recv_encrypted_bid, + [msg_outcome] = &fp_priv_recv_outcome, + [msg_decrypt] = &fp_priv_recv_decryption, + }, + [outcome_public] = { + [msg_init] = &smc_recv_keyshare, + [msg_bid] = &smc_recv_encrypted_bid, + [msg_outcome] = &fp_pub_recv_outcome, + [msg_decrypt] = &fp_pub_recv_decryption, + }, + }, + [auction_mPlusFirstPrice] = { + [outcome_private] = { + [msg_init] = &smc_recv_keyshare, + [msg_bid] = &smc_recv_encrypted_bid, + }, + [outcome_public] = { + [msg_init] = &smc_recv_keyshare, + [msg_bid] = &smc_recv_encrypted_bid, + }, + }, +}; + + +/** + * stores the function pointers to message buffer creating functions for each + * state. + * + * The first index denotes if a first price auction or a M+1st price auction is + * used. If it is 0, it is a first price auction, if it is 1, it is a M+1st + * price auction. + * + * The second index denotes if the outcome should be public or private. A value + * of 0 means a private outcome, while a value of 1 means public outcome. + */ +static const msg_out handler_out[auction_last][outcome_last][msg_last] = { + [auction_firstPrice] = { + [outcome_private] = { + [msg_init] = &smc_gen_keyshare, + [msg_bid] = &smc_encrypt_bid, + [msg_outcome] = &fp_priv_compute_outcome, + [msg_decrypt] = &fp_priv_decrypt_outcome, + }, + [outcome_public] = { + [msg_init] = &smc_gen_keyshare, + [msg_bid] = &smc_encrypt_bid, + [msg_outcome] = &fp_pub_compute_outcome, + [msg_decrypt] = &fp_pub_decrypt_outcome, + }, + }, + [auction_mPlusFirstPrice] = { + [outcome_private] = { + [msg_init] = &smc_gen_keyshare, + [msg_bid] = &smc_encrypt_bid, + }, + [outcome_public] = { + [msg_init] = &smc_gen_keyshare, + [msg_bid] = &smc_encrypt_bid, + }, + }, +}; + + #endif /* ifndef _BRANDT_CRYPTO_H */ diff --git a/internals.h b/internals.h index 4624347..656e798 100644 --- a/internals.h +++ b/internals.h @@ -37,13 +37,27 @@ enum rounds { }; +enum auction_type { + auction_firstPrice, + auction_mPlusFirstPrice, + auction_last +}; + + +enum outcome_type { + outcome_private, + outcome_public, + outcome_last +}; + + /** * This struct describes an auction and has to be followed by #description_len * bytes of arbitrary data where the description of the item to be sold is * stored. - * + * * \todo: align to a multiple of 64bit */ -struct BRANDT_AuctionDescrP { +struct BRANDT_DescrP { /** The length of the description in bytes directly following this struct */ uint32_t description_len; @@ -63,7 +77,7 @@ struct BRANDT_AuctionDescrP { struct BRANDT_Auction { - struct BRANDT_AuctionDescrP *desc; /** pointer to the auction information */ + struct BRANDT_DescrP *desc; /** pointer to the auction information */ BRANDT_CbBroadcast bcast; /** broadcast callback */ BRANDT_CbUnicast ucast; /** unicast callback */ diff --git a/test_crypto.c b/test_crypto.c index 09a0838..297ac69 100644 --- a/test_crypto.c +++ b/test_crypto.c @@ -30,8 +30,8 @@ #include "test.h" -static uint16_t bidders; -static uint16_t prizes; +static uint16_t bidders; +static uint16_t prizes; static struct BRANDT_Auction *ad; int @@ -199,141 +199,83 @@ test_setup_auction_data () } -int -test_prologue () -{ - uint16_t i, s; - unsigned char *bufs[bidders]; - size_t lens[bidders]; - - for (i = 0; i < bidders; i++) - { - bufs[i] = smc_gen_keyshare (&ad[i], &lens[i]); - check (bufs[i], "failed to gen keyshare"); - } - - for (i = 0; i < bidders; i++) - { - for (s = 0; s < bidders; s++) - { - if (s == i) - continue; - check (smc_recv_keyshare (&ad[i], bufs[s], lens[s], s), - "failed checking keyshare"); - } - } - - for (i = 0; i < bidders; i++) - free (bufs[i]); - return 1; -} +#define ROUND(type, oc, index) do { \ + for (uint16_t i = 0; i < bidders; i++) \ + { \ + bufs[i] = handler_out[type][oc][index] (&ad[i], &lens[i]); \ + check (bufs[i], "failed to gen keyshare"); \ + } \ + \ + for (uint16_t i = 0; i < bidders; i++) \ + { \ + for (uint16_t s = 0; s < bidders; s++) \ + { \ + if (s == i) \ + continue; \ + check (handler_in[type][oc][index] (&ad[i], bufs[s], lens[s], s), \ + "failed checking keyshare"); \ + } \ + } \ + \ + for (uint16_t i = 0; i < bidders; i++) \ + free (bufs[i]); \ +} while (0) int -test_round1 () +test_private_first_price () { - uint16_t i, s; unsigned char *bufs[bidders]; size_t lens[bidders]; + int32_t winner = -1; - for (i = 0; i < bidders; i++) - { - bufs[i] = smc_encrypt_bid (&ad[i], &lens[i]); - check (bufs[i], "failed to encrypt bid"); - } - - for (i = 0; i < bidders; i++) - { - for (s = 0; s < bidders; s++) - { - if (s == i) - continue; - check (smc_recv_encrypted_bid (&ad[i], bufs[s], lens[s], s), - "failed checking encrypted bid"); - } - } - - for (i = 0; i < bidders; i++) - free (bufs[i]); - return 1; -} - - -int -test_round2 () -{ - uint16_t i, s; - unsigned char *bufs[bidders]; - size_t lens[bidders]; - - for (i = 0; i < bidders; i++) - { - bufs[i] = fp_priv_compute_outcome (&ad[i], &lens[i]); - check (bufs[i], "failed to compute outcome"); - } - - for (i = 0; i < bidders; i++) - { - for (s = 0; s < bidders; s++) - { - if (s == i) - continue; - check (fp_priv_recv_outcome (&ad[i], bufs[s], lens[s], s), - "failed checking outcome"); - } - } - - for (i = 0; i < bidders; i++) - free (bufs[i]); - return 1; -} - - -static int -test_round3 () -{ - uint16_t i, s; - unsigned char *bufs[bidders]; - size_t lens[bidders]; - - for (i = 0; i < bidders; i++) - { - bufs[i] = fp_priv_decrypt_outcome (&ad[i], &lens[i]); - check (bufs[i], "failed to decrypt outcome"); - } - - for (i = 0; i < bidders; i++) - { - for (s = 0; s < bidders; s++) - { - if (s == i) - continue; - check (fp_priv_recv_decryption (&ad[i], bufs[s], lens[s], s), - "failed checking decrypted outcome"); - } - } - - for (i = 0; i < bidders; i++) - free (bufs[i]); - return 1; -} - - -static int -test_outcome () -{ - int32_t ret = -1; + ROUND (auction_firstPrice, outcome_private, msg_init); + ROUND (auction_firstPrice, outcome_private, msg_bid); + ROUND (auction_firstPrice, outcome_private, msg_outcome); + ROUND (auction_firstPrice, outcome_private, msg_decrypt); + /* outcome */ for (uint16_t i = 0; i < ad->n; i++) { if (-1 != fp_priv_determine_outcome (&ad[i])) { - check (-1 == ret, "multiple winners detected"); - ret = i; + check (-1 == winner, "multiple winners detected"); + winner = i; } } - check (-1 != ret, "no winner detected"); - fputs ("winner detected", stderr); + check (-1 != winner, "no winner detected"); + fputs ("good: one winner detected", stderr); + return 1; +} + + +int +test_public_first_price () +{ + unsigned char *bufs[bidders]; + size_t lens[bidders]; + int32_t wret = -1; + int32_t pret = -1; + uint16_t winner = -1; + uint16_t price = -1; + + ROUND (auction_firstPrice, outcome_public, msg_init); + ROUND (auction_firstPrice, outcome_public, msg_bid); + ROUND (auction_firstPrice, outcome_public, msg_outcome); + ROUND (auction_firstPrice, outcome_public, msg_decrypt); + + /* outcome */ + for (uint16_t i = 0; i < ad->n; i++) + { + price = fp_pub_determine_outcome (&ad[i], &winner); + if (-1 == pret) + pret = price; + check (price == pret, "different prices detected"); + if (-1 == wret) + wret = winner; + check (winner == wret, "different winners detected"); + } + fputs ("good: same winner detected", stderr); return 1; } @@ -364,7 +306,7 @@ cleanup_auction_data () int main (int argc, char *argv[]) { - int repeat = 1; + int repeat = 1; struct GNUNET_CRYPTO_EccDlogContext *edc; bidders = 2; @@ -386,11 +328,10 @@ main (int argc, char *argv[]) } run (test_setup_auction_data); - run (test_prologue); - run (test_round1); - run (test_round2); - run (test_round3); - run (test_outcome); + run (test_private_first_price); + cleanup_auction_data (); + run (test_setup_auction_data); + run (test_public_first_price); cleanup_auction_data (); GNUNET_CRYPTO_ecc_dlog_release (edc);