ec_base.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**********************************************************************
  2. Copyright(c) 2011-2015 Intel Corporation All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the
  11. distribution.
  12. * Neither the name of Intel Corporation nor the names of its
  13. contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. **********************************************************************/
  27. #include <limits.h>
  28. #include <string.h> // for memset
  29. #include "erasure_code.h"
  30. #include "ec_base.h" // for GF tables
  31. void ec_init_tables(int k, int rows, unsigned char *a, unsigned char *g_tbls)
  32. {
  33. int i, j;
  34. for (i = 0; i < rows; i++) {
  35. for (j = 0; j < k; j++) {
  36. gf_vect_mul_init(*a++, g_tbls);
  37. g_tbls += 32;
  38. }
  39. }
  40. }
  41. unsigned char gf_mul_erasure(unsigned char a, unsigned char b)
  42. {
  43. #ifndef GF_LARGE_TABLES
  44. int i;
  45. if ((a == 0) || (b == 0))
  46. return 0;
  47. return gff_base[(i = gflog_base[a] + gflog_base[b]) > 254 ? i - 255 : i];
  48. #else
  49. return gf_mul_table_base[b * 256 + a];
  50. #endif
  51. }
  52. unsigned char gf_inv(unsigned char a)
  53. {
  54. #ifndef GF_LARGE_TABLES
  55. if (a == 0)
  56. return 0;
  57. return gff_base[255 - gflog_base[a]];
  58. #else
  59. return gf_inv_table_base[a];
  60. #endif
  61. }
  62. void gf_gen_rs_matrix(unsigned char *a, int m, int k)
  63. {
  64. int i, j;
  65. unsigned char p, gen = 1;
  66. memset(a, 0, k * m);
  67. for (i = 0; i < k; i++)
  68. a[k * i + i] = 1;
  69. for (i = k; i < m; i++) {
  70. p = 1;
  71. for (j = 0; j < k; j++) {
  72. a[k * i + j] = p;
  73. p = gf_mul_erasure(p, gen);
  74. }
  75. gen = gf_mul_erasure(gen, 2);
  76. }
  77. }
  78. void gf_gen_cauchy1_matrix(unsigned char *a, int m, int k)
  79. {
  80. int i, j;
  81. unsigned char *p;
  82. // Identity matrix in high position
  83. memset(a, 0, k * m);
  84. for (i = 0; i < k; i++)
  85. a[k * i + i] = 1;
  86. // For the rest choose 1/(i + j) | i != j
  87. p = &a[k * k];
  88. for (i = k; i < m; i++)
  89. for (j = 0; j < k; j++)
  90. *p++ = gf_inv(i ^ j);
  91. }
  92. int gf_invert_matrix(unsigned char *in_mat, unsigned char *out_mat, const int n)
  93. {
  94. int i, j, k;
  95. unsigned char temp;
  96. // Set out_mat[] to the identity matrix
  97. for (i = 0; i < n * n; i++) // memset(out_mat, 0, n*n)
  98. out_mat[i] = 0;
  99. for (i = 0; i < n; i++)
  100. out_mat[i * n + i] = 1;
  101. // Inverse
  102. for (i = 0; i < n; i++) {
  103. // Check for 0 in pivot element
  104. if (in_mat[i * n + i] == 0) {
  105. // Find a row with non-zero in current column and swap
  106. for (j = i + 1; j < n; j++)
  107. if (in_mat[j * n + i])
  108. break;
  109. if (j == n) // Couldn't find means it's singular
  110. return -1;
  111. for (k = 0; k < n; k++) { // Swap rows i,j
  112. temp = in_mat[i * n + k];
  113. in_mat[i * n + k] = in_mat[j * n + k];
  114. in_mat[j * n + k] = temp;
  115. temp = out_mat[i * n + k];
  116. out_mat[i * n + k] = out_mat[j * n + k];
  117. out_mat[j * n + k] = temp;
  118. }
  119. }
  120. temp = gf_inv(in_mat[i * n + i]); // 1/pivot
  121. for (j = 0; j < n; j++) { // Scale row i by 1/pivot
  122. in_mat[i * n + j] = gf_mul_erasure(in_mat[i * n + j], temp);
  123. out_mat[i * n + j] = gf_mul_erasure(out_mat[i * n + j], temp);
  124. }
  125. for (j = 0; j < n; j++) {
  126. if (j == i)
  127. continue;
  128. temp = in_mat[j * n + i];
  129. for (k = 0; k < n; k++) {
  130. out_mat[j * n + k] ^= gf_mul_erasure(temp, out_mat[i * n + k]);
  131. in_mat[j * n + k] ^= gf_mul_erasure(temp, in_mat[i * n + k]);
  132. }
  133. }
  134. }
  135. return 0;
  136. }
  137. // Calculates const table gftbl in GF(2^8) from single input A
  138. // gftbl(A) = {A{00}, A{01}, A{02}, ... , A{0f} }, {A{00}, A{10}, A{20}, ... , A{f0} }
  139. void gf_vect_mul_init(unsigned char c, unsigned char *tbl)
  140. {
  141. unsigned char c2 = (c << 1) ^ ((c & 0x80) ? 0x1d : 0); //Mult by GF{2}
  142. unsigned char c4 = (c2 << 1) ^ ((c2 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  143. unsigned char c8 = (c4 << 1) ^ ((c4 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  144. #if __WORDSIZE == 64 || _WIN64 || __x86_64__
  145. unsigned long long v1, v2, v4, v8, *t;
  146. unsigned long long v10, v20, v40, v80;
  147. unsigned char c17, c18, c20, c24;
  148. t = (unsigned long long *)tbl;
  149. v1 = c * 0x0100010001000100ull;
  150. v2 = c2 * 0x0101000001010000ull;
  151. v4 = c4 * 0x0101010100000000ull;
  152. v8 = c8 * 0x0101010101010101ull;
  153. v4 = v1 ^ v2 ^ v4;
  154. t[0] = v4;
  155. t[1] = v8 ^ v4;
  156. c17 = (c8 << 1) ^ ((c8 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  157. c18 = (c17 << 1) ^ ((c17 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  158. c20 = (c18 << 1) ^ ((c18 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  159. c24 = (c20 << 1) ^ ((c20 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  160. v10 = c17 * 0x0100010001000100ull;
  161. v20 = c18 * 0x0101000001010000ull;
  162. v40 = c20 * 0x0101010100000000ull;
  163. v80 = c24 * 0x0101010101010101ull;
  164. v40 = v10 ^ v20 ^ v40;
  165. t[2] = v40;
  166. t[3] = v80 ^ v40;
  167. #else // 32-bit or other
  168. unsigned char c3, c5, c6, c7, c9, c10, c11, c12, c13, c14, c15;
  169. unsigned char c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30,
  170. c31;
  171. c3 = c2 ^ c;
  172. c5 = c4 ^ c;
  173. c6 = c4 ^ c2;
  174. c7 = c4 ^ c3;
  175. c9 = c8 ^ c;
  176. c10 = c8 ^ c2;
  177. c11 = c8 ^ c3;
  178. c12 = c8 ^ c4;
  179. c13 = c8 ^ c5;
  180. c14 = c8 ^ c6;
  181. c15 = c8 ^ c7;
  182. tbl[0] = 0;
  183. tbl[1] = c;
  184. tbl[2] = c2;
  185. tbl[3] = c3;
  186. tbl[4] = c4;
  187. tbl[5] = c5;
  188. tbl[6] = c6;
  189. tbl[7] = c7;
  190. tbl[8] = c8;
  191. tbl[9] = c9;
  192. tbl[10] = c10;
  193. tbl[11] = c11;
  194. tbl[12] = c12;
  195. tbl[13] = c13;
  196. tbl[14] = c14;
  197. tbl[15] = c15;
  198. c17 = (c8 << 1) ^ ((c8 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  199. c18 = (c17 << 1) ^ ((c17 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  200. c19 = c18 ^ c17;
  201. c20 = (c18 << 1) ^ ((c18 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  202. c21 = c20 ^ c17;
  203. c22 = c20 ^ c18;
  204. c23 = c20 ^ c19;
  205. c24 = (c20 << 1) ^ ((c20 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  206. c25 = c24 ^ c17;
  207. c26 = c24 ^ c18;
  208. c27 = c24 ^ c19;
  209. c28 = c24 ^ c20;
  210. c29 = c24 ^ c21;
  211. c30 = c24 ^ c22;
  212. c31 = c24 ^ c23;
  213. tbl[16] = 0;
  214. tbl[17] = c17;
  215. tbl[18] = c18;
  216. tbl[19] = c19;
  217. tbl[20] = c20;
  218. tbl[21] = c21;
  219. tbl[22] = c22;
  220. tbl[23] = c23;
  221. tbl[24] = c24;
  222. tbl[25] = c25;
  223. tbl[26] = c26;
  224. tbl[27] = c27;
  225. tbl[28] = c28;
  226. tbl[29] = c29;
  227. tbl[30] = c30;
  228. tbl[31] = c31;
  229. #endif //__WORDSIZE == 64 || _WIN64 || __x86_64__
  230. }
  231. void gf_vect_dot_prod_base(int len, int vlen, unsigned char *v,
  232. unsigned char **src, unsigned char *dest)
  233. {
  234. int i, j;
  235. unsigned char s;
  236. for (i = 0; i < len; i++) {
  237. s = 0;
  238. for (j = 0; j < vlen; j++)
  239. s ^= gf_mul_erasure(src[j][i], v[j * 32 + 1]);
  240. dest[i] = s;
  241. }
  242. }
  243. void gf_vect_mad_base(int len, int vec, int vec_i,
  244. unsigned char *v, unsigned char *src, unsigned char *dest)
  245. {
  246. int i;
  247. unsigned char s;
  248. for (i = 0; i < len; i++) {
  249. s = dest[i];
  250. s ^= gf_mul_erasure(src[i], v[vec_i * 32 + 1]);
  251. dest[i] = s;
  252. }
  253. }
  254. void ec_encode_data_base(int len, int srcs, int dests, unsigned char *v,
  255. unsigned char **src, unsigned char **dest)
  256. {
  257. int i, j, l;
  258. unsigned char s;
  259. for (l = 0; l < dests; l++) {
  260. for (i = 0; i < len; i++) {
  261. s = 0;
  262. for (j = 0; j < srcs; j++)
  263. s ^= gf_mul_erasure(src[j][i], v[j * 32 + l * srcs * 32 + 1]);
  264. dest[l][i] = s;
  265. }
  266. }
  267. }
  268. void ec_encode_data_update_base(int len, int k, int rows, int vec_i, unsigned char *v,
  269. unsigned char *data, unsigned char **dest)
  270. {
  271. int i, l;
  272. unsigned char s;
  273. for (l = 0; l < rows; l++) {
  274. for (i = 0; i < len; i++) {
  275. s = dest[l][i];
  276. s ^= gf_mul_erasure(data[i], v[vec_i * 32 + l * k * 32 + 1]);
  277. dest[l][i] = s;
  278. }
  279. }
  280. }
  281. void gf_vect_mul_base(int len, unsigned char *a, unsigned char *src, unsigned char *dest)
  282. {
  283. //2nd element of table array is ref value used to fill it in
  284. unsigned char c = a[1];
  285. while (len-- > 0)
  286. *dest++ = gf_mul_erasure(c, *src++);
  287. }
  288. struct slver {
  289. unsigned short snum;
  290. unsigned char ver;
  291. unsigned char core;
  292. };
  293. // Version info
  294. struct slver gf_vect_mul_init_slver_00020035;
  295. struct slver gf_vect_mul_init_slver = { 0x0035, 0x02, 0x00 };
  296. struct slver ec_encode_data_base_slver_00010135;
  297. struct slver ec_encode_data_base_slver = { 0x0135, 0x01, 0x00 };
  298. struct slver gf_vect_mul_base_slver_00010136;
  299. struct slver gf_vect_mul_base_slver = { 0x0136, 0x01, 0x00 };
  300. struct slver gf_vect_dot_prod_base_slver_00010137;
  301. struct slver gf_vect_dot_prod_base_slver = { 0x0137, 0x01, 0x00 };
  302. struct slver gf_mul_slver_00000214;
  303. struct slver gf_mul_slver = { 0x0214, 0x00, 0x00 };
  304. struct slver gf_invert_matrix_slver_00000215;
  305. struct slver gf_invert_matrix_slver = { 0x0215, 0x00, 0x00 };
  306. struct slver gf_gen_rs_matrix_slver_00000216;
  307. struct slver gf_gen_rs_matrix_slver = { 0x0216, 0x00, 0x00 };
  308. struct slver gf_gen_cauchy1_matrix_slver_00000217;
  309. struct slver gf_gen_cauchy1_matrix_slver = { 0x0217, 0x00, 0x00 };