Beispiel #1
0
 def test_order_functional_1(self):
     print("\n\nRunning test for cryptanalysis module: order")
     generator = 9704
     modulus = 17389
     expected_order = 1242
     output_order = order(generator, modulus)
     self.assertEqual(output_order, expected_order)
Beispiel #2
0
def bsgs(
    g,
    h,
    p,
    smoothness=10000
):  # Solving x given g,h,p for the equation g^x (mod p) = h, so logg(h) (mod p) = x
    N = order(g, p, smoothness)
    n = int(sqrt(N)) + 1
    e = 1
    u = mod_inv(fast_power(g, n, p), p)
    uk = u
    gi = g
    hui = (h * uk) % p
    list_one = {gi: 1}
    list_two = {hui: 1}
    # Create the two list
    for i in range(2, n + 1):
        gi = (gi * g) % p
        uk = (uk * u) % p
        hui = (h * uk) % p
        list_one.update({gi: i})
        list_two.update({hui: i})
    # Now Check for a collision between the list
    for huk in list_two.keys():
        for gk in list_one.keys():
            if huk == gk:
                x = (list_one.get(gk) + (list_two.get(huk) * n)) % N
                xs = []
                for i in range(0, int(((p - 1) / N) + 1)):
                    xs.append(x + N * i)
                return xs
Beispiel #3
0
def pohlig_hellman(
    g, h, p
):  #Solving x given g,h,p for the equation g^x (mod p) = h, so logg(h) (mod p) = x
    n = p - 1
    congruences_and_mods = []
    prime_factors = naive_factor(n, 10000).get("prime_factors_dict")
    for factor in prime_factors.keys():
        modulo = factor**prime_factors.get(factor)
        gi = fast_power(g, n / modulo, p)
        hi = fast_power(h, n / modulo, p)
        ax = bsgs(
            gi, hi,
            p)[0]  # retuns a list of solutions, we just want the first one
        congruences_and_mods.append((ax, modulo))
    x = crt(congruences_and_mods)
    N = order(g, p, 10000)
    xs = []
    for i in range(1, int(((p - 1) / N) + 1)):
        xs.append(int((x + N * i) % n))
    return xs
Beispiel #4
0
 def test_order_functional_6(self):
     generator = 1
     modulus = 2
     expected_order = 1
     output_order = order(generator, modulus)
     self.assertEqual(output_order, expected_order)
Beispiel #5
0
 def test_order_functional_5(self):
     generator = 37
     modulus = 839
     expected_order = 419
     output_order = order(generator, modulus)
     self.assertEqual(output_order, expected_order)
Beispiel #6
0
 def test_order_functional_4(self):
     generator = 3
     modulus = 7
     expected_order = 6
     output_order = order(generator, modulus)
     self.assertEqual(output_order, expected_order)
Beispiel #7
0
 def test_order_functional_3(self):
     generator = 23378
     modulus = 2860486313
     expected_order = 2860486312
     output_order = order(generator, modulus)
     self.assertEqual(output_order, expected_order)
Beispiel #8
0
 def test_order_functional_2(self):
     generator = 2
     modulus = 3267000013
     expected_order = 1089000004
     output_order = order(generator, modulus)
     self.assertEqual(output_order, expected_order)