test_ecc_scalarproduct.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2015 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/test_ecc_scalarproduct.c
  18. * @brief testcase for math behind ECC SP calculation
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include <gcrypt.h>
  24. /**
  25. * Global context.
  26. */
  27. static struct GNUNET_CRYPTO_EccDlogContext *edc;
  28. /**
  29. * Perform SP calculation.
  30. *
  31. * @param avec 0-terminated vector of Alice's values
  32. * @param bvec 0-terminated vector of Bob's values
  33. * @return avec * bvec
  34. */
  35. static int
  36. test_sp (const unsigned int *avec,
  37. const unsigned int *bvec)
  38. {
  39. unsigned int len;
  40. struct GNUNET_CRYPTO_EccScalar a;
  41. struct GNUNET_CRYPTO_EccScalar a_neg;
  42. struct GNUNET_CRYPTO_EccPoint *g;
  43. struct GNUNET_CRYPTO_EccPoint *h;
  44. struct GNUNET_CRYPTO_EccPoint pg;
  45. struct GNUNET_CRYPTO_EccPoint ph;
  46. /* determine length */
  47. for (len = 0; 0 != avec[len]; len++)
  48. ;
  49. if (0 == len)
  50. return 0;
  51. /* Alice */
  52. GNUNET_CRYPTO_ecc_rnd_mpi (&a,
  53. &a_neg);
  54. g = GNUNET_new_array (len,
  55. struct GNUNET_CRYPTO_EccPoint);
  56. h = GNUNET_new_array (len,
  57. struct GNUNET_CRYPTO_EccPoint);
  58. for (unsigned int i = 0; i < len; i++)
  59. {
  60. struct GNUNET_CRYPTO_EccScalar tmp;
  61. struct GNUNET_CRYPTO_EccScalar ri;
  62. struct GNUNET_CRYPTO_EccScalar ria;
  63. GNUNET_CRYPTO_ecc_random_mod_n (&ri);
  64. GNUNET_assert (GNUNET_OK ==
  65. GNUNET_CRYPTO_ecc_dexp_mpi (&ri,
  66. &g[i]));
  67. /* ria = ri * a mod L, where L is the order of the main subgroup */
  68. crypto_core_ed25519_scalar_mul (ria.v,
  69. ri.v,
  70. a.v);
  71. /* tmp = ria + avec[i] */
  72. {
  73. int64_t val = avec[i];
  74. struct GNUNET_CRYPTO_EccScalar vali;
  75. GNUNET_assert (INT64_MIN != val);
  76. GNUNET_CRYPTO_ecc_scalar_from_int (val > 0 ? val : -val,
  77. &vali);
  78. if (val > 0)
  79. crypto_core_ed25519_scalar_add (tmp.v,
  80. ria.v,
  81. vali.v);
  82. else
  83. crypto_core_ed25519_scalar_sub (tmp.v,
  84. ria.v,
  85. vali.v);
  86. }
  87. /* h[i] = g^tmp = g^{ria + avec[i]} */
  88. GNUNET_assert (GNUNET_OK ==
  89. GNUNET_CRYPTO_ecc_dexp_mpi (&tmp,
  90. &h[i]));
  91. }
  92. /* Bob */
  93. for (unsigned int i = 0; i < len; i++)
  94. {
  95. struct GNUNET_CRYPTO_EccPoint gm;
  96. struct GNUNET_CRYPTO_EccPoint hm;
  97. {
  98. int64_t val = bvec[i];
  99. struct GNUNET_CRYPTO_EccScalar vali;
  100. GNUNET_assert (INT64_MIN != val);
  101. GNUNET_CRYPTO_ecc_scalar_from_int (val > 0 ? val : -val,
  102. &vali);
  103. if (val < 0)
  104. crypto_core_ed25519_scalar_negate (vali.v,
  105. vali.v);
  106. /* gm = g[i]^vali */
  107. GNUNET_assert (GNUNET_OK ==
  108. GNUNET_CRYPTO_ecc_pmul_mpi (&g[i],
  109. &vali,
  110. &gm));
  111. /* hm = h[i]^vali */
  112. GNUNET_assert (GNUNET_OK ==
  113. GNUNET_CRYPTO_ecc_pmul_mpi (&h[i],
  114. &vali,
  115. &hm));
  116. }
  117. if (0 != i)
  118. {
  119. /* pg += gm */
  120. GNUNET_assert (GNUNET_OK ==
  121. GNUNET_CRYPTO_ecc_add (&gm,
  122. &pg,
  123. &pg));
  124. /* ph += hm */
  125. GNUNET_assert (GNUNET_OK ==
  126. GNUNET_CRYPTO_ecc_add (&hm,
  127. &ph,
  128. &ph));
  129. }
  130. else
  131. {
  132. pg = gm;
  133. ph = hm;
  134. }
  135. }
  136. GNUNET_free (g);
  137. GNUNET_free (h);
  138. /* Alice */
  139. {
  140. struct GNUNET_CRYPTO_EccPoint pgi;
  141. struct GNUNET_CRYPTO_EccPoint gsp;
  142. /* pgi = pg^inv */
  143. GNUNET_assert (GNUNET_OK ==
  144. GNUNET_CRYPTO_ecc_pmul_mpi (&pg,
  145. &a_neg,
  146. &pgi));
  147. /* gsp = pgi + ph */
  148. GNUNET_assert (GNUNET_OK ==
  149. GNUNET_CRYPTO_ecc_add (&pgi,
  150. &ph,
  151. &gsp));
  152. return GNUNET_CRYPTO_ecc_dlog (edc,
  153. &gsp);
  154. }
  155. }
  156. /**
  157. * Macro that checks that @a want is equal to @a have and
  158. * if not returns with a failure code.
  159. */
  160. #define CHECK(want,have) do { \
  161. if (want != have) { \
  162. GNUNET_break (0); \
  163. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, \
  164. "Wanted %d, got %d\n", want, have); \
  165. GNUNET_CRYPTO_ecc_dlog_release (edc); \
  166. return 1; \
  167. } } while (0)
  168. int
  169. main (int argc, char *argv[])
  170. {
  171. static unsigned int v11[] = { 1, 1, 0 };
  172. static unsigned int v22[] = { 2, 2, 0 };
  173. static unsigned int v35[] = { 3, 5, 0 };
  174. static unsigned int v24[] = { 2, 4, 0 };
  175. GNUNET_log_setup ("test-ecc-scalarproduct",
  176. "WARNING",
  177. NULL);
  178. edc = GNUNET_CRYPTO_ecc_dlog_prepare (128, 128);
  179. CHECK (2, test_sp (v11, v11));
  180. CHECK (4, test_sp (v22, v11));
  181. CHECK (8, test_sp (v35, v11));
  182. CHECK (26, test_sp (v35, v24));
  183. CHECK (26, test_sp (v24, v35));
  184. CHECK (16, test_sp (v22, v35));
  185. GNUNET_CRYPTO_ecc_dlog_release (edc);
  186. return 0;
  187. }
  188. /* end of test_ecc_scalarproduct.c */