Ask Your Question
0

Matrix to polynomial

asked 2026-01-20 15:55:49 +0100

mille gravatar image

updated 2026-01-23 17:39:29 +0100

Max Alekseyev gravatar image

Hi,

I want to make a python function to use in sagemath that converts from polynomial to matrix using a normal basis generator in a GF. I am now running

F.<a> = GF(2^4)
a = F.gen()

R.<x> = PolynomialRing(F)
poly = x^3

b = a^3

M = polynomial_to_matrix(F, poly, b)
poly_rec = matrix_to_polynomial(F, M, basis)

Then expecting a matrix like

[ 0, a^6, 1, a^3 ]
[ a^6, 0, a^12, 1 ]
[ 1, a^12, 0, a^9 ]
[ a^3, 1, a^9, 0 ]

Rather I get

[            0 a^3 + a^2 + a   a^2 + a + 1 a^3 + a^2 + 1]
[a^3 + a^2 + a             0   a^3 + a + 1       a^2 + a]
[  a^2 + a + 1   a^3 + a + 1             0       a^3 + 1]
[a^3 + a^2 + 1       a^2 + a       a^3 + 1             0

What concept do I not get or have misinterpreted? All help is greatly appreciated

The code

def polynomial_to_matrix(F, poly, basis_gen):
    n = F.degree()

    basis = [basis_gen**(2**i) for i in range(n)]
    M = [[poly(bi) + poly(bj) + poly(bi + bj) + poly(0) for bj in basis] for bi in basis]

    return Matrix(F, M)


def matrix_to_polynomial(F, M, basis):
    n = F.degree()    
    R = PolynomialRing(F, 'x')
    x = R.gen()

    M_alpha = Matrix(F, [[basis[i]**(2**j) for j in range(n)] for i in range(n)])
    M_alpha_inv = M_alpha.inverse()
    CF = M_alpha_inv.transpose() * M * M_alpha_inv

    poly = 0
    for i in range(n):
        for j in range(i):
            c = CF[i,j]
            if c != 0:
                poly += c * x**(2**i + 2**j)

    return poly
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2026-01-23 17:47:51 +0100

Max Alekseyev gravatar image

I did not check the details but the matrix you get may be correct. Note that a from F.<a> = GF(2^4) is a zero of some degree-4 polynomial over GF(2) and thus powers of a are reduced modulo that polynomial (and their reduced form depends on that polynomial). For example, on SageCell the following code

F.<a> = GF(2^4)
print( F.modulus() )
for d in (1..12):
    print(f'a^{d} = {a^d}')

prints

x^4 + x + 1
a^1 = a
a^2 = a^2
a^3 = a^3
a^4 = a + 1
a^5 = a^2 + a
a^6 = a^3 + a^2
a^7 = a^3 + a + 1
a^8 = a^2 + 1
a^9 = a^3 + a
a^10 = a^2 + a + 1
a^11 = a^3 + a^2 + a
a^12 = a^3 + a^2 + a + 1
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2026-01-20 15:55:49 +0100

Seen: 23 times

Last updated: 2 days ago