Exemplo n.º 1
0
def timeit_shoup_poly_F20_zassenhaus():
    gf_factor_sqf(F_20, P_18, method='zassenhaus')
Exemplo n.º 2
0
def timeit_shoup_poly_F20_shoup():
    gf_factor_sqf(F_20, P_18, method='shoup')
Exemplo n.º 3
0
def timeit_shoup_poly_F10_zassenhaus():
    gf_factor_sqf(F_10, P_08, method='zassenhaus')
Exemplo n.º 4
0
def timeit_shoup_poly_F10_shoup():
    gf_factor_sqf(F_10, P_08, method='shoup')
Exemplo n.º 5
0
def timeit_gathen_poly_f20_zassenhaus():
    gf_factor_sqf(f_20, p_20, method='zassenhaus')
Exemplo n.º 6
0
def timeit_gathen_poly_f20_shoup():
    gf_factor_sqf(f_20, p_20, method='shoup')
Exemplo n.º 7
0
def zzx_zassenhaus(f):
    """Factor square-free polynomials over Z[x]. """
    n = zzx_degree(f)

    if n == 1:
        return [f]

    A = zzx_max_norm(f)
    b = zzx_LC(f)
    B = abs(int(sqrt(n + 1) * 2**n * A * b))
    C = (n + 1)**(2 * n) * A**(2 * n - 1)
    gamma = int(ceil(2 * log(C, 2)))
    prime_max = int(2 * gamma * log(gamma))

    for p in xrange(3, prime_max + 1):
        if not isprime(p) or b % p == 0:
            continue

        F = gf_from_int_poly(f, p)

        if gf_sqf_p(F, p):
            break

    l = int(ceil(log(2 * B + 1, p)))

    modular = []

    for ff in gf_factor_sqf(F, p)[1]:
        modular.append(gf_to_int_poly(ff, p))

    g = zzx_hensel_lift(p, f, modular, l)

    T = set(range(len(g)))
    factors, s = [], 1

    while 2 * s <= len(T):
        for S in subsets(T, s):
            G, H = [b], [b]

            S = set(S)

            for i in S:
                G = zzx_mul(G, g[i])
            for i in T - S:
                H = zzx_mul(H, g[i])

            G = zzx_trunc(G, p**l)
            H = zzx_trunc(H, p**l)

            G_norm = zzx_l1_norm(G)
            H_norm = zzx_l1_norm(H)

            if G_norm * H_norm <= B:
                T = T - S

                G = zzx_primitive(G)[1]
                f = zzx_primitive(H)[1]

                factors.append(G)
                b = zzx_LC(f)

                break
        else:
            s += 1

    return factors + [f]
Exemplo n.º 8
0
def test_gf_factor():
    assert gf_factor([], 11) == (0, [])
    assert gf_factor([1], 11) == (1, [])
    assert gf_factor([1,1], 11) == (1, [([1, 1], 1)])

    f, p = [1,0,0,1,0], 2

    g = (1, [([1, 0], 1),
             ([1, 1], 1),
             ([1, 1, 1], 1)])

    assert gf_factor(f, p, method='zassenhaus') == g
    assert gf_factor(f, p, method='shoup') == g

    g = (1, [[1, 0],
             [1, 1],
             [1, 1, 1]])

    assert gf_factor_sqf(f, p, method='zassenhaus') == g
    assert gf_factor_sqf(f, p, method='shoup') == g

    assert gf_factor([1, 5, 8, 4], 11) == \
        (1, [([1, 1], 1), ([1, 2], 2)])

    assert gf_factor([1, 1, 10, 1, 0, 10, 10, 10, 0, 0], 11) == \
        (1, [([1, 0], 2), ([1, 9, 5], 1), ([1, 3, 0, 8, 5, 2], 1)])

    assert gf_factor(gf_from_dict({32: 1, 0: 1}, 11), 11) == \
        (1, [([1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 10], 1),
             ([1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 10], 1)])

    assert gf_factor(gf_from_dict({32: 8, 0: 5}, 11), 11) == \
        (8, [([1, 3], 1),
             ([1, 8], 1),
             ([1, 0, 9], 1),
             ([1, 2, 2], 1),
             ([1, 9, 2], 1),
             ([1, 0, 5, 0, 7], 1),
             ([1, 0, 6, 0, 7], 1),
             ([1, 0, 0, 0, 1, 0, 0, 0, 6], 1),
             ([1, 0, 0, 0, 10, 0, 0, 0, 6], 1)])

    assert gf_factor(gf_from_dict({63: 8, 0: 5}, 11), 11) == \
        (8, [([1, 7], 1),
             ([1, 4, 5], 1),
             ([1, 6, 8, 2], 1),
             ([1, 9, 9, 2], 1),
             ([1, 0, 0, 9, 0, 0, 4], 1),
             ([1, 2, 0, 8, 4, 6, 4], 1),
             ([1, 2, 3, 8, 0, 6, 4], 1),
             ([1, 2, 6, 0, 8, 4, 4], 1),
             ([1, 3, 3, 1, 6, 8, 4], 1),
             ([1, 5, 6, 0, 8, 6, 4], 1),
             ([1, 6, 2, 7, 9, 8, 4], 1),
             ([1, 10, 4, 7, 10, 7, 4], 1),
             ([1, 10, 10, 1, 4, 9, 4], 1)])

    # Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi)

    p = nextprime(int((2**15 * pi).evalf()))
    f = gf_from_dict({15: 1, 1: 1, 0: 1}, p)

    assert gf_sqf_p(f, p) == True

    g = (1, [([1, 22730, 68144], 1),
             ([1, 81553, 77449, 86810, 4724], 1),
             ([1, 86276, 56779, 14859, 31575], 1),
             ([1, 15347, 95022, 84569, 94508, 92335], 1)])

    assert gf_factor(f, p, method='zassenhaus') == g
    assert gf_factor(f, p, method='shoup') == g

    g = (1, [[1, 22730, 68144],
             [1, 81553, 77449, 86810, 4724],
             [1, 86276, 56779, 14859, 31575],
             [1, 15347, 95022, 84569, 94508, 92335]])

    assert gf_factor_sqf(f, p, method='zassenhaus') == g
    assert gf_factor_sqf(f, p, method='shoup') == g

    # Shoup polynomials: f = a_0 x**n + a_1 x**(n-1) + ... + a_n
    # (mod p > 2**(n-2) * pi), where a_n = a_{n-1}**2 + 1, a_0 = 1

    p = nextprime(int((2**4 * pi).evalf()))
    f = [1, 2, 5, 26, 41, 39, 38]  # deg(f) = 6

    assert gf_sqf_p(f, p) == True

    g = (1, [([1, 44, 26], 1),
             ([1, 11, 25, 18, 30], 1)])

    assert gf_factor(f, p, method='zassenhaus') == g
    assert gf_factor(f, p, method='shoup') == g

    g = (1, [[1, 44, 26],
             [1, 11, 25, 18, 30]])

    assert gf_factor_sqf(f, p, method='zassenhaus') == g
    assert gf_factor_sqf(f, p, method='shoup') == g
Exemplo n.º 9
0
def timeit_shoup_poly_F20_zassenhaus():
    gf_factor_sqf(F_20, P_18, method='zassenhaus')
Exemplo n.º 10
0
def timeit_shoup_poly_F20_shoup():
    gf_factor_sqf(F_20, P_18, method='shoup')
Exemplo n.º 11
0
def timeit_shoup_poly_F10_shoup():
    gf_factor_sqf(F_10, P_08, method='shoup')
Exemplo n.º 12
0
def timeit_shoup_poly_F10_zassenhaus():
    gf_factor_sqf(F_10, P_08, method='zassenhaus')
Exemplo n.º 13
0
def timeit_gathen_poly_f20_shoup():
    gf_factor_sqf(f_20, p_20, method='shoup')
Exemplo n.º 14
0
def timeit_gathen_poly_f20_zassenhaus():
    gf_factor_sqf(f_20, p_20, method='zassenhaus')
Exemplo n.º 15
0
def zzx_zassenhaus(f):
    """Factor square-free polynomials over Z[x]. """
    n = zzx_degree(f)

    if n == 1:
        return [f]

    A = zzx_max_norm(f)
    b = zzx_LC(f)
    B = abs(int(sqrt(n+1)*2**n*A*b))
    C = (n+1)**(2*n)*A**(2*n-1)
    gamma = int(ceil(2*log(C, 2)))
    prime_max = int(2*gamma*log(gamma))

    for p in xrange(3, prime_max+1):
        if not isprime(p) or b % p == 0:
            continue

        F = gf_from_int_poly(f, p)

        if gf_sqf_p(F, p):
            break

    l = int(ceil(log(2*B + 1, p)))

    modular = []

    for ff in gf_factor_sqf(F, p)[1]:
        modular.append(gf_to_int_poly(ff, p))

    g = zzx_hensel_lift(p, f, modular, l)

    T = set(range(len(g)))
    factors, s = [], 1

    while 2*s <= len(T):
        for S in subsets(T, s):
            G, H = [b], [b]

            S = set(S)

            for i in S:
                G = zzx_mul(G, g[i])
            for i in T-S:
                H = zzx_mul(H, g[i])

            G = zzx_trunc(G, p**l)
            H = zzx_trunc(H, p**l)

            G_norm = zzx_l1_norm(G)
            H_norm = zzx_l1_norm(H)

            if G_norm*H_norm <= B:
                T = T - S

                G = zzx_primitive(G)[1]
                f = zzx_primitive(H)[1]

                factors.append(G)
                b = zzx_LC(f)

                break
        else:
            s += 1

    return factors + [f]