-
Notifications
You must be signed in to change notification settings - Fork 21
/
FiniteFieldPolynomial.cs
183 lines (139 loc) · 6.79 KB
/
FiniteFieldPolynomial.cs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System.Numerics;
using Moserware.Numerics;
namespace Moserware.Algebra {
/// <summary>
/// Represents a polynomial modulo an irreducible polynomial (in a finite field).
/// </summary>
public class FiniteFieldPolynomial {
private readonly IrreduciblePolynomial _PrimePolynomial;
private readonly BigInteger _PolynomialSetCoefficients;
public FiniteFieldPolynomial(IrreduciblePolynomial primePolynomial, BigInteger polynomial) {
_PrimePolynomial = primePolynomial;
_PolynomialSetCoefficients = polynomial;
}
public FiniteFieldPolynomial(IrreduciblePolynomial primePolynomial, params int[] setCoefficients) {
_PrimePolynomial = primePolynomial;
_PolynomialSetCoefficients = BigInteger.Zero;
for (int i = 0; i < setCoefficients.Length; i++) {
_PolynomialSetCoefficients = _PolynomialSetCoefficients.SetBit(setCoefficients[i]);
}
}
public FiniteFieldPolynomial Clone() {
return new FiniteFieldPolynomial(_PrimePolynomial, _PolynomialSetCoefficients);
}
public BigInteger PolynomialValue {
get { return _PolynomialSetCoefficients; }
}
public IrreduciblePolynomial PrimePolynomial {
get { return _PrimePolynomial; }
}
public static FiniteFieldPolynomial EvaluateAt(long x, FiniteFieldPolynomial[] coefficients) {
// Use Horner's Scheme: http://en.wikipedia.org/wiki/Horner_scheme
FiniteFieldPolynomial xAsPoly = coefficients[0].GetValueInField(x);
// assume the coefficient for highest monomial is 1
FiniteFieldPolynomial result = xAsPoly.Clone();
for (int i = coefficients.Length - 1; i > 0; i--) {
result = result + coefficients[i];
result = result*xAsPoly;
}
result = result + coefficients[0];
return result;
}
public static FiniteFieldPolynomial operator +(FiniteFieldPolynomial left, FiniteFieldPolynomial right) {
BigInteger result = left._PolynomialSetCoefficients ^ right._PolynomialSetCoefficients;
return new FiniteFieldPolynomial(left._PrimePolynomial, result);
}
public static FiniteFieldPolynomial operator *(FiniteFieldPolynomial left, FiniteFieldPolynomial right) {
// Use a modified version of the "peasant's algorithm":
// http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication
// The invariant is that a * b + p must always equal the product. We keep
// doubling "a" and halving "b". If "b" is odd, then we add "a" to "p"
BigInteger p = BigInteger.Zero;
BigInteger a = left._PolynomialSetCoefficients;
BigInteger b = right._PolynomialSetCoefficients;
int degree = left._PrimePolynomial.Degree;
BigInteger mask = (BigInteger.One << degree) - BigInteger.One;
for (int i = 0; i < degree; i++) {
if ((a == BigInteger.Zero) || (b == BigInteger.Zero)) {
break;
}
if (b.TestBit(0)) {
// It's odd, add it
p = p ^ a;
}
bool highBitSet = a.TestBit(degree - 1);
// multiply a by "x"
a = a << 1;
a = a & mask;
if (highBitSet) {
a = a ^ left._PrimePolynomial.PolynomialValue;
a = a & mask;
}
b = b >> 1;
}
p = p & mask;
return new FiniteFieldPolynomial(left._PrimePolynomial, p);
}
public FiniteFieldPolynomial GetInverse() {
// We need to compute the inverse of the current polynomial
// modulo the irreducible polynomial. We'll do this with
// a simplified version of the Euclidean algorithm:
// http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Computing_a_multiplicative_inverse_in_a_finite_field
// Instead of allowing division by arbitrary polynomials such as
// x^4 + x^3 + 1, we'll always divide by monomials like x^4.
// This makes multiplication and division by using just shifts.
BigInteger r_minus2 = _PrimePolynomial.PolynomialValue;
BigInteger r_minus1 = _PolynomialSetCoefficients;
BigInteger a_minus2 = BigInteger.Zero;
BigInteger a_minus1 = BigInteger.One;
while (!r_minus1.Equals(BigInteger.One)) {
// How much do I need to shift by?
int shiftAmount = r_minus2.GetBitLength() - r_minus1.GetBitLength();
if (shiftAmount < 0) {
Swap(ref r_minus2, ref r_minus1);
Swap(ref a_minus2, ref a_minus1);
shiftAmount = -shiftAmount;
}
// Now r_minus2 should be as big or bigger than r_minus1
// q = BigInteger.One.ShiftLeft(shiftAmount)
BigInteger r_minus1TimesQ = r_minus1 << shiftAmount;
BigInteger r_new = r_minus1TimesQ ^ r_minus2;
BigInteger a_new = (a_minus1 << shiftAmount) ^ a_minus2;
r_minus2 = r_minus1;
r_minus1 = r_new;
a_minus2 = a_minus1;
a_minus1 = a_new;
}
return new FiniteFieldPolynomial(_PrimePolynomial, a_minus1);
}
public FiniteFieldPolynomial Zero {
get { return GetValueInField(0); }
}
public FiniteFieldPolynomial One {
get { return GetValueInField(1); }
}
private FiniteFieldPolynomial GetValueInField(long n) {
return new FiniteFieldPolynomial(_PrimePolynomial, new BigInteger(n));
}
public override string ToString() {
return _PolynomialSetCoefficients.ToPolynomialString();
}
private static void Swap(ref BigInteger a, ref BigInteger b) {
var temp = b;
b = a;
a = temp;
}
public override bool Equals(object obj) {
var other = obj as FiniteFieldPolynomial;
if (other == null) {
return base.Equals(obj);
}
return (PrimePolynomial == other.PrimePolynomial)
&&
(PolynomialValue.Equals(other.PolynomialValue));
}
public override int GetHashCode() {
return PrimePolynomial.GetHashCode() ^ PolynomialValue.GetHashCode();
}
}
}