Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(215)

Side by Side Diff: third_party/google-endpoints/rsa/parallel.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/google-endpoints/rsa/key.py ('k') | third_party/google-endpoints/rsa/pem.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # https://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """Functions for parallel computation on multiple cores.
18
19 Introduced in Python-RSA 3.1.
20
21 .. note::
22
23 Requires Python 2.6 or newer.
24
25 """
26
27 from __future__ import print_function
28
29 import multiprocessing as mp
30
31 import rsa.prime
32 import rsa.randnum
33
34
35 def _find_prime(nbits, pipe):
36 while True:
37 integer = rsa.randnum.read_random_odd_int(nbits)
38
39 # Test for primeness
40 if rsa.prime.is_prime(integer):
41 pipe.send(integer)
42 return
43
44
45 def getprime(nbits, poolsize):
46 """Returns a prime number that can be stored in 'nbits' bits.
47
48 Works in multiple threads at the same time.
49
50 >>> p = getprime(128, 3)
51 >>> rsa.prime.is_prime(p-1)
52 False
53 >>> rsa.prime.is_prime(p)
54 True
55 >>> rsa.prime.is_prime(p+1)
56 False
57
58 >>> from rsa import common
59 >>> common.bit_size(p) == 128
60 True
61
62 """
63
64 (pipe_recv, pipe_send) = mp.Pipe(duplex=False)
65
66 # Create processes
67 try:
68 procs = [mp.Process(target=_find_prime, args=(nbits, pipe_send))
69 for _ in range(poolsize)]
70 # Start processes
71 for p in procs:
72 p.start()
73
74 result = pipe_recv.recv()
75 finally:
76 pipe_recv.close()
77 pipe_send.close()
78
79 # Terminate processes
80 for p in procs:
81 p.terminate()
82
83 return result
84
85
86 __all__ = ['getprime']
87
88 if __name__ == '__main__':
89 print('Running doctests 1000x or until failure')
90 import doctest
91
92 for count in range(100):
93 (failures, tests) = doctest.testmod()
94 if failures:
95 break
96
97 if count and count % 10 == 0:
98 print('%i times' % count)
99
100 print('Doctests done')
OLDNEW
« no previous file with comments | « third_party/google-endpoints/rsa/key.py ('k') | third_party/google-endpoints/rsa/pem.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698