You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
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))
  48. for _ in range(poolsize)]
  49. # Start processes
  50. for p in procs:
  51. p.start()
  52. result = pipe_recv.recv()
  53. finally:
  54. pipe_recv.close()
  55. pipe_send.close()
  56. # Terminate processes
  57. for p in procs:
  58. p.terminate()
  59. return result
  60. __all__ = ['getprime']
  61. if __name__ == '__main__':
  62. print('Running doctests 1000x or until failure')
  63. import doctest
  64. for count in range(100):
  65. (failures, tests) = doctest.testmod()
  66. if failures:
  67. break
  68. if count % 10 == 0 and count:
  69. print('%i times' % count)
  70. print('Doctests done')