Skip to content

Commit

Permalink
fix: Torus compression exception case
Browse files Browse the repository at this point in the history
  • Loading branch information
yelhousni committed Jun 17, 2022
1 parent 3dfebd9 commit a3cca23
Show file tree
Hide file tree
Showing 40 changed files with 368 additions and 65 deletions.
9 changes: 7 additions & 2 deletions ecc/bls12-377/internal/fptower/e12.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecc/bls12-377/internal/fptower/e12_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecc/bls12-377/pairing_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions ecc/bls12-378/internal/fptower/e12.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecc/bls12-378/internal/fptower/e12_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecc/bls12-378/pairing_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions ecc/bls12-381/internal/fptower/e12.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecc/bls12-381/internal/fptower/e12_pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (z *E12) ExptHalf(x *E12) *E12 {
return z.Conjugate(&result) // because tAbsVal is negative
}

// Expt set z to x^t in E12 and return z
// Expt set z to xᵗ in E12 and return z
// const t uint64 = 15132376222941642752 // negative
func (z *E12) Expt(x *E12) *E12 {
var result E12
Expand Down
2 changes: 1 addition & 1 deletion ecc/bls12-381/internal/fptower/e12_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecc/bls12-381/pairing_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions ecc/bls24-315/internal/fptower/e2.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type E2 struct {
A0, A1 fp.Element
}

// Equal returns true if z equals x, fasle otherwise
// Equal returns true if z equals x, false otherwise
func (z *E2) Equal(x *E2) bool {
return z.A0.Equal(&x.A0) && z.A1.Equal(&x.A1)
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (z *E2) SetRandom() (*E2, error) {
return z, nil
}

// IsZero returns true if the two elements are equal, fasle otherwise
// IsZero returns true if the two elements are equal, false otherwise
func (z *E2) IsZero() bool {
return z.A0.IsZero() && z.A1.IsZero()
}
Expand Down Expand Up @@ -219,3 +219,9 @@ func (z *E2) Sqrt(x *E2) *E2 {

return z
}

func (z *E2) Div(x *E2, y *E2) *E2 {
var r E2
r.Inverse(y).Mul(x, &r)
return z.Set(&r)
}
9 changes: 7 additions & 2 deletions ecc/bls24-315/internal/fptower/e24.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecc/bls24-315/internal/fptower/e24_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions ecc/bls24-315/internal/fptower/e2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,25 @@ func BenchmarkE2Conjugate(b *testing.B) {
a.Conjugate(&a)
}
}

func TestE2Div(t *testing.T) {

parameters := gopter.DefaultTestParameters()
properties := gopter.NewProperties(parameters)

genA := GenE2()
genB := GenE2()

properties.Property("[BLS24-317] dividing then multiplying by the same element does nothing", prop.ForAll(
func(a, b *E2) bool {
var c E2
c.Div(a, b)
c.Mul(&c, b)
return c.Equal(a)
},
genA,
genB,
))

properties.TestingRun(t, gopter.ConsoleReporter(false))
}
6 changes: 6 additions & 0 deletions ecc/bls24-315/internal/fptower/e4.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,9 @@ func BatchInvertE4(a []E4) []E4 {

return res
}

func (z *E4) Div(x *E4, y *E4) *E4 {
var r E4
r.Inverse(y).Mul(x, &r)
return z.Set(&r)
}
2 changes: 1 addition & 1 deletion ecc/bls24-315/pairing_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions ecc/bls24-317/internal/fptower/e12.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (z *E12) SetRandom() (*E12, error) {
return z, nil
}

// IsZero returns true if the two elements are equal, fasle otherwise
func (z *E12) IsZero() bool {
return z.C0.IsZero() && z.C1.IsZero() && z.C2.IsZero()
}

// ToMont converts to Mont form
func (z *E12) ToMont() *E12 {
z.C0.ToMont()
Expand Down Expand Up @@ -201,6 +206,40 @@ func (z *E12) Inverse(x *E12) *E12 {
return z
}

// BatchInvertE12 returns a new slice with every element inverted.
// Uses Montgomery batch inversion trick
func BatchInvertE12(a []E12) []E12 {
res := make([]E12, len(a))
if len(a) == 0 {
return res
}

zeroes := make([]bool, len(a))
var accumulator E12
accumulator.SetOne()

for i := 0; i < len(a); i++ {
if a[i].IsZero() {
zeroes[i] = true
continue
}
res[i].Set(&accumulator)
accumulator.Mul(&accumulator, &a[i])
}

accumulator.Inverse(&accumulator)

for i := len(a) - 1; i >= 0; i-- {
if zeroes[i] {
continue
}
res[i].Mul(&res[i], &accumulator)
accumulator.Mul(&accumulator, &a[i])
}

return res
}

// Exp sets z=x**e and returns it
func (z *E12) Exp(x *E12, e big.Int) *E12 {
var res E12
Expand Down
10 changes: 1 addition & 9 deletions ecc/bls24-317/internal/fptower/e2.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Code generated by consensys/gnark-crypto DO NOT EDIT

package fptower

import (
Expand Down Expand Up @@ -124,7 +122,7 @@ func (z *E2) Neg(x *E2) *E2 {

// String implements Stringer interface for fancy printing
func (z *E2) String() string {
return z.A0.String() + "+" + z.A1.String() + "*u"
return (z.A0.String() + "+" + z.A1.String() + "*u")
}

// ToMont converts to mont form
Expand Down Expand Up @@ -157,12 +155,6 @@ func (z *E2) Conjugate(x *E2) *E2 {
return z
}

// Halve sets z = z / 2
func (z *E2) Halve() {
z.A0.Halve()
z.A1.Halve()
}

// Legendre returns the Legendre symbol of z
func (z *E2) Legendre() int {
var n fp.Element
Expand Down

0 comments on commit a3cca23

Please sign in to comment.