summaryrefslogtreecommitdiff
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.c163
1 files changed, 82 insertions, 81 deletions
diff --git a/src/util/test_crypto_ecc_dlog.c b/src/util/test_crypto_ecc_dlog.c
index 1be7bb678..b3b2a7480 100644
--- a/src/util/test_crypto_ecc_dlog.c
+++ b/src/util/test_crypto_ecc_dlog.c
@@ -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))
84 { 90 {
85 fprintf(stderr, "."); 91 gcry_mpi_set_ui (fact, x);
86 x = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 92 gcry_mpi_sub (fact, n, fact);
87 MAX_FACT); 93 x = -x;
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 {
104 fprintf(stderr,
105 "DLOG failed for value %d (%d)\n",
106 x,
107 iret);
108 GNUNET_assert(0);
109 }
110 } 94 }
111 gcry_mpi_release(fact); 95 else
112 gcry_mpi_release(n); 96 {
113 gcry_mpi_point_release(g); 97 gcry_mpi_set_ui (fact, x);
114 gcry_mpi_point_release(q); 98 }
115 gcry_ctx_release(ctx); 99 gcry_mpi_ec_mul (q, fact, g, ctx);
116 fprintf(stderr, "\n"); 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 }
110 }
111 gcry_mpi_release (fact);
112 gcry_mpi_release (n);
113 gcry_mpi_point_release (g);
114 gcry_mpi_point_release (q);
115 gcry_ctx_release (ctx);
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;
@@ -136,58 +136,59 @@ test_math(struct GNUNET_CRYPTO_EccDlogContext *edc)
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++)
139 { 142 {
140 ip = GNUNET_CRYPTO_ecc_dexp(edc, i); 143 fprintf (stderr, ".");
141 for (j = -MATH_MAX; j < MATH_MAX; j++) 144 jp = GNUNET_CRYPTO_ecc_dexp (edc, j);
142 { 145 GNUNET_CRYPTO_ecc_rnd (edc,
143 fprintf(stderr, "."); 146 &r,
144 jp = GNUNET_CRYPTO_ecc_dexp(edc, j); 147 &r_inv);
145 GNUNET_CRYPTO_ecc_rnd(edc, 148 ir = GNUNET_CRYPTO_ecc_add (edc, ip, r);
146 &r, 149 irj = GNUNET_CRYPTO_ecc_add (edc, ir, jp);
147 &r_inv); 150 sum = GNUNET_CRYPTO_ecc_add (edc, irj, r_inv);
148 ir = GNUNET_CRYPTO_ecc_add(edc, ip, r); 151 GNUNET_assert (i + j ==
149 irj = GNUNET_CRYPTO_ecc_add(edc, ir, jp); 152 GNUNET_CRYPTO_ecc_dlog (edc,
150 sum = GNUNET_CRYPTO_ecc_add(edc, irj, r_inv); 153 sum));
151 GNUNET_assert(i + j == 154 GNUNET_CRYPTO_ecc_free (jp);
152 GNUNET_CRYPTO_ecc_dlog(edc, 155 GNUNET_CRYPTO_ecc_free (ir);
153 sum)); 156 GNUNET_CRYPTO_ecc_free (irj);
154 GNUNET_CRYPTO_ecc_free(jp); 157 GNUNET_CRYPTO_ecc_free (r);
155 GNUNET_CRYPTO_ecc_free(ir); 158 GNUNET_CRYPTO_ecc_free (r_inv);
156 GNUNET_CRYPTO_ecc_free(irj); 159 GNUNET_CRYPTO_ecc_free (sum);
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);
162 } 160 }
163 fprintf(stderr, "\n"); 161 GNUNET_CRYPTO_ecc_free (ip);
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 (
178 "1.6.0"); 178 "libgcrypt has not the expected version (version %s is required).\n"),
179 return 0; 179 "1.6.0");
180 } 180 return 0;
181 if (getenv("GNUNET_GCRYPT_DEBUG")) 181 }
182 gcry_control(GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); 182 if (getenv ("GNUNET_GCRYPT_DEBUG"))
183 GNUNET_log_setup("test-crypto-ecc-dlog", 183 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
184 "WARNING", 184 GNUNET_log_setup ("test-crypto-ecc-dlog",
185 NULL); 185 "WARNING",
186 edc = GNUNET_CRYPTO_ecc_dlog_prepare(MAX_FACT, 186 NULL);
187 MAX_MEM); 187 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_FACT,
188 test_dlog(edc); 188 MAX_MEM);
189 test_math(edc); 189 test_dlog (edc);
190 GNUNET_CRYPTO_ecc_dlog_release(edc); 190 test_math (edc);
191 GNUNET_CRYPTO_ecc_dlog_release (edc);
191 return 0; 192 return 0;
192} 193}
193 194