ffrs.RS256#
- class ffrs.RS256#
Reed-Solomon coding over \(GF(2^8)\)
Methods
Instantiate a Reed-Solomon encoder with the given configuration
Systematic decode
Systematic encode
Encode the input buffer in blocks, storing the parity bytes right next to their corresponding block
Attributes
RS256.block_lenRS256.ecc_lenRS256.generatorRS256.generator_rootsRS256.gfRS256.message_len- __init__(self: ffrs.RS256, block_len: int | None = None, message_len: int | None = None, ecc_len: int | None = None, primitive: int = 2, polynomial: int = 285) None#
Instantiate a Reed-Solomon encoder with the given configuration
- Examples:
RS256(255, 223)Equivalent to
RS256(ecc_len=32)Creates an encoder for 32 bytes of parity, capable of correcting up to 16 errors in a 255-byte block.
RS256(6, 4)block_len ╭──────┴──────╮ 🟦🟦🟦🟦🟨🟨 ╰────┬───╯╰─┬─╯ message_len ecc_len
- Parameters:
- block_len
- \(n\) – default block size used by
encode_blocks()block_len = message_len + ecc_lenDefaults to255Note
The maximum possible
block_lenforRS256is 255 bytes
- message_len
- \(k\) – number of actual data bytes in a blockCan be omitted if
ecc_lenis supplied
- ecc_len
- \((n - k)\) – number of parity bytes in a blockCan be omitted if
message_lenis supplied
- primitive
- \(a\) – primitive value for
GF256Defaults to2
- polynomial
- \(P\) – irreducible polynomial for
GF256Defaults to0x11b
- decode(self: ffrs.RS256, buffer: Buffer) bool#
Systematic decode
- encode(self: ffrs.RS256, buffer: Buffer) None#
Systematic encode
- encode_blocks(self: ffrs.RS256, buffer: Buffer, block_len: int | None = None) bytearray#
Encode the input buffer in blocks, storing the parity bytes right next to their corresponding block
- Example:
Considering
RS256(6, 4)and 32 bytes of input:
import ffrs, random rs62 = ffrs.RS256(6, 4) input_buffer = random.randbytes(32) output_buffer = rs62.encode_blocks(input_buffer)
Input buffer (32 bytes): 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦🟦🟦🟦 Output buffer (32 data bytes + 16 parity bytes): 🟦🟦🟦🟦🟨🟨🟦🟦🟦🟦🟨🟨🟦🟦🟦🟦🟨🟨🟦🟦🟦🟦🟨🟨 🟦🟦🟦🟦🟨🟨🟦🟦🟦🟦🟨🟨🟦🟦🟦🟦🟨🟨🟦🟦🟦🟦🟨🟨
Note
To allow concatenating the results of multiple calls to
encode_blocks(), the size ofbuffershould be a multiple ofRS256.message_len(orblock_size - RS256.ecc_lenif the argumentblock_sizeis notNone)If the size of the input buffer is not a multiple of
RS256.message_lenthe remaining bytes at the end of the buffer will be encoded as if the block size were smaller.- Example:
Considering
RS256(6, 4)and 29 bytes of input:
Input buffer (29 bytes): 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦🟦🟦🟦 🟦 Output buffer (29 data bytes + 16 parity bytes): 🟦🟦🟦🟦🟨🟨🟦🟦🟦🟦🟨🟨🟦🟦🟦🟦🟨🟨🟦🟦🟦🟦🟨🟨 🟦🟦🟦🟦🟨🟨🟦🟦🟦🟦🟨🟨🟦🟦🟦🟦🟨🟨🟦🟨🟨