def generate_random_safe_prime(bits): """Unused at the moment. Generates a random prime number. :param bits: Number of bits. :return: Prime number long value. """ assert not bits < 10 # The 1.5 ensures the 2 MSBs are set # Thus, when used for p,q in RSA, n will have its MSB set # # Since 30 is lcm(2,3,5), we'll set our test numbers to # 29 % 30 and keep them there # low = (2 ** (bits-2)) * 3 // 2 # high = (2 ** (bits-1)) - 30 low = (1 << (bits - 2)) * 3 // 2 high = (1 << (bits - 1)) - 30 random_uint = random.generate_random_uint_between(low, high) random_uint += 29 - (random_uint % 30) while 1: random_uint += 30 if random_uint >= high: random_uint = random.generate_random_uint_between(low, high) random_uint += 29 - (random_uint % 30) # Ideas from Tom Wu's SRP code # Do trial division on p and q before Rabin-Miller if is_prime(random_uint, 0): possible_prime = (2 * random_uint) + 1 if is_prime(possible_prime): if is_prime(random_uint): return possible_prime
def generate_random_prime(bits): """Generates a random prime number. :param bits: Number of bits. :return: Prime number long value. """ assert not bits < 10 # The 1.5 ensures the 2 MSBs are set # Thus, when used for p,q in RSA, n will have its MSB set # # Since 30 is lcm(2,3,5), we'll set our test numbers to # 29 % 30 and keep them there # low = (2 ** (bits-1)) * 3 // 2 # high = 2 ** bits - 30 low = (1 << (bits - 1)) * 3 // 2 high = (1 << bits) - 30 random_uint = random.generate_random_uint_between(low, high) random_uint += 29 - (random_uint % 30) while 1: random_uint += 30 if random_uint >= high: random_uint = random.generate_random_uint_between(low, high) random_uint += 29 - (random_uint % 30) if is_prime(random_uint): return random_uint
def _pure_is_prime(num, iterations=5, _sieve=sieve): """ Determines whether a number is prime. :param num: Number :param iterations: Number of iterations. :returns: ``True`` if prime; ``False`` otherwise. """ #Trial division with sieve for x in _sieve: if x >= num: return True if not num % x: return False #Passed trial division, proceed to Rabin-Miller #Rabin-Miller implemented per Ferguson & Schneier #Compute s, t for Rabin-Miller s, t = num-1, 0 while not s % 2: s, t = s // 2, t+1 #Repeat Rabin-Miller x times a = 2 #Use 2 as a base for first iteration speedup, per HAC for count in range(iterations): v = _pure_pow_mod(a, s, num) if v == 1: continue i = 0 while v != num-1: if i == t-1: return False else: v, i = _pure_pow_mod(v, 2, num), i+1 a = generate_random_uint_between(2, num) return True
def _pure_is_prime(num, iterations=5, _sieve=SIEVE): """ Determines whether a number is prime. :param num: Number :param iterations: Number of iterations. :returns: ``True`` if prime; ``False`` otherwise. """ # Trial division with sieve for prime_number in _sieve: if prime_number >= num: return True if not num % prime_number: return False # Passed trial division, proceed to Rabin-Miller # Rabin-Miller implemented per Ferguson & Schneier # Compute s, t for Rabin-Miller num_s, num_t = num - 1, 0 while not num_s % 2: num_s, num_t = num_s // 2, num_t + 1 # Repeat Rabin-Miller x times base = 2 # Use 2 as a base for first iteration speedup, per HAC for _ in range(iterations): num_v = _pure_pow_mod(base, num_s, num) if num_v == 1: continue i = 0 while num_v != num - 1: if i == num_t - 1: return False else: num_v, i = _pure_pow_mod(num_v, 2, num), i + 1 base = generate_random_uint_between(2, num) return True
def _pure_is_prime(num, iterations=5, _sieve=prime_sieve.SIEVE): """Determines whether a number is prime. :param num: Number :param iterations: Number of iterations. :returns: ``True`` if prime; ``False`` otherwise. """ # Trial division with sieve for prime_number in _sieve: if prime_number >= num: return True if not num % prime_number: return False # Passed trial division, proceed to Rabin-Miller # Rabin-Miller implemented per Ferguson & Schneier # Compute s, t for Rabin-Miller num_s, num_t = num - 1, 0 while not num_s % 2: num_s, num_t = num_s // 2, num_t + 1 # Repeat Rabin-Miller x times base = 2 # Use 2 as a base for first iteration speedup, per HAC for _ in builtins.range(iterations): num_v = _pure_pow_mod(base, num_s, num) if num_v == 1: continue i = 0 while num_v != num - 1: if i == num_t - 1: return False else: num_v, i = _pure_pow_mod(num_v, 2, num), i + 1 base = random.generate_random_uint_between(2, num) return True
b = builtins.b BASE85_RAW = test_mom_codec_base85.RAW BASE85_ENCODED = test_mom_codec_base85.ENCODED # Generates a 1024-bit strength random byte string. RANDOM_BYTES_1024 = random.generate_random_bytes(1024 >> 3) # Generates a 2048-bit strength random byte string. RANDOM_BYTES_2048 = random.generate_random_bytes(2048 >> 3) # Generates a 4093 byte length random byte string. RANDOM_BYTES_4093 = random.generate_random_bytes(4093) ZERO_BYTES = b("\x00\x00\x00\x00") ONE_ZERO_BYTE = b("\x00") RANDOM_LONG_VALUE = random.generate_random_uint_between(0, 99999999999999999) ZERO_LONG = 0 NEGATIVE_LONG_VALUE = -1 LONG_VALUE = 71671831749689734735896910666236152091910950933161125188784836897624039426313152092699961904060141667369 EXPECTED_BLOCKSIZE_BYTES = b("""\ \x00\x01\xff\xff\xff\xff\xff\xff\xff\xff\x000 0\x0c\x06\x08*\x86H\x86\ \xf7\r\x02\x05\x05\x00\x04\x10\xd6\xc7\xde\x19\xf6}\xb3#\xbdhI\xafDL\x04)""") LONG_VALUE_BLOCKSIZE = 45 EXPECTED_BYTES = b("""\ \x01\xff\xff\xff\xff\xff\xff\xff\xff\x000 0\x0c\x06\x08*\x86H\x86\ \xf7\r\x02\x05\x05\x00\x04\x10\xd6\xc7\xde\x19\xf6}\xb3#\xbdhI\xafDL\x04)""") # Base64 encoding this sequence of bytes using the standard Base-64 alphabet # produces a "/", "+", and "=" in the encoded sequence.
def test_range(self): low, high = 1, 10 for _ in range(1000): value = random.generate_random_uint_between(low, high) self.assertTrue(value >= low and value < high)
def test_0_1(self): self.assertEqual(random.generate_random_uint_between(0, 1), 0)
base85_encode, base85_decode, base58_encode, base58_decode,\ base64_urlsafe_encode, base64_urlsafe_decode from mom.tests.test_mom_codec_base85 import raw as base85_raw,\ encoded as base85_encoded # Generates a 1024-bit strength random byte string. random_bytes_1024 = generate_random_bytes(1024 >> 3) # Generates a 2048-bit strength random byte string. random_bytes_2048 = generate_random_bytes(2048 >> 3) # Generates a 4093 byte length random byte string. random_bytes_len_4093 = generate_random_bytes(4093) zero_bytes = b('\x00\x00\x00\x00') one_zero_byte = b('\x00') random_long_value = generate_random_uint_between(0, 99999999999999999) zero_long = 0 negative_long_value = -1 long_value = 71671831749689734735896910666236152091910950933161125188784836897624039426313152092699961904060141667369 expected_blocksize_bytes = b('''\ \x00\x01\xff\xff\xff\xff\xff\xff\xff\xff\x000 0\x0c\x06\x08*\x86H\x86\ \xf7\r\x02\x05\x05\x00\x04\x10\xd6\xc7\xde\x19\xf6}\xb3#\xbdhI\xafDL\x04)''') long_value_blocksize = 45 expected_bytes = b('''\ \x01\xff\xff\xff\xff\xff\xff\xff\xff\x000 0\x0c\x06\x08*\x86H\x86\ \xf7\r\x02\x05\x05\x00\x04\x10\xd6\xc7\xde\x19\xf6}\xb3#\xbdhI\xafDL\x04)''') # Base64 encoding this sequence of bytes using the standard Base-64 alphabet # produces a "/", "+", and "=" in the encoded sequence.