1package utils
2
3type GFPoly struct {
4	gf           *GaloisField
5	Coefficients []int
6}
7
8func (gp *GFPoly) Degree() int {
9	return len(gp.Coefficients) - 1
10}
11
12func (gp *GFPoly) Zero() bool {
13	return gp.Coefficients[0] == 0
14}
15
16// GetCoefficient returns the coefficient of x ^ degree
17func (gp *GFPoly) GetCoefficient(degree int) int {
18	return gp.Coefficients[gp.Degree()-degree]
19}
20
21func (gp *GFPoly) AddOrSubstract(other *GFPoly) *GFPoly {
22	if gp.Zero() {
23		return other
24	} else if other.Zero() {
25		return gp
26	}
27	smallCoeff := gp.Coefficients
28	largeCoeff := other.Coefficients
29	if len(smallCoeff) > len(largeCoeff) {
30		largeCoeff, smallCoeff = smallCoeff, largeCoeff
31	}
32	sumDiff := make([]int, len(largeCoeff))
33	lenDiff := len(largeCoeff) - len(smallCoeff)
34	copy(sumDiff, largeCoeff[:lenDiff])
35	for i := lenDiff; i < len(largeCoeff); i++ {
36		sumDiff[i] = int(gp.gf.AddOrSub(int(smallCoeff[i-lenDiff]), int(largeCoeff[i])))
37	}
38	return NewGFPoly(gp.gf, sumDiff)
39}
40
41func (gp *GFPoly) MultByMonominal(degree int, coeff int) *GFPoly {
42	if coeff == 0 {
43		return gp.gf.Zero()
44	}
45	size := len(gp.Coefficients)
46	result := make([]int, size+degree)
47	for i := 0; i < size; i++ {
48		result[i] = int(gp.gf.Multiply(int(gp.Coefficients[i]), int(coeff)))
49	}
50	return NewGFPoly(gp.gf, result)
51}
52
53func (gp *GFPoly) Multiply(other *GFPoly) *GFPoly {
54	if gp.Zero() || other.Zero() {
55		return gp.gf.Zero()
56	}
57	aCoeff := gp.Coefficients
58	aLen := len(aCoeff)
59	bCoeff := other.Coefficients
60	bLen := len(bCoeff)
61	product := make([]int, aLen+bLen-1)
62	for i := 0; i < aLen; i++ {
63		ac := int(aCoeff[i])
64		for j := 0; j < bLen; j++ {
65			bc := int(bCoeff[j])
66			product[i+j] = int(gp.gf.AddOrSub(int(product[i+j]), gp.gf.Multiply(ac, bc)))
67		}
68	}
69	return NewGFPoly(gp.gf, product)
70}
71
72func (gp *GFPoly) Divide(other *GFPoly) (quotient *GFPoly, remainder *GFPoly) {
73	quotient = gp.gf.Zero()
74	remainder = gp
75	fld := gp.gf
76	denomLeadTerm := other.GetCoefficient(other.Degree())
77	inversDenomLeadTerm := fld.Invers(int(denomLeadTerm))
78	for remainder.Degree() >= other.Degree() && !remainder.Zero() {
79		degreeDiff := remainder.Degree() - other.Degree()
80		scale := int(fld.Multiply(int(remainder.GetCoefficient(remainder.Degree())), inversDenomLeadTerm))
81		term := other.MultByMonominal(degreeDiff, scale)
82		itQuot := NewMonominalPoly(fld, degreeDiff, scale)
83		quotient = quotient.AddOrSubstract(itQuot)
84		remainder = remainder.AddOrSubstract(term)
85	}
86	return
87}
88
89func NewMonominalPoly(field *GaloisField, degree int, coeff int) *GFPoly {
90	if coeff == 0 {
91		return field.Zero()
92	}
93	result := make([]int, degree+1)
94	result[0] = coeff
95	return NewGFPoly(field, result)
96}
97
98func NewGFPoly(field *GaloisField, coefficients []int) *GFPoly {
99	for len(coefficients) > 1 && coefficients[0] == 0 {
100		coefficients = coefficients[1:]
101	}
102	return &GFPoly{field, coefficients}
103}
104