def test_jacobi_properties(self): p = random_prime(5) q = random_prime(5) n = p * q a = random.randint(p // 2, p - 1) b = a + n self.assertEqual(jacobi(a, n), jacobi(b, n))
def __init__(self, bit_size): """ Given the bit size of the RSA key, construct a RsaKeyGenerator, which will generated a key at least as big as the specified key size. """ self.p = random_prime(bit_size) self.q = random_prime(bit_size) self.n = self.p * self.q self.e = 0 self.d = 0 self.limit = (self.p - 1) * (self.q - 1) self.public_key = self.gen_public_key() self.private_key = self.gen_private_key()
def test_mod_inverse(self): self.assertEqual(mod_inverse(3, 11), 4) self.assertEqual(mod_inverse(10, 17), 12) p = random_prime(3) a = random.randint(1, p - 1) a_inv = mod_inverse(a, p) self.assertEqual(a * a_inv % p, 1)
def test_legendre_properties(self): p = 7 a = 4 # 11 = 4 (mod 7) b = 11 self.assertEqual(legendre(a, p), legendre(b, p)) p = random_prime(100) a = random.randint(p // 2, p - 1) # an easy was of asserting $a \equiv b \pmod{p}$ b = a + p self.assertEqual(legendre(a, p), legendre(b, p)) p = random_prime(100) a = random.randint(p // 2, p - 1) b = random.randint(p // 2, p - 1) self.assertEqual(legendre(a * b, p), legendre(a, p) * legendre(b, p))
def test_solovay_2(self): # This test is slow for large primes p = random_prime(16) self.assertTrue(is_prime(p, 'solovay-strassen')) ps = list(primes(20)) self.assertTrue(all(is_prime(p, 'solovay-strassen') for p in ps))
def test_miller_rabin_small_2(self): n = 64 prime = random_prime(n) self.assertTrue(is_prime(prime))
def test_miller_rabin_random_large(self): n = 64 actual = random_prime(n) self.assertEqual(actual.bit_length(), n) self.assertTrue(is_prime(actual))
#!/usr/bin/python3 from math import sqrt import sys sys.path.append('../../') from crypto.random import random_prime def quadratic(a, b, c): d = b**2 - 4 * a * c return (-b + sqrt(d)) / (2 * a), (-b - sqrt(d)) / (2 * a) p = random_prime(16) q = random_prime(16) n = p * q phi = (p - 1) * (q - 1) a = 1 b = -(n + 1 - phi) c = n print(p, q) print(quadratic(a, b, c))