der_tests.c 2.4 KB

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