infectious

package module
v0.0.0-...-25a574a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2020 License: BSD-2-Clause, MIT Imports: 7 Imported by: 20

README

infectious

GoDoc

Infectious implements Reed-Solomon forward error correction. It uses the Berlekamp-Welch error correction algorithm to achieve the ability to actually correct errors.

We wrote a blog post about how this library works!

Example
const (
	required = 8
	total    = 14
)

// Create a *FEC, which will require required pieces for reconstruction at
// minimum, and generate total total pieces.
f, err := infectious.NewFEC(required, total)
if err != nil {
	panic(err)
}

// Prepare to receive the shares of encoded data.
shares := make([]infectious.Share, total)
output := func(s infectious.Share) {
	// the memory in s gets reused, so we need to make a deep copy
	shares[s.Number] = s.DeepCopy()
}

// the data to encode must be padded to a multiple of required, hence the
// underscores.
err = f.Encode([]byte("hello, world! __"), output)
if err != nil {
	panic(err)
}

// we now have total shares.
for _, share := range shares {
	fmt.Printf("%d: %#v\n", share.Number, string(share.Data))
}

// Let's reconstitute with two pieces missing and one piece corrupted.
shares = shares[2:]     // drop the first two pieces
shares[2].Data[1] = '!' // mutate some data

result, err := f.Decode(nil, shares)
if err != nil {
	panic(err)
}

// we have the original data!
fmt.Printf("got: %#v\n", string(result))

Caution: this package API leans toward providing the user more power and performance at the expense of having some really sharp edges! Read the documentation about memory lifecycles carefully!

Please see the docs at http://godoc.org/github.com/vivint/infectious

Thanks

We're forever indebted to the giants on whose shoulders we stand. The LICENSE has our full copyright history, but an extra special thanks to Klaus Post for much of the initial Go code. See his post for more: http://blog.klauspost.com/blazingly-fast-reed-solomon-coding/

LICENSE

Portions derived from code by Phil Karn (karn@ka9q.ampr.org), Robert Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari Thirumoorthy (harit@spectra.eng.hawaii.edu), Aug 1995

Portions of this project (labeled in each file) are licensed under this license:

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

All other portions of this project are licensed under this license:

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package infectious implements Reed-Solomon forward error correction [1]. It uses the Berlekamp-Welch [2] error correction algorithm to achieve the ability to actually correct errors.

Caution: this package API leans toward providing the user more power and performance at the expense of having some really sharp edges! Read the documentation about memory lifecycles carefully!

We wrote a blog post about how this library works! https://innovation.vivint.com/introduction-to-reed-solomon-bc264d0794f8

[1] https://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction
[2] https://en.wikipedia.org/wiki/Berlekamp%E2%80%93Welch_algorithm

Index

Constants

This section is empty.

Variables

View Source
var (
	NotEnoughShares = errors.New("not enough shares")
	TooManyErrors   = errors.New("too many errors to reconstruct")
)

Functions

This section is empty.

Types

type FEC

type FEC struct {
	// contains filtered or unexported fields
}

FEC represents operations performed on a Reed-Solomon-based forward error correction code. Make sure to construct using NewFEC.

func NewFEC

func NewFEC(k, n int) (*FEC, error)

NewFEC creates a *FEC using k required pieces and n total pieces. Encoding data with this *FEC will generate n pieces, and decoding data requires k uncorrupted pieces. If during decode more than k pieces exist, corrupted data can be detected and recovered from.

func (*FEC) Correct

func (fc *FEC) Correct(shares []Share) error

Correct implements the Berlekamp-Welch algorithm for correcting errors in given FEC encoded data. It will correct the supplied shares, mutating the underlying byte slices and reordering the shares

func (*FEC) Decode

func (f *FEC) Decode(dst []byte, shares []Share) ([]byte, error)

Decode will take a destination buffer (can be nil) and a list of shares (pieces). It will return the data passed in to the corresponding Encode call or return an error.

It will first correct the shares using Correct, mutating and reordering the passed-in shares arguments. Then it will rebuild the data using Rebuild. Finally it will concatenate the data into the given output buffer dst if it has capacity, growing it otherwise.

If you already know your data does not contain errors, Rebuild will be faster.

If you only want to identify which pieces are bad, you may be interested in Correct.

If you don't want the data concatenated for you, you can use Correct and then Rebuild individually.

func (*FEC) Encode

func (f *FEC) Encode(input []byte, output func(Share)) error

Encode will take input data and encode to the total number of pieces n this *FEC is configured for. It will call the callback output n times.

The input data must be a multiple of the required number of pieces k. Padding to this multiple is up to the caller.

Note that the byte slices in Shares passed to output may be reused when output returns.

func (*FEC) EncodeSingle

func (f *FEC) EncodeSingle(input, output []byte, num int) error

EncodeSingle will take input data and encode it to output only for the num piece.

The input data must be a multiple of the required number of pieces k. Padding to this multiple is up to the caller.

The output must be exactly len(input) / k bytes.

The num must be 0 <= num < n.

func (*FEC) Rebuild

func (f *FEC) Rebuild(shares []Share, output func(Share)) error

Rebuild will take a list of corrected shares (pieces) and a callback output. output will be called k times ((*FEC).Required() times) with 1/k of the original data each time and the index of that data piece. Decode is usually preferred.

Note that the data is not necessarily sent to output ordered by the piece number.

Note that the byte slices in Shares passed to output may be reused when output returns.

Rebuild assumes that you have already called Correct or did not need to.

func (*FEC) Required

func (f *FEC) Required() int

Required returns the number of required pieces for reconstruction. This is the k value passed to NewFEC.

func (*FEC) Total

func (f *FEC) Total() int

Total returns the number of total pieces that will be generated during encoding. This is the n value passed to NewFEC.

type Share

type Share struct {
	Number int
	Data   []byte
}

A Share represents a piece of the FEC-encoded data. Both fields are required.

func (*Share) DeepCopy

func (s *Share) DeepCopy() (c Share)

DeepCopy makes getting a deep copy of a Share easier. It will return an identical Share that uses all new memory locations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL