A bit of cleanup
Move some helper files to other folders. Actually remove the .sage files from the main directory, as I wrote I would in the readme.
This commit is contained in:
parent
bacd7cff71
commit
03ec008d0d
@ -1,12 +0,0 @@
|
||||
F = GF(2)
|
||||
PR = PolynomialRing(F, 'x')
|
||||
|
||||
max_k = 31
|
||||
whitespace = max_k+1
|
||||
|
||||
for i in range(1, max_k+1):
|
||||
poly_int = '0b'+ ''.join(map(str, PR.irreducible_element(i).coefficients(sparse=False)[::-1]))
|
||||
poly_int += ','
|
||||
poly_int += ' '*(max_k-i)
|
||||
print(f' {poly_int}// irreducible polynomial of degree {i}')
|
||||
|
@ -1,96 +0,0 @@
|
||||
k = 31
|
||||
F = GF(2)
|
||||
E = GF(2^k)
|
||||
PR = PolynomialRing(F, 'x')
|
||||
p = PR.irreducible_element(k)
|
||||
|
||||
while(True):
|
||||
gamma = E.random_element()
|
||||
if gamma not in F:
|
||||
break
|
||||
|
||||
# gamma = PR('x^3+x')
|
||||
# print('0b'+ ''.join(map(str, gamma.coefficients(sparse=False)[::-1])))
|
||||
|
||||
eqs = [PR(gamma)^i % p for i in range(k+1)]
|
||||
for eq in eqs:
|
||||
print(eq)
|
||||
print()
|
||||
|
||||
eqs = list(map(lambda eq: eq.coefficients(sparse=False) + [0]*(k-eq.degree()-1), eqs))
|
||||
for eq in eqs:
|
||||
print(eq)
|
||||
print()
|
||||
eqs = matrix(eqs[::-1])
|
||||
print(eqs)
|
||||
print()
|
||||
eqs = eqs.transpose()
|
||||
print(eqs)
|
||||
print()
|
||||
|
||||
rref = eqs.rref()
|
||||
print(rref)
|
||||
irrpol = PR(rref[:,-1].list() + [1])
|
||||
print(irrpol)
|
||||
print(irrpol.is_irreducible())
|
||||
print()
|
||||
|
||||
# manual rref
|
||||
def calc_rref_Z2(A, m, n):
|
||||
"""Calculate the reduced row echelon form of a (mxn)-matrix"""
|
||||
A = copy(A)
|
||||
|
||||
# calculate row echelon form
|
||||
# https://en.wikipedia.org/wiki/Gaussian_elimination#Pseudocode
|
||||
h = 0 # Initialization of the pivot row
|
||||
k = 0 # Initialization of the pivot column
|
||||
|
||||
while h < m and k < n:
|
||||
# Find the k-th pivot:
|
||||
i_max = -1
|
||||
for i in range(h, m):
|
||||
if A[i,k] == 1:
|
||||
i_max = i
|
||||
break
|
||||
|
||||
if i_max == -1:
|
||||
# No pivot in this column, pass to next column
|
||||
k += 1
|
||||
else:
|
||||
tmp = A[i_max, :]
|
||||
A[i_max, :] = A[h, :]
|
||||
A[h, :] = tmp
|
||||
|
||||
|
||||
# Do for all rows below pivot:
|
||||
for i in range(h+1, m):
|
||||
f = A[i][k];
|
||||
if f == 0:
|
||||
continue;
|
||||
A[i, k] = 0;
|
||||
for j in range(k+1, n):
|
||||
A[i, j] = A[i, j] - A[h, j]
|
||||
|
||||
# Increase pivot row and column
|
||||
h += 1
|
||||
k += 1
|
||||
|
||||
# perform back substitution
|
||||
for i in range(1, m):
|
||||
for j in range(i):
|
||||
if A[j, i] == 1:
|
||||
A[j, :] -= A[i, :]
|
||||
|
||||
return A
|
||||
|
||||
print('Manual RREF:\n')
|
||||
|
||||
rref = calc_rref_Z2(eqs, k, k+1) # cols = rows+1, since we transposed the matrix
|
||||
print(rref)
|
||||
print()
|
||||
irrpol = PR(rref[:,-1].list() + [1])
|
||||
print(irrpol)
|
||||
print(irrpol.is_irreducible())
|
||||
print()
|
||||
|
||||
print('0b'+ ''.join(map(str, irrpol.coefficients(sparse=False)[::-1])))
|
@ -1,68 +0,0 @@
|
||||
import math
|
||||
|
||||
k = 31
|
||||
F = GF(2)
|
||||
var = 'x'
|
||||
PR = PolynomialRing(F, var)
|
||||
p = PR.irreducible_element(k)
|
||||
gamma = PR.random_element(k)
|
||||
|
||||
exp = 10
|
||||
target = gamma^exp % p
|
||||
|
||||
def polynomial_power_in_Z2(q, exp, p, var):
|
||||
if exp == 0:
|
||||
return 1
|
||||
|
||||
if q.degree() == p.degree():
|
||||
q += p
|
||||
|
||||
res = q
|
||||
|
||||
for i in range(2, exp+1):
|
||||
res = multiply_polynomials_in_Z2(res, q, p, var)
|
||||
|
||||
return res
|
||||
|
||||
def polynomial_power_in_Z2_V2(q, exp, p, var):
|
||||
if exp == 0:
|
||||
return 1
|
||||
|
||||
if q.degree() == p.degree():
|
||||
q += p
|
||||
|
||||
log2deg = math.floor(math.log2(exp)) + 1
|
||||
arr = [0]*log2deg
|
||||
arr[0] = q
|
||||
for i in range(1, log2deg):
|
||||
arr[i] = multiply_polynomials_in_Z2(arr[i-1], arr[i-1], p, var)
|
||||
|
||||
res = PR(1)
|
||||
for i, b in enumerate(bin(exp)[:1:-1]):
|
||||
if b == '1':
|
||||
res = multiply_polynomials_in_Z2(res, arr[i], p, var)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def multiply_polynomials_in_Z2(q1, q2, p, var):
|
||||
if q1.degree() > p.degree():
|
||||
raise ValueError('Unsupported!')
|
||||
|
||||
if q1.degree() == p.degree():
|
||||
q1 += p
|
||||
|
||||
arr = [0]*(q2.degree()+1)
|
||||
arr[0] = q1
|
||||
for i in range(1, q2.degree()+1):
|
||||
arr[i] = arr[i-1]*PR(var)
|
||||
if arr[i].degree() == p.degree():
|
||||
arr[i] += p
|
||||
|
||||
return sum(arr[i] for i in q2.exponents())
|
||||
|
||||
print(target)
|
||||
res = polynomial_power_in_Z2(gamma, exp, p, var)
|
||||
print(res)
|
||||
res = polynomial_power_in_Z2_V2(gamma, exp, p, var)
|
||||
print(res)
|
Loading…
Reference in New Issue
Block a user