walsh-anf.c 2.47 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
int walshspectrum(int * spectrum, int * sbox, int extractor, int dimension)
{
	int size = (1<<dimension);
	int i,j,k,tmp,lin;
	
	for (i=0;i<size;i++)
	{
		spectrum[i] = (__builtin_popcount(sbox[i] & extractor) % 2)
		            ? -1
		            : 1;
	}

	/* walsh spectrum */
	
	int step = 1;
	for (i=0;i<dimension;i++)
	{
		for (k=0;k<size;k+=(step<<1))
			for (j=0;j<step;j++)
			{
				tmp = spectrum[j+k] + spectrum[j+k+step];
				spectrum[j+k+step] = spectrum[j+k] - spectrum[j+k+step];
				spectrum[j+k] = tmp;
			}
         step <<= 1;
	}

	lin = 0;
	for (i=0;i<size;i++)
	{
		if (spectrum[i] < 0)
		{
			if (- spectrum[i] > lin) lin = - spectrum[i];
		}
		else
		{
			if (spectrum[i] > lin)   lin = spectrum[i];
		}
	}
	return lin;
}

// returns degree of the ANF

int anf(int * ANF, int * sbox, int extractor, int dimension, int verbose)
{
	int size = (1<<dimension);
	int M[size][dimension+1];
	int i, j, k, step;
	int w, max_deg = 0;

    for (i=0;i<size;i++)
	{
		M[i][0] = __builtin_popcount(sbox[i] & extractor) % 2;
	}

    /* ANF */

    step = 1;
    for (i=0;i<dimension;i++)
    {

        for (k=0;k<size;k+=(step<<1))
        {
            for (j=0;j<step;j++)
            {
                // printf("combining pair (%d,%d)\n",j+k,j+k+step); fflush (stdout);
                M[j+k     ][i+1] = M[j+k][i];
                M[j+k+step][i+1] = (M[j+k][i] + M[j+k+step][i]) % 2;
            }
        }

        step <<= 1;
    }
    
	if (verbose)
	{
		for (i=0;i<size;i++)
		{
			for (j=size/2;j>0;j>>=1)
				printf( (i&j) ? "1" : "0");
			for (j=0; j <= dimension; j++)
				printf(" & %3d ", M[i][j]);
			printf(" \\\\\n");
		}
	}
	
	for (i=0;i<size;i++)
	{
		ANF[i] = M[i][dimension];
		if (ANF[i])
		{
			w = __builtin_popcount(i);
			if (w > max_deg) max_deg = w;
		}
	}
	
	return max_deg;
	
}

int linearity(int * sbox, int dimension)
{
	int size = (1<<dimension);
	int i,maxlin=0;
	int spectrum[size];

	for (i=1;i<size;i++)
	{
		int lin = walshspectrum(spectrum, sbox, i, dimension);
		if (lin < 0) lin = -lin;
		if (lin > maxlin) maxlin = lin;
	}
	return maxlin;
}

void flippedbits(int * flips, int * counts, int * sbox, int dimension)
{
	int i,j;
	int size = 1<<dimension;
	int mask;
	
	for (i=0,mask=1; i<dimension; i++,mask<<=1)
	{
		flips[i] = dimension;
		counts[i] = 0;
		for (j=0;j<size;j++)
		{
			if (!(j & mask))
			{
				int flip = __builtin_popcount(sbox[j]^sbox[j|mask]);
				if (flip == flips[i]) counts[i]++;
				if (flip < flips[i]) { flips[i] = flip; counts[i] = 1; }
			}
		}
	}
}