-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prover/feat/Adding field extension support to Smartvectors (#569)
* field extension support for the maths package
- Loading branch information
1 parent
3c392be
commit db31170
Showing
69 changed files
with
3,766 additions
and
3,922 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package polyext | ||
|
||
import ( | ||
"github.com/consensys/gnark/frontend" | ||
"github.com/consensys/linea-monorepo/prover/maths/field" | ||
"github.com/consensys/linea-monorepo/prover/maths/field/fext/gnarkfext" | ||
) | ||
|
||
// EvaluateLagrangeAnyDomainGnark mirrors [EvaluateLagrangesAnyDomain] but in | ||
// a gnark circuit. The same usage precautions applies for it. | ||
func EvaluateLagrangeAnyDomainGnark(api frontend.API, domain []gnarkfext.Variable, x gnarkfext.Variable) []gnarkfext.Variable { | ||
|
||
outerAPI := gnarkfext.API{Inner: api} | ||
lagrange := make([]gnarkfext.Variable, len(domain)) | ||
|
||
for i, hi := range domain { | ||
lhix := gnarkfext.Variable{field.One(), field.Zero()} | ||
for j, hj := range domain { | ||
if i == j { | ||
// Skip it | ||
continue | ||
} | ||
// more convenient to store -h instead of h | ||
factor := outerAPI.Sub(x, hj) | ||
den := outerAPI.Sub(hi, hj) // so x - h | ||
den = outerAPI.Inverse(den) | ||
|
||
// accumulate the product | ||
lhix = outerAPI.Mul(lhix, factor, den) | ||
} | ||
lagrange[i] = lhix | ||
} | ||
|
||
return lagrange | ||
|
||
} | ||
|
||
// EvaluateUnivariateGnark evaluate a univariate polynomial in a gnark circuit. | ||
// It mirrors [EvalUnivariate]. | ||
func EvaluateUnivariateGnark(api frontend.API, pol []gnarkfext.Variable, x gnarkfext.Variable) gnarkfext.Variable { | ||
res := gnarkfext.Variable{frontend.Variable(0), frontend.Variable(0)} | ||
outerAPI := gnarkfext.API{Inner: api} | ||
for i := len(pol) - 1; i >= 0; i-- { | ||
res = outerAPI.Mul(res, x) | ||
res = outerAPI.Add(res, pol[i]) | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package polyext | ||
|
||
import ( | ||
"github.com/consensys/linea-monorepo/prover/maths/common/vectorext" | ||
"github.com/consensys/linea-monorepo/prover/maths/field/fext" | ||
"github.com/consensys/linea-monorepo/prover/maths/field/fext/gnarkfext" | ||
"testing" | ||
|
||
"github.com/consensys/gnark/frontend" | ||
"github.com/consensys/linea-monorepo/prover/utils/gnarkutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGnarkEval(t *testing.T) { | ||
|
||
t.Run("normal-poly", func(t *testing.T) { | ||
|
||
def := func(api frontend.API) error { | ||
outerAPI := gnarkfext.API{Inner: api} | ||
var ( | ||
pol = vectorext.IntoGnarkAssignment(vectorext.ForTestFromPairs(1, 2, 3, 4, -1, -2)) | ||
x = gnarkfext.Variable{2, 1} | ||
expected = gnarkfext.Variable{ | ||
-5*fext.RootPowers[1] + 3, | ||
-2*fext.RootPowers[1] + 1, | ||
} | ||
res = EvaluateUnivariateGnark(api, pol, x) | ||
) | ||
outerAPI.AssertIsEqual(expected, res) | ||
return nil | ||
} | ||
|
||
gnarkutil.AssertCircuitSolved(t, def) | ||
}) | ||
|
||
t.Run("empty-poly", func(t *testing.T) { | ||
def := func(api frontend.API) error { | ||
outerAPI := gnarkfext.API{Inner: api} | ||
var ( | ||
pol = vectorext.IntoGnarkAssignment([]fext.Element{}) | ||
x = gnarkfext.Variable{2, 3} | ||
expected = gnarkfext.NewZero() | ||
res = EvaluateUnivariateGnark(api, pol, x) | ||
) | ||
outerAPI.AssertIsEqual(expected, res) | ||
return nil | ||
} | ||
gnarkutil.AssertCircuitSolved(t, def) | ||
}) | ||
|
||
} | ||
|
||
func TestGnarkEvalAnyDomain(t *testing.T) { | ||
|
||
t.Run("single-variable", func(t *testing.T) { | ||
|
||
def := func(api frontend.API) error { | ||
outerAPI := gnarkfext.API{Inner: api} | ||
var ( | ||
domain = vectorext.IntoGnarkAssignment(vectorext.ForTestFromPairs(0, 0)) | ||
x = gnarkfext.Variable{42, 0} | ||
expected = vectorext.IntoGnarkAssignment(vectorext.ForTestFromPairs(1, 0)) | ||
res = EvaluateLagrangeAnyDomainGnark(api, domain, x) | ||
) | ||
|
||
require.Len(t, res, len(expected)) | ||
for i := range expected { | ||
outerAPI.AssertIsEqual(expected[i], res[i]) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
gnarkutil.AssertCircuitSolved(t, def) | ||
}) | ||
|
||
t.Run("multiple-variable", func(t *testing.T) { | ||
|
||
def := func(api frontend.API) error { | ||
outerAPI := gnarkfext.API{Inner: api} | ||
var ( | ||
domain = vectorext.IntoGnarkAssignment(vectorext.ForTestFromPairs(0, 0, 1, 0)) | ||
x = gnarkfext.Variable{42, 0} | ||
expected = vectorext.IntoGnarkAssignment(vectorext.ForTestFromPairs(-41, 0, 42, 0)) | ||
res = EvaluateLagrangeAnyDomainGnark(api, domain, x) | ||
) | ||
|
||
require.Len(t, res, len(expected)) | ||
for i := range expected { | ||
outerAPI.AssertIsEqual(expected[i], res[i]) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
gnarkutil.AssertCircuitSolved(t, def) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.