我是完全新編程,我發現這個代碼在網絡中我知道它的語法是正確的,但我不知道如何調用SAES_FromStateMatrix
函數,所以請幫助我 這裏是代碼在Python中調用函數
F = GF(2);
L.<a> = GF(2^4);
V = L.vector_space();
VF8 = VectorSpace(F, 8);
MixColumns_matrix = Matrix(L, [[1,a^2],[a^2,1]]);
InverseMixColumns_matrix = MixColumns_matrix.inverse();
SBox_matrix = Matrix(L,
[
[ 1 + a^3, a^2, a + a^3, 1 + a + a^3],
[ 1 + a^2 + a^3, 1, a^3, 1 + a^2],
[ a + a^2, a, 0, 1 + a],
[ a^2 + a^3, a + a^2 + a^3, 1 + a + a^2 + a^3, 1 + a + a^2]
]);
InverseSBox_matrix = Matrix(L,
[
[ a + a^3, 1 + a^2, 1 + a^3, 1 + a + a^3],
[ 1, 1 + a + a^2, a^3, 1 + a + a^2 + a^3],
[ a + a^2, 0, a, 1 + a],
[ a^2 + a^3, a^2, 1 + a^2 + a^3, a + a^2 + a^3]
]);
RCON = [
VF8([F(0), F(0), F(0), F(0), F(0), F(0), F(0), F(1)]),
VF8([F(0), F(0), F(0), F(0), F(1), F(1), F(0), F(0)])
];
def SAES_ToStateMatrix(block):
B = block
S00 = L(V([B[0], B[1], B[2], B[3]]));
S01 = L(V([B[4], B[5], B[6], B[7]]));
S10 = L(V([B[8], B[9], B[10], B[11]]));
S11 = L(V([B[12], B[13], B[14], B[15]]));
state_matrix = Matrix(L, [[S00,S01],[S10,S11]]);
return state_matrix;
def SAES_FromStateMatrix(state_matrix):
output = [];
for r in xrange(2):
for c in xrange(2):
v = V(state_matrix[r,c]);
for j in xrange(4):
output.append(Integer(v[j]));
return output;
'L. '這不是有效的Python語法,AFAIK(第2行) –
我也懷疑第3行。雖然有效,但不需要在Python中的每行之後放置';'。 – MacUsers
爲了Python感知但Sage沒有經驗的讀者的利益:'L. = GF(2^4)'是有效的Sage語法。 Sage preparser將其轉換爲'L = GF(Integer(2)** Integer(4),names =('a',)); (a,)= L._first_ngens(1)'幕後。 – DSM