add 3dim array helpers

This commit is contained in:
Markus Teich 2016-06-20 00:36:18 +02:00
parent 8e44164fac
commit 65775f8276
3 changed files with 73 additions and 6 deletions

View File

@ -148,7 +148,7 @@ BRANDT_join (BRANDT_BroadcastCallback broadcast,
* the price of the highest loosing bid. \todo: what if bidders < m? * the price of the highest loosing bid. \todo: what if bidders < m?
* @param[in] outcome_public If 1, the auction winner and price will be public * @param[in] outcome_public If 1, the auction winner and price will be public
* to all participants, if 0, this information will only be revealed to the * to all participants, if 0, this information will only be revealed to the
* winner and the seller. => \todo: Turn into AuctionMode bit flag! * winner and the seller.
* @return A pointer, which should only be remembered and passed to * @return A pointer, which should only be remembered and passed to
* libbrandt functions when the client needs to refer to this auction. This is a * libbrandt functions when the client needs to refer to this auction. This is a
* black-box pointer, do NOT access/change it or the data it points to! * black-box pointer, do NOT access/change it or the data it points to!
@ -160,7 +160,7 @@ BRANDT_new (BRANDT_BroadcastCallback broadcast,
const void ** auction_data, const void ** auction_data,
size_t * auction_data_len, size_t * auction_data_len,
uint16_t num_prices, uint16_t num_prices,
enum BRANDT_AuctionMode m, uint16_t m,
int outcome_public); int outcome_public);

View File

@ -37,9 +37,8 @@ static gcry_mpi_t ec_n;
/** /**
* brandt_crypto_init * brandt_crypto_init initializes the crypto system and must be called before
* * any other function from this file.
*
*/ */
void void
brandt_crypto_init () brandt_crypto_init ()
@ -297,8 +296,9 @@ smc_init2 (uint16_t size1, uint16_t size2)
gcry_mpi_point_t **ret; gcry_mpi_point_t **ret;
gcry_mpi_point_t *data; gcry_mpi_point_t *data;
ret = calloc (size1, sizeof (*ret) + (size2 * sizeof (**ret))); ret = calloc (size1, sizeof (*ret) + size2 * sizeof (**ret));
brandt_assert (NULL != ret); brandt_assert (NULL != ret);
data = (gcry_mpi_point_t *)&ret[size1]; data = (gcry_mpi_point_t *)&ret[size1];
for (i = 0; i < size1; i++) for (i = 0; i < size1; i++)
{ {
@ -322,6 +322,51 @@ smc_free2 (gcry_mpi_point_t **dst, uint16_t size1, uint16_t size2)
} }
static gcry_mpi_point_t ***
smc_init3 (uint16_t size1, uint16_t size2, uint16_t size3)
{
uint16_t i, j, k;
gcry_mpi_point_t ***ret;
gcry_mpi_point_t **layer1;
gcry_mpi_point_t *layer2;
ret = calloc (size1, sizeof (*ret) +
size2 * sizeof (**ret) +
size2 * size3 * sizeof (***ret));
brandt_assert (NULL != ret);
layer1 = (gcry_mpi_point_t **)&ret[size1];
layer2 = (gcry_mpi_point_t *)&layer1[size1*size2];
for (i = 0; i < size1; i++)
{
ret[i] = &layer1[i * size2];
for (j = 0; j < size2; j++)
{
layer1[i * size2 + j] = &layer2[(i * size2 + j) * size3];
for (k = 0; k < size3; k++)
ret[i][j][k] = gcry_mpi_point_new (0);
}
}
return ret;
}
static void
smc_free3 (gcry_mpi_point_t ***dst,
uint16_t size1,
uint16_t size2,
uint16_t size3)
{
uint16_t i, j, k;
for (i = 0; i < size1; i++)
for (j = 0; j < size2; j++)
for (k = 0; k < size3; k++)
gcry_mpi_point_release (dst[i][j][k]);
free (dst);
}
/** /**
* smc_sums_partial calculates sums up until the current index and stores them * smc_sums_partial calculates sums up until the current index and stores them
* in @a out. \f$\forall i \leq len: out_i=\sum_{h=1}^iin_h\f$ * in @a out. \f$\forall i \leq len: out_i=\sum_{h=1}^iin_h\f$

View File

@ -46,6 +46,27 @@ test_smc_2d_array ()
} }
int
test_smc_3d_array ()
{
gcry_mpi_point_t ***array;
uint16_t size1 = 3;
uint16_t size2 = 7;
uint16_t size3 = 11;
uint16_t i, j, k;
array = smc_init3 (size1, size2, size3);
check (array, "memory allocation failed");
for (i = 0; i < size1; i++)
for (j = 0; j < size2; j++)
for (k = 0; k < size3; k++)
check (array[i][j][k], "point has not been initialized");
smc_free3 (array, size1, size2, size3);
}
int int
test_smc_zkp_dl () test_smc_zkp_dl ()
{ {
@ -185,6 +206,7 @@ main (int argc, char *argv[])
/* tests that need to run only once */ /* tests that need to run only once */
run (test_smc_2d_array); run (test_smc_2d_array);
run (test_smc_3d_array);
for (tests_run = 0; tests_run < repeat; tests_run++) for (tests_run = 0; tests_run < repeat; tests_run++)
{ {