cyclic.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * ZMap Copyright 2013 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. */
  8. /*
  9. * cyclic provides an inexpensive approach to iterating over the IPv4 address
  10. * space in a random(-ish) manner such that we connect to every host once in
  11. * a scan execution without having to keep track of the IPs that have been
  12. * scanned or need to be scanned and such that each scan has a different
  13. * ordering. We accomplish this by utilizing a cyclic multiplicative group
  14. * of integers modulo a prime and generating a new primitive root (generator)
  15. * for each scan.
  16. *
  17. * We know that 3 is a generator of (Z mod 2^32 + 15 - {0}, *)
  18. * and that we have coverage over the entire address space because 2**32 + 15
  19. * is prime and ||(Z mod PRIME - {0}, *)|| == PRIME - 1. Therefore, we
  20. * just need to find a new generator (primitive root) of the cyclic group for
  21. * each scan that we perform.
  22. *
  23. * Because generators map to generators over an isomorphism, we can efficiently
  24. * find random primitive roots of our mult. group by finding random generators
  25. * of the group (Zp-1, +) which is isomorphic to (Zp*, *). Specifically the
  26. * generators of (Zp-1, +) are { s | (s, p-1) == 1 } which implies that
  27. * the generators of (Zp*, *) are { d^s | (s, p-1) == 1 }. where d is a known
  28. * generator of the multiplicative group. We efficiently find
  29. * generators of the additive group by precalculating the psub1_f of
  30. * p - 1 and randomly checking random numbers against the psub1_f until
  31. * we find one that is coprime and map it into Zp*. Because
  32. * totient(totient(p)) ~= 10^9, this should take relatively few
  33. * iterations to find a new generator.
  34. */
  35. #include "cyclic.h"
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <stdint.h>
  39. #include <time.h>
  40. #include <assert.h>
  41. #include <string.h>
  42. #include <math.h>
  43. #include <gmp.h>
  44. #include "../lib/includes.h"
  45. #include "../lib/logger.h"
  46. // We will pick the first cyclic group from this list that is
  47. // larger than the number of IPs in our whitelist. E.g. for an
  48. // entire Internet scan, this would be cyclic32
  49. // Note: this list should remain ordered by size (primes) ascending.
  50. static cyclic_group_t groups[] = {
  51. { // 2^8 + 1
  52. .prime = 257,
  53. .known_primroot = 3,
  54. .prime_factors = {2},
  55. .num_prime_factors = 1
  56. },
  57. { // 2^16 + 1
  58. .prime = 65537,
  59. .known_primroot = 3,
  60. .prime_factors = {2},
  61. .num_prime_factors = 1
  62. },
  63. { // 2^24 + 43
  64. .prime = 16777259,
  65. .known_primroot = 2,
  66. .prime_factors = {2, 23, 103, 3541},
  67. .num_prime_factors = 4
  68. },
  69. { // 2^28 + 3
  70. .prime = 268435459,
  71. .known_primroot = 2,
  72. .prime_factors = {2, 3, 19, 87211},
  73. .num_prime_factors = 4
  74. },
  75. { // 2^32 + 15
  76. .prime = 4294967311,
  77. .known_primroot = 3,
  78. .prime_factors = {2, 3, 5, 131, 364289},
  79. .num_prime_factors = 5
  80. }
  81. };
  82. #define COPRIME 1
  83. #define NOT_COPRIME 0
  84. // Check whether an integer is coprime with (p - 1)
  85. static int check_coprime(uint64_t check, const cyclic_group_t *group)
  86. {
  87. for (unsigned i=0; i < group->num_prime_factors; i++) {
  88. if (group->prime_factors[i] > check && !(group->prime_factors[i] % check)) {
  89. return NOT_COPRIME;
  90. } else if (group->prime_factors[i] < check && !(check % group->prime_factors[i])) {
  91. return NOT_COPRIME;
  92. } else if (group->prime_factors[i] == check) {
  93. return NOT_COPRIME;
  94. }
  95. }
  96. return COPRIME;
  97. }
  98. // Return a (random) number coprime with (p - 1) of the group,
  99. // which is a generator of the additive group mod (p - 1)
  100. static uint32_t find_primroot(const cyclic_group_t *group, aesrand_t *aes)
  101. {
  102. uint32_t candidate = (uint32_t) ((aesrand_getword(aes) & 0xFFFFFFFF) % group->prime);
  103. if (candidate == 0) {
  104. ++candidate;
  105. }
  106. while (check_coprime(candidate, group) != COPRIME) {
  107. ++candidate;
  108. //special case where we need to restart check from begin
  109. if(candidate >= group->prime) {
  110. candidate = 1;
  111. }
  112. }
  113. uint64_t retv = isomorphism(candidate, group);
  114. return retv;
  115. }
  116. const cyclic_group_t* get_group(uint64_t min_size)
  117. {
  118. for(unsigned i = 0; i < sizeof(groups); ++i) {
  119. if (groups[i].prime > min_size) {
  120. return &groups[i];
  121. }
  122. }
  123. // Should not reach, final group should always be larger than 2^32
  124. assert(0);
  125. }
  126. cycle_t make_cycle(const cyclic_group_t* group, aesrand_t *aes)
  127. {
  128. cycle_t cycle;
  129. cycle.group = group;
  130. cycle.generator = find_primroot(group, aes);
  131. cycle.offset = (uint32_t) (aesrand_getword(aes) & 0xFFFFFFFF);
  132. cycle.offset %= group->prime;
  133. return cycle;
  134. }
  135. uint64_t isomorphism(uint64_t additive_elt, const cyclic_group_t* mult_group)
  136. {
  137. assert(additive_elt < mult_group->prime);
  138. mpz_t base, power, prime, primroot;
  139. mpz_init_set_ui(base, mult_group->known_primroot);
  140. mpz_init_set_ui(power, additive_elt);
  141. mpz_init_set_ui(prime, mult_group->prime);
  142. mpz_init(primroot);
  143. mpz_powm(primroot, base, power, prime);
  144. uint64_t retv = (uint64_t) mpz_get_ui(primroot);
  145. log_trace("zmap", "Isomorphism: %llu", retv);
  146. mpz_clear(base);
  147. mpz_clear(power);
  148. mpz_clear(prime);
  149. mpz_clear(primroot);
  150. return retv;
  151. }