-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
624 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package vergo | ||
|
||
type Slice[T any] Vergo[[]T] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |