diff --git a/ecc/bls12-377/kzg/mpcsetup.go b/ecc/bls12-377/kzg/mpcsetup.go index 1d9da0058..c8243506f 100644 --- a/ecc/bls12-377/kzg/mpcsetup.go +++ b/ecc/bls12-377/kzg/mpcsetup.go @@ -10,9 +10,11 @@ import ( "crypto/sha256" "encoding/binary" "errors" + "fmt" curve "github.com/consensys/gnark-crypto/ecc/bls12-377" "github.com/consensys/gnark-crypto/ecc/bls12-377/fr" "github.com/consensys/gnark-crypto/ecc/bls12-377/mpcsetup" + "github.com/consensys/gnark-crypto/utils" "io" "math/big" ) @@ -120,11 +122,27 @@ func (s *MpcSetup) Verify(next *MpcSetup) error { } if !next.srs.Vk.G2[1].IsInSubGroup() { - return errors.New("𝔾₂ representation not in subgroup") + return errors.New("[x]₂ representation not in subgroup") } - for i := 1; i < len(next.srs.Pk.G1); i++ { - if !next.srs.Pk.G1[i].IsInSubGroup() { - return errors.New("𝔾₁ representation not in subgroup") + + // TODO @Tabaie replace with batch subgroup check + n := len(next.srs.Pk.G1) - 1 + wp := utils.NewWorkerPool() + defer wp.Stop() + fail := make(chan error, wp.NbWorkers()) + + wp.Submit(n, func(start, end int) { + for i := start; i < end; i++ { + if !next.srs.Pk.G1[i+1].IsInSubGroup() { + fail <- fmt.Errorf("[x^%d]₁ representation not in subgroup", i+1) + break + } + } + }, n/wp.NbWorkers()+1).Wait() + close(fail) + for err := range fail { + if err != nil { + return err } } diff --git a/ecc/bls12-381/kzg/mpcsetup.go b/ecc/bls12-381/kzg/mpcsetup.go index d7fd7e8ec..f44826135 100644 --- a/ecc/bls12-381/kzg/mpcsetup.go +++ b/ecc/bls12-381/kzg/mpcsetup.go @@ -10,9 +10,11 @@ import ( "crypto/sha256" "encoding/binary" "errors" + "fmt" curve "github.com/consensys/gnark-crypto/ecc/bls12-381" "github.com/consensys/gnark-crypto/ecc/bls12-381/fr" "github.com/consensys/gnark-crypto/ecc/bls12-381/mpcsetup" + "github.com/consensys/gnark-crypto/utils" "io" "math/big" ) @@ -120,11 +122,27 @@ func (s *MpcSetup) Verify(next *MpcSetup) error { } if !next.srs.Vk.G2[1].IsInSubGroup() { - return errors.New("𝔾₂ representation not in subgroup") + return errors.New("[x]₂ representation not in subgroup") } - for i := 1; i < len(next.srs.Pk.G1); i++ { - if !next.srs.Pk.G1[i].IsInSubGroup() { - return errors.New("𝔾₁ representation not in subgroup") + + // TODO @Tabaie replace with batch subgroup check + n := len(next.srs.Pk.G1) - 1 + wp := utils.NewWorkerPool() + defer wp.Stop() + fail := make(chan error, wp.NbWorkers()) + + wp.Submit(n, func(start, end int) { + for i := start; i < end; i++ { + if !next.srs.Pk.G1[i+1].IsInSubGroup() { + fail <- fmt.Errorf("[x^%d]₁ representation not in subgroup", i+1) + break + } + } + }, n/wp.NbWorkers()+1).Wait() + close(fail) + for err := range fail { + if err != nil { + return err } } diff --git a/ecc/bls24-315/kzg/mpcsetup.go b/ecc/bls24-315/kzg/mpcsetup.go index 2afb83046..7d41f3d74 100644 --- a/ecc/bls24-315/kzg/mpcsetup.go +++ b/ecc/bls24-315/kzg/mpcsetup.go @@ -10,9 +10,11 @@ import ( "crypto/sha256" "encoding/binary" "errors" + "fmt" curve "github.com/consensys/gnark-crypto/ecc/bls24-315" "github.com/consensys/gnark-crypto/ecc/bls24-315/fr" "github.com/consensys/gnark-crypto/ecc/bls24-315/mpcsetup" + "github.com/consensys/gnark-crypto/utils" "io" "math/big" ) @@ -120,11 +122,27 @@ func (s *MpcSetup) Verify(next *MpcSetup) error { } if !next.srs.Vk.G2[1].IsInSubGroup() { - return errors.New("𝔾₂ representation not in subgroup") + return errors.New("[x]₂ representation not in subgroup") } - for i := 1; i < len(next.srs.Pk.G1); i++ { - if !next.srs.Pk.G1[i].IsInSubGroup() { - return errors.New("𝔾₁ representation not in subgroup") + + // TODO @Tabaie replace with batch subgroup check + n := len(next.srs.Pk.G1) - 1 + wp := utils.NewWorkerPool() + defer wp.Stop() + fail := make(chan error, wp.NbWorkers()) + + wp.Submit(n, func(start, end int) { + for i := start; i < end; i++ { + if !next.srs.Pk.G1[i+1].IsInSubGroup() { + fail <- fmt.Errorf("[x^%d]₁ representation not in subgroup", i+1) + break + } + } + }, n/wp.NbWorkers()+1).Wait() + close(fail) + for err := range fail { + if err != nil { + return err } } diff --git a/ecc/bls24-317/kzg/mpcsetup.go b/ecc/bls24-317/kzg/mpcsetup.go index 37c3e50dc..a460fe934 100644 --- a/ecc/bls24-317/kzg/mpcsetup.go +++ b/ecc/bls24-317/kzg/mpcsetup.go @@ -10,9 +10,11 @@ import ( "crypto/sha256" "encoding/binary" "errors" + "fmt" curve "github.com/consensys/gnark-crypto/ecc/bls24-317" "github.com/consensys/gnark-crypto/ecc/bls24-317/fr" "github.com/consensys/gnark-crypto/ecc/bls24-317/mpcsetup" + "github.com/consensys/gnark-crypto/utils" "io" "math/big" ) @@ -120,11 +122,27 @@ func (s *MpcSetup) Verify(next *MpcSetup) error { } if !next.srs.Vk.G2[1].IsInSubGroup() { - return errors.New("𝔾₂ representation not in subgroup") + return errors.New("[x]₂ representation not in subgroup") } - for i := 1; i < len(next.srs.Pk.G1); i++ { - if !next.srs.Pk.G1[i].IsInSubGroup() { - return errors.New("𝔾₁ representation not in subgroup") + + // TODO @Tabaie replace with batch subgroup check + n := len(next.srs.Pk.G1) - 1 + wp := utils.NewWorkerPool() + defer wp.Stop() + fail := make(chan error, wp.NbWorkers()) + + wp.Submit(n, func(start, end int) { + for i := start; i < end; i++ { + if !next.srs.Pk.G1[i+1].IsInSubGroup() { + fail <- fmt.Errorf("[x^%d]₁ representation not in subgroup", i+1) + break + } + } + }, n/wp.NbWorkers()+1).Wait() + close(fail) + for err := range fail { + if err != nil { + return err } } diff --git a/ecc/bn254/kzg/mpcsetup.go b/ecc/bn254/kzg/mpcsetup.go index 39d6e10f9..c5bc0a954 100644 --- a/ecc/bn254/kzg/mpcsetup.go +++ b/ecc/bn254/kzg/mpcsetup.go @@ -10,9 +10,11 @@ import ( "crypto/sha256" "encoding/binary" "errors" + "fmt" curve "github.com/consensys/gnark-crypto/ecc/bn254" "github.com/consensys/gnark-crypto/ecc/bn254/fr" "github.com/consensys/gnark-crypto/ecc/bn254/mpcsetup" + "github.com/consensys/gnark-crypto/utils" "io" "math/big" ) @@ -120,11 +122,27 @@ func (s *MpcSetup) Verify(next *MpcSetup) error { } if !next.srs.Vk.G2[1].IsInSubGroup() { - return errors.New("𝔾₂ representation not in subgroup") + return errors.New("[x]₂ representation not in subgroup") } - for i := 1; i < len(next.srs.Pk.G1); i++ { - if !next.srs.Pk.G1[i].IsInSubGroup() { - return errors.New("𝔾₁ representation not in subgroup") + + // TODO @Tabaie replace with batch subgroup check + n := len(next.srs.Pk.G1) - 1 + wp := utils.NewWorkerPool() + defer wp.Stop() + fail := make(chan error, wp.NbWorkers()) + + wp.Submit(n, func(start, end int) { + for i := start; i < end; i++ { + if !next.srs.Pk.G1[i+1].IsInSubGroup() { + fail <- fmt.Errorf("[x^%d]₁ representation not in subgroup", i+1) + break + } + } + }, n/wp.NbWorkers()+1).Wait() + close(fail) + for err := range fail { + if err != nil { + return err } } diff --git a/ecc/bw6-633/kzg/mpcsetup.go b/ecc/bw6-633/kzg/mpcsetup.go index d2bf84592..2fdc65557 100644 --- a/ecc/bw6-633/kzg/mpcsetup.go +++ b/ecc/bw6-633/kzg/mpcsetup.go @@ -10,9 +10,11 @@ import ( "crypto/sha256" "encoding/binary" "errors" + "fmt" curve "github.com/consensys/gnark-crypto/ecc/bw6-633" "github.com/consensys/gnark-crypto/ecc/bw6-633/fr" "github.com/consensys/gnark-crypto/ecc/bw6-633/mpcsetup" + "github.com/consensys/gnark-crypto/utils" "io" "math/big" ) @@ -120,11 +122,27 @@ func (s *MpcSetup) Verify(next *MpcSetup) error { } if !next.srs.Vk.G2[1].IsInSubGroup() { - return errors.New("𝔾₂ representation not in subgroup") + return errors.New("[x]₂ representation not in subgroup") } - for i := 1; i < len(next.srs.Pk.G1); i++ { - if !next.srs.Pk.G1[i].IsInSubGroup() { - return errors.New("𝔾₁ representation not in subgroup") + + // TODO @Tabaie replace with batch subgroup check + n := len(next.srs.Pk.G1) - 1 + wp := utils.NewWorkerPool() + defer wp.Stop() + fail := make(chan error, wp.NbWorkers()) + + wp.Submit(n, func(start, end int) { + for i := start; i < end; i++ { + if !next.srs.Pk.G1[i+1].IsInSubGroup() { + fail <- fmt.Errorf("[x^%d]₁ representation not in subgroup", i+1) + break + } + } + }, n/wp.NbWorkers()+1).Wait() + close(fail) + for err := range fail { + if err != nil { + return err } } diff --git a/ecc/bw6-761/kzg/mpcsetup.go b/ecc/bw6-761/kzg/mpcsetup.go index 77f2e1599..f85644f4c 100644 --- a/ecc/bw6-761/kzg/mpcsetup.go +++ b/ecc/bw6-761/kzg/mpcsetup.go @@ -10,9 +10,11 @@ import ( "crypto/sha256" "encoding/binary" "errors" + "fmt" curve "github.com/consensys/gnark-crypto/ecc/bw6-761" "github.com/consensys/gnark-crypto/ecc/bw6-761/fr" "github.com/consensys/gnark-crypto/ecc/bw6-761/mpcsetup" + "github.com/consensys/gnark-crypto/utils" "io" "math/big" ) @@ -120,11 +122,27 @@ func (s *MpcSetup) Verify(next *MpcSetup) error { } if !next.srs.Vk.G2[1].IsInSubGroup() { - return errors.New("𝔾₂ representation not in subgroup") + return errors.New("[x]₂ representation not in subgroup") } - for i := 1; i < len(next.srs.Pk.G1); i++ { - if !next.srs.Pk.G1[i].IsInSubGroup() { - return errors.New("𝔾₁ representation not in subgroup") + + // TODO @Tabaie replace with batch subgroup check + n := len(next.srs.Pk.G1) - 1 + wp := utils.NewWorkerPool() + defer wp.Stop() + fail := make(chan error, wp.NbWorkers()) + + wp.Submit(n, func(start, end int) { + for i := start; i < end; i++ { + if !next.srs.Pk.G1[i+1].IsInSubGroup() { + fail <- fmt.Errorf("[x^%d]₁ representation not in subgroup", i+1) + break + } + } + }, n/wp.NbWorkers()+1).Wait() + close(fail) + for err := range fail { + if err != nil { + return err } } diff --git a/internal/generator/kzg/template/mpcsetup.go.tmpl b/internal/generator/kzg/template/mpcsetup.go.tmpl index 973d23652..913cccca6 100644 --- a/internal/generator/kzg/template/mpcsetup.go.tmpl +++ b/internal/generator/kzg/template/mpcsetup.go.tmpl @@ -3,9 +3,11 @@ import ( "crypto/sha256" "encoding/binary" "errors" + "fmt" curve "github.com/consensys/gnark-crypto/ecc/{{.Name}}" "github.com/consensys/gnark-crypto/ecc/{{.Name}}/fr" "github.com/consensys/gnark-crypto/ecc/{{.Name}}/mpcsetup" + "github.com/consensys/gnark-crypto/utils" "io" "math/big" ) @@ -113,11 +115,27 @@ func (s *MpcSetup) Verify(next *MpcSetup) error { } if !next.srs.Vk.G2[1].IsInSubGroup() { - return errors.New("𝔾₂ representation not in subgroup") + return errors.New("[x]₂ representation not in subgroup") } - for i := 1; i < len(next.srs.Pk.G1); i++ { - if !next.srs.Pk.G1[i].IsInSubGroup() { - return errors.New("𝔾₁ representation not in subgroup") + + // TODO @Tabaie replace with batch subgroup check + n := len(next.srs.Pk.G1) - 1 + wp := utils.NewWorkerPool() + defer wp.Stop() + fail := make(chan error, wp.NbWorkers()) + + wp.Submit(n, func(start, end int) { + for i := start; i < end; i++ { + if !next.srs.Pk.G1[i+1].IsInSubGroup() { + fail <- fmt.Errorf("[x^%d]₁ representation not in subgroup", i+1) + break + } + } + }, n/wp.NbWorkers()+1).Wait() + close(fail) + for err := range fail { + if err != nil { + return err } } diff --git a/utils/parallel.go b/utils/parallel.go index 0421fab50..1a67bc95b 100644 --- a/utils/parallel.go +++ b/utils/parallel.go @@ -35,6 +35,10 @@ func NewWorkerPool() *WorkerPool { return p } +func (wp *WorkerPool) NbWorkers() int { + return wp.nbWorkers +} + // Stop (but does not wait) the pool. It frees the worker. func (wp *WorkerPool) Stop() { close(wp.chJobs)