---
name: "gf256"
section: "3"
platform: "linux"
package: "ecc"
version: "1.35-1"
release: "Debian 1.1 (Buzz)"
platforms: ["linux"]
url: "https://new.carta.tech/gf256"
---

# gf256(3)

Routines to do arithmetic in the Galois Field GF(256).

delim $$

## NAME

gf256\_log\_2, gf256\_antilog\_2, gf256\_power\_of\_2, gf256\_add, gf256\_minus, gf256\_subtract, gf256\_mul, gf256\_inv, gf256\_div, gf256\_product, gf256\_exp, gf256\_term, gfmul, gfadd, gfinv, gfexp - Routines to do arithmetic in the Galois Field GF(256).

## SYNOPSIS

```

```

**#include \<gf256.h\>**

**cc ... -lecc**

**GF256** *x***;**

*r***= gf256\_log\_2 (***x***)** **GF256** *r***,** *x***;**

**GF256 gf256\_antilog\_2 (***x***)** **GF256** *x***;**

**GF256 gf256\_power\_of\_2 (***n***)** **GF256** *n***;**

**GF256 gf255\_add (***a***,** *b***)** **GF256** *a***,** *b***;**

*r***= gf256\_minus (***a***)** **GF256** *r***,** *a***;**

**GF256 gf256\_subtract (***a***,** *b***)** **GF256** *a***,** *b***;**

**GF256 gf256\_mul (***x***,** *y***)** **GF256** *x***,** *y***;**

*r***= gf256\_inv (***x***)** **GF256** *r***,** *x***;**

**GF256 gf256\_div (***x***,** *y***)** **GF256** *x***,** *y***;**

**GF256 gf256\_product (***x***,** *y***,** *z***)** **GF256** *x***,** *y***,** *z***;**

**GF256 gf256\_exp (***a***,** *n***)** **GF256** *a***;** **int** *n***;**

**GF256 gf256\_term (***c***,** *i***,** *n***)** **GF256** *c***;** **int** *i***,** *n***;**

**unsigned char gfmul (***x***,** *y***)** **unsigned char** *x***,** *y***;**

**unsigned char gfadd (***a***,** *b***)** **unsigned char** *a***,** *b***;**

*r***= gfinv (***x***)** **unsigned char** *r***,** *x***;**

**unsigned char gfexp (***a***,** *n***)** **unsigned char** *a***,** *n***;**

## DESCRIPTION

This manual page documents the GNU ecc library - routines to do arithmetic in the Galois Field GF($2 sup 8$).

The representation of GF($2 sup 8$) used in this library is isomorphic to $GF(2)\[X\] ~ modulo ~ P (x)$ where

P (x) = x sup 8 + x sup 4 + x sup 3 + x sup 2 + 1

which is a primitive polynomial.  The polynomials chosen are

f sub 1 ( x ) = 1\
 f sub 2 ( x ) = x cdot f sub 1 ( x ) ~ modulo ~ P ( x )\
 f sub 3 ( x ) = x cdot f sub 2 ( x ) ~ modulo ~ P ( x )\
\
 f sub n ( x ) = x cdot f sub {n-1} ( x ) ~ modulo ~ P ( x )

where the modulo is done in $GF(2)\[X\]$.

A Discrete Antilogrithm table is formed by evaluating these polynomials at $x = 2$ which is a cyclic element of GF($2 sup 8$).  This representation can be found in many textbooks; see, for instance, *\`\`Theory and Practice of Error Control Codes''* by Richard Blahut.

**gf256\_log\_2()** returns the discrete logarithm of *x* such that $2 sup r = x$ for non-zero value of $x$.

**gf256\_antilog\_2()** returns the discrete antilogrithm of *x* such that\

**gf256\_antilog\_2 (gf256\_log\_2 (***a***)) $==$** *a*\
 **gf256\_log\_2 (gf256\_antilog\_2 (***a***)) $==$** *a*

for all non-zero $a$ in GF($2 sup 8$).

**gf256\_power\_of\_2()** returns the $2 sup n$.  The range of *n* is restricted to **GF256**.

**gf256\_add()** and **gfadd()** return the sum of their arguments.

**gf256\_minus()** returns the additive inverse of its argument.  That is, $a + r = 0$.

**gf256\_subtract()** returns $a - b$.

**gf256\_mul()** and **gfmul()** return the product of their arguments.

**gf256\_inv()** and **gfinv()** return the multiplicative inverse of their argument, which must have non-zero value.  That is, $x cdot r = 1$.

**gf256\_div()** returns $x / y$ for non-zero value of $y$.

**gf256\_product()** returns the products of the 3 arguments.

**gf256\_exp()** and **gfexp()** return $a sup n$.

**gf256\_term()** returns the value of the $x sup n$ term which has coefficient $c$ in the evaluation of a polynomial at $x = 2 sup i$.  In short, it returns $c cdot 2 sup {i cdot n}$.

## CAVEATS

Note that most of the functions are C-preprocessor macros.  Please, do not have side-effects in the arguments.  Avoid functional compositions, instead use temporary variables.  For instance, instead of

y = gf256\_add (gf256\_mul (s2, s4), gf256\_mul (s3, s3));

write

y = gf256\_mul (s2, s4);\
 p = gf256\_mul (s3, s3);\
 y = gf256\_add (y, p);

The values of **gf256\_log\_2()**, **gf256\_inv()**, and **gf256\_div()** are undefined for invalid argument values; programs may continue to run, but the results may be wrong.

## WARNING

The 4 functions **gfmul()**, **gfadd()**, **gfinv()**, and **gfexp()** are included in the library for backward-compatibility to earlier versions of this library.  They may be removed in future releases.

## SEE ALSO

[rsff-f9(3)](/rsff-f9/3.md)
