You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

148 lines
2.9 KiB

  1. #include "test.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #ifndef LIMBPERM
  5. #define LIMBPERM(x) (x)
  6. #endif
  7. int failed_tests, n_tests, failed_this_test, running_a_test;
  8. static void end_test(void) {
  9. if (!failed_this_test) {
  10. printf("[PASS]\n");
  11. }
  12. n_tests ++;
  13. running_a_test = 0;
  14. }
  15. static void begin_test(const char *name) {
  16. if (running_a_test) end_test();
  17. printf("%s...%*s",name,(int)(30-strlen(name)),"");
  18. fflush(stdout);
  19. failed_this_test = 0;
  20. running_a_test = 1;
  21. }
  22. void youfail(void) {
  23. if (failed_this_test) return;
  24. failed_this_test = 1;
  25. failed_tests ++;
  26. printf("[FAIL]\n");
  27. }
  28. static int
  29. hexchar (char c) {
  30. if (c >= '0' && c <= '9') {
  31. return c - '0';
  32. } else if (c >= 'a' && c <= 'f') {
  33. return 10 + c - 'a';
  34. } else if (c >= 'A' && c <= 'F') {
  35. return 10 + c - 'A';
  36. } else {
  37. return -1;
  38. }
  39. }
  40. int
  41. hexdecode (
  42. unsigned char *bytes,
  43. const char *hex,
  44. unsigned int nbytes
  45. ) {
  46. if (strlen(hex) != nbytes*2) {
  47. return -1;
  48. }
  49. unsigned int i;
  50. for (i=0; i<nbytes; i++) {
  51. int hi = hexchar(hex[2*i]),
  52. lo = hexchar(hex[2*i+1]);
  53. if (hi<0 || lo<0) return -1;
  54. bytes[i] = hi*16 + lo;
  55. }
  56. return 0;
  57. }
  58. void
  59. hexprint (
  60. const char *descr,
  61. const unsigned char *bytes,
  62. unsigned int nbytes
  63. ) {
  64. if (descr) printf("%s = ", descr);
  65. unsigned int i;
  66. for (i=0; i<nbytes; i++) {
  67. printf("%02x", bytes[i]);
  68. }
  69. printf("\n");
  70. }
  71. void field_print (
  72. const char *descr,
  73. const struct field_t *a
  74. ) {
  75. field_t b;
  76. field_copy(&b, a);
  77. field_strong_reduce(&b);
  78. int j;
  79. printf("%s = 0x", descr);
  80. for (j=FIELD_WORDS - 1; j>=0; j--) {
  81. printf(PRIxWORD58, b.limb[LIMBPERM(j)]);
  82. }
  83. printf("\n");
  84. }
  85. void scalar_print (
  86. const char *descr,
  87. const word_t *scalar,
  88. int nwords
  89. ) {
  90. int j;
  91. printf("%s = 0x", descr);
  92. for (j=nwords-1; j>=0; j--) {
  93. printf(PRIxWORDfull, scalar[j]);
  94. }
  95. printf("\n");
  96. }
  97. int main(int argc, char **argv) {
  98. (void) argc;
  99. (void) argv;
  100. n_tests = running_a_test = failed_tests = 0;
  101. begin_test("Arithmetic");
  102. test_arithmetic();
  103. begin_test("EC point operations");
  104. test_pointops();
  105. begin_test("Scalarmul compatibility");
  106. test_scalarmul_compatibility();
  107. begin_test("Scalarmul commutativity");
  108. test_scalarmul_commutativity();
  109. begin_test("Linear combo");
  110. test_linear_combo();
  111. begin_test("SHA-512 NIST Monte Carlo");
  112. test_sha512_monte_carlo();
  113. begin_test("Goldilocks complete system");
  114. test_goldilocks();
  115. if (running_a_test) end_test();
  116. printf("\n");
  117. if (failed_tests) {
  118. printf("Failed %d / %d tests.\n", failed_tests, n_tests);
  119. } else {
  120. printf("Passed all %d tests.\n", n_tests);
  121. }
  122. return failed_tests ? 1 : 0;
  123. }