From be02fb6c83365e29afc6b0a1308911f50c2843b3 Mon Sep 17 00:00:00 2001 From: Arya Tabaie <15056835+Tabaie@users.noreply.github.com> Date: Wed, 18 Dec 2024 18:19:42 -0600 Subject: [PATCH] build generify parallelization --- backend/groth16/bls12-377/mpcsetup/phase2.go | 21 ++++++++++--------- backend/groth16/bls12-381/mpcsetup/phase2.go | 21 ++++++++++--------- backend/groth16/bls24-315/mpcsetup/phase2.go | 21 ++++++++++--------- backend/groth16/bls24-317/mpcsetup/phase2.go | 21 ++++++++++--------- backend/groth16/bw6-633/mpcsetup/phase2.go | 21 ++++++++++--------- backend/groth16/bw6-761/mpcsetup/phase2.go | 21 ++++++++++--------- .../groth16/mpcsetup/phase2.go.tmpl | 21 ++++++++++--------- 7 files changed, 77 insertions(+), 70 deletions(-) diff --git a/backend/groth16/bls12-377/mpcsetup/phase2.go b/backend/groth16/bls12-377/mpcsetup/phase2.go index b5647d47a..825dd65a3 100644 --- a/backend/groth16/bls12-377/mpcsetup/phase2.go +++ b/backend/groth16/bls12-377/mpcsetup/phase2.go @@ -16,6 +16,7 @@ import ( "github.com/consensys/gnark/backend/groth16/internal" "github.com/consensys/gnark/constraint" cs "github.com/consensys/gnark/constraint/bls12-377" + "github.com/consensys/gnark/internal/utils" "math/big" "slices" ) @@ -121,14 +122,18 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { panic("unknown type") } } + scaleG1Slice := func(s []curve.G1Affine) { + utils.Parallelize(len(s), func(start, end int) { + for i := start; i < end; i++ { + s[i].ScalarMultiplication(&s[i], &I) + } + }) + } for i := range sigma { sigma[i].BigInt(&I) - s := p.Parameters.G1.SigmaCKK[i] - for j := range s { - scale(&s[j]) - } scale(&p.Parameters.G2.Sigma[i]) + scaleG1Slice(p.Parameters.G1.SigmaCKK[i]) } delta.BigInt(&I) @@ -137,12 +142,8 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { delta.Inverse(delta) delta.BigInt(&I) - for i := range p.Parameters.G1.Z { - scale(&p.Parameters.G1.Z[i]) - } - for i := range p.Parameters.G1.PKK { - scale(&p.Parameters.G1.PKK[i]) - } + scaleG1Slice(p.Parameters.G1.Z) + scaleG1Slice(p.Parameters.G1.PKK) } func (p *Phase2) Contribute() { diff --git a/backend/groth16/bls12-381/mpcsetup/phase2.go b/backend/groth16/bls12-381/mpcsetup/phase2.go index b9af18976..692b07988 100644 --- a/backend/groth16/bls12-381/mpcsetup/phase2.go +++ b/backend/groth16/bls12-381/mpcsetup/phase2.go @@ -16,6 +16,7 @@ import ( "github.com/consensys/gnark/backend/groth16/internal" "github.com/consensys/gnark/constraint" cs "github.com/consensys/gnark/constraint/bls12-381" + "github.com/consensys/gnark/internal/utils" "math/big" "slices" ) @@ -121,14 +122,18 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { panic("unknown type") } } + scaleG1Slice := func(s []curve.G1Affine) { + utils.Parallelize(len(s), func(start, end int) { + for i := start; i < end; i++ { + s[i].ScalarMultiplication(&s[i], &I) + } + }) + } for i := range sigma { sigma[i].BigInt(&I) - s := p.Parameters.G1.SigmaCKK[i] - for j := range s { - scale(&s[j]) - } scale(&p.Parameters.G2.Sigma[i]) + scaleG1Slice(p.Parameters.G1.SigmaCKK[i]) } delta.BigInt(&I) @@ -137,12 +142,8 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { delta.Inverse(delta) delta.BigInt(&I) - for i := range p.Parameters.G1.Z { - scale(&p.Parameters.G1.Z[i]) - } - for i := range p.Parameters.G1.PKK { - scale(&p.Parameters.G1.PKK[i]) - } + scaleG1Slice(p.Parameters.G1.Z) + scaleG1Slice(p.Parameters.G1.PKK) } func (p *Phase2) Contribute() { diff --git a/backend/groth16/bls24-315/mpcsetup/phase2.go b/backend/groth16/bls24-315/mpcsetup/phase2.go index 6fb1c91de..35fb5020b 100644 --- a/backend/groth16/bls24-315/mpcsetup/phase2.go +++ b/backend/groth16/bls24-315/mpcsetup/phase2.go @@ -16,6 +16,7 @@ import ( "github.com/consensys/gnark/backend/groth16/internal" "github.com/consensys/gnark/constraint" cs "github.com/consensys/gnark/constraint/bls24-315" + "github.com/consensys/gnark/internal/utils" "math/big" "slices" ) @@ -121,14 +122,18 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { panic("unknown type") } } + scaleG1Slice := func(s []curve.G1Affine) { + utils.Parallelize(len(s), func(start, end int) { + for i := start; i < end; i++ { + s[i].ScalarMultiplication(&s[i], &I) + } + }) + } for i := range sigma { sigma[i].BigInt(&I) - s := p.Parameters.G1.SigmaCKK[i] - for j := range s { - scale(&s[j]) - } scale(&p.Parameters.G2.Sigma[i]) + scaleG1Slice(p.Parameters.G1.SigmaCKK[i]) } delta.BigInt(&I) @@ -137,12 +142,8 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { delta.Inverse(delta) delta.BigInt(&I) - for i := range p.Parameters.G1.Z { - scale(&p.Parameters.G1.Z[i]) - } - for i := range p.Parameters.G1.PKK { - scale(&p.Parameters.G1.PKK[i]) - } + scaleG1Slice(p.Parameters.G1.Z) + scaleG1Slice(p.Parameters.G1.PKK) } func (p *Phase2) Contribute() { diff --git a/backend/groth16/bls24-317/mpcsetup/phase2.go b/backend/groth16/bls24-317/mpcsetup/phase2.go index 305154621..f57126c67 100644 --- a/backend/groth16/bls24-317/mpcsetup/phase2.go +++ b/backend/groth16/bls24-317/mpcsetup/phase2.go @@ -16,6 +16,7 @@ import ( "github.com/consensys/gnark/backend/groth16/internal" "github.com/consensys/gnark/constraint" cs "github.com/consensys/gnark/constraint/bls24-317" + "github.com/consensys/gnark/internal/utils" "math/big" "slices" ) @@ -121,14 +122,18 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { panic("unknown type") } } + scaleG1Slice := func(s []curve.G1Affine) { + utils.Parallelize(len(s), func(start, end int) { + for i := start; i < end; i++ { + s[i].ScalarMultiplication(&s[i], &I) + } + }) + } for i := range sigma { sigma[i].BigInt(&I) - s := p.Parameters.G1.SigmaCKK[i] - for j := range s { - scale(&s[j]) - } scale(&p.Parameters.G2.Sigma[i]) + scaleG1Slice(p.Parameters.G1.SigmaCKK[i]) } delta.BigInt(&I) @@ -137,12 +142,8 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { delta.Inverse(delta) delta.BigInt(&I) - for i := range p.Parameters.G1.Z { - scale(&p.Parameters.G1.Z[i]) - } - for i := range p.Parameters.G1.PKK { - scale(&p.Parameters.G1.PKK[i]) - } + scaleG1Slice(p.Parameters.G1.Z) + scaleG1Slice(p.Parameters.G1.PKK) } func (p *Phase2) Contribute() { diff --git a/backend/groth16/bw6-633/mpcsetup/phase2.go b/backend/groth16/bw6-633/mpcsetup/phase2.go index 530dbcfd5..7e18270ff 100644 --- a/backend/groth16/bw6-633/mpcsetup/phase2.go +++ b/backend/groth16/bw6-633/mpcsetup/phase2.go @@ -16,6 +16,7 @@ import ( "github.com/consensys/gnark/backend/groth16/internal" "github.com/consensys/gnark/constraint" cs "github.com/consensys/gnark/constraint/bw6-633" + "github.com/consensys/gnark/internal/utils" "math/big" "slices" ) @@ -121,14 +122,18 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { panic("unknown type") } } + scaleG1Slice := func(s []curve.G1Affine) { + utils.Parallelize(len(s), func(start, end int) { + for i := start; i < end; i++ { + s[i].ScalarMultiplication(&s[i], &I) + } + }) + } for i := range sigma { sigma[i].BigInt(&I) - s := p.Parameters.G1.SigmaCKK[i] - for j := range s { - scale(&s[j]) - } scale(&p.Parameters.G2.Sigma[i]) + scaleG1Slice(p.Parameters.G1.SigmaCKK[i]) } delta.BigInt(&I) @@ -137,12 +142,8 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { delta.Inverse(delta) delta.BigInt(&I) - for i := range p.Parameters.G1.Z { - scale(&p.Parameters.G1.Z[i]) - } - for i := range p.Parameters.G1.PKK { - scale(&p.Parameters.G1.PKK[i]) - } + scaleG1Slice(p.Parameters.G1.Z) + scaleG1Slice(p.Parameters.G1.PKK) } func (p *Phase2) Contribute() { diff --git a/backend/groth16/bw6-761/mpcsetup/phase2.go b/backend/groth16/bw6-761/mpcsetup/phase2.go index c5cfad1b2..75435b045 100644 --- a/backend/groth16/bw6-761/mpcsetup/phase2.go +++ b/backend/groth16/bw6-761/mpcsetup/phase2.go @@ -16,6 +16,7 @@ import ( "github.com/consensys/gnark/backend/groth16/internal" "github.com/consensys/gnark/constraint" cs "github.com/consensys/gnark/constraint/bw6-761" + "github.com/consensys/gnark/internal/utils" "math/big" "slices" ) @@ -121,14 +122,18 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { panic("unknown type") } } + scaleG1Slice := func(s []curve.G1Affine) { + utils.Parallelize(len(s), func(start, end int) { + for i := start; i < end; i++ { + s[i].ScalarMultiplication(&s[i], &I) + } + }) + } for i := range sigma { sigma[i].BigInt(&I) - s := p.Parameters.G1.SigmaCKK[i] - for j := range s { - scale(&s[j]) - } scale(&p.Parameters.G2.Sigma[i]) + scaleG1Slice(p.Parameters.G1.SigmaCKK[i]) } delta.BigInt(&I) @@ -137,12 +142,8 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { delta.Inverse(delta) delta.BigInt(&I) - for i := range p.Parameters.G1.Z { - scale(&p.Parameters.G1.Z[i]) - } - for i := range p.Parameters.G1.PKK { - scale(&p.Parameters.G1.PKK[i]) - } + scaleG1Slice(p.Parameters.G1.Z) + scaleG1Slice(p.Parameters.G1.PKK) } func (p *Phase2) Contribute() { diff --git a/internal/generator/backend/template/zkpschemes/groth16/mpcsetup/phase2.go.tmpl b/internal/generator/backend/template/zkpschemes/groth16/mpcsetup/phase2.go.tmpl index 2bf6316a5..12876f53c 100644 --- a/internal/generator/backend/template/zkpschemes/groth16/mpcsetup/phase2.go.tmpl +++ b/internal/generator/backend/template/zkpschemes/groth16/mpcsetup/phase2.go.tmpl @@ -2,6 +2,7 @@ import ( "bytes" "crypto/sha256" "errors" + "github.com/consensys/gnark/internal/utils" "math/big" "fmt" "slices" @@ -115,14 +116,18 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { panic("unknown type") } } + scaleG1Slice := func(s []curve.G1Affine) { + utils.Parallelize(len(s), func(start, end int) { + for i := start; i < end; i++ { + s[i].ScalarMultiplication(&s[i], &I) + } + }) + } for i := range sigma { sigma[i].BigInt(&I) - s := p.Parameters.G1.SigmaCKK[i] - for j := range s { - scale(&s[j]) - } scale(&p.Parameters.G2.Sigma[i]) + scaleG1Slice(p.Parameters.G1.SigmaCKK[i]) } delta.BigInt(&I) @@ -131,12 +136,8 @@ func (p *Phase2) update(delta *fr.Element, sigma []fr.Element) { delta.Inverse(delta) delta.BigInt(&I) - for i := range p.Parameters.G1.Z { - scale(&p.Parameters.G1.Z[i]) - } - for i := range p.Parameters.G1.PKK { - scale(&p.Parameters.G1.PKK[i]) - } + scaleG1Slice(p.Parameters.G1.Z) + scaleG1Slice(p.Parameters.G1.PKK) } func (p *Phase2) Contribute() {