ver2.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. uint32_t convert(uint64_t * nn)
  46. {
  47. uint32_t steps = 0;
  48. /**
  49. * Here we start making a bunch of assumptions.
  50. * First, we assume the "w" here is 64 bits, which should be the size
  51. * (in bits) of a mp_limb_t.
  52. * Secondly, the amount of zeros to check, "d" here is 8.
  53. */
  54. #define strip_size 8
  55. #define distinguished(x) ((x[23] & (~(ULLONG_MAX >> strip_size)))) == 0
  56. #define lbindex(x) __builtin_clzll(x);
  57. while (!distinguished(nn)) {
  58. /**
  59. * Here we try to find a strip of zeros for "w2" bits.
  60. * When we find one (up to w2 = 64), then we jump of w = w/2.
  61. * I tried to optimize this code:
  62. * - by integrating the if statement above with the for loop invariant;
  63. * - by making the loop algebraic (i.e. no if-s), given that in the
  64. * generated assembly I read a lot of jumps.
  65. * Unfortunately, both approaches actually lead to a slow down in the code.
  66. */
  67. uint64_t x = nn[23];
  68. if (x == 0) return steps;
  69. uint32_t first_bit = lbindex(x);
  70. uint32_t second_bit = 0;
  71. while (x != 0) {
  72. /* clear that bit */
  73. x &= ~(0x8000000000000000 >> first_bit);
  74. if (x == 0) {
  75. const uint32_t w = 64 - first_bit;
  76. if (w > strip_size) {
  77. return steps + first_bit + 1;
  78. } else {
  79. /**
  80. * We found no distinguished point.
  81. */
  82. const uint64_t a = mpn_lshift(nn, nn, 24, 36) * gg;
  83. mpn_add_1(nn, nn, 24, a);
  84. steps += 36;
  85. }
  86. } else {
  87. second_bit = lbindex(x);
  88. if (second_bit - first_bit > strip_size) {
  89. return steps + first_bit + 1;
  90. } else {
  91. first_bit = second_bit;
  92. }
  93. }
  94. }
  95. }
  96. return steps;
  97. }
  98. uint32_t naif_convert(mpz_t n)
  99. {
  100. uint32_t i;
  101. mpz_t t;
  102. mpz_init_set_ui(t, 1);
  103. mpz_mul_2exp(t, t, 1536-strip_size);
  104. for (i = 0; mpz_cmp(n, t) > -1; i++) {
  105. mpz_mul_2exp(n, n, 1);
  106. mpz_mod(n, n, p);
  107. }
  108. mpz_clear(t);
  109. return i;
  110. }
  111. int main()
  112. {
  113. mpz_init_set_str(p, p_str, 0);
  114. mpz_init_set_str(g, g_str, 10);
  115. gmp_randstate_t _rstate;
  116. unsigned long int _rseed;
  117. gmp_randinit_default(_rstate);
  118. getrandom(&_rseed, sizeof(unsigned long int), GRND_NONBLOCK); //GRND_RANDOM
  119. gmp_randseed_ui(_rstate, _rseed);
  120. mpz_t n, n0;
  121. mpz_inits(n, n0, NULL);
  122. INIT_TIMEIT();
  123. uint32_t converted;
  124. for (int i=0; i < 24e3; i++) {
  125. mpz_urandomm(n0, _rstate, p);
  126. mpz_set(n, n0);
  127. START_TIMEIT();
  128. converted = convert(n->_mp_d);
  129. END_TIMEIT();
  130. mpz_set(n, n0);
  131. assert(converted == naif_convert(n));
  132. }
  133. printf(TIMEIT_FORMAT "\n", GET_TIMEIT());
  134. /* memset(n->_mp_d, 0, 24*8); */
  135. /* memset(n0->_mp_d, 0, 24*8); */
  136. /* n0->_mp_d[0] = 13423523; */
  137. /* n0->_mp_d[1] = 1; */
  138. /* uint64_t v[64] = {0}; */
  139. /* unpack(v, n->_mp_d); */
  140. /* pack(n0, v); */
  141. mpz_clears(n, n0, p, g, NULL);
  142. return 0;
  143. }