aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_crypto_ecc_dlog.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_crypto_ecc_dlog.c')
-rw-r--r--src/util/test_crypto_ecc_dlog.c218
1 files changed, 0 insertions, 218 deletions
diff --git a/src/util/test_crypto_ecc_dlog.c b/src/util/test_crypto_ecc_dlog.c
deleted file mode 100644
index 51f290d51..000000000
--- a/src/util/test_crypto_ecc_dlog.c
+++ /dev/null
@@ -1,218 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2015 GNUnet e.V.
4
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.
9
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.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19
20 */
21/**
22 * @file util/test_crypto_ecc_dlog.c
23 * @brief testcase for ECC DLOG calculation
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include <gcrypt.h>
29
30
31/**
32 * Name of the curve we are using. Note that we have hard-coded
33 * structs that use 256 bits, so using a bigger curve will require
34 * changes that break stuff badly. The name of the curve given here
35 * must be agreed by all peers and be supported by libgcrypt.
36 */
37#define CURVE "Ed25519"
38
39/**
40 * Maximum value we test dlog for.
41 */
42#define MAX_FACT 100
43
44/**
45 * Maximum memory to use, sqrt(MAX_FACT) is a good choice.
46 */
47#define MAX_MEM 10
48
49/**
50 * How many values do we test?
51 */
52#define TEST_ITER 100
53
54/**
55 * Range of values to use for MATH tests.
56 */
57#define MATH_MAX 5
58
59
60/**
61 * Do some DLOG operations for testing.
62 *
63 * @param edc context for ECC operations
64 */
65static void
66test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc)
67{
68 for (unsigned int i = 0; i < TEST_ITER; i++)
69 {
70 struct GNUNET_CRYPTO_EccScalar fact;
71 struct GNUNET_CRYPTO_EccScalar n;
72 struct GNUNET_CRYPTO_EccPoint q;
73 int x;
74
75 fprintf (stderr, ".");
76 x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
77 MAX_FACT);
78 memset (&n,
79 0,
80 sizeof (n));
81 for (unsigned int j = 0; j < x; j++)
82 sodium_increment (n.v,
83 sizeof (n.v));
84 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
85 2))
86 {
87 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
88 "Trying negative %d\n",
89 -x);
90 crypto_core_ed25519_scalar_negate (fact.v,
91 n.v);
92 x = -x;
93 }
94 else
95 {
96 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
97 "Trying positive %d\n",
98 x);
99 fact = n;
100 }
101 if (0 == x)
102 {
103 /* libsodium does not like to multiply with zero; make sure
104 'q' is a valid point (g) first, then use q = q - q to get
105 the product with zero */
106 sodium_increment (fact.v,
107 sizeof (fact.v));
108 GNUNET_assert (0 ==
109 crypto_scalarmult_ed25519_base_noclamp (q.v,
110 fact.v));
111 GNUNET_assert (
112 0 ==
113 crypto_core_ed25519_sub (q.v,
114 q.v,
115 q.v));
116 }
117 else
118 GNUNET_assert (0 ==
119 crypto_scalarmult_ed25519_base_noclamp (q.v,
120 fact.v));
121 {
122 int iret;
123
124 if (x !=
125 (iret = GNUNET_CRYPTO_ecc_dlog (edc,
126 &q)))
127 {
128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
129 "DLOG failed for value %d (got: %d)\n",
130 x,
131 iret);
132 GNUNET_assert (0);
133 }
134 }
135 }
136 fprintf (stderr,
137 "\n");
138}
139
140
141/**
142 * Do some arithmetic operations for testing.
143 *
144 * @param edc context for ECC operations
145 */
146static void
147test_math (struct GNUNET_CRYPTO_EccDlogContext *edc)
148{
149 int i;
150 int j;
151 struct GNUNET_CRYPTO_EccPoint ip;
152 struct GNUNET_CRYPTO_EccPoint jp;
153 struct GNUNET_CRYPTO_EccPoint r;
154 struct GNUNET_CRYPTO_EccPoint ir;
155 struct GNUNET_CRYPTO_EccPoint irj;
156 struct GNUNET_CRYPTO_EccPoint r_inv;
157 struct GNUNET_CRYPTO_EccPoint sum;
158
159 for (i = -MATH_MAX; i < MATH_MAX; i++)
160 {
161 GNUNET_CRYPTO_ecc_dexp (i, &ip);
162 for (j = -MATH_MAX; j < MATH_MAX; j++)
163 {
164 fprintf (stderr, ".");
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "%d + %d\n",
167 i,
168 j);
169 GNUNET_CRYPTO_ecc_dexp (j, &jp);
170 GNUNET_CRYPTO_ecc_rnd (&r,
171 &r_inv);
172 GNUNET_CRYPTO_ecc_add (&ip, &r, &ir);
173 GNUNET_CRYPTO_ecc_add (&ir, &jp, &irj);
174 GNUNET_CRYPTO_ecc_add (&irj, &r_inv, &sum);
175 int res = GNUNET_CRYPTO_ecc_dlog (edc, &sum);
176 if (i + j != res)
177 {
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 "Got %d, expected %d\n",
180 res,
181 i + j);
182 // GNUNET_assert (0);
183 }
184 }
185 }
186 fprintf (stderr, "\n");
187}
188
189
190int
191main (int argc, char *argv[])
192{
193 struct GNUNET_CRYPTO_EccDlogContext *edc;
194
195 if (! gcry_check_version ("1.6.0"))
196 {
197 fprintf (stderr,
198 _
199 (
200 "libgcrypt has not the expected version (version %s is required).\n"),
201 "1.6.0");
202 return 0;
203 }
204 if (getenv ("GNUNET_GCRYPT_DEBUG"))
205 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
206 GNUNET_log_setup ("test-crypto-ecc-dlog",
207 "WARNING",
208 NULL);
209 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_FACT,
210 MAX_MEM);
211 test_dlog (edc);
212 test_math (edc);
213 GNUNET_CRYPTO_ecc_dlog_release (edc);
214 return 0;
215}
216
217
218/* end of test_crypto_ecc_dlog.c */