github.com/consensys/gnark-crypto@v0.14.0/ecc/bn254/fr/iop/expressions.go (about) 1 // Copyright 2020 Consensys Software Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Code generated by consensys/gnark-crypto DO NOT EDIT 16 17 package iop 18 19 import ( 20 "errors" 21 "github.com/consensys/gnark-crypto/ecc/bn254/fr" 22 "github.com/consensys/gnark-crypto/internal/parallel" 23 "math/bits" 24 ) 25 26 // Expression represents a multivariate polynomial. 27 type Expression func(i int, x ...fr.Element) fr.Element 28 29 // Evaluate evaluates f on each entry of x. The returned value is 30 // the vector of evaluations of e on x. 31 // The form of the result is form. 32 // if r is provided (not nil), it is used as the result vector, 33 // that is the memory space for the coefficients of the resulting polynomial. 34 // The Size field of the result is the same as the one of x[0]. 35 // The blindedSize field of the result is the same as Size. 36 // The Shift field of the result is 0. 37 func Evaluate(f Expression, r []fr.Element, form Form, x ...*Polynomial) (*Polynomial, error) { 38 if len(x) == 0 { 39 return nil, errors.New("need at lest one input") 40 } 41 42 // check that the sizes are consistent 43 n := x[0].coefficients.Len() 44 m := len(x) 45 for i := 1; i < m; i++ { 46 if n != x[i].coefficients.Len() { 47 return nil, ErrInconsistentSize 48 } 49 } 50 51 // check result len 52 if r == nil { 53 r = make([]fr.Element, n) 54 } else if len(r) != n { 55 return nil, ErrInconsistentSize 56 } 57 58 // result coefficients 59 idx := func(i int) int { 60 return i 61 } 62 if form.Layout != Regular { 63 nn := uint64(64 - bits.TrailingZeros(uint(n))) 64 idx = func(i int) int { 65 return int(bits.Reverse64(uint64(i)) >> nn) 66 } 67 } 68 69 parallel.Execute(n, func(start, end int) { 70 vx := make([]fr.Element, m) 71 for i := start; i < end; i++ { 72 for j := 0; j < m; j++ { 73 vx[j] = x[j].GetCoeff(i) 74 } 75 r[idx(i)] = f(i, vx...) 76 } 77 }) 78 79 res := NewPolynomial(&r, form) 80 res.size = x[0].size 81 res.blindedSize = x[0].size 82 res.shift = 0 83 84 return res, nil 85 }