-
Notifications
You must be signed in to change notification settings - Fork 1
/
conway.go
68 lines (60 loc) · 2.02 KB
/
conway.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Package conway contains a database of Conway polynomials. The list of
// polynomials was compiled by Frank Lübeck:
// http://www.math.rwth-aachen.de/~Frank.Luebeck/data/ConwayPol/index.html?LANG=en
package conway
import (
"fmt"
"github.com/ReneBoedker/algobra/errors"
"regexp"
"strconv"
"strings"
)
// The list of conway polynomials is defined as a constant in cpimport.go
// Lookup returns the coefficients for a Conway polynomial for the finite field
// of characteristic char and extension degree extDeg. The element at position i
// is the coefficient of X^i.
//
// If no such polynomial is in the database, an InputValue-error is returned.
func Lookup(char, extDeg uint) (coefs []uint, err error) {
return lookupInternal(char, extDeg, cpimport)
}
// lookupInternal has an additional input parameter which allows testing
func lookupInternal(char, extDeg uint, conwayList string) (coefs []uint, err error) {
const op = "Searching for Conway polynomial"
pattern, err := regexp.Compile(fmt.Sprintf(`\[%d,%d,\[([^]]*)\]\]`, char, extDeg))
if err != nil {
return nil, errors.New(
op, errors.InputValue,
"Failed to construct search pattern for characteristic %d and "+
"extension degree %d", char, extDeg,
)
}
match := pattern.FindStringSubmatch(conwayList)
if match == nil {
return nil, errors.New(
op, errors.InputValue,
"No polynomial was found for characteristic %d and extension degree %d",
char, extDeg,
)
}
coefStr := strings.Split(match[1], ",")
if tmp := uint(len(coefStr)); tmp != extDeg+1 {
return nil, errors.New(
op, errors.Internal,
"Polynomial in database has degree %d rather than %d", tmp, extDeg+1,
)
}
coefs = make([]uint, len(coefStr), len(coefStr))
for i, c := range coefStr {
tmp, err := strconv.ParseUint(c, 10, 0)
if err != nil {
return nil, errors.New(
op, errors.Internal,
"Could not parse coefficient %s of database polynomial for "+
"characteristic %d and extension degree %d", c, char, extDeg,
)
}
coefs[i] = uint(tmp)
}
return coefs, nil
}