예제 #1
0
def test_state():
    poly = galois.Poly.Degrees([7,1,0])

    lfsr = galois.LFSR(poly, state=1, config="galois")
    assert np.array_equal(lfsr.state, [0, 0, 0, 0, 0, 0, 1])

    lfsr = galois.LFSR(poly, state=4, config="galois")
    assert np.array_equal(lfsr.state, [0, 0, 0, 0, 1, 0, 0])
예제 #2
0
def test_exceptions():
    poly = galois.Poly.Degrees([7,1,0])

    with pytest.raises(TypeError):
        galois.LFSR(poly.coeffs)
    with pytest.raises(TypeError):
        galois.LFSR(poly, state=float(poly.integer))
    with pytest.raises(TypeError):
        galois.LFSR(poly.coeffs, config=1)

    with pytest.raises(ValueError):
        galois.LFSR(poly, config="invalid-argument")
예제 #3
0
def test_output_is_reversed_state():
    poly = galois.Poly.Degrees([7, 1, 0])
    state = galois.GF2.Zeros(7)
    state[-1] = 1
    lfsr = galois.LFSR(poly, state=state)
    y = lfsr.step(7)
    assert np.array_equal(y, state[::-1])
예제 #4
0
def test_gf3_output():
    """
    Sage:
        F = GF(3)
        key = [F(2),F(0),F(0),F(2)]
        fill = [F(1),F(0),F(0),F(0)]
        n = 200
        s = lfsr_sequence(key,fill,n); np.array(s)
    """
    GF = galois.GF(3)
    poly = galois.Poly([2, 0, 0, 2, 1],
                       field=GF)  # galois.conway_poly(3, 4) / GF(2)
    state = GF([0, 0, 0, 1])
    y_truth = GF([
        1, 0, 0, 0, 2, 1, 2, 1, 0, 2, 2, 0, 0, 1, 0, 0, 0, 2, 1, 2, 1, 0, 2, 2,
        0, 0, 1, 0, 0, 0, 2, 1, 2, 1, 0, 2, 2, 0, 0, 1, 0, 0, 0, 2, 1, 2, 1, 0,
        2, 2, 0, 0, 1, 0, 0, 0, 2, 1, 2, 1, 0, 2, 2, 0, 0, 1, 0, 0, 0, 2, 1, 2,
        1, 0, 2, 2, 0, 0, 1, 0, 0, 0, 2, 1, 2, 1, 0, 2, 2, 0, 0, 1, 0, 0, 0, 2,
        1, 2, 1, 0, 2, 2, 0, 0, 1, 0, 0, 0, 2, 1, 2, 1, 0, 2, 2, 0, 0, 1, 0, 0,
        0, 2, 1, 2, 1, 0, 2, 2, 0, 0, 1, 0, 0, 0, 2, 1, 2, 1, 0, 2, 2, 0, 0, 1,
        0, 0, 0, 2, 1, 2, 1, 0, 2, 2, 0, 0, 1, 0, 0, 0, 2, 1, 2, 1, 0, 2, 2, 0,
        0, 1, 0, 0, 0, 2, 1, 2, 1, 0, 2, 2, 0, 0, 1, 0, 0, 0, 2, 1, 2, 1, 0, 2,
        2, 0, 0, 1, 0, 0, 0, 2
    ])
    lfsr = galois.LFSR(poly, state=state)
    y = lfsr.step(y_truth.size)
    assert np.array_equal(y, y_truth)
예제 #5
0
def test_step_exceptions():
    poly = galois.Poly.Degrees([7,1,0])
    lfsr = galois.LFSR(poly, config="galois")

    with pytest.raises(TypeError):
        lfsr.step(10.0)
    with pytest.raises(ValueError):
        lfsr.step(0)
    with pytest.raises(ValueError):
        lfsr.step(-1)
예제 #6
0
def test_gf2_output_2():
    """
    The states of the Galois LFSR generate the binary extension field with the connection polynomial as its
    irreducible polynomial.
    """
    GF = galois.GF2
    poly = galois.conway_poly(2, 8)
    state = GF([0,0,0,0,0,0,0,1])
    lfsr = galois.LFSR(poly, state=state, config="galois")

    GFE = galois.GF(2**8, irreducible_poly=poly)
    alpha = GFE.primitive_element

    for i in range(GFE.order - 1):
        np.array_equal(lfsr.state, (alpha**i).vector())
        lfsr.step()
예제 #7
0
def test_gf2_output_2():
    """
    Sage:
        F = GF(2)
        o = F(0); l = F(1)
        key = [l,l,o,l]
        fill = [l,l,o,l]
        n = 20
        s = lfsr_sequence(key,fill,n); s
    """
    GF = galois.GF2
    poly = galois.Poly([1, 1, 0, 1, 1], field=GF)
    state = GF([1, 0, 1, 1])
    y_truth = GF([1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1])
    lfsr = galois.LFSR(poly, state=state)
    y = lfsr.step(y_truth.size)
    assert np.array_equal(y, y_truth)
예제 #8
0
def test_gfp_large_output():
    """
    Sage:
        F = GF(36893488147419103183)
        key = [F(3),F(0),F(3),F(1)]
        fill = [F(1),F(0),F(0),F(0)]
        n = 20
        s = lfsr_sequence(key,fill,n); np.array(s)
    """
    GF = galois.GF(36893488147419103183)
    poly = galois.Poly([3, 0, 3, 1, 1],
                       field=GF)  # Not a primitive polynomial...
    state = GF([0, 0, 0, 1])
    y_truth = GF([
        1, 0, 0, 0, 3, 3, 12, 21, 66, 138, 372, 849, 2163, 5124, 12729, 30648,
        75324, 182640, 446799, 1086663, 2653032, 6460941, 15760434, 38403246,
        93643644, 228236205, 556448439, 1356366792, 3306643041, 8060452032,
        19649726472, 47900182944, 116769291483, 284651196411, 693908250276,
        1691562388341, 4123595013618, 10052235767874, 24504745559556,
        59736140028201
    ])
    lfsr = galois.LFSR(poly, state=state)
    y = lfsr.step(y_truth.size)
    assert np.array_equal(y, y_truth)
예제 #9
0
def test_repr():
    poly = galois.Poly.Degrees([7,1,0])
    lfsr = galois.LFSR(poly, config="galois")
    assert repr(lfsr) == "<Galois LFSR: poly=Poly(x^7 + x + 1, GF(2))>"
예제 #10
0
def test_repr():
    poly = galois.Poly.Degrees([7, 1, 0])
    lfsr = galois.LFSR(poly)
    assert repr(lfsr) == "<Fibonacci LFSR: poly=Poly(x^7 + x + 1, GF(2))>"