2 This file is part of GNUnet.
3 Copyright (C) 2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
17 * @file util/test_ecc_scalarproduct.c
18 * @brief testcase for math behind ECC SP calculation
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
28 static struct GNUNET_CRYPTO_EccDlogContext *edc;
32 * Perform SP calculation.
34 * @param avec 0-terminated vector of Alice's values
35 * @param bvec 0-terminated vector of Bob's values
39 test_sp (const unsigned int *avec,
40 const unsigned int *bvec)
58 /* determine length */
59 for (len=0;0 != avec[len];len++) ;
64 GNUNET_CRYPTO_ecc_rnd_mpi (edc,
66 g = GNUNET_new_array (len,
68 h = GNUNET_new_array (len,
70 ria = gcry_mpi_new (0);
71 tmp = gcry_mpi_new (0);
74 ri = GNUNET_CRYPTO_ecc_random_mod_n (edc);
75 g[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
81 /* tmp = ria + avec[i] */
85 h[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
88 gcry_mpi_release (ria);
89 gcry_mpi_release (tmp);
92 val = gcry_mpi_new (0);
93 gcry_mpi_set_ui (val, bvec[0]);
94 pg = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
97 ph = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
103 gcry_mpi_point_t tmp;
105 gcry_mpi_set_ui (val, bvec[i]);
106 m = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
109 tmp = GNUNET_CRYPTO_ecc_add (edc,
112 gcry_mpi_point_release (m);
113 gcry_mpi_point_release (pg);
114 gcry_mpi_point_release (g[i]);
117 m = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
120 tmp = GNUNET_CRYPTO_ecc_add (edc,
123 gcry_mpi_point_release (m);
124 gcry_mpi_point_release (ph);
125 gcry_mpi_point_release (h[i]);
128 gcry_mpi_release (val);
133 pgi = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
136 gsp = GNUNET_CRYPTO_ecc_add (edc,
139 gcry_mpi_point_release (pgi);
140 gcry_mpi_point_release (ph);
141 sp = GNUNET_CRYPTO_ecc_dlog (edc,
143 gcry_mpi_point_release (gsp);
149 main (int argc, char *argv[])
151 static unsigned int v11[] = { 1, 1, 0 };
152 static unsigned int v22[] = { 2, 2, 0 };
153 static unsigned int v35[] = { 3, 5, 0 };
154 static unsigned int v24[] = { 2, 4, 0 };
156 GNUNET_log_setup ("test-ecc-scalarproduct",
159 edc = GNUNET_CRYPTO_ecc_dlog_prepare (128, 128);
160 GNUNET_assert ( 2 == test_sp (v11, v11));
161 GNUNET_assert ( 4 == test_sp (v22, v11));
162 GNUNET_assert ( 8 == test_sp (v35, v11));
163 GNUNET_assert (26 == test_sp (v35, v24));
164 GNUNET_assert (26 == test_sp (v24, v35));
165 GNUNET_assert (16 == test_sp (v22, v35));
166 GNUNET_CRYPTO_ecc_dlog_release (edc);
170 /* end of test_ecc_scalarproduct.c */