3 * $Id: sac.c,v 1.1 2000/05/21 11:28:30 mdw Exp $
5 * Testing for strict avalanche
7 * (c) 2000 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * Copyright (c) 2000 Mark Wooding
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
22 * 2, Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * 3. The name of the authors may not be used to endorse or promote
27 * products derived from this software without specific prior written
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
31 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
33 * NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
42 * Instead of accepting the above terms, you may redistribute and/or modify
43 * this software under the terms of either the GNU General Public License,
44 * or the GNU Library General Public License, published by the Free
45 * Software Foundation; either version 2 of the License, or (at your
46 * option) any later version.
49 /*----- Revision history --------------------------------------------------*
52 * Revision 1.1 2000/05/21 11:28:30 mdw
57 /*----- Header files ------------------------------------------------------*/
67 #include "storin-tab.h"
69 /*----- Static variables --------------------------------------------------*/
73 /*----- The constant matrix -----------------------------------------------*/
75 static const uint24 m[] = STORIN_M;
77 /*----- Magic numbers -----------------------------------------------------*/
81 #define ROT(x) ((x) ^ ((x) >> 12))
82 /* #define ROT(x) ROL24(x, 7) */
84 /*----- Main code ---------------------------------------------------------*/
88 #define HAMWEIGHT(x) (w[U8((x) >> 16)] + w[U8((x) >> 8)] + w[U8(x)])
90 static void haminit(void) {
92 for (i = 0; i < 256; i++) {
95 for (j = 0; j < 8; j++) {
103 static void eblk(uint24 *x)
108 for (i = 0; i < ROUNDS; i++) {
110 matmul(t, m, x, 4, 4, 1);
111 for (j = 0; j < 4; j++)
116 typedef struct stats {
122 static void probe(unsigned bit, uint24 *delta, struct stats *ps)
126 struct stats s = { 0, 0, 0 };
128 for (i = 0; i < PROBES; i++) {
130 for (j = 0; j < 4; j++) {
131 x[j] = U24(fibrand_step(&r));
132 y[j] = x[j] ^ delta[j];
137 for (j = 0; j < 4; j++) {
138 uint24 ww = x[j] ^ y[j];
141 /* printf("%06x %06x %06x %06x -> %06x %06x %06x %06x\n", */
142 /* delta[0], delta[1], delta[2], delta[3], */
143 /* x[0] ^ y[0], x[1] ^ y[1], x[2] ^ y[2], x[3] ^ y[3]); */
144 s.sx += h; ps->sx += h;
145 s.sx2 += h * h; ps->sx2 += h * h;
150 double mean = s.sx / s.n;
151 double var = s.sx2 / s.n - mean * mean;
152 printf("bit %u: mean = %g, sd = %g\n", bit, mean, sqrt(var));
158 uint24 delta[4] = { 0 };
160 struct stats s = { 0, 0, 0 };
164 fibrand_lcseed(&r, 0);
166 for (i = 0; i < 96; i++) {
167 uint24 *dd = delta + i / 24;
168 uint24 m = 1 << (i % 24);
175 var = s.sx2 / s.n - mean * mean;
176 printf("\nsummary: mean = %g, sd = %g\n", mean, sqrt(var));
181 /*----- That's all, folks -------------------------------------------------*/