### Install UniReedSolomon (Python 3.7+) Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Install the latest version of the UniReedSolomon library for Python 3.7 and newer. ```sh pip install --upgrade unireedsolomon ``` -------------------------------- ### Quickstart: Encode and Decode Message Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Demonstrates basic usage of the RSCoder class for encoding a message and then decoding it after introducing simulated corruption. ```python import unireedsolomon as rs coder = rs.RSCoder(20,13) c = coder.encode("Hello, world!") print repr(c) r = "\0"*3 + c[3:] print repr(r) coder.decode(r) ``` -------------------------------- ### Install UniReedSolomon with Cython Optimization Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Install UniReedSolomon with Cython speed-optimized modules for a significant performance boost during encoding. Requires Cython 0.29.x. ```sh pip install --upgrade unireedsolomon --config-setting="--build-option=--cythonize" ``` -------------------------------- ### Install UniReedSolomon (Python <= 3.6) Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Install version 1.0.5 of the UniReedSolomon library for Python 2.7 and versions up to 3.6. ```sh pip install --upgrade unireedsolomon==1.0.5 ``` -------------------------------- ### Encode and Decode Data Blocks Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Example implementation for encoding text into Reed-Solomon protected blocks and decoding them back. ```python # Example usage of imageencode module (requires PIL/Pillow) from unireedsolomon import rs from io import BytesIO # Create RS coder with standard parameters coder = rs.RSCoder(255, 223) # Encode text data row by row def encode_text_to_blocks(text): """Encode text into RS-protected blocks of 255 bytes each.""" blocks = [] # Pad text to multiple of 223 bytes padded = text + '\x00' * (223 - len(text) % 223) for i in range(0, len(padded), 223): block = padded[i:i+223] encoded = coder.encode_fast(block) blocks.append(encoded) return blocks # Decode blocks back to text def decode_blocks_to_text(blocks): """Decode RS-protected blocks back to text.""" decoded_text = "" for block in blocks: try: decoded_msg, _ = coder.decode_fast(block) decoded_text += decoded_msg except Exception as e: print(f"Block decoding failed: {e}") decoded_text += block[:223] # Fallback to uncorrected data return decoded_text.rstrip('\x00') ``` -------------------------------- ### RSCoder.check() and RSCoder.check_fast() - Codeword Validation Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Verifies if a codeword is valid by checking if it divides the generator polynomial or has a zero syndrome. Returns True if valid, False if corrupted. Includes an example of using check_fast() after decoding to verify recovery. ```python import unireedsolomon as rs coder = rs.RSCoder(20, 13) message = "Hello, world!" encoded = coder.encode(message) # Check valid codeword is_valid = coder.check(encoded) print(f"Valid codeword: {is_valid}") # True # Check corrupted codeword corrupted = "X" + encoded[1:] is_valid = coder.check(corrupted) print(f"Corrupted codeword: {is_valid}") # False ``` ```python # Fast check (uses syndrome computation) is_valid_fast = coder.check_fast(encoded) print(f"Fast check valid: {is_valid_fast}") # True # Use check after decoding to verify success decoded_msg, decoded_ecc = coder.decode(corrupted) full_codeword = decoded_msg + decoded_ecc is_recovered = coder.check_fast(full_codeword) print(f"Recovery verified: {is_recovered}") # True # Warning: check() can return True for heavily corrupted messages # (beyond Singleton bound) that decoded incorrectly # Always use additional integrity checks (CRC, hash) for critical data ``` -------------------------------- ### Initialize Finite Field Generator Source: https://github.com/lrq3000/unireedsolomon/blob/master/Generating the exponent and log tables.ipynb Import the library and initialize a generator for the finite field. ```python import unireedsolomon.ff as ff ``` ```python generator = ff.GF2int(3) generator ``` -------------------------------- ### Initialize Polynomial with Keyword Arguments Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Create a Polynomial object by specifying coefficients using keyword arguments, where the keyword indicates the power of x. ```python print Polynomial(x32=5, x64=8) ``` -------------------------------- ### Initialize Polynomial with Mixed Arguments Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Create a Polynomial object using a mix of keyword arguments for specific powers and default coefficient ordering. ```python print Polynomial(x5=5, x9=4, x0=2) ``` -------------------------------- ### Initialize Galois Field Look-up Tables Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Generate the look-up tables for a Galois Field with specified parameters. This defines the field's arithmetic. ```python ff.init_lut(generator=3, prim=0x11b, c_exp=8) ``` -------------------------------- ### Galois Field Initialization Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Functions to find prime polynomials and initialize lookup tables for Galois Fields. ```APIDOC ## Galois Field Initialization Functions ### Description Provides functions to manage and configure Galois Fields, including finding suitable prime polynomials and initializing the necessary lookup tables. ### Functions - `ff.find_prime_polynomials(generator=2, c_exp=8, fast_primes=False, single=False)`: Finds a list of prime polynomials suitable for generating lookup tables for a specified Galois Field. - `generator` (int): The generator for the field. - `c_exp` (int): The exponent for the field (e.g., 8 for GF(2^8)). - `fast_primes` (bool): Whether to use fast prime finding algorithms. - `single` (bool): Whether to return only a single prime polynomial. - `ff.init_lut(generator=3, prim=0x11b, c_exp=8)`: Generates the lookup tables for a Galois Field based on the provided parameters. This effectively parametrizes the Galois Field. - `generator` (int): The generator for the field. - `prim` (int): The irreducible polynomial defining the field. - `c_exp` (int): The exponent for the field (e.g., 8 for GF(2^8)). ``` -------------------------------- ### Perform Galois Field Arithmetic Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Demonstrates basic arithmetic operations and lookup table initialization for GF(2^p) fields. ```python from unireedsolomon.ff import GF2int, init_lut # Basic GF(256) arithmetic a = GF2int(100) b = GF2int(200) # Addition and subtraction are XOR in GF(2^p) print(f"100 + 200 = {a + b}") # XOR operation print(f"100 - 200 = {a - b}") # Same as addition in GF(2) print(f"-100 = {-a}") # Negation is identity in GF(2) # Multiplication and division use log/antilog tables print(f"100 * 200 = {a * b}") print(f"100 / 200 = {a / b}") # Exponentiation print(f"100^5 = {a ** 5}") # Multiplicative inverse print(f"inv(100) = {a.inverse()}") print(f"100 * inv(100) = {a * a.inverse()}") # Should be 1 # Reinitialize lookup tables for different GF parameters init_lut(generator=2, prim=0x11d, c_exp=8) # For GF(2^16), reinitialize with appropriate parameters from unireedsolomon.ff import find_prime_polynomials prim16 = find_prime_polynomials(generator=2, c_exp=16, single=True) init_lut(generator=2, prim=prim16, c_exp=16) # Now GF2int operates in GF(2^16) x = GF2int(1000) y = GF2int(50000) print(f"GF(2^16): 1000 * 50000 = {x * y}") ``` -------------------------------- ### Initialize Polynomial with Coefficients Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Create a Polynomial object using a list, tuple, or other iterable for coefficients, ordered by decreasing power. ```python print Polynomial([5, 0, 0, 0, 0, 0]) ``` -------------------------------- ### Initialize Empty Polynomial Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Create an empty Polynomial object, which is equivalent to a polynomial with a single coefficient of 0. ```python Polynomial([0]) ``` -------------------------------- ### Generate Log Table Source: https://github.com/lrq3000/unireedsolomon/blob/master/Generating the exponent and log tables.ipynb Construct the inverse log table from the exponent table. ```python logtable = [None for _ in range(256)] # Ignore the last element of the field because fields wrap back around. # The log of 1 could be 0 (g^0=1) or it could be 255 (g^255=1) for i, x in enumerate(exptable[:-1]): logtable[x] = i print([int(x) if x is not None else None for x in logtable]) ``` ```python [int(x) if x is not None else None for x in logtable] == [int(x) if x >= 0 else None for x in ff.GF2int_logtable] ``` -------------------------------- ### Initialize RSCoder Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Configure the Reed-Solomon coder with specific generator, primitive polynomial, and first consecutive root parameters. ```python # Use custom prime polynomial in RSCoder import unireedsolomon as rs # Standard configurations for common applications: # QR Code: generator=2, prim=0x11d, fcr=0 # DVD: generator=2, prim=0x11d, fcr=1 qr_coder = rs.RSCoder(255, 223, generator=2, prim=0x11d, fcr=0) ``` -------------------------------- ### Simulate Reed-Solomon Error Correction in Python Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Demonstrates encoding text into blocks, simulating data corruption, and recovering the original message. ```python text = "This is a test message that will be protected with Reed-Solomon codes." blocks = encode_text_to_blocks(text) print(f"Encoded {len(text)} bytes into {len(blocks)} protected blocks") # Simulate corruption (up to 16 bytes per 255-byte block can be recovered) corrupted_blocks = [] for block in blocks: # Corrupt first 10 bytes of each block corrupted = '\xff' * 10 + block[10:] corrupted_blocks.append(corrupted) # Recover original text recovered = decode_blocks_to_text(corrupted_blocks) print(f"Recovered text: {recovered}") print(f"Recovery successful: {recovered == text}") ``` -------------------------------- ### Initialize GF2int Object Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Create an instance of GF2int, representing an element in the finite field GF(2^p). Defaults to GF(2^8). ```python ff.GF2int(value) ``` -------------------------------- ### Execute Command-Line Encoding and Decoding Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Provides CLI commands for processing text, files, and images using standard RS(255, 223) parameters. ```bash # Encode text from stdin to stdout echo "Hello, World!" | python -m unireedsolomon.rs > encoded.bin # Decode RS-encoded data python -m unireedsolomon.rs -d < encoded.bin # Encode a file python -m unireedsolomon.rs < input.txt > encoded.bin # Decode a file python -m unireedsolomon.rs -d < encoded.bin > decoded.txt # Image encoding example (requires PIL) python -m unireedsolomon.imageencode input.txt output.png python -m unireedsolomon.imageencode -d output.png > recovered.txt ``` -------------------------------- ### Create RSCoder Instances Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Instantiate RSCoder with different parameters for standard or custom Reed-Solomon configurations. Supports various Galois Field parameters for compatibility with other implementations. ```python import unireedsolomon as rs # Create a standard RS(255, 223) coder - can correct up to 16 errors or 32 erasures coder = rs.RSCoder(255, 223) # Create a smaller coder RS(20, 13) - can correct up to 3 errors or 7 erasures small_coder = rs.RSCoder(20, 13) # Create a coder with custom Galois Field parameters for compatibility # with other RS implementations (e.g., QR codes, DVB, etc.) custom_coder = rs.RSCoder( n=255, # codeword length k=223, # message length generator=2, # generator number (must be prime) prim=0x11d, # primitive polynomial fcr=0, # first consecutive root c_exp=8 # GF(2^8) = GF(256) ) # For larger fields (e.g., GF(2^16) for longer messages) # First find a suitable prime polynomial from unireedsolomon import find_prime_polynomials primes = find_prime_polynomials(generator=2, c_exp=16, single=True) large_coder = rs.RSCoder(65535, 65503, generator=2, prim=primes, c_exp=16) ``` -------------------------------- ### RSCoder Initialization Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Creates a new Reed-Solomon Encoder/Decoder object. ```APIDOC ## RSCoder(n, k, generator=3, prim=0x11b, fcr=1, c_exp=8) ### Description Creates a new Reed-Solomon Encoder/Decoder object configured with the given n and k values. ### Parameters - **n** (int) - Required - Length of a codeword, must be less than 256. - **k** (int) - Required - Length of the message, must be less than n. - **generator** (int) - Optional - Parametrizes the Galois Field. - **prim** (int) - Optional - Parametrizes the Galois Field. - **fcr** (int) - Optional - Parametrizes the Galois Field. - **c_exp** (int) - Optional - Galois Field range (e.g., 8 means GF(2^8)). ``` -------------------------------- ### RSCoder.decode_fast() - Optimized Decoding Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Uses optimized Berlekamp-Massey and Chien search algorithms for faster decoding. Best performance is achieved with PyPy. Demonstrates decoding with random errors and known erasures. ```python import unireedsolomon as rs coder = rs.RSCoder(255, 223) # Create message and introduce errors message = "The quick brown fox jumps over the lazy dog. " * 5 encoded = coder.encode_fast(message[:223]) # Corrupt 10 random positions (within correction capability of 16) corrupted = list(encoded) error_positions = [5, 23, 67, 89, 102, 145, 178, 200, 220, 250] for pos in error_positions: corrupted[pos] = chr((ord(corrupted[pos]) + 50) % 256) corrupted = ''.join(corrupted) # Fast decode decoded_msg, decoded_ecc = coder.decode_fast(corrupted) print(f"Original length: {len(message[:223])}") print(f"Decoded length: {len(decoded_msg)}") print(f"Decoded matches: {decoded_msg == message[:223]}") ``` ```python # Fast decode with erasures for maximum correction # With 16 known erasure positions, we can correct all 16 erasures = list(range(16)) # First 16 bytes are "erased" corrupted_erasures = "\x00" * 16 + encoded[16:] decoded_msg, _ = coder.decode_fast(corrupted_erasures, erasures_pos=erasures) print(f"16 erasures corrected: {decoded_msg == message[:223]}") ``` ```python # Only erasures mode (faster when you know there are no random errors) decoded_msg, _ = coder.decode_fast( corrupted_erasures, erasures_pos=erasures, only_erasures=True ) print(f"Only erasures mode: {decoded_msg == message[:223]}") ``` -------------------------------- ### Perform Finite Field Exponentiation Source: https://github.com/lrq3000/unireedsolomon/blob/master/Generating the exponent and log tables.ipynb Raise field elements to a power. ```python generator**1 ``` ```python generator**2 ``` ```python generator**3 ``` -------------------------------- ### Generate Exponent Table Source: https://github.com/lrq3000/unireedsolomon/blob/master/Generating the exponent and log tables.ipynb Enumerate the field elements by repeatedly multiplying by the generator. ```python exptable = [ff.GF2int(1), generator] for _ in range(254): # minus 2 because the first 2 elements are hardcoded exptable.append(exptable[-1].multiply(generator)) # Turn back to ints for a more compact print representation print([int(x) for x in exptable]) ``` -------------------------------- ### Verify Exponent Table Source: https://github.com/lrq3000/unireedsolomon/blob/master/Generating the exponent and log tables.ipynb Validate the generated exponent table against generator powers and built-in tables. ```python exptable[5] == generator**5 ``` ```python all(exptable[n] == generator**n for n in range(256)) ``` ```python [int(x) for x in exptable] == [int(x) for x in ff.GF2int_exptable] ``` -------------------------------- ### Find Prime Polynomials for GF Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Utility functions to identify prime polynomials for specific Galois Field configurations. ```python # Find prime polynomials for GF(2^16) - for longer codewords prime_gf65536 = find_prime_polynomials(generator=2, c_exp=16, single=True) print(f"Prime for GF(2^16): {hex(prime_gf65536)}") # Fast primes mode - tests only actual prime numbers (fewer results but faster) fast_primes = find_prime_polynomials(generator=2, c_exp=8, fast_primes=True) print(f"Fast primes found: {len(fast_primes)}") ``` -------------------------------- ### RSCoder Initialization Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Initializes the Reed-Solomon coder with specific codeword length, message length, and Galois Field parameters. ```APIDOC ## RSCoder Initialization ### Description Initializes the RSCoder class to handle Reed-Solomon encoding and decoding with configurable parameters. ### Parameters #### Request Body - **n** (int) - Required - Codeword length - **k** (int) - Required - Message length - **generator** (int) - Optional - Generator number (must be prime) - **prim** (int) - Optional - Primitive polynomial - **fcr** (int) - Optional - First consecutive root - **c_exp** (int) - Optional - Galois Field exponent (e.g., 8 for GF(2^8)) ``` -------------------------------- ### Fast Encoding with RSCoder Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Utilize the optimized encode_fast() method for faster Reed-Solomon encoding. It provides identical results to encode() but with improved performance, especially on PyPy. ```python import unireedsolomon as rs import time coder = rs.RSCoder(255, 223) message = "A" * 223 # Maximum length message # Compare standard vs fast encoding start = time.time() for _ in range(100): encoded_standard = coder.encode(message) standard_time = time.time() - start start = time.time() for _ in range(100): encoded_fast = coder.encode_fast(message) fast_time = time.time() - start print(f"Standard encode: {standard_time:.4f}s") print(f"Fast encode: {fast_time:.4f}s") print(f"Results match: {encoded_standard == encoded_fast}") # Output (times will vary): # Standard encode: 0.8234s # Fast encode: 0.3156s # Results match: True ``` -------------------------------- ### Execute Polynomial Operations Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Perform arithmetic, division, evaluation, and differentiation on polynomials, including those over Galois Fields. ```python from unireedsolomon.polynomial import Polynomial from unireedsolomon.ff import GF2int # Create polynomials using coefficient lists (highest degree first) # Represents: 5x^2 + 3x + 1 p1 = Polynomial([5, 3, 1]) print(f"p1 = {p1}") # Create using keyword arguments (x{degree}=coefficient) # Represents: 8x^4 + 5x^2 + 2 p2 = Polynomial(x4=8, x2=5, x0=2) print(f"p2 = {p2}") # Polynomial arithmetic p3 = Polynomial([2, 1]) # 2x + 1 p4 = Polynomial([3, 4]) # 3x + 4 print(f"Addition: {p3} + {p4} = {p3 + p4}") print(f"Subtraction: {p3} - {p4} = {p3 - p4}") print(f"Multiplication: {p3} * {p4} = {p3 * p4}") # Division with remainder quotient, remainder = divmod(p1, p3) print(f"Division: {p1} / {p3} = {quotient} remainder {remainder}") # Evaluate polynomial at a value result = p1.evaluate(2) # 5(2)^2 + 3(2) + 1 = 27 print(f"p1(2) = {result}") # Polynomials over Galois Fields (for Reed-Solomon) gf_p1 = Polynomial([GF2int(5), GF2int(3), GF2int(1)]) gf_p2 = Polynomial([GF2int(2), GF2int(7)]) gf_product = gf_p1 * gf_p2 print(f"GF polynomial product: {gf_product}") # Get coefficient by degree print(f"Coefficient of x^1 in p1: {p1.get_coefficient(1)}") print(f"Degree of p1: {p1.degree}") # Formal derivative (used in Forney algorithm) derivative = gf_p1.derive() print(f"Derivative of {gf_p1}: {derivative}") ``` -------------------------------- ### Find Prime Polynomials for Galois Field Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Find a list of prime polynomials suitable for generating look-up tables for a specified Galois Field. ```python ff.find_prime_polynomials(generator=2, c_exp=8, fast_primes=False, single=False) ``` -------------------------------- ### find_prime_polynomials() - Galois Field Configuration Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Computes prime (irreducible) polynomials for configuring custom Galois Fields. Essential for compatibility with specific RS implementations or larger fields like GF(2^16). ```python from unireedsolomon.ff import find_prime_polynomials # Find all prime polynomials for GF(2^8) with generator 2 all_primes_gf256 = find_prime_polynomials(generator=2, c_exp=8) print(f"GF(2^8) prime polynomials: {len(all_primes_gf256)} found") print(f"First few: {[hex(p) for p in all_primes_gf256[:5]]}") ``` ```python # Find just one prime polynomial (faster) single_prime = find_prime_polynomials(generator=2, c_exp=8, single=True) print(f"Single prime for GF(2^8): {hex(single_prime)}") ``` -------------------------------- ### Polynomial Class Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Represents a polynomial with coefficients. Supports initialization via lists, keyword arguments, or empty. Provides standard polynomial arithmetic operations. ```APIDOC ## Polynomial Class ### Description Represents a polynomial with coefficients. Supports initialization via lists, keyword arguments, or empty. Provides standard polynomial arithmetic operations. ### Initialization 1. **From Iterable**: `Polynomial(coefficients=[5, 0, 0, 0, 0, 0])` 2. **From Keyword Arguments**: `Polynomial(x32=5, x64=8)` 3. **Empty**: `Polynomial()` ### Methods - `__add__(self, other)`: Adds two polynomials. - `__divmod__(self, other)`: Divides two polynomials and returns quotient and remainder. - `__eq__(self, other)`: Checks for equality between two polynomials. - `__floordiv__(self, other)`: Integer division of polynomials. - `__hash__(self)`: Returns the hash of the polynomial. - `__len__(self)`: Returns the number of terms in the polynomial. - `__mod__(self, other)`: Returns the remainder of polynomial division. - `__mul__(self, other)`: Multiplies two polynomials. - `__ne__(self, other)`: Checks for inequality between two polynomials. - `__neg__(self)`: Negates the polynomial. - `__sub__(self, other)`: Subtracts two polynomials. - `evaluate(self, x)`: Evaluates the polynomial at a given value `x`. - `degree(self)`: Returns the degree of the polynomial. - `get_coefficient(self, degree)`: Returns the coefficient of the specified term degree. ``` -------------------------------- ### Verification Methods Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Methods to verify the integrity of a codeword. ```APIDOC ## RSCoder.check(message_ecc, k=None) ## RSCoder.check_fast(message_ecc, k=None) ### Description Verifies the codeword is valid by testing if the code as a polynomial code divides g or if the syndrome is all 0 coefficients. ### Response - **result** (bool) - Returns True if valid, False if corrupted. ``` -------------------------------- ### Decode corrupted data with erasures Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Demonstrates decoding corrupted data, including scenarios with known erasure positions and mixed errors/erasures. Preserves leading null bytes for binary data. ```python corrupted = "\x00\x00\x00" + encoded[3:] print(f"Corrupted: {repr(corrupted)}") # Decode and recover the original message decoded_message, decoded_ecc = coder.decode(corrupted) print(f"Decoded: {repr(decoded_message)}") print(f"Success: {decoded_message == original}") ``` ```python # Decode with erasures (known error positions) - can correct twice as many # Erasures: positions 0, 1, 2, 3, 4, 5 are corrupted (6 erasures) corrupted_heavy = "\x00\x00\x00\x00\x00\x00" + encoded[6:] erasure_positions = [0, 1, 2, 3, 4, 5] decoded_msg, decoded_ecc = coder.decode(corrupted_heavy, erasures_pos=erasure_positions) print(f"Heavy corruption decoded: {repr(decoded_msg)}") ``` ```python # Mixed errors and erasures decoding # Can correct 2*e + v <= 7 (for RS(20,13)) mixed_corrupted = "\x00\x00" + "XX" + encoded[4:] # 2 erasures + 2 errors decoded_msg, _ = coder.decode(mixed_corrupted, erasures_pos=[0, 1]) print(f"Mixed errors/erasures: {repr(decoded_msg)}") ``` ```python # Preserve leading null bytes for binary data binary_data = "\x00\x00\x00Test" encoded_binary = coder.encode(binary_data) decoded_binary, _ = coder.decode(encoded_binary, nostrip=True) print(f"Binary preserved: {repr(decoded_binary)}") ``` -------------------------------- ### RSCoder.encode_fast() Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Optimized encoding method using synthetic division for better performance. ```APIDOC ## RSCoder.encode_fast() ### Description A faster encoding method using synthetic division and optimization tricks, providing identical results to encode(). ### Parameters #### Request Body - **message** (str/bytes) - Required - The data to encode ``` -------------------------------- ### Encoding Methods Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Methods for encoding messages using Reed-Solomon. ```APIDOC ## RSCoder.encode(message, poly=False, k=None) ## RSCoder.encode_fast(message, poly=False, k=None) ### Description Encodes a given string with Reed-Solomon encoding. Returns a byte string with the k message bytes and n-k parity bytes at the end. ### Parameters - **message** (string/bytes) - Required - The message to encode. - **poly** (bool) - Optional - If True, returns the encoded Polynomial object. - **k** (int) - Optional - Number of parity/ecc bytes to use. ``` -------------------------------- ### Perform Finite Field Multiplication Source: https://github.com/lrq3000/unireedsolomon/blob/master/Generating the exponent and log tables.ipynb Multiply field elements using standard operators or the explicit multiply method. ```python generator*generator ``` ```python generator*generator*generator ``` ```python generator.multiply(generator) ``` ```python generator.multiply(generator.multiply(generator)) ``` -------------------------------- ### Decoding Methods Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Methods for decoding and correcting errors in messages. ```APIDOC ## RSCoder.decode(message_ecc, nostrip=False, k=None, erasures_pos=None, only_erasures=False) ## RSCoder.decode_fast(message_ecc, nostrip=False, k=None, erasures_pos=None, only_erasures=False) ### Description Attempts to decode a received string or byte array. If valid or within the Singleton bound, the message is returned. ### Parameters - **message_ecc** (string/bytes) - Required - The received message + ecc symbols. - **nostrip** (bool) - Optional - If True, returns k bytes long messages. - **k** (int) - Optional - Number of parity/ecc bytes. - **erasures_pos** (list) - Optional - List of erasure positions. - **only_erasures** (bool) - Optional - If True, assumes only erasures exist. ``` -------------------------------- ### GF2int Class Source: https://github.com/lrq3000/unireedsolomon/blob/master/README.rst Represents elements of a Galois Field GF(2^p). Supports standard integer operations overridden for finite field arithmetic. ```APIDOC ## GF2int Class ### Description Represents elements of a Galois Field GF(2^p). Supports standard integer operations overridden for finite field arithmetic. By default, it is GF(2^8) with specific irreducible polynomial and generator. ### Initialization - `ff.GF2int(value)`: Creates an instance of GF2int. ### Methods - `__add__(self, other)`: Addition in GF(2^p). - `__div__(self, other)`: Division in GF(2^p). - `__mul__(self, other)`: Multiplication in GF(2^p). - `__neg__(self)`: Negation in GF(2^p). - `__pow__(self, exponent)`: Exponentiation in GF(2^p). - `__radd__(self, other)`: Right-side addition in GF(2^p). - `__rdiv__(self, other)`: Right-side division in GF(2^p). - `__rmul__(self, other)`: Right-side multiplication in GF(2^p). - `__rsub__(self, other)`: Right-side subtraction in GF(2^p). - `__sub__(self, other)`: Subtraction in GF(2^p). - `inverse(self)`: Computes the multiplicative inverse in GF(2^p). ``` -------------------------------- ### RSCoder.encode() Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Encodes a message string or byte array into a codeword with parity bytes. ```APIDOC ## RSCoder.encode() ### Description Encodes a message string or byte array with Reed-Solomon parity bytes appended at the end. ### Parameters #### Request Body - **message** (str/bytes) - Required - The data to encode - **k** (int) - Optional - Override message length - **return_string** (bool) - Optional - If False, returns list of integers - **poly** (bool) - Optional - If True, returns polynomial object ``` -------------------------------- ### Encode Message with RSCoder Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Encode a message string or byte array using the RSCoder. The method appends Reed-Solomon parity bytes and returns the full codeword. Supports variable redundancy and different return types. ```python import unireedsolomon as rs coder = rs.RSCoder(20, 13) # Encode a simple text message message = "Hello, world!" encoded = coder.encode(message) print(f"Original: {repr(message)}") print(f"Encoded: {repr(encoded)}") print(f"Length: {len(encoded)} bytes (13 message + 7 parity)") # Output: # Original: 'Hello, world!' # Encoded: 'Hello, world!\x8d\x13\xf4\xf9C\x10\xe5' # Length: 20 bytes (13 message + 7 parity) # Encode with variable redundancy (override k at encode time) high_redundancy = coder.encode(message[:10], k=10) # 10 message + 10 parity bytes print(f"High redundancy: {len(high_redundancy)} bytes") # Return as list of integers instead of string (useful for GF > 256) encoded_list = coder.encode(message, return_string=False) print(f"As integers: {encoded_list}") # Return polynomial object for debugging encoded_poly = coder.encode(message, poly=True) print(f"As polynomial: {encoded_poly}") ``` -------------------------------- ### Decode Codeword with RSCoder Source: https://context7.com/lrq3000/unireedsolomon/llms.txt Decode a received Reed-Solomon codeword using the decode() method. It automatically detects and corrects errors up to the Singleton bound and supports erasure decoding. ```python import unireedsolomon as rs coder = rs.RSCoder(20, 13) original = "Hello, world!" encoded = coder.encode(original) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.