diff --git a/ecc/bls12-377/internal/fptower/e12.go b/ecc/bls12-377/internal/fptower/e12.go index 723902e2b..4be61f114 100644 --- a/ecc/bls12-377/internal/fptower/e12.go +++ b/ecc/bls12-377/internal/fptower/e12.go @@ -778,13 +778,14 @@ func (z *E12) CompressTorus() (E6, error) { return res, nil } -// BatchCompressTorus GT/E12 elements to half their size -// using a batch inversion +// BatchCompressTorus GT/E12 elements to half their size using a batch inversion. +// +// if len(x) == 0 or if any of the x[i].C1 coordinate is 0, this function returns an error. func BatchCompressTorus(x []E12) ([]E6, error) { n := len(x) if n == 0 { - return []E6{}, errors.New("invalid input size") + return nil, errors.New("invalid input size") } var one E6 @@ -793,6 +794,10 @@ func BatchCompressTorus(x []E12) ([]E6, error) { for i := 0; i < n; i++ { res[i].Set(&x[i].C1) + // throw an error if any of the x[i].C1 is 0 + if res[i].IsZero() { + return nil, errors.New("invalid input; C1 is 0") + } } t := BatchInvertE6(res) // costs 1 inverse diff --git a/ecc/bls12-378/internal/fptower/e12.go b/ecc/bls12-378/internal/fptower/e12.go index 0169ee505..3a3b7726c 100644 --- a/ecc/bls12-378/internal/fptower/e12.go +++ b/ecc/bls12-378/internal/fptower/e12.go @@ -778,13 +778,14 @@ func (z *E12) CompressTorus() (E6, error) { return res, nil } -// BatchCompressTorus GT/E12 elements to half their size -// using a batch inversion +// BatchCompressTorus GT/E12 elements to half their size using a batch inversion. +// +// if len(x) == 0 or if any of the x[i].C1 coordinate is 0, this function returns an error. func BatchCompressTorus(x []E12) ([]E6, error) { n := len(x) if n == 0 { - return []E6{}, errors.New("invalid input size") + return nil, errors.New("invalid input size") } var one E6 @@ -793,6 +794,10 @@ func BatchCompressTorus(x []E12) ([]E6, error) { for i := 0; i < n; i++ { res[i].Set(&x[i].C1) + // throw an error if any of the x[i].C1 is 0 + if res[i].IsZero() { + return nil, errors.New("invalid input; C1 is 0") + } } t := BatchInvertE6(res) // costs 1 inverse diff --git a/ecc/bls12-381/internal/fptower/e12.go b/ecc/bls12-381/internal/fptower/e12.go index 0eaa9f3df..92fffa576 100644 --- a/ecc/bls12-381/internal/fptower/e12.go +++ b/ecc/bls12-381/internal/fptower/e12.go @@ -778,13 +778,14 @@ func (z *E12) CompressTorus() (E6, error) { return res, nil } -// BatchCompressTorus GT/E12 elements to half their size -// using a batch inversion +// BatchCompressTorus GT/E12 elements to half their size using a batch inversion. +// +// if len(x) == 0 or if any of the x[i].C1 coordinate is 0, this function returns an error. func BatchCompressTorus(x []E12) ([]E6, error) { n := len(x) if n == 0 { - return []E6{}, errors.New("invalid input size") + return nil, errors.New("invalid input size") } var one E6 @@ -793,6 +794,10 @@ func BatchCompressTorus(x []E12) ([]E6, error) { for i := 0; i < n; i++ { res[i].Set(&x[i].C1) + // throw an error if any of the x[i].C1 is 0 + if res[i].IsZero() { + return nil, errors.New("invalid input; C1 is 0") + } } t := BatchInvertE6(res) // costs 1 inverse diff --git a/ecc/bls24-315/internal/fptower/e24.go b/ecc/bls24-315/internal/fptower/e24.go index 043b85e0d..602ff6ae2 100644 --- a/ecc/bls24-315/internal/fptower/e24.go +++ b/ecc/bls24-315/internal/fptower/e24.go @@ -827,6 +827,10 @@ func BatchCompressTorus(x []E24) ([]E12, error) { for i := 0; i < n; i++ { res[i].Set(&x[i].D1) + // throw an error if any of the x[i].C1 is 0 + if res[i].IsZero() { + return []E12{}, errors.New("invalid input") + } } t := BatchInvertE12(res) // costs 1 inverse diff --git a/ecc/bls24-317/internal/fptower/e24.go b/ecc/bls24-317/internal/fptower/e24.go index c29a23021..a279dd74b 100644 --- a/ecc/bls24-317/internal/fptower/e24.go +++ b/ecc/bls24-317/internal/fptower/e24.go @@ -827,6 +827,10 @@ func BatchCompressTorus(x []E24) ([]E12, error) { for i := 0; i < n; i++ { res[i].Set(&x[i].D1) + // throw an error if any of the x[i].C1 is 0 + if res[i].IsZero() { + return []E12{}, errors.New("invalid input") + } } t := BatchInvertE12(res) // costs 1 inverse diff --git a/ecc/bn254/internal/fptower/e12.go b/ecc/bn254/internal/fptower/e12.go index 950d60de6..17736f9ba 100644 --- a/ecc/bn254/internal/fptower/e12.go +++ b/ecc/bn254/internal/fptower/e12.go @@ -749,13 +749,14 @@ func (z *E12) CompressTorus() (E6, error) { return res, nil } -// BatchCompressTorus GT/E12 elements to half their size -// using a batch inversion +// BatchCompressTorus GT/E12 elements to half their size using a batch inversion. +// +// if len(x) == 0 or if any of the x[i].C1 coordinate is 0, this function returns an error. func BatchCompressTorus(x []E12) ([]E6, error) { n := len(x) if n == 0 { - return []E6{}, errors.New("invalid input size") + return nil, errors.New("invalid input size") } var one E6 @@ -764,6 +765,10 @@ func BatchCompressTorus(x []E12) ([]E6, error) { for i := 0; i < n; i++ { res[i].Set(&x[i].C1) + // throw an error if any of the x[i].C1 is 0 + if res[i].IsZero() { + return nil, errors.New("invalid input; C1 is 0") + } } t := BatchInvertE6(res) // costs 1 inverse diff --git a/ecc/bw6-633/internal/fptower/e6.go b/ecc/bw6-633/internal/fptower/e6.go index 92f54a1dd..285738e8c 100644 --- a/ecc/bw6-633/internal/fptower/e6.go +++ b/ecc/bw6-633/internal/fptower/e6.go @@ -727,6 +727,10 @@ func BatchCompressTorus(x []E6) ([]E3, error) { for i := 0; i < n; i++ { res[i].Set(&x[i].B1) + // throw an error if any of the x[i].C1 is 0 + if res[i].IsZero() { + return []E3{}, errors.New("invalid input") + } } t := BatchInvertE3(res) // costs 1 inverse diff --git a/ecc/bw6-756/internal/fptower/e6.go b/ecc/bw6-756/internal/fptower/e6.go index 35d6b82a7..9cdbe071d 100644 --- a/ecc/bw6-756/internal/fptower/e6.go +++ b/ecc/bw6-756/internal/fptower/e6.go @@ -644,6 +644,10 @@ func BatchCompressTorus(x []E6) ([]E3, error) { for i := 0; i < n; i++ { res[i].Set(&x[i].B1) + // throw an error if any of the x[i].C1 is 0 + if res[i].IsZero() { + return []E3{}, errors.New("invalid input") + } } t := BatchInvertE3(res) // costs 1 inverse diff --git a/ecc/bw6-761/internal/fptower/e6.go b/ecc/bw6-761/internal/fptower/e6.go index f211c4a0c..ac2fa715f 100644 --- a/ecc/bw6-761/internal/fptower/e6.go +++ b/ecc/bw6-761/internal/fptower/e6.go @@ -697,6 +697,10 @@ func BatchCompressTorus(x []E6) ([]E3, error) { for i := 0; i < n; i++ { res[i].Set(&x[i].B1) + // throw an error if any of the x[i].C1 is 0 + if res[i].IsZero() { + return []E3{}, errors.New("invalid input") + } } t := BatchInvertE3(res) // costs 1 inverse diff --git a/internal/generator/tower/template/fq12over6over2/fq12.go.tmpl b/internal/generator/tower/template/fq12over6over2/fq12.go.tmpl index 8fdcabdd0..6bc729ca5 100644 --- a/internal/generator/tower/template/fq12over6over2/fq12.go.tmpl +++ b/internal/generator/tower/template/fq12over6over2/fq12.go.tmpl @@ -757,13 +757,14 @@ func (z *E12) CompressTorus() (E6, error) { return res, nil } -// BatchCompressTorus GT/E12 elements to half their size -// using a batch inversion +// BatchCompressTorus GT/E12 elements to half their size using a batch inversion. +// +// if len(x) == 0 or if any of the x[i].C1 coordinate is 0, this function returns an error. func BatchCompressTorus(x []E12) ([]E6, error) { n := len(x) if n == 0 { - return []E6{}, errors.New("invalid input size") + return nil, errors.New("invalid input size") } var one E6 @@ -772,6 +773,10 @@ func BatchCompressTorus(x []E12) ([]E6, error) { for i := 0; i < n; i++ { res[i].Set(&x[i].C1) + // throw an error if any of the x[i].C1 is 0 + if res[i].IsZero() { + return nil, errors.New("invalid input; C1 is 0") + } } t := BatchInvertE6(res) // costs 1 inverse