~/bend-docscommunity

gf.bend checks

raw source on the hub · import bend-osdev-gf@0.1.1.0/gf.bend as Gf

bend-osdev-gf: arithmetic in the finite field GF(2^8), with the reduction polynomial as a parameter.

GF(2^8) is GF(2)[x] modulo a polynomial of degree 8. An element is a byte: bit i is the coefficient of x^i. Addition is XOR; multiplication is the product of the polynomials, reduced modulo the field's polynomial (FIPS 197, section 4; any algebra text). A Gf8Field names the polynomial (all nine bits, so 0x11B is 283) and a generator of the multiplicative group, used for the exponential and logarithm.

Elements are held in U32; only the low 8 bits of an argument are read, and every result is below 256. Inside, a byte is its eight bits (Gf8Bits), and the product is computed by Horner's rule over the bits of the first factor: acc := x * acc + bit * b, from the top bit down. Computing on bits keeps the code short and branch-free, and lets the laws be proved for every polynomial (see LAWS.bend).

1 import
import Base

Types

type Gf8Field source · line 19 · raw

Data

type Gf8Bits source · line 47 · raw

Data

Definitions

def Gf8.aes source · line 25 · raw

Gf8Field

AES (FIPS 197): x^8 + x^4 + x^3 + x + 1 (0x11B), generator x + 1 (3)

def Gf8.rs source · line 29 · raw

Gf8Field

Reed-Solomon and RAID-6: x^8 + x^4 + x^3 + x^2 + 1 (0x11D), generator x (2)

def Gf8.raid6 source · line 32 · raw

Gf8Field

def Gf8.poly source · line 35 · raw

@f:Gf8Field -> U32

def Gf8.gen source · line 40 · raw

@f:Gf8Field -> U32

def Gf8Bits.of source · line 51 · raw

@a:U32 -> Gf8Bits

the low 8 bits of a

def Gf8Bits.to source · line 56 · raw

@r:Gf8Bits -> U32

def Gf8Bits.zero source · line 61 · raw

Gf8Bits

def Gf8Bits.xor source · line 64 · raw

@a:Gf8Bits -> @b:Gf8Bits -> Gf8Bits

def Gf8Bits.sel source · line 70 · raw

@t:Bool -> @a:Gf8Bits -> Gf8Bits

a if t, else zero

def Gf8Bits.x source · line 79 · raw

@a:Gf8Bits -> @p:Gf8Bits -> Gf8Bits

a times x, where p holds the low 8 bits of the polynomial: shift up one place, and if the coefficient of x^8 was set, add p (x^8 = p in the field)

def Gf8Bits.step source · line 85 · raw

@t:Bool -> @acc:Gf8Bits -> @+b:Gf8Bits -> @+p:Gf8Bits -> Gf8Bits

one step of Horner's rule: x * acc + t * b

def Gf8Bits.horner source · line 89 · raw

@ts:List<&2, Bool> -> @acc:Gf8Bits -> @+b:Gf8Bits -> @+p:Gf8Bits -> Gf8Bits

Horner's rule over the bits ts, most significant first

def Gf8Bits.top source · line 97 · raw

@a:Gf8Bits -> List<&2, Bool>

the bits of a, most significant first

def Gf8Bits.mul source · line 102 · raw

@a:Gf8Bits -> @+b:Gf8Bits -> @+p:Gf8Bits -> Gf8Bits

def Gf8.elt source · line 108 · raw

@a:U32 -> U32

the element a: its low 8 bits

def Gf8Bits.is_zero source · line 112 · raw

@a:Gf8Bits -> Bool

whether a is the zero element (its low 8 bits are all 0)

def Gf8.is_zero source · line 117 · raw

@a:U32 -> Bool

def Gf8.add source · line 120 · raw

@a:U32 -> @b:U32 -> U32

def Gf8.sub source · line 124 · raw

@a:U32 -> @b:U32 -> U32

subtraction is addition in characteristic 2

def Gf8.mul source · line 127 · raw

@f:Gf8Field -> @a:U32 -> @b:U32 -> U32

def Gf8.xtime source · line 133 · raw

@f:Gf8Field -> @a:U32 -> U32

a times x (FIPS 197's xtime)

def Gf8.pow source · line 139 · raw

@+f:Gf8Field -> @+a:U32 -> @n:Nat -> U32

a^n, with a^0 = 1 (also for a = 0)

def Gf8.inv source · line 148 · raw

@+f:Gf8Field -> @+a:U32 -> U32

the inverse a^254 (a^255 = 1 for a nonzero); the inverse of 0 is 0, as in the AES S-box

def Gf8.div source · line 152 · raw

@+f:Gf8Field -> @a:U32 -> @+b:U32 -> U32

a / b = a * b^-1 (and a / 0 = 0)

def Gf8.exp source · line 158 · raw

@+f:Gf8Field -> @i:Nat -> U32

gen^i

def Gf8.log.go source · line 163 · raw

@n:Nat -> @hit:Bool -> @+f:Gf8Field -> @+a:U32 -> @+cur:U32 -> @+i:Nat -> Nat

the least i with gen^i = a (searching i = 0 to 254); 0 for a = 0, or when gen does not generate a

def Gf8.log source · line 173 · raw

@+f:Gf8Field -> @a:U32 -> Nat