ddlog.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <limits.h>
  4. #include <strings.h>
  5. #include <gmp.h>
  6. #include "ddlog.h"
  7. #include "group.h"
  8. #include "hss.h"
  9. uint64_t lookup[0x01 << strip_size];
  10. uint64_t offset[0x01 << strip_size];
  11. static const uint64_t topmask = ~(ULLONG_MAX >> halfstrip_size);
  12. //static const uint64_t topbigmask = ~(ULLONG_MAX >> strip_size);
  13. static const uint64_t bottommask = (0x01 << halfstrip_size) -1;
  14. static const uint64_t failuremask = ~(ULLONG_MAX >> FAILURE);
  15. #define distinguished_limb 0x8000000000000000
  16. #define clz(x) __builtin_clzll(x)
  17. static inline
  18. void add_1(uint64_t *b, const size_t start, const size_t len, uint64_t a)
  19. {
  20. for (size_t i = start; a != 0; i = (i+1)%len) {
  21. const uint64_t r = b[i] + a;
  22. a = (r < a);
  23. b[i] = r;
  24. }
  25. /*
  26. we don't check for overflows in "i".
  27. If it happens, buy a lottery ticket and retry.
  28. */
  29. }
  30. #define zdistinguished(x) (((x)[head] & topbigmask)) == 0
  31. uint32_t __attribute__((optimize("unroll-loops"))) convert(uint64_t * nn)
  32. {
  33. uint32_t steps;
  34. size_t head = 23;
  35. #define next_head ((head + 23) % 24)
  36. #define tail ((head + 1) % 24)
  37. #define next_tail ((head + 2) % 24)
  38. /** Check the most significant block */
  39. const uint64_t x = nn[head];
  40. for (uint32_t w2 = clz(x) + halfstrip_size - (clz(x) % halfstrip_size);
  41. w2 < 64 - halfstrip_size;
  42. w2 += halfstrip_size) {
  43. if (!(x & (topmask >> w2))) {
  44. const size_t previous = (x >> (64 - halfstrip_size - w2 + halfstrip_size)) & bottommask;
  45. const uint32_t next = (x >> (64 - halfstrip_size - w2 - halfstrip_size)) & bottommask;
  46. if (next <= lookup[previous]) return w2 - offset[previous] - 1;
  47. }
  48. }
  49. for (steps = 64; true; steps += 64) {
  50. const uint64_t x = nn[head];
  51. const uint64_t y = nn[next_head];
  52. if (!(x & bottommask)) {
  53. const size_t previous = (x >> halfstrip_size) & bottommask;
  54. const uint32_t next = y >> (64 - halfstrip_size);
  55. if (next <= lookup[previous]) return steps - halfstrip_size - offset[previous] - 1;
  56. }
  57. if (!(y & topmask)) {
  58. const size_t previous = x & bottommask;
  59. const uint32_t next = (y >> (64 - 2*halfstrip_size)) & bottommask;
  60. if (next <= lookup[previous]) return steps - offset[previous] - 1;
  61. }
  62. for (uint32_t w2 = halfstrip_size; w2 < 64-halfstrip_size; w2 += halfstrip_size) {
  63. if (!(y & (topmask >> w2))) {
  64. const size_t previous = (y >> (64 - halfstrip_size - w2 + halfstrip_size)) & bottommask;
  65. const uint32_t next = (y >> (64 - halfstrip_size - w2 - halfstrip_size)) & bottommask;
  66. if (next <= lookup[previous]) return steps + w2 - offset[previous] - 1;
  67. }
  68. }
  69. /**
  70. * We found no distinguished point.
  71. */
  72. const uint128_t a = (uint128_t) x * gg;
  73. const uint64_t al = (uint64_t) a;
  74. const uint64_t ah = (a >> 64);
  75. head = next_head;
  76. nn[tail] = al;
  77. add_1(nn, next_tail, 24, ah);
  78. }
  79. }
  80. bool distinguished(mpz_t n)
  81. {
  82. return (n->_mp_d[23] & failuremask) == distinguished_limb;
  83. }
  84. uint32_t naif_convert(mpz_t n)
  85. {
  86. uint32_t steps;
  87. for (steps = 0; !distinguished(n); steps++) {
  88. mpz_mul_ui_modp(n, n, 2);
  89. }
  90. return steps;
  91. }
  92. void dlog_precompute()
  93. {
  94. for (size_t i = 0; i <= bottommask; i++) {
  95. uint32_t j = ffs(i) ? ffs(i) - 1 : halfstrip_size;
  96. lookup[i] = bottommask >> (halfstrip_size - j);
  97. offset[i] = j;
  98. }
  99. }