سؤال

guys when calling the first function as SAES_ToStateMatrix([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]) it gives me

[a^3 + a a^3 + a]
[a^3 + a a^3 + a]

but by passing the output as SAES_FromStateMatrix([[a^3 + a a^3 + a],[a^3 + a a^3 + a]]) an error appeared " invalid syntax " so how to avoid this error

here is my code

F = GF(2);
L.<a> = GF(2^4);
V = L.vector_space();
VF8 = VectorSpace(F, 8);

def SAES_ToStateMatrix(block):
    r"""
    Converts a bit list into an SAES State Matrix
    """
    B = block;

    # form the plaintext block into a matrix of GF(2^n) elements
    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):
    r"""
    Converts an SAES state_matrix to a bit list.
    """

    output = [];

    # convert state matrix back into bit list
    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;
هل كانت مفيدة؟

المحلول

It seems to me you got confused about the string representation of an object and the object itself.

In short: Pass in the actual result of the function, not its string representation:

mat = SAES_ToStateMatrix([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
SAES_FromStateMatrix(mat)

Explanation

When you enter something in an interactive Python interpreter (which the sage environment is, plus some more math bells and whistles), it evaluates the python expression you entered, and prints the string representation of the result on the console.

So, for example, if you create a dict and just enter its name, Python prints the string representation of that dict:

>>> d = dict(foo=1, bar=2)
>>> d
{'foo': 1, 'bar': 2}

This is also called a read-eval-print-loop (REPL).

Now, for a dictionary the string representation of it happens to be exactly what the syntax for the literal definition of a dictionary is({key: value, ...}. See below for more on literals.

However, this is not true for all objects. For common Python types it's a convention that their string representation is something, that when evaluated in a Python interpreter, will recreate an object with the same state. But that's just a convention, not something you can rely on, and it simply wouldn't be possible for more complex objects. For example:

>>> class Foobar(object):
...     def __init__(self):
...         self.attr = 'foo'
...
>>> f = Foobar()
>>> f
<__main__.Foobar object at 0x109ee7750>

Here, the string representation of instances of the Foobar class is <scope.Foobar object at memory_address>. Which is the default string representation for objects.

One can define the string representation by defining the __repr__ method:

>>> class Foobar(object):
...     def __init__(self, value):
...         self.value = value
...     def __repr__(self):
...         return '<Foobar instance with value=%s>' % self.value
...
>>> f = Foobar(42)
>>> f
<Foobar instance with value=42>

So you see, __repr__ can return anything - could be a valid python expression to recreate that object, or not:

For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object.

Literals

For some primitive types, there are so called literals (special symbols) that are ways (built into the syntax of the language) to express common data types.

For example:

Dicts

Form to create a dictionary using the dict() constructor:

>>> dict(foo=1, bar=2)

Form to create a dictionary using the { and } literals:

>>> {'foo': 1, 'bar': 2}

Lists

>>> list((1, 2, 3))

vs. using the [ and ] literals:

>>> [1, 2, 3]

Imaginary numbers

>>> 3.14j

Floats

>>> 3.14e-10

So if you enter {'foo': 1, 'bar': 2} back into the interpreter, and get a dict back, that works because the string representation of a dictionary is exactly the same as the literal form for defining a dictionary.

Sage's Matrix

Now, in the case of sage's Matrix class, it looks like the string representation is some mathematical notation of a matrix, but it's not a valid python expression. How could it be? a is not defined where Python is concerned, so even if it wasn't for the invalid syntax, entering something like [a^3 + a a^3 + a] into Python would cause a NameError, unless a was previously defined with an actual value. So you can't enter algebraic formulas like x = 2 * y into Python either, without actually defining y.

What you need to do is to pass the actual return value of SAES_ToStateMatrix(lst) to SAES_FromStateMatrix(). For example by storing it in a temporary variable first:

mat = SAES_ToStateMatrix([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
SAES_FromStateMatrix(mat)

or passing the function's result in directly, works just as well:

SAES_FromStateMatrix(SAES_ToStateMatrix([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top