ec_base.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 <stdint.h>
  30. #include "erasure_code.h"
  31. #include "ec_base.h" // for GF tables
  32. void ec_init_tables_base(int k, int rows, unsigned char *a, unsigned char *g_tbls)
  33. {
  34. int i, j;
  35. for (i = 0; i < rows; i++) {
  36. for (j = 0; j < k; j++) {
  37. gf_vect_mul_init(*a++, g_tbls);
  38. g_tbls += 32;
  39. }
  40. }
  41. }
  42. unsigned char gf_mul_erasure(unsigned char a, unsigned char b)
  43. {
  44. #ifndef GF_LARGE_TABLES
  45. int i;
  46. if ((a == 0) || (b == 0))
  47. return 0;
  48. return gff_base[(i = gflog_base[a] + gflog_base[b]) > 254 ? i - 255 : i];
  49. #else
  50. return gf_mul_table_base[b * 256 + a];
  51. #endif
  52. }
  53. unsigned char gf_inv(unsigned char a)
  54. {
  55. #ifndef GF_LARGE_TABLES
  56. if (a == 0)
  57. return 0;
  58. return gff_base[255 - gflog_base[a]];
  59. #else
  60. return gf_inv_table_base[a];
  61. #endif
  62. }
  63. void gf_gen_rs_matrix(unsigned char *a, int m, int k)
  64. {
  65. int i, j;
  66. unsigned char p, gen = 1;
  67. memset(a, 0, k * m);
  68. for (i = 0; i < k; i++)
  69. a[k * i + i] = 1;
  70. for (i = k; i < m; i++) {
  71. p = 1;
  72. for (j = 0; j < k; j++) {
  73. a[k * i + j] = p;
  74. p = gf_mul_erasure(p, gen);
  75. }
  76. gen = gf_mul_erasure(gen, 2);
  77. }
  78. }
  79. void gf_gen_cauchy1_matrix(unsigned char *a, int m, int k)
  80. {
  81. int i, j;
  82. unsigned char *p;
  83. // Identity matrix in high position
  84. memset(a, 0, k * m);
  85. for (i = 0; i < k; i++)
  86. a[k * i + i] = 1;
  87. // For the rest choose 1/(i + j) | i != j
  88. p = &a[k * k];
  89. for (i = k; i < m; i++)
  90. for (j = 0; j < k; j++)
  91. *p++ = gf_inv(i ^ j);
  92. }
  93. int gf_invert_matrix(unsigned char *in_mat, unsigned char *out_mat, const int n)
  94. {
  95. int i, j, k;
  96. unsigned char temp;
  97. // Set out_mat[] to the identity matrix
  98. for (i = 0; i < n * n; i++) // memset(out_mat, 0, n*n)
  99. out_mat[i] = 0;
  100. for (i = 0; i < n; i++)
  101. out_mat[i * n + i] = 1;
  102. // Inverse
  103. for (i = 0; i < n; i++) {
  104. // Check for 0 in pivot element
  105. if (in_mat[i * n + i] == 0) {
  106. // Find a row with non-zero in current column and swap
  107. for (j = i + 1; j < n; j++)
  108. if (in_mat[j * n + i])
  109. break;
  110. if (j == n) // Couldn't find means it's singular
  111. return -1;
  112. for (k = 0; k < n; k++) { // Swap rows i,j
  113. temp = in_mat[i * n + k];
  114. in_mat[i * n + k] = in_mat[j * n + k];
  115. in_mat[j * n + k] = temp;
  116. temp = out_mat[i * n + k];
  117. out_mat[i * n + k] = out_mat[j * n + k];
  118. out_mat[j * n + k] = temp;
  119. }
  120. }
  121. temp = gf_inv(in_mat[i * n + i]); // 1/pivot
  122. for (j = 0; j < n; j++) { // Scale row i by 1/pivot
  123. in_mat[i * n + j] = gf_mul_erasure(in_mat[i * n + j], temp);
  124. out_mat[i * n + j] = gf_mul_erasure(out_mat[i * n + j], temp);
  125. }
  126. for (j = 0; j < n; j++) {
  127. if (j == i)
  128. continue;
  129. temp = in_mat[j * n + i];
  130. for (k = 0; k < n; k++) {
  131. out_mat[j * n + k] ^= gf_mul_erasure(temp, out_mat[i * n + k]);
  132. in_mat[j * n + k] ^= gf_mul_erasure(temp, in_mat[i * n + k]);
  133. }
  134. }
  135. }
  136. return 0;
  137. }
  138. // Calculates const table gftbl in GF(2^8) from single input A
  139. // gftbl(A) = {A{00}, A{01}, A{02}, ... , A{0f} }, {A{00}, A{10}, A{20}, ... , A{f0} }
  140. void gf_vect_mul_init(unsigned char c, unsigned char *tbl)
  141. {
  142. unsigned char c2 = (c << 1) ^ ((c & 0x80) ? 0x1d : 0); //Mult by GF{2}
  143. unsigned char c4 = (c2 << 1) ^ ((c2 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  144. unsigned char c8 = (c4 << 1) ^ ((c4 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  145. #if (__WORDSIZE == 64 || _WIN64 || __x86_64__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  146. unsigned long long v1, v2, v4, v8, *t;
  147. unsigned long long v10, v20, v40, v80;
  148. unsigned char c17, c18, c20, c24;
  149. t = (unsigned long long *)tbl;
  150. v1 = c * 0x0100010001000100ull;
  151. v2 = c2 * 0x0101000001010000ull;
  152. v4 = c4 * 0x0101010100000000ull;
  153. v8 = c8 * 0x0101010101010101ull;
  154. v4 = v1 ^ v2 ^ v4;
  155. t[0] = v4;
  156. t[1] = v8 ^ v4;
  157. c17 = (c8 << 1) ^ ((c8 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  158. c18 = (c17 << 1) ^ ((c17 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  159. c20 = (c18 << 1) ^ ((c18 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  160. c24 = (c20 << 1) ^ ((c20 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  161. v10 = c17 * 0x0100010001000100ull;
  162. v20 = c18 * 0x0101000001010000ull;
  163. v40 = c20 * 0x0101010100000000ull;
  164. v80 = c24 * 0x0101010101010101ull;
  165. v40 = v10 ^ v20 ^ v40;
  166. t[2] = v40;
  167. t[3] = v80 ^ v40;
  168. #else // 32-bit or other
  169. unsigned char c3, c5, c6, c7, c9, c10, c11, c12, c13, c14, c15;
  170. unsigned char c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30,
  171. c31;
  172. c3 = c2 ^ c;
  173. c5 = c4 ^ c;
  174. c6 = c4 ^ c2;
  175. c7 = c4 ^ c3;
  176. c9 = c8 ^ c;
  177. c10 = c8 ^ c2;
  178. c11 = c8 ^ c3;
  179. c12 = c8 ^ c4;
  180. c13 = c8 ^ c5;
  181. c14 = c8 ^ c6;
  182. c15 = c8 ^ c7;
  183. tbl[0] = 0;
  184. tbl[1] = c;
  185. tbl[2] = c2;
  186. tbl[3] = c3;
  187. tbl[4] = c4;
  188. tbl[5] = c5;
  189. tbl[6] = c6;
  190. tbl[7] = c7;
  191. tbl[8] = c8;
  192. tbl[9] = c9;
  193. tbl[10] = c10;
  194. tbl[11] = c11;
  195. tbl[12] = c12;
  196. tbl[13] = c13;
  197. tbl[14] = c14;
  198. tbl[15] = c15;
  199. c17 = (c8 << 1) ^ ((c8 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  200. c18 = (c17 << 1) ^ ((c17 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  201. c19 = c18 ^ c17;
  202. c20 = (c18 << 1) ^ ((c18 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  203. c21 = c20 ^ c17;
  204. c22 = c20 ^ c18;
  205. c23 = c20 ^ c19;
  206. c24 = (c20 << 1) ^ ((c20 & 0x80) ? 0x1d : 0); //Mult by GF{2}
  207. c25 = c24 ^ c17;
  208. c26 = c24 ^ c18;
  209. c27 = c24 ^ c19;
  210. c28 = c24 ^ c20;
  211. c29 = c24 ^ c21;
  212. c30 = c24 ^ c22;
  213. c31 = c24 ^ c23;
  214. tbl[16] = 0;
  215. tbl[17] = c17;
  216. tbl[18] = c18;
  217. tbl[19] = c19;
  218. tbl[20] = c20;
  219. tbl[21] = c21;
  220. tbl[22] = c22;
  221. tbl[23] = c23;
  222. tbl[24] = c24;
  223. tbl[25] = c25;
  224. tbl[26] = c26;
  225. tbl[27] = c27;
  226. tbl[28] = c28;
  227. tbl[29] = c29;
  228. tbl[30] = c30;
  229. tbl[31] = c31;
  230. #endif //__WORDSIZE == 64 || _WIN64 || __x86_64__
  231. }
  232. void gf_vect_dot_prod_base(int len, int vlen, unsigned char *v,
  233. unsigned char **src, unsigned char *dest)
  234. {
  235. int i, j;
  236. unsigned char s;
  237. for (i = 0; i < len; i++) {
  238. s = 0;
  239. for (j = 0; j < vlen; j++)
  240. s ^= gf_mul_erasure(src[j][i], v[j * 32 + 1]);
  241. dest[i] = s;
  242. }
  243. }
  244. void gf_vect_mad_base(int len, int vec, int vec_i,
  245. unsigned char *v, unsigned char *src, unsigned char *dest)
  246. {
  247. int i;
  248. unsigned char s;
  249. for (i = 0; i < len; i++) {
  250. s = dest[i];
  251. s ^= gf_mul_erasure(src[i], v[vec_i * 32 + 1]);
  252. dest[i] = s;
  253. }
  254. }
  255. void ec_encode_data_base(int len, int srcs, int dests, unsigned char *v,
  256. unsigned char **src, unsigned char **dest)
  257. {
  258. int i, j, l;
  259. unsigned char s;
  260. for (l = 0; l < dests; l++) {
  261. for (i = 0; i < len; i++) {
  262. s = 0;
  263. for (j = 0; j < srcs; j++)
  264. s ^= gf_mul_erasure(src[j][i], v[j * 32 + l * srcs * 32 + 1]);
  265. dest[l][i] = s;
  266. }
  267. }
  268. }
  269. void ec_encode_data_update_base(int len, int k, int rows, int vec_i, unsigned char *v,
  270. unsigned char *data, unsigned char **dest)
  271. {
  272. int i, l;
  273. unsigned char s;
  274. for (l = 0; l < rows; l++) {
  275. for (i = 0; i < len; i++) {
  276. s = dest[l][i];
  277. s ^= gf_mul_erasure(data[i], v[vec_i * 32 + l * k * 32 + 1]);
  278. dest[l][i] = s;
  279. }
  280. }
  281. }
  282. int gf_vect_mul_base(int len, unsigned char *a, unsigned char *src, unsigned char *dest)
  283. {
  284. //2nd element of table array is ref value used to fill it in
  285. unsigned char c = a[1];
  286. // Len must be aligned to 32B
  287. if ((len % 32) != 0) {
  288. return -1;
  289. }
  290. while (len-- > 0)
  291. *dest++ = gf_mul_erasure(c, *src++);
  292. return 0;
  293. }