-
Notifications
You must be signed in to change notification settings - Fork 153
/
multilin.go
291 lines (240 loc) · 8.78 KB
/
multilin.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
// 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"
"math/bits"
)
// MultiLin tracks the values of a (dense i.e. not sparse) multilinear polynomial
// The variables are X₁ through Xₙ where n = log(len(.))
// .[∑ᵢ 2ⁱ⁻¹ bₙ₋ᵢ] = the polynomial evaluated at (b₁, b₂, ..., bₙ)
// It is understood that any hypercube evaluation can be extrapolated to a multilinear polynomial
type MultiLin []fr.Element
// Fold is partial evaluation function k[X₁, X₂, ..., Xₙ] → k[X₂, ..., Xₙ] by setting X₁=r
func (m *MultiLin) Fold(r fr.Element) {
mid := len(*m) / 2
bottom, top := (*m)[:mid], (*m)[mid:]
var t fr.Element // no need to update the top part
// updating bookkeeping table
// knowing that the polynomial f ∈ (k[X₂, ..., Xₙ])[X₁] is linear, we would get f(r) = f(0) + r(f(1) - f(0))
// the following loop computes the evaluations of f(r) accordingly:
// f(r, b₂, ..., bₙ) = f(0, b₂, ..., bₙ) + r(f(1, b₂, ..., bₙ) - f(0, b₂, ..., bₙ))
for i := 0; i < mid; i++ {
// table[i] ← table[i] + r (table[i + mid] - table[i])
t.Sub(&top[i], &bottom[i])
t.Mul(&t, &r)
bottom[i].Add(&bottom[i], &t)
}
*m = (*m)[:mid]
}
func (m *MultiLin) FoldParallel(r fr.Element) utils.Task {
mid := len(*m) / 2
bottom, top := (*m)[:mid], (*m)[mid:]
*m = bottom
return func(start, end int) {
var t fr.Element // no need to update the top part
for i := start; i < end; i++ {
// table[i] ← table[i] + r (table[i + mid] - table[i])
t.Sub(&top[i], &bottom[i])
t.Mul(&t, &r)
bottom[i].Add(&bottom[i], &t)
}
}
}
func (m MultiLin) Sum() fr.Element {
s := m[0]
for i := 1; i < len(m); i++ {
s.Add(&s, &m[i])
}
return s
}
func _clone(m MultiLin, p *Pool) MultiLin {
if p == nil {
return m.Clone()
} else {
return p.Clone(m)
}
}
func _dump(m MultiLin, p *Pool) {
if p != nil {
p.Dump(m)
}
}
// Evaluate extrapolate the value of the multilinear polynomial corresponding to m
// on the given coordinates
func (m MultiLin) Evaluate(coordinates []fr.Element, p *Pool) fr.Element {
// Folding is a mutating operation
bkCopy := _clone(m, p)
// Evaluate step by step through repeated folding (i.e. evaluation at the first remaining variable)
for _, r := range coordinates {
bkCopy.Fold(r)
}
result := bkCopy[0]
_dump(bkCopy, p)
return result
}
// Clone creates a deep copy of a bookkeeping table.
// Both multilinear interpolation and sumcheck require folding an underlying
// array, but folding changes the array. To do both one requires a deep copy
// of the bookkeeping table.
func (m MultiLin) Clone() MultiLin {
res := make(MultiLin, len(m))
copy(res, m)
return res
}
// Add two bookKeepingTables
func (m *MultiLin) Add(left, right MultiLin) {
size := len(left)
// Check that left and right have the same size
if len(right) != size || len(*m) != size {
panic("left, right and destination must have the right size")
}
// Add elementwise
for i := 0; i < size; i++ {
(*m)[i].Add(&left[i], &right[i])
}
}
// EvalEq computes Eq(q₁, ... , qₙ, h₁, ... , hₙ) = Π₁ⁿ Eq(qᵢ, hᵢ)
// where Eq(x,y) = xy + (1-x)(1-y) = 1 - x - y + xy + xy interpolates
//
// _________________
// | | |
// | 0 | 1 |
// |_______|_______|
// y | | |
// | 1 | 0 |
// |_______|_______|
//
// x
//
// In other words the polynomial evaluated here is the multilinear extrapolation of
// one that evaluates to q' == h' for vectors q', h' of binary values
func EvalEq(q, h []fr.Element) fr.Element {
var res, nxt, one, sum fr.Element
one.SetOne()
for i := 0; i < len(q); i++ {
nxt.Mul(&q[i], &h[i]) // nxt <- qᵢ * hᵢ
nxt.Double(&nxt) // nxt <- 2 * qᵢ * hᵢ
nxt.Add(&nxt, &one) // nxt <- 1 + 2 * qᵢ * hᵢ
sum.Add(&q[i], &h[i]) // sum <- qᵢ + hᵢ TODO: Why not subtract one by one from nxt? More parallel?
if i == 0 {
res.Sub(&nxt, &sum) // nxt <- 1 + 2 * qᵢ * hᵢ - qᵢ - hᵢ
} else {
nxt.Sub(&nxt, &sum) // nxt <- 1 + 2 * qᵢ * hᵢ - qᵢ - hᵢ
res.Mul(&res, &nxt) // res <- res * nxt
}
}
return res
}
// Eq sets m to the representation of the polynomial Eq(q₁, ..., qₙ, *, ..., *) × m[0]
func (m *MultiLin) Eq(q []fr.Element) {
n := len(q)
if len(*m) != 1<<n {
panic("destination must have size 2 raised to the size of source")
}
//At the end of each iteration, m(h₁, ..., hₙ) = Eq(q₁, ..., qᵢ₊₁, h₁, ..., hᵢ₊₁)
for i := range q { // In the comments we use a 1-based index so q[i] = qᵢ₊₁
// go through all assignments of (b₁, ..., bᵢ) ∈ {0,1}ⁱ
for j := 0; j < (1 << i); j++ {
j0 := j << (n - i) // bᵢ₊₁ = 0
j1 := j0 + 1<<(n-1-i) // bᵢ₊₁ = 1
(*m)[j1].Mul(&q[i], &(*m)[j0]) // Eq(q₁, ..., qᵢ₊₁, b₁, ..., bᵢ, 1) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) Eq(qᵢ₊₁, 1) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) qᵢ₊₁
(*m)[j0].Sub(&(*m)[j0], &(*m)[j1]) // Eq(q₁, ..., qᵢ₊₁, b₁, ..., bᵢ, 0) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) Eq(qᵢ₊₁, 0) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) (1-qᵢ₊₁)
}
}
}
func (m MultiLin) NumVars() int {
return bits.TrailingZeros(uint(len(m)))
}
func init() {
//TODO: Check for whether already computed in the Getter or this?
lagrangeBasis = make([][]Polynomial, maxLagrangeDomainSize+1)
//size = 0: Cannot extrapolate with no data points
//size = 1: Constant polynomial
lagrangeBasis[1] = []Polynomial{make(Polynomial, 1)}
lagrangeBasis[1][0][0].SetOne()
//for size ≥ 2, the function works
for size := uint8(2); size <= maxLagrangeDomainSize; size++ {
lagrangeBasis[size] = computeLagrangeBasis(size)
}
}
func getLagrangeBasis(domainSize int) []Polynomial {
//TODO: Precompute everything at init or this?
/*if lagrangeBasis[domainSize] == nil {
lagrangeBasis[domainSize] = computeLagrangeBasis(domainSize)
}*/
return lagrangeBasis[domainSize]
}
const maxLagrangeDomainSize uint8 = 12
var lagrangeBasis [][]Polynomial
// computeLagrangeBasis precomputes in explicit coefficient form for each 0 ≤ l < domainSize the polynomial
// pₗ := X (X-1) ... (X-l-1) (X-l+1) ... (X - domainSize + 1) / ( l (l-1) ... 2 (-1) ... (l - domainSize +1) )
// Note that pₗ(l) = 1 and pₗ(n) = 0 if 0 ≤ l < domainSize, n ≠ l
func computeLagrangeBasis(domainSize uint8) []Polynomial {
constTerms := make([]fr.Element, domainSize)
for i := uint8(0); i < domainSize; i++ {
constTerms[i].SetInt64(-int64(i))
}
res := make([]Polynomial, domainSize)
multScratch := make(Polynomial, domainSize-1)
// compute pₗ
for l := uint8(0); l < domainSize; l++ {
// TODO: Optimize this with some trees? O(log(domainSize)) polynomial mults instead of O(domainSize)? Then again it would be fewer big poly mults vs many small poly mults
d := uint8(0) //d is the current degree of res
for i := uint8(0); i < domainSize; i++ {
if i == l {
continue
}
if d == 0 {
res[l] = make(Polynomial, domainSize)
res[l][domainSize-2] = constTerms[i]
res[l][domainSize-1].SetOne()
} else {
current := res[l][domainSize-d-2:]
timesConst := multScratch[domainSize-d-2:]
timesConst.Scale(&constTerms[i], current[1:]) //TODO: Directly double and add since constTerms are tiny? (even less than 4 bits)
nonLeading := current[0 : d+1]
nonLeading.Add(nonLeading, timesConst)
}
d++
}
}
// We have pₗ(i≠l)=0. Now scale so that pₗ(l)=1
// Replace the constTerms with norms
for l := uint8(0); l < domainSize; l++ {
constTerms[l].Neg(&constTerms[l])
constTerms[l] = res[l].Eval(&constTerms[l])
}
constTerms = fr.BatchInvert(constTerms)
for l := uint8(0); l < domainSize; l++ {
res[l].ScaleInPlace(&constTerms[l])
}
return res
}
// InterpolateOnRange performs the interpolation of the given list of elements
// On the range [0, 1,..., len(values) - 1]
func InterpolateOnRange(values []fr.Element) Polynomial {
nEvals := len(values)
lagrange := getLagrangeBasis(nEvals)
var res Polynomial
res.Scale(&values[0], lagrange[0])
temp := make(Polynomial, nEvals)
for i := 1; i < nEvals; i++ {
temp.Scale(&values[i], lagrange[i])
res.Add(res, temp)
}
return res
}