What does this function do? (why do we xor?)

77 Views Asked by At

I stumbled across some source code on GitHub, but the author did not provide any documentation so I'm trying to figure it out by myself.

def s(k,x,mul=gf832_mul):
    if k==0:
        return x
    tmp = s(k-1,x,mul)
    result = mul(tmp,tmp)
return result ^ tmp

It's my understanding that this code just multiplies the second parameter by itself (in galois field) and then xors the result. It does this recursively k times. What I don't understand is why.

For example, for k=2, and x=4 it would be the same as doing this:

((4*4)^4 * (4*4)^4) ^ (4*4)^4

In the first iteration, temp will be 4, and in the second it will be (4*4)^4. If k were 3, temp would be the aforementioned result for k=2 xor with itself. All done in GF(8).

I can understand wanting to find the multiplication of two numbers in a GF, but why would you xor the result?

1

There are 1 best solutions below

2
On BEST ANSWER

The elements of $GF(8)$ are presumably represented as $3$-bit numbers standing for vectors over $GF(2)$. Then XOR is the addition in the field.

When $k=0$ the function returns $x$;

When $k=1$ the function returns $x^2+x$;

When $k=2$ the function returns $(x^2+x)^2+x^2+x$;

and so on, where addition and multiplication are in $GF(8)$.