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.
 
 
 
 
 

150 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 field_a_t a
  74. ) {
  75. int j;
  76. unsigned char ser[FIELD_BYTES];
  77. field_serialize(ser,a);
  78. printf("%s = 0x", descr);
  79. for (j=FIELD_BYTES - 1; j>=0; j--) {
  80. printf("%02x", ser[j]);
  81. }
  82. printf("\n");
  83. }
  84. void scalar_print (
  85. const char *descr,
  86. const word_t *scalar,
  87. int nwords
  88. ) {
  89. int j;
  90. printf("%s = 0x", descr);
  91. for (j=nwords-1; j>=0; j--) {
  92. printf(PRIxWORDfull, scalar[j]);
  93. }
  94. printf("\n");
  95. }
  96. int main(int argc, char **argv) {
  97. (void) argc;
  98. (void) argv;
  99. n_tests = running_a_test = failed_tests = 0;
  100. begin_test("Arithmetic");
  101. test_arithmetic();
  102. begin_test("EC point operations");
  103. test_pointops();
  104. begin_test("Decaf point encoding");
  105. test_decaf();
  106. begin_test("Scalarmul compatibility");
  107. test_scalarmul_compatibility();
  108. begin_test("Scalarmul commutativity");
  109. test_scalarmul_commutativity();
  110. begin_test("Linear combo");
  111. test_linear_combo();
  112. begin_test("SHA-512 NIST Monte Carlo");
  113. test_sha512_monte_carlo();
  114. begin_test("Goldilocks complete system");
  115. test_goldilocks();
  116. if (running_a_test) end_test();
  117. printf("\n");
  118. if (failed_tests) {
  119. printf("Failed %d / %d tests.\n", failed_tests, n_tests);
  120. } else {
  121. printf("Passed all %d tests.\n", n_tests);
  122. }
  123. return failed_tests ? 1 : 0;
  124. }