Solve Linear System Over Finite Field with Module - sage

Is there in sage, any instruction to solve a linear system equations
module p(x) (polynomial over finite field), where the system coefficients are polynomials over finite field in any indeterminate?. I know that for integers exists something like, example
sage: I6 = IntegerModRing(6)
sage: M = random_matrix(I6, 4, 4)
sage: v = random_vector(I6, 4)
sage: M \ v
(4, 0, 2, 1)
Here my code
F.<a> = GF(2^4)
PR = PolynomialRing(F,'X')
X = PR.gen()
a11 = (a^2)*(X^3)+(a^11)*(X^2)+1
a12 = (a)*(X^4)+(a^13)*(X^3)+X+1
a13 = X^2+(a^13)*(X^3)+a*(X^2)+1
a21 = X^3
a22 = X+a
a23 = X^2+X^3+a*X
a31 = (a^12)*X+a*(X^2)
a32 = (a^8)*(X^2)+X^2+X^3
a33 = a*X + (a^2)*(X^3)
M = matrix([[a11,a12,a13],[a21,a22,a23],[a31,a32,a33]])
v = vector([(a^6)*(X^14)+X^13+X,a*(X^2)+(X^3)*(a^11)+X^2+X+a^12,(a^8)*(X^7)+a*(X^2)+(a^12)* (X^13)+X^3+X^2+X+1])
p = (a^2 + a)*X^3 + (a + 1)*X^2 + (a^2 + 1)*X + 1 # is than 6 in the firs code
I'm trying
matrix(PolynomialModRing(p),M)\vector(PolynomialModRing(p),v)
but PolynomialModRing not exist ...
EDIT
another person talk me that I will make
R.<Xbar> = PR.quotient(PR.ideal(p))
# change your formulas to Xbar instead of X
A \ b
# ==> (a^3 + a, a^2, (a^3 + a^2)*Xbar^2 + (a + 1)*Xbar + a^3 + a)
this work fine but Now I'm trying to apply the Chinese Theorem Remainder after the code, then .... I defined
q = X^18 + a*X^15 + a*X^12 + X^11 + (a + 1)*X^2 + a
r = a^3*X^3 + (a^3 + a^2 + a)*X^2 + (a^2 + 1)*X + a^3 + a^2 + a
#p,q and r are relatively prime
and I'm trying ...
crt([(A\b)[0],(A\b)[1],(A\b)[2]],[p,q,r])
but I get
File "element.pyx", line 344, in sage.structure.element.Element.getattr (sage/structure/element.c:3871)
File "misc.pyx", line 251, in sage.structure.misc.getattr_from_other_class (sage/structure/misc.c:1606)
AttributeError: 'PolynomialQuotientRing_field_with_category.element_class' object has no attribute 'quo_rem'
I'm thinking that problem is the change Xbar to X
Here my complete example to integers
from numpy import arange, eye, linalg
#2x-3y+2z=21
#x+4y-z=1
#-x+2y+z=17
A = matrix([[2,-3,2],[1,4,-1],[-1,2,1]])
b=vector([21,1,17])
p=[17,11,13]
d=det(A)
dlist=[0,0,0]
ylist=matrix(IntegerModRing(p[i]),[[2,-3,2],[1,4,-1], [-1,2,1]])\vector(IntegerModRing(p[i]),[21,1,17])
p1=[int(ylist[0]),int(ylist[1]),int(ylist[2])]
CRT(p1,p)

Maybe... this is what you want? Continuing your example:
G = F.extension(p) # This is what you want for "PolynomialModRing(p)
matrix(G,M)\vector(G,v)
which outputs
(a^3 + a, a^2, (a^3 + a^2)*X^2 + (a + 1)*X + a^3 + a)
In your question you ask "where the system coefficients are polynomials over finite field in any indeterminate" so what I'm doing above is NOT what you have actually asked, which would be a weird question to ask given your example. So, I'm going to just try to read your mind... :-)

Related

SICP Exercise 1.19 PQ transformation

T(p,q) transforms the pair (a,b) according to a <-- bq + aq + ap and b <-- bp + aq.
Can someone explain how this transformation works?
Just as you wrote,
T(p,q)
(a, -----------> ( bq + aq + ap ,
b) bp + aq )
In pseudocode,
T(p,q)(a,b) = ( b*q + a*q + a*p , b*p + a*q )
The transformation T(p,q), given a pair (a,b), calculates two new values,
a2 = b*q + a*q + a*p , and
b2 = b*p + a*q
and then constructs a pair, (a2,b2), and returns it as the result.

Multiplication over GF(256) in SAGE

I am trying to reproduce the multiplication over GF(256) of this question. Specifically, I am trying d4*02 in sage. According to the authors, this multiplication is 饾煼饾煻饾煼饾煼饾煻饾煻饾煼饾煼. In Sage I tried
k.<a> = FiniteField(256, impl='givaro', repr='int')
print(k((a**2+a**4+a**6+a**7)*(a))) # a**2+a**4+a**6+a**7 is d4 and a is 2
181
But 181 is not equal to 饾煼饾煻饾煼饾煼饾煻饾煻饾煼饾煼. What I am doing wrong? Could you help me, please?
You need to give your finite field constructor the correct modulus for Rijndael.
# Rijndael finite field
k.<a> = GF(2^8, modulus=x^8+x^4+x^3+x+1)
r = (a^7 + a^6 + a^4 + a^2) * a
v = r.integer_representation()
print(r, v, hex(v))
Output
a^7 + a^5 + a^4 + a + 1 179 0xb3
It's usually more convenient to specify field elements using integers. Eg,
# Rijndael finite field
k.<a> = GF(2^8, modulus=x^8+x^4+x^3+x+1)
kint = k._cache.fetch_int
p, q = [kint(u) for u in (0xd4, 0x02)]
r = p * q
v = r.integer_representation()
print(r, v, hex(v))
Alternatively, you can use a list and a dict to convert integers to and from field elements. The following code does the second multiplication from the linked question.
# Rijndael finite field
k.<a> = GF(2^8, modulus=x^8+x^4+x^3+x+1)
i2f = sorted(k)
f2i = {v: i for i, v in enumerate(i2f)}
p, q = [i2f[u] for u in (0xbf, 0x03)]
print(p)
print(q)
r = p * q
v = f2i[r]
print(r, v, hex(v))
Output
a^7 + a^5 + a^4 + a^3 + a^2 + a + 1
a + 1
a^7 + a^6 + a^4 + a^3 + a 218 0xda

Boolean algebra simplification exercise

So I have been given the following expression, but I cannot seem to solve it, can anyone do this and show the steps please?
Prove XY'Z + XYZ' + XYZ = XY + XZ
XY'Z + XYZ' + XYZ = XY + XZ
Notice X and Z are common factors between XY'Z and XYZ.
XZ(Y' + Y) + XYZ' =
Y' + Y is equal to 1 (if Y=0 then Y'=1 and so 0 + 1 = 1, that is 0 or 1 = 1. Similarly, if Y=1 then Y'=0 and so 1 + 0 = 1). Therefore, what you get is:
XZ路1 + XYZ' =
XZ路1 = XZ since A路1 = A (if A=0 then 0路1 is 0 and if A=1 then 1路1 = 1). Now the function is simplified to:
XZ + XYZ' =
Notice once again X is a common factor between XZ and XYZ'.
X(Z + YZ') =
Notice this time that Z + YZ' is a special case of the distributive law, which is A + A'B = A + B. This is because if we apply the general distributive law A + BC = (A + B)路(A + C) then we get A + A'B = (A + A')路(A + B) = 1路(A + B) = A + B. Following this reasoning we get to simplify the function even further:
X(Z + Y) =
All that's left is for us to use the distributive law and we finally arrive to the final result:
XY + XZ
Please note that nothing is written between variables, an AND operator (or "路" symbol) is assumed. It's just a way to save space.

Find possible values

I want to verify a formula of the form:
Exists p . ForAll x != 0 . f(x, p) > 0 and g(x, p) < 0
All variables are reals.
As suggested here, I add this list to the solver:
[ForAll([x0, x1],
Implies(Or(x0 != 0, x1 != 0),
And(P0*x0*x0 + P1*x0*x1 + P2*x0*x1 + P3*x1*x1 > 0,
-2*P0*x0*x1 + P1*x0*x0 - P1*x0*x1 - P1*x1*x1 + P2*x0*x0 - P2*x0*x1 - P2*x1*x1 + 2*P3*x0*x1 - 2*P3*x1*x1 < 0
)
)
)
]
The solver with the above formula returns unsat. A possible solution is for P to be [[1.5, -0.5], [-0.5, 1]] and in fact, by substituting those values, the formula is satisfied:
And(3/2*x0*x0 - 1*x0*x1 + x1*x1 > 0,
-1*x0*x0 - 1*x1*x1 < 0)
Is there a way to actually compute such a p? If it's hard for z3, is there any alternative for this problem?
When you say 'Exists' followed by 'Forall', then you are saying that the formula should be true for every such x0, x1. And Z3 is telling you that is simply not the case.
If you are interested in finding one such P, and corresponding x values, simply drop the quantification and make everything a top-level variable:
from z3 import *
def f(x0, x1, P0, P1, P2, P3):
return P0*x0*x0 + P1*x0*x1 + P2*x0*x1 + P3*x1*x1
def g(x0, x1, P0, P1, P2, P3):
return -2*P0*x0*x1 + P1*x0*x0 - P1*x0*x1 - P1*x1*x1 + P2*x0*x0 - P2*x0*x1 - P2*x1*x1 + 2*P3*x0*x1 - 2*P3*x1*x1
p0, p1, p2, p3 = Reals('p0 p1 p2 p3')
x0, x1 = Reals('x0 x1')
fmls = [Implies(Or(x0 != 0, x1 != 0), And(f(x0, x1, p0, p1, p2, p3) > 0, g(x0, x1, p0, p1, p2, p3) < 0))]
while True:
s = Solver()
s.add(fmls)
res = s.check()
print res
if res == sat:
m = s.model()
print m
fmls += [Or(p0 != m[p0], p1 != m[p1])]
else:
print "giving up"
break
When I run this, I get:
sat
[x0 = 1/8, p0 = -1/2, p1 = -1/2, x1 = 1/2, p2 = 1, p3 = 1]
and many others; which is I believe what you're after.
Note that you can also do some programming to get rid of the existential quantification depending on where you are; i.e., start with the quantified version, if you get an unsat, then switch to a new solver and use the unquantified version to automate this process. Of course, this is just programming and doesn't really have anything to do with z3 at this point.

Defining a particular polynomial ring in some CAS (Computer Algebra System)

I'm interested in defining the following polynomial quotient ring in some CAS (Singular, GAP, Sage, etc.):
R = GF(256)[x] / (x^4 + 1)
Specifically, R is the set of all polynomials of degree at most 3, whose coefficients belong to GF(256). Two examples include:
p(x) = {03}x^3 + {01}x^2 + {01}x + {02}
q(x) = {0B}x^3 + {0D}x^2 + {09}x + {0E}
Addition and multiplication are defined as the per ring laws. Here, I mention them for emphasis:
Addition: The corresponding coefficients are XOR-ed (the addition law in GF(256)):
p(x) + q(x) = {08}x^3 + {0C}x^2 + {08}x + {0C}
Multiplication: The polynomials are multiplicated (coefficients are added and multiplicated in GF(256)). The result is computed modulo x^4 + 1:
p(x) * q(x) = ({03}*{0B}x^6 + ... + {02}*{0E}) mod (x^4 + 1)
= ({03}*{0B}x^6 + ... + {02}*{0E}) mod (x^4 + 1)
= ({1D}x^6 + {1C}x^5 + {1D}x^4 + {00}x^3 + {1D}x^2 + {1C}x + {1C}) mod (x^4 + 1)
= {01}
Please tell me how to define R = GF(256)[x] / (x^4 + 1) in a CAS of your choice, and show how to implement the above addition and multiplication between p(x) and q(x).

Resources