Skip to content

Commit

Permalink
New readme (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eagle941 authored Jul 31, 2023
1 parent 3203848 commit 1432edc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
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)

0 comments on commit 1432edc

Please sign in to comment.