Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
kustosz committed May 14, 2024
1 parent 663768c commit 9eabf32
Show file tree
Hide file tree
Showing 9 changed files with 624 additions and 0 deletions.
438 changes: 438 additions & 0 deletions extractor/test/merkle_recover2_test.go

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions extractor/test/nonnative_field_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package extractor_test

import (
"fmt"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/math/emulated"
"github.com/reilabs/gnark-lean-extractor/v3/extractor"
"log"
"testing"
)

type NonnativeCube[T emulated.FieldParams] struct {
Input emulated.Element[T]
Cubed emulated.Element[T]
}

func (gadget *NonnativeCube[T]) Define(api frontend.API) error {
field, err := emulated.NewField[T](api)
if err != nil {
return err
}
result := field.Mul(&gadget.Input, &gadget.Input)
result = field.Mul(result, &gadget.Input)
field.AssertIsEqual(result, &gadget.Cubed)
return nil
}

func TestNonnativeCubeCircuit(t *testing.T) {
circuit := NonnativeCube[emulated.BLS12381Fr]{}
out, err := extractor.CircuitToLean(&circuit, ecc.BN254)
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
checkOutput(t, out)
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fxamacker/cbor/v2 v2.5.0 // indirect
github.com/google/pprof v0.0.0-20230817174616-7a8ec2ada47b // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/zerolog v1.30.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fxamacker/cbor/v2 v2.5.0 h1:oHsG0V/Q6E/wqTS2O1Cozzsy69nqCiguo5Q1a1ADivE=
github.com/fxamacker/cbor/v2 v2.5.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/pprof v0.0.0-20230817174616-7a8ec2ada47b h1:h9U78+dx9a4BKdQkBBos92HalKpaGKHrp+3Uo6yTodo=
github.com/google/pprof v0.0.0-20230817174616-7a8ec2ada47b/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/ingonyama-zk/icicle v0.0.0-20230928131117-97f0079e5c71 h1:YxI1RTPzpFJ3MBmxPl3Bo0F7ume7CmQEC1M9jL6CT94=
github.com/ingonyama-zk/iciclegnark v0.1.0 h1:88MkEghzjQBMjrYRJFxZ9oR9CTIpB8NG2zLeCJSvXKQ=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down Expand Up @@ -47,7 +51,9 @@ github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiS
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions vergo/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package vergo

type Slice[T any] Vergo[[]T]
7 changes: 7 additions & 0 deletions vergo/uint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package vergo

type Uint64 Vergo[uint64]

func Add(a, b Uint64) Uint64 {
return Uint64(PrimOp1(func() uint64 { return a.Value + b.Value }, *a.Variable, *b.Variable))
}
111 changes: 111 additions & 0 deletions vergo/vergo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package vergo

import (
"fmt"
"runtime"
"strings"
)

func getQualifiedCallerName(skip int) string {
pc, _, _, ok := runtime.Caller(skip + 1)
if !ok {
panic("oops")
}
frames := runtime.CallersFrames((&[1]uintptr{pc})[:])
frame, _ := frames.Next()
return frame.Function
}

type Op struct {
tag string
inputs []Variable
outputs []Variable
}

func (o Op) StringWithIndent(indent int) string {
indents := strings.Repeat("\t", indent)
results := make([]string, len(o.outputs))
for i, v := range o.outputs {
results[i] = fmt.Sprintf("v%d", v.id)
}
inputs := make([]string, len(o.inputs))
for i, v := range o.inputs {
inputs[i] = fmt.Sprintf("v%d", v.id)
}
res := fmt.Sprintf("%s%s := %s(%s)", indents, strings.Join(results, ", "), o.tag, strings.Join(inputs, ", "))
return res
}

type Scope struct {
lastVar int
ops []Op
}

func (s *Scope) String() string {
res := ""
for _, op := range s.ops {
res += op.StringWithIndent(1) + "\n"
}
return res
}

func (s *Scope) NewVariable() Variable {
s.lastVar++
return Variable{
id: s.lastVar,
scope: s,
}
}

var globalScope *Scope = nil

func NewScope() *Scope {
return &Scope{}
}

type Variable struct {
id int
scope *Scope
}

type Vergo[T any] struct {
Variable *Variable
Value T
}

func (vergo *Vergo[T]) OfVar(v *Variable) *Vergo[T] {
vergo.Variable = v
return vergo
}

func IsExtracting() bool {
return globalScope != nil
}

func PrimOp1[T any](handler func() T, vars ...Variable) Vergo[T] {
if globalScope == nil {
return Vergo[T]{Value: handler()}
} else {
res := globalScope.NewVariable()
globalScope.ops = append(globalScope.ops, Op{getQualifiedCallerName(1), vars, []Variable{res}})
return Vergo[T]{Variable: &res}
}
}

func StartExtracting() {
globalScope = NewScope()
}

func StopExtracting() *Scope {
res := globalScope
globalScope = nil
return res
}

func FreeVar() *Variable {
if globalScope == nil {
return nil
}
r := globalScope.NewVariable()
return &r
}
18 changes: 18 additions & 0 deletions vergo/vergo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package vergo_test

import (
"fmt"
"github.com/reilabs/gnark-lean-extractor/v3/vergo"
"testing"
)

func TestThisThing(t *testing.T) {
vergo.StartExtracting()
v1 := vergo.Uint64(*new(vergo.Vergo[uint64]).OfVar(vergo.FreeVar()))
v2 := vergo.Uint64(*new(vergo.Vergo[uint64]).OfVar(vergo.FreeVar()))
v3 := vergo.Add(v1, v2)
vergo.Add(v3, v3)
scope := vergo.StopExtracting()
fmt.Printf("%v", scope)
fmt.Printf("TESTANDO\n")
}

0 comments on commit 9eabf32

Please sign in to comment.