aboutsummaryrefslogtreecommitdiff
path: root/src/scalarproduct/test_ecc_scalarproduct.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/scalarproduct/test_ecc_scalarproduct.c')
-rw-r--r--src/scalarproduct/test_ecc_scalarproduct.c187
1 files changed, 94 insertions, 93 deletions
diff --git a/src/scalarproduct/test_ecc_scalarproduct.c b/src/scalarproduct/test_ecc_scalarproduct.c
index f078ab37d..543ac4c7e 100644
--- a/src/scalarproduct/test_ecc_scalarproduct.c
+++ b/src/scalarproduct/test_ecc_scalarproduct.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_ecc_scalarproduct.c 22 * @file util/test_ecc_scalarproduct.c
23 * @brief testcase for math behind ECC SP calculation 23 * @brief testcase for math behind ECC SP calculation
@@ -41,8 +41,8 @@ static struct GNUNET_CRYPTO_EccDlogContext *edc;
41 * @return avec * bvec 41 * @return avec * bvec
42 */ 42 */
43static int 43static int
44test_sp (const unsigned int *avec, 44test_sp(const unsigned int *avec,
45 const unsigned int *bvec) 45 const unsigned int *bvec)
46{ 46{
47 unsigned int len; 47 unsigned int len;
48 unsigned int i; 48 unsigned int i;
@@ -61,114 +61,115 @@ test_sp (const unsigned int *avec,
61 int sp; 61 int sp;
62 62
63 /* determine length */ 63 /* determine length */
64 for (len=0;0 != avec[len];len++) ; 64 for (len = 0; 0 != avec[len]; len++)
65 ;
65 if (0 == len) 66 if (0 == len)
66 return 0; 67 return 0;
67 68
68 /* Alice */ 69 /* Alice */
69 GNUNET_CRYPTO_ecc_rnd_mpi (edc, 70 GNUNET_CRYPTO_ecc_rnd_mpi(edc,
70 &a, &a_inv); 71 &a, &a_inv);
71 g = GNUNET_new_array (len, 72 g = GNUNET_new_array(len,
72 gcry_mpi_point_t); 73 gcry_mpi_point_t);
73 h = GNUNET_new_array (len, 74 h = GNUNET_new_array(len,
74 gcry_mpi_point_t); 75 gcry_mpi_point_t);
75 ria = gcry_mpi_new (0); 76 ria = gcry_mpi_new(0);
76 tmp = gcry_mpi_new (0); 77 tmp = gcry_mpi_new(0);
77 for (i=0;i<len;i++) 78 for (i = 0; i < len; i++)
78 { 79 {
79 ri = GNUNET_CRYPTO_ecc_random_mod_n (edc); 80 ri = GNUNET_CRYPTO_ecc_random_mod_n(edc);
80 g[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc, 81 g[i] = GNUNET_CRYPTO_ecc_dexp_mpi(edc,
81 ri); 82 ri);
82 /* ria = ri * a */ 83 /* ria = ri * a */
83 gcry_mpi_mul (ria, 84 gcry_mpi_mul(ria,
84 ri, 85 ri,
85 a); 86 a);
86 /* tmp = ria + avec[i] */ 87 /* tmp = ria + avec[i] */
87 gcry_mpi_add_ui (tmp, 88 gcry_mpi_add_ui(tmp,
88 ria, 89 ria,
89 avec[i]); 90 avec[i]);
90 h[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc, 91 h[i] = GNUNET_CRYPTO_ecc_dexp_mpi(edc,
91 tmp); 92 tmp);
92 } 93 }
93 gcry_mpi_release (ria); 94 gcry_mpi_release(ria);
94 gcry_mpi_release (tmp); 95 gcry_mpi_release(tmp);
95 96
96 /* Bob */ 97 /* Bob */
97 val = gcry_mpi_new (0); 98 val = gcry_mpi_new(0);
98 gcry_mpi_set_ui (val, bvec[0]); 99 gcry_mpi_set_ui(val, bvec[0]);
99 pg = GNUNET_CRYPTO_ecc_pmul_mpi (edc, 100 pg = GNUNET_CRYPTO_ecc_pmul_mpi(edc,
100 g[0], 101 g[0],
101 val); 102 val);
102 ph = GNUNET_CRYPTO_ecc_pmul_mpi (edc, 103 ph = GNUNET_CRYPTO_ecc_pmul_mpi(edc,
103 h[0], 104 h[0],
104 val); 105 val);
105 for (i=1;i<len;i++) 106 for (i = 1; i < len; i++)
106 { 107 {
107 gcry_mpi_point_t m; 108 gcry_mpi_point_t m;
108 gcry_mpi_point_t tmp; 109 gcry_mpi_point_t tmp;
109 110
110 gcry_mpi_set_ui (val, bvec[i]); 111 gcry_mpi_set_ui(val, bvec[i]);
111 m = GNUNET_CRYPTO_ecc_pmul_mpi (edc, 112 m = GNUNET_CRYPTO_ecc_pmul_mpi(edc,
112 g[i], 113 g[i],
113 val); 114 val);
114 tmp = GNUNET_CRYPTO_ecc_add (edc, 115 tmp = GNUNET_CRYPTO_ecc_add(edc,
115 m, 116 m,
116 pg); 117 pg);
117 gcry_mpi_point_release (m); 118 gcry_mpi_point_release(m);
118 gcry_mpi_point_release (pg); 119 gcry_mpi_point_release(pg);
119 gcry_mpi_point_release (g[i]); 120 gcry_mpi_point_release(g[i]);
120 pg = tmp; 121 pg = tmp;
121 122
122 m = GNUNET_CRYPTO_ecc_pmul_mpi (edc, 123 m = GNUNET_CRYPTO_ecc_pmul_mpi(edc,
123 h[i], 124 h[i],
124 val); 125 val);
125 tmp = GNUNET_CRYPTO_ecc_add (edc, 126 tmp = GNUNET_CRYPTO_ecc_add(edc,
126 m, 127 m,
127 ph); 128 ph);
128 gcry_mpi_point_release (m); 129 gcry_mpi_point_release(m);
129 gcry_mpi_point_release (ph); 130 gcry_mpi_point_release(ph);
130 gcry_mpi_point_release (h[i]); 131 gcry_mpi_point_release(h[i]);
131 ph = tmp; 132 ph = tmp;
132 } 133 }
133 gcry_mpi_release (val); 134 gcry_mpi_release(val);
134 GNUNET_free (g); 135 GNUNET_free(g);
135 GNUNET_free (h); 136 GNUNET_free(h);
136 137
137 /* Alice */ 138 /* Alice */
138 pgi = GNUNET_CRYPTO_ecc_pmul_mpi (edc, 139 pgi = GNUNET_CRYPTO_ecc_pmul_mpi(edc,
139 pg, 140 pg,
140 a_inv); 141 a_inv);
141 gsp = GNUNET_CRYPTO_ecc_add (edc, 142 gsp = GNUNET_CRYPTO_ecc_add(edc,
142 pgi, 143 pgi,
143 ph); 144 ph);
144 gcry_mpi_point_release (pgi); 145 gcry_mpi_point_release(pgi);
145 gcry_mpi_point_release (ph); 146 gcry_mpi_point_release(ph);
146 sp = GNUNET_CRYPTO_ecc_dlog (edc, 147 sp = GNUNET_CRYPTO_ecc_dlog(edc,
147 gsp); 148 gsp);
148 gcry_mpi_point_release (gsp); 149 gcry_mpi_point_release(gsp);
149 return sp; 150 return sp;
150} 151}
151 152
152 153
153int 154int
154main (int argc, char *argv[]) 155main(int argc, char *argv[])
155{ 156{
156 static unsigned int v11[] = { 1, 1, 0 }; 157 static unsigned int v11[] = { 1, 1, 0 };
157 static unsigned int v22[] = { 2, 2, 0 }; 158 static unsigned int v22[] = { 2, 2, 0 };
158 static unsigned int v35[] = { 3, 5, 0 }; 159 static unsigned int v35[] = { 3, 5, 0 };
159 static unsigned int v24[] = { 2, 4, 0 }; 160 static unsigned int v24[] = { 2, 4, 0 };
160 161
161 GNUNET_log_setup ("test-ecc-scalarproduct", 162 GNUNET_log_setup("test-ecc-scalarproduct",
162 "WARNING", 163 "WARNING",
163 NULL); 164 NULL);
164 edc = GNUNET_CRYPTO_ecc_dlog_prepare (128, 128); 165 edc = GNUNET_CRYPTO_ecc_dlog_prepare(128, 128);
165 GNUNET_assert ( 2 == test_sp (v11, v11)); 166 GNUNET_assert(2 == test_sp(v11, v11));
166 GNUNET_assert ( 4 == test_sp (v22, v11)); 167 GNUNET_assert(4 == test_sp(v22, v11));
167 GNUNET_assert ( 8 == test_sp (v35, v11)); 168 GNUNET_assert(8 == test_sp(v35, v11));
168 GNUNET_assert (26 == test_sp (v35, v24)); 169 GNUNET_assert(26 == test_sp(v35, v24));
169 GNUNET_assert (26 == test_sp (v24, v35)); 170 GNUNET_assert(26 == test_sp(v24, v35));
170 GNUNET_assert (16 == test_sp (v22, v35)); 171 GNUNET_assert(16 == test_sp(v22, v35));
171 GNUNET_CRYPTO_ecc_dlog_release (edc); 172 GNUNET_CRYPTO_ecc_dlog_release(edc);
172 return 0; 173 return 0;
173} 174}
174 175