2 * Copyright (c) 2014, James S. Plank and Kevin Greenan
5 * Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure
8 * Revision 2.0: Galois Field backend now links to GF-Complete
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * - Neither the name of the University of Tennessee nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
33 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
36 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 /* Jerasure's authors:
42 Revision 2.x - 2014: James S. Plank and Kevin M. Greenan.
43 Revision 1.2 - 2008: James S. Plank, Scott Simmerman and Catherine D. Schuman.
44 Revision 1.0 - 2007: James S. Plank.
52 #define talloc(type, num) (type *) malloc(sizeof(type)*(num))
54 static void usage(char *s)
56 fprintf(stderr, "usage: jerasure_03 k w - Creates a kxk Cauchy matrix in GF(2^w). \n\n");
57 fprintf(stderr, " k must be < 2^w. Element i,j is 1/(i+(2^w-j-1)). (If that is\n");
58 fprintf(stderr, " If that is 1/0, then it sets it to zero). \n");
59 fprintf(stderr, " It then tests whether that matrix is invertible.\n");
60 fprintf(stderr, " If it is invertible, then it prints out the inverse.\n");
61 fprintf(stderr, " Finally, it prints the product of the matrix and its inverse.\n");
62 fprintf(stderr, " \n");
63 fprintf(stderr, "This demonstrates: jerasure_print_matrix()\n");
64 fprintf(stderr, " jerasure_invertible_matrix()\n");
65 fprintf(stderr, " jerasure_invert_matrix()\n");
66 fprintf(stderr, " jerasure_matrix_multiply().\n");
67 if (s != NULL) fprintf(stderr, "%s\n", s);
71 int main(int argc, char **argv)
73 unsigned int k, w, i, j, n;
79 if (argc != 3) usage(NULL);
80 if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
81 if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 31) usage("Bad w");
82 if (k >= (1 << w)) usage("K too big");
84 matrix = talloc(int, k*k);
85 matrix_copy = talloc(int, k*k);
86 inverse = talloc(int, k*k);
88 for (i = 0; i < k; i++) {
89 for (j = 0; j < k; j++) {
90 n = i ^ ((1 << w)-1-j);
91 matrix[i*k+j] = (n == 0) ? 0 : galois_single_divide(1, n, w);
95 printf("<HTML><TITLE>jerasure_03");
96 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
98 printf("<h3>jerasure_03");
99 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
103 printf("The Cauchy Matrix:\n");
104 jerasure_print_matrix(matrix, k, k, w);
105 memcpy(matrix_copy, matrix, sizeof(int)*k*k);
106 i = jerasure_invertible_matrix(matrix_copy, k, w);
107 printf("\nInvertible: %s\n", (i == 1) ? "Yes" : "No");
109 printf("\nInverse:\n");
110 memcpy(matrix_copy, matrix, sizeof(int)*k*k);
111 i = jerasure_invert_matrix(matrix_copy, inverse, k, w);
112 jerasure_print_matrix(inverse, k, k, w);
113 identity = jerasure_matrix_multiply(inverse, matrix, k, k, k, k, w);
114 printf("\nInverse times matrix (should be identity):\n");
115 jerasure_print_matrix(identity, k, k, w);