test_residue.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. from collections import defaultdict
  2. from sympy.core.containers import Tuple
  3. from sympy.core.singleton import S
  4. from sympy.core.symbol import (Dummy, Symbol)
  5. from sympy.ntheory import n_order, is_primitive_root, is_quad_residue, \
  6. legendre_symbol, jacobi_symbol, totient, primerange, sqrt_mod, \
  7. primitive_root, quadratic_residues, is_nthpow_residue, nthroot_mod, \
  8. sqrt_mod_iter, mobius, discrete_log, quadratic_congruence, \
  9. polynomial_congruence
  10. from sympy.ntheory.residue_ntheory import _primitive_root_prime_iter, \
  11. _discrete_log_trial_mul, _discrete_log_shanks_steps, \
  12. _discrete_log_pollard_rho, _discrete_log_pohlig_hellman
  13. from sympy.polys.domains import ZZ
  14. from sympy.testing.pytest import raises
  15. def test_residue():
  16. assert n_order(2, 13) == 12
  17. assert [n_order(a, 7) for a in range(1, 7)] == \
  18. [1, 3, 6, 3, 6, 2]
  19. assert n_order(5, 17) == 16
  20. assert n_order(17, 11) == n_order(6, 11)
  21. assert n_order(101, 119) == 6
  22. assert n_order(11, (10**50 + 151)**2) == 10000000000000000000000000000000000000000000000030100000000000000000000000000000000000000000000022650
  23. raises(ValueError, lambda: n_order(6, 9))
  24. assert is_primitive_root(2, 7) is False
  25. assert is_primitive_root(3, 8) is False
  26. assert is_primitive_root(11, 14) is False
  27. assert is_primitive_root(12, 17) == is_primitive_root(29, 17)
  28. raises(ValueError, lambda: is_primitive_root(3, 6))
  29. for p in primerange(3, 100):
  30. it = _primitive_root_prime_iter(p)
  31. assert len(list(it)) == totient(totient(p))
  32. assert primitive_root(97) == 5
  33. assert primitive_root(97**2) == 5
  34. assert primitive_root(40487) == 5
  35. # note that primitive_root(40487) + 40487 = 40492 is a primitive root
  36. # of 40487**2, but it is not the smallest
  37. assert primitive_root(40487**2) == 10
  38. assert primitive_root(82) == 7
  39. p = 10**50 + 151
  40. assert primitive_root(p) == 11
  41. assert primitive_root(2*p) == 11
  42. assert primitive_root(p**2) == 11
  43. raises(ValueError, lambda: primitive_root(-3))
  44. assert is_quad_residue(3, 7) is False
  45. assert is_quad_residue(10, 13) is True
  46. assert is_quad_residue(12364, 139) == is_quad_residue(12364 % 139, 139)
  47. assert is_quad_residue(207, 251) is True
  48. assert is_quad_residue(0, 1) is True
  49. assert is_quad_residue(1, 1) is True
  50. assert is_quad_residue(0, 2) == is_quad_residue(1, 2) is True
  51. assert is_quad_residue(1, 4) is True
  52. assert is_quad_residue(2, 27) is False
  53. assert is_quad_residue(13122380800, 13604889600) is True
  54. assert [j for j in range(14) if is_quad_residue(j, 14)] == \
  55. [0, 1, 2, 4, 7, 8, 9, 11]
  56. raises(ValueError, lambda: is_quad_residue(1.1, 2))
  57. raises(ValueError, lambda: is_quad_residue(2, 0))
  58. assert quadratic_residues(S.One) == [0]
  59. assert quadratic_residues(1) == [0]
  60. assert quadratic_residues(12) == [0, 1, 4, 9]
  61. assert quadratic_residues(13) == [0, 1, 3, 4, 9, 10, 12]
  62. assert [len(quadratic_residues(i)) for i in range(1, 20)] == \
  63. [1, 2, 2, 2, 3, 4, 4, 3, 4, 6, 6, 4, 7, 8, 6, 4, 9, 8, 10]
  64. assert list(sqrt_mod_iter(6, 2)) == [0]
  65. assert sqrt_mod(3, 13) == 4
  66. assert sqrt_mod(3, -13) == 4
  67. assert sqrt_mod(6, 23) == 11
  68. assert sqrt_mod(345, 690) == 345
  69. assert sqrt_mod(67, 101) == None
  70. assert sqrt_mod(1020, 104729) == None
  71. for p in range(3, 100):
  72. d = defaultdict(list)
  73. for i in range(p):
  74. d[pow(i, 2, p)].append(i)
  75. for i in range(1, p):
  76. it = sqrt_mod_iter(i, p)
  77. v = sqrt_mod(i, p, True)
  78. if v:
  79. v = sorted(v)
  80. assert d[i] == v
  81. else:
  82. assert not d[i]
  83. assert sqrt_mod(9, 27, True) == [3, 6, 12, 15, 21, 24]
  84. assert sqrt_mod(9, 81, True) == [3, 24, 30, 51, 57, 78]
  85. assert sqrt_mod(9, 3**5, True) == [3, 78, 84, 159, 165, 240]
  86. assert sqrt_mod(81, 3**4, True) == [0, 9, 18, 27, 36, 45, 54, 63, 72]
  87. assert sqrt_mod(81, 3**5, True) == [9, 18, 36, 45, 63, 72, 90, 99, 117,\
  88. 126, 144, 153, 171, 180, 198, 207, 225, 234]
  89. assert sqrt_mod(81, 3**6, True) == [9, 72, 90, 153, 171, 234, 252, 315,\
  90. 333, 396, 414, 477, 495, 558, 576, 639, 657, 720]
  91. assert sqrt_mod(81, 3**7, True) == [9, 234, 252, 477, 495, 720, 738, 963,\
  92. 981, 1206, 1224, 1449, 1467, 1692, 1710, 1935, 1953, 2178]
  93. for a, p in [(26214400, 32768000000), (26214400, 16384000000),
  94. (262144, 1048576), (87169610025, 163443018796875),
  95. (22315420166400, 167365651248000000)]:
  96. assert pow(sqrt_mod(a, p), 2, p) == a
  97. n = 70
  98. a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+2)
  99. it = sqrt_mod_iter(a, p)
  100. for i in range(10):
  101. assert pow(next(it), 2, p) == a
  102. a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+3)
  103. it = sqrt_mod_iter(a, p)
  104. for i in range(2):
  105. assert pow(next(it), 2, p) == a
  106. n = 100
  107. a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+1)
  108. it = sqrt_mod_iter(a, p)
  109. for i in range(2):
  110. assert pow(next(it), 2, p) == a
  111. assert type(next(sqrt_mod_iter(9, 27))) is int
  112. assert type(next(sqrt_mod_iter(9, 27, ZZ))) is type(ZZ(1))
  113. assert type(next(sqrt_mod_iter(1, 7, ZZ))) is type(ZZ(1))
  114. assert is_nthpow_residue(2, 1, 5)
  115. #issue 10816
  116. assert is_nthpow_residue(1, 0, 1) is False
  117. assert is_nthpow_residue(1, 0, 2) is True
  118. assert is_nthpow_residue(3, 0, 2) is True
  119. assert is_nthpow_residue(0, 1, 8) is True
  120. assert is_nthpow_residue(2, 3, 2) is True
  121. assert is_nthpow_residue(2, 3, 9) is False
  122. assert is_nthpow_residue(3, 5, 30) is True
  123. assert is_nthpow_residue(21, 11, 20) is True
  124. assert is_nthpow_residue(7, 10, 20) is False
  125. assert is_nthpow_residue(5, 10, 20) is True
  126. assert is_nthpow_residue(3, 10, 48) is False
  127. assert is_nthpow_residue(1, 10, 40) is True
  128. assert is_nthpow_residue(3, 10, 24) is False
  129. assert is_nthpow_residue(1, 10, 24) is True
  130. assert is_nthpow_residue(3, 10, 24) is False
  131. assert is_nthpow_residue(2, 10, 48) is False
  132. assert is_nthpow_residue(81, 3, 972) is False
  133. assert is_nthpow_residue(243, 5, 5103) is True
  134. assert is_nthpow_residue(243, 3, 1240029) is False
  135. assert is_nthpow_residue(36010, 8, 87382) is True
  136. assert is_nthpow_residue(28552, 6, 2218) is True
  137. assert is_nthpow_residue(92712, 9, 50026) is True
  138. x = {pow(i, 56, 1024) for i in range(1024)}
  139. assert {a for a in range(1024) if is_nthpow_residue(a, 56, 1024)} == x
  140. x = { pow(i, 256, 2048) for i in range(2048)}
  141. assert {a for a in range(2048) if is_nthpow_residue(a, 256, 2048)} == x
  142. x = { pow(i, 11, 324000) for i in range(1000)}
  143. assert [ is_nthpow_residue(a, 11, 324000) for a in x]
  144. x = { pow(i, 17, 22217575536) for i in range(1000)}
  145. assert [ is_nthpow_residue(a, 17, 22217575536) for a in x]
  146. assert is_nthpow_residue(676, 3, 5364)
  147. assert is_nthpow_residue(9, 12, 36)
  148. assert is_nthpow_residue(32, 10, 41)
  149. assert is_nthpow_residue(4, 2, 64)
  150. assert is_nthpow_residue(31, 4, 41)
  151. assert not is_nthpow_residue(2, 2, 5)
  152. assert is_nthpow_residue(8547, 12, 10007)
  153. assert is_nthpow_residue(Dummy(even=True) + 3, 3, 2) == True
  154. assert nthroot_mod(Dummy(odd=True), 3, 2) == 1
  155. assert nthroot_mod(29, 31, 74) == [45]
  156. assert nthroot_mod(1801, 11, 2663) == 44
  157. for a, q, p in [(51922, 2, 203017), (43, 3, 109), (1801, 11, 2663),
  158. (26118163, 1303, 33333347), (1499, 7, 2663), (595, 6, 2663),
  159. (1714, 12, 2663), (28477, 9, 33343)]:
  160. r = nthroot_mod(a, q, p)
  161. assert pow(r, q, p) == a
  162. assert nthroot_mod(11, 3, 109) is None
  163. assert nthroot_mod(16, 5, 36, True) == [4, 22]
  164. assert nthroot_mod(9, 16, 36, True) == [3, 9, 15, 21, 27, 33]
  165. assert nthroot_mod(4, 3, 3249000) == []
  166. assert nthroot_mod(36010, 8, 87382, True) == [40208, 47174]
  167. assert nthroot_mod(0, 12, 37, True) == [0]
  168. assert nthroot_mod(0, 7, 100, True) == [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
  169. assert nthroot_mod(4, 4, 27, True) == [5, 22]
  170. assert nthroot_mod(4, 4, 121, True) == [19, 102]
  171. assert nthroot_mod(2, 3, 7, True) == []
  172. for p in range(5, 100):
  173. qv = range(3, p, 4)
  174. for q in qv:
  175. d = defaultdict(list)
  176. for i in range(p):
  177. d[pow(i, q, p)].append(i)
  178. for a in range(1, p - 1):
  179. res = nthroot_mod(a, q, p, True)
  180. if d[a]:
  181. assert d[a] == res
  182. else:
  183. assert res == []
  184. assert legendre_symbol(5, 11) == 1
  185. assert legendre_symbol(25, 41) == 1
  186. assert legendre_symbol(67, 101) == -1
  187. assert legendre_symbol(0, 13) == 0
  188. assert legendre_symbol(9, 3) == 0
  189. raises(ValueError, lambda: legendre_symbol(2, 4))
  190. assert jacobi_symbol(25, 41) == 1
  191. assert jacobi_symbol(-23, 83) == -1
  192. assert jacobi_symbol(3, 9) == 0
  193. assert jacobi_symbol(42, 97) == -1
  194. assert jacobi_symbol(3, 5) == -1
  195. assert jacobi_symbol(7, 9) == 1
  196. assert jacobi_symbol(0, 3) == 0
  197. assert jacobi_symbol(0, 1) == 1
  198. assert jacobi_symbol(2, 1) == 1
  199. assert jacobi_symbol(1, 3) == 1
  200. raises(ValueError, lambda: jacobi_symbol(3, 8))
  201. assert mobius(13*7) == 1
  202. assert mobius(1) == 1
  203. assert mobius(13*7*5) == -1
  204. assert mobius(13**2) == 0
  205. raises(ValueError, lambda: mobius(-3))
  206. p = Symbol('p', integer=True, positive=True, prime=True)
  207. x = Symbol('x', positive=True)
  208. i = Symbol('i', integer=True)
  209. assert mobius(p) == -1
  210. raises(TypeError, lambda: mobius(x))
  211. raises(ValueError, lambda: mobius(i))
  212. assert _discrete_log_trial_mul(587, 2**7, 2) == 7
  213. assert _discrete_log_trial_mul(941, 7**18, 7) == 18
  214. assert _discrete_log_trial_mul(389, 3**81, 3) == 81
  215. assert _discrete_log_trial_mul(191, 19**123, 19) == 123
  216. assert _discrete_log_shanks_steps(442879, 7**2, 7) == 2
  217. assert _discrete_log_shanks_steps(874323, 5**19, 5) == 19
  218. assert _discrete_log_shanks_steps(6876342, 7**71, 7) == 71
  219. assert _discrete_log_shanks_steps(2456747, 3**321, 3) == 321
  220. assert _discrete_log_pollard_rho(6013199, 2**6, 2, rseed=0) == 6
  221. assert _discrete_log_pollard_rho(6138719, 2**19, 2, rseed=0) == 19
  222. assert _discrete_log_pollard_rho(36721943, 2**40, 2, rseed=0) == 40
  223. assert _discrete_log_pollard_rho(24567899, 3**333, 3, rseed=0) == 333
  224. raises(ValueError, lambda: _discrete_log_pollard_rho(11, 7, 31, rseed=0))
  225. raises(ValueError, lambda: _discrete_log_pollard_rho(227, 3**7, 5, rseed=0))
  226. assert _discrete_log_pohlig_hellman(98376431, 11**9, 11) == 9
  227. assert _discrete_log_pohlig_hellman(78723213, 11**31, 11) == 31
  228. assert _discrete_log_pohlig_hellman(32942478, 11**98, 11) == 98
  229. assert _discrete_log_pohlig_hellman(14789363, 11**444, 11) == 444
  230. assert discrete_log(587, 2**9, 2) == 9
  231. assert discrete_log(2456747, 3**51, 3) == 51
  232. assert discrete_log(32942478, 11**127, 11) == 127
  233. assert discrete_log(432751500361, 7**324, 7) == 324
  234. args = 5779, 3528, 6215
  235. assert discrete_log(*args) == 687
  236. assert discrete_log(*Tuple(*args)) == 687
  237. assert quadratic_congruence(400, 85, 125, 1600) == [295, 615, 935, 1255, 1575]
  238. assert quadratic_congruence(3, 6, 5, 25) == [3, 20]
  239. assert quadratic_congruence(120, 80, 175, 500) == []
  240. assert quadratic_congruence(15, 14, 7, 2) == [1]
  241. assert quadratic_congruence(8, 15, 7, 29) == [10, 28]
  242. assert quadratic_congruence(160, 200, 300, 461) == [144, 431]
  243. assert quadratic_congruence(100000, 123456, 7415263, 48112959837082048697) == [30417843635344493501, 36001135160550533083]
  244. assert quadratic_congruence(65, 121, 72, 277) == [249, 252]
  245. assert quadratic_congruence(5, 10, 14, 2) == [0]
  246. assert quadratic_congruence(10, 17, 19, 2) == [1]
  247. assert quadratic_congruence(10, 14, 20, 2) == [0, 1]
  248. assert polynomial_congruence(6*x**5 + 10*x**4 + 5*x**3 + x**2 + x + 1,
  249. 972000) == [220999, 242999, 463999, 485999, 706999, 728999, 949999, 971999]
  250. assert polynomial_congruence(x**3 - 10*x**2 + 12*x - 82, 33075) == [30287]
  251. assert polynomial_congruence(x**2 + x + 47, 2401) == [785, 1615]
  252. assert polynomial_congruence(10*x**2 + 14*x + 20, 2) == [0, 1]
  253. assert polynomial_congruence(x**3 + 3, 16) == [5]
  254. assert polynomial_congruence(65*x**2 + 121*x + 72, 277) == [249, 252]
  255. assert polynomial_congruence(x**4 - 4, 27) == [5, 22]
  256. assert polynomial_congruence(35*x**3 - 6*x**2 - 567*x + 2308, 148225) == [86957,
  257. 111157, 122531, 146731]
  258. assert polynomial_congruence(x**16 - 9, 36) == [3, 9, 15, 21, 27, 33]
  259. assert polynomial_congruence(x**6 - 2*x**5 - 35, 6125) == [3257]
  260. raises(ValueError, lambda: polynomial_congruence(x**x, 6125))
  261. raises(ValueError, lambda: polynomial_congruence(x**i, 6125))
  262. raises(ValueError, lambda: polynomial_congruence(0.1*x**2 + 6, 100))