chiark / gitweb /
Boring.
[storin] / storin-mktab.c
1 /* -*-c-*-
2  *
3  * $Id: storin-mktab.c,v 1.2 2000/07/02 15:21:20 mdw Exp $
4  *
5  * Search for invertible matrices
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 AUTHORS 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: storin-mktab.c,v $
52  * Revision 1.2  2000/07/02 15:21:20  mdw
53  * Fix licence text.
54  *
55  * Revision 1.1  2000/05/21 11:28:30  mdw
56  * Initial check-in.
57  *
58  */
59
60 /*----- Header files ------------------------------------------------------*/
61
62 #include <stdio.h>
63 #include <stdlib.h>
64
65 #include "bits.h"
66
67 #include "dsarand.h"
68 #include "sha.h"
69
70 #include "arith24.h"
71 #include "matrix.h"
72
73 /*----- Main code ---------------------------------------------------------*/
74
75 int main(int argc, char *argv[])
76 {
77   dsarand r;
78   int i, j;
79   uint24 d[16], a[16];
80
81   /* --- Make the seed string --- */
82
83   {
84     sha_ctx s;
85     octet h[SHA_HASHSZ];
86     const char *p = "matrix-seed-string";
87     size_t sz;
88
89     if (argv[1])
90       p = argv[1];
91     sz = strlen(p);
92     sha_init(&s);
93     sha_hash(&s, p, sz);
94     sha_done(&s, h);
95     dsarand_init(&r, h, sizeof(h));
96     r.passes = 2;
97   }
98
99   /* --- Make a matrix --- */
100
101   for (;;) {
102     octet b[4];
103
104     for (i = 0; i < 16; i++) {
105       octet w[3];
106       dsarand_fill(&r, w, sizeof(w));
107       a[i] = LOAD24(w);
108     }
109
110     /* --- Ensure the LSBs have appropriate properties --- */
111
112     for (i = 0; i < 4; i++) {
113       int bb = -1;
114       for (j = 0; j < 4; j++) {
115         if ((a[i * 4 + j] & 1) == 0) {
116           if (bb == -1)
117             bb = j;
118           else
119             goto reject;
120         }
121       }
122       if (bb == -1)
123         goto reject;
124       for (j = 0; j < i; j++) {
125         if (b[j] == bb)
126           goto reject;
127       }
128       b[i] = bb;
129     }
130
131     /* --- Ensure the matrix is invertible (and invert it) --- */
132
133     if (matinv(d, a, 4, 4) == 0)
134       break;
135   reject:;
136   }
137
138   fputs("\
139 /* -*-c-*-\n\
140  *\n\
141  * Tables for Storin [generated]\n\
142  */\n\
143 \n\
144 #ifndef STORIN_TAB_H\n\
145 #define STORIN_TAB_H\n\
146 \n\
147 #define STORIN_M {                                                      \\\n\
148   ", stdout);
149
150   for (i = 0; i < 16; i++) {
151     printf("0x%06x", a[i]);
152     if (i == 15)
153       fputs("                           \\\n}\n\n", stdout);
154     else if (i % 4 == 3)
155       fputs(",                          \\\n  ", stdout);
156     else
157       fputs(", ", stdout);
158   }
159
160   fputs("\
161 #define STORIN_MI {                                                     \\\n\
162   ", stdout);
163
164   for (i = 0; i < 16; i++) {
165     printf("0x%06x", d[i]);
166     if (i == 15)
167       fputs("                           \\\n}\n\n", stdout);
168     else if (i % 4 == 3)
169       fputs(",                          \\\n  ", stdout);
170     else
171       fputs(", ", stdout);
172   }
173
174   fputs("#endif\n", stdout);
175
176   if (fclose(stdout)) {
177     fputs("error writing data\n", stderr);
178     exit(EXIT_FAILURE);
179   }
180
181   return (0);
182 }
183
184 /*----- That's all, folks -------------------------------------------------*/