diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-09-05 19:20:06 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-09-05 19:20:06 +0000 |
commit | e8cafb8bf10ef6ebd8f07b64fa2bfab948dd6294 (patch) | |
tree | e114a760800d07604b65d4eedf64083bd30ae392 /src/scalarproduct | |
parent | dce29804b2af0ff52ea3365039024d3dd1079617 (diff) | |
download | gnunet-e8cafb8bf10ef6ebd8f07b64fa2bfab948dd6294.tar.gz gnunet-e8cafb8bf10ef6ebd8f07b64fa2bfab948dd6294.zip |
add testcase for SP logic
Diffstat (limited to 'src/scalarproduct')
-rw-r--r-- | src/scalarproduct/Makefile.am | 3 | ||||
-rw-r--r-- | src/scalarproduct/test_ecc_scalarproduct.c | 114 |
2 files changed, 113 insertions, 4 deletions
diff --git a/src/scalarproduct/Makefile.am b/src/scalarproduct/Makefile.am index 57df0e840..e2487d9f3 100644 --- a/src/scalarproduct/Makefile.am +++ b/src/scalarproduct/Makefile.am | |||
@@ -114,4 +114,5 @@ endif | |||
114 | test_ecc_scalarproduct_SOURCES = \ | 114 | test_ecc_scalarproduct_SOURCES = \ |
115 | test_ecc_scalarproduct.c | 115 | test_ecc_scalarproduct.c |
116 | test_ecc_scalarproduct_LDADD = \ | 116 | test_ecc_scalarproduct_LDADD = \ |
117 | $(top_builddir)/src/util/libgnunetutil.la | 117 | $(top_builddir)/src/util/libgnunetutil.la \ |
118 | -lgcrypt | ||
diff --git a/src/scalarproduct/test_ecc_scalarproduct.c b/src/scalarproduct/test_ecc_scalarproduct.c index 273fb666f..3d81dbd31 100644 --- a/src/scalarproduct/test_ecc_scalarproduct.c +++ b/src/scalarproduct/test_ecc_scalarproduct.c | |||
@@ -27,6 +27,11 @@ | |||
27 | #include "gnunet_util_lib.h" | 27 | #include "gnunet_util_lib.h" |
28 | #include <gcrypt.h> | 28 | #include <gcrypt.h> |
29 | 29 | ||
30 | /** | ||
31 | * Global context. | ||
32 | */ | ||
33 | static struct GNUNET_CRYPTO_EccDlogContext *edc; | ||
34 | |||
30 | 35 | ||
31 | /** | 36 | /** |
32 | * Perform SP calculation. | 37 | * Perform SP calculation. |
@@ -39,7 +44,110 @@ static int | |||
39 | test_sp (const unsigned int *avec, | 44 | test_sp (const unsigned int *avec, |
40 | const unsigned int *bvec) | 45 | const unsigned int *bvec) |
41 | { | 46 | { |
42 | return -1; | 47 | unsigned int len; |
48 | unsigned int i; | ||
49 | gcry_mpi_t a; | ||
50 | gcry_mpi_t a_inv; | ||
51 | gcry_mpi_t ri; | ||
52 | gcry_mpi_t val; | ||
53 | gcry_mpi_point_t *g; | ||
54 | gcry_mpi_point_t *h; | ||
55 | gcry_mpi_point_t pg; | ||
56 | gcry_mpi_point_t ph; | ||
57 | gcry_mpi_point_t pgi; | ||
58 | gcry_mpi_point_t gsp; | ||
59 | int sp; | ||
60 | |||
61 | /* determine length */ | ||
62 | for (len=0;0 != avec[len];len++) ; | ||
63 | if (0 == len) | ||
64 | return 0; | ||
65 | |||
66 | /* Alice */ | ||
67 | GNUNET_CRYPTO_ecc_rnd_mpi (edc, | ||
68 | &a, &a_inv); | ||
69 | g = GNUNET_new_array (len, | ||
70 | gcry_mpi_point_t); | ||
71 | h = GNUNET_new_array (len, | ||
72 | gcry_mpi_point_t); | ||
73 | for (i=0;i<len;i++) | ||
74 | { | ||
75 | gcry_mpi_t tmp; | ||
76 | gcry_mpi_t ria; | ||
77 | |||
78 | ri = GNUNET_CRYPTO_ecc_random_mod_n (edc); | ||
79 | g[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc, | ||
80 | ri); | ||
81 | /* ria = ri * a */ | ||
82 | ria = gcry_mpi_new (0); | ||
83 | gcry_mpi_mul (ria, | ||
84 | ri, | ||
85 | a); | ||
86 | /* tmp = ria + avec[i] */ | ||
87 | tmp = gcry_mpi_new (0); | ||
88 | gcry_mpi_add_ui (tmp, | ||
89 | ria, | ||
90 | avec[i]); | ||
91 | gcry_mpi_release (ria); | ||
92 | h[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc, | ||
93 | tmp); | ||
94 | gcry_mpi_release (tmp); | ||
95 | } | ||
96 | |||
97 | /* Bob */ | ||
98 | val = gcry_mpi_new (0); | ||
99 | gcry_mpi_set_ui (val, bvec[0]); | ||
100 | pg = GNUNET_CRYPTO_ecc_pmul_mpi (edc, | ||
101 | g[0], | ||
102 | val); | ||
103 | ph = GNUNET_CRYPTO_ecc_pmul_mpi (edc, | ||
104 | h[0], | ||
105 | val); | ||
106 | for (i=1;i<len;i++) | ||
107 | { | ||
108 | gcry_mpi_point_t m; | ||
109 | gcry_mpi_point_t tmp; | ||
110 | |||
111 | gcry_mpi_set_ui (val, bvec[i]); | ||
112 | m = GNUNET_CRYPTO_ecc_pmul_mpi (edc, | ||
113 | g[i], | ||
114 | val); | ||
115 | tmp = GNUNET_CRYPTO_ecc_add (edc, | ||
116 | m, | ||
117 | pg); | ||
118 | gcry_mpi_point_release (m); | ||
119 | gcry_mpi_point_release (pg); | ||
120 | gcry_mpi_point_release (g[i]); | ||
121 | pg = tmp; | ||
122 | |||
123 | m = GNUNET_CRYPTO_ecc_pmul_mpi (edc, | ||
124 | h[i], | ||
125 | val); | ||
126 | tmp = GNUNET_CRYPTO_ecc_add (edc, | ||
127 | m, | ||
128 | ph); | ||
129 | gcry_mpi_point_release (m); | ||
130 | gcry_mpi_point_release (ph); | ||
131 | gcry_mpi_point_release (h[i]); | ||
132 | ph = tmp; | ||
133 | } | ||
134 | gcry_mpi_release (val); | ||
135 | GNUNET_free (g); | ||
136 | GNUNET_free (h); | ||
137 | |||
138 | /* Alice */ | ||
139 | pgi = GNUNET_CRYPTO_ecc_pmul_mpi (edc, | ||
140 | pg, | ||
141 | a_inv); | ||
142 | gsp = GNUNET_CRYPTO_ecc_add (edc, | ||
143 | pgi, | ||
144 | ph); | ||
145 | gcry_mpi_point_release (pgi); | ||
146 | gcry_mpi_point_release (ph); | ||
147 | sp = GNUNET_CRYPTO_ecc_dlog (edc, | ||
148 | gsp); | ||
149 | gcry_mpi_point_release (gsp); | ||
150 | return sp; | ||
43 | } | 151 | } |
44 | 152 | ||
45 | 153 | ||
@@ -51,17 +159,17 @@ main (int argc, char *argv[]) | |||
51 | static unsigned int v35[] = { 3, 5, 0 }; | 159 | static unsigned int v35[] = { 3, 5, 0 }; |
52 | static unsigned int v24[] = { 2, 4, 0 }; | 160 | static unsigned int v24[] = { 2, 4, 0 }; |
53 | 161 | ||
54 | if (1) | ||
55 | return 0; // disable for now | ||
56 | GNUNET_log_setup ("test-ecc-scalarproduct", | 162 | GNUNET_log_setup ("test-ecc-scalarproduct", |
57 | "WARNING", | 163 | "WARNING", |
58 | NULL); | 164 | NULL); |
165 | edc = GNUNET_CRYPTO_ecc_dlog_prepare (128, 128); | ||
59 | GNUNET_assert ( 2 == test_sp (v11, v11)); | 166 | GNUNET_assert ( 2 == test_sp (v11, v11)); |
60 | GNUNET_assert ( 4 == test_sp (v22, v11)); | 167 | GNUNET_assert ( 4 == test_sp (v22, v11)); |
61 | GNUNET_assert ( 8 == test_sp (v35, v11)); | 168 | GNUNET_assert ( 8 == test_sp (v35, v11)); |
62 | GNUNET_assert (26 == test_sp (v35, v24)); | 169 | GNUNET_assert (26 == test_sp (v35, v24)); |
63 | GNUNET_assert (26 == test_sp (v24, v35)); | 170 | GNUNET_assert (26 == test_sp (v24, v35)); |
64 | GNUNET_assert (16 == test_sp (v22, v35)); | 171 | GNUNET_assert (16 == test_sp (v22, v35)); |
172 | GNUNET_CRYPTO_ecc_dlog_release (edc); | ||
65 | return 0; | 173 | return 0; |
66 | } | 174 | } |
67 | 175 | ||