From 7afd8ff875a26fc22577310bef8e84b1d3c0fa3c Mon Sep 17 00:00:00 2001 From: Youssef El Housni Date: Mon, 18 Jul 2022 10:11:35 +0100 Subject: [PATCH 1/3] fix: throw an error when inverse by 0 in BatchCompressTorus --- ecc/bls12-377/internal/fptower/e12.go | 4 ++++ ecc/bls12-378/internal/fptower/e12.go | 4 ++++ ecc/bls12-381/internal/fptower/e12.go | 4 ++++ ecc/bls24-315/internal/fptower/e24.go | 4 ++++ ecc/bls24-317/internal/fptower/e24.go | 4 ++++ ecc/bn254/internal/fptower/e12.go | 4 ++++ ecc/bw6-633/internal/fptower/e6.go | 4 ++++ ecc/bw6-756/internal/fptower/e6.go | 4 ++++ ecc/bw6-761/internal/fptower/e6.go | 4 ++++ internal/generator/tower/template/fq12over6over2/fq12.go.tmpl | 4 ++++ 10 files changed, 40 insertions(+) diff --git a/ecc/bls12-377/internal/fptower/e12.go b/ecc/bls12-377/internal/fptower/e12.go index 723902e2b..01f5467f1 100644 --- a/ecc/bls12-377/internal/fptower/e12.go +++ b/ecc/bls12-377/internal/fptower/e12.go @@ -793,6 +793,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 []E6{}, errors.New("invalid input") + } } 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..8e718ea62 100644 --- a/ecc/bls12-378/internal/fptower/e12.go +++ b/ecc/bls12-378/internal/fptower/e12.go @@ -793,6 +793,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 []E6{}, errors.New("invalid input") + } } 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..b67afb701 100644 --- a/ecc/bls12-381/internal/fptower/e12.go +++ b/ecc/bls12-381/internal/fptower/e12.go @@ -793,6 +793,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 []E6{}, errors.New("invalid input") + } } 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..508305fde 100644 --- a/ecc/bls24-317/internal/fptower/e24.go +++ b/ecc/bls24-317/internal/fptower/e24.go @@ -826,6 +826,10 @@ func BatchCompressTorus(x []E24) ([]E12, error) { res := make([]E12, n) for i := 0; i < n; i++ { + // throw an error if any of the x[i].C1 is 0 + if res[i].IsZero() { + return []E12{}, errors.New("invalid input") + } res[i].Set(&x[i].D1) } diff --git a/ecc/bn254/internal/fptower/e12.go b/ecc/bn254/internal/fptower/e12.go index 950d60de6..d85d4e89e 100644 --- a/ecc/bn254/internal/fptower/e12.go +++ b/ecc/bn254/internal/fptower/e12.go @@ -764,6 +764,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 []E6{}, errors.New("invalid input") + } } 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..43facdab0 100644 --- a/internal/generator/tower/template/fq12over6over2/fq12.go.tmpl +++ b/internal/generator/tower/template/fq12over6over2/fq12.go.tmpl @@ -772,6 +772,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 []E6{}, errors.New("invalid input") + } } t := BatchInvertE6(res) // costs 1 inverse From e4fea339cc8086204a75f90d2e717b0e7a09f43f Mon Sep 17 00:00:00 2001 From: Youssef El Housni Date: Wed, 27 Jul 2022 12:44:31 +0100 Subject: [PATCH 2/3] fix(bls24-317): throw an error when inverse by 0 in BatchCompressTorus --- ecc/bls24-317/internal/fptower/e24.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecc/bls24-317/internal/fptower/e24.go b/ecc/bls24-317/internal/fptower/e24.go index 508305fde..a279dd74b 100644 --- a/ecc/bls24-317/internal/fptower/e24.go +++ b/ecc/bls24-317/internal/fptower/e24.go @@ -826,11 +826,11 @@ func BatchCompressTorus(x []E24) ([]E12, error) { res := make([]E12, n) 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") } - res[i].Set(&x[i].D1) } t := BatchInvertE12(res) // costs 1 inverse From 507fbce05c30cc1609238b73a378b4741d855229 Mon Sep 17 00:00:00 2001 From: Gautam Botrel Date: Wed, 27 Jul 2022 15:44:38 -0500 Subject: [PATCH 3/3] docs: prettify doc --- ecc/bls12-377/internal/fptower/e12.go | 9 +++++---- ecc/bls12-378/internal/fptower/e12.go | 9 +++++---- ecc/bls12-381/internal/fptower/e12.go | 9 +++++---- ecc/bn254/internal/fptower/e12.go | 9 +++++---- .../generator/tower/template/fq12over6over2/fq12.go.tmpl | 9 +++++---- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/ecc/bls12-377/internal/fptower/e12.go b/ecc/bls12-377/internal/fptower/e12.go index 01f5467f1..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 @@ -795,7 +796,7 @@ func BatchCompressTorus(x []E12) ([]E6, error) { res[i].Set(&x[i].C1) // throw an error if any of the x[i].C1 is 0 if res[i].IsZero() { - return []E6{}, errors.New("invalid input") + return nil, errors.New("invalid input; C1 is 0") } } diff --git a/ecc/bls12-378/internal/fptower/e12.go b/ecc/bls12-378/internal/fptower/e12.go index 8e718ea62..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 @@ -795,7 +796,7 @@ func BatchCompressTorus(x []E12) ([]E6, error) { res[i].Set(&x[i].C1) // throw an error if any of the x[i].C1 is 0 if res[i].IsZero() { - return []E6{}, errors.New("invalid input") + return nil, errors.New("invalid input; C1 is 0") } } diff --git a/ecc/bls12-381/internal/fptower/e12.go b/ecc/bls12-381/internal/fptower/e12.go index b67afb701..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 @@ -795,7 +796,7 @@ func BatchCompressTorus(x []E12) ([]E6, error) { res[i].Set(&x[i].C1) // throw an error if any of the x[i].C1 is 0 if res[i].IsZero() { - return []E6{}, errors.New("invalid input") + return nil, errors.New("invalid input; C1 is 0") } } diff --git a/ecc/bn254/internal/fptower/e12.go b/ecc/bn254/internal/fptower/e12.go index d85d4e89e..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 @@ -766,7 +767,7 @@ func BatchCompressTorus(x []E12) ([]E6, error) { res[i].Set(&x[i].C1) // throw an error if any of the x[i].C1 is 0 if res[i].IsZero() { - return []E6{}, errors.New("invalid input") + return nil, errors.New("invalid input; C1 is 0") } } diff --git a/internal/generator/tower/template/fq12over6over2/fq12.go.tmpl b/internal/generator/tower/template/fq12over6over2/fq12.go.tmpl index 43facdab0..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 @@ -774,7 +775,7 @@ func BatchCompressTorus(x []E12) ([]E6, error) { res[i].Set(&x[i].C1) // throw an error if any of the x[i].C1 is 0 if res[i].IsZero() { - return []E6{}, errors.New("invalid input") + return nil, errors.New("invalid input; C1 is 0") } }