| 6 |
| 7 | Redistribution and use in source and binary forms, with or without |
| 8 | modification, are permitted provided that the following conditions are met: |
| 9 |
| 10 | * Redistributions of source code must retain the above copyright notice, |
| 11 | this list of conditions and the following disclaimer. |
| 12 | * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | this list of conditions and the following disclaimer in the documentation |
| 14 | and/or other materials provided with the distribution. |
| 15 | * Neither the name of CM256 nor the names of its contributors may be |
| 16 | used to endorse or promote products derived from this software without |
| 17 | specific prior written permission. |
| 18 |
| 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 |
| 32 | #include "cm256.h" |
| 33 |
| 34 | CM256::CM256() |
| 35 | { |
| 36 | m_initialized = m_gf256Ctx.isInitialized(); |
| 37 | } |
| 38 |
| 39 | CM256::~CM256() |
| 40 | { |
| 41 | } |
| 42 |
| 43 | /* |
| 44 | GF(256) Cauchy Matrix Overview |
| 45 |
| 46 | As described on Wikipedia, each element of a normal Cauchy matrix is defined as: |
| 47 |
| 48 | a_ij = 1 / (x_i - y_j) |
| 49 | The arrays x_i and y_j are vector parameters of the matrix. |
| 50 | The values in x_i cannot be reused in y_j. |
| 51 |
| 52 | Moving beyond the Wikipedia... |
| 53 |
| 54 | (1) Number of rows (R) is the range of i, and number of columns (C) is the range of j. |
| 55 |
| 56 | (2) Being able to select x_i and y_j makes Cauchy matrices more flexible in practice |
| 57 | than Vandermonde matrices, which only have one parameter per row. |
| 58 |
| 59 | (3) Cauchy matrices are always invertible, AKA always full rank, AKA when treated as |
| 60 | as linear system y = M*x, the linear system has a single solution. |
| 61 |
| 62 | (4) A Cauchy matrix concatenated below a square CxC identity matrix always has rank C, |
| 63 | Meaning that any R rows can be eliminated from the concatenated matrix and the |
| 64 | matrix will still be invertible. This is how Reed-Solomon erasure codes work. |
| 65 |
| 66 | (5) Any row or column can be multiplied by non-zero values, and the resulting matrix |
| 67 | is still full rank. This is true for any matrix, since it is effectively the same |
| 68 | as pre and post multiplying by diagonal matrices, which are always invertible. |
| 69 |
| 70 | (6) Matrix elements with a value of 1 are much faster to operate on than other values. |
| 71 | For instance a matrix of [1, 1, 1, 1, 1] is invertible and much faster for various |
| 72 | purposes than [2, 2, 2, 2, 2]. |
| 73 |
| 74 | (7) For GF(256) matrices, the symbols in x_i and y_j are selected from the numbers |
| 75 | 0...255, and so the number of rows + number of columns may not exceed 256. |
| 76 | Note that values in x_i and y_j may not be reused as stated above. |
| 77 |
| 78 | In summary, Cauchy matrices |
| 79 | are preferred over Vandermonde matrices. (2) |
| 80 | are great for MDS erasure codes. (3) and (4) |
| 81 | should be optimized to include more 1 elements. (5) and (6) |
| 82 | have a limited size in GF(256), rows+cols <= 256. (7) |
| 83 | */ |
| 84 |
| 85 | /* |
| 86 | Selected Cauchy Matrix Form |
| 87 |
| 88 | The matrix consists of elements a_ij, where i = row, j = column. |
| 89 | a_ij = 1 / (x_i - y_j), where x_i and y_j are sets of GF(256) values |
| 90 | that do not intersect. |
| 91 |
| 92 | We select x_i and y_j to just be incrementing numbers for the |
| 93 | purposes of this library. Further optimizations may yield matrices |
| 94 | with more 1 elements, but the benefit seems relatively small. |
| 95 |
| 96 | The x_i values range from 0...(originalCount - 1). |
| 97 | The y_j values range from originalCount...(originalCount + recoveryCount - 1). |
| 98 |
| 99 | We then improve the Cauchy matrix by dividing each column by the |
| 100 | first row element of that column. The result is an invertible |
| 101 | matrix that has all 1 elements in the first row. This is equivalent |
| 102 | to a rotated Vandermonde matrix, so we could have used one of those. |
| 103 |
| 104 | The advantage of doing this is that operations involving the first |
| 105 | row will be extremely fast (just memory XOR), so the decoder can |
| 106 | be optimized to take advantage of the shortcut when the first |
| 107 | recovery row can be used. |
| 108 |
| 109 | First row element of Cauchy matrix for each column: |
| 110 | a_0j = 1 / (x_0 - y_j) = 1 / (x_0 - y_j) |
| 111 |
| 112 | Our Cauchy matrix sets first row to ones, so: |
| 113 | a_ij = (1 / (x_i - y_j)) / a_0j |
| 114 | a_ij = (y_j - x_0) / (x_i - y_j) |
| 115 | a_ij = (y_j + x_0) div (x_i + y_j) in GF(256) |
| 116 | */ |
| 117 |
| 118 | //----------------------------------------------------------------------------- |
| 119 | // Encoding |
| 120 |
| 121 | void CM256::cm256_encode_block( |
| 122 | cm256_encoder_params params, // Encoder parameters |
| 123 | cm256_block* originals, // Array of pointers to original blocks |
| 124 | int recoveryBlockIndex, // Return value from cm256_get_recovery_block_index() |
| 125 | void* recoveryBlock) // Output recovery block |
| 126 | { |
| 127 | // If only one block of input data, |
| 128 | if (params.OriginalCount == 1) |
| 129 | { |
| 130 | // No meaningful operation here, degenerate to outputting the same data each time. |
| 131 |
| 132 | memcpy(recoveryBlock, originals[0].Block, params.BlockBytes); |
| 133 | return; |
| 134 | } |
| 135 | // else OriginalCount >= 2: |
| 136 |
| 137 | // Unroll first row of recovery matrix: |
| 138 | // The matrix we generate for the first row is all ones, |
| 139 | // so it is merely a parity of the original data. |
| 140 | if (recoveryBlockIndex == params.OriginalCount) |
| 141 | { |
| 142 | gf256_ctx::gf256_addset_mem(recoveryBlock, originals[0].Block, originals[1].Block, params.BlockBytes); |
| 143 | for (int j = 2; j < params.OriginalCount; ++j) |
| 144 | { |
| 145 | gf256_ctx::gf256_add_mem(recoveryBlock, originals[j].Block, params.BlockBytes); |
| 146 | } |
| 147 | return; |
| 148 | } |
| 149 |
| 150 | // TBD: Faster algorithms seem to exist for computing this matrix-vector product. |
| 151 |
| 152 | // Start the x_0 values arbitrarily from the original count. |
| 153 | const uint8_t x_0 = static_cast<uint8_t>(params.OriginalCount); |
| 154 |
| 155 | // For other rows: |
| 156 | { |
| 157 | const uint8_t x_i = static_cast<uint8_t>(recoveryBlockIndex); |
| 158 |
| 159 | // Unroll first operation for speed |
| 160 | { |
| 161 | const uint8_t y_0 = 0; |
| 162 | const uint8_t matrixElement = m_gf256Ctx.getMatrixElement(x_i, x_0, y_0); |
| 163 |
| 164 | m_gf256Ctx.gf256_mul_mem(recoveryBlock, originals[0].Block, matrixElement, params.BlockBytes); |
| 165 | } |
| 166 |
| 167 | // For each original data column, |
| 168 | for (int j = 1; j < params.OriginalCount; ++j) |
| 169 | { |
| 170 | const uint8_t y_j = static_cast<uint8_t>(j); |
| 171 | const uint8_t matrixElement = m_gf256Ctx.getMatrixElement(x_i, x_0, y_j); |
| 172 |
| 173 | m_gf256Ctx.gf256_muladd_mem(recoveryBlock, matrixElement, originals[j].Block, params.BlockBytes); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 |
| 178 | int CM256::cm256_encode( |
| 179 | cm256_encoder_params params, // Encoder params |
| 180 | cm256_block* originals, // Array of pointers to original blocks |
| 181 | void* recoveryBlocks) // Output recovery blocks end-to-end |
| 182 | { |
| 183 | // Validate input: |
| 184 | if (params.OriginalCount <= 0 || params.RecoveryCount <= 0 || params.BlockBytes <= 0) |
| 185 | { |
| 186 | return -1; |
| 187 | } |
| 188 | if (params.OriginalCount + params.RecoveryCount > 256) |
| 189 | { |
| 190 | return -2; |
| 191 | } |
| 192 | if (!originals || !recoveryBlocks) |
| 193 | { |
| 194 | return -3; |
| 195 | } |
| 196 |
| 197 | uint8_t* recoveryBlock = static_cast<uint8_t*>(recoveryBlocks); |
| 198 |
| 199 | for (int block = 0; block < params.RecoveryCount; ++block, recoveryBlock += params.BlockBytes) |
| 200 | { |
| 201 | cm256_encode_block(params, originals, (params.OriginalCount + block), recoveryBlock); |
| 202 | } |
| 203 |
| 204 | return 0; |
| 205 | } |
| 206 |
| 207 | //----------------------------------------------------------------------------- |
| 208 | // Decoding |
| 209 |
| 210 | CM256::CM256Decoder::CM256Decoder(gf256_ctx& gf256Ctx) |
| 211 | : RecoveryCount(0) |
| 212 | , OriginalCount(0) |
| 213 | , m_gf256Ctx(gf256Ctx) |
| 214 | { |
| 215 | } |
| 216 |
| 217 | CM256::CM256Decoder::~CM256Decoder() |
| 218 | { |
| 219 | } |
| 220 |
| 221 | bool CM256::CM256Decoder::Initialize(cm256_encoder_params& params, cm256_block* blocks) |
| 222 | { |
| 223 | Params = params; |
| 224 |
| 225 | cm256_block* block = blocks; |
| 226 | OriginalCount = 0; |
| 227 | RecoveryCount = 0; |
| 228 |
| 229 | // Initialize erasures to zeros |
| 230 | for (int ii = 0; ii < params.OriginalCount; ++ii) |
| 231 | { |
| 232 | ErasuresIndices[ii] = 0; |
| 233 | } |
| 234 |
| 235 | // For each input block, |
| 236 | for (int ii = 0; ii < params.OriginalCount; ++ii, ++block) |
| 237 | { |
| 238 | int row = block->Index; |
| 239 |
| 240 | // If it is an original block, |
| 241 | if (row < params.OriginalCount) |
| 242 | { |
| 243 | Original[OriginalCount++] = block; |
| 244 |
| 245 | if (ErasuresIndices[row] != 0) |
| 246 | { |
| 247 | // Error out if two row indices repeat |
| 248 | return false; |
| 249 | } |
| 250 |
| 251 | ErasuresIndices[row] = 1; |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | Recovery[RecoveryCount++] = block; |
| 256 | } |
| 257 | } |
| 258 |
| 259 | // Identify erasures |
| 260 | for (int ii = 0, indexCount = 0; ii < 256; ++ii) |
| 261 | { |
| 262 | if (!ErasuresIndices[ii]) |
| 263 | { |
| 264 | ErasuresIndices[indexCount] = static_cast<uint8_t>(ii); |
| 265 |
| 266 | if (++indexCount >= RecoveryCount) |
| 267 | { |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 |
| 273 | return true; |
| 274 | } |
| 275 |
| 276 | void CM256::CM256Decoder::DecodeM1() |
| 277 | { |
| 278 | // XOR all other blocks into the recovery block |
| 279 | uint8_t* outBlock = static_cast<uint8_t*>(Recovery[0]->Block); |
| 280 | const uint8_t* inBlock = nullptr; |
| 281 |
| 282 | // For each block, |
| 283 | for (int ii = 0; ii < OriginalCount; ++ii) |
| 284 | { |
| 285 | const uint8_t* inBlock2 = static_cast<const uint8_t*>(Original[ii]->Block); |
| 286 |
| 287 | if (!inBlock) |
| 288 | { |
| 289 | inBlock = inBlock2; |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | // outBlock ^= inBlock ^ inBlock2 |
| 294 | gf256_ctx::gf256_add2_mem(outBlock, inBlock, inBlock2, Params.BlockBytes); |
| 295 | inBlock = nullptr; |
| 296 | } |
| 297 | } |
| 298 |
| 299 | // Complete XORs |
| 300 | if (inBlock) |
| 301 | { |
| 302 | gf256_ctx::gf256_add_mem(outBlock, inBlock, Params.BlockBytes); |
| 303 | } |
| 304 |
| 305 | // Recover the index it corresponds to |
| 306 | Recovery[0]->Index = ErasuresIndices[0]; |
| 307 | } |
| 308 |
| 309 | // Generate the LU decomposition of the matrix |
| 310 | void CM256::CM256Decoder::GenerateLDUDecomposition(uint8_t* matrix_L, uint8_t* diag_D, uint8_t* matrix_U) |
| 311 | { |
| 312 | // Schur-type-direct-Cauchy algorithm 2.5 from |
| 313 | // "Pivoting and Backward Stability of Fast Algorithms for Solving Cauchy Linear Equations" |
| 314 | // T. Boros, T. Kailath, V. Olshevsky |
| 315 | // Modified for practical use. I folded the diagonal parts of U/L matrices into the |
| 316 | // diagonal one to reduce the number of multiplications to perform against the input data, |
| 317 | // and organized the triangle matrices in memory to allow for faster SSE3 GF multiplications. |
| 318 |
| 319 | // Matrix size NxN |
| 320 | const int N = RecoveryCount; |
| 321 |
| 322 | // Generators |
| 323 | uint8_t g[256], b[256]; |
| 324 | for (int i = 0; i < N; ++i) |
| 325 | { |
| 326 | g[i] = 1; |
| 327 | b[i] = 1; |
| 328 | } |
| 329 |
| 330 | // Temporary buffer for rotated row of U matrix |
| 331 | // This allows for faster GF bulk multiplication |
| 332 | uint8_t rotated_row_U[256]; |
| 333 | uint8_t* last_U = matrix_U + ((N - 1) * N) / 2 - 1; |
| 334 | int firstOffset_U = 0; |
| 335 |
| 336 | // Start the x_0 values arbitrarily from the original count. |
| 337 | const uint8_t x_0 = static_cast<uint8_t>(Params.OriginalCount); |
| 338 |
| 339 | // Unrolling k = 0 just makes it slower for some reason. |
| 340 | for (int k = 0; k < N - 1; ++k) |
| 341 | { |
| 342 | const uint8_t x_k = Recovery[k]->Index; |
| 343 | const uint8_t y_k = ErasuresIndices[k]; |
| 344 |
| 345 | // D_kk = (x_k + y_k) |
| 346 | // L_kk = g[k] / (x_k + y_k) |
| 347 | // U_kk = b[k] * (x_0 + y_k) / (x_k + y_k) |
| 348 | const uint8_t D_kk = gf256_ctx::gf256_add(x_k, y_k); |
| 349 | const uint8_t L_kk = m_gf256Ctx.gf256_div(g[k], D_kk); |
| 350 | const uint8_t U_kk = m_gf256Ctx.gf256_mul(m_gf256Ctx.gf256_div(b[k], D_kk), gf256_ctx::gf256_add(x_0, y_k)); |
| 351 |
| 352 | // diag_D[k] = D_kk * L_kk * U_kk |
| 353 | diag_D[k] = m_gf256Ctx.gf256_mul(D_kk, m_gf256Ctx.gf256_mul(L_kk, U_kk)); |
| 354 |
| 355 | // Computing the k-th row of L and U |
| 356 | uint8_t* row_L = matrix_L; |
| 357 | uint8_t* row_U = rotated_row_U; |
| 358 | for (int j = k + 1; j < N; ++j) |
| 359 | { |
| 360 | const uint8_t x_j = Recovery[j]->Index; |
| 361 | const uint8_t y_j = ErasuresIndices[j]; |
| 362 |
| 363 | // L_jk = g[j] / (x_j + y_k) |
| 364 | // U_kj = b[j] / (x_k + y_j) |
| 365 | const uint8_t L_jk = m_gf256Ctx.gf256_div(g[j], gf256_ctx::gf256_add(x_j, y_k)); |
| 366 | const uint8_t U_kj = m_gf256Ctx.gf256_div(b[j], gf256_ctx::gf256_add(x_k, y_j)); |
| 367 |
| 368 | *matrix_L++ = L_jk; |
| 369 | *row_U++ = U_kj; |
| 370 |
| 371 | // g[j] = g[j] * (x_j + x_k) / (x_j + y_k) |
| 372 | // b[j] = b[j] * (y_j + y_k) / (y_j + x_k) |
| 373 | g[j] = m_gf256Ctx.gf256_mul(g[j], m_gf256Ctx.gf256_div(gf256_ctx::gf256_add(x_j, x_k), gf256_ctx::gf256_add(x_j, y_k))); |
| 374 | b[j] = m_gf256Ctx.gf256_mul(b[j], m_gf256Ctx.gf256_div(gf256_ctx::gf256_add(y_j, y_k), gf256_ctx::gf256_add(y_j, x_k))); |
| 375 | } |
| 376 |
| 377 | // Do these row/column divisions in bulk for speed. |
| 378 | // L_jk /= L_kk |
| 379 | // U_kj /= U_kk |
| 380 | const int count = N - (k + 1); |
| 381 | m_gf256Ctx.gf256_div_mem(row_L, row_L, L_kk, count); |
| 382 | m_gf256Ctx.gf256_div_mem(rotated_row_U, rotated_row_U, U_kk, count); |
| 383 |
| 384 | // Copy U matrix row into place in memory. |
| 385 | uint8_t* output_U = last_U + firstOffset_U; |
| 386 | row_U = rotated_row_U; |
| 387 | for (int j = k + 1; j < N; ++j) |
| 388 | { |
| 389 | *output_U = *row_U++; |
| 390 | output_U -= j; |
| 391 | } |
| 392 | firstOffset_U -= k + 2; |
| 393 | } |
| 394 |
| 395 | // Multiply diagonal matrix into U |
| 396 | uint8_t* row_U = matrix_U; |
| 397 | for (int j = N - 1; j > 0; --j) |
| 398 | { |
| 399 | const uint8_t y_j = ErasuresIndices[j]; |
| 400 | const int count = j; |
| 401 |
| 402 | m_gf256Ctx.gf256_mul_mem(row_U, row_U, gf256_ctx::gf256_add(x_0, y_j), count); |
| 403 | row_U += count; |
| 404 | } |
| 405 |
| 406 | const uint8_t x_n = Recovery[N - 1]->Index; |
| 407 | const uint8_t y_n = ErasuresIndices[N - 1]; |
| 408 |
| 409 | // D_nn = 1 / (x_n + y_n) |
| 410 | // L_nn = g[N-1] |
| 411 | // U_nn = b[N-1] * (x_0 + y_n) |
| 412 | const uint8_t L_nn = g[N - 1]; |
| 413 | const uint8_t U_nn = m_gf256Ctx.gf256_mul(b[N - 1], gf256_ctx::gf256_add(x_0, y_n)); |
| 414 |
| 415 | // diag_D[N-1] = L_nn * D_nn * U_nn |
| 416 | diag_D[N - 1] = m_gf256Ctx.gf256_div(m_gf256Ctx.gf256_mul(L_nn, U_nn), gf256_ctx::gf256_add(x_n, y_n)); |
| 417 | } |
| 418 |
| 419 | void CM256::CM256Decoder::Decode() |
| 420 | { |
| 421 | // Matrix size is NxN, where N is the number of recovery blocks used. |
| 422 | const int N = RecoveryCount; |
| 423 |
| 424 | // Start the x_0 values arbitrarily from the original count. |
| 425 | const uint8_t x_0 = static_cast<uint8_t>(Params.OriginalCount); |
| 426 |
| 427 | // Eliminate original data from the the recovery rows |
| 428 | for (int originalIndex = 0; originalIndex < OriginalCount; ++originalIndex) |
| 429 | { |
| 430 | const uint8_t* inBlock = static_cast<const uint8_t*>(Original[originalIndex]->Block); |
| 431 | const uint8_t inRow = Original[originalIndex]->Index; |
| 432 |
| 433 | for (int recoveryIndex = 0; recoveryIndex < N; ++recoveryIndex) |
| 434 | { |
| 435 | uint8_t* outBlock = static_cast<uint8_t*>(Recovery[recoveryIndex]->Block); |
| 436 | const uint8_t x_i = Recovery[recoveryIndex]->Index; |
| 437 | const uint8_t y_j = inRow; |
| 438 | const uint8_t matrixElement = m_gf256Ctx.getMatrixElement(x_i, x_0, y_j); |
| 439 |
| 440 | m_gf256Ctx.gf256_muladd_mem(outBlock, matrixElement, inBlock, Params.BlockBytes); |
| 441 | } |
| 442 | } |
| 443 |
| 444 | // Allocate matrix |
| 445 | static const int StackAllocSize = 2048; |
| 446 | uint8_t stackMatrix[StackAllocSize]; |
| 447 | uint8_t* dynamicMatrix = nullptr; |
| 448 | uint8_t* matrix = stackMatrix; |
| 449 | const int requiredSpace = N * N; |
| 450 | if (requiredSpace > StackAllocSize) |
| 451 | { |
| 452 | dynamicMatrix = new uint8_t[requiredSpace]; |
| 453 | matrix = dynamicMatrix; |
| 454 | } |
| 455 |
| 456 | /* |
| 457 | Compute matrix decomposition: |
| 458 |
| 459 | G = L * D * U |
| 460 |
| 461 | L is lower-triangular, diagonal is all ones. |
| 462 | D is a diagonal matrix. |
| 463 | U is upper-triangular, diagonal is all ones. |
| 464 | */ |
| 465 | uint8_t* matrix_U = matrix; |
| 466 | uint8_t* diag_D = matrix_U + (N - 1) * N / 2; |
| 467 | uint8_t* matrix_L = diag_D + N; |
| 468 | GenerateLDUDecomposition(matrix_L, diag_D, matrix_U); |
| 469 |
| 470 | /* |
| 471 | Eliminate lower left triangle. |
| 472 | */ |
| 473 | // For each column, |
| 474 | for (int j = 0; j < N - 1; ++j) |
| 475 | { |
| 476 | const void* block_j = Recovery[j]->Block; |
| 477 |
| 478 | // For each row, |
| 479 | for (int i = j + 1; i < N; ++i) |
| 480 | { |
| 481 | void* block_i = Recovery[i]->Block; |
| 482 | const uint8_t c_ij = *matrix_L++; // Matrix elements are stored column-first, top-down. |
| 483 |
| 484 | m_gf256Ctx.gf256_muladd_mem(block_i, c_ij, block_j, Params.BlockBytes); |
| 485 | } |
| 486 | } |
| 487 |
| 488 | /* |
| 489 | Eliminate diagonal. |
| 490 | */ |
| 491 | for (int i = 0; i < N; ++i) |
| 492 | { |
| 493 | void* block = Recovery[i]->Block; |
| 494 |
| 495 | Recovery[i]->Index = ErasuresIndices[i]; |
| 496 |
| 497 | m_gf256Ctx.gf256_div_mem(block, block, diag_D[i], Params.BlockBytes); |
| 498 | } |
| 499 |
| 500 | /* |
| 501 | Eliminate upper right triangle. |
| 502 | */ |
| 503 | for (int j = N - 1; j >= 1; --j) |
| 504 | { |
| 505 | const void* block_j = Recovery[j]->Block; |
| 506 |
| 507 | for (int i = j - 1; i >= 0; --i) |
| 508 | { |
| 509 | void* block_i = Recovery[i]->Block; |
| 510 | const uint8_t c_ij = *matrix_U++; // Matrix elements are stored column-first, bottom-up. |
| 511 |
| 512 | m_gf256Ctx.gf256_muladd_mem(block_i, c_ij, block_j, Params.BlockBytes); |
| 513 | } |
| 514 | } |
| 515 |
| 516 | delete[] dynamicMatrix; |
| 517 | } |
| 518 |
| 519 | int CM256::cm256_decode( |
| 520 | cm256_encoder_params params, // Encoder params |
| 521 | cm256_block* blocks) // Array of 'originalCount' blocks as described above |
| 522 | { |
| 523 | if (params.OriginalCount <= 0 || params.RecoveryCount <= 0 || params.BlockBytes <= 0) |
| 524 | { |
| 525 | return -1; |
| 526 | } |
| 527 | if (params.OriginalCount + params.RecoveryCount > 256) |
| 528 | { |
| 529 | return -2; |
| 530 | } |
| 531 | if (!blocks) |
| 532 | { |
| 533 | return -3; |
| 534 | } |
| 535 |
| 536 | // If there is only one block, |
| 537 | if (params.OriginalCount == 1) |
| 538 | { |
| 539 | // It is the same block repeated |
| 540 | blocks[0].Index = 0; |
| 541 | return 0; |
| 542 | } |
| 543 |
| 544 | CM256Decoder state(m_gf256Ctx); |
| 545 | if (!state.Initialize(params, blocks)) |
| 546 | { |
| 547 | return -5; |
| 548 | } |
| 549 |
| 550 | // If nothing is erased, |
| 551 | if (state.RecoveryCount <= 0) |
| 552 | { |
| 553 | return 0; |
| 554 | } |
| 555 |
| 556 | // If m=1, |
| 557 | if (params.RecoveryCount == 1) |
| 558 | { |
| 559 | state.DecodeM1(); |
| 560 | return 0; |
| 561 | } |
| 562 |
| 563 | // Decode for m>1 |
| 564 | state.Decode(); |
| 565 | return 0; |
| 566 | } |
| 567 |