aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_crypto_ecc_dlog.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-07-06 14:22:51 +0000
committerChristian Grothoff <christian@grothoff.org>2015-07-06 14:22:51 +0000
commit0f9e6bcd1e511abae16ecc4c86056b0c26d73936 (patch)
tree4ba3af76391ee6c67563316de29b6ad8830cd7f2 /src/util/test_crypto_ecc_dlog.c
parentf1e619572751f7652db025f66f119d6a0308114b (diff)
downloadgnunet-0f9e6bcd1e511abae16ecc4c86056b0c26d73936.tar.gz
gnunet-0f9e6bcd1e511abae16ecc4c86056b0c26d73936.zip
-fix non-deterministic peerstore sync failure
Diffstat (limited to 'src/util/test_crypto_ecc_dlog.c')
-rw-r--r--src/util/test_crypto_ecc_dlog.c102
1 files changed, 90 insertions, 12 deletions
diff --git a/src/util/test_crypto_ecc_dlog.c b/src/util/test_crypto_ecc_dlog.c
index a594e5795..2101b28f1 100644
--- a/src/util/test_crypto_ecc_dlog.c
+++ b/src/util/test_crypto_ecc_dlog.c
@@ -22,9 +22,6 @@
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
24 * @author Christian Grothoff 24 * @author Christian Grothoff
25 *
26 * TODO:
27 * - test negative numbers
28 */ 25 */
29#include "platform.h" 26#include "platform.h"
30#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
@@ -42,52 +39,132 @@
42/** 39/**
43 * Maximum value we test dlog for. 40 * Maximum value we test dlog for.
44 */ 41 */
45#define MAX_FACT 1000000 42#define MAX_FACT 100
46 43
47/** 44/**
48 * Maximum memory to use, sqrt(MAX_FACT) is a good choice. 45 * Maximum memory to use, sqrt(MAX_FACT) is a good choice.
49 */ 46 */
50#define MAX_MEM 1000 47#define MAX_MEM 10
48
49/**
50 * How many values do we test?
51 */
52#define TEST_ITER 10
51 53
54/**
55 * Range of values to use for MATH tests.
56 */
57#define MATH_MAX 5
52 58
59
60/**
61 * Do some DLOG operations for testing.
62 *
63 * @param edc context for ECC operations
64 */
53static void 65static void
54test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc) 66test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc)
55{ 67{
56 gcry_mpi_t fact; 68 gcry_mpi_t fact;
69 gcry_mpi_t n;
57 gcry_ctx_t ctx; 70 gcry_ctx_t ctx;
58 gcry_mpi_point_t q; 71 gcry_mpi_point_t q;
59 gcry_mpi_point_t g; 72 gcry_mpi_point_t g;
60 unsigned int i; 73 unsigned int i;
61 unsigned int x; 74 int x;
75 int iret;
62 76
63 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE)); 77 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
64 g = gcry_mpi_ec_get_point ("g", ctx, 0); 78 g = gcry_mpi_ec_get_point ("g", ctx, 0);
65 GNUNET_assert (NULL != g); 79 GNUNET_assert (NULL != g);
80 n = gcry_mpi_ec_get_mpi ("n", ctx, 0);
66 q = gcry_mpi_point_new (0); 81 q = gcry_mpi_point_new (0);
67 fact = gcry_mpi_new (0); 82 fact = gcry_mpi_new (0);
68 for (i=0;i<10;i++) 83 for (i=0;i<TEST_ITER;i++)
69 { 84 {
85 fprintf (stderr, ".");
70 x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 86 x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
71 MAX_FACT); 87 MAX_FACT);
72 gcry_mpi_set_ui (fact, 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 }
73 gcry_mpi_ec_mul (q, fact, g, ctx); 99 gcry_mpi_ec_mul (q, fact, g, ctx);
74 if (x != 100 if (x !=
75 GNUNET_CRYPTO_ecc_dlog (edc, 101 (iret = GNUNET_CRYPTO_ecc_dlog (edc,
76 q)) 102 q)))
77 { 103 {
78 fprintf (stderr, 104 fprintf (stderr,
79 "DLOG failed for value %u\n", 105 "DLOG failed for value %d (%d)\n",
80 x); 106 x,
107 iret);
81 GNUNET_assert (0); 108 GNUNET_assert (0);
82 } 109 }
83 } 110 }
84 gcry_mpi_release (fact); 111 gcry_mpi_release (fact);
112 gcry_mpi_release (n);
85 gcry_mpi_point_release (g); 113 gcry_mpi_point_release (g);
86 gcry_mpi_point_release (q); 114 gcry_mpi_point_release (q);
87 gcry_ctx_release (ctx); 115 gcry_ctx_release (ctx);
116 fprintf (stderr, "\n");
117}
118
119
120/**
121 * Do some arithmetic operations for testing.
122 *
123 * @param edc context for ECC operations
124 */
125static void
126test_math (struct GNUNET_CRYPTO_EccDlogContext *edc)
127{
128 int i;
129 int j;
130 gcry_mpi_point_t ip;
131 gcry_mpi_point_t jp;
132 gcry_mpi_point_t r;
133 gcry_mpi_point_t ir;
134 gcry_mpi_point_t irj;
135 gcry_mpi_point_t r_inv;
136 gcry_mpi_point_t sum;
137
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 {
143 fprintf (stderr, ".");
144 jp = GNUNET_CRYPTO_ecc_dexp (edc, j);
145 GNUNET_CRYPTO_ecc_rnd (edc,
146 &r,
147 &r_inv);
148 ir = GNUNET_CRYPTO_ecc_add (edc, ip, r);
149 irj = GNUNET_CRYPTO_ecc_add (edc, ir, jp);
150 sum = GNUNET_CRYPTO_ecc_add (edc, irj, r_inv);
151 GNUNET_assert (i + j ==
152 GNUNET_CRYPTO_ecc_dlog (edc,
153 sum));
154 GNUNET_CRYPTO_ecc_free (jp);
155 GNUNET_CRYPTO_ecc_free (ir);
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);
162 }
163 fprintf (stderr, "\n");
88} 164}
89 165
90 166
167
91int 168int
92main (int argc, char *argv[]) 169main (int argc, char *argv[])
93{ 170{
@@ -109,6 +186,7 @@ main (int argc, char *argv[])
109 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_FACT, 186 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_FACT,
110 MAX_MEM); 187 MAX_MEM);
111 test_dlog (edc); 188 test_dlog (edc);
189 test_math (edc);
112 GNUNET_CRYPTO_ecc_dlog_release (edc); 190 GNUNET_CRYPTO_ecc_dlog_release (edc);
113 return 0; 191 return 0;
114} 192}