-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
69 lines (51 loc) · 1.91 KB
/
Copy pathutils.py
File metadata and controls
69 lines (51 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from random import randint
from sage.all import *
def gf2n_encode_matrices(F):
assert F.characteristic() == 2
n = F.degree()
mod = F.modulus()
entry = [[0]*n for _ in range(n)]
for i in range(n-1):
entry[i+1][i] = 1
for i in range(n):
entry[i][n-1] = mod[i]
A = matrix(GF(2), entry)
return [A**i for i in range(n)]
def gf2vec_to_gf2n(vec, encode_mats):
return sum(b * mat for b, mat in zip(vec, encode_mats))
def gf2n_to_gf2vec(mat):
return mat.column(0)
def random_nonzero_gf2n_elem(F):
assert F.characteristic() == 2
n = F.degree()
return F.fetch_int(randint(1, 2**n-1))
def random_gf2n_elem(F):
assert F.characteristic() == 2
n = F.degree()
return F.fetch_int(randint(0, 2**n-1))
def random_rain_lin(F):
"""Build matrix from a random linearized polynomial L(X) such that
1) L^{-1}(X) exists
2) coefficients of L(X) and L^{-1} are all nonzero
3) degree of L(X) and L^{-1}(X) are n-1
"""
assert F.characteristic() == 2
n = F.degree()
# construct appropriate linearized polynomial
while True:
linpoly_coeffs = [random_nonzero_gf2n_elem(F) for i in range(n)]
# construct dickson matrix
entry = [[linpoly_coeffs[j-i]**(2**i) for j in range(n)] for i in range(n)]
matrix_dickson = matrix(F, entry)
# check inverse polynomial
inv_coeffs = matrix_dickson.adjugate().row(0) # skip multiplying 1/det
if matrix_dickson.is_invertible() and F.zero() not in inv_coeffs:
break
# Precompute basis and polynomial evaluations
basis = [F.fetch_int(2)**i for i in range(n)]
dual_basis = F.dual_basis()
poly_evals = [sum([linpoly_coeffs[j] * (basis[i]**(2**j)) for j in range(n)])
for i in range(n)]
entry = [[(dual_basis[i] * poly_evals[j]).trace() for j in range(n)]
for i in range(n)]
return matrix(GF(2), entry)