der_tests.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "test.h"
  2. #ifndef LTC_DER
  3. int der_tests(void)
  4. {
  5. printf("NOP");
  6. return 0;
  7. }
  8. #else
  9. int der_tests(void)
  10. {
  11. unsigned long x, y, z, zz;
  12. unsigned char buf[2][4096];
  13. mp_int a, b, c, d, e, f, g;
  14. DO(mpi_to_ltc_error(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL)));
  15. for (zz = 0; zz < 16; zz++) {
  16. for (z = 0; z < 1024; z++) {
  17. if (yarrow_read(buf[0], z, &test_yarrow) != z) {
  18. printf("Failed to read %lu bytes from yarrow\n", z);
  19. return 1;
  20. }
  21. DO(mpi_to_ltc_error(mp_read_unsigned_bin(&a, buf[0], z)));
  22. x = sizeof(buf[0]);
  23. DO(der_encode_integer(&a, buf[0], &x));
  24. y = x;
  25. mp_zero(&b);
  26. DO(der_decode_integer(buf[0], &y, &b));
  27. if (y != x || mp_cmp(&a, &b) != MP_EQ) {
  28. printf("%lu: %lu vs %lu\n", z, x, y);
  29. #ifdef BN_MP_TORADIX_C
  30. mp_todecimal(&a, buf[0]);
  31. mp_todecimal(&b, buf[1]);
  32. printf("a == %s\nb == %s\n", buf[0], buf[1]);
  33. #endif
  34. mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
  35. return 1;
  36. }
  37. }
  38. }
  39. /* test the multi */
  40. mp_set(&a, 1);
  41. x = sizeof(buf[0]);
  42. DO(der_put_multi_integer(buf[0], &x, &a, NULL));
  43. y = x;
  44. mp_zero(&a);
  45. DO(der_get_multi_integer(buf[0], &y, &a, NULL));
  46. if (x != y || mp_cmp_d(&a, 1)) {
  47. printf("%lu, %lu, %d\n", x, y, mp_cmp_d(&a, 1));
  48. mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
  49. return 1;
  50. }
  51. mp_set(&a, 1);
  52. mp_set(&b, 2);
  53. x = sizeof(buf[0]);
  54. DO(der_put_multi_integer(buf[0], &x, &a, &b, NULL));
  55. y = x;
  56. mp_zero(&a);
  57. mp_zero(&b);
  58. DO(der_get_multi_integer(buf[0], &y, &a, &b, NULL));
  59. if (x != y || mp_cmp_d(&a, 1) || mp_cmp_d(&b, 2)) {
  60. printf("%lu, %lu, %d, %d\n", x, y, mp_cmp_d(&a, 1), mp_cmp_d(&b, 2));
  61. mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
  62. return 1;
  63. }
  64. mp_set(&a, 1);
  65. mp_set(&b, 2);
  66. mp_set(&c, 3);
  67. x = sizeof(buf[0]);
  68. DO(der_put_multi_integer(buf[0], &x, &a, &b, &c, NULL));
  69. y = x;
  70. mp_zero(&a);
  71. mp_zero(&b);
  72. mp_zero(&c);
  73. DO(der_get_multi_integer(buf[0], &y, &a, &b, &c, NULL));
  74. if (x != y || mp_cmp_d(&a, 1) || mp_cmp_d(&b, 2) || mp_cmp_d(&c, 3)) {
  75. printf("%lu, %lu, %d, %d, %d\n", x, y, mp_cmp_d(&a, 1), mp_cmp_d(&b, 2), mp_cmp_d(&c, 3));
  76. mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
  77. return 1;
  78. }
  79. mp_clear_multi(&a, &b, &c, &d, &e, &f, &g, NULL);
  80. return 0;
  81. }
  82. #endif