| 1 |
package reedsolomon |
| 2 |
|
| 3 |
// This is a O(n*log n) implementation of Reed-Solomon |
| 4 |
// codes, ported from the C++ library https://github.com/catid/leopard. |
| 5 |
// |
| 6 |
// The implementation is based on the paper |
| 7 |
// |
| 8 |
// S.-J. Lin, T. Y. Al-Naffouri, Y. S. Han, and W.-H. Chung, |
| 9 |
// "Novel Polynomial Basis with Fast Fourier Transform |
| 10 |
// and Its Application to Reed-Solomon Erasure Codes" |
| 11 |
// IEEE Trans. on Information Theory, pp. 6284-6299, November, 2016. |
| 12 |
|
| 13 |
import ( |
| 14 |
"bytes" |
| 15 |
"encoding/binary" |
| 16 |
"io" |
| 17 |
"math/bits" |
| 18 |
"sync" |
| 19 |
) |
| 20 |
|
| 21 |
// leopardFF8 is like reedSolomon but for the 8-bit "leopard" implementation. |
| 22 |
type leopardFF8 struct { |
| 23 |
dataShards int // Number of data shards, should not be modified. |
| 24 |
parityShards int // Number of parity shards, should not be modified. |
| 25 |
totalShards int // Total number of shards. Calculated, and should not be modified. |
| 26 |
|
| 27 |
workAlloc WorkAllocator |
| 28 |
inversion map[[inversion8Bytes]byte]leopardGF8cache |
| 29 |
inversionMu sync.Mutex |
| 30 |
|
| 31 |
o options |
| 32 |
} |
| 33 |
|
| 34 |
const inversion8Bytes = 256 / 8 |
| 35 |
|
| 36 |
type leopardGF8cache struct { |
| 37 |
errorLocs [256]ffe8 |
| 38 |
bits *errorBitfield8 |
| 39 |
} |
| 40 |
|
| 41 |
// newFF8 is like New, but for the 8-bit "leopard" implementation. |
| 42 |
func newFF8(dataShards, parityShards int, opt options) (*leopardFF8, error) { |
| 43 |
initConstants8() |
| 44 |
|
| 45 |
if dataShards <= 0 || parityShards <= 0 { |
| 46 |
return nil, ErrInvShardNum |
| 47 |
} |
| 48 |
|
| 49 |
if dataShards+parityShards > 65536 { |
| 50 |
return nil, ErrMaxShardNum |
| 51 |
} |
| 52 |
|
| 53 |
r := &leopardFF8{ |
| 54 |
dataShards: dataShards, |
| 55 |
parityShards: parityShards, |
| 56 |
totalShards: dataShards + parityShards, |
| 57 |
workAlloc: opt.workAlloc, |
| 58 |
o: opt, |
| 59 |
} |
| 60 |
if opt.inversionCache && (r.totalShards <= 64 || opt.forcedInversionCache) { |
| 61 |
// Inversion cache is relatively ineffective for big shard counts and takes up potentially lots of memory |
| 62 |
// r.totalShards is not covering the space, but an estimate. |
| 63 |
r.inversion = make(map[[inversion8Bytes]byte]leopardGF8cache, r.totalShards) |
| 64 |
} |
| 65 |
return r, nil |
| 66 |
} |
| 67 |
|
| 68 |
var _ = Extensions(&leopardFF8{}) |
| 69 |
|
| 70 |
func (r *leopardFF8) ShardSizeMultiple() int { |
| 71 |
return 64 |
| 72 |
} |
| 73 |
|
| 74 |
func (r *leopardFF8) DataShards() int { |
| 75 |
return r.dataShards |
| 76 |
} |
| 77 |
|
| 78 |
func (r *leopardFF8) ParityShards() int { |
| 79 |
return r.parityShards |
| 80 |
} |
| 81 |
|
| 82 |
func (r *leopardFF8) TotalShards() int { |
| 83 |
return r.totalShards |
| 84 |
} |
| 85 |
|
| 86 |
func (r *leopardFF8) AllocAligned(each int) [][]byte { |
| 87 |
return AllocAligned(r.totalShards, each) |
| 88 |
} |
| 89 |
|
| 90 |
func (r *leopardFF8) DecodeIdx(dst [][]byte, expectInput []bool, input [][]byte) error { |
| 91 |
return ErrNotSupported |
| 92 |
} |
| 93 |
|
| 94 |
type ffe8 uint8 |
| 95 |
|
| 96 |
const ( |
| 97 |
bitwidth8 = 8 |
| 98 |
order8 = 1 << bitwidth8 |
| 99 |
modulus8 = order8 - 1 |
| 100 |
polynomial8 = 0x11D |
| 101 |
|
| 102 |
// Encode in blocks of this size. |
| 103 |
workSize8 = 32 << 10 |
| 104 |
) |
| 105 |
|
| 106 |
var ( |
| 107 |
fftSkew8 *[modulus8]ffe8 |
| 108 |
logWalsh8 *[order8]ffe8 |
| 109 |
) |
| 110 |
|
| 111 |
// Logarithm Tables |
| 112 |
var ( |
| 113 |
logLUT8 *[order8]ffe8 |
| 114 |
expLUT8 *[order8]ffe8 |
| 115 |
) |
| 116 |
|
| 117 |
// Stores the partial products of x * y at offset x + y * 256 |
| 118 |
// Repeated accesses from the same y value are faster |
| 119 |
var mul8LUTs *[order8]mul8LUT |
| 120 |
|
| 121 |
type mul8LUT struct { |
| 122 |
Value [256]ffe8 |
| 123 |
} |
| 124 |
|
| 125 |
// Stores lookup for avx2 |
| 126 |
var multiply256LUT8 *[order8][2 * 16]byte |
| 127 |
|
| 128 |
func (r *leopardFF8) Encode(shards [][]byte) error { |
| 129 |
if len(shards) != r.totalShards { |
| 130 |
return ErrTooFewShards |
| 131 |
} |
| 132 |
|
| 133 |
if err := checkShards(shards, false); err != nil { |
| 134 |
return err |
| 135 |
} |
| 136 |
return r.encode(shards) |
| 137 |
} |
| 138 |
|
| 139 |
func (r *leopardFF8) encode(shards [][]byte) error { |
| 140 |
shardSize := shardSize(shards) |
| 141 |
if shardSize%64 != 0 { |
| 142 |
return ErrInvalidShardSize |
| 143 |
} |
| 144 |
|
| 145 |
m := ceilPow2(r.parityShards) |
| 146 |
work := r.workAlloc.Get(m*2, workSize8) |
| 147 |
defer r.workAlloc.Put(work) |
| 148 |
|
| 149 |
mtrunc := min(r.dataShards, m) |
| 150 |
|
| 151 |
skewLUT := fftSkew8[m-1:] |
| 152 |
|
| 153 |
// Split large shards. |
| 154 |
// More likely on lower shard count. |
| 155 |
off := 0 |
| 156 |
sh := make([][]byte, len(shards)) |
| 157 |
|
| 158 |
// work slice we can modify |
| 159 |
wMod := make([][]byte, len(work)) |
| 160 |
copy(wMod, work) |
| 161 |
for off < shardSize { |
| 162 |
work := wMod |
| 163 |
sh := sh |
| 164 |
end := off + workSize8 |
| 165 |
if end > shardSize { |
| 166 |
end = shardSize |
| 167 |
sz := shardSize - off |
| 168 |
for i := range work { |
| 169 |
// Last iteration only... |
| 170 |
work[i] = work[i][:sz] |
| 171 |
} |
| 172 |
} |
| 173 |
for i := range shards { |
| 174 |
sh[i] = shards[i][off:end] |
| 175 |
} |
| 176 |
|
| 177 |
// Replace work slices, so we write directly to output. |
| 178 |
// Note that work has parity *before* data shards. |
| 179 |
res := shards[r.dataShards:r.totalShards] |
| 180 |
for i := range res { |
| 181 |
work[i] = res[i][off:end] |
| 182 |
} |
| 183 |
|
| 184 |
ifftDITEncoder8( |
| 185 |
sh[:r.dataShards], |
| 186 |
mtrunc, |
| 187 |
work, |
| 188 |
nil, // No xor output |
| 189 |
m, |
| 190 |
skewLUT, |
| 191 |
&r.o, |
| 192 |
) |
| 193 |
|
| 194 |
lastCount := r.dataShards % m |
| 195 |
skewLUT2 := skewLUT |
| 196 |
if m >= r.dataShards { |
| 197 |
goto skip_body |
| 198 |
} |
| 199 |
|
| 200 |
// For sets of m data pieces: |
| 201 |
for i := m; i+m <= r.dataShards; i += m { |
| 202 |
sh = sh[m:] |
| 203 |
skewLUT2 = skewLUT2[m:] |
| 204 |
|
| 205 |
// work <- work xor IFFT(data + i, m, m + i) |
| 206 |
|
| 207 |
ifftDITEncoder8( |
| 208 |
sh, // data source |
| 209 |
m, |
| 210 |
work[m:], // temporary workspace |
| 211 |
work, // xor destination |
| 212 |
m, |
| 213 |
skewLUT2, |
| 214 |
&r.o, |
| 215 |
) |
| 216 |
} |
| 217 |
|
| 218 |
// Handle final partial set of m pieces: |
| 219 |
if lastCount != 0 { |
| 220 |
sh = sh[m:] |
| 221 |
skewLUT2 = skewLUT2[m:] |
| 222 |
|
| 223 |
// work <- work xor IFFT(data + i, m, m + i) |
| 224 |
|
| 225 |
ifftDITEncoder8( |
| 226 |
sh, // data source |
| 227 |
lastCount, |
| 228 |
work[m:], // temporary workspace |
| 229 |
work, // xor destination |
| 230 |
m, |
| 231 |
skewLUT2, |
| 232 |
&r.o, |
| 233 |
) |
| 234 |
} |
| 235 |
|
| 236 |
skip_body: |
| 237 |
// work <- FFT(work, m, 0) |
| 238 |
fftDIT8(work, r.parityShards, m, fftSkew8[:], &r.o) |
| 239 |
off += workSize8 |
| 240 |
} |
| 241 |
|
| 242 |
return nil |
| 243 |
} |
| 244 |
|
| 245 |
func (r *leopardFF8) EncodeIdx(dataShard []byte, idx int, parity [][]byte) error { |
| 246 |
return ErrNotSupported |
| 247 |
} |
| 248 |
|
| 249 |
func (r *leopardFF8) Join(dst io.Writer, shards [][]byte, outSize int) error { |
| 250 |
// Do we have enough shards? |
| 251 |
if len(shards) < r.dataShards { |
| 252 |
return ErrTooFewShards |
| 253 |
} |
| 254 |
shards = shards[:r.dataShards] |
| 255 |
|
| 256 |
// Do we have enough data? |
| 257 |
size := 0 |
| 258 |
for _, shard := range shards { |
| 259 |
if shard == nil { |
| 260 |
return ErrReconstructRequired |
| 261 |
} |
| 262 |
size += len(shard) |
| 263 |
|
| 264 |
// Do we have enough data already? |
| 265 |
if size >= outSize { |
| 266 |
break |
| 267 |
} |
| 268 |
} |
| 269 |
if size < outSize { |
| 270 |
return ErrShortData |
| 271 |
} |
| 272 |
|
| 273 |
// Copy data to dst |
| 274 |
write := outSize |
| 275 |
for _, shard := range shards { |
| 276 |
if write < len(shard) { |
| 277 |
_, err := dst.Write(shard[:write]) |
| 278 |
return err |
| 279 |
} |
| 280 |
n, err := dst.Write(shard) |
| 281 |
if err != nil { |
| 282 |
return err |
| 283 |
} |
| 284 |
write -= n |
| 285 |
} |
| 286 |
return nil |
| 287 |
} |
| 288 |
|
| 289 |
func (r *leopardFF8) Update(shards [][]byte, newDatashards [][]byte) error { |
| 290 |
return ErrNotSupported |
| 291 |
} |
| 292 |
|
| 293 |
func (r *leopardFF8) Split(data []byte) ([][]byte, error) { |
| 294 |
if len(data) == 0 { |
| 295 |
return nil, ErrShortData |
| 296 |
} |
| 297 |
if r.totalShards == 1 && len(data)&63 == 0 { |
| 298 |
return [][]byte{data}, nil |
| 299 |
} |
| 300 |
|
| 301 |
dataLen := len(data) |
| 302 |
// Calculate number of bytes per data shard. |
| 303 |
perShard := (len(data) + r.dataShards - 1) / r.dataShards |
| 304 |
perShard = ((perShard + 63) / 64) * 64 |
| 305 |
needTotal := r.totalShards * perShard |
| 306 |
|
| 307 |
if cap(data) > len(data) { |
| 308 |
if cap(data) > needTotal { |
| 309 |
data = data[:needTotal] |
| 310 |
} else { |
| 311 |
data = data[:cap(data)] |
| 312 |
} |
| 313 |
clear(data[dataLen:]) |
| 314 |
} |
| 315 |
|
| 316 |
// Only allocate memory if necessary |
| 317 |
var padding [][]byte |
| 318 |
if len(data) < needTotal { |
| 319 |
// calculate maximum number of full shards in `data` slice |
| 320 |
fullShards := len(data) / perShard |
| 321 |
padding = AllocAligned(r.totalShards-fullShards, perShard) |
| 322 |
if dataLen > perShard*fullShards { |
| 323 |
// Copy partial shards |
| 324 |
copyFrom := data[perShard*fullShards : dataLen] |
| 325 |
for i := range padding { |
| 326 |
if len(copyFrom) == 0 { |
| 327 |
break |
| 328 |
} |
| 329 |
copyFrom = copyFrom[copy(padding[i], copyFrom):] |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
// Split into equal-length shards. |
| 335 |
dst := make([][]byte, r.totalShards) |
| 336 |
i := 0 |
| 337 |
for ; i < len(dst) && len(data) >= perShard; i++ { |
| 338 |
dst[i] = data[:perShard:perShard] |
| 339 |
data = data[perShard:] |
| 340 |
} |
| 341 |
|
| 342 |
for j := 0; i+j < len(dst); j++ { |
| 343 |
dst[i+j] = padding[0] |
| 344 |
padding = padding[1:] |
| 345 |
} |
| 346 |
|
| 347 |
return dst, nil |
| 348 |
} |
| 349 |
|
| 350 |
func (r *leopardFF8) ReconstructSome(shards [][]byte, required []bool) error { |
| 351 |
if len(required) == r.totalShards { |
| 352 |
return r.reconstruct(shards, true) |
| 353 |
} |
| 354 |
return r.reconstruct(shards, false) |
| 355 |
} |
| 356 |
|
| 357 |
func (r *leopardFF8) Reconstruct(shards [][]byte) error { |
| 358 |
return r.reconstruct(shards, true) |
| 359 |
} |
| 360 |
|
| 361 |
func (r *leopardFF8) ReconstructData(shards [][]byte) error { |
| 362 |
return r.reconstruct(shards, false) |
| 363 |
} |
| 364 |
|
| 365 |
func (r *leopardFF8) Verify(shards [][]byte) (bool, error) { |
| 366 |
if len(shards) != r.totalShards { |
| 367 |
return false, ErrTooFewShards |
| 368 |
} |
| 369 |
if err := checkShards(shards, false); err != nil { |
| 370 |
return false, err |
| 371 |
} |
| 372 |
|
| 373 |
// Re-encode parity shards to temporary storage. |
| 374 |
shardSize := len(shards[0]) |
| 375 |
outputs := make([][]byte, r.totalShards) |
| 376 |
copy(outputs, shards[:r.dataShards]) |
| 377 |
for i := r.dataShards; i < r.totalShards; i++ { |
| 378 |
outputs[i] = make([]byte, shardSize) |
| 379 |
} |
| 380 |
if err := r.Encode(outputs); err != nil { |
| 381 |
return false, err |
| 382 |
} |
| 383 |
|
| 384 |
// Compare. |
| 385 |
for i := r.dataShards; i < r.totalShards; i++ { |
| 386 |
if !bytes.Equal(outputs[i], shards[i]) { |
| 387 |
return false, nil |
| 388 |
} |
| 389 |
} |
| 390 |
return true, nil |
| 391 |
} |
| 392 |
|
| 393 |
func (r *leopardFF8) reconstruct(shards [][]byte, recoverAll bool) error { |
| 394 |
if len(shards) != r.totalShards { |
| 395 |
return ErrTooFewShards |
| 396 |
} |
| 397 |
|
| 398 |
if err := checkShards(shards, true); err != nil { |
| 399 |
return err |
| 400 |
} |
| 401 |
|
| 402 |
// Quick check: are all of the shards present? If so, there's |
| 403 |
// nothing to do. |
| 404 |
numberPresent := 0 |
| 405 |
dataPresent := 0 |
| 406 |
for i := 0; i < r.totalShards; i++ { |
| 407 |
if len(shards[i]) != 0 { |
| 408 |
numberPresent++ |
| 409 |
if i < r.dataShards { |
| 410 |
dataPresent++ |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
if numberPresent == r.totalShards || !recoverAll && dataPresent == r.dataShards { |
| 415 |
// Cool. All of the shards have data. We don't |
| 416 |
// need to do anything. |
| 417 |
return nil |
| 418 |
} |
| 419 |
|
| 420 |
// Check if we have enough to reconstruct. |
| 421 |
if numberPresent < r.dataShards { |
| 422 |
return ErrTooFewShards |
| 423 |
} |
| 424 |
|
| 425 |
shardSize := shardSize(shards) |
| 426 |
if shardSize%64 != 0 { |
| 427 |
return ErrInvalidShardSize |
| 428 |
} |
| 429 |
|
| 430 |
// Use only if we are missing less than 1/4 parity, |
| 431 |
// And we are restoring a significant amount of data. |
| 432 |
useBits := r.totalShards-numberPresent <= r.parityShards/4 && shardSize*r.totalShards >= 64<<10 |
| 433 |
|
| 434 |
m := ceilPow2(r.parityShards) |
| 435 |
n := ceilPow2(m + r.dataShards) |
| 436 |
|
| 437 |
const LEO_ERROR_BITFIELD_OPT = true |
| 438 |
|
| 439 |
// Fill in error locations. |
| 440 |
var errorBits errorBitfield8 |
| 441 |
var errLocs [order8]ffe8 |
| 442 |
for i := 0; i < r.parityShards; i++ { |
| 443 |
if len(shards[i+r.dataShards]) == 0 { |
| 444 |
errLocs[i] = 1 |
| 445 |
if LEO_ERROR_BITFIELD_OPT && recoverAll { |
| 446 |
errorBits.set(i) |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
for i := r.parityShards; i < m; i++ { |
| 451 |
errLocs[i] = 1 |
| 452 |
if LEO_ERROR_BITFIELD_OPT && recoverAll { |
| 453 |
errorBits.set(i) |
| 454 |
} |
| 455 |
} |
| 456 |
for i := 0; i < r.dataShards; i++ { |
| 457 |
if len(shards[i]) == 0 { |
| 458 |
errLocs[i+m] = 1 |
| 459 |
if LEO_ERROR_BITFIELD_OPT { |
| 460 |
errorBits.set(i + m) |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
var gotInversion bool |
| 466 |
if LEO_ERROR_BITFIELD_OPT && r.inversion != nil { |
| 467 |
cacheID := errorBits.cacheID() |
| 468 |
r.inversionMu.Lock() |
| 469 |
if inv, ok := r.inversion[cacheID]; ok { |
| 470 |
r.inversionMu.Unlock() |
| 471 |
errLocs = inv.errorLocs |
| 472 |
if inv.bits != nil && useBits { |
| 473 |
errorBits = *inv.bits |
| 474 |
useBits = true |
| 475 |
} else { |
| 476 |
useBits = false |
| 477 |
} |
| 478 |
gotInversion = true |
| 479 |
} else { |
| 480 |
r.inversionMu.Unlock() |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
if !gotInversion { |
| 485 |
// No inversion... |
| 486 |
if LEO_ERROR_BITFIELD_OPT && useBits { |
| 487 |
errorBits.prepare() |
| 488 |
} |
| 489 |
|
| 490 |
// Evaluate error locator polynomial8 |
| 491 |
fwht8(&errLocs, m+r.dataShards) |
| 492 |
|
| 493 |
for i := range order8 { |
| 494 |
errLocs[i] = ffe8((uint(errLocs[i]) * uint(logWalsh8[i])) % modulus8) |
| 495 |
} |
| 496 |
|
| 497 |
fwht8(&errLocs, order8) |
| 498 |
|
| 499 |
if r.inversion != nil { |
| 500 |
c := leopardGF8cache{ |
| 501 |
errorLocs: errLocs, |
| 502 |
} |
| 503 |
if useBits { |
| 504 |
// Heap alloc |
| 505 |
var x errorBitfield8 |
| 506 |
x = errorBits |
| 507 |
c.bits = &x |
| 508 |
} |
| 509 |
r.inversionMu.Lock() |
| 510 |
r.inversion[errorBits.cacheID()] = c |
| 511 |
r.inversionMu.Unlock() |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
work := r.workAlloc.Get(n, workSize8) |
| 516 |
defer r.workAlloc.Put(work) |
| 517 |
|
| 518 |
// work <- recovery data |
| 519 |
|
| 520 |
// Split large shards. |
| 521 |
// More likely on lower shard count. |
| 522 |
sh := make([][]byte, len(shards)) |
| 523 |
// Copy... |
| 524 |
copy(sh, shards) |
| 525 |
|
| 526 |
// Add output |
| 527 |
for i, sh := range shards { |
| 528 |
if !recoverAll && i >= r.dataShards { |
| 529 |
continue |
| 530 |
} |
| 531 |
if len(sh) == 0 { |
| 532 |
if cap(sh) >= shardSize { |
| 533 |
shards[i] = sh[:shardSize] |
| 534 |
} else { |
| 535 |
shards[i] = make([]byte, shardSize) |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
off := 0 |
| 541 |
for off < shardSize { |
| 542 |
endSlice := off + workSize8 |
| 543 |
if endSlice > shardSize { |
| 544 |
endSlice = shardSize |
| 545 |
sz := shardSize - off |
| 546 |
// Last iteration only |
| 547 |
for i := range work { |
| 548 |
work[i] = work[i][:sz] |
| 549 |
} |
| 550 |
} |
| 551 |
for i := range shards { |
| 552 |
if len(sh[i]) != 0 { |
| 553 |
sh[i] = shards[i][off:endSlice] |
| 554 |
} |
| 555 |
} |
| 556 |
for i := 0; i < r.parityShards; i++ { |
| 557 |
if len(sh[i+r.dataShards]) != 0 { |
| 558 |
mulgf8(work[i], sh[i+r.dataShards], errLocs[i], &r.o) |
| 559 |
} else { |
| 560 |
clear(work[i]) |
| 561 |
} |
| 562 |
} |
| 563 |
for i := r.parityShards; i < m; i++ { |
| 564 |
clear(work[i]) |
| 565 |
} |
| 566 |
|
| 567 |
// work <- original data |
| 568 |
|
| 569 |
for i := 0; i < r.dataShards; i++ { |
| 570 |
if len(sh[i]) != 0 { |
| 571 |
mulgf8(work[m+i], sh[i], errLocs[m+i], &r.o) |
| 572 |
} else { |
| 573 |
clear(work[m+i]) |
| 574 |
} |
| 575 |
} |
| 576 |
for i := m + r.dataShards; i < n; i++ { |
| 577 |
clear(work[i]) |
| 578 |
} |
| 579 |
|
| 580 |
// work <- IFFT(work, n, 0) |
| 581 |
|
| 582 |
ifftDITDecoder8( |
| 583 |
m+r.dataShards, |
| 584 |
work, |
| 585 |
n, |
| 586 |
fftSkew8[:], |
| 587 |
&r.o, |
| 588 |
) |
| 589 |
|
| 590 |
// work <- FormalDerivative(work, n) |
| 591 |
|
| 592 |
for i := 1; i < n; i++ { |
| 593 |
width := ((i ^ (i - 1)) + 1) >> 1 |
| 594 |
slicesXor(work[i-width:i], work[i:i+width], &r.o) |
| 595 |
} |
| 596 |
|
| 597 |
// work <- FFT(work, n, 0) truncated to m + dataShards |
| 598 |
|
| 599 |
outputCount := m + r.dataShards |
| 600 |
|
| 601 |
if LEO_ERROR_BITFIELD_OPT && useBits { |
| 602 |
errorBits.fftDIT8(work, outputCount, n, fftSkew8[:], &r.o) |
| 603 |
} else { |
| 604 |
fftDIT8(work, outputCount, n, fftSkew8[:], &r.o) |
| 605 |
} |
| 606 |
|
| 607 |
// Reveal erasures |
| 608 |
// |
| 609 |
// Original = -ErrLocator * FFT( Derivative( IFFT( ErrLocator * ReceivedData ) ) ) |
| 610 |
// mul_mem(x, y, log_m, ) equals x[] = y[] * log_m |
| 611 |
// |
| 612 |
// mem layout: [Recovery Data (Power of Two = M)] [Original Data (K)] [Zero Padding out to N] |
| 613 |
end := r.dataShards |
| 614 |
if recoverAll { |
| 615 |
end = r.totalShards |
| 616 |
} |
| 617 |
// Restore |
| 618 |
for i := 0; i < end; i++ { |
| 619 |
if len(sh[i]) != 0 { |
| 620 |
continue |
| 621 |
} |
| 622 |
|
| 623 |
if i >= r.dataShards { |
| 624 |
// Parity shard. |
| 625 |
mulgf8(shards[i][off:endSlice], work[i-r.dataShards], modulus8-errLocs[i-r.dataShards], &r.o) |
| 626 |
} else { |
| 627 |
// Data shard. |
| 628 |
mulgf8(shards[i][off:endSlice], work[i+m], modulus8-errLocs[i+m], &r.o) |
| 629 |
} |
| 630 |
} |
| 631 |
off += workSize8 |
| 632 |
} |
| 633 |
return nil |
| 634 |
} |
| 635 |
|
| 636 |
// Basic no-frills version for decoder |
| 637 |
func ifftDITDecoder8(mtrunc int, work [][]byte, m int, skewLUT []ffe8, o *options) { |
| 638 |
// Decimation in time: Unroll 2 layers at a time |
| 639 |
dist := 1 |
| 640 |
dist4 := 4 |
| 641 |
for dist4 <= m { |
| 642 |
// For each set of dist*4 elements: |
| 643 |
for r := 0; r < mtrunc; r += dist4 { |
| 644 |
iend := r + dist |
| 645 |
log_m01 := skewLUT[iend-1] |
| 646 |
log_m02 := skewLUT[iend+dist-1] |
| 647 |
log_m23 := skewLUT[iend+dist*2-1] |
| 648 |
|
| 649 |
// For each set of dist elements: |
| 650 |
for i := r; i < iend; i++ { |
| 651 |
ifftDIT48(work[i:], dist, log_m01, log_m23, log_m02, o) |
| 652 |
} |
| 653 |
} |
| 654 |
dist = dist4 |
| 655 |
dist4 <<= 2 |
| 656 |
} |
| 657 |
|
| 658 |
// If there is one layer left: |
| 659 |
if dist < m { |
| 660 |
// Assuming that dist = m / 2 |
| 661 |
if dist*2 != m { |
| 662 |
panic("internal error") |
| 663 |
} |
| 664 |
|
| 665 |
log_m := skewLUT[dist-1] |
| 666 |
|
| 667 |
if log_m == modulus8 { |
| 668 |
slicesXor(work[dist:2*dist], work[:dist], o) |
| 669 |
} else { |
| 670 |
for i := 0; i < dist; i++ { |
| 671 |
ifftDIT28( |
| 672 |
work[i], |
| 673 |
work[i+dist], |
| 674 |
log_m, |
| 675 |
o, |
| 676 |
) |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
// In-place FFT for encoder and decoder |
| 683 |
func fftDIT8(work [][]byte, mtrunc, m int, skewLUT []ffe8, o *options) { |
| 684 |
// Decimation in time: Unroll 2 layers at a time |
| 685 |
dist4 := m |
| 686 |
dist := m >> 2 |
| 687 |
for dist != 0 { |
| 688 |
// For each set of dist*4 elements: |
| 689 |
for r := 0; r < mtrunc; r += dist4 { |
| 690 |
iend := r + dist |
| 691 |
log_m01 := skewLUT[iend-1] |
| 692 |
log_m02 := skewLUT[iend+dist-1] |
| 693 |
log_m23 := skewLUT[iend+dist*2-1] |
| 694 |
|
| 695 |
// For each set of dist elements: |
| 696 |
for i := r; i < iend; i++ { |
| 697 |
fftDIT48( |
| 698 |
work[i:], |
| 699 |
dist, |
| 700 |
log_m01, |
| 701 |
log_m23, |
| 702 |
log_m02, |
| 703 |
o, |
| 704 |
) |
| 705 |
} |
| 706 |
} |
| 707 |
dist4 = dist |
| 708 |
dist >>= 2 |
| 709 |
} |
| 710 |
|
| 711 |
// If there is one layer left: |
| 712 |
if dist4 == 2 { |
| 713 |
for r := 0; r < mtrunc; r += 2 { |
| 714 |
log_m := skewLUT[r+1-1] |
| 715 |
|
| 716 |
if log_m == modulus8 { |
| 717 |
sliceXor(work[r], work[r+1], o) |
| 718 |
} else { |
| 719 |
fftDIT28(work[r], work[r+1], log_m, o) |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
// 4-way butterfly |
| 726 |
func fftDIT4Ref8(work [][]byte, dist int, log_m01, log_m23, log_m02 ffe8, o *options) { |
| 727 |
// First layer: |
| 728 |
if log_m02 == modulus8 { |
| 729 |
sliceXor(work[0], work[dist*2], o) |
| 730 |
sliceXor(work[dist], work[dist*3], o) |
| 731 |
} else { |
| 732 |
fftDIT28(work[0], work[dist*2], log_m02, o) |
| 733 |
fftDIT28(work[dist], work[dist*3], log_m02, o) |
| 734 |
} |
| 735 |
|
| 736 |
// Second layer: |
| 737 |
if log_m01 == modulus8 { |
| 738 |
sliceXor(work[0], work[dist], o) |
| 739 |
} else { |
| 740 |
fftDIT28(work[0], work[dist], log_m01, o) |
| 741 |
} |
| 742 |
|
| 743 |
if log_m23 == modulus8 { |
| 744 |
sliceXor(work[dist*2], work[dist*3], o) |
| 745 |
} else { |
| 746 |
fftDIT28(work[dist*2], work[dist*3], log_m23, o) |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
// zeroBufferPool returns a pointer to a zeroed array. |
| 751 |
// This should never be written to. |
| 752 |
var zeroBufferPool = &[workSize8]byte{} |
| 753 |
|
| 754 |
// Unrolled IFFT for encoder |
| 755 |
func ifftDITEncoder8(data [][]byte, mtrunc int, work [][]byte, xorRes [][]byte, m int, skewLUT []ffe8, o *options) { |
| 756 |
dist := 1 |
| 757 |
dist4 := 4 |
| 758 |
|
| 759 |
if dist4 <= m { |
| 760 |
if o.useAvx512GFNI || (pshufb && o.useAVX2) { |
| 761 |
// SIMD path: fuse copy with first layer butterfly |
| 762 |
fullGroups := mtrunc &^ 3 |
| 763 |
zb := zeroBufferPool[:] |
| 764 |
|
| 765 |
for r := 0; r < fullGroups; r += dist4 { |
| 766 |
iend := r + dist |
| 767 |
log_m01 := skewLUT[iend] |
| 768 |
log_m02 := skewLUT[iend+dist] |
| 769 |
log_m23 := skewLUT[iend+dist*2] |
| 770 |
ifftDIT48Dst(work[r:], data[r:], dist, log_m01, log_m23, log_m02, o) |
| 771 |
} |
| 772 |
|
| 773 |
if fullGroups < mtrunc { |
| 774 |
r := fullGroups |
| 775 |
iend := r + dist |
| 776 |
log_m01 := skewLUT[iend] |
| 777 |
log_m02 := skewLUT[iend+dist] |
| 778 |
log_m23 := skewLUT[iend+dist*2] |
| 779 |
|
| 780 |
src := [4][]byte{zb, zb, zb, zb} |
| 781 |
for i := 0; i < mtrunc-fullGroups; i++ { |
| 782 |
src[i] = data[fullGroups+i] |
| 783 |
} |
| 784 |
ifftDIT48Dst(work[r:], src[:], dist, log_m01, log_m23, log_m02, o) |
| 785 |
} |
| 786 |
|
| 787 |
clearStart := (mtrunc + 3) &^ 3 |
| 788 |
for i := clearStart; i < m; i++ { |
| 789 |
clear(work[i]) |
| 790 |
} |
| 791 |
} else { |
| 792 |
// Non-SIMD path: bulk copy + clear, then in-place butterfly |
| 793 |
for i := range mtrunc { |
| 794 |
copy(work[i], data[i]) |
| 795 |
} |
| 796 |
for i := mtrunc; i < m; i++ { |
| 797 |
clear(work[i]) |
| 798 |
} |
| 799 |
|
| 800 |
for r := 0; r < mtrunc; r += dist4 { |
| 801 |
iend := r + dist |
| 802 |
log_m01 := skewLUT[iend] |
| 803 |
log_m02 := skewLUT[iend+dist] |
| 804 |
log_m23 := skewLUT[iend+dist*2] |
| 805 |
for i := r; i < iend; i++ { |
| 806 |
ifftDIT48(work[i:], dist, log_m01, log_m23, log_m02, o) |
| 807 |
} |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
dist = dist4 |
| 812 |
dist4 <<= 2 |
| 813 |
|
| 814 |
// Subsequent layers: in-place (same for both paths) |
| 815 |
for dist4 <= m { |
| 816 |
for r := 0; r < mtrunc; r += dist4 { |
| 817 |
iend := r + dist |
| 818 |
log_m01 := skewLUT[iend] |
| 819 |
log_m02 := skewLUT[iend+dist] |
| 820 |
log_m23 := skewLUT[iend+dist*2] |
| 821 |
for i := r; i < iend; i++ { |
| 822 |
ifftDIT48(work[i:], dist, log_m01, log_m23, log_m02, o) |
| 823 |
} |
| 824 |
} |
| 825 |
dist = dist4 |
| 826 |
dist4 <<= 2 |
| 827 |
} |
| 828 |
} else { |
| 829 |
// m < 4: fallback |
| 830 |
for i := range mtrunc { |
| 831 |
copy(work[i], data[i]) |
| 832 |
} |
| 833 |
for i := mtrunc; i < m; i++ { |
| 834 |
clear(work[i]) |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
// Final layer |
| 839 |
if dist < m { |
| 840 |
if dist*2 != m { |
| 841 |
panic("internal error") |
| 842 |
} |
| 843 |
logm := skewLUT[dist] |
| 844 |
if logm == modulus8 { |
| 845 |
slicesXor(work[dist:dist*2], work[:dist], o) |
| 846 |
} else { |
| 847 |
for i := 0; i < dist; i++ { |
| 848 |
ifftDIT28(work[i], work[i+dist], logm, o) |
| 849 |
} |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
if xorRes != nil { |
| 854 |
slicesXor(xorRes[:m], work[:m], o) |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
func ifftDIT4Ref8(work [][]byte, dist int, log_m01, log_m23, log_m02 ffe8, o *options) { |
| 859 |
// First layer: |
| 860 |
if log_m01 == modulus8 { |
| 861 |
sliceXor(work[0], work[dist], o) |
| 862 |
} else { |
| 863 |
ifftDIT28(work[0], work[dist], log_m01, o) |
| 864 |
} |
| 865 |
|
| 866 |
if log_m23 == modulus8 { |
| 867 |
sliceXor(work[dist*2], work[dist*3], o) |
| 868 |
} else { |
| 869 |
ifftDIT28(work[dist*2], work[dist*3], log_m23, o) |
| 870 |
} |
| 871 |
|
| 872 |
// Second layer: |
| 873 |
if log_m02 == modulus8 { |
| 874 |
sliceXor(work[0], work[dist*2], o) |
| 875 |
sliceXor(work[dist], work[dist*3], o) |
| 876 |
} else { |
| 877 |
ifftDIT28(work[0], work[dist*2], log_m02, o) |
| 878 |
ifftDIT28(work[dist], work[dist*3], log_m02, o) |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
func ifftDIT4DstRef8(dst, work [][]byte, dist int, log_m01, log_m23, log_m02 ffe8, o *options) { |
| 883 |
// Copy the 4 elements touched by the butterfly |
| 884 |
for i := range 4 { |
| 885 |
copy(dst[i*dist], work[i*dist][:]) |
| 886 |
} |
| 887 |
ifftDIT4Ref8(dst, dist, log_m01, log_m23, log_m02, o) |
| 888 |
} |
| 889 |
|
| 890 |
// Reference version of muladd: x[] ^= y[] * log_m |
| 891 |
func refMulAdd8(x, y []byte, log_m ffe8) { |
| 892 |
lut := &mul8LUTs[log_m] |
| 893 |
|
| 894 |
for len(x) >= 64 { |
| 895 |
// Assert sizes for no bounds checks in loop |
| 896 |
src := y[:64] |
| 897 |
dst := x[:len(src)] // Needed, but not checked... |
| 898 |
for i, y1 := range src { |
| 899 |
dst[i] ^= byte(lut.Value[y1]) |
| 900 |
} |
| 901 |
x = x[64:] |
| 902 |
y = y[64:] |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
// Reference version of mul: x[] = y[] * log_m |
| 907 |
func refMul8(x, y []byte, log_m ffe8) { |
| 908 |
lut := &mul8LUTs[log_m] |
| 909 |
|
| 910 |
for off := 0; off < len(x); off += 64 { |
| 911 |
src := y[off : off+64] |
| 912 |
for i, y1 := range src { |
| 913 |
x[off+i] = byte(lut.Value[y1]) |
| 914 |
} |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
// Returns a * Log(b) |
| 919 |
func mulLog8(a, log_b ffe8) ffe8 { |
| 920 |
/* |
| 921 |
Note that this operation is not a normal multiplication in a finite |
| 922 |
field because the right operand is already a logarithm. This is done |
| 923 |
because it moves K table lookups from the Decode() method into the |
| 924 |
initialization step that is less performance critical. The LogWalsh[] |
| 925 |
table below contains precalculated logarithms so it is easier to do |
| 926 |
all the other multiplies in that form as well. |
| 927 |
*/ |
| 928 |
if a == 0 { |
| 929 |
return 0 |
| 930 |
} |
| 931 |
return expLUT8[addMod8(logLUT8[a], log_b)] |
| 932 |
} |
| 933 |
|
| 934 |
// addMod returns the sum of a and b modulo modulus. This can return modulus but |
| 935 |
// it is expected that callers will convert modulus to 0. |
| 936 |
func addMod8(a, b ffe8) ffe8 { |
| 937 |
sum := uint(a) + uint(b) |
| 938 |
|
| 939 |
// Partial reduction step which allows for modulus to be returned. |
| 940 |
return ffe8(sum + sum>>bitwidth8) |
| 941 |
} |
| 942 |
|
| 943 |
// z = x - y (mod kModulus) |
| 944 |
func subMod8(a, b ffe8) ffe8 { |
| 945 |
dif := uint(a) - uint(b) |
| 946 |
|
| 947 |
// Partial reduction step, allowing for kModulus to be returned |
| 948 |
return ffe8(dif + dif>>bitwidth8) |
| 949 |
} |
| 950 |
|
| 951 |
// Decimation in time (DIT) Fast Walsh-Hadamard Transform |
| 952 |
// Unrolls pairs of layers to perform cross-layer operations in registers |
| 953 |
// mtrunc: Number of elements that are non-zero at the front of data |
| 954 |
func fwht8(data *[order8]ffe8, mtrunc int) { |
| 955 |
// Decimation in time: Unroll 2 layers at a time |
| 956 |
dist := 1 |
| 957 |
dist4 := 4 |
| 958 |
for dist4 <= order8 { |
| 959 |
// For each set of dist*4 elements: |
| 960 |
for r := 0; r < mtrunc; r += dist4 { |
| 961 |
// For each set of dist elements: |
| 962 |
// Use 16 bit indices to avoid bounds check on [65536]ffe8. |
| 963 |
dist := uint16(dist) |
| 964 |
off := uint16(r) |
| 965 |
for range dist { |
| 966 |
// fwht48(data[i:], dist) inlined... |
| 967 |
// Reading values appear faster than updating pointers. |
| 968 |
// Casting to uint is not faster. |
| 969 |
t0 := data[off] |
| 970 |
t1 := data[off+dist] |
| 971 |
t2 := data[off+dist*2] |
| 972 |
t3 := data[off+dist*3] |
| 973 |
|
| 974 |
t0, t1 = fwht2alt8(t0, t1) |
| 975 |
t2, t3 = fwht2alt8(t2, t3) |
| 976 |
t0, t2 = fwht2alt8(t0, t2) |
| 977 |
t1, t3 = fwht2alt8(t1, t3) |
| 978 |
|
| 979 |
data[off] = t0 |
| 980 |
data[off+dist] = t1 |
| 981 |
data[off+dist*2] = t2 |
| 982 |
data[off+dist*3] = t3 |
| 983 |
off++ |
| 984 |
} |
| 985 |
} |
| 986 |
dist = dist4 |
| 987 |
dist4 <<= 2 |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
func fwht48(data []ffe8, s int) { |
| 992 |
s2 := s << 1 |
| 993 |
|
| 994 |
t0 := &data[0] |
| 995 |
t1 := &data[s] |
| 996 |
t2 := &data[s2] |
| 997 |
t3 := &data[s2+s] |
| 998 |
|
| 999 |
fwht28(t0, t1) |
| 1000 |
fwht28(t2, t3) |
| 1001 |
fwht28(t0, t2) |
| 1002 |
fwht28(t1, t3) |
| 1003 |
} |
| 1004 |
|
| 1005 |
// {a, b} = {a + b, a - b} (Mod Q) |
| 1006 |
func fwht28(a, b *ffe8) { |
| 1007 |
sum := addMod8(*a, *b) |
| 1008 |
dif := subMod8(*a, *b) |
| 1009 |
*a = sum |
| 1010 |
*b = dif |
| 1011 |
} |
| 1012 |
|
| 1013 |
// fwht2alt8 is as fwht28, but returns result. |
| 1014 |
func fwht2alt8(a, b ffe8) (ffe8, ffe8) { |
| 1015 |
return addMod8(a, b), subMod8(a, b) |
| 1016 |
} |
| 1017 |
|
| 1018 |
var initOnce8 sync.Once |
| 1019 |
|
| 1020 |
func initConstants8() { |
| 1021 |
initOnce8.Do(func() { |
| 1022 |
initLUTs8() |
| 1023 |
initFFTSkew8() |
| 1024 |
initMul8LUT() |
| 1025 |
}) |
| 1026 |
} |
| 1027 |
|
| 1028 |
// Initialize logLUT8, expLUT8. |
| 1029 |
func initLUTs8() { |
| 1030 |
cantorBasis := [bitwidth8]ffe8{ |
| 1031 |
1, 214, 152, 146, 86, 200, 88, 230, |
| 1032 |
} |
| 1033 |
|
| 1034 |
expLUT8 = &[order8]ffe8{} |
| 1035 |
logLUT8 = &[order8]ffe8{} |
| 1036 |
|
| 1037 |
// LFSR table generation: |
| 1038 |
state := 1 |
| 1039 |
for i := range ffe8(modulus8) { |
| 1040 |
expLUT8[state] = i |
| 1041 |
state <<= 1 |
| 1042 |
if state >= order8 { |
| 1043 |
state ^= polynomial8 |
| 1044 |
} |
| 1045 |
} |
| 1046 |
expLUT8[0] = modulus8 |
| 1047 |
|
| 1048 |
// Conversion to Cantor basis: |
| 1049 |
|
| 1050 |
logLUT8[0] = 0 |
| 1051 |
for i := range bitwidth8 { |
| 1052 |
basis := cantorBasis[i] |
| 1053 |
width := 1 << i |
| 1054 |
|
| 1055 |
for j := range width { |
| 1056 |
logLUT8[j+width] = logLUT8[j] ^ basis |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
for i := range order8 { |
| 1061 |
logLUT8[i] = expLUT8[logLUT8[i]] |
| 1062 |
} |
| 1063 |
|
| 1064 |
for i := range order8 { |
| 1065 |
expLUT8[logLUT8[i]] = ffe8(i) |
| 1066 |
} |
| 1067 |
|
| 1068 |
expLUT8[modulus8] = expLUT8[0] |
| 1069 |
} |
| 1070 |
|
| 1071 |
// Initialize fftSkew8. |
| 1072 |
func initFFTSkew8() { |
| 1073 |
var temp [bitwidth8 - 1]ffe8 |
| 1074 |
|
| 1075 |
// Generate FFT skew vector {1}: |
| 1076 |
|
| 1077 |
for i := 1; i < bitwidth8; i++ { |
| 1078 |
temp[i-1] = ffe8(1 << i) |
| 1079 |
} |
| 1080 |
|
| 1081 |
fftSkew8 = &[modulus8]ffe8{} |
| 1082 |
logWalsh8 = &[order8]ffe8{} |
| 1083 |
|
| 1084 |
for m := range bitwidth8 - 1 { |
| 1085 |
step := 1 << (m + 1) |
| 1086 |
|
| 1087 |
fftSkew8[1<<m-1] = 0 |
| 1088 |
|
| 1089 |
for i := m; i < bitwidth8-1; i++ { |
| 1090 |
s := 1 << (i + 1) |
| 1091 |
|
| 1092 |
for j := 1<<m - 1; j < s; j += step { |
| 1093 |
fftSkew8[j+s] = fftSkew8[j] ^ temp[i] |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
temp[m] = modulus8 - logLUT8[mulLog8(temp[m], logLUT8[temp[m]^1])] |
| 1098 |
|
| 1099 |
for i := m + 1; i < bitwidth8-1; i++ { |
| 1100 |
sum := addMod8(logLUT8[temp[i]^1], temp[m]) |
| 1101 |
temp[i] = mulLog8(temp[i], sum) |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
for i := range modulus8 { |
| 1106 |
fftSkew8[i] = logLUT8[fftSkew8[i]] |
| 1107 |
} |
| 1108 |
|
| 1109 |
// Precalculate FWHT(Log[i]): |
| 1110 |
|
| 1111 |
for i := range order8 { |
| 1112 |
logWalsh8[i] = logLUT8[i] |
| 1113 |
} |
| 1114 |
logWalsh8[0] = 0 |
| 1115 |
|
| 1116 |
fwht8(logWalsh8, order8) |
| 1117 |
} |
| 1118 |
|
| 1119 |
func initMul8LUT() { |
| 1120 |
mul8LUTs = &[order8]mul8LUT{} |
| 1121 |
|
| 1122 |
// For each log_m multiplicand: |
| 1123 |
for log_m := range order8 { |
| 1124 |
var tmp [64]ffe8 |
| 1125 |
for nibble, shift := 0, 0; nibble < 4; { |
| 1126 |
nibble_lut := tmp[nibble*16:] |
| 1127 |
|
| 1128 |
for xnibble := range 16 { |
| 1129 |
prod := mulLog8(ffe8(xnibble<<shift), ffe8(log_m)) |
| 1130 |
nibble_lut[xnibble] = prod |
| 1131 |
} |
| 1132 |
nibble++ |
| 1133 |
shift += 4 |
| 1134 |
} |
| 1135 |
lut := &mul8LUTs[log_m] |
| 1136 |
for i := range lut.Value[:] { |
| 1137 |
lut.Value[i] = tmp[i&15] ^ tmp[((i>>4)+16)] |
| 1138 |
} |
| 1139 |
} |
| 1140 |
// Always initialize assembly tables. |
| 1141 |
// Not as big resource hog as gf16. |
| 1142 |
if true { |
| 1143 |
multiply256LUT8 = &[order8][16 * 2]byte{} |
| 1144 |
|
| 1145 |
for logM := range multiply256LUT8[:] { |
| 1146 |
// For each 4 bits of the finite field width in bits: |
| 1147 |
shift := 0 |
| 1148 |
for i := range 2 { |
| 1149 |
// Construct 16 entry LUT for PSHUFB |
| 1150 |
prod := multiply256LUT8[logM][i*16 : i*16+16] |
| 1151 |
for x := range prod[:] { |
| 1152 |
prod[x] = byte(mulLog8(ffe8(x<<shift), ffe8(logM))) |
| 1153 |
} |
| 1154 |
shift += 4 |
| 1155 |
} |
| 1156 |
} |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
const kWords8 = order8 / 64 |
| 1161 |
|
| 1162 |
// errorBitfield contains progressive errors to help indicate which |
| 1163 |
// shards need reconstruction. |
| 1164 |
type errorBitfield8 struct { |
| 1165 |
Words [7][kWords8]uint64 |
| 1166 |
} |
| 1167 |
|
| 1168 |
func (e *errorBitfield8) set(i int) { |
| 1169 |
e.Words[0][(i/64)&3] |= uint64(1) << (i & 63) |
| 1170 |
} |
| 1171 |
|
| 1172 |
func (e *errorBitfield8) cacheID() [inversion8Bytes]byte { |
| 1173 |
var res [inversion8Bytes]byte |
| 1174 |
binary.LittleEndian.PutUint64(res[0:8], e.Words[0][0]) |
| 1175 |
binary.LittleEndian.PutUint64(res[8:16], e.Words[0][1]) |
| 1176 |
binary.LittleEndian.PutUint64(res[16:24], e.Words[0][2]) |
| 1177 |
binary.LittleEndian.PutUint64(res[24:32], e.Words[0][3]) |
| 1178 |
return res |
| 1179 |
} |
| 1180 |
|
| 1181 |
func (e *errorBitfield8) isNeeded(mipLevel, bit int) bool { |
| 1182 |
if mipLevel >= 8 || mipLevel <= 0 { |
| 1183 |
return true |
| 1184 |
} |
| 1185 |
return 0 != (e.Words[mipLevel-1][bit/64] & (uint64(1) << (bit & 63))) |
| 1186 |
} |
| 1187 |
|
| 1188 |
func (e *errorBitfield8) prepare() { |
| 1189 |
// First mip level is for final layer of FFT: pairs of data |
| 1190 |
for i := range kWords8 { |
| 1191 |
w_i := e.Words[0][i] |
| 1192 |
hi2lo0 := w_i | ((w_i & kHiMasks[0]) >> 1) |
| 1193 |
lo2hi0 := (w_i & (kHiMasks[0] >> 1)) << 1 |
| 1194 |
w_i = hi2lo0 | lo2hi0 |
| 1195 |
e.Words[0][i] = w_i |
| 1196 |
|
| 1197 |
bits := 2 |
| 1198 |
for j := 1; j < 5; j++ { |
| 1199 |
hi2lo_j := w_i | ((w_i & kHiMasks[j]) >> bits) |
| 1200 |
lo2hi_j := (w_i & (kHiMasks[j] >> bits)) << bits |
| 1201 |
w_i = hi2lo_j | lo2hi_j |
| 1202 |
e.Words[j][i] = w_i |
| 1203 |
bits <<= 1 |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
for i := range kWords8 { |
| 1208 |
w := e.Words[4][i] |
| 1209 |
w |= w >> 32 |
| 1210 |
w |= w << 32 |
| 1211 |
e.Words[5][i] = w |
| 1212 |
} |
| 1213 |
|
| 1214 |
for i := 0; i < kWords8; i += 2 { |
| 1215 |
t := e.Words[5][i] | e.Words[5][i+1] |
| 1216 |
e.Words[6][i] = t |
| 1217 |
e.Words[6][i+1] = t |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
func (e *errorBitfield8) fftDIT8(work [][]byte, mtrunc, m int, skewLUT []ffe8, o *options) { |
| 1222 |
// Decimation in time: Unroll 2 layers at a time |
| 1223 |
mipLevel := bits.Len32(uint32(m)) - 1 |
| 1224 |
|
| 1225 |
dist4 := m |
| 1226 |
dist := m >> 2 |
| 1227 |
for dist != 0 { |
| 1228 |
// For each set of dist*4 elements: |
| 1229 |
for r := 0; r < mtrunc; r += dist4 { |
| 1230 |
if !e.isNeeded(mipLevel, r) { |
| 1231 |
continue |
| 1232 |
} |
| 1233 |
iEnd := r + dist |
| 1234 |
logM01 := skewLUT[iEnd-1] |
| 1235 |
logM02 := skewLUT[iEnd+dist-1] |
| 1236 |
logM23 := skewLUT[iEnd+dist*2-1] |
| 1237 |
|
| 1238 |
// For each set of dist elements: |
| 1239 |
for i := r; i < iEnd; i++ { |
| 1240 |
fftDIT48( |
| 1241 |
work[i:], |
| 1242 |
dist, |
| 1243 |
logM01, |
| 1244 |
logM23, |
| 1245 |
logM02, |
| 1246 |
o) |
| 1247 |
} |
| 1248 |
} |
| 1249 |
dist4 = dist |
| 1250 |
dist >>= 2 |
| 1251 |
mipLevel -= 2 |
| 1252 |
} |
| 1253 |
|
| 1254 |
// If there is one layer left: |
| 1255 |
if dist4 == 2 { |
| 1256 |
for r := 0; r < mtrunc; r += 2 { |
| 1257 |
if !e.isNeeded(mipLevel, r) { |
| 1258 |
continue |
| 1259 |
} |
| 1260 |
logM := skewLUT[r+1-1] |
| 1261 |
|
| 1262 |
if logM == modulus8 { |
| 1263 |
sliceXor(work[r], work[r+1], o) |
| 1264 |
} else { |
| 1265 |
fftDIT28(work[r], work[r+1], logM, o) |
| 1266 |
} |
| 1267 |
} |
| 1268 |
} |
| 1269 |
} |
| 1270 |
|
| 1271 |
var gf2p811dMulMatricesLeo8 = [256]uint64{ |
| 1272 |
0x0102040810204080, 0xaeb88835d2a91d5e, 0xf2106b5d296cc1ca, 0x80d297dc3209edce, |
| 1273 |
0x5e29122cc39bf946, 0xca326a1455afa32d, 0xcec3395ae95ce1ba, 0x4655e042717244a8, |
| 1274 |
0x2de9eaa5c8de95c2, 0xba7167d17694aafb, 0xa8c82a3f56047a1b, 0xc27624d0ff88ebf1, |
| 1275 |
0xfb562191936bc996, 0x1bff07229a97d8bc, 0xf1939e110112a498, 0x969a897cae6a7fb9, |
| 1276 |
0xbc01c5dbf239cdbe, 0x98ae65b280e05020, 0xb9f2929d5eeacfa9, 0xbe80349fca67e86c, |
| 1277 |
0x205ef327ce2adf09, 0xa9ca2e3746243a9b, 0x6cceace52d21f6af, 0x09464accba07085c, |
| 1278 |
0x9b2d90fea89e3572, 0xafba8c3dc2895dde, 0x5ca8e368fbc5dc94, 0x72c2fc811b652c04, |
| 1279 |
0xdefb85f0f1921488, 0x941b783896345a6b, 0x04f1534ebcf34297, 0x8896d918982ea512, |
| 1280 |
0x6bbc0ae7b9acd16a, 0x97988d74be4a3f39, 0x12b94dee2090d0e0, 0x6abe0eefa98c91ea, |
| 1281 |
0x392005416ce32267, 0xe0a926b309fc112a, 0xea6c99339b857c24, 0x6709176daf78db21, |
| 1282 |
0x2a9b4ca75c53b207, 0x24afa06972d99d9e, 0x215cf72fde0a9f89, 0x0772a602948d27c5, |
| 1283 |
0x9edec7b8044d3765, 0x8994dd10880ee592, 0xc50482d26b05cc34, 0x6588e6299726fef3, |
| 1284 |
0x926bda3212993d2e, 0x34971cc36a1768ac, 0xf3126f55394c814a, 0x2e6a1fe9e0a0f090, |
| 1285 |
0xac397971eaf7388c, 0x4ae0fdc867a64ee3, 0x90ea2b762ac718fc, 0x8c678a5624dde785, |
| 1286 |
0xe32ad3ff21827478, 0xfc24879307e6ee53, 0x8521c09a9edaefd9, 0x78074301891c410a, |
| 1287 |
0x539e0baec56fb38d, 0xd98923f2651f334d, 0x0ac5bf8092796d0e, 0x8d658e5e34fda705, |
| 1288 |
0x4d925bcaf32b6926, 0x0e34ecce2e8a2f99, 0x05f35746acd30217, 0x262e512d4a87b84c, |
| 1289 |
0x99ac61ba90c010a0, 0x174a1aa88c43d2f7, 0x4c905fc2e30b29a6, 0xa08c64fbfc2332c7, |
| 1290 |
0xf7e33c1b85bfc3dd, 0xa6fcc6f1788e5582, 0xc7857396535be9e6, 0xdd7870bcd9ec71da, |
| 1291 |
0x825366980a57c81c, 0xe6d984b98d51766f, 0xda0ad6be4d61561f, 0x1c8da1200e1aff79, |
| 1292 |
0x6f4d59a9055f93fd, 0x1f0e546c26649a2b, 0x79054709993c018a, 0xfd26839b17c6aed3, |
| 1293 |
0x2b9948af4c73f287, 0x8a17285ca07080c0, 0xd34c9c72f7665e43, 0x87a031dea684ca0b, |
| 1294 |
0xc0f7d594c7d6ce23, 0x43a6b704dda146bf, 0x0bc7bb8882592d8e, 0x23dd066be654ba5b, |
| 1295 |
0xbf823097da47a8ec, 0x8ee67b121c83c257, 0x5bda456a6f48fb51, 0xec1c3b391f281b61, |
| 1296 |
0x576f58e0799cf11a, 0x511ffaeafd31965f, 0x6179b5672bd5bc64, 0x1afd032a8ab7983c, |
| 1297 |
0x5f2b1624d3bbb9c6, 0x648ae2218706be73, 0x3cd35207c0302070, 0xc687779e437ba966, |
| 1298 |
0x73c0f8890b456c84, 0x70430dc5233b09d6, 0x660b1365bf589ba1, 0x8423c4928efaaf59, |
| 1299 |
0xd6bfcb345bb55c54, 0xa18e60f3ec037247, 0x595bb42e5716de83, 0x54ecadac51e29448, |
| 1300 |
0x4757e44a61520428, 0x835162901a77889c, 0x48610c8c5ff86b31, 0x281abde3640d97d5, |
| 1301 |
0x9c5f36fc3c1312b7, 0x31644b85c6c46abb, 0xd53c3e7873cb3906, 0xb7c67e537060e030, |
| 1302 |
0xbb7363d966b4ea7b, 0x0670a20a84ad6745, 0x30664f8dd6e42a3b, 0x7b84b64da1622458, |
| 1303 |
0x45d6150e590c21fa, 0x3ba1f40554bd07b5, 0x5859b02647369e03, 0xfa542599834b8916, |
| 1304 |
0xb5478f17483ec5e2, 0x0383f54c287e6552, 0x16481ea09c639277, 0xe228d7f731a234f8, |
| 1305 |
0x529c0fa6d54ff30d, 0x7731abc7b7b62e13, 0xf8d5d4ddbb15acc4, 0x0db7198206f44acb, |
| 1306 |
0x13bb49e630b09060, 0xc40686da7b258cb4, 0xcb306e1c458fe3ad, 0x607bb16f3bf5fce4, |
| 1307 |
0xb4458b1f581e8562, 0xad3b7d79fad7780c, 0xe45875fdb50f53bd, 0x62fa402b03abd936, |
| 1308 |
0x0cb51d8a16d40a4b, 0xbd03c1d3e2198d3e, 0x3616ed8752494d7e, 0x4be2f9c077860e63, |
| 1309 |
0x3e52a343f86e05a2, 0x7e77e10b0db1264f, 0x63f84423138b99b6, 0xa20d95bfc47d1715, |
| 1310 |
0x4f13aa8ecb754cf4, 0xb6c47a5b6040a0b0, 0x15cbebecb41df725, 0xf460c957adc1a68f, |
| 1311 |
0xb0b4d851e4edc7f5, 0x25ada46162f9dd1e, 0x8fe47f1a0ca382d7, 0xf562cd5fbde1e60f, |
| 1312 |
0x1e0c50643644daab, 0xd7bdcf3c4b951cd4, 0x0f36e8c63eaa6f19, 0xab4bdf737e7a1f49, |
| 1313 |
0xd43e3a7063eb7986, 0x197ef666a2c9fd6e, 0x496308844fd82bb1, 0x86a235d6b6a48a8b, |
| 1314 |
0x6e4f5da1157fd37d, 0xb1b6dc59f4cd8775, 0x8b152c54b050c040, 0x7df4144725cf431d, |
| 1315 |
0x75b05a838fe80bc1, 0x40254248f5df23ed, 0x1d8fa5281e3abff9, 0xc1f5d19cd7f68ea3, |
| 1316 |
0xed1e3f310f085be1, 0xf9d7d0d5ab35ec44, 0xa30f91b7d45d5795, 0xe1ab22bb19dc51aa, |
| 1317 |
0x44d41106492c617a, 0x95197c3086141aeb, 0xaa49db7b6e5a5fc9, 0x7a86b245b14264d8, |
| 1318 |
0xeb6e9d3b8ba53ca4, 0xc9b19f587dd1c67f, 0xd88b27fa753f73cd, 0xa47d37b540d07050, |
| 1319 |
0x7f75e5031d9166cf, 0xcd40cc16c12284e8, 0x501dfee2ed11d6df, 0xcfc13d52f97ca13a, |
| 1320 |
0xe8ed6877a3db59f6, 0xdff981f8e1b25408, 0x3aa3f00d449d4735, 0xf6e13813959f835d, |
| 1321 |
0x08444ec4aa2748dc, 0x359518cb7a37282c, 0x5daae760ebe59c14, 0xdc7a74b4c9cc315a, |
| 1322 |
0x2cebeeadd8fed542, 0x14c9efe4a43db7a5, 0x5ad841627f68bbd1, 0x42a4b30ccd81063f, |
| 1323 |
0xa57f33bd50f030d0, 0xd1cd6d36cf387b91, 0x3f50a74be84e4522, 0xd0cf693edf183b11, |
| 1324 |
0x91e82f7e3ae7587c, 0x22df0263f674fadb, 0x113ab8a208eeb5b2, 0x7cf6104f35ef039d, |
| 1325 |
0xdb08d2b65d41169f, 0xb2352915dcb3e227, 0x9d5d32f42c335237, 0x9fdcc3b0146d77e5, |
| 1326 |
0x272c55255aa7f8cc, 0x3714e98f42690dfe, 0xe55a71f5a52f133d, 0xcc42c81ed102c468, |
| 1327 |
0xfea576d73fb8cb81, 0x3dd1560fd01060f0, 0x683fffab91d2b438, 0x81d093d42229ad4e, |
| 1328 |
0xf0919a191132e418, 0x382201497cc362e7, 0x4e11ae86db550c74, 0x187cf26eb2e9bdee, |
| 1329 |
0xe7db80b19d7136ef, 0x74b25e8b9fc84b41, 0xee9dca7d27763eb3, 0xef9fce7537567e33, |
| 1330 |
0x41274640e5ff636d, 0xb3372d1dcc93a2a7, 0x33e5bac1fe9a4f69, 0x6dcca8ed3d01b62f, |
| 1331 |
0xa7fec2f968ae1502, 0x693dfba381f2f4b8, 0x2f681be1f080b010, 0x0281f144385e25d2, |
| 1332 |
0xb8f096954eca8f29, 0x1038bcaa18cef532, 0xd24e987ae7461ec3, 0x2918b9eb742dd755, |
| 1333 |
0x32e7bec9eeba0fe9, 0xc37420d8efa8ab71, 0x55eea9a441c2d4c8, 0xe9ef6c7fb3fb1976, |
| 1334 |
0x714109cd331b4956, 0xc8b39b506df186ff, 0x7633afcfa7966e93, 0x566d5ce869bcb19a, |
| 1335 |
0xffa772df2f988b01, 0x9369de3a02b97dae, 0x9a2f94f6b8be75f2, 0x0102040810204080, |
| 1336 |
} |