class galois.ReedSolomon

A general \(\mathrm{RS}(n, k)\) code over \(\mathrm{GF}(q)\).

A \(\mathrm{RS}(n, k)\) code is a maximum-distance separable (MDS) \([n, k, n - k + 1]_q\) linear block code of length \(n\), dimension \(k\), and minimum distance \(d = n - k + 1\), with symbols drawn from \(\mathrm{GF}(q)\). Reed-Solomon codes achieve the largest possible minimum distance for any \(q\)-ary linear block code with the same length and dimension.

Shortened codes

To create the shortened \(\mathrm{RS}(n - s, k - s)\) code, construct the full-length \(\mathrm{RS}(n, k)\) code, pass only the first \((k - s)\) message symbols to encode(), and provide only the first \((n - s)\) received symbols to decode(). This produces the standard shortened RS construction.

Reed-Solomon codes admit two mathematically equivalent interpretations: the BCH (cyclic, root-based) formulation and the polynomial evaluation formulation. This implementation follows the BCH construction, which realizes RS codes as cyclic codes whose generator polynomials are specified by a consecutive set of roots in an extension field.

BCH (cyclic) construction. Assume \(n \mid (q - 1)\), so that \(\mathrm{GF}(q)\) contains a primitive \(n\)-th root of unity. Let \(\alpha \in \mathrm{GF}(q)\) be such an element. A Reed-Solomon code of designed distance \(d = n - k + 1\) and starting exponent \(c\) is the cyclic code whose generator polynomial has the consecutive roots

\[ \alpha^c,\ \alpha^{c+1},\ \ldots,\ \alpha^{c + (d - 2)}. \]

Because all these roots lie in \(\mathrm{GF}(q)\) itself (in contrast to BCH codes in general, which require \(\mathrm{GF}(q^m)\)), the generator polynomial is simply the product of linear factors:

\[ g(x) = (x - \alpha^c)\, (x - \alpha^{c+1}) \cdots (x - \alpha^{c + d - 2}). \]

The resulting cyclic code has length \(n\), generator degree \(\deg g(x) = n - k\), and therefore dimension \(k = n - \deg g(x)\). Its minimum distance satisfies \(d = n - k + 1\), i.e., the code is MDS.

Evaluation-code interpretation (equivalent). For completeness: RS codes may also be defined by evaluating a message polynomial \(m(x) = m_0 + m_1 x + \cdots + m_{k-1} x^{k-1}\) at a set of \(n\) distinct points in \(\mathrm{GF}(q)\), typically \(\{\alpha^0, \alpha^1, \ldots, \alpha^{n-1}\}\). In this viewpoint,

\[ (\, m(\alpha^0), m(\alpha^1), \ldots, m(\alpha^{n-1}) \,) \in \mathrm{GF}(q)^n \]

is the codeword corresponding to \(m(x)\). Choosing the evaluation points as consecutive powers of a primitive element yields a cyclic RS code whose generator matches the BCH construction above. Thus the BCH formulation used here is fully consistent with the general algebraic interpretation of Reed-Solomon codes.

Examples

Construct a \(\textrm{RS}(15, 9)\) code.

In [1]: rs = galois.ReedSolomon(15, 9); rs
Out[1]: <Reed-Solomon Code: [15, 9, 7] over GF(2^4)>

In [2]: GF = rs.field; GF
Out[2]: <class 'galois.GF(2^4, primitive_element='x', irreducible_poly='x^4 + x + 1')'>

Encode a message.

In [3]: m = GF.Random(rs.k); m
Out[3]: GF([11,  6,  9, 11,  9,  9,  0,  2, 14], order=2^4)

In [4]: c = rs.encode(m); c
Out[4]: GF([11,  6,  9, 11,  9,  9,  0,  2, 14,  6, 14, 15,  7,  2,  0], order=2^4)

Corrupt the codeword and decode the message.

# Corrupt the first symbol in the codeword
In [5]: c[0] ^= 13; c
Out[5]: GF([ 6,  6,  9, 11,  9,  9,  0,  2, 14,  6, 14, 15,  7,  2,  0], order=2^4)

In [6]: dec_m = rs.decode(c); dec_m
Out[6]: GF([11,  6,  9, 11,  9,  9,  0,  2, 14], order=2^4)

In [7]: assert np.array_equal(dec_m, m)

Instruct the decoder to return the number of corrected symbol errors.

In [8]: dec_m, N = rs.decode(c, errors=True); dec_m, N
Out[8]: (GF([11,  6,  9, 11,  9,  9,  0,  2, 14], order=2^4), 1)

In [9]: assert np.array_equal(dec_m, m)

Constructors

ReedSolomon(n: int, k: int | None = None, d: int | None = None, ...)

Constructs a general \(\textrm{RS}(n, k)\) code over \(\mathrm{GF}(q)\).

String representation

__repr__() str

A terse representation of the Reed-Solomon code.

__str__() str

A formatted string with relevant properties of the Reed-Solomon code.

Methods

decode(codeword: ArrayLike, ...) FieldArray
decode(codeword: ArrayLike, ...) tuple[FieldArray, int | ndarray]

Decodes the codeword \(\mathbf{c}\) into the message \(\mathbf{m}\).

detect(codeword: ArrayLike) bool | ndarray

Detects if errors are present in the codeword \(\mathbf{c}\).

encode(message: ArrayLike, ...) FieldArray

Encodes the message \(\mathbf{m}\) into the codeword \(\mathbf{c}\).

Properties

property c : int

The first consecutive power \(c\) of \(\alpha\) that defines the roots \(\alpha^c, \dots, \alpha^{c+d-2}\) of the generator polynomial \(g(x)\).

property d : int

The minimum distance \(d\) of the \([n, k, d]_q\) code.

property field : type[FieldArray]

The Galois field \(\mathrm{GF}(q)\) that defines the codeword alphabet.

property k : int

The message size \(k\) of the \([n, k, d]_q\) code. This is also called the code dimension.

property n : int

The codeword size \(n\) of the \([n, k, d]_q\) code. This is also called the code length.

property t : int

The error-correcting capability \(t\) of the code.

Attributes

property is_narrow_sense : bool

Indicates if the Reed-Solomon code is narrow-sense, meaning the roots of the generator polynomial are consecutive powers of \(\alpha\) starting at 1, that is \(\alpha, \dots, \alpha^{d-1}\).

property is_primitive : bool

Indicates if the Reed-Solomon code is primitive, meaning \(n = q - 1\).

property is_systematic : bool

Indicates if the code is systematic, meaning the codewords have parity appended to the message.

Matrices

property G : FieldArray

The generator matrix \(\mathbf{G}\) with shape \((k, n)\).

property H : FieldArray

The parity-check matrix \(\mathbf{H}\) with shape \((n - k, n)\).

Polynomials

property alpha : FieldArray

A primitive \(n\)-th root of unity \(\alpha\) in \(\mathrm{GF}(q)\) whose consecutive powers \(\alpha^c, \dots, \alpha^{c+d-2}\) are roots of the generator polynomial \(g(x)\).

property generator_poly : Poly

The generator polynomial \(g(x)\) over \(\mathrm{GF}(q)\).

property parity_check_poly : Poly

The parity-check polynomial \(h(x)\).

property roots : FieldArray

The \(d - 1\) roots of the generator polynomial \(g(x)\).