summaryrefslogtreecommitdiff
path: root/src/scalarproduct/test_ecc_scalarproduct.c
blob: 8d3d716fb4d74d3a0850c33ef8e15389ffc4e20a (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
     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 Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     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
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later

 */
/**
 * @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;
  struct GNUNET_CRYPTO_EccScalar a;
  struct GNUNET_CRYPTO_EccScalar a_inv;
  struct GNUNET_CRYPTO_EccScalar ri;
  struct GNUNET_CRYPTO_EccScalar ria;
  struct GNUNET_CRYPTO_EccScalar tmp;
  struct GNUNET_CRYPTO_EccPoint *g;
  struct GNUNET_CRYPTO_EccPoint *h;
  struct GNUNET_CRYPTO_EccPoint pg;
  struct GNUNET_CRYPTO_EccPoint ph;
  struct GNUNET_CRYPTO_EccPoint pgi;
  struct GNUNET_CRYPTO_EccPoint gsp;

  /* determine length */
  for (len = 0; 0 != avec[len]; len++)
    ;
  if (0 == len)
    return 0;

  /* Alice */
  GNUNET_CRYPTO_ecc_rnd_mpi (&a,
                             &a_inv);
  g = GNUNET_new_array (len,
                        struct GNUNET_CRYPTO_EccPoint);
  h = GNUNET_new_array (len,
                        struct GNUNET_CRYPTO_EccPoint);
  for (unsigned int i = 0; i < len; i++)
  {
    GNUNET_CRYPTO_ecc_random_mod_n (&ri);
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CRYPTO_ecc_dexp_mpi (&ri,
                                               &g[i]));
    /* ria = ri * a */
    crypto_core_ed25519_scalar_mul (&ria.v[0],
                                    &ri.v[0],
                                    &a.v[0]);
    /* tmp = ria + avec[i] */
    {
      int64_t val = avec[i];
      struct GNUNET_CRYPTO_EccScalar vali;

      GNUNET_CRYPTO_ecc_scalar_from_int (val > 0 ? val : -val,
                                         &vali);
      tmp = ria;
      if (val > 0)
        sodium_add (tmp.v,
                    vali.v,
                    sizeof (vali.v));
      else
        sodium_sub (tmp.v,
                    vali.v,
                    sizeof (vali.v));
    }
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CRYPTO_ecc_dexp_mpi (&tmp,
                                               &h[i]));
  }

  /* Bob */
  {
    int64_t val = bvec[0];
    struct GNUNET_CRYPTO_EccScalar vali;

    GNUNET_CRYPTO_ecc_scalar_from_int (val > 0 ? val : -val,
                                       &vali);
    if (val < 0)
      crypto_core_ed25519_scalar_negate (&vali.v[0],
                                         &vali.v[0]);
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CRYPTO_ecc_pmul_mpi (&g[0],
                                               &vali,
                                               &pg));
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CRYPTO_ecc_pmul_mpi (&h[0],
                                               &vali,
                                               &ph));
  }
  for (unsigned int i = 0; i < len; i++)
  {
    struct GNUNET_CRYPTO_EccPoint m;

    {
      int64_t val = bvec[i];
      struct GNUNET_CRYPTO_EccScalar vali;

      GNUNET_CRYPTO_ecc_scalar_from_int (val > 0 ? val : -val,
                                         &vali);
      if (val < 0)
        crypto_core_ed25519_scalar_negate (&vali.v[0],
                                           &vali.v[0]);
      GNUNET_assert (GNUNET_OK ==
                     GNUNET_CRYPTO_ecc_pmul_mpi (&g[i],
                                                 &vali,
                                                 &m));
      GNUNET_assert (GNUNET_OK ==
                     GNUNET_CRYPTO_ecc_pmul_mpi (&h[i],
                                                 &vali,
                                                 &m));
    }

    if (0 != i)
    {
      struct GNUNET_CRYPTO_EccPoint tmp;

      GNUNET_assert (GNUNET_OK ==
                     GNUNET_CRYPTO_ecc_add (&m,
                                            &pg,
                                            &tmp));
      pg = tmp;
      GNUNET_assert (GNUNET_OK ==
                     GNUNET_CRYPTO_ecc_add (&m,
                                            &ph,
                                            &tmp));
      ph = tmp;
    }
  }
  GNUNET_free (g);
  GNUNET_free (h);

  /* Alice */
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CRYPTO_ecc_pmul_mpi (&pg,
                                             &a_inv,
                                             &pgi));
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CRYPTO_ecc_add (&pgi,
                                        &ph,
                                        &gsp));
  return GNUNET_CRYPTO_ecc_dlog (edc,
                                 &gsp);
}


/**
 * Macro that checks that @a want is equal to @a have and
 * if not returns with a failure code.
 */
#define CHECK(want,have) do { \
    if (want != have) {         \
      GNUNET_break (0);         \
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, \
                  "Wanted %d, got %d\n", want, have);   \
      GNUNET_CRYPTO_ecc_dlog_release (edc); \
      return 1; \
    } } while (0)


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);
  CHECK (2, test_sp (v11, v11));
  CHECK (4, test_sp (v22, v11));
  CHECK (8, test_sp (v35, v11));
  CHECK (26, test_sp (v35, v24));
  CHECK (26, test_sp (v24, v35));
  CHECK (16, test_sp (v22, v35));
  GNUNET_CRYPTO_ecc_dlog_release (edc);
  return 0;
}


/* end of test_ecc_scalarproduct.c */