Skip to content

Commit

Permalink
fix: organize std packages hints registrations (#1043)
Browse files Browse the repository at this point in the history
* refactor: bls12377 hints

* chore: remove ineffective code

* refactor: bls24315 hints

* refactor: sw_bls12377 hints

* refactor: sw_bls24315 hints

* fix: include square torus hints in registration

* fix: register package hints

* feat: add side-effect std hint registration

* refactor: make all the hints private

The preferred way to get hints would be to use GetHints() methods in packages.
In that way, the users cannot depend on individual hints and makes refactoring
easier (and avoids any kind of assumptions of the existence of hints).

* fix: remove duplicate hints
  • Loading branch information
ivokub committed Feb 8, 2024
1 parent d11824f commit 9b8efda
Show file tree
Hide file tree
Showing 18 changed files with 668 additions and 659 deletions.
94 changes: 2 additions & 92 deletions std/algebra/native/fields_bls12377/e12.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

bls12377 "github.com/consensys/gnark-crypto/ecc/bls12-377"

"github.com/consensys/gnark/constraint/solver"
"github.com/consensys/gnark/frontend"
)

Expand Down Expand Up @@ -469,48 +468,10 @@ func (e *E12) FrobeniusSquare(api frontend.API, e1 E12) *E12 {
return e
}

var InverseE12Hint = func(_ *big.Int, inputs []*big.Int, res []*big.Int) error {
var a, c bls12377.E12

a.C0.B0.A0.SetBigInt(inputs[0])
a.C0.B0.A1.SetBigInt(inputs[1])
a.C0.B1.A0.SetBigInt(inputs[2])
a.C0.B1.A1.SetBigInt(inputs[3])
a.C0.B2.A0.SetBigInt(inputs[4])
a.C0.B2.A1.SetBigInt(inputs[5])
a.C1.B0.A0.SetBigInt(inputs[6])
a.C1.B0.A1.SetBigInt(inputs[7])
a.C1.B1.A0.SetBigInt(inputs[8])
a.C1.B1.A1.SetBigInt(inputs[9])
a.C1.B2.A0.SetBigInt(inputs[10])
a.C1.B2.A1.SetBigInt(inputs[11])

c.Inverse(&a)

c.C0.B0.A0.BigInt(res[0])
c.C0.B0.A1.BigInt(res[1])
c.C0.B1.A0.BigInt(res[2])
c.C0.B1.A1.BigInt(res[3])
c.C0.B2.A0.BigInt(res[4])
c.C0.B2.A1.BigInt(res[5])
c.C1.B0.A0.BigInt(res[6])
c.C1.B0.A1.BigInt(res[7])
c.C1.B1.A0.BigInt(res[8])
c.C1.B1.A1.BigInt(res[9])
c.C1.B2.A0.BigInt(res[10])
c.C1.B2.A1.BigInt(res[11])

return nil
}

func init() {
solver.RegisterHint(InverseE12Hint)
}

// Inverse e12 elmts
func (e *E12) Inverse(api frontend.API, e1 E12) *E12 {

res, err := api.NewHint(InverseE12Hint, 12, e1.C0.B0.A0, e1.C0.B0.A1, e1.C0.B1.A0, e1.C0.B1.A1, e1.C0.B2.A0, e1.C0.B2.A1, e1.C1.B0.A0, e1.C1.B0.A1, e1.C1.B1.A0, e1.C1.B1.A1, e1.C1.B2.A0, e1.C1.B2.A1)
res, err := api.NewHint(inverseE12Hint, 12, e1.C0.B0.A0, e1.C0.B0.A1, e1.C0.B1.A0, e1.C0.B1.A1, e1.C0.B2.A0, e1.C0.B2.A1, e1.C1.B0.A0, e1.C1.B0.A1, e1.C1.B1.A0, e1.C1.B1.A1, e1.C1.B2.A0, e1.C1.B2.A1)
if err != nil {
// err is non-nil only for invalid number of inputs
panic(err)
Expand All @@ -529,61 +490,10 @@ func (e *E12) Inverse(api frontend.API, e1 E12) *E12 {
return e
}

var DivE12Hint = func(_ *big.Int, inputs []*big.Int, res []*big.Int) error {
var a, b, c bls12377.E12

a.C0.B0.A0.SetBigInt(inputs[0])
a.C0.B0.A1.SetBigInt(inputs[1])
a.C0.B1.A0.SetBigInt(inputs[2])
a.C0.B1.A1.SetBigInt(inputs[3])
a.C0.B2.A0.SetBigInt(inputs[4])
a.C0.B2.A1.SetBigInt(inputs[5])
a.C1.B0.A0.SetBigInt(inputs[6])
a.C1.B0.A1.SetBigInt(inputs[7])
a.C1.B1.A0.SetBigInt(inputs[8])
a.C1.B1.A1.SetBigInt(inputs[9])
a.C1.B2.A0.SetBigInt(inputs[10])
a.C1.B2.A1.SetBigInt(inputs[11])

b.C0.B0.A0.SetBigInt(inputs[12])
b.C0.B0.A1.SetBigInt(inputs[13])
b.C0.B1.A0.SetBigInt(inputs[14])
b.C0.B1.A1.SetBigInt(inputs[15])
b.C0.B2.A0.SetBigInt(inputs[16])
b.C0.B2.A1.SetBigInt(inputs[17])
b.C1.B0.A0.SetBigInt(inputs[18])
b.C1.B0.A1.SetBigInt(inputs[19])
b.C1.B1.A0.SetBigInt(inputs[20])
b.C1.B1.A1.SetBigInt(inputs[21])
b.C1.B2.A0.SetBigInt(inputs[22])
b.C1.B2.A1.SetBigInt(inputs[23])

c.Inverse(&b).Mul(&c, &a)

c.C0.B0.A0.BigInt(res[0])
c.C0.B0.A1.BigInt(res[1])
c.C0.B1.A0.BigInt(res[2])
c.C0.B1.A1.BigInt(res[3])
c.C0.B2.A0.BigInt(res[4])
c.C0.B2.A1.BigInt(res[5])
c.C1.B0.A0.BigInt(res[6])
c.C1.B0.A1.BigInt(res[7])
c.C1.B1.A0.BigInt(res[8])
c.C1.B1.A1.BigInt(res[9])
c.C1.B2.A0.BigInt(res[10])
c.C1.B2.A1.BigInt(res[11])

return nil
}

func init() {
solver.RegisterHint(DivE12Hint)
}

// DivUnchecked e12 elmts
func (e *E12) DivUnchecked(api frontend.API, e1, e2 E12) *E12 {

res, err := api.NewHint(DivE12Hint, 12, e1.C0.B0.A0, e1.C0.B0.A1, e1.C0.B1.A0, e1.C0.B1.A1, e1.C0.B2.A0, e1.C0.B2.A1, e1.C1.B0.A0, e1.C1.B0.A1, e1.C1.B1.A0, e1.C1.B1.A1, e1.C1.B2.A0, e1.C1.B2.A1, e2.C0.B0.A0, e2.C0.B0.A1, e2.C0.B1.A0, e2.C0.B1.A1, e2.C0.B2.A0, e2.C0.B2.A1, e2.C1.B0.A0, e2.C1.B0.A1, e2.C1.B1.A0, e2.C1.B1.A1, e2.C1.B2.A0, e2.C1.B2.A1)
res, err := api.NewHint(divE12Hint, 12, e1.C0.B0.A0, e1.C0.B0.A1, e1.C0.B1.A0, e1.C0.B1.A1, e1.C0.B2.A0, e1.C0.B2.A1, e1.C1.B0.A0, e1.C1.B0.A1, e1.C1.B1.A0, e1.C1.B1.A1, e1.C1.B2.A0, e1.C1.B2.A1, e2.C0.B0.A0, e2.C0.B0.A1, e2.C0.B1.A0, e2.C0.B1.A1, e2.C0.B2.A0, e2.C0.B2.A1, e2.C1.B0.A0, e2.C1.B0.A1, e2.C1.B1.A0, e2.C1.B1.A1, e2.C1.B2.A0, e2.C1.B2.A1)
if err != nil {
// err is non-nil only for invalid number of inputs
panic(err)
Expand Down
45 changes: 2 additions & 43 deletions std/algebra/native/fields_bls12377/e2.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ limitations under the License.
package fields_bls12377

import (
"math/big"

bls12377 "github.com/consensys/gnark-crypto/ecc/bls12-377"
"github.com/consensys/gnark-crypto/ecc/bw6-761/fr"

"github.com/consensys/gnark/constraint/solver"
"github.com/consensys/gnark/frontend"
)

Expand Down Expand Up @@ -145,28 +142,10 @@ func (e *E2) Conjugate(api frontend.API, e1 E2) *E2 {
return e
}

var InverseE2Hint = func(_ *big.Int, inputs []*big.Int, res []*big.Int) error {
var a, c bls12377.E2

a.A0.SetBigInt(inputs[0])
a.A1.SetBigInt(inputs[1])

c.Inverse(&a)

c.A0.BigInt(res[0])
c.A1.BigInt(res[1])

return nil
}

func init() {
solver.RegisterHint(InverseE2Hint)
}

// Inverse e2 elmts
func (e *E2) Inverse(api frontend.API, e1 E2) *E2 {

res, err := api.NewHint(InverseE2Hint, 2, e1.A0, e1.A1)
res, err := api.NewHint(inverseE2Hint, 2, e1.A0, e1.A1)
if err != nil {
// err is non-nil only for invalid number of inputs
panic(err)
Expand All @@ -185,30 +164,10 @@ func (e *E2) Inverse(api frontend.API, e1 E2) *E2 {
return e
}

var DivE2Hint = func(_ *big.Int, inputs []*big.Int, res []*big.Int) error {
var a, b, c bls12377.E2

a.A0.SetBigInt(inputs[0])
a.A1.SetBigInt(inputs[1])
b.A0.SetBigInt(inputs[2])
b.A1.SetBigInt(inputs[3])

c.Inverse(&b).Mul(&c, &a)

c.A0.BigInt(res[0])
c.A1.BigInt(res[1])

return nil
}

func init() {
solver.RegisterHint(DivE2Hint)
}

// DivUnchecked e2 elmts
func (e *E2) DivUnchecked(api frontend.API, e1, e2 E2) *E2 {

res, err := api.NewHint(DivE2Hint, 2, e1.A0, e1.A1, e2.A0, e2.A1)
res, err := api.NewHint(divE2Hint, 2, e1.A0, e1.A1, e2.A0, e2.A1)
if err != nil {
// err is non-nil only for invalid number of inputs
panic(err)
Expand Down
66 changes: 2 additions & 64 deletions std/algebra/native/fields_bls12377/e6.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ limitations under the License.
package fields_bls12377

import (
"math/big"

bls12377 "github.com/consensys/gnark-crypto/ecc/bls12-377"

"github.com/consensys/gnark/constraint/solver"
"github.com/consensys/gnark/frontend"
)

Expand Down Expand Up @@ -184,43 +181,10 @@ func (e *E6) Square(api frontend.API, x E6) *E6 {
return e
}

var DivE6Hint = func(_ *big.Int, inputs []*big.Int, res []*big.Int) error {
var a, b, c bls12377.E6

a.B0.A0.SetBigInt(inputs[0])
a.B0.A1.SetBigInt(inputs[1])
a.B1.A0.SetBigInt(inputs[2])
a.B1.A1.SetBigInt(inputs[3])
a.B2.A0.SetBigInt(inputs[4])
a.B2.A1.SetBigInt(inputs[5])

b.B0.A0.SetBigInt(inputs[6])
b.B0.A1.SetBigInt(inputs[7])
b.B1.A0.SetBigInt(inputs[8])
b.B1.A1.SetBigInt(inputs[9])
b.B2.A0.SetBigInt(inputs[10])
b.B2.A1.SetBigInt(inputs[11])

c.Inverse(&b).Mul(&c, &a)

c.B0.A0.BigInt(res[0])
c.B0.A1.BigInt(res[1])
c.B1.A0.BigInt(res[2])
c.B1.A1.BigInt(res[3])
c.B2.A0.BigInt(res[4])
c.B2.A1.BigInt(res[5])

return nil
}

func init() {
solver.RegisterHint(DivE6Hint)
}

// DivUnchecked e6 elmts
func (e *E6) DivUnchecked(api frontend.API, e1, e2 E6) *E6 {

res, err := api.NewHint(DivE6Hint, 6, e1.B0.A0, e1.B0.A1, e1.B1.A0, e1.B1.A1, e1.B2.A0, e1.B2.A1, e2.B0.A0, e2.B0.A1, e2.B1.A0, e2.B1.A1, e2.B2.A0, e2.B2.A1)
res, err := api.NewHint(divE6Hint, 6, e1.B0.A0, e1.B0.A1, e1.B1.A0, e1.B1.A1, e1.B2.A0, e1.B2.A1, e2.B0.A0, e2.B0.A1, e2.B1.A0, e2.B1.A1, e2.B2.A0, e2.B2.A1)
if err != nil {
// err is non-nil only for invalid number of inputs
panic(err)
Expand All @@ -239,36 +203,10 @@ func (e *E6) DivUnchecked(api frontend.API, e1, e2 E6) *E6 {
return e
}

var InverseE6Hint = func(_ *big.Int, inputs []*big.Int, res []*big.Int) error {
var a, c bls12377.E6

a.B0.A0.SetBigInt(inputs[0])
a.B0.A1.SetBigInt(inputs[1])
a.B1.A0.SetBigInt(inputs[2])
a.B1.A1.SetBigInt(inputs[3])
a.B2.A0.SetBigInt(inputs[4])
a.B2.A1.SetBigInt(inputs[5])

c.Inverse(&a)

c.B0.A0.BigInt(res[0])
c.B0.A1.BigInt(res[1])
c.B1.A0.BigInt(res[2])
c.B1.A1.BigInt(res[3])
c.B2.A0.BigInt(res[4])
c.B2.A1.BigInt(res[5])

return nil
}

func init() {
solver.RegisterHint(InverseE6Hint)
}

// Inverse e6 elmts
func (e *E6) Inverse(api frontend.API, e1 E6) *E6 {

res, err := api.NewHint(InverseE6Hint, 6, e1.B0.A0, e1.B0.A1, e1.B1.A0, e1.B1.A1, e1.B2.A0, e1.B2.A1)
res, err := api.NewHint(inverseE6Hint, 6, e1.B0.A0, e1.B0.A1, e1.B1.A0, e1.B1.A1, e1.B2.A0, e1.B2.A1)
if err != nil {
// err is non-nil only for invalid number of inputs
panic(err)
Expand Down
Loading

0 comments on commit 9b8efda

Please sign in to comment.