aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_crypto_ecc_dlog.c
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
committerng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
commitd41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb (patch)
tree9efd18ea7d425652085ed0bd5e8e45604bc5f6b9 /src/util/test_crypto_ecc_dlog.c
parenta0fce305c565c0937d917a92712f15e9c5736260 (diff)
downloadgnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.tar.gz
gnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.zip
uncrustify as demanded.
Diffstat (limited to 'src/util/test_crypto_ecc_dlog.c')
-rw-r--r--src/util/test_crypto_ecc_dlog.c174
1 files changed, 87 insertions, 87 deletions
diff --git a/src/util/test_crypto_ecc_dlog.c b/src/util/test_crypto_ecc_dlog.c
index c0828c354..1be7bb678 100644
--- a/src/util/test_crypto_ecc_dlog.c
+++ b/src/util/test_crypto_ecc_dlog.c
@@ -11,13 +11,13 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 19
20*/ 20 */
21/** 21/**
22 * @file util/test_crypto_ecc_dlog.c 22 * @file util/test_crypto_ecc_dlog.c
23 * @brief testcase for ECC DLOG calculation 23 * @brief testcase for ECC DLOG calculation
@@ -48,12 +48,12 @@
48 48
49/** 49/**
50 * How many values do we test? 50 * How many values do we test?
51 */ 51 */
52#define TEST_ITER 10 52#define TEST_ITER 10
53 53
54/** 54/**
55 * Range of values to use for MATH tests. 55 * Range of values to use for MATH tests.
56 */ 56 */
57#define MATH_MAX 5 57#define MATH_MAX 5
58 58
59 59
@@ -63,7 +63,7 @@
63 * @param edc context for ECC operations 63 * @param edc context for ECC operations
64 */ 64 */
65static void 65static void
66test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc) 66test_dlog(struct GNUNET_CRYPTO_EccDlogContext *edc)
67{ 67{
68 gcry_mpi_t fact; 68 gcry_mpi_t fact;
69 gcry_mpi_t n; 69 gcry_mpi_t n;
@@ -74,46 +74,46 @@ test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc)
74 int x; 74 int x;
75 int iret; 75 int iret;
76 76
77 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE)); 77 GNUNET_assert(0 == gcry_mpi_ec_new(&ctx, NULL, CURVE));
78 g = gcry_mpi_ec_get_point ("g", ctx, 0); 78 g = gcry_mpi_ec_get_point("g", ctx, 0);
79 GNUNET_assert (NULL != g); 79 GNUNET_assert(NULL != g);
80 n = gcry_mpi_ec_get_mpi ("n", ctx, 0); 80 n = gcry_mpi_ec_get_mpi("n", ctx, 0);
81 q = gcry_mpi_point_new (0); 81 q = gcry_mpi_point_new(0);
82 fact = gcry_mpi_new (0); 82 fact = gcry_mpi_new(0);
83 for (i=0;i<TEST_ITER;i++) 83 for (i = 0; i < TEST_ITER; i++)
84 {
85 fprintf (stderr, ".");
86 x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
87 MAX_FACT);
88 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
89 2))
90 {
91 gcry_mpi_set_ui (fact, x);
92 gcry_mpi_sub (fact, n, fact);
93 x = - x;
94 }
95 else
96 {
97 gcry_mpi_set_ui (fact, x);
98 }
99 gcry_mpi_ec_mul (q, fact, g, ctx);
100 if (x !=
101 (iret = GNUNET_CRYPTO_ecc_dlog (edc,
102 q)))
103 { 84 {
104 fprintf (stderr, 85 fprintf(stderr, ".");
105 "DLOG failed for value %d (%d)\n", 86 x = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
106 x, 87 MAX_FACT);
107 iret); 88 if (0 == GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
108 GNUNET_assert (0); 89 2))
90 {
91 gcry_mpi_set_ui(fact, x);
92 gcry_mpi_sub(fact, n, fact);
93 x = -x;
94 }
95 else
96 {
97 gcry_mpi_set_ui(fact, x);
98 }
99 gcry_mpi_ec_mul(q, fact, g, ctx);
100 if (x !=
101 (iret = GNUNET_CRYPTO_ecc_dlog(edc,
102 q)))
103 {
104 fprintf(stderr,
105 "DLOG failed for value %d (%d)\n",
106 x,
107 iret);
108 GNUNET_assert(0);
109 }
109 } 110 }
110 } 111 gcry_mpi_release(fact);
111 gcry_mpi_release (fact); 112 gcry_mpi_release(n);
112 gcry_mpi_release (n); 113 gcry_mpi_point_release(g);
113 gcry_mpi_point_release (g); 114 gcry_mpi_point_release(q);
114 gcry_mpi_point_release (q); 115 gcry_ctx_release(ctx);
115 gcry_ctx_release (ctx); 116 fprintf(stderr, "\n");
116 fprintf (stderr, "\n");
117} 117}
118 118
119 119
@@ -123,7 +123,7 @@ test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc)
123 * @param edc context for ECC operations 123 * @param edc context for ECC operations
124 */ 124 */
125static void 125static void
126test_math (struct GNUNET_CRYPTO_EccDlogContext *edc) 126test_math(struct GNUNET_CRYPTO_EccDlogContext *edc)
127{ 127{
128 int i; 128 int i;
129 int j; 129 int j;
@@ -135,59 +135,59 @@ test_math (struct GNUNET_CRYPTO_EccDlogContext *edc)
135 gcry_mpi_point_t r_inv; 135 gcry_mpi_point_t r_inv;
136 gcry_mpi_point_t sum; 136 gcry_mpi_point_t sum;
137 137
138 for (i=-MATH_MAX;i<MATH_MAX;i++) 138 for (i = -MATH_MAX; i < MATH_MAX; i++)
139 {
140 ip = GNUNET_CRYPTO_ecc_dexp (edc, i);
141 for (j=-MATH_MAX;j<MATH_MAX;j++)
142 { 139 {
143 fprintf (stderr, "."); 140 ip = GNUNET_CRYPTO_ecc_dexp(edc, i);
144 jp = GNUNET_CRYPTO_ecc_dexp (edc, j); 141 for (j = -MATH_MAX; j < MATH_MAX; j++)
145 GNUNET_CRYPTO_ecc_rnd (edc, 142 {
146 &r, 143 fprintf(stderr, ".");
147 &r_inv); 144 jp = GNUNET_CRYPTO_ecc_dexp(edc, j);
148 ir = GNUNET_CRYPTO_ecc_add (edc, ip, r); 145 GNUNET_CRYPTO_ecc_rnd(edc,
149 irj = GNUNET_CRYPTO_ecc_add (edc, ir, jp); 146 &r,
150 sum = GNUNET_CRYPTO_ecc_add (edc, irj, r_inv); 147 &r_inv);
151 GNUNET_assert (i + j == 148 ir = GNUNET_CRYPTO_ecc_add(edc, ip, r);
152 GNUNET_CRYPTO_ecc_dlog (edc, 149 irj = GNUNET_CRYPTO_ecc_add(edc, ir, jp);
153 sum)); 150 sum = GNUNET_CRYPTO_ecc_add(edc, irj, r_inv);
154 GNUNET_CRYPTO_ecc_free (jp); 151 GNUNET_assert(i + j ==
155 GNUNET_CRYPTO_ecc_free (ir); 152 GNUNET_CRYPTO_ecc_dlog(edc,
156 GNUNET_CRYPTO_ecc_free (irj); 153 sum));
157 GNUNET_CRYPTO_ecc_free (r); 154 GNUNET_CRYPTO_ecc_free(jp);
158 GNUNET_CRYPTO_ecc_free (r_inv); 155 GNUNET_CRYPTO_ecc_free(ir);
159 GNUNET_CRYPTO_ecc_free (sum); 156 GNUNET_CRYPTO_ecc_free(irj);
157 GNUNET_CRYPTO_ecc_free(r);
158 GNUNET_CRYPTO_ecc_free(r_inv);
159 GNUNET_CRYPTO_ecc_free(sum);
160 }
161 GNUNET_CRYPTO_ecc_free(ip);
160 } 162 }
161 GNUNET_CRYPTO_ecc_free (ip); 163 fprintf(stderr, "\n");
162 }
163 fprintf (stderr, "\n");
164} 164}
165 165
166 166
167 167
168int 168int
169main (int argc, char *argv[]) 169main(int argc, char *argv[])
170{ 170{
171 struct GNUNET_CRYPTO_EccDlogContext *edc; 171 struct GNUNET_CRYPTO_EccDlogContext *edc;
172 172
173 if (! gcry_check_version ("1.6.0")) 173 if (!gcry_check_version("1.6.0"))
174 { 174 {
175 fprintf (stderr, 175 fprintf(stderr,
176 _ 176 _
177 ("libgcrypt has not the expected version (version %s is required).\n"), 177 ("libgcrypt has not the expected version (version %s is required).\n"),
178 "1.6.0"); 178 "1.6.0");
179 return 0; 179 return 0;
180 } 180 }
181 if (getenv ("GNUNET_GCRYPT_DEBUG")) 181 if (getenv("GNUNET_GCRYPT_DEBUG"))
182 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0); 182 gcry_control(GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
183 GNUNET_log_setup ("test-crypto-ecc-dlog", 183 GNUNET_log_setup("test-crypto-ecc-dlog",
184 "WARNING", 184 "WARNING",
185 NULL); 185 NULL);
186 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_FACT, 186 edc = GNUNET_CRYPTO_ecc_dlog_prepare(MAX_FACT,
187 MAX_MEM); 187 MAX_MEM);
188 test_dlog (edc); 188 test_dlog(edc);
189 test_math (edc); 189 test_math(edc);
190 GNUNET_CRYPTO_ecc_dlog_release (edc); 190 GNUNET_CRYPTO_ecc_dlog_release(edc);
191 return 0; 191 return 0;
192} 192}
193 193