ver1.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #define _GNU_SOURCE
  2. #include <assert.h>
  3. #include <limits.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <linux/random.h>
  10. #include <sys/syscall.h>
  11. #include <sys/time.h>
  12. #include <gmp.h>
  13. #define INIT_TIMEIT() \
  14. struct timeval __start, __end; \
  15. double __sdiff = 0, __udiff = 0
  16. #define START_TIMEIT() \
  17. gettimeofday(&__start, NULL)
  18. #define END_TIMEIT() \
  19. gettimeofday(&__end, NULL); \
  20. __sdiff += (__end.tv_sec - __start.tv_sec); \
  21. __udiff += (__end.tv_usec - __start.tv_usec)
  22. #define GET_TIMEIT() \
  23. __sdiff + __udiff * 1e-6
  24. #define TIMEIT_FORMAT "%lf"
  25. /**
  26. * p is our prime modulus, and is 2^n - g
  27. * where g is referred to as "gamma" (built-in function in C, so transliterated)
  28. */
  29. const static char* p_str =
  30. "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  31. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  32. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  33. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  34. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  35. "505CAF";
  36. const static char* g_str =
  37. "11510609";
  38. mpz_t p, g;
  39. uint32_t gg = 11510609;
  40. static inline ssize_t
  41. getrandom(void *buffer, size_t length, unsigned int flags)
  42. {
  43. return syscall(SYS_getrandom, buffer, length, flags);
  44. }
  45. INIT_TIMEIT();
  46. static const uint32_t strip_size = 16;
  47. static uint8_t lookup[256];
  48. static uint8_t offset[256];
  49. uint32_t convert(uint64_t *nn)
  50. {
  51. uint32_t steps = 0;
  52. /**
  53. * Here we start making a bunch of assumptions.
  54. * First, we assume the "w" here is 64 bits, which should be the size
  55. * (in bits) of a mp_limb_t.
  56. * Secondly, the amount of zeros to check, "d" here is 8.
  57. */
  58. static const uint32_t window = (32+30) /8;
  59. mpz_t a;
  60. mpz_init(a);
  61. #define distinguished(x) (((x)[23] & (~(ULLONG_MAX >> strip_size)))) == 0
  62. while (!distinguished(nn)) {
  63. /**
  64. * Here we try to find a strip of zeros for "w2" bits.
  65. * When we find one (up to w2 = 64), then we jump of w = w/2.
  66. * I tried to optimize this code:
  67. * - by integrating the if statement above with the for loop invariant;
  68. * - by making the loop algebraic (i.e. no if-s), given that in the
  69. * generated assembly I read a lot of jumps.
  70. * Unfortunately, both approaches actually lead to a slow down in the code.
  71. */
  72. START_TIMEIT();
  73. const uint8_t *x = (uint8_t *) &nn[22];
  74. uint8_t *y = memrchr(x, '\0', window*2-1);
  75. if (y && y[-1] <= lookup[y[+1]]) {
  76. return steps + (x + 15 - y)*8 - offset[y[+1]];
  77. }
  78. END_TIMEIT();
  79. /**
  80. * We found no distinguished point.
  81. */
  82. const uint64_t a = mpn_lshift(nn, nn, 24, window) * gg;
  83. mpn_add_1(nn, nn, 24, a);
  84. steps += window;
  85. }
  86. mpz_clear(a);
  87. return steps;
  88. }
  89. uint32_t naif_convert(mpz_t n)
  90. {
  91. uint32_t i;
  92. mpz_t t;
  93. mpz_init_set_ui(t, 1);
  94. mpz_mul_2exp(t, t, 1536-16);
  95. for (i = 0; mpz_cmp(n, t) > -1; i++) {
  96. mpz_mul_2exp(n, n, 1);
  97. mpz_mod(n, n, p);
  98. }
  99. mpz_clear(t);
  100. return i;
  101. }
  102. int main()
  103. {
  104. mpz_init_set_str(p, p_str, 0);
  105. mpz_init_set_str(g, g_str, 10);
  106. gmp_randstate_t _rstate;
  107. unsigned long int _rseed;
  108. gmp_randinit_default(_rstate);
  109. getrandom(&_rseed, sizeof(unsigned long int), GRND_NONBLOCK);
  110. gmp_randseed_ui(_rstate, _rseed);
  111. for (uint32_t i = 0; i <= 0xFF; i++) {
  112. uint32_t j = ffs(i) ? ffs(i) - 1 : 8;
  113. lookup[i] = 0xFF >> (8-j);
  114. offset[i] = j;
  115. }
  116. uint32_t converted;
  117. for (uint64_t i=0; i < 1e4; i++) {
  118. mpz_t n, n0;
  119. mpz_inits(n, n0, NULL);
  120. mpz_urandomm(n0, _rstate, p);
  121. mpz_set(n, n0);
  122. converted = convert(n->_mp_d);
  123. mpz_set(n, n0);
  124. #ifndef NDEBUG
  125. uint32_t expected = naif_convert(n);
  126. if (converted != expected) printf("%d %d\n", converted, expected);
  127. assert(converted == expected);
  128. #endif
  129. mpz_clears(n, n0, NULL);
  130. }
  131. printf(TIMEIT_FORMAT "\n", GET_TIMEIT());
  132. /* memset(n->_mp_d, 0, 24*8); */
  133. /* memset(n0->_mp_d, 0, 24*8); */
  134. /* n0->_mp_d[0] = 13423523; */
  135. /* n0->_mp_d[1] = 1; */
  136. /* uint64_t v[64] = {0}; */
  137. /* unpack(v, n->_mp_d); */
  138. /* pack(n0, v); */
  139. mpz_clears(p, g, NULL);
  140. return 0;
  141. }