Multiplication over GF(256) in SAGE - 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

Related

How can I plot frequency response for a vibratory system with nonlinear differential equations?

I have a system of nonlinear differential equations for a 3 degree of freedom vibratory system.
system of differential equations
First I want to plot y, y_L and y_R against time (for a given value for Omega) and then I want to plot the domains (max values of y, y_L and y_R) against various amounts of Omega.
Unfortunately, I am not good at Octave. I have written the following code in Octave (based on a sample given by one of the users), but it ends with this error: "anonymous function bodies must be single expressions".
I would be grateful if anyone can help me.
Here is the code:
Me = 4000;
me = 20;
c = 2000;
c1 = 700;
c2 = 700;
k = 20000;
k1 = 250000;
k2 = 20000;
a0 = 0.01;
om = 25;
mu1 = (c+2*c2)/(Me);
mu2 = (c2)/(Me);
mu3 = (c1+c2)/(me);
mu4 = (c2)/(me);
w12 = (2*k2)/(Me);
w22 = (k1+k2)/(me);
a1 = (k2)/(me);
a2 = (k)/(Me);
F0 = (k1*a0)/(Me);
couplode = #(t,y) [y(2); mu4*y(4) - mu3*y(2) - w22*y(1) + a1*y(3) + F0*cos(om*t); y(4); mu2*(y(2)+y(6)) - mu1*y(4) - w12*y(3) + 0.5*w12*(y(1)+y(5)) + a2((y(3)).^3; y(6); mu4*y(4) - mu3*y(6) - w22*y(5) + a1*y(3) + F0*cos(om*t)];
[t,y] = ode45(couplode, [0 0.49*pi], [1;1;1;1;1;1]*1E-8);
figure(1)
plot(t, y)
grid
str = {'$$ \dot{y_L} $$', '$$ y_L $$', '$$ \dot{y} $$', '$$ y $$', '$$ \dot{y_R} $$', '$$ y_R $$'};
legend(str, 'Interpreter','latex', 'Location','NW')
You have a strange term rather at the end of the vector definition
... + a2((y(3)).^3
You certainly meant
... + a2*y(3).^3
You get better visibility and easier debugging by breaking that into separate lines
couplode = #(t,y) [ y(2);
mu4*y(4)-mu3*y(2)-w22*y(1)+a1*y(3)+F0*cos(om*t);
y(4);
mu2*(y(2)+y(6)) - mu1*y(4) - w12*y(3) + 0.5*w12*(y(1)+y(5)) + a2*y(3).^3;
y(6);
mu4*y(4)-mu3*y(6)-w22*y(5)+a1*y(3)+F0*cos(om*t)];
At least in this form, spaces or no spaces makes no difference. In general in matlab/octave [a +b -c] is the same as [a, +b, -c], so one has to be careful that the expression is not interpreted as matrix row. Spaces on both sites of the operation sign switches back to the single-expression interpretation.

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.

Unclassified statement at (1) in a mathematical expression

My first Fortran lesson is to plot the probability density function of the radial Sturmian functions. In case you are interested, the radial Sturmian functions are used to graph the momentum space eigenfunctions for the hydrogen atom.
In order to produce these radial functions, one needs to first produce some polynomials called the Gegenbauer polynomials, denoted
Cba(x),
where a and b should be stacked atop each other. One needs these polynomials because the Sturmians (let's call them R_n,l) are defined like so,
R_n,l(p) = N pl⁄(p2 + k2)l+2 Cn - l - 1l + 1(p2 - k2⁄p2 + k2),
where N is a normalisation constant, p is the momentum, n is the principle quantum number, l is the angular momentum and k is a constant. The normalisation constant is there so that when I come to square this function, it will produce a probability distribution for the momentum of the electron in a hydrogen atom.
Gegenbauer polynomials are generated using the following recurrence relation:
Cnl(x) = 1⁄n[2(l + n - 1) x Cn - 1l(x) - (2l + n - 2)Cn - 2l(x)],
with C0l(x) = 1 and C1l(x) = 2lx, as you may have noticed, l is fixed but n is not. At the start of my program, I will specify both l and n and work out the Gegenbauer polynomial I need for the radial function I wish to plot.
The problems I am having with my code at the moment are all in my subroutine for working out the value of the Gegenbauer polynomial Cn-l-1l+1(p2 - k2⁄p2 + k2) for incremental values of p between 0 and 3. I keep getting the error
Unclassified statement at (1)
but I cannot see what the issue is.
program Radial_Plot
implicit none
real, parameter :: pi = 4*atan(1.0)
integer, parameter :: top = 1000, l = 50, n = 100
real, dimension(1:top) :: x, y
real increment
real :: a=0.0, b = 2.5, k = 0.3
integer :: i
real, dimension(1:top) :: C
increment = (b-a)/(real(top)-1)
x(1) = 0.0
do i = 2, top
x(i) = x(i-1) + increment
end do
Call Gegenbauer(top, n, l, k, C)
y = x*C
! y is the function that I shall be plotting between values a and b.
end program Radial_Plot
Subroutine Gegenbauer(top1, n1, l1, k1, CSub)
! This subroutine is my attempt to calculate the Gegenbauer polynomials evaluated at a certain number of values between c and d.
implicit none
integer :: top1, i, j, n1, l1
real :: k1, increment1, c, d
real, dimension(1:top1) :: x1
real, dimension(1:n1 - l1, 1:top1) :: C1
real, dimension(1:n1 - l1) :: CSub
c = 0.0
d = 3.0
k1 = 0.3
n1 = 50
l1 = 25
top1 = 1000
increment1 = (d - c)/(real(top1) - 1)
x1(1) = 0.0
do i = 2, top1
x1(i) = x1(i-1) + increment1
end do
do j = 1, top1
C1(1,j) = 1
C1(2,j) = 2(l1 + 1)(x1(i)^2 - k1^2)/(x1(i)^2 + k1^2)
! All the errors occurring here are all due to, and I quote, 'Unclassifiable statement at (1)', I can't see what the heck I have done wrong.
do i = 3, n1 - l1
C1(i,j) = 2(((l1 + 1)/n1) + 1)(x1(i)^2 - k1^2)/(x1(i)^2 + k1^2)C1(i,j-1) - ((2(l1+1)/n1) + 1)C1(i,j-2)
end do
CSub(j) = Cn(n1 - l1,j)^2
end do
return
end Subroutine Gegenbauer
As francesalus correctly pointed out, the problem is because you use ^ instead of ** for exponentiation. Additionally, you do not put * between the terms you are multiplying.
C1(1,j) = 1
C1(2,j) = 2*(l1 + 1)*(x1(i)**2 - k1**2)/(x1(i)**2 + k1**2)
do i = 3, n1 - l1
C1(i,j) = 2 * (((l1 + 1)/n1) + 1) * (x1(i)**2 - k1**2) / &
(x1(i)**2 + k1**2)*C1(i,j-1) - ((2(l1+1)/n1) + 1) * &
C1(i,j-2)
end do
CSub(j) = Cn(n1 - l1,j)**2
Since you are beginning I have some advice. Learn to put all subroutines and functions to modules (unless they are internal). There is no reason for the return statement at the and of the subroutine, similarly as a stop statement isn't necessary at the and of the program.

Given a list of coefficients, create a polynomial

I want to create a polynomial with given coefficients. This seems very simple but what I have found till now did not appear to be the thing I desired.
For example in such an environment;
n = 11
K = GF(4,'a')
R = PolynomialRing(GF(4,'a'),"x")
x = R.gen()
a = K.gen()
v = [1,a,0,0,1,1,1,a,a,0,1]
Given a list/vector v of length n (I will set this n and v at the begining), I want to get the polynomial v(x) as v[i]*x^i.
(Actually after that I am going to build the quotient ring GF(4,'a')[x] /< x^n-v(x) > after getting this v(x) from above) then I will say;
S = R.quotient(x^n-v(x), 'y')
y = S.gen()
But I couldn't write it.
This is a frequently asked question in many places so it is better to leave it here as an answer although the answer I have is so simple:
I just wrote R(v) and it gave me the polynomial:
sage
n = 11
K = GF(4,'a')
R = PolynomialRing(GF(4,'a'),"x")
x = R.gen()
a = K.gen()
v = [1,a,0,0,1,1,1,a,a,0,1]
R(v)
x^10 + a*x^8 + a*x^7 + x^6 + x^5 + x^4 + a*x + 1
Basically (that is, ignoring the specifics of your polynomial ring) you have a list/vector v of length n and you require a polynomial which is the sum of all v[i]*x^i. Note that this sum equals the matrix product V.X where V is a one row matrix (essentially equal to the vector v) and X is a column matrix consisting of powers of x. In Maxima you could write
v: [1,a,0,0,1,1,1,a,a,0,1]$
n: length(v)$
V: matrix(v)$
X: genmatrix(lambda([i,j], x^(i-1)), n, 1)$
V.X;
The output is
x^10+ax^8+ax^7+x^6+x^5+x^4+a*x+1

Solve Linear System Over Finite Field with Module

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... :-)

Resources