Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

97 rader
2.3 KiB

  1. # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # https://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Functions for parallel computation on multiple cores.
  15. Introduced in Python-RSA 3.1.
  16. .. note::
  17. Requires Python 2.6 or newer.
  18. """
  19. import multiprocessing as mp
  20. from multiprocessing.connection import Connection
  21. import rsa.prime
  22. import rsa.randnum
  23. def _find_prime(nbits: int, pipe: Connection) -> None:
  24. while True:
  25. integer = rsa.randnum.read_random_odd_int(nbits)
  26. # Test for primeness
  27. if rsa.prime.is_prime(integer):
  28. pipe.send(integer)
  29. return
  30. def getprime(nbits: int, poolsize: int) -> int:
  31. """Returns a prime number that can be stored in 'nbits' bits.
  32. Works in multiple threads at the same time.
  33. >>> p = getprime(128, 3)
  34. >>> rsa.prime.is_prime(p-1)
  35. False
  36. >>> rsa.prime.is_prime(p)
  37. True
  38. >>> rsa.prime.is_prime(p+1)
  39. False
  40. >>> from rsa import common
  41. >>> common.bit_size(p) == 128
  42. True
  43. """
  44. (pipe_recv, pipe_send) = mp.Pipe(duplex=False)
  45. # Create processes
  46. try:
  47. procs = [mp.Process(target=_find_prime, args=(nbits, pipe_send)) for _ in range(poolsize)]
  48. # Start processes
  49. for p in procs:
  50. p.start()
  51. result = pipe_recv.recv()
  52. finally:
  53. pipe_recv.close()
  54. pipe_send.close()
  55. # Terminate processes
  56. for p in procs:
  57. p.terminate()
  58. return result
  59. __all__ = ["getprime"]
  60. if __name__ == "__main__":
  61. print("Running doctests 1000x or until failure")
  62. import doctest
  63. for count in range(100):
  64. (failures, tests) = doctest.testmod()
  65. if failures:
  66. break
  67. if count % 10 == 0 and count:
  68. print("%i times" % count)
  69. print("Doctests done")