Skip to content

Commit

Permalink
Added field in code extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
Eagle941 committed Jun 21, 2023
1 parent 3b27a85 commit f3be700
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"
"reflect"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/hint"
"github.com/consensys/gnark/frontend"
)
Expand Down Expand Up @@ -129,6 +130,7 @@ type ExCircuit struct {
type CodeExtractor struct {
Code []App
Gadgets []ExGadget
Field ecc.ID
}

func operandFromArray(arg []frontend.Variable) Operand {
Expand Down Expand Up @@ -194,7 +196,7 @@ func (ce *CodeExtractor) Inverse(i1 frontend.Variable) frontend.Variable {
}

func (ce *CodeExtractor) ToBinary(i1 frontend.Variable, n ...int) []frontend.Variable {
nbBits := 254 // Taken from gnark lib
nbBits := ce.Field.ScalarField().BitLen()
if len(n) == 1 {
nbBits = n[0]
if nbBits < 0 {
Expand Down
5 changes: 3 additions & 2 deletions extractor/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"gnark-extractor/abstractor"
"testing"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/frontend"
)

Expand Down Expand Up @@ -47,7 +48,7 @@ func (circuit MerkleRecover) Define(api frontend.API) error {

func TestMerkleRecover(t *testing.T) {
assignment := MerkleRecover{}
err := CircuitToLean(&assignment)
err := CircuitToLean(&assignment, ecc.BW6_756)
if err != nil {
fmt.Println("CircuitToLean error!")
fmt.Println(err.Error())
Expand Down Expand Up @@ -89,7 +90,7 @@ func (circuit TwoGadgets) Define(api frontend.API) error {

func TestTwoGadgets(t *testing.T) {
assignment := TwoGadgets{}
err := CircuitToLean(&assignment)
err := CircuitToLean(&assignment, ecc.BN254)
if err != nil {
fmt.Println("CircuitToLean error!")
fmt.Println(err.Error())
Expand Down
4 changes: 3 additions & 1 deletion extractor/lean_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"strings"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/frontend"
)

Expand All @@ -30,10 +31,11 @@ func ExportCircuit(circuit ExCircuit) string {
return fmt.Sprintf("%s\n\n%s", strings.Join(gadgets, "\n\n"), circ)
}

func CircuitToLean(circuit abstractor.Circuit) error {
func CircuitToLean(circuit abstractor.Circuit, field ecc.ID) error {
api := CodeExtractor{
Code: []App{},
Gadgets: []ExGadget{},
Field: field,
}
err := circuit.AbsDefine(&api)
if err != nil {
Expand Down

0 comments on commit f3be700

Please sign in to comment.