ecp_test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. *******************************************************************************
  3. \file ecp_test.c
  4. \brief Tests for elliptic curves over prime fields
  5. \project bee2/test
  6. \author (C) Sergey Agievich [agievich@{bsu.by|gmail.com}]
  7. \created 2017.05.29
  8. \version 2017.08.23
  9. \license This program is released under the GNU General Public License
  10. version 3. See Copyright Notices in bee2/info.h.
  11. *******************************************************************************
  12. */
  13. #include <bee2/core/hex.h>
  14. #include <bee2/core/obj.h>
  15. #include <bee2/core/util.h>
  16. #include <bee2/math/gfp.h>
  17. #include <bee2/math/ecp.h>
  18. /*
  19. *******************************************************************************
  20. Проверочная кривая
  21. *******************************************************************************
  22. */
  23. static const size_t no = 32;
  24. static char p[] =
  25. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF43";
  26. static char a[] =
  27. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF40";
  28. static char b[] =
  29. "00000000000000000000000000000000000000000000000000000000000014B8";
  30. static char q[] =
  31. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D1229165911507C328526818EC4E11D";
  32. static char xbase[] =
  33. "0000000000000000000000000000000000000000000000000000000000000000";
  34. static char ybase[] =
  35. "B0E9804939D7C2E931D4CE052CCC6B6B692514CCADBA44940484EEA5F52D9268";
  36. static u32 cofactor = 1;
  37. /*
  38. *******************************************************************************
  39. Тестирование
  40. *******************************************************************************
  41. */
  42. bool_t ecpTest()
  43. {
  44. // размерности
  45. const size_t n = W_OF_O(no);
  46. const size_t f_keep = gfpCreate_keep(no);
  47. const size_t f_deep = gfpCreate_deep(no);
  48. const size_t ec_keep = ecpCreateJ_keep(n);
  49. const size_t ec_deep = ecpCreateJ_deep(n, f_deep);
  50. // состояние и стек
  51. octet state[2048];
  52. octet stack[2048];
  53. octet t[96];
  54. // поле и эк
  55. qr_o* f;
  56. ec_o* ec;
  57. // хватает памяти?
  58. ASSERT(f_keep + ec_keep <= sizeof(state));
  59. ASSERT(ec_deep <= sizeof(stack));
  60. // создать f = GF(p)
  61. hexToRev(t, p);
  62. f = (qr_o*)(state + ec_keep);
  63. if (!gfpCreate(f, t, no, stack))
  64. return FALSE;
  65. // создать ec = EC_{ab}(f)
  66. hexToRev(t, a), hexToRev(t + 32, b);
  67. ec = (ec_o*)state;
  68. if (!ecpCreateJ(ec, f, t, t + 32, stack))
  69. return FALSE;
  70. // создать группу точек ec
  71. hexToRev(t, xbase), hexToRev(t + 32, ybase), hexToRev(t + 64, q);
  72. if (!ecCreateGroup(ec, t, t + 32, t + 64, no, cofactor, stack))
  73. return FALSE;
  74. // присоединить f к ec
  75. objAppend(ec, f, 0);
  76. // корректная кривая?
  77. ASSERT(ecpIsValid_deep(n, f_deep) <= sizeof(stack));
  78. if (!ecpIsValid(ec, stack))
  79. return FALSE;
  80. // корректная группа?
  81. ASSERT(ecpSeemsValidGroup_deep(n, f_deep) <= sizeof(stack));
  82. if (!ecpSeemsValidGroup(ec, stack))
  83. return FALSE;
  84. // надежная группа?
  85. ASSERT(ecpIsSafeGroup_deep(n) <= sizeof(stack));
  86. if (!ecpIsSafeGroup(ec, 40, stack))
  87. return FALSE;
  88. // базовая точка имеет порядок q?
  89. ASSERT(ecHasOrderA_deep(n, ec->d, ec_deep, n) <= sizeof(stack));
  90. if (!ecHasOrderA(ec->base, ec, ec->order, n, stack))
  91. return FALSE;
  92. // все нормально
  93. return TRUE;
  94. }