Welcome to GF256¶
GF256 is an implementation of GF(2 ** 8). A finite field commonly used in cryptographic algorithms.
Installation¶
You can install the library using pip:
pip install gf256
The library comes with a C extension (via CFFI) for better performance. You can set an environment variable GF256_WITHOUT_SPEEDUPS=1 to prevent it from being built.
Usage¶
The library provides a class GF256
which you can use to
represent elements of GF(2 ** 8).
>>> from gf256 import GF256
>>> GF256(1)
GF256(0b00000001)
You can perform arithmetic on GF256 objects like you can on other numbers.
>>> GF256(1) + GF256(2)
GF256(0b00000011)
>>> GF256(1) - GF256(2)
GF256(0b00000011)
>>> GF256(2) * GF256(3)
GF256(0b00000110)
>>> GF256(6) / GF256(3)
GF256(0b00000010)
In addition to the special methods required for arithmetic, GF256 provides special methods for coercion to integers and hashing:
>>> int(GF256(1))
1
>>> hash(GF256(2))
2
The latter allows you to use GF256 objects as keys in dictionaries or as elements of a set. Of course equality and inequality comparisons are also possible.
API Reference¶
-
gf256.
__version__
= 'major.minor.bugfix'¶ str(object=’‘) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to ‘strict’.
-
gf256.
__version_info__
= (major, minor, bugfix)¶ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable’s items
If the argument is a tuple, the return value is the same object.
-
class
gf256.
GF256
(n)¶ Represents an element in GF(2 ** 8).
You can do arithmetic using +, -, *, / and **. Additionally == and != operations are implemented. GF256 objects are hashable and can be used as keys. Use int() to turn an object into an integer.
-
to_polynomial_string
()¶ Returns a string representation of the polynomial:
>>> GF256(0b00011011).to_polynomial_string() 'x**4 + x**3 + x + 1'
-