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