-
Notifications
You must be signed in to change notification settings - Fork 163
/
polynomial.go
220 lines (181 loc) · 4.45 KB
/
polynomial.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
// Copyright 2020 Consensys Software Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by consensys/gnark-crypto DO NOT EDIT
package polynomial
import (
"github.com/consensys/gnark-crypto/ecc/bls12-378/fr"
"github.com/consensys/gnark-crypto/utils"
"strconv"
"strings"
)
// Polynomial represented by coefficients in the field.
type Polynomial []fr.Element
// Degree returns the degree of the polynomial, which is the length of Data.
func (p *Polynomial) Degree() uint64 {
return uint64(len(*p) - 1)
}
// Eval evaluates p at v
// returns a fr.Element
func (p *Polynomial) Eval(v *fr.Element) fr.Element {
res := (*p)[len(*p)-1]
for i := len(*p) - 2; i >= 0; i-- {
res.Mul(&res, v)
res.Add(&res, &(*p)[i])
}
return res
}
// Clone returns a copy of the polynomial
func (p *Polynomial) Clone() Polynomial {
_p := make(Polynomial, len(*p))
copy(_p, *p)
return _p
}
// Set to another polynomial
func (p *Polynomial) Set(p1 Polynomial) {
if len(*p) != len(p1) {
*p = p1.Clone()
return
}
for i := 0; i < len(p1); i++ {
(*p)[i].Set(&p1[i])
}
}
// AddConstantInPlace adds a constant to the polynomial, modifying p
func (p *Polynomial) AddConstantInPlace(c *fr.Element) {
for i := 0; i < len(*p); i++ {
(*p)[i].Add(&(*p)[i], c)
}
}
// SubConstantInPlace subs a constant to the polynomial, modifying p
func (p *Polynomial) SubConstantInPlace(c *fr.Element) {
for i := 0; i < len(*p); i++ {
(*p)[i].Sub(&(*p)[i], c)
}
}
// ScaleInPlace multiplies p by v, modifying p
func (p *Polynomial) ScaleInPlace(c *fr.Element) {
for i := 0; i < len(*p); i++ {
(*p)[i].Mul(&(*p)[i], c)
}
}
// Scale multiplies p0 by v, storing the result in p
func (p *Polynomial) Scale(c *fr.Element, p0 Polynomial) {
if len(*p) != len(p0) {
*p = make(Polynomial, len(p0))
}
for i := 0; i < len(p0); i++ {
(*p)[i].Mul(c, &p0[i])
}
}
// Add adds p1 to p2
// This function allocates a new slice unless p == p1 or p == p2
func (p *Polynomial) Add(p1, p2 Polynomial) *Polynomial {
bigger := p1
smaller := p2
if len(bigger) < len(smaller) {
bigger, smaller = smaller, bigger
}
if len(*p) == len(bigger) && (&(*p)[0] == &bigger[0]) {
for i := 0; i < len(smaller); i++ {
(*p)[i].Add(&(*p)[i], &smaller[i])
}
return p
}
if len(*p) == len(smaller) && (&(*p)[0] == &smaller[0]) {
for i := 0; i < len(smaller); i++ {
(*p)[i].Add(&(*p)[i], &bigger[i])
}
*p = append(*p, bigger[len(smaller):]...)
return p
}
res := make(Polynomial, len(bigger))
copy(res, bigger)
for i := 0; i < len(smaller); i++ {
res[i].Add(&res[i], &smaller[i])
}
*p = res
return p
}
// Sub subtracts p2 from p1
// TODO make interface more consistent with Add
func (p *Polynomial) Sub(p1, p2 Polynomial) *Polynomial {
if len(p1) != len(p2) || len(p2) != len(*p) {
return nil
}
for i := 0; i < len(*p); i++ {
(*p)[i].Sub(&p1[i], &p2[i])
}
return p
}
// Equal checks equality between two polynomials
func (p *Polynomial) Equal(p1 Polynomial) bool {
if (*p == nil) != (p1 == nil) {
return false
}
if len(*p) != len(p1) {
return false
}
for i := range p1 {
if !(*p)[i].Equal(&p1[i]) {
return false
}
}
return true
}
func (p Polynomial) SetZero() {
for i := 0; i < len(p); i++ {
p[i].SetZero()
}
}
func (p Polynomial) Text(base int) string {
var builder strings.Builder
first := true
for d := len(p) - 1; d >= 0; d-- {
if p[d].IsZero() {
continue
}
pD := p[d]
pDText := pD.Text(base)
initialLen := builder.Len()
if pDText[0] == '-' {
pDText = pDText[1:]
if first {
builder.WriteString("-")
} else {
builder.WriteString(" - ")
}
} else if !first {
builder.WriteString(" + ")
}
first = false
if !pD.IsOne() || d == 0 {
builder.WriteString(pDText)
}
if builder.Len()-initialLen > 10 {
builder.WriteString("×")
}
if d != 0 {
builder.WriteString("X")
}
if d > 1 {
builder.WriteString(
utils.ToSuperscript(strconv.Itoa(d)),
)
}
}
if first {
return "0"
}
return builder.String()
}