-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgf2n_field.go
48 lines (41 loc) · 1.04 KB
/
gf2n_field.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package gf2n
import (
"errors"
)
// arithmetic over GF(2^n)
// n must be less than 64
// x^n + irre must be a irreducible polynomial over GF(2^n)
// where irre is a polynomial over GF(2) of degree less than n
type GF2nField struct {
n uint8
irre uint64
}
// generate a binary extension field
// we do not check whether x^n + irre is irreducible
func NewGF2nField(n uint8, irre uint64) (*GF2nField, error) {
if n >= 64 || irre > (1<<n) {
return nil, errors.New("invalid parameters.")
}
return &GF2nField{n, irre}, nil
}
// return the degree of extension
func (f *GF2nField) ExtDegree() uint8 {
return f.n
}
// return irreducible polynomial wo/ the highest order term
func (f *GF2nField) IrrePolyWithoutHighest() uint64 {
return f.irre
}
func (f *GF2nField) Equal(e *GF2nField) bool {
return f.n == e.n && f.irre == e.irre
}
// return an integer which is less than 2^n
func (f *GF2nField) normalize(v uint64) uint64 {
var big uint64 = (1 << f.n) - 1
var t uint64 = v >> f.n
for t > 0 {
v = (v & big) ^ (t * f.irre)
t = v >> f.n
}
return v
}