| 6 |
| 7 | * Redistributions of source code must retain the above copyright notice, |
| 8 | this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | this list of conditions and the following disclaimer in the documentation |
| 11 | and/or other materials provided with the distribution. |
| 12 | * Neither the name of CM256 nor the names of its contributors may be |
| 13 | used to endorse or promote products derived from this software without |
| 14 | specific prior written permission. |
| 15 |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 |
| 29 | #include <cstdio> |
| 30 | #include <cstdlib> |
| 31 |
| 32 | #include "gf256.h" |
| 33 |
| 34 | const uint8_t gf256_ctx::GF256_GEN_POLY[GF256_GEN_POLY_COUNT] = { |
| 35 | 0x8e, |
| 36 | 0x95, |
| 37 | 0x96, |
| 38 | 0xa6, |
| 39 | 0xaf, |
| 40 | 0xb1, |
| 41 | 0xb2, |
| 42 | 0xb4, |
| 43 | 0xb8, |
| 44 | 0xc3, |
| 45 | 0xc6, |
| 46 | 0xd4, |
| 47 | 0xe1, |
| 48 | 0xe7, |
| 49 | 0xf3, |
| 50 | 0xfa, |
| 51 | }; |
| 52 |
| 53 | gf256_ctx::gf256_ctx() |
| 54 | : initialized(false) |
| 55 | { |
| 56 | gf256_init_(); |
| 57 | } |
| 58 |
| 59 | gf256_ctx::~gf256_ctx() |
| 60 | { |
| 61 | } |
| 62 |
| 63 | // Select which polynomial to use |
| 64 | void gf256_ctx::gf255_poly_init(int polynomialIndex) |
| 65 | { |
| 66 | if (polynomialIndex < 0 || polynomialIndex >= GF256_GEN_POLY_COUNT) |
| 67 | { |
| 68 | polynomialIndex = 0; |
| 69 | } |
| 70 |
| 71 | Polynomial = (GF256_GEN_POLY[polynomialIndex] << 1) | 1; |
| 72 | } |
| 73 |
| 74 | //----------------------------------------------------------------------------- |
| 75 | // Exponential and Log Tables |
| 76 |
| 77 | // Construct EXP and LOG tables from polynomial |
| 78 | void gf256_ctx::gf256_explog_init() |
| 79 | { |
| 80 | unsigned poly = Polynomial; |
| 81 | uint8_t* exptab = GF256_EXP_TABLE; |
| 82 | uint16_t* logtab = GF256_LOG_TABLE; |
| 83 |
| 84 | logtab[0] = 512; |
| 85 | exptab[0] = 1; |
| 86 | for (unsigned jj = 1; jj < 255; ++jj) |
| 87 | { |
| 88 | unsigned next = (unsigned)exptab[jj - 1] * 2; |
| 89 | if (next >= 256) |
| 90 | next ^= poly; |
| 91 |
| 92 | exptab[jj] = static_cast<uint8_t>(next); |
| 93 | logtab[exptab[jj]] = static_cast<uint16_t>(jj); |
| 94 | } |
| 95 |
| 96 | exptab[255] = exptab[0]; |
| 97 | logtab[exptab[255]] = 255; |
| 98 |
| 99 | for (unsigned jj = 256; jj < 2 * 255; ++jj) |
| 100 | { |
| 101 | exptab[jj] = exptab[jj % 255]; |
| 102 | } |
| 103 |
| 104 | exptab[2 * 255] = 1; |
| 105 |
| 106 | for (unsigned jj = 2 * 255 + 1; jj < 4 * 255; ++jj) |
| 107 | { |
| 108 | exptab[jj] = 0; |
| 109 | } |
| 110 | } |
| 111 |
| 112 | //----------------------------------------------------------------------------- |
| 113 | // Multiply and Divide Tables |
| 114 |
| 115 | // Initialize MUL and DIV tables using LOG and EXP tables |
| 116 | void gf256_ctx::gf256_muldiv_init() |
| 117 | { |
| 118 | // Allocate table memory 65KB x 2 |
| 119 | uint8_t* m = GF256_MUL_TABLE; |
| 120 | uint8_t* d = GF256_DIV_TABLE; |
| 121 |
| 122 | // Unroll y = 0 subtable |
| 123 | for (int x = 0; x < 256; ++x) |
| 124 | { |
| 125 | m[x] = d[x] = 0; |
| 126 | } |
| 127 |
| 128 | // For each other y value, |
| 129 | for (int y = 1; y < 256; ++y) |
| 130 | { |
| 131 | // Calculate log(y) for mult and 255 - log(y) for div |
| 132 | const uint8_t log_y = static_cast<uint8_t>(GF256_LOG_TABLE[y]); |
| 133 | const uint8_t log_yn = 255 - log_y; |
| 134 |
| 135 | // Next subtable |
| 136 | m += 256; |
| 137 | d += 256; |
| 138 |
| 139 | // Unroll x = 0 |
| 140 | m[0] = 0; |
| 141 | d[0] = 0; |
| 142 |
| 143 | // Calculate x * y, x / y |
| 144 | for (int x = 1; x < 256; ++x) |
| 145 | { |
| 146 | uint16_t log_x = GF256_LOG_TABLE[x]; |
| 147 |
| 148 | m[x] = GF256_EXP_TABLE[log_x + log_y]; |
| 149 | d[x] = GF256_EXP_TABLE[log_x + log_yn]; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 |
| 154 | //----------------------------------------------------------------------------- |
| 155 | // Inverse Table |
| 156 |
| 157 | // Initialize INV table using DIV table |
| 158 | void gf256_ctx::gf256_inv_init() |
| 159 | { |
| 160 | for (int x = 0; x < 256; ++x) |
| 161 | { |
| 162 | GF256_INV_TABLE[x] = gf256_div(1, static_cast<uint8_t>(x)); |
| 163 | } |
| 164 | } |
| 165 |
| 166 | //----------------------------------------------------------------------------- |
| 167 | // Multiply and Add Memory Tables |
| 168 |
| 169 | /* |
| 170 | Fast algorithm to compute m[1..8] = a[1..8] * b in GF(256) |
| 171 | using SSE3 SIMD instruction set: |
| 172 |
| 173 | Consider z = x * y in GF(256). |
| 174 | This operation can be performed bit-by-bit. Usefully, the partial product |
| 175 | of each bit is combined linearly with the rest. This means that the 8-bit |
| 176 | number x can be split into its high and low 4 bits, and partial products |
| 177 | can be formed from each half. Then the halves can be linearly combined: |
| 178 |
| 179 | z = x[0..3] * y + x[4..7] * y |
| 180 |
| 181 | The multiplication of each half can be done efficiently via table lookups, |
| 182 | and the addition in GF(256) is XOR. There must be two tables that map 16 |
| 183 | input elements for the low or high 4 bits of x to the two partial products. |
| 184 | Each value for y has a different set of two tables: |
| 185 |
| 186 | z = TABLE_LO_y(x[0..3]) xor TABLE_HI_y(x[4..7]) |
| 187 |
| 188 | This means that we need 16 * 2 * 256 = 8192 bytes for precomputed tables. |
| 189 |
| 190 | Computing z[] = x[] * y can be performed 16 bytes at a time by using the |
| 191 | 128-bit register operations supported by modern processors. |
| 192 |
| 193 | This is efficiently realized in SSE3 using the _mm_shuffle_epi8() function |
| 194 | provided by Visual Studio 2010 or newer in <tmmintrin.h>. This function |
| 195 | uses the low bits to do a table lookup on each byte. Unfortunately the |
| 196 | high bit of each mask byte has the special feature that it clears the |
| 197 | output byte when it is set, so we need to make sure it's cleared by masking |
| 198 | off the high bit of each byte before using it: |
| 199 |
| 200 | clr_mask = _mm_set1_epi8(0x0f) = 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f |
| 201 |
| 202 | For the low half of the partial product, clear the high bit of each byte |
| 203 | and perform the table lookup: |
| 204 |
| 205 | p_lo = _mm_and_si128(x, clr_mask) |
| 206 | p_lo = _mm_shuffle_epi8(p_lo, TABLE_LO_y) |
| 207 |
| 208 | For the high half of the partial product, shift the high 4 bits of each |
| 209 | byte into the low 4 bits and clear the high bit of each byte, and then |
| 210 | perform the table lookup: |
| 211 |
| 212 | p_hi = _mm_srli_epi64(x, 4) |
| 213 | p_hi = _mm_and_si128(p_hi, clr_mask) |
| 214 | p_hi = _mm_shuffle_epi8(p_hi, TABLE_HI_y) |
| 215 |
| 216 | Finally add the two partial products to form the product, recalling that |
| 217 | addition is XOR in a Galois field: |
| 218 |
| 219 | result = _mm_xor_si128(p_lo, p_hi) |
| 220 |
| 221 | This crunches 16 bytes of x at a time, and the result can be stored in z. |
| 222 | */ |
| 223 |
| 224 | /* |
| 225 | Intrinsic reference: |
| 226 |
| 227 | SSE3, VS2010+, tmmintrin.h: |
| 228 |
| 229 | GF256_M128 _mm_shuffle_epi8(GF256_M128 a, GF256_M128 mask); |
| 230 | Emits the Supplemental Streaming SIMD Extensions 3 (SSSE3) instruction pshufb. This instruction shuffles 16-byte parameters from a 128-bit parameter. |
| 231 |
| 232 | Pseudo-code for PSHUFB (with 128 bit operands): |
| 233 |
| 234 | for i = 0 to 15 { |
| 235 | if (SRC[(i * 8)+7] = 1 ) then |
| 236 | DEST[(i*8)+7..(i*8)+0] <- 0; |
| 237 | else |
| 238 | index[3..0] <- SRC[(i*8)+3 .. (i*8)+0]; |
| 239 | DEST[(i*8)+7..(i*8)+0] <- DEST[(index*8+7)..(index*8+0)]; |
| 240 | endif |
| 241 | } |
| 242 |
| 243 | SSE2, VS2008+, emmintrin.h: |
| 244 |
| 245 | GF256_M128 _mm_slli_epi64 (GF256_M128 a, int count); |
| 246 | Shifts the 2 signed or unsigned 64-bit integers in a left by count bits while shifting in zeros. |
| 247 | GF256_M128 _mm_srli_epi64 (GF256_M128 a, int count); |
| 248 | Shifts the 2 signed or unsigned 64-bit integers in a right by count bits while shifting in zeros. |
| 249 | GF256_M128 _mm_set1_epi8 (char b); |
| 250 | Sets the 16 signed 8-bit integer values to b. |
| 251 | GF256_M128 _mm_and_si128 (GF256_M128 a, GF256_M128 b); |
| 252 | Computes the bitwise AND of the 128-bit value in a and the 128-bit value in b. |
| 253 | GF256_M128 _mm_xor_si128 ( GF256_M128 a, GF256_M128 b); |
| 254 | Computes the bitwise XOR of the 128-bit value in a and the 128-bit value in b. |
| 255 | */ |
| 256 |
| 257 | // Initialize the MM256 tables using gf256_mul() |
| 258 | void gf256_ctx::gf256_muladd_mem_init() |
| 259 | { |
| 260 | for (int y = 0; y < 256; ++y) |
| 261 | { |
| 262 | uint8_t lo[16], hi[16]; |
| 263 |
| 264 | // TABLE_LO_Y maps 0..15 to 8-bit partial product based on y. |
| 265 | for (unsigned char x = 0; x < 16; ++x) |
| 266 | { |
| 267 | lo[x] = gf256_mul(x, static_cast<uint8_t>(y)); |
| 268 | hi[x] = gf256_mul(x << 4, static_cast<uint8_t>(y)); |
| 269 | } |
| 270 |
| 271 | const GF256_M128 table_lo = _mm_set_epi8( |
| 272 | lo[15], lo[14], lo[13], lo[12], lo[11], lo[10], lo[9], lo[8], |
| 273 | lo[7], lo[6], lo[5], lo[4], lo[3], lo[2], lo[1], lo[0]); |
| 274 | const GF256_M128 table_hi = _mm_set_epi8( |
| 275 | hi[15], hi[14], hi[13], hi[12], hi[11], hi[10], hi[9], hi[8], |
| 276 | hi[7], hi[6], hi[5], hi[4], hi[3], hi[2], hi[1], hi[0]); |
| 277 | _mm_store_si128(MM256_TABLE_LO_Y + y, table_lo); |
| 278 | _mm_store_si128(MM256_TABLE_HI_Y + y, table_hi); |
| 279 | } |
| 280 | } |
| 281 |
| 282 | //----------------------------------------------------------------------------- |
| 283 | // Initialization |
| 284 | // |
| 285 | // Initialize a context, filling in the tables. |
| 286 | // |
| 287 | // Thread-safety / Usage Notes: |
| 288 | // |
| 289 | // It is perfectly safe and encouraged to use a gf256_ctx object from multiple |
| 290 | // threads. The gf256_init() is relatively expensive and should only be done |
| 291 | // once, though it will take less than a millisecond. |
| 292 | // |
| 293 | // The gf256_ctx object must be aligned to 16 byte boundary. |
| 294 | // Simply tag the object with GF256_ALIGNED to achieve this. |
| 295 | // |
| 296 | // Example: |
| 297 | // static GF256_ALIGNED gf256_ctx TheGF256Context; |
| 298 | // gf256_init(&TheGF256Context, 0); |
| 299 | // |
| 300 | // Returns 0 on success and other values on failure. |
| 301 |
| 302 | int gf256_ctx::gf256_init_() |
| 303 | { |
| 304 | // Avoid multiple initialization |
| 305 | if (initialized) |
| 306 | { |
| 307 | return 0; |
| 308 | } |
| 309 |
| 310 | if (!IsLittleEndian()) |
| 311 | { |
| 312 | fprintf(stderr, "gf256_ctx::gf256_init_: Little Endian architecture expected (code won't work without mods)\n"); |
| 313 | return -2; |
| 314 | } |
| 315 |
| 316 | gf255_poly_init(DefaultPolynomialIndex); |
| 317 | gf256_explog_init(); |
| 318 | gf256_muldiv_init(); |
| 319 | gf256_inv_init(); |
| 320 | gf256_muladd_mem_init(); |
| 321 |
| 322 | initialized = true; |
| 323 | fprintf(stderr, "gf256_ctx::gf256_init_: initialized\n"); |
| 324 | return 0; |
| 325 | } |
| 326 |
| 327 | //----------------------------------------------------------------------------- |
| 328 | // Operations with context |
| 329 |
| 330 | void gf256_ctx::gf256_mul_mem(void* GF256_RESTRICT vz, const void* GF256_RESTRICT vx, uint8_t y, int bytes) |
| 331 | { |
| 332 | // Use a single if-statement to handle special cases |
| 333 | if (y <= 1) |
| 334 | { |
| 335 | if (y == 0) |
| 336 | { |
| 337 | memset(vz, 0, bytes); |
| 338 | } |
| 339 | return; |
| 340 | } |
| 341 |
| 342 | // Partial product tables; see above |
| 343 | const GF256_M128 table_lo_y = _mm_load_si128(MM256_TABLE_LO_Y + y); |
| 344 | const GF256_M128 table_hi_y = _mm_load_si128(MM256_TABLE_HI_Y + y); |
| 345 |
| 346 | // clr_mask = 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f |
| 347 | const GF256_M128 clr_mask = _mm_set1_epi8(0x0f); |
| 348 |
| 349 | GF256_M128* GF256_RESTRICT z16 = reinterpret_cast<GF256_M128*>(vz); |
| 350 | const GF256_M128* GF256_RESTRICT x16 = reinterpret_cast<const GF256_M128*>(vx); |
| 351 |
| 352 | // Handle multiples of 16 bytes |
| 353 | while (bytes >= 16) |
| 354 | { |
| 355 | // See above comments for details |
| 356 | GF256_M128 x0 = _mm_loadu_si128(x16); |
| 357 | GF256_M128 l0 = _mm_and_si128(x0, clr_mask); |
| 358 | x0 = _mm_srli_epi64(x0, 4); |
| 359 | GF256_M128 h0 = _mm_and_si128(x0, clr_mask); |
| 360 | l0 = _mm_shuffle_epi8(table_lo_y, l0); |
| 361 | h0 = _mm_shuffle_epi8(table_hi_y, h0); |
| 362 | _mm_storeu_si128(z16, _mm_xor_si128(l0, h0)); |
| 363 |
| 364 | x16++; |
| 365 | z16++; |
| 366 | bytes -= 16; |
| 367 | } |
| 368 |
| 369 | uint8_t* GF256_RESTRICT z8 = reinterpret_cast<uint8_t*>(z16); |
| 370 | const uint8_t* GF256_RESTRICT x8 = reinterpret_cast<const uint8_t*>(x16); |
| 371 | const uint8_t* GF256_RESTRICT table = GF256_MUL_TABLE + ((unsigned)y << 8); |
| 372 |
| 373 | // Handle a block of 8 bytes |
| 374 | if (bytes >= 8) |
| 375 | { |
| 376 | uint64_t word = table[x8[0]]; |
| 377 | word |= (uint64_t)table[x8[1]] << 8; |
| 378 | word |= (uint64_t)table[x8[2]] << 16; |
| 379 | word |= (uint64_t)table[x8[3]] << 24; |
| 380 | word |= (uint64_t)table[x8[4]] << 32; |
| 381 | word |= (uint64_t)table[x8[5]] << 40; |
| 382 | word |= (uint64_t)table[x8[6]] << 48; |
| 383 | word |= (uint64_t)table[x8[7]] << 56; |
| 384 | *(uint64_t*)z8 = word; |
| 385 |
| 386 | x8 += 8; |
| 387 | z8 += 8; |
| 388 | bytes -= 8; |
| 389 | } |
| 390 |
| 391 | // Handle a block of 4 bytes |
| 392 | if (bytes >= 4) |
| 393 | { |
| 394 | uint32_t word = table[x8[0]]; |
| 395 | word |= (uint32_t)table[x8[1]] << 8; |
| 396 | word |= (uint32_t)table[x8[2]] << 16; |
| 397 | word |= (uint32_t)table[x8[3]] << 24; |
| 398 | *(uint32_t*)z8 = word; |
| 399 |
| 400 | x8 += 4; |
| 401 | z8 += 4; |
| 402 | bytes -= 4; |
| 403 | } |
| 404 |
| 405 | // Handle single bytes |
| 406 | for (int i = bytes; i > 0; i--) |
| 407 | { |
| 408 | z8[i - 1] = table[x8[i - 1]]; |
| 409 | } |
| 410 | } |
| 411 |
| 412 | void gf256_ctx::gf256_muladd_mem(void* GF256_RESTRICT vz, uint8_t y, const void* GF256_RESTRICT vx, int bytes) |
| 413 | { |
| 414 | // Use a single if-statement to handle special cases |
| 415 | if (y <= 1) |
| 416 | { |
| 417 | if (y == 1) |
| 418 | { |
| 419 | gf256_add_mem(vz, vx, bytes); |
| 420 | } |
| 421 | return; |
| 422 | } |
| 423 |
| 424 | // Partial product tables; see above |
| 425 | const GF256_M128 table_lo_y = _mm_load_si128(MM256_TABLE_LO_Y + y); |
| 426 | const GF256_M128 table_hi_y = _mm_load_si128(MM256_TABLE_HI_Y + y); |
| 427 |
| 428 | // clr_mask = 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f |
| 429 | const GF256_M128 clr_mask = _mm_set1_epi8(0x0f); |
| 430 |
| 431 | GF256_M128* GF256_RESTRICT z16 = reinterpret_cast<GF256_M128*>(vz); |
| 432 | const GF256_M128* GF256_RESTRICT x16 = reinterpret_cast<const GF256_M128*>(vx); |
| 433 |
| 434 | // Handle multiples of 16 bytes |
| 435 | while (bytes >= 16) |
| 436 | { |
| 437 | // See above comments for details |
| 438 | GF256_M128 x0 = _mm_loadu_si128(x16); |
| 439 | GF256_M128 l0 = _mm_and_si128(x0, clr_mask); |
| 440 | x0 = _mm_srli_epi64(x0, 4); |
| 441 | GF256_M128 h0 = _mm_and_si128(x0, clr_mask); |
| 442 | l0 = _mm_shuffle_epi8(table_lo_y, l0); |
| 443 | h0 = _mm_shuffle_epi8(table_hi_y, h0); |
| 444 | const GF256_M128 p0 = _mm_xor_si128(l0, h0); |
| 445 | const GF256_M128 z0 = _mm_loadu_si128(z16); |
| 446 | _mm_storeu_si128(z16, _mm_xor_si128(p0, z0)); |
| 447 |
| 448 | x16++; |
| 449 | z16++; |
| 450 | bytes -= 16; |
| 451 | } |
| 452 |
| 453 | uint8_t* GF256_RESTRICT z8 = reinterpret_cast<uint8_t*>(z16); |
| 454 | const uint8_t* GF256_RESTRICT x8 = reinterpret_cast<const uint8_t*>(x16); |
| 455 | const uint8_t* GF256_RESTRICT table = GF256_MUL_TABLE + ((unsigned)y << 8); |
| 456 |
| 457 | // Handle a block of 8 bytes |
| 458 | if (bytes >= 8) |
| 459 | { |
| 460 | uint64_t word = table[x8[0]]; |
| 461 | word |= (uint64_t)table[x8[1]] << 8; |
| 462 | word |= (uint64_t)table[x8[2]] << 16; |
| 463 | word |= (uint64_t)table[x8[3]] << 24; |
| 464 | word |= (uint64_t)table[x8[4]] << 32; |
| 465 | word |= (uint64_t)table[x8[5]] << 40; |
| 466 | word |= (uint64_t)table[x8[6]] << 48; |
| 467 | word |= (uint64_t)table[x8[7]] << 56; |
| 468 | *(uint64_t*)z8 ^= word; |
| 469 |
| 470 | x8 += 8; |
| 471 | z8 += 8; |
| 472 | bytes -= 8; |
| 473 | } |
| 474 |
| 475 | // Handle a block of 4 bytes |
| 476 | if (bytes >= 4) |
| 477 | { |
| 478 | uint32_t word = table[x8[0]]; |
| 479 | word |= (uint32_t)table[x8[1]] << 8; |
| 480 | word |= (uint32_t)table[x8[2]] << 16; |
| 481 | word |= (uint32_t)table[x8[3]] << 24; |
| 482 | *(uint32_t*)z8 ^= word; |
| 483 |
| 484 | x8 += 4; |
| 485 | z8 += 4; |
| 486 | bytes -= 4; |
| 487 | } |
| 488 |
| 489 | // Handle single bytes |
| 490 | for (int i = bytes; i > 0; i--) |
| 491 | { |
| 492 | z8[i - 1] ^= table[x8[i - 1]]; |
| 493 | } |
| 494 | } |
| 495 |
| 496 | //----------------------------------------------------------------------------- |
| 497 | // Static operations |
| 498 |
| 499 | void gf256_ctx::gf256_add_mem(void* GF256_RESTRICT vx, const void* GF256_RESTRICT vy, int bytes) |
| 500 | { |
| 501 | GF256_M128* GF256_RESTRICT x16 = reinterpret_cast<GF256_M128*>(vx); |
| 502 | const GF256_M128* GF256_RESTRICT y16 = reinterpret_cast<const GF256_M128*>(vy); |
| 503 |
| 504 | // Handle multiples of 64 bytes |
| 505 | while (bytes >= 64) |
| 506 | { |
| 507 | GF256_M128 x0 = _mm_loadu_si128(x16); |
| 508 | GF256_M128 x1 = _mm_loadu_si128(x16 + 1); |
| 509 | GF256_M128 x2 = _mm_loadu_si128(x16 + 2); |
| 510 | GF256_M128 x3 = _mm_loadu_si128(x16 + 3); |
| 511 | GF256_M128 y0 = _mm_loadu_si128(y16); |
| 512 | GF256_M128 y1 = _mm_loadu_si128(y16 + 1); |
| 513 | GF256_M128 y2 = _mm_loadu_si128(y16 + 2); |
| 514 | GF256_M128 y3 = _mm_loadu_si128(y16 + 3); |
| 515 |
| 516 | _mm_storeu_si128(x16, |
| 517 | _mm_xor_si128(x0, y0)); |
| 518 | _mm_storeu_si128(x16 + 1, |
| 519 | _mm_xor_si128(x1, y1)); |
| 520 | _mm_storeu_si128(x16 + 2, |
| 521 | _mm_xor_si128(x2, y2)); |
| 522 | _mm_storeu_si128(x16 + 3, |
| 523 | _mm_xor_si128(x3, y3)); |
| 524 |
| 525 | x16 += 4; |
| 526 | y16 += 4; |
| 527 | bytes -= 64; |
| 528 | } |
| 529 |
| 530 | // Handle multiples of 16 bytes |
| 531 | while (bytes >= 16) |
| 532 | { |
| 533 | // x[i] = x[i] xor y[i] |
| 534 | _mm_storeu_si128(x16, |
| 535 | _mm_xor_si128( |
| 536 | _mm_loadu_si128(x16), |
| 537 | _mm_loadu_si128(y16))); |
| 538 |
| 539 | x16++; |
| 540 | y16++; |
| 541 | bytes -= 16; |
| 542 | } |
| 543 |
| 544 | uint8_t* GF256_RESTRICT x1 = reinterpret_cast<uint8_t*>(x16); |
| 545 | const uint8_t* GF256_RESTRICT y1 = reinterpret_cast<const uint8_t*>(y16); |
| 546 |
| 547 | // Handle a block of 8 bytes |
| 548 | if (bytes >= 8) |
| 549 | { |
| 550 | uint64_t* GF256_RESTRICT x8 = reinterpret_cast<uint64_t*>(x1); |
| 551 | const uint64_t* GF256_RESTRICT y8 = reinterpret_cast<const uint64_t*>(y1); |
| 552 | *x8 ^= *y8; |
| 553 |
| 554 | x1 += 8; |
| 555 | y1 += 8; |
| 556 | bytes -= 8; |
| 557 | } |
| 558 |
| 559 | // Handle a block of 4 bytes |
| 560 | if (bytes >= 4) |
| 561 | { |
| 562 | uint32_t* GF256_RESTRICT x4 = reinterpret_cast<uint32_t*>(x1); |
| 563 | const uint32_t* GF256_RESTRICT y4 = reinterpret_cast<const uint32_t*>(y1); |
| 564 | *x4 ^= *y4; |
| 565 |
| 566 | x1 += 4; |
| 567 | y1 += 4; |
| 568 | bytes -= 4; |
| 569 | } |
| 570 |
| 571 | // Handle final bytes |
| 572 | for (int i = bytes; i > 0; i--) |
| 573 | { |
| 574 | x1[i - 1] ^= y1[i - 1]; |
| 575 | } |
| 576 | } |
| 577 |
| 578 | void gf256_ctx::gf256_add2_mem(void* GF256_RESTRICT vz, const void* GF256_RESTRICT vx, const void* GF256_RESTRICT vy, int bytes) |
| 579 | { |
| 580 | GF256_M128* GF256_RESTRICT z16 = reinterpret_cast<GF256_M128*>(vz); |
| 581 | const GF256_M128* GF256_RESTRICT x16 = reinterpret_cast<const GF256_M128*>(vx); |
| 582 | const GF256_M128* GF256_RESTRICT y16 = reinterpret_cast<const GF256_M128*>(vy); |
| 583 |
| 584 | // Handle multiples of 16 bytes |
| 585 | while (bytes >= 16) |
| 586 | { |
| 587 | // z[i] = x[i] xor y[i] |
| 588 | _mm_storeu_si128(z16, |
| 589 | _mm_xor_si128( |
| 590 | _mm_loadu_si128(z16), |
| 591 | _mm_xor_si128( |
| 592 | _mm_loadu_si128(x16), |
| 593 | _mm_loadu_si128(y16)))); |
| 594 |
| 595 | x16++; |
| 596 | y16++; |
| 597 | z16++; |
| 598 | bytes -= 16; |
| 599 | } |
| 600 |
| 601 | uint8_t* GF256_RESTRICT z1 = reinterpret_cast<uint8_t*>(z16); |
| 602 | const uint8_t* GF256_RESTRICT x1 = reinterpret_cast<const uint8_t*>(x16); |
| 603 | const uint8_t* GF256_RESTRICT y1 = reinterpret_cast<const uint8_t*>(y16); |
| 604 |
| 605 | // Handle a block of 8 bytes |
| 606 | if (bytes >= 8) |
| 607 | { |
| 608 | uint64_t* GF256_RESTRICT z8 = reinterpret_cast<uint64_t*>(z1); |
| 609 | const uint64_t* GF256_RESTRICT x8 = reinterpret_cast<const uint64_t*>(x1); |
| 610 | const uint64_t* GF256_RESTRICT y8 = reinterpret_cast<const uint64_t*>(y1); |
| 611 | *z8 ^= *x8 ^ *y8; |
| 612 |
| 613 | x1 += 8; |
| 614 | y1 += 8; |
| 615 | z1 += 8; |
| 616 | bytes -= 8; |
| 617 | } |
| 618 |
| 619 | // Handle a block of 4 bytes |
| 620 | if (bytes >= 4) |
| 621 | { |
| 622 | uint32_t* GF256_RESTRICT z4 = reinterpret_cast<uint32_t*>(z1); |
| 623 | const uint32_t* GF256_RESTRICT x4 = reinterpret_cast<const uint32_t*>(x1); |
| 624 | const uint32_t* GF256_RESTRICT y4 = reinterpret_cast<const uint32_t*>(y1); |
| 625 | *z4 ^= *x4 ^ *y4; |
| 626 |
| 627 | x1 += 4; |
| 628 | y1 += 4; |
| 629 | z1 += 4; |
| 630 | bytes -= 4; |
| 631 | } |
| 632 |
| 633 | // Handle final bytes |
| 634 | for (int i = bytes; i > 0; i--) |
| 635 | { |
| 636 | z1[i - 1] ^= x1[i - 1] ^ y1[i - 1]; |
| 637 | } |
| 638 | } |
| 639 |
| 640 | void gf256_ctx::gf256_addset_mem(void* GF256_RESTRICT vz, const void* GF256_RESTRICT vx, const void* GF256_RESTRICT vy, int bytes) |
| 641 | { |
| 642 | GF256_M128* GF256_RESTRICT z16 = reinterpret_cast<GF256_M128*>(vz); |
| 643 | const GF256_M128* GF256_RESTRICT x16 = reinterpret_cast<const GF256_M128*>(vx); |
| 644 | const GF256_M128* GF256_RESTRICT y16 = reinterpret_cast<const GF256_M128*>(vy); |
| 645 |
| 646 | // Handle multiples of 64 bytes |
| 647 | while (bytes >= 64) |
| 648 | { |
| 649 | GF256_M128 x0 = _mm_loadu_si128(x16); |
| 650 | GF256_M128 x1 = _mm_loadu_si128(x16 + 1); |
| 651 | GF256_M128 x2 = _mm_loadu_si128(x16 + 2); |
| 652 | GF256_M128 x3 = _mm_loadu_si128(x16 + 3); |
| 653 | GF256_M128 y0 = _mm_loadu_si128(y16); |
| 654 | GF256_M128 y1 = _mm_loadu_si128(y16 + 1); |
| 655 | GF256_M128 y2 = _mm_loadu_si128(y16 + 2); |
| 656 | GF256_M128 y3 = _mm_loadu_si128(y16 + 3); |
| 657 |
| 658 | _mm_storeu_si128(z16, _mm_xor_si128(x0, y0)); |
| 659 | _mm_storeu_si128(z16 + 1, _mm_xor_si128(x1, y1)); |
| 660 | _mm_storeu_si128(z16 + 2, _mm_xor_si128(x2, y2)); |
| 661 | _mm_storeu_si128(z16 + 3, _mm_xor_si128(x3, y3)); |
| 662 |
| 663 | x16 += 4; |
| 664 | y16 += 4; |
| 665 | z16 += 4; |
| 666 | bytes -= 64; |
| 667 | } |
| 668 |
| 669 | // Handle multiples of 16 bytes |
| 670 | while (bytes >= 16) |
| 671 | { |
| 672 | // z[i] = x[i] xor y[i] |
| 673 | _mm_storeu_si128(z16, |
| 674 | _mm_xor_si128( |
| 675 | _mm_loadu_si128(x16), |
| 676 | _mm_loadu_si128(y16))); |
| 677 |
| 678 | x16++; |
| 679 | y16++; |
| 680 | z16++; |
| 681 | bytes -= 16; |
| 682 | } |
| 683 |
| 684 | uint8_t* GF256_RESTRICT z1 = reinterpret_cast<uint8_t*>(z16); |
| 685 | const uint8_t* GF256_RESTRICT x1 = reinterpret_cast<const uint8_t*>(x16); |
| 686 | const uint8_t* GF256_RESTRICT y1 = reinterpret_cast<const uint8_t*>(y16); |
| 687 |
| 688 | // Handle a block of 8 bytes |
| 689 | if (bytes >= 8) |
| 690 | { |
| 691 | uint64_t* GF256_RESTRICT z8 = reinterpret_cast<uint64_t*>(z1); |
| 692 | const uint64_t* GF256_RESTRICT x8 = reinterpret_cast<const uint64_t*>(x1); |
| 693 | const uint64_t* GF256_RESTRICT y8 = reinterpret_cast<const uint64_t*>(y1); |
| 694 | *z8 = *x8 ^ *y8; |
| 695 |
| 696 | x1 += 8; |
| 697 | y1 += 8; |
| 698 | z1 += 8; |
| 699 | bytes -= 8; |
| 700 | } |
| 701 |
| 702 | // Handle a block of 4 bytes |
| 703 | if (bytes >= 4) |
| 704 | { |
| 705 | uint32_t* GF256_RESTRICT z4 = reinterpret_cast<uint32_t*>(z1); |
| 706 | const uint32_t* GF256_RESTRICT x4 = reinterpret_cast<const uint32_t*>(x1); |
| 707 | const uint32_t* GF256_RESTRICT y4 = reinterpret_cast<const uint32_t*>(y1); |
| 708 | *z4 = *x4 ^ *y4; |
| 709 |
| 710 | x1 += 4; |
| 711 | y1 += 4; |
| 712 | z1 += 4; |
| 713 | bytes -= 4; |
| 714 | } |
| 715 |
| 716 | // Handle final bytes |
| 717 | for (int i = bytes; i > 0; i--) |
| 718 | { |
| 719 | z1[i - 1] = x1[i - 1] ^ y1[i - 1]; |
| 720 | } |
| 721 | } |
| 722 |
| 723 | void gf256_memswap(void* GF256_RESTRICT vx, void* GF256_RESTRICT vy, int bytes) |
| 724 | { |
| 725 | GF256_M128* GF256_RESTRICT x16 = reinterpret_cast<GF256_M128*>(vx); |
| 726 | GF256_M128* GF256_RESTRICT y16 = reinterpret_cast<GF256_M128*>(vy); |
| 727 |
| 728 | // Handle blocks of 16 bytes |
| 729 | while (bytes >= 16) |
| 730 | { |
| 731 | GF256_M128 x0 = _mm_loadu_si128(x16); |
| 732 | GF256_M128 y0 = _mm_loadu_si128(y16); |
| 733 | _mm_storeu_si128(x16, y0); |
| 734 | _mm_storeu_si128(y16, x0); |
| 735 |
| 736 | bytes -= 16; |
| 737 | ++x16; |
| 738 | ++y16; |
| 739 | } |
| 740 |
| 741 | uint8_t* GF256_RESTRICT x1 = reinterpret_cast<uint8_t*>(x16); |
| 742 | uint8_t* GF256_RESTRICT y1 = reinterpret_cast<uint8_t*>(y16); |
| 743 |
| 744 | // Handle a block of 8 bytes |
| 745 | if (bytes >= 8) |
| 746 | { |
| 747 | uint64_t* GF256_RESTRICT x8 = reinterpret_cast<uint64_t*>(x1); |
| 748 | uint64_t* GF256_RESTRICT y8 = reinterpret_cast<uint64_t*>(y1); |
| 749 |
| 750 | uint64_t temp = *x8; |
| 751 | *x8 = *y8; |
| 752 | *y8 = temp; |
| 753 |
| 754 | x1 += 8; |
| 755 | y1 += 8; |
| 756 | bytes -= 8; |
| 757 | } |
| 758 |
| 759 | // Handle a block of 4 bytes |
| 760 | if (bytes >= 4) |
| 761 | { |
| 762 | uint32_t* GF256_RESTRICT x4 = reinterpret_cast<uint32_t*>(x1); |
| 763 | uint32_t* GF256_RESTRICT y4 = reinterpret_cast<uint32_t*>(y1); |
| 764 |
| 765 | uint32_t temp = *x4; |
| 766 | *x4 = *y4; |
| 767 | *y4 = temp; |
| 768 |
| 769 | x1 += 4; |
| 770 | y1 += 4; |
| 771 | bytes -= 4; |
| 772 | } |
| 773 |
| 774 | // Handle final bytes |
| 775 | uint8_t temp; |
| 776 |
| 777 | for (int i = bytes; i > 0; i--) |
| 778 | { |
| 779 | temp = x1[i - 1]; |
| 780 | x1[i - 1] = y1[i - 1]; |
| 781 | y1[i - 1] = temp; |
| 782 | } |
| 783 | } |
| 784 |