Example #1
0
def test_dmp_zz_wang():
    R, x, y, z = ring("x,y,z", ZZ)
    UV, _x = ring("x", ZZ)

    p = ZZ(nextprime(R.dmp_zz_mignotte_bound(w_1)))
    assert p == 6291469

    t_1, k_1, e_1 = y, 1, ZZ(-14)
    t_2, k_2, e_2 = z, 2, ZZ(3)
    t_3, k_3, e_3 = y + z, 2, ZZ(-11)
    t_4, k_4, e_4 = y - z, 1, ZZ(-17)

    T = [t_1, t_2, t_3, t_4]
    K = [k_1, k_2, k_3, k_4]
    E = [e_1, e_2, e_3, e_4]

    T = zip([t.drop(x) for t in T], K)

    A = [ZZ(-14), ZZ(3)]

    S = R.dmp_eval_tail(w_1, A)
    cs, s = UV.dmp_ground_primitive(S)

    assert cs == 1 and s == S == \
        1036728*_x**6 + 915552*_x**5 + 55748*_x**4 + 105621*_x**3 - 17304*_x**2 - 26841*_x - 644

    assert R.dmp_zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17]
    assert s.is_squarefree and UV.dmp_degree_in(s, 0) == R.dmp_degree_in(
        w_1, 0)

    _, H = UV.dup_zz_factor_sqf(s)

    h_1 = 44 * _x**2 + 42 * _x + 1
    h_2 = 126 * _x**2 - 9 * _x + 28
    h_3 = 187 * _x**2 - 23

    assert H == [h_1, h_2, h_3]

    LC = [lc.drop(x) for lc in [-4 * y - 4 * z, -y * z**2, y**2 - z**2]]

    assert R.dmp_zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC)

    # H_1 = [44*x**2 + 42*x + 1, 126*x**2 - 9*x + 28, 187*x**2 - 23]
    # H_2 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9]
    # H_3 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9]

    # c_1 = -70686*x**5 - 5863*x**4 - 17826*x**3 + 2009*x**2 + 5031*x + 74
    # c_2 = 9*x**5*y**4 + 12*x**5*y**3 - 45*x**5*y**2 - 108*x**5*y - 324*x**5 + 18*x**4*y**3 - 216*x**4*y**2 - 810*x**4*y + 2*x**3*y**4 + 9*x**3*y**3 - 252*x**3*y**2 - 288*x**3*y - 945*x**3 - 30*x**2*y**2 - 414*x**2*y + 2*x*y**3 - 54*x*y**2 - 3*x*y + 81*x + 12*y
    # c_3 = -36*x**4*y**2 - 108*x**4*y - 27*x**3*y**2 - 36*x**3*y - 108*x**3 - 8*x**2*y**2 - 42*x**2*y - 6*x*y**2 + 9*x + 2*y

    # TODO
    # assert R.dmp_zz_diophantine(H_1, c_1, [], 5, p) == [-3*x, -2, 1]
    # assert R.dmp_zz_diophantine(H_2, c_2, [ZZ(-14)], 5, p) == [-x*y, -3*x, -6]
    # assert R.dmp_zz_diophantine(H_3, c_3, [ZZ(-14)], 5, p) == [0, 0, -1]

    factors = R.dmp_zz_wang_hensel_lifting(w_1, H, LC, A, p)
    assert R.dmp_expand(factors) == w_1
Example #2
0
def test_dmp_zz_wang():
    R,  x, y, z = ring("x,y,z", ZZ)
    UV, _x = ring("x", ZZ)

    p = ZZ(nextprime(R.dmp_zz_mignotte_bound(w_1)))
    assert p == 6291469

    t_1, k_1, e_1 = y, 1, ZZ(-14)
    t_2, k_2, e_2 = z, 2, ZZ(3)
    t_3, k_3, e_3 = y + z, 2, ZZ(-11)
    t_4, k_4, e_4 = y - z, 1, ZZ(-17)

    T = [t_1, t_2, t_3, t_4]
    K = [k_1, k_2, k_3, k_4]
    E = [e_1, e_2, e_3, e_4]

    T = zip([ t.drop(x) for t in T ], K)

    A = [ZZ(-14), ZZ(3)]

    S = R.dmp_eval_tail(w_1, A)
    cs, s = UV.dmp_ground_primitive(S)

    assert cs == 1 and s == S == \
        1036728*_x**6 + 915552*_x**5 + 55748*_x**4 + 105621*_x**3 - 17304*_x**2 - 26841*_x - 644

    assert R.dmp_zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17]
    assert s.is_squarefree and UV.dmp_degree_in(s, 0) == R.dmp_degree_in(w_1, 0)

    _, H = UV.dup_zz_factor_sqf(s)

    h_1 = 44*_x**2 + 42*_x + 1
    h_2 = 126*_x**2 - 9*_x + 28
    h_3 = 187*_x**2 - 23

    assert H == [h_1, h_2, h_3]

    LC = [ lc.drop(x) for lc in [-4*y - 4*z, -y*z**2, y**2 - z**2] ]

    assert R.dmp_zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC)

    # H_1 = [44*x**2 + 42*x + 1, 126*x**2 - 9*x + 28, 187*x**2 - 23]
    # H_2 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9]
    # H_3 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9]

    # c_1 = -70686*x**5 - 5863*x**4 - 17826*x**3 + 2009*x**2 + 5031*x + 74
    # c_2 = 9*x**5*y**4 + 12*x**5*y**3 - 45*x**5*y**2 - 108*x**5*y - 324*x**5 + 18*x**4*y**3 - 216*x**4*y**2 - 810*x**4*y + 2*x**3*y**4 + 9*x**3*y**3 - 252*x**3*y**2 - 288*x**3*y - 945*x**3 - 30*x**2*y**2 - 414*x**2*y + 2*x*y**3 - 54*x*y**2 - 3*x*y + 81*x + 12*y
    # c_3 = -36*x**4*y**2 - 108*x**4*y - 27*x**3*y**2 - 36*x**3*y - 108*x**3 - 8*x**2*y**2 - 42*x**2*y - 6*x*y**2 + 9*x + 2*y

    # TODO
    # assert R.dmp_zz_diophantine(H_1, c_1, [], 5, p) == [-3*x, -2, 1]
    # assert R.dmp_zz_diophantine(H_2, c_2, [ZZ(-14)], 5, p) == [-x*y, -3*x, -6]
    # assert R.dmp_zz_diophantine(H_3, c_3, [ZZ(-14)], 5, p) == [0, 0, -1]

    factors = R.dmp_zz_wang_hensel_lifting(w_1, H, LC, A, p)
    assert R.dmp_expand(factors) == w_1
Example #3
0
def test_gf_ddf():
    f = gf_from_dict({15: ZZ(1), 0: ZZ(-1)}, 11, ZZ)
    g = [([1, 0, 0, 0, 0, 10], 1), ([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 2)]

    assert gf_ddf_zassenhaus(f, 11, ZZ) == g
    assert gf_ddf_shoup(f, 11, ZZ) == g

    f = gf_from_dict({63: ZZ(1), 0: ZZ(1)}, 2, ZZ)
    g = [([1, 1], 1), ([1, 1, 1], 2), ([1, 1, 1, 1, 1, 1, 1], 3),
         ([
             1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
             1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
             0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1
         ], 6)]

    assert gf_ddf_zassenhaus(f, 2, ZZ) == g
    assert gf_ddf_shoup(f, 2, ZZ) == g

    f = gf_from_dict({
        6: ZZ(1),
        5: ZZ(-1),
        4: ZZ(1),
        3: ZZ(1),
        1: ZZ(-1)
    }, 3, ZZ)
    g = [([1, 1, 0], 1), ([1, 1, 0, 1, 2], 2)]

    assert gf_ddf_zassenhaus(f, 3, ZZ) == g
    assert gf_ddf_shoup(f, 3, ZZ) == g

    f = ZZ.map([1, 2, 5, 26, 677, 436, 791, 325, 456, 24, 577])
    g = [([1, 701], 1), ([1, 110, 559, 532, 694, 151, 110, 70, 735, 122], 9)]

    assert gf_ddf_zassenhaus(f, 809, ZZ) == g
    assert gf_ddf_shoup(f, 809, ZZ) == g

    p = ZZ(nextprime(int((2**15 * pi).evalf())))
    f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)
    g = [([1, 22730, 68144], 2),
         ([1, 64876, 83977, 10787, 12561, 68608, 52650, 88001, 84356], 4),
         ([1, 15347, 95022, 84569, 94508, 92335], 5)]

    assert gf_ddf_zassenhaus(f, p, ZZ) == g
    assert gf_ddf_shoup(f, p, ZZ) == g
Example #4
0
def test_dmp_zz_wang():
    R,  x, y, z = ring('x,y,z', ZZ)
    UV, _x = ring('x', ZZ)

    p = ZZ(nextprime(R.dmp_zz_mignotte_bound(w_1)))

    assert p == 6291469

    t_1, k_1, e_1 = y, 1, ZZ(-14)
    t_2, k_2, e_2 = z, 2, ZZ(3)
    t_3, k_3, e_3 = y + z, 2, ZZ(-11)
    t_4, k_4, e_4 = y - z, 1, ZZ(-17)

    T = [t_1, t_2, t_3, t_4]
    K = [k_1, k_2, k_3, k_4]
    E = [e_1, e_2, e_3, e_4]

    T = zip([t.drop(x) for t in T], K)

    A = [ZZ(-14), ZZ(3)]

    S = R.dmp_eval_tail(w_1, A)
    cs, s = S.primitive()

    assert cs == 1 and s == S == (1036728*_x**6 + 915552*_x**5 + 55748*_x**4 +
                                  105621*_x**3 - 17304*_x**2 - 26841*_x - 644)

    assert R.dmp_zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17]
    assert s.is_squarefree and UV.dmp_degree_in(s, 0) == R.dmp_degree_in(w_1, 0)

    _, H = UV.dup_zz_factor_sqf(s)

    h_1 = 44*_x**2 + 42*_x + 1
    h_2 = 126*_x**2 - 9*_x + 28
    h_3 = 187*_x**2 - 23

    LC = [lc.drop(x) for lc in [-4*y - 4*z, -y*z**2, y**2 - z**2]]
    factors = R.dmp_zz_wang_hensel_lifting(w_1, H, LC, A, p)

    assert H == [h_1, h_2, h_3]
    assert R.dmp_zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC)
    assert R.dmp_expand(factors) == w_1
Example #5
0
def test_gf_ddf():
    f = gf_from_dict({15: ZZ(1), 0: ZZ(-1)}, 11, ZZ)
    g = [([1, 0, 0, 0, 0, 10], 1),
         ([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 2)]

    assert gf_ddf_zassenhaus(f, 11, ZZ) == g
    assert gf_ddf_shoup(f, 11, ZZ) == g

    f = gf_from_dict({63: ZZ(1), 0: ZZ(1)}, 2, ZZ)
    g = [([1, 1], 1),
         ([1, 1, 1], 2),
         ([1, 1, 1, 1, 1, 1, 1], 3),
         ([1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0,
           0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0,
           0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1], 6)]

    assert gf_ddf_zassenhaus(f, 2, ZZ) == g
    assert gf_ddf_shoup(f, 2, ZZ) == g

    f = gf_from_dict({6: ZZ(1), 5: ZZ(-1), 4: ZZ(1), 3: ZZ(1), 1: ZZ(-1)}, 3, ZZ)
    g = [([1, 1, 0], 1),
         ([1, 1, 0, 1, 2], 2)]

    assert gf_ddf_zassenhaus(f, 3, ZZ) == g
    assert gf_ddf_shoup(f, 3, ZZ) == g

    f = [1, 2, 5, 26, 677, 436, 791, 325, 456, 24, 577]
    g = [([1, 701], 1),
         ([1, 110, 559, 532, 694, 151, 110, 70, 735, 122], 9)]

    assert gf_ddf_zassenhaus(f, 809, ZZ) == g
    assert gf_ddf_shoup(f, 809, ZZ) == g

    p = ZZ(nextprime(int((2**15 * pi).evalf())))
    f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)
    g = [([1, 22730, 68144], 2),
         ([1, 64876, 83977, 10787, 12561, 68608, 52650, 88001, 84356], 4),
         ([1, 15347, 95022, 84569, 94508, 92335], 5)]

    assert gf_ddf_zassenhaus(f, p, ZZ) == g
    assert gf_ddf_shoup(f, p, ZZ) == g
Example #6
0
def test_gf_factor():
    R, x = ring('x', FF(2))

    f = x**4 + x
    g = (1, [(x, 1), (x + 1, 1), (x**2 + x + 1, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**18 + x**17 + x**16 + x**14 + x**12 + x**11 + x**8 + x**5 + x**3 + 1
    g = (1, [(x + 1, 4), (x**4 + x**3 + 1, 1),
             (x**10 + x**8 + x**7 + x**5 + 1, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**63 + 1
    g = (1, [(x + 1, 1), (x**2 + x + 1, 1),
             (x**3 + x + 1, 1), (x**6 + x + 1, 1), (x**3 + x**2 + 1, 1),
             (x**6 + x**3 + 1, 1), (x**6 + x**5 + 1, 1),
             (x**6 + x**4 + x**2 + x + 1, 1), (x**6 + x**5 + x**2 + x + 1, 1),
             (x**6 + x**4 + x**3 + x + 1, 1), (x**6 + x**5 + x**4 + x + 1, 1),
             (x**6 + x**5 + x**3 + x**2 + 1, 1),
             (x**6 + x**5 + x**4 + x**2 + 1, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = (x**28 + x**27 + x**26 + x**25 + x**24 + x**20 + x**19 + x**17 +
         x**16 + x**15 + x**14 + x**13 + x**12 + x**11 + x**9 + x**8 + x**5 +
         x**4 + x**2 + x)
    g = (1, [(x, 1), (x + 1, 2), (x**5 + x**4 + x**3 + x + 1, 1),
             (x**10 + x**9 + x**8 + x**7 + 1, 1),
             (x**10 + x**9 + x**8 + x**5 + x**4 + x**2 + 1, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    R, x = ring('x', FF(3))

    f = x**6 - x**5 + x**4 + x**3 - x
    g = (1, [(x, 1), (x + 1, 1), (x**2 + 1, 1), (x**2 + x + 2, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**4 + x**3 + x + 2
    g = (1, [(x**2 + 1, 1), (x**2 + x + 2, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    R, x = ring('x', FF(11))

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert R(0).factor_list() == (0, [])
            assert R(1).factor_list() == (1, [])
            assert x.factor_list() == (1, [(x, 1)])
            assert (x + 1).factor_list() == (1, [(x + 1, 1)])
            assert (2 * x + 3).factor_list() == (2, [(x + 7, 1)])

    assert (5 * x**3 + 2 * x**2 + 7 * x + 2).factor_list() == (5, [(x + 2, 1),
                                                                   (x + 8, 2)])

    f = x**6 + 8 * x**5 + x**4 + 8 * x**3 + 10 * x**2 + 8 * x + 1
    g = (1, [(x + 1, 1), (x**2 + 5 * x + 3, 1),
             (x**3 + 2 * x**2 + 3 * x + 4, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**3 + 5 * x**2 + 8 * x + 4
    g = (1, [(x + 1, 1), (x + 2, 2)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**9 + x**8 + 10 * x**7 + x**6 + 10 * x**4 + 10 * x**3 + 10 * x**2
    g = (1, [(x, 2), (x**2 + 9 * x + 5, 1),
             (x**5 + 3 * x**4 + 8 * x**2 + 5 * x + 2, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**32 + 1
    g = (1, [(x**16 + 3 * x**8 + 10, 1), (x**16 + 8 * x**8 + 10, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = 8 * x**32 + 5
    g = (8, [(x + 3, 1), (x + 8, 1), (x**2 + 9, 1), (x**2 + 2 * x + 2, 1),
             (x**2 + 9 * x + 2, 1), (x**8 + x**4 + 6, 1),
             (x**8 + 10 * x**4 + 6, 1), (x**4 + 5 * x**2 + 7, 1),
             (x**4 + 6 * x**2 + 7, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = 8 * x**63 + 5
    g = (8,
         [(x + 7, 1), (x**6 + 9 * x**3 + 4, 1), (x**2 + 4 * x + 5, 1),
          (x**3 + 6 * x**2 + 8 * x + 2, 1), (x**3 + 9 * x**2 + 9 * x + 2, 1),
          (x**6 + 2 * x**5 + 6 * x**4 + 8 * x**2 + 4 * x + 4, 1),
          (x**6 + 2 * x**5 + 8 * x**3 + 4 * x**2 + 6 * x + 4, 1),
          (x**6 + 5 * x**5 + 6 * x**4 + 8 * x**2 + 6 * x + 4, 1),
          (x**6 + 2 * x**5 + 3 * x**4 + 8 * x**3 + 6 * x + 4, 1),
          (x**6 + 10 * x**5 + 4 * x**4 + 7 * x**3 + 10 * x**2 + 7 * x + 4, 1),
          (x**6 + 3 * x**5 + 3 * x**4 + x**3 + 6 * x**2 + 8 * x + 4, 1),
          (x**6 + 6 * x**5 + 2 * x**4 + 7 * x**3 + 9 * x**2 + 8 * x + 4, 1),
          (x**6 + 10 * x**5 + 10 * x**4 + x**3 + 4 * x**2 + 9 * x + 4, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**15 - 1
    g = (1, [(x + 2, 1), (x + 6, 1), (x + 7, 1), (x + 8, 1), (x + 10, 1),
             (x**2 + x + 1, 1), (x**2 + 5 * x + 3, 1), (x**2 + 9 * x + 4, 1),
             (x**2 + 4 * x + 5, 1), (x**2 + 3 * x + 9, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    with using(gf_factor_method='other'):
        pytest.raises(KeyError, lambda: (x + 1).factor_list())

    R, x = ring('x', FF(13))

    f = x**8 + x**6 + 10 * x**4 + 10 * x**3 + 8 * x**2 + 2 * x + 8
    g = (1, [(x + 3, 1), (x**3 + 8 * x**2 + 4 * x + 12, 1),
             (x**4 + 2 * x**3 + 3 * x**2 + 4 * x + 6, 1)])

    with using(gf_factor_method='berlekamp'):
        assert f.factor_list() == g

    R, x = ring('x', FF(809))

    f = (x**10 + 2 * x**9 + 5 * x**8 + 26 * x**7 + 677 * x**6 + 436 * x**5 +
         791 * x**4 + 325 * x**3 + 456 * x**2 + 24 * x + 577)
    g = (1, [(x + 701, 1),
             (x**9 + 110 * x**8 + 559 * x**7 + 532 * x**6 + 694 * x**5 +
              151 * x**4 + 110 * x**3 + 70 * x**2 + 735 * x + 122, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

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

    R, x = ring('x', FF(nextprime(2**15 * pi)))

    f = x**15 + x + 1
    g = (1, [
        (x**2 + 22730 * x + 68144, 1),
        (x**4 + 81553 * x**3 + 77449 * x**2 + 86810 * x + 4724, 1),
        (x**4 + 86276 * x**3 + 56779 * x**2 + 14859 * x + 31575, 1),
        (x**5 + 15347 * x**4 + 95022 * x**3 + 84569 * x**2 + 94508 * x + 92335,
         1)
    ])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == 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

    R, x = ring('x', FF(nextprime(2**4 * pi)))

    f = x**6 + 2 * x**5 + 5 * x**4 + 26 * x**3 + 41 * x**2 + 39 * x + 38
    g = (1, [(x**2 + 44 * x + 26, 1),
             (x**4 + 11 * x**3 + 25 * x**2 + 18 * x + 30, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    F8 = FF(2, [1, 1, 0, 1])
    R, x = ring('x', F8)

    f = x**10 + x**9 + F8(2) * x**8 + F8(2) * x**7 + F8(5) * x**6 + F8(
        3) * x**5
    g = (F8(1), [(x, 5), (x + F8(3), 1), (x + F8(6), 1),
                 (x**3 + F8(4) * x**2 + x + F8(3), 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    F9 = FF(3, [2, 2, 1])
    R, x = ring('x', F9)

    f = x**5 + F9(2) * x**4 + F9(6) * x**3 + F9(8) * x**2 + F9(5) * x + F9(4)
    g = (1, [(x + F9(8), 1), (x**2 + 2 * x + F9(4), 1),
             (x**2 + F9(4) * x + F9(4), 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g
Example #7
0
def test__zz_wang():
    R, x, y, z = ring('x y z', ZZ)
    UV, _x = ring('x', ZZ)

    p = ZZ(nextprime(R._zz_mignotte_bound(w_1)))

    assert p == 6291469

    t_1, k_1, e_1 = y, 1, ZZ(-14)
    t_2, k_2, e_2 = z, 2, ZZ(3)
    t_3, k_3, e_3 = y + z, 2, ZZ(-11)
    t_4, k_4, e_4 = y - z, 1, ZZ(-17)

    T = [t_1, t_2, t_3, t_4]
    K = [k_1, k_2, k_3, k_4]
    E = [e_1, e_2, e_3, e_4]

    T = list(zip([t.drop(x) for t in T], K))

    A = [ZZ(-14), ZZ(3)]

    S = w_1.eval([(y, A[0]), (z, A[1])])
    cs, s = S.primitive()

    assert cs == 1 and s == S == (1036728 * _x**6 + 915552 * _x**5 +
                                  55748 * _x**4 + 105621 * _x**3 -
                                  17304 * _x**2 - 26841 * _x - 644)

    assert R._zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17]
    assert s.is_squarefree and s.degree() == w_1.degree()

    _, H = UV._zz_factor_sqf(s)

    h_1 = 187 * _x**2 - 23
    h_2 = 44 * _x**2 + 42 * _x + 1
    h_3 = 126 * _x**2 - 9 * _x + 28

    LC = [lc.drop(x) for lc in [y**2 - z**2, -4 * y - 4 * z, -y * z**2]]
    factors = R._zz_wang_hensel_lifting(w_1, H, LC, A, p)

    assert H == [h_1, h_2, h_3]
    assert R._zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC)
    assert functools.reduce(operator.mul, factors) == w_1

    # coverage tests
    f = x**6 + 5 * x**4 * y - 5 * x**2 * y**2 - y**3

    assert R._zz_wang(f, mod=4,
                      seed=1) == [x**2 - y, x**4 + 6 * x**2 * y + y**2]

    # This tests a bug in the Wang algorithm that occured only with a very
    # specific set of random numbers; issue sympy/sympy#6355.
    random_sequence = [
        -1, -1, 0, 0, 0, 0, -1, -1, 0, -1, 3, -1, 3, 3, 3, 3, -1, 3
    ]

    R, x, y, z = ring('x y z', ZZ)

    f = 2 * x**2 + y * z - y - z**2 + z

    assert R._zz_wang(f, seed=random_sequence) == [f]

    with using(eez_restart_if_needed=False):
        pytest.raises(ExtraneousFactors,
                      lambda: R._zz_wang(f, seed=random_sequence))
Example #8
0
def test_gf_factor():
    assert gf_factor([], 11, ZZ) == (0, [])
    assert gf_factor([1], 11, ZZ) == (1, [])
    assert gf_factor([1, 1], 11, ZZ) == (1, [([1, 1], 1)])

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'shoup')

    assert gf_factor_sqf(ZZ.map([]), 11, ZZ) == (0, [])
    assert gf_factor_sqf(ZZ.map([1]), 11, ZZ) == (1, [])
    assert gf_factor_sqf(ZZ.map([1, 1]), 11, ZZ) == (1, [[1, 1]])

    f, p = ZZ.map([1, 0, 0, 1, 0]), 2

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

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

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

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == g

    f, p = gf_from_int_poly([1, -3, 1, -3, -1, -3, 1], 11), 11

    g = (1, [([1, 1], 1), ([1, 5, 3], 1), ([1, 2, 3, 4], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = [1, 5, 8, 4], 11

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

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = [1, 1, 10, 1, 0, 10, 10, 10, 0, 0], 11

    g = (1, [([1, 0], 2), ([1, 9, 5], 1), ([1, 3, 0, 8, 5, 2], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({32: 1, 0: 1}, 11, ZZ), 11

    g = (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)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({32: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11

    g = (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)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({63: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11

    g = (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)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

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

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

    assert gf_sqf_p(f, p, ZZ) is 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)])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

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

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == 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 = ZZ(nextprime(int((2**4 * pi).evalf())))
    f = ZZ.map([1, 2, 5, 26, 41, 39, 38])

    assert gf_sqf_p(f, p, ZZ) is True

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

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

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

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'other')
    pytest.raises(KeyError, lambda: gf_factor([1, 1], 11, ZZ))
    config.setup('GF_FACTOR_METHOD')
Example #9
0
def test_gf_factor_sqf():
    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])
    assert gf_factor_sqf([2, 3], 11, ZZ) == (2, [[1, 7]])

    with config.using(gf_factor_method='berlekamp'):
        assert gf_factor_sqf([], 11, ZZ) == (0, [])
        assert gf_factor_sqf([1], 11, ZZ) == (1, [])
        assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])
        assert gf_factor_sqf([1, 0], 11, ZZ) == (1, [[1, 0]])

    with config.using(gf_factor_method='zassenhaus'):
        assert gf_factor_sqf([], 11, ZZ) == (0, [])
        assert gf_factor_sqf([1], 11, ZZ) == (1, [])
        assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])
        assert gf_factor_sqf([1, 0], 11, ZZ) == (1, [[1, 0]])

    with config.using(gf_factor_method='shoup'):
        assert gf_factor_sqf([], 11, ZZ) == (0, [])
        assert gf_factor_sqf([1], 11, ZZ) == (1, [])
        assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])
        assert gf_factor_sqf([1, 0], 11, ZZ) == (1, [[1, 0]])

    f, p = [1, 0, 0, 1, 0], 2
    g = (1, [[1, 0],
             [1, 1],
             [1, 1, 1]])

    with config.using(gf_factor_method='berlekamp'):
        assert gf_factor_sqf(f, p, ZZ) == g

    with config.using(gf_factor_method='zassenhaus'):
        assert gf_factor_sqf(f, p, ZZ) == g

    with config.using(gf_factor_method='shoup'):
        assert gf_factor_sqf(f, p, ZZ) == g

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

    p = ZZ(nextprime(int((2**15*pi))))
    f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)
    g = (1, [[1, 22730, 68144],
             [1, 81553, 77449, 86810, 4724],
             [1, 86276, 56779, 14859, 31575],
             [1, 15347, 95022, 84569, 94508, 92335]])

    with config.using(gf_factor_method='zassenhaus'):
        assert gf_factor_sqf(f, p, ZZ) == g

    with config.using(gf_factor_method='shoup'):
        assert gf_factor_sqf(f, p, ZZ) == 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 = ZZ(nextprime(int((2**4*pi))))
    f = [1, 2, 5, 26, 41, 39, 38]

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

    with config.using(gf_factor_method='zassenhaus'):
        assert gf_factor_sqf(f, p, ZZ) == g

    with config.using(gf_factor_method='shoup'):
        assert gf_factor_sqf(f, p, ZZ) == g
Example #10
0
def test_gf_factor():
    assert gf_factor([], 11, ZZ) == (0, [])
    assert gf_factor([1], 11, ZZ) == (1, [])
    assert gf_factor([1, 1], 11, ZZ) == (1, [([1, 1], 1)])

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'shoup')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

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

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

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

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

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == g

    f, p = gf_from_int_poly([1, -3, 1, -3, -1, -3, 1], 11), 11

    g = (1, [([1, 1], 1),
             ([1, 5, 3], 1),
             ([1, 2, 3, 4], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = [1, 5, 8, 4], 11

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

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = [1, 1, 10, 1, 0, 10, 10, 10, 0, 0], 11

    g = (1, [([1, 0], 2), ([1, 9, 5], 1), ([1, 3, 0, 8, 5, 2], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({32: 1, 0: 1}, 11, ZZ), 11

    g = (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)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({32: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11

    g = (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)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({63: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11

    g = (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)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

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

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

    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)])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

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

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == 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 = ZZ(nextprime(int((2**4 * pi).evalf())))
    f = [1, 2, 5, 26, 41, 39, 38]

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

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

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

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'other')
    pytest.raises(KeyError, lambda: gf_factor([1, 1], 11, ZZ))
    config.setup('GF_FACTOR_METHOD')

    # IPoly interface:
    R, t = ring("t", FF(11))
    assert R.gf_factor_sqf(2*t + 3) == (2, [t + 7])
Example #11
0
def test_gf_factor():
    R, x = ring('x', FF(11))

    assert R(0).factor_list() == (0, [])
    assert R(1).factor_list() == (1, [])
    assert (x + 1).factor_list() == (1, [(x + 1, 1)])

    assert (5 * x**3 + 2 * x**2 + 7 * x + 2).factor_list() == (5, [(x + 2, 1),
                                                                   (x + 8, 2)])

    f = x**6 + 8 * x**5 + x**4 + 8 * x**3 + 10 * x**2 + 8 * x + 1
    g = (1, [(x + 1, 1), (x**2 + 5 * x + 3, 1),
             (x**3 + 2 * x**2 + 3 * x + 4, 1)])

    with config.using(gf_factor_method='berlekamp'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='zassenhaus'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='shoup'):
        assert f.factor_list() == g

    f = x**3 + 5 * x**2 + 8 * x + 4

    g = (1, [(x + 1, 1), (x + 2, 2)])

    with config.using(gf_factor_method='berlekamp'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='zassenhaus'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='shoup'):
        assert f.factor_list() == g

    f = x**9 + x**8 + 10 * x**7 + x**6 + 10 * x**4 + 10 * x**3 + 10 * x**2
    g = (1, [(x, 2), (x**2 + 9 * x + 5, 1),
             (x**5 + 3 * x**4 + 8 * x**2 + 5 * x + 2, 1)])

    with config.using(gf_factor_method='berlekamp'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='zassenhaus'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='shoup'):
        assert f.factor_list() == g

    f = x**32 + 1

    g = (1, [(x**16 + 3 * x**8 + 10, 1), (x**16 + 8 * x**8 + 10, 1)])

    with config.using(gf_factor_method='berlekamp'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='zassenhaus'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='shoup'):
        assert f.factor_list() == g

    f = 8 * x**32 + 5
    g = (8, [(x + 3, 1), (x + 8, 1), (x**2 + 9, 1), (x**2 + 2 * x + 2, 1),
             (x**2 + 9 * x + 2, 1), (x**4 + 5 * x**2 + 7, 1),
             (x**4 + 6 * x**2 + 7, 1), (x**8 + x**4 + 6, 1),
             (x**8 + 10 * x**4 + 6, 1)])

    with config.using(gf_factor_method='berlekamp'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='zassenhaus'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='shoup'):
        assert f.factor_list() == g

    f = 8 * x**63 + 5
    g = (8,
         [(x + 7, 1), (x**2 + 4 * x + 5, 1), (x**3 + 6 * x**2 + 8 * x + 2, 1),
          (x**3 + 9 * x**2 + 9 * x + 2, 1), (x**6 + 9 * x**3 + 4, 1),
          (x**6 + 2 * x**5 + 8 * x**3 + 4 * x**2 + 6 * x + 4, 1),
          (x**6 + 2 * x**5 + 3 * x**4 + 8 * x**3 + 6 * x + 4, 1),
          (x**6 + 2 * x**5 + 6 * x**4 + 8 * x**2 + 4 * x + 4, 1),
          (x**6 + 3 * x**5 + 3 * x**4 + x**3 + 6 * x**2 + 8 * x + 4, 1),
          (x**6 + 5 * x**5 + 6 * x**4 + 8 * x**2 + 6 * x + 4, 1),
          (x**6 + 6 * x**5 + 2 * x**4 + 7 * x**3 + 9 * x**2 + 8 * x + 4, 1),
          (x**6 + 10 * x**5 + 4 * x**4 + 7 * x**3 + 10 * x**2 + 7 * x + 4, 1),
          (x**6 + 10 * x**5 + 10 * x**4 + x**3 + 4 * x**2 + 9 * x + 4, 1)])

    with config.using(gf_factor_method='berlekamp'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='zassenhaus'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='shoup'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='other'):
        pytest.raises(KeyError, lambda: (x + 1).factor_list())

    R, x = ring('x', FF(2))

    f = x**4 + x
    g = (1, [(x, 1), (x + 1, 1), (x**2 + x + 1, 1)])

    with config.using(gf_factor_method='berlekamp'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='zassenhaus'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='shoup'):
        assert f.factor_list() == g

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

    p = ZZ(nextprime(int((2**15 * pi))))
    R, x = ring('x', FF(p))

    f = x**15 + x + 1
    g = (1, [
        (x**2 + 22730 * x + 68144, 1),
        (x**4 + 81553 * x**3 + 77449 * x**2 + 86810 * x + 4724, 1),
        (x**4 + 86276 * x**3 + 56779 * x**2 + 14859 * x + 31575, 1),
        (x**5 + 15347 * x**4 + 95022 * x**3 + 84569 * x**2 + 94508 * x + 92335,
         1)
    ])

    with config.using(gf_factor_method='zassenhaus'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='shoup'):
        assert f.factor_list() == 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 = ZZ(nextprime(int((2**4 * pi))))
    R, x = ring('x', FF(p))

    f = x**6 + 2 * x**5 + 5 * x**4 + 26 * x**3 + 41 * x**2 + 39 * x + 38

    g = (1, [(x**2 + 44 * x + 26, 1),
             (x**4 + 11 * x**3 + 25 * x**2 + 18 * x + 30, 1)])

    with config.using(gf_factor_method='zassenhaus'):
        assert f.factor_list() == g

    with config.using(gf_factor_method='shoup'):
        assert f.factor_list() == g