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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/test_ecc_scalarproduct.c
23 * @brief testcase for math behind ECC SP calculation
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
33 static struct GNUNET_CRYPTO_EccDlogContext *edc;
37 * Perform SP calculation.
39 * @param avec 0-terminated vector of Alice's values
40 * @param bvec 0-terminated vector of Bob's values
44 test_sp (const unsigned int *avec,
45 const unsigned int *bvec)
63 /* determine length */
64 for (len=0;0 != avec[len];len++) ;
69 GNUNET_CRYPTO_ecc_rnd_mpi (edc,
71 g = GNUNET_new_array (len,
73 h = GNUNET_new_array (len,
75 ria = gcry_mpi_new (0);
76 tmp = gcry_mpi_new (0);
79 ri = GNUNET_CRYPTO_ecc_random_mod_n (edc);
80 g[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
86 /* tmp = ria + avec[i] */
90 h[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
93 gcry_mpi_release (ria);
94 gcry_mpi_release (tmp);
97 val = gcry_mpi_new (0);
98 gcry_mpi_set_ui (val, bvec[0]);
99 pg = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
102 ph = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
108 gcry_mpi_point_t tmp;
110 gcry_mpi_set_ui (val, bvec[i]);
111 m = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
114 tmp = GNUNET_CRYPTO_ecc_add (edc,
117 gcry_mpi_point_release (m);
118 gcry_mpi_point_release (pg);
119 gcry_mpi_point_release (g[i]);
122 m = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
125 tmp = GNUNET_CRYPTO_ecc_add (edc,
128 gcry_mpi_point_release (m);
129 gcry_mpi_point_release (ph);
130 gcry_mpi_point_release (h[i]);
133 gcry_mpi_release (val);
138 pgi = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
141 gsp = GNUNET_CRYPTO_ecc_add (edc,
144 gcry_mpi_point_release (pgi);
145 gcry_mpi_point_release (ph);
146 sp = GNUNET_CRYPTO_ecc_dlog (edc,
148 gcry_mpi_point_release (gsp);
154 main (int argc, char *argv[])
156 static unsigned int v11[] = { 1, 1, 0 };
157 static unsigned int v22[] = { 2, 2, 0 };
158 static unsigned int v35[] = { 3, 5, 0 };
159 static unsigned int v24[] = { 2, 4, 0 };
161 GNUNET_log_setup ("test-ecc-scalarproduct",
164 edc = GNUNET_CRYPTO_ecc_dlog_prepare (128, 128);
165 GNUNET_assert ( 2 == test_sp (v11, v11));
166 GNUNET_assert ( 4 == test_sp (v22, v11));
167 GNUNET_assert ( 8 == test_sp (v35, v11));
168 GNUNET_assert (26 == test_sp (v35, v24));
169 GNUNET_assert (26 == test_sp (v24, v35));
170 GNUNET_assert (16 == test_sp (v22, v35));
171 GNUNET_CRYPTO_ecc_dlog_release (edc);
175 /* end of test_ecc_scalarproduct.c */