chiark / gitweb /
Improve analysis section.
[storin] / diffan.c
1 /* -*-c-*-
2  *
3  * $Id: diffan.c,v 1.1 2000/05/21 11:28:30 mdw Exp $
4  *
5  * Differential analysis of matrix multiplication
6  *
7  * (c) 2000 Mark Wooding
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * Copyright (c) 2000 Mark Wooding
13  * All rights reserved.
14  * 
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are
17  * met:
18  * 
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 
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.
25  * 
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
28  *    permission.
29  * 
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.
41  * 
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.
47  */
48
49 /*----- Revision history --------------------------------------------------* 
50  *
51  * $Log: diffan.c,v $
52  * Revision 1.1  2000/05/21 11:28:30  mdw
53  * Initial check-in.
54  *
55  */
56
57 /*----- Header files ------------------------------------------------------*/
58
59 #include <stdio.h>
60 #include <stdlib.h>
61
62 #include "bits.h"
63 #include "sym.h"
64 #include "fibrand.h"
65 #include "matrix.h"
66 #include "storin-tab.h"
67
68 /*----- The constant matrix -----------------------------------------------*/
69
70 static const uint24 m[] = STORIN_M;
71
72 /*----- Magic numbers -----------------------------------------------------*/
73
74 #define PROBES 8192
75 #define EXHAUST 4
76
77 /*----- Static variables --------------------------------------------------*/
78
79 static fibrand r;
80
81 /*----- Main code ---------------------------------------------------------*/
82
83 typedef struct {
84   sym_base b;
85   unsigned n;
86 } diff;
87
88 static void probe(uint24 *delta)
89 {
90   unsigned i, j;
91   unsigned max = 0;
92   uint24 mout[4];
93   sym_table t;
94
95   sym_create(&t);
96
97   for (i = 0; i < PROBES; i++) {
98     uint24 x[4], y[4];
99     uint24 xi[4], yi[4];
100     uint24 dd[4];
101     octet db[12];
102     diff *p;
103     unsigned c;
104
105     for (j = 0; j < 4; j++) {
106       x[j] = U24(fibrand_step(&r));
107       y[j] = x[j] & delta[j];
108     }
109
110     matmul(xi, m, x, 4, 4, 1);
111     matmul(yi, m, y, 4, 4, 1);
112
113     for (j = 0; j < 4; j++)
114       dd[j] = xi[j] ^ yi[j];
115
116     STORE24(db + 0, dd[0]);
117     STORE24(db + 3, dd[1]);
118     STORE24(db + 6, dd[2]);
119     STORE24(db + 9, dd[3]);
120
121     p = sym_find(&t, (char *)db, 12, sizeof(*p), &c);
122     if (!c)
123       p->n = 1;
124     else
125       p->n++;
126     if (p->n > max) {
127       max = p->n;
128       for (j = 0; j < 4; j++)
129         mout[j] = dd[j];
130     }
131   }
132
133   sym_destroy(&t);
134
135   if (max > 1) {
136     printf("%06x %06x %06x %06x -> %06x %06x %06x %06x: %u\n",
137            delta[0], delta[1], delta[2], delta[3],
138            mout[0], mout[1], mout[2], mout[3], max);
139   }
140 }
141
142 static void rdiff(uint24 *delta, unsigned i, unsigned n)
143 {
144   if (!n) {
145     probe(delta);
146     return;
147   }
148   for (; i < 96; i++) {
149     uint24 *dd = delta + i / 24;
150     uint24 m = 1 << (i % 24);
151     *dd ^= m;
152     rdiff(delta, i + 1, n - 1);
153     *dd ^= m;
154   }
155 }
156
157 static void bitdiffs(unsigned n)
158 {
159   uint24 delta[4] = { 0 };
160   rdiff(delta, 0, n);
161   probe(delta);
162 }
163
164 int main(void)
165 {
166   unsigned i, j;
167   uint24 delta[4];
168
169   fibrand_lcseed(&r, 0);
170
171   for (i = 1; i <= EXHAUST; i++)
172     bitdiffs(i);
173
174   printf("*** ok, trying random probing\n");
175
176   for (;;) {
177     for (j = 0; j < 4; j++)
178       delta[j] = U24(fibrand_step(&r));
179     probe(delta);
180   }
181
182   return (0);
183 }
184
185 /*----- That's all, folks -------------------------------------------------*/