-
Notifications
You must be signed in to change notification settings - Fork 8
/
poly_test.go
136 lines (120 loc) · 4.3 KB
/
poly_test.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
/*
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 <[email protected]>
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 (
"math/big"
gc "gopkg.in/check.v1"
)
type PolySuite struct{}
var _ = gc.Suite(&PolySuite{})
func (s *PolySuite) TestPolyDegree(c *gc.C) {
p := big.NewInt(int64(65537))
poly := NewPoly(Zi(p, 4), Zi(p, 3), Zi(p, 2))
c.Assert(2, gc.Equals, poly.Degree())
poly = NewPoly(nil, nil, nil, nil, nil, nil, nil, nil)
c.Assert(0, gc.Equals, poly.Degree())
poly = NewPoly(nil, nil, nil, nil, nil, nil, nil, Zi(p, 1))
c.Assert(7, gc.Equals, poly.Degree())
poly = NewPoly(nil, nil, nil, nil, nil, nil, nil, Zi(p, 1), nil, nil)
c.Assert(7, gc.Equals, poly.Degree())
}
func (s *PolySuite) TestPolyFmt(c *gc.C) {
p := big.NewInt(int64(65537))
poly := NewPoly(Zi(p, 4), Zi(p, 3), Zi(p, 2))
c.Assert("2z^2 + 3z^1 + 4", gc.Equals, poly.String())
}
func (s *PolySuite) TestPolyEval(c *gc.C) {
var poly *Poly
var z *Zp
p := big.NewInt(int64(97))
// Constant
poly = NewPoly(Zi(p, 5))
z = poly.Eval(Zi(p, 8))
c.Assert(int64(5), gc.Equals, z.Int64())
// Linear
poly = NewPoly(Zi(p, 5), Zi(p, 3))
z = poly.Eval(Zi(p, 8))
c.Assert(int64(29), gc.Equals, z.Int64())
// Quadratic
poly = NewPoly(Zi(p, 5), Zi(p, 3), Zi(p, 2))
z = poly.Eval(Zi(p, 8))
c.Assert(Zi(p, 157).Int64(), gc.Equals, z.Int64())
}
func (s *PolySuite) TestPolyMul(c *gc.C) {
p := big.NewInt(int64(97))
x := NewPoly(Zi(p, -6), Zi(p, 11), Zi(p, -6), Zi(p, 1))
y := NewPoly(Zi(p, 2), Zi(p, 1))
z := NewPoly().Mul(x, y)
c.Assert(5, gc.Equals, len(z.coeff))
c.Logf("z=%v", z)
for i, v := range []int{85, 16, 96, 93, 1} {
c.Assert(Zi(p, v).String(), gc.Equals, z.coeff[i].String())
}
}
func (s *PolySuite) TestPolyAdd(c *gc.C) {
p := big.NewInt(int64(97))
// (x+1) + (x+2) = (2x+3)
x := NewPoly(Zi(p, 1), Zi(p, 1))
y := NewPoly(Zi(p, 2), Zi(p, 1))
z := NewPoly().Add(x, y)
c.Assert(1, gc.Equals, z.degree)
c.Assert(int64(3), gc.Equals, z.coeff[0].Int64())
c.Assert(int64(2), gc.Equals, z.coeff[1].Int64())
// (2x+3) - (x+2) = (x+1)
x = NewPoly(Zi(p, 3), Zi(p, 2))
y = NewPoly(Zi(p, 2), Zi(p, 1))
z = NewPoly().Sub(x, y)
c.Assert(1, gc.Equals, z.degree)
c.Assert(int64(1), gc.Equals, z.coeff[0].Int64())
c.Assert(int64(1), gc.Equals, z.coeff[1].Int64())
// (x+1) - (x^2+2x+1) = (-x^2 - x)
x = NewPoly(Zi(p, 1), Zi(p, 1))
y = NewPoly(Zi(p, 1), Zi(p, 2), Zi(p, 1))
z = NewPoly().Sub(x, y)
c.Assert(2, gc.Equals, z.degree)
c.Assert(int64(0), gc.Equals, z.coeff[0].Int64())
c.Assert(Zi(p, -1).Int64(), gc.Equals, z.coeff[1].Int64())
c.Assert(Zi(p, -1).Int64(), gc.Equals, z.coeff[2].Int64())
}
func (s *PolySuite) TestPolyDivmod(c *gc.C) {
// (x^2 + 2x + 1) / (x + 1) = (x + 1)
p := big.NewInt(int64(97))
x := NewPoly(Zi(p, 1), Zi(p, 2), Zi(p, 1))
y := NewPoly(Zi(p, 1), Zi(p, 1))
q, r, err := PolyDivmod(x, y)
c.Logf("q=(%v) r=(%v) err=(%v)", q, r, err)
c.Assert(1, gc.Equals, q.degree)
c.Assert(int64(1), gc.Equals, q.coeff[0].Int64())
c.Assert(int64(1), gc.Equals, q.coeff[1].Int64())
c.Assert(2, gc.Equals, len(q.coeff))
c.Assert(err, gc.IsNil, gc.Commentf("%v", err))
c.Assert(0, gc.Equals, r.degree)
c.Assert(err, gc.IsNil)
}
func (s *PolySuite) TestGcd(c *gc.C) {
p := big.NewInt(int64(97))
x := NewPoly(Zi(p, 1), Zi(p, 2), Zi(p, 1))
y := NewPoly(Zi(p, 1), Zi(p, 1))
r, err := PolyGcd(x, y)
c.Assert(err, gc.IsNil)
c.Logf("r=(%v)", r)
c.Assert(1, gc.Equals, r.degree)
c.Assert(int64(1), gc.Equals, r.coeff[0].Int64())
c.Assert(int64(1), gc.Equals, r.coeff[1].Int64())
c.Assert(2, gc.Equals, len(r.coeff))
}