pycyphal.transport.commons.crc package

Module contents

This module contains implementations of various CRC algorithms used by the transports.

32-Bit Cyclic Redundancy Codes for Internet Applications (Philip Koopman).

class pycyphal.transport.commons.crc.CRCAlgorithm[source]

Bases: ABC

Implementations are default-constructible.

abstract add(data: bytes | bytearray | memoryview) None[source]

Updates the value with the specified block of data.

abstract check_residue() bool[source]

Checks if the current state matches the algorithm-specific residue.

abstract property value: int[source]

The current CRC value, with output XOR applied, if applicable.

abstract property value_as_bytes: bytes[source]

The current CRC value serialized in the algorithm-specific byte order.

classmethod new(*fragments: bytes | bytearray | memoryview) CRCAlgorithm[source]

A factory that creates the new instance with the value computed over the fragments.

class pycyphal.transport.commons.crc.CRC16CCITT[source]

Bases: CRCAlgorithm

  • Name: CRC-16/CCITT-FALSE

  • Initial value: 0xFFFF

  • Polynomial: 0x1021

  • Reverse: No

  • Output XOR: 0

  • Residue: 0

  • Check: 0x29B1

>>> assert CRC16CCITT().value == 0xFFFF
>>> c = CRC16CCITT()
>>> c.add(b'123456')
>>> c.add(b'789')
>>> c.value
10673
>>> c.add(b'')
>>> c.value
10673
>>> c.add(c.value_as_bytes)
>>> c.value
0
>>> c.check_residue()
True
__init__() None[source]
add(data: bytes | bytearray | memoryview) None[source]
check_residue() bool[source]
property value: int[source]
property value_as_bytes: bytes[source]
class pycyphal.transport.commons.crc.CRC32C[source]

Bases: CRCAlgorithm

32-Bit Cyclic Redundancy Codes for Internet Applications (Philip Koopman).

CRC-32C (Castagnoli) for C++ and .NET.

  • Name: CRC-32/ISCSI, CRC-32C, CRC-32/CASTAGNOLI

  • Initial value: 0xFFFFFFFF

  • Polynomial: 0x1EDC6F41

  • Output XOR: 0xFFFFFFFF

  • Residue: 0xB798B438

  • Check: 0xE3069283

>>> assert CRC32C().value == 0
>>> c = CRC32C()
>>> c.add(b'123456')
>>> c.add(b'789')
>>> c.value  # 0xE3069283
3808858755
>>> c.add(b'')
>>> c.value
3808858755
>>> c.add(c.value_as_bytes)
>>> c.value  # Inverted residue
1214729159
>>> c.check_residue()
True
>>> CRC32C.new(b'123', b'', b'456789').value
3808858755
__init__() None[source]
add(data: bytes | bytearray | memoryview) None[source]
check_residue() bool[source]
property value: int[source]
property value_as_bytes: bytes[source]
class pycyphal.transport.commons.crc.CRC64WE[source]

Bases: CRCAlgorithm

A CRC-64/WE algorithm implementation.

  • Name: CRC-64/WE

  • Initial value: 0xFFFFFFFFFFFFFFFF

  • Polynomial: 0x42F0E1EBA9EA3693

  • Output XOR: 0xFFFFFFFFFFFFFFFF

  • Residue: 0xFCACBEBD5931A992

  • Check: 0x62EC59E3F1A4F00A

>>> assert CRC64WE().value == 0
>>> c = CRC64WE()
>>> c.add(b'123456')
>>> c.add(b'789')
>>> c.value  # 0x62EC59E3F1A4F00A
7128171145767219210
>>> c.add(b'')
>>> c.value
7128171145767219210
>>> c.add(c.value_as_bytes)
>>> c.value  # Inverted residue
239606959702955629
>>> c.check_residue()
True
>>> CRC64WE.new(b'123', b'', b'456789').value
7128171145767219210
__init__() None[source]
add(data: bytes | bytearray | memoryview) None[source]
check_residue() bool[source]
property value: int[source]
property value_as_bytes: bytes[source]