# How to define function Anonymous

I want to generate a random function from $GF(2)^4$ to $GF(2)^4$. So my function takes 4 bit input value and 4 bit output value. Also how to define if my function is say $f(x_1,x_2,x_3,x_4)=(x_1 \oplus x_2, x_3, x_4,x_1)$?

edit retag close merge delete

What do you mean by random? You want it uniformly among all possible functions?

Yes, it is taken uniformly among all possible functions.

Sort by » oldest newest most voted

We can create a random function as a substitution table (over arbitrary Cartesian power d of a finite object K, also doing some coersing):

import random
from sage.combinat.cartesian_product import CartesianProduct_iters

def get_random_function(K,d):
E = [tuple(e) for e in CartesianProduct_iters(*([K]*d))]
print(E)
D = dict( zip(E,random.choices(E,k=len(E))) )
def rf(x):
return D[tuple(K(e) for e in x)]
return rf


Now, we get get two particular random function my_func1 and my_func2, and evaluate them on all elements of $GF(2)^4$:

my_func1 = get_random_function(GF(2),4)
print([my1_func(x) for x in CartesianProduct_iters(*([GF(2)]*4))])
my_func2 = get_random_function(GF(2),4)
print([my_func2(x) for x in CartesianProduct_iters(*([GF(2)]*4))])


Answer to the second question is straightforward:

def f(x):
y = (x+x, x, x, x)
return (GF(2)(e) for e in y)
print( f( (1,1,1,1) ) )

more