-
Notifications
You must be signed in to change notification settings - Fork 158
/
expressions.go
77 lines (67 loc) · 2.08 KB
/
expressions.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
// 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 iop
import (
"errors"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
"github.com/consensys/gnark-crypto/internal/parallel"
"math/bits"
)
// Expression represents a multivariate polynomial.
type Expression func(x ...fr.Element) fr.Element
// Evaluate evaluates f on each entry of x. The returned value is
// the vector of evaluations of e on x.
// The form of the result is form.
// The Size field of the result is the same as the one of x[0].
// The blindedSize field of the result is the same as Size.
// The Shift field of the result is 0.
func Evaluate(f Expression, form Form, x ...*Polynomial) (*Polynomial, error) {
if len(x) == 0 {
return nil, errors.New("need at lest one input")
}
// check that the sizes are consistent
n := x[0].coefficients.Len()
m := len(x)
for i := 1; i < m; i++ {
if n != x[i].coefficients.Len() {
return nil, ErrInconsistentSize
}
}
// result coefficients
r := make([]fr.Element, n)
idx := func(i int) int {
return i
}
if form.Layout != Regular {
nn := uint64(64 - bits.TrailingZeros(uint(n)))
idx = func(i int) int {
return int(bits.Reverse64(uint64(i)) >> nn)
}
}
parallel.Execute(n, func(start, end int) {
vx := make([]fr.Element, m)
for i := start; i < end; i++ {
for j := 0; j < m; j++ {
vx[j] = x[j].GetCoeff(i)
}
r[idx(i)] = f(vx...)
}
})
res := NewPolynomial(&r, form)
res.size = x[0].size
res.blindedSize = x[0].size
res.shift = 0
return res, nil
}