예제 #1
0
def test_properties():
    from cryptools.sbox2 import SBox2

    s = SBox2([5, 6, 3, 2, 1, 7, 0, 4])
    assert props(s) == {s.is_permutation, s.is_balanced}
    assert props(s.resize(4)) == set()

    s = SBox2((0, 13, 14, 3, 8, 5, 6, 11, 12, 1, 2, 15, 4, 9, 10, 7))
    assert props(s) == {
        s.is_permutation, s.is_balanced, s.is_affine, s.is_linear
    }

    s = SBox2((14, 13, 6, 5, 0, 3, 8, 11, 15, 12, 7, 4, 1, 2, 9, 10))
    assert props(s) == {s.is_permutation, s.is_balanced, s.is_affine}

    s = SBox2([1] * 16)
    assert props(s) == {s.is_affine, s.is_constant}

    s = SBox2([0] * 16)
    assert props(s) == {
        s.is_affine,
        s.is_constant,
        s.is_linear,
        s.is_zero,
        s.is_balanced,
    }

    s = SBox2([0] * 15 + [1])
    assert props(s) == set()

    s = SBox2(range(16))
    assert props(s) == {
        s.is_identity,
        s.is_permutation,
        s.is_balanced,
        s.is_involution,
        s.is_affine,
        s.is_linear,
    }

    s = SBox2([0] * 8 + [1] * 8)
    assert props(s) == {s.is_balanced, s.is_affine, s.is_linear}

    s = SBox2([1] * 8 + [0] * 8)
    assert props(s) == {s.is_balanced, s.is_affine}

    s = SBox2(list(range(8, 16)) + list(range(8)))
    assert props(s) == {
        s.is_balanced,
        s.is_permutation,
        s.is_involution,
        s.is_affine,
    }
예제 #2
0
def test_inversion():
    from cryptools.sbox2 import SBox2
    s = SBox2([5, 6, 3, 2, 1, 7, 0, 4])

    assert ~s == s.inverse() == [6, 4, 3, 2, 7, 0, 1, 5]

    s = SBox2([3, 4, 7, 2, 1, 1, 6, 6], n=4)
    assert s.image() == (1, 2, 3, 4, 6, 7)
    assert s.preimage(2) == s.index(2) == 3
    assert s.preimage(1) == s.index(1) == 4
    assert s.preimage(6) == s.index(6) == 6
    assert s.preimages(2) == (3, )
    assert s.preimages(1) == (4, 5)
    assert s.preimages(6) == (6, 7)

    assert sorted(s.preimage_structure().items()) == [(1, 4), (2, 2)]
예제 #3
0
 def make_transition(self, exp2, log2):
     large_to_double = [0, 1]
     for x in xrange(2, 2**self.N):
         l = self.log1[x]
         y = exp2[l]
         large_to_double.append(y)
     large_to_double = SBox2(large_to_double)
     return large_to_double
예제 #4
0
def test_degrees():
    from cryptools.sbox2 import SBox2

    s = SBox2([0] * 15 + [1])
    assert s.degrees() == (4,)
    assert str(s.anfs()) == "[x0*x1*x2*x3]"

    s = SBox2([1] * 8 + [0] * 8)
    assert s.degrees() == (1,)
    assert str(s.anfs()) == "[x0 + 1]"

    s = SBox2([0] * 16)
    assert s.degrees() == ()
    assert str(s.anfs()) == "[]"

    s = SBox2([1] * 16)
    assert s.degrees() == (0,)
    assert str(s.anfs()) == "[1]"
예제 #5
0
파일: test.py 프로젝트: Tubbz-alt/cryptools
def try_good():
    A = SBox2.gen.random_linear_permutation(N)
    B = SBox2.gen.random_linear_permutation(N)
    s1 = gen()
    s2 = B * s1 * A
    At, Bt = SBox2.are_linear_equivalent(s1, s2)
    assert Bt * s1 * At == s2
    assert At.is_linear()
    assert Bt.is_linear()
    assert At.is_permutation()
    assert Bt.is_permutation()
예제 #6
0
def test_main():
    from cryptools.sbox2 import SBox2
    s = SBox2([3, 4, 7, 2, 1, 1, 6, 6], n=4)

    assert s.in_bits == 3
    assert s.out_bits == 4
    assert len(s) == 8

    assert list(s.in_range()) == list(range(8))
    assert list(s.out_range()) == list(range(16))

    assert list(s.graph()) == [(0, 3), (1, 4), (2, 7), (3, 2), (4, 1), (5, 1),
                               (6, 6), (7, 6)]

    assert s.as_hex_str(format="%sh", sep=":") == "3h:4h:7h:2h:1h:1h:6h:6h"
    assert s.as_hex_str(format="%s", sep="") == "34721166"

    ss = SBox2([3, 4, 7, 2, 1, 1, 6, 6])
    assert s != ss
    assert s == ss.resize(4)
    assert s.resize(2) == (3, 0, 3, 2, 1, 1, 2, 2)
    ss = SBox2([3, 4, 7, 2, 1, 1, 6, 6], 4)
    assert s == ss
    ss = SBox2([3, 4, 7, 2, 1, 1, 6, 6], 4)
    assert s == ss
    ss = SBox2([2, 4, 7, 2, 1, 1, 6, 6], 4)
    assert s != ss
    assert s == [3, 4, 7, 2, 1, 1, 6, 6]
    assert s == (3, 4, 7, 2, 1, 1, 6, 6)
    assert s == (3, 4, 7, 2, 1, 1, 6, 6)
    assert s != (2, 4, 7, 2, 1, 1, 6, 6)

    assert hash(s) != 0

    assert s ^ 1 == s ^ 1 == s ^ Integer(1) == s ^ SBox2([1] * 8, n=4) \
        == (2, 5, 6, 3, 0, 0, 7, 7)
    assert s ^ s == [0] * 8

    assert s.get(0) == s[0] == s(0) == s[0, 0, 0] == 3
    assert s.get(3) == s[3] == s(3) == s[0, 1, 1] == 2
    assert s.get(7) == s[7] == s(7) == s[1, 1, 1] == 6
    assert tuple(s)[1:3] == (4, 7)
    assert tuple(s)[-3:] == (1, 6, 6)
예제 #7
0
파일: test.py 프로젝트: Tubbz-alt/cryptools
def try_bad():
    s1 = gen()
    s2 = gen()
    res = SBox2.are_linear_equivalent(s1, s2)
    assert res is False
예제 #8
0
def test_transform():
    from cryptools.sbox2 import SBox2

    s = SBox2([5, 6, 3, 2, 1, 7, 0, 4])
    assert s.xor(0, 3) == s ^ 3
    assert s.mobius().mobius() == s