mirror of
https://github.com/arnaucube/go-circom-prover-verifier.git
synced 2026-02-07 03:16:46 +01:00
Add polynomials arithmetic in goff
Polynomials and ifft moved to goff (iden3/go-iden3-crypto/ff) instead of *big.Int. Benchmarks: - Before: BenchmarkArithmetic/polynomialSub-4 2774 441063 ns/op BenchmarkArithmetic/polynomialMul-4 1 1135732757 ns/op BenchmarkArithmetic/polynomialDiv-4 768 1425192 ns/op BenchmarkGenerateProof-4 1 2844488975 ns/op - With this commit: BenchmarkArithmetic/polynomialSubE-4 23097 54152 ns/op BenchmarkArithmetic/polynomialMulE-4 25 44914327 ns/op BenchmarkArithmetic/polynomialDivE-4 8703 132573 ns/op BenchmarkGenerateProof-4 1 1530398526 ns/op
This commit is contained in:
@@ -3,6 +3,8 @@ package prover
|
||||
import (
|
||||
"bytes"
|
||||
"math/big"
|
||||
|
||||
"github.com/iden3/go-iden3-crypto/ff"
|
||||
)
|
||||
|
||||
func arrayOfZeroes(n int) []*big.Int {
|
||||
@@ -12,6 +14,13 @@ func arrayOfZeroes(n int) []*big.Int {
|
||||
}
|
||||
return r
|
||||
}
|
||||
func arrayOfZeroesE(n int) []*ff.Element {
|
||||
var r []*ff.Element
|
||||
for i := 0; i < n; i++ {
|
||||
r = append(r, ff.NewElement())
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func fAdd(a, b *big.Int) *big.Int {
|
||||
ab := new(big.Int).Add(a, b)
|
||||
@@ -75,6 +84,17 @@ func polynomialSub(a, b []*big.Int) []*big.Int {
|
||||
return r
|
||||
}
|
||||
|
||||
func polynomialSubE(a, b []*ff.Element) []*ff.Element {
|
||||
r := arrayOfZeroesE(max(len(a), len(b)))
|
||||
for i := 0; i < len(a); i++ {
|
||||
r[i].Add(r[i], a[i])
|
||||
}
|
||||
for i := 0; i < len(b); i++ {
|
||||
r[i].Sub(r[i], b[i])
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func polynomialMul(a, b []*big.Int) []*big.Int {
|
||||
r := arrayOfZeroes(len(a) + len(b) - 1)
|
||||
for i := 0; i < len(a); i++ {
|
||||
@@ -85,6 +105,16 @@ func polynomialMul(a, b []*big.Int) []*big.Int {
|
||||
return r
|
||||
}
|
||||
|
||||
func polynomialMulE(a, b []*ff.Element) []*ff.Element {
|
||||
r := arrayOfZeroesE(len(a) + len(b) - 1)
|
||||
for i := 0; i < len(a); i++ {
|
||||
for j := 0; j < len(b); j++ {
|
||||
r[i+j].Add(r[i+j], ff.NewElement().Mul(a[i], b[j]))
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func polynomialDiv(a, b []*big.Int) ([]*big.Int, []*big.Int) {
|
||||
// https://en.wikipedia.org/wiki/Division_algorithm
|
||||
r := arrayOfZeroes(len(a) - len(b) + 1)
|
||||
@@ -100,3 +130,31 @@ func polynomialDiv(a, b []*big.Int) ([]*big.Int, []*big.Int) {
|
||||
}
|
||||
return r, rem
|
||||
}
|
||||
|
||||
func polynomialDivE(a, b []*ff.Element) ([]*ff.Element, []*ff.Element) {
|
||||
// https://en.wikipedia.org/wiki/Division_algorithm
|
||||
r := arrayOfZeroesE(len(a) - len(b) + 1)
|
||||
rem := a
|
||||
for len(rem) >= len(b) {
|
||||
l := ff.NewElement().Div(rem[len(rem)-1], b[len(b)-1])
|
||||
pos := len(rem) - len(b)
|
||||
r[pos] = l
|
||||
aux := arrayOfZeroesE(pos)
|
||||
aux1 := append(aux, l)
|
||||
aux2 := polynomialSubE(rem, polynomialMulE(b, aux1))
|
||||
rem = aux2[:len(aux2)-1]
|
||||
}
|
||||
return r, rem
|
||||
}
|
||||
|
||||
// once https://github.com/iden3/go-iden3-crypto/pull/22 is merged, use the fucntion from there
|
||||
func ElementArrayToBigIntArray(e []*ff.Element) []*big.Int {
|
||||
var o []*big.Int
|
||||
for i := range e {
|
||||
ei := e[i]
|
||||
bi := big.NewInt(0)
|
||||
ei.ToBigIntRegular(bi)
|
||||
o = append(o, bi)
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user