update tests

This commit is contained in:
Markus Teich 2016-08-03 01:28:51 +02:00
parent 017a90a88c
commit fd52f708dd
4 changed files with 206 additions and 220 deletions

View File

@ -26,72 +26,6 @@
#include "util.h" #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 void
BRANDT_init (struct GNUNET_CRYPTO_EccDlogContext *dlogctx) BRANDT_init (struct GNUNET_CRYPTO_EccDlogContext *dlogctx)
{ {

141
crypto.h
View File

@ -113,44 +113,141 @@ int smc_zkp_0og_check (const gcry_mpi_point_t y,
const gcry_mpi_point_t beta, const gcry_mpi_point_t beta,
const struct proof_0og *proof); const struct proof_0og *proof);
/* --- Protocol implementation --- */ /* --- Protocol implementation --- */
unsigned char *smc_gen_keyshare (struct BRANDT_Auction *ad, size_t *buflen); unsigned char *smc_gen_keyshare (struct BRANDT_Auction *ad, size_t *buflen);
int smc_recv_keyshare (struct BRANDT_Auction *ad, int smc_recv_keyshare (struct BRANDT_Auction *ad,
const unsigned char *buf, const unsigned char *buf,
size_t buflen, size_t buflen,
uint16_t sender_index); uint16_t sender_index);
unsigned char *smc_encrypt_bid (struct BRANDT_Auction *ad, size_t *buflen); unsigned char *smc_encrypt_bid (struct BRANDT_Auction *ad, size_t *buflen);
int smc_recv_encrypted_bid (struct BRANDT_Auction *ad, int smc_recv_encrypted_bid (struct BRANDT_Auction *ad,
const unsigned char *buf, const unsigned char *buf,
size_t buflen, size_t buflen,
uint16_t sender_index); 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, int fp_priv_recv_outcome (struct BRANDT_Auction *ad,
const unsigned char *buf, const unsigned char *buf,
size_t buflen, size_t buflen,
uint16_t sender); 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, int fp_priv_recv_decryption (struct BRANDT_Auction *ad,
const unsigned char *buf, const unsigned char *buf,
size_t buflen, size_t buflen,
uint16_t sender); 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, int fp_pub_recv_outcome (struct BRANDT_Auction *ad,
const unsigned char *buf, const unsigned char *buf,
size_t buflen, size_t buflen,
uint16_t sender); 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, int fp_pub_recv_decryption (struct BRANDT_Auction *ad,
const unsigned char *buf, const unsigned char *buf,
size_t buflen, size_t buflen,
uint16_t sender); uint16_t sender);
int32_t fp_priv_determine_outcome (struct BRANDT_Auction *ad); 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 */ #endif /* ifndef _BRANDT_CRYPTO_H */

View File

@ -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 * 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 * bytes of arbitrary data where the description of the item to be sold is
* stored. * stored.
* *
* \todo: align to a multiple of 64bit */ * \todo: align to a multiple of 64bit */
struct BRANDT_AuctionDescrP { struct BRANDT_DescrP {
/** The length of the description in bytes directly following this struct */ /** The length of the description in bytes directly following this struct */
uint32_t description_len; uint32_t description_len;
@ -63,7 +77,7 @@ struct BRANDT_AuctionDescrP {
struct BRANDT_Auction { 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_CbBroadcast bcast; /** broadcast callback */
BRANDT_CbUnicast ucast; /** unicast callback */ BRANDT_CbUnicast ucast; /** unicast callback */

View File

@ -30,8 +30,8 @@
#include "test.h" #include "test.h"
static uint16_t bidders; static uint16_t bidders;
static uint16_t prizes; static uint16_t prizes;
static struct BRANDT_Auction *ad; static struct BRANDT_Auction *ad;
int int
@ -199,141 +199,83 @@ test_setup_auction_data ()
} }
int #define ROUND(type, oc, index) do { \
test_prologue () for (uint16_t i = 0; i < bidders; i++) \
{ { \
uint16_t i, s; bufs[i] = handler_out[type][oc][index] (&ad[i], &lens[i]); \
unsigned char *bufs[bidders]; check (bufs[i], "failed to gen keyshare"); \
size_t lens[bidders]; } \
\
for (i = 0; i < bidders; i++) for (uint16_t i = 0; i < bidders; i++) \
{ { \
bufs[i] = smc_gen_keyshare (&ad[i], &lens[i]); for (uint16_t s = 0; s < bidders; s++) \
check (bufs[i], "failed to gen keyshare"); { \
} if (s == i) \
continue; \
for (i = 0; i < bidders; i++) check (handler_in[type][oc][index] (&ad[i], bufs[s], lens[s], s), \
{ "failed checking keyshare"); \
for (s = 0; s < bidders; s++) } \
{ } \
if (s == i) \
continue; for (uint16_t i = 0; i < bidders; i++) \
check (smc_recv_keyshare (&ad[i], bufs[s], lens[s], s), free (bufs[i]); \
"failed checking keyshare"); } while (0)
}
}
for (i = 0; i < bidders; i++)
free (bufs[i]);
return 1;
}
int int
test_round1 () test_private_first_price ()
{ {
uint16_t i, s;
unsigned char *bufs[bidders]; unsigned char *bufs[bidders];
size_t lens[bidders]; size_t lens[bidders];
int32_t winner = -1;
for (i = 0; i < bidders; i++) ROUND (auction_firstPrice, outcome_private, msg_init);
{ ROUND (auction_firstPrice, outcome_private, msg_bid);
bufs[i] = smc_encrypt_bid (&ad[i], &lens[i]); ROUND (auction_firstPrice, outcome_private, msg_outcome);
check (bufs[i], "failed to encrypt bid"); ROUND (auction_firstPrice, outcome_private, msg_decrypt);
}
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;
/* outcome */
for (uint16_t i = 0; i < ad->n; i++) for (uint16_t i = 0; i < ad->n; i++)
{ {
if (-1 != fp_priv_determine_outcome (&ad[i])) if (-1 != fp_priv_determine_outcome (&ad[i]))
{ {
check (-1 == ret, "multiple winners detected"); check (-1 == winner, "multiple winners detected");
ret = i; winner = i;
} }
} }
check (-1 != ret, "no winner detected"); check (-1 != winner, "no winner detected");
fputs ("winner detected", stderr); 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; return 1;
} }
@ -364,7 +306,7 @@ cleanup_auction_data ()
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
int repeat = 1; int repeat = 1;
struct GNUNET_CRYPTO_EccDlogContext *edc; struct GNUNET_CRYPTO_EccDlogContext *edc;
bidders = 2; bidders = 2;
@ -386,11 +328,10 @@ main (int argc, char *argv[])
} }
run (test_setup_auction_data); run (test_setup_auction_data);
run (test_prologue); run (test_private_first_price);
run (test_round1); cleanup_auction_data ();
run (test_round2); run (test_setup_auction_data);
run (test_round3); run (test_public_first_price);
run (test_outcome);
cleanup_auction_data (); cleanup_auction_data ();
GNUNET_CRYPTO_ecc_dlog_release (edc); GNUNET_CRYPTO_ecc_dlog_release (edc);