Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added readme #22

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions extractor/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@ import (
"github.com/stretchr/testify/assert"
)

// Example: readme circuit
type DummyCircuit struct {
In_1 frontend.Variable
In_2 frontend.Variable
Out frontend.Variable
}

func (circuit *DummyCircuit) AbsDefine(api abstractor.API) error {
sum := api.Add(circuit.In_1, circuit.In_2)
api.AssertIsEqual(sum, circuit.Out)
return nil
}

func (circuit DummyCircuit) Define(api frontend.API) error {
return abstractor.Concretize(api, &circuit)
}

func TestDummyCircuit(t *testing.T) {
assignment := DummyCircuit{}
out, err := CircuitToLean(&assignment, ecc.BN254)
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
}

// Example: circuit with constant parameter
type SliceGadget struct {
In_1 []frontend.Variable
Expand Down
48 changes: 45 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
# `gnark-lean-extractor` library

This is a transpiler from `Go` to `Lean` for arithmetic circuits. You can design your circuit in `Go` and export it to `Lean`.
The benefit of this library is the ability to prototype circuits in a high level language and perform formal verification using `Lean`.

## Example
Here is how a simple circuit is designed using the `gnark-lean-extractor` library:

```go
type DummyCircuit struct {
In_1 frontend.Variable
In_2 frontend.Variable
Out frontend.Variable
}

func (circuit *DummyCircuit) AbsDefine(api abstractor.API) error {
sum := api.Add(circuit.In_1, circuit.In_2)
api.AssertIsEqual(sum, circuit.Out)
return nil
}
```

Here is how it looks like exported for `Lean`:

```lean
namespace DummyCircuit
def Order : ℕ := 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
variable [Fact (Nat.Prime Order)]
abbrev F := ZMod Order
def circuit (In_1: F) (In_2: F) (Out: F): Prop :=
∃gate_0, gate_0 = Gates.add In_1 In_2 ∧
Gates.eq gate_0 Out ∧
True
end DummyCircuit
```

Further examples are available in `extractor/extractor_test.go` with various levels of complexity.

## How to use
Implement the methods `AbsDefine` and `Define` for `MyCircuit` struct. Choose a curve to test the circuit on, then call the Lean extractor the following way:
The circuit API is based on the `consensys/gnark` library with the addition of Gadgets: this makes for easy integration of existing circuits.
To integrate your own circuit, implement the methods `AbsDefine` and `Define` for `MyCircuit` struct. Choose a circuit curve, then call the extractor with the function `CircuitToLean`:

```go
assignment := MyCircuit{}
out, err := CircuitToLean(&assignment, ecc.BN254)
Expand All @@ -10,10 +51,11 @@ if err != nil {
}
fmt.Println(out)
```

The curves supported match the curves present in the library `consensys/gnark`.

`CircuitToLean` prints in the `stdout` the circuit to be imported in Lean4 for verification. The Lean code depends on library `proven-zk` for the representation of Gates.
`CircuitToLean` returns a string which contains the circuit in a format readable by `Lean`. The `Lean` code depends on the library `proven-zk` for the representation of Gates, Vectors and other useful circuit verification utilities.

## Windows
## Notes
If using Powershell, change font to `NSimSun` for correct view of all characters:
[check this answer](https://stackoverflow.com/a/48029600)