1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
This file is part of GNUnet.
Copyright (C) 2015 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
* @file util/test_ecc_scalarproduct.c
* @brief testcase for math behind ECC SP calculation
* @author Christian Grothoff
*/
#include "platform.h"
#include "gnunet_util_lib.h"
#include <gcrypt.h>
/**
* Global context.
*/
static struct GNUNET_CRYPTO_EccDlogContext *edc;
/**
* Perform SP calculation.
*
* @param avec 0-terminated vector of Alice's values
* @param bvec 0-terminated vector of Bob's values
* @return avec * bvec
*/
static int
test_sp (const unsigned int *avec,
const unsigned int *bvec)
{
unsigned int len;
unsigned int i;
gcry_mpi_t a;
gcry_mpi_t a_inv;
gcry_mpi_t ri;
gcry_mpi_t val;
gcry_mpi_t ria;
gcry_mpi_t tmp;
gcry_mpi_point_t *g;
gcry_mpi_point_t *h;
gcry_mpi_point_t pg;
gcry_mpi_point_t ph;
gcry_mpi_point_t pgi;
gcry_mpi_point_t gsp;
int sp;
/* determine length */
for (len=0;0 != avec[len];len++) ;
if (0 == len)
return 0;
/* Alice */
GNUNET_CRYPTO_ecc_rnd_mpi (edc,
&a, &a_inv);
g = GNUNET_new_array (len,
gcry_mpi_point_t);
h = GNUNET_new_array (len,
gcry_mpi_point_t);
ria = gcry_mpi_new (0);
tmp = gcry_mpi_new (0);
for (i=0;i<len;i++)
{
ri = GNUNET_CRYPTO_ecc_random_mod_n (edc);
g[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
ri);
/* ria = ri * a */
gcry_mpi_mul (ria,
ri,
a);
/* tmp = ria + avec[i] */
gcry_mpi_add_ui (tmp,
ria,
avec[i]);
h[i] = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
tmp);
}
gcry_mpi_release (ria);
gcry_mpi_release (tmp);
/* Bob */
val = gcry_mpi_new (0);
gcry_mpi_set_ui (val, bvec[0]);
pg = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
g[0],
val);
ph = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
h[0],
val);
for (i=1;i<len;i++)
{
gcry_mpi_point_t m;
gcry_mpi_point_t tmp;
gcry_mpi_set_ui (val, bvec[i]);
m = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
g[i],
val);
tmp = GNUNET_CRYPTO_ecc_add (edc,
m,
pg);
gcry_mpi_point_release (m);
gcry_mpi_point_release (pg);
gcry_mpi_point_release (g[i]);
pg = tmp;
m = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
h[i],
val);
tmp = GNUNET_CRYPTO_ecc_add (edc,
m,
ph);
gcry_mpi_point_release (m);
gcry_mpi_point_release (ph);
gcry_mpi_point_release (h[i]);
ph = tmp;
}
gcry_mpi_release (val);
GNUNET_free (g);
GNUNET_free (h);
/* Alice */
pgi = GNUNET_CRYPTO_ecc_pmul_mpi (edc,
pg,
a_inv);
gsp = GNUNET_CRYPTO_ecc_add (edc,
pgi,
ph);
gcry_mpi_point_release (pgi);
gcry_mpi_point_release (ph);
sp = GNUNET_CRYPTO_ecc_dlog (edc,
gsp);
gcry_mpi_point_release (gsp);
return sp;
}
int
main (int argc, char *argv[])
{
static unsigned int v11[] = { 1, 1, 0 };
static unsigned int v22[] = { 2, 2, 0 };
static unsigned int v35[] = { 3, 5, 0 };
static unsigned int v24[] = { 2, 4, 0 };
GNUNET_log_setup ("test-ecc-scalarproduct",
"WARNING",
NULL);
edc = GNUNET_CRYPTO_ecc_dlog_prepare (128, 128);
GNUNET_assert ( 2 == test_sp (v11, v11));
GNUNET_assert ( 4 == test_sp (v22, v11));
GNUNET_assert ( 8 == test_sp (v35, v11));
GNUNET_assert (26 == test_sp (v35, v24));
GNUNET_assert (26 == test_sp (v24, v35));
GNUNET_assert (16 == test_sp (v22, v35));
GNUNET_CRYPTO_ecc_dlog_release (edc);
return 0;
}
/* end of test_ecc_scalarproduct.c */
|