-
Notifications
You must be signed in to change notification settings - Fork 8
/
poly.go
314 lines (279 loc) · 7.47 KB
/
poly.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
conflux - Distributed database synchronization library
Based on the algorithm described in
"Set Reconciliation with Nearly Optimal Communication Complexity",
Yaron Minsky, Ari Trachtenberg, and Richard Zippel, 2004.
Copyright (c) 2012-2015 Casey Marshall <cmars@cmarstech.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package conflux
import (
"bytes"
"fmt"
"math/big"
"gopkg.in/errgo.v1"
)
// Poly represents a polynomial in a finite field.
type Poly struct {
// coeff is a list of the polynomial coefficients, ordered by ascending
// degree.
coeff []*Zp
// degree is the highest degree of the polynomial.
degree int
// p defines the finite field of all coefficients.
p *big.Int
}
// NewPoly creates a polynomial with the given coefficients, in ascending
// degree order.
//
// For example, NewPoly(1,-2,3) represents the polynomial 3x^2 - 2x + 1.
func NewPoly(coeff ...*Zp) *Poly {
p := &Poly{}
for i := 0; i < len(coeff); i++ {
if coeff[i] == nil {
if p.p == nil {
continue
} else {
coeff[i] = Z(p.p)
}
}
c := coeff[i].Copy().Norm()
p.coeff = append(p.coeff, c)
if !c.IsZero() {
p.degree = i
}
// All coefficients must be in same field
if p.p == nil {
p.p = c.P
// Initialize prior nils now that we know P
for j := 0; j <= i; j++ {
if p.coeff == nil {
p.coeff[j] = Z(p.p)
}
}
} else {
c.assertP(p.p)
}
}
return p
}
// String represents a polynomial in a more readable form,
// such as "z^2 + 2z^1 + 1".
func (p *Poly) String() string {
result := bytes.NewBuffer(nil)
first := true
for i := len(p.coeff) - 1; i >= 0; i-- {
c := p.coeff[i]
if c.IsZero() {
continue
}
if first {
first = false
} else {
fmt.Fprintf(result, " + ")
}
fmt.Fprintf(result, "%v", c.String())
if i > 0 {
fmt.Fprintf(result, "z^%d", i)
}
}
return string(result.Bytes())
}
// Degree returns the highest exponent that appears in the polynomial.
// For example, the degree of (x^2 + 1) is 2, the degree of (x^1) is 1.
func (p *Poly) Degree() int {
return p.degree
}
// Coeff returns the coefficients for each term of the polynomial. Coefficients
// are represented as integers in a finite field Zp.
func (p *Poly) Coeff() []*Zp {
return p.coeff
}
// P returns the integer P defining the finite field of the polynomial's
// coefficients.
func (p *Poly) P() *big.Int {
return p.p
}
// Copy returns a deep copy of the polynomial and its term coefficients.
func (p *Poly) Copy() *Poly {
newP := &Poly{degree: p.degree, p: p.p}
for i := 0; i <= p.degree; i++ {
newP.coeff = append(newP.coeff, p.coeff[i].Copy())
}
return newP
}
// assertP asserts that the polynomial's integer coefficients are in the finite
// field Z(fp).
func (p *Poly) assertP(fp *big.Int) {
if p.p.Cmp(fp) != 0 {
panic(fmt.Sprintf("expected finite field Z(%v), was Z(%v)", fp, p.p))
}
}
// Equal compares with another polynomial for equality.
func (p *Poly) Equal(q *Poly) bool {
p.assertP(q.p)
if p.degree != q.degree {
return false
}
for i := 0; i <= p.degree; i++ {
if (p.coeff[i] == nil) != (q.coeff[i] == nil) {
return false
}
if p.coeff[i] != nil && p.coeff[i].Cmp(q.coeff[i]) != 0 {
return false
}
}
return true
}
// Add sets the Poly instance to the sum of two Polys, returning the result.
func (p *Poly) Add(x, y *Poly) *Poly {
x.assertP(y.p)
p.p = x.p
p.degree = x.degree
if y.degree > p.degree {
p.degree = y.degree
}
p.coeff = make([]*Zp, p.degree+1)
for i := 0; i <= p.degree; i++ {
p.coeff[i] = Z(x.p)
if i <= x.degree && x.coeff[i] != nil {
p.coeff[i].Add(p.coeff[i], x.coeff[i])
}
if i <= y.degree && y.coeff[i] != nil {
p.coeff[i].Add(p.coeff[i], y.coeff[i])
}
}
p.trim()
return p
}
func (p *Poly) trim() {
for p.degree > 0 && p.coeff[p.degree].IsZero() {
p.degree--
}
}
// Neg negates the Poly, returning the result.
func (p *Poly) Neg() *Poly {
for i := 0; i <= p.degree; i++ {
p.coeff[i].Neg()
}
return p
}
// Sub sets the Poly to the difference of two Polys, returning the result.
func (p *Poly) Sub(x, y *Poly) *Poly {
return p.Add(x, y.Copy().Neg())
}
// Mul sets the Poly to the product of two Polys, returning the result.
func (p *Poly) Mul(x, y *Poly) *Poly {
x.assertP(y.p)
p.p = x.p
p.coeff = make([]*Zp, x.degree+y.degree+1)
p.degree = x.degree + y.degree
for i := 0; i <= x.degree; i++ {
for j := 0; j <= y.degree; j++ {
zp := p.coeff[i+j]
if zp == nil {
zp = Z(p.p)
p.coeff[i+j] = zp
}
zp.Add(zp, Z(p.p).Mul(x.coeff[i], y.coeff[j]))
}
}
p.trim()
return p
}
// IsConstant returns whether the Poly is just a constant value.
func (p *Poly) IsConstant(c *Zp) bool {
return p.degree == 0 && p.coeff[0].Cmp(c) == 0
}
// Eval returns the output value of the Poly at the given sample point z.
func (p *Poly) Eval(z *Zp) *Zp {
sum := Zi(p.p, 0)
for d := 0; d <= p.degree; d++ {
sum.Add(sum.Copy(), Z(p.p).Mul(p.coeff[d], Z(p.p).Exp(z, Zi(p.p, d))))
}
return sum
}
// PolyTerm creates a new Poly with a single non-zero coefficient.
func PolyTerm(degree int, c *Zp) *Poly {
p := &Poly{p: c.P, degree: degree,
coeff: make([]*Zp, degree+1)}
for i := 0; i <= degree; i++ {
if i == degree {
p.coeff[i] = c.Copy()
} else {
p.coeff[i] = Z(p.p)
}
}
return p
}
// PolyDivmod returns the quotient and remainder between two Polys.
func PolyDivmod(x, y *Poly) (q *Poly, r *Poly, err error) {
x.assertP(y.p)
if x.IsConstant(Zi(x.p, 0)) {
return NewPoly(Z(x.p)), NewPoly(Z(y.p)), nil
} else if y.degree > x.degree {
return NewPoly(Z(x.p)), x, nil
}
degDiff := x.degree - y.degree
if degDiff < 0 {
return nil, nil, errgo.Newf("quotient degree %d < dividend %d", x.degree, y.degree)
}
c := Z(x.p).Div(x.coeff[x.degree], y.coeff[y.degree])
m := PolyTerm(degDiff, c)
my := NewPoly().Mul(m, y)
newX := NewPoly().Sub(x, my)
if newX.degree < x.degree || x.degree == 0 {
// TODO: eliminate recursion
q, r, err := PolyDivmod(newX, y)
if err != nil {
return nil, nil, errgo.Mask(err)
}
q = NewPoly().Add(q, m)
return q, r, nil
}
return nil, nil, errgo.New("divmod error")
}
// PolyDiv returns the quotient between two Polys.
func PolyDiv(x, y *Poly) (*Poly, error) {
q, _, err := PolyDivmod(x, y)
return q, err
}
// PolyDiv returns the mod function between two Polys.
func PolyMod(x, y *Poly) (*Poly, error) {
_, r, err := PolyDivmod(x, y)
return r, err
}
func polyGcd(x, y *Poly) (*Poly, error) {
if y.IsConstant(Zi(x.p, 0)) {
return x, nil
}
_, r, err := PolyDivmod(x, y)
if err != nil {
return nil, errgo.Mask(err)
}
return polyGcd(y, r)
}
// PolyGcd returns the greatest common divisor between two Polys.
func PolyGcd(x, y *Poly) (*Poly, error) {
result, err := polyGcd(x, y)
if err != nil {
return nil, errgo.Mask(err)
}
result = NewPoly().Mul(result,
NewPoly(result.coeff[result.degree].Copy().Inv()))
return result, nil
}
// RationalFn describes a function that is the ratio between two polynomials.
type RationalFn struct {
Num *Poly
Denom *Poly
}