Python Crypto.Util.number.isPrime() Examples
The following are 9
code examples of Crypto.Util.number.isPrime().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
Crypto.Util.number
, or try the search function
.
Example #1
Source File: test_number.py From earthengine with MIT License | 6 votes |
def test_isPrime(self): """Util.number.isPrime""" self.assertEqual(number.isPrime(-3), False) # Regression test: negative numbers should not be prime self.assertEqual(number.isPrime(-2), False) # Regression test: negative numbers should not be prime self.assertEqual(number.isPrime(1), False) # Regression test: isPrime(1) caused some versions of PyCrypto to crash. self.assertEqual(number.isPrime(2), True) self.assertEqual(number.isPrime(3), True) self.assertEqual(number.isPrime(4), False) self.assertEqual(number.isPrime(2L**1279-1), True) self.assertEqual(number.isPrime(-(2L**1279-1)), False) # Regression test: negative numbers should not be prime # test some known gmp pseudo-primes taken from # http://www.trnicely.net/misc/mpzspsp.html for composite in (43 * 127 * 211, 61 * 151 * 211, 15259 * 30517, 346141L * 692281L, 1007119L * 2014237L, 3589477L * 7178953L, 4859419L * 9718837L, 2730439L * 5460877L, 245127919L * 490255837L, 963939391L * 1927878781L, 4186358431L * 8372716861L, 1576820467L * 3153640933L): self.assertEqual(number.isPrime(long(composite)), False)
Example #2
Source File: test_number.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_isPrime(self): """Util.number.isPrime""" self.assertEqual(number.isPrime(-3), False) # Regression test: negative numbers should not be prime self.assertEqual(number.isPrime(-2), False) # Regression test: negative numbers should not be prime self.assertEqual(number.isPrime(1), False) # Regression test: isPrime(1) caused some versions of PyCrypto to crash. self.assertEqual(number.isPrime(2), True) self.assertEqual(number.isPrime(3), True) self.assertEqual(number.isPrime(4), False) self.assertEqual(number.isPrime(2L**1279-1), True) self.assertEqual(number.isPrime(-(2L**1279-1)), False) # Regression test: negative numbers should not be prime # test some known gmp pseudo-primes taken from # http://www.trnicely.net/misc/mpzspsp.html for composite in (43 * 127 * 211, 61 * 151 * 211, 15259 * 30517, 346141L * 692281L, 1007119L * 2014237L, 3589477L * 7178953L, 4859419L * 9718837L, 2730439L * 5460877L, 245127919L * 490255837L, 963939391L * 1927878781L, 4186358431L * 8372716861L, 1576820467L * 3153640933L): self.assertEqual(number.isPrime(long(composite)), False)
Example #3
Source File: primes.py From imoocc with GNU General Public License v2.0 | 6 votes |
def _generate_prime(bits, rng): "primtive attempt at prime generation" hbyte_mask = pow(2, bits % 8) - 1 while True: # loop catches the case where we increment n into a higher bit-range x = rng.read((bits+7) // 8) if hbyte_mask > 0: x = chr(ord(x[0]) & hbyte_mask) + x[1:] n = util.inflate_long(x, 1) n |= 1 n |= (1 << (bits - 1)) while not number.isPrime(n): n += 2 if util.bit_length(n) == bits: break return n
Example #4
Source File: primes.py From imoocc with GNU General Public License v2.0 | 6 votes |
def _generate_prime(bits, rng): "primtive attempt at prime generation" hbyte_mask = pow(2, bits % 8) - 1 while True: # loop catches the case where we increment n into a higher bit-range x = rng.read((bits+7) // 8) if hbyte_mask > 0: x = chr(ord(x[0]) & hbyte_mask) + x[1:] n = util.inflate_long(x, 1) n |= 1 n |= (1 << (bits - 1)) while not number.isPrime(n): n += 2 if util.bit_length(n) == bits: break return n
Example #5
Source File: test_number.py From FODI with GNU General Public License v3.0 | 6 votes |
def test_isPrime(self): """Util.number.isPrime""" self.assertEqual(number.isPrime(-3), False) # Regression test: negative numbers should not be prime self.assertEqual(number.isPrime(-2), False) # Regression test: negative numbers should not be prime self.assertEqual(number.isPrime(1), False) # Regression test: isPrime(1) caused some versions of PyCrypto to crash. self.assertEqual(number.isPrime(2), True) self.assertEqual(number.isPrime(3), True) self.assertEqual(number.isPrime(4), False) self.assertEqual(number.isPrime(2**1279-1), True) self.assertEqual(number.isPrime(-(2**1279-1)), False) # Regression test: negative numbers should not be prime # test some known gmp pseudo-primes taken from # http://www.trnicely.net/misc/mpzspsp.html for composite in (43 * 127 * 211, 61 * 151 * 211, 15259 * 30517, 346141 * 692281, 1007119 * 2014237, 3589477 * 7178953, 4859419 * 9718837, 2730439 * 5460877, 245127919 * 490255837, 963939391 * 1927878781, 4186358431 * 8372716861, 1576820467 * 3153640933): self.assertEqual(number.isPrime(int(composite)), False)
Example #6
Source File: test_number.py From android_universal with MIT License | 6 votes |
def test_isPrime(self): """Util.number.isPrime""" self.assertEqual(number.isPrime(-3), False) # Regression test: negative numbers should not be prime self.assertEqual(number.isPrime(-2), False) # Regression test: negative numbers should not be prime self.assertEqual(number.isPrime(1), False) # Regression test: isPrime(1) caused some versions of PyCrypto to crash. self.assertEqual(number.isPrime(2), True) self.assertEqual(number.isPrime(3), True) self.assertEqual(number.isPrime(4), False) self.assertEqual(number.isPrime(2**1279-1), True) self.assertEqual(number.isPrime(-(2**1279-1)), False) # Regression test: negative numbers should not be prime # test some known gmp pseudo-primes taken from # http://www.trnicely.net/misc/mpzspsp.html for composite in (43 * 127 * 211, 61 * 151 * 211, 15259 * 30517, 346141 * 692281, 1007119 * 2014237, 3589477 * 7178953, 4859419 * 9718837, 2730439 * 5460877, 245127919 * 490255837, 963939391 * 1927878781, 4186358431 * 8372716861, 1576820467 * 3153640933): self.assertEqual(number.isPrime(int(composite)), False)
Example #7
Source File: task.py From starctf2018 with MIT License | 5 votes |
def do_factor(num): try: res = run(cmd, stdout=PIPE, input=('factor(%d)'%num).encode(), timeout=timelimit) except TimeoutExpired: return False tmp = res.stdout.decode() tmp = tmp[tmp.find('***factors found***\n\n')+21:].split('\n') for line in tmp: pos = line.find(' = ') if pos == -1: break factor = int(line[pos+3:]) if line[0] == 'P' and not isPrime(factor): return True return False
Example #8
Source File: dsks.py From fbctf-2019-challenges with MIT License | 4 votes |
def generate_smooth_prime(bit_size, primitive_roots=[], smooth_bit_size=50, exclude=[]): """Generate smooth prime n Args: bit_size(int): size of generated prime in bits primitive_roots(list(int)): list of numbers that will be primitive roots modulo n smooth_bit_size(int): most factors of n-1 will be of this bit size exclude(list(int)): n-1 won't have any factor from that list Returns: int: n """ while True: n = 2 factors = {2:1} # get random primes of correct size print('smooth prime - loop of size about {}'.format((bit_size - 2*smooth_bit_size)//smooth_bit_size)) while n.bit_length() < bit_size - 2*smooth_bit_size: q = getPrime(smooth_bit_size) if q in exclude: continue n *= q if q in factors: factors[q] += 1 else: factors[q] = 1 # find last prime so that n+1 is prime and the size is correct smooth_bit_size_padded = bit_size - n.bit_length() print('smooth prime - smooth_bit_size_padded = {}'.format(smooth_bit_size_padded)) while True: q = getPrime(smooth_bit_size_padded) if q in exclude: continue if isPrime((n*q)+1): n = (n*q)+1 if q in factors: factors[q] += 1 else: factors[q] = 1 break # check if given numbers are primitive roots print('smooth prime - checking primitive roots') are_primitive_roots = True if len(primitive_roots) > 0: for factor, factor_power in factors.items(): for primitive_root in primitive_roots: if pow(primitive_root, (n-1)//(factor**factor_power), n) == 1: are_primitive_roots = False break if are_primitive_roots: print('smooth prime - done') return n, factors else: print('primitive roots criterion not met')
Example #9
Source File: Math.py From CryptoAttacks with MIT License | 4 votes |
def generate_smooth_prime(bit_size, primitive_roots=[], smooth_bit_size=50, exclude=[]): """Generate smooth prime n Args: bit_size(int): size of generated prime in bits primitive_roots(list(int)): list of numbers that will be primitive roots modulo n smooth_bit_size(int): most factors of n-1 will be of this bit size exclude(list(int)): n-1 won't have any factor from that list Returns: int: n """ while True: n = 2 factors = {2:1} # get random primes of correct size log.debug('smooth prime - loop of size about {}'.format((bit_size - 2*smooth_bit_size)//smooth_bit_size)) while n.bit_length() < bit_size - 2*smooth_bit_size: q = getPrime(smooth_bit_size) if q in exclude: continue n *= q if q in factors: factors[q] += 1 else: factors[q] = 1 # find last prime so that n+1 is prime and the size is correct smooth_bit_size_padded = bit_size - n.bit_length() log.debug('smooth prime - smooth_bit_size_padded = {}'.format(smooth_bit_size_padded)) while True: q = getPrime(smooth_bit_size_padded) if q in exclude: continue if isPrime((n*q)+1): n = (n*q)+1 if q in factors: factors[q] += 1 else: factors[q] = 1 break # check if given numbers are primitive roots log.debug('smooth prime - checking primitive roots') are_primitive_roots = True if len(primitive_roots) > 0: for factor, factor_power in factors.items(): for primitive_root in primitive_roots: if pow(primitive_root, (n-1)//(factor**factor_power), n) == 1: are_primitive_roots = False break if are_primitive_roots: log.debug('smooth prime - done') return n, factors else: log.debug('primitive roots criterion not met')