ver1.c 4.6 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. 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 halfstrip_size 4
  56. /** Check if x has a halfstrip starting from start */
  57. #define halfstrip(x, start) (((x) << (start)) & 0xF000000000000000)
  58. #define distinguished(x) ((x[23] & (~(ULLONG_MAX >> strip_size)))) == 0
  59. #define lbindex(x) __builtin_clzll(x);
  60. #define tbindex(x) __builtin_ctzll(x);
  61. uint32_t w2 = 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. uint64_t x = nn[23];
  73. for (w2 = 4; w2 < 32; w2 += halfstrip_size) {
  74. if (halfstrip(x, w2) == 0) {
  75. const uint64_t previous_strip = halfstrip(x, w2-halfstrip_size);
  76. uint32_t before = tbindex(previous_strip); before -= 60;
  77. const uint64_t next_strip = halfstrip(x, w2+halfstrip_size);
  78. uint32_t after = lbindex(next_strip); if (next_strip == 0) after = 4;
  79. if (before + after >= halfstrip_size) {
  80. return steps + w2 - before;
  81. }
  82. }
  83. }
  84. /**
  85. * We found no distinguished point.
  86. */
  87. const uint64_t a = mpn_lshift(nn, nn, 24, 16) * gg;
  88. mpn_add_1(nn, nn, 32, a);
  89. steps += 16;
  90. }
  91. return steps;
  92. }
  93. uint32_t naif_convert(mpz_t n)
  94. {
  95. uint32_t i;
  96. mpz_t t;
  97. mpz_init_set_ui(t, 1);
  98. mpz_mul_2exp(t, t, 1536-strip_size);
  99. for (i = 0; mpz_cmp(n, t) > -1; i++) {
  100. mpz_mul_2exp(n, n, 1);
  101. mpz_mod(n, n, p);
  102. }
  103. mpz_clear(t);
  104. return i;
  105. }
  106. int main()
  107. {
  108. mpz_init_set_str(p, p_str, 0);
  109. mpz_init_set_str(g, g_str, 10);
  110. gmp_randstate_t _rstate;
  111. unsigned long int _rseed;
  112. gmp_randinit_default(_rstate);
  113. getrandom(&_rseed, sizeof(unsigned long int), GRND_NONBLOCK); //GRND_RANDOM
  114. gmp_randseed_ui(_rstate, _rseed);
  115. mpz_t n, n0;
  116. mpz_inits(n, n0, NULL);
  117. INIT_TIMEIT();
  118. uint32_t converted, expected;
  119. for (int i=0; i < 1e6; i++) {
  120. mpz_urandomm(n0, _rstate, p);
  121. mpz_set(n, n0);
  122. START_TIMEIT();
  123. converted = convert(n->_mp_d);
  124. END_TIMEIT();
  125. mpz_set(n, n0);
  126. expected = naif_convert(n);
  127. if (converted != expected) printf("%d %d\n", converted, expected);
  128. assert(converted == expected);
  129. }
  130. printf(TIMEIT_FORMAT "\n", GET_TIMEIT());
  131. /* memset(n->_mp_d, 0, 24*8); */
  132. /* memset(n0->_mp_d, 0, 24*8); */
  133. /* n0->_mp_d[0] = 13423523; */
  134. /* n0->_mp_d[1] = 1; */
  135. /* uint64_t v[64] = {0}; */
  136. /* unpack(v, n->_mp_d); */
  137. /* pack(n0, v); */
  138. mpz_clears(n, n0, p, g, NULL);
  139. return 0;
  140. }