Beispiel #1
0
def mat_field_mul_const(field, c):
    assert field.base_ring() == GF(2)
    d = field.degree()
    m = matrix(GF(2), d, d)
    for i, e in enumerate(reversed(range(d))):
        x = 1 << e
        res = field.fetch_int(x) * field.fetch_int(c)
        res = tobin(res.integer_representation(), d)
        m.set_column(i, res)
    return m
Beispiel #2
0
def matrix_mult_int(mat, x):
    """
    MSB to LSB vector
    >>> matrix_mult_int( \
        matrix(GF(2), [[1, 0, 1], [1, 0, 0]]), \
        0b110) # read as 6 -> 1,1,0 -> 1,1 -> 3
    3
    """
    assert mat.base_ring() == GF(2)
    n = mat.ncols()
    x = vector(GF(2), tobin(x, n))
    y = mat * x
    return frombin(y)
Beispiel #3
0
 def cmul_xor_ddt(self, F=None):
     if F is None:
         F = GF(self.insize, name='a')
     cxddt = matrix(ZZ, self.insize, self.outsize)
     for x in xrange(1, self.insize):
         for dx in xrange(2, self.insize):
             x2 = (F.fetch_int(x) *
                   F.fetch_int(dx)).integer_representation()
             y = self[x]
             y2 = self[x2]
             dy = y2 ^ y
             cxddt[dx, dy] += 1
     return cxddt
Beispiel #4
0
def matrix_mult_int_rev(mat, x):
    """
    LSB to MSB vector
    >>> matrix_mult_int_rev( \
        matrix(GF(2), [[1, 0, 1], [1, 0, 0]]), \
        0b110) # read as 6 -> 0,1,1 -> 1,0 -> 1
    1
    """
    assert mat.base_ring() == GF(2)
    n = mat.ncols()
    x = vector(GF(2), tobin(x, n)[::-1])
    y = mat * x
    return frombin(y[::-1])
Beispiel #5
0
 def xor_cmul_ddt(self, F=None):
     if F is None:
         F = GF(self.insize, name='a')
     xcddt = matrix(ZZ, self.insize, self.outsize)
     for x in xrange(self.insize):
         for dx in xrange(1, self.insize):
             x2 = x ^ dx
             y = self[x]
             y2 = self[x2]
             dy = (F.fetch_int(y2) * F.fetch_int(y)
                   **(self.outsize - 2)).integer_representation()
             xcddt[dx, dy] += 1
     return xcddt
Beispiel #6
0
def mat_from_linear_func(m, n, func):
    mat = matrix(GF(2), n, m)
    for i, e in enumerate(reversed(range(m))):
        x = 1 << e
        res = tobin(func(x), n)
        mat.set_column(i, res)
    return mat
Beispiel #7
0
    def as_matrix(self):
        assert self.is_linear()

        m = matrix(GF(2), self.n, self.m)
        for e in range(self.m):
            x = 1 << e
            m.set_column(self.m - 1 - e, tobin(self[x], self.n))
        return m
Beispiel #8
0
def idup(mat):
    """
    I mat
    0  I
    """
    assert mat.nrows() == mat.ncols()
    n = mat.nrows()
    res = identity_matrix(GF(2), 2 * n)
    res[:n, n:] = mat
    return res
Beispiel #9
0
def idlo(mat):
    """
    I   0
    mat I
    """
    assert mat.nrows() == mat.ncols()
    n = mat.nrows()
    res = identity_matrix(GF(2), 2 * n)
    res[n:, :n] = mat
    return res
Beispiel #10
0
def diag(a, b):
    """
    a 0
    0 b
    """
    assert a.nrows() == a.ncols() == b.nrows() == b.ncols()
    n = a.nrows()
    res = identity_matrix(GF(2), 2 * n)
    res[:n, :n] = a
    res[n:, n:] = b
    return res
Beispiel #11
0
 def hdim(self, right_to_left=False):
     """
     hdim[i,j] = i-th output bit contains monomial x1...xn/xj
     """
     res = matrix(GF(2), self.in_bits, self.out_bits)
     anf = mobius(tuple(self))
     for j in xrange(self.in_bits):
         mask = (1 << self.in_bits) - 1
         mask ^= 1 << (self.in_bits - 1 - j)
         res.set_column(j, tobin(anf[mask], self.out_bits))
     if right_to_left:
         res = res[::-1, ::-1]
     return res
Beispiel #12
0
def random_matrix(*args):
    return sage_random_matrix(GF(2), *args)
Beispiel #13
0
 def minilat_binary(self, mod=4):
     """minilat % mod and then mod/2 -> 1, 0 -> 0"""
     mat = self.minilat() % 4
     for x in mat.list():
         assert x in (0, mod / 2), "invalid mod for given function"
     return (mat.lift() / (mod / 2)).change_ring(GF(2))
Beispiel #14
0
def exponent(n, e, fld=None):
    fld = fld or GF(2**n, name='a')
    x = PolynomialRing(fld, names='x').gen()
    return from_poly(x**e)