Skip to content

Commit

Permalink
v0.6.0 (#10)
Browse files Browse the repository at this point in the history
* remove gnarkd section

* updated groth16 benchmarks with gnark v0.5.0 numbers

* Added redirect.

Signed-off-by: bgravenorst <byron.gravenorst@consensys.net>

* updating submodule to latest

* docs: cs *frontend.ConstraintSystem to api frontend.API

* docs: reflect Assert changes in debug_test.md

* docs: added Hints documentation

* update submodule

remove failing minify module

* docs: remove curveID from Define

* docs: minor clean up

* fix vale error

* fix markdownlint errors

* fix markdownlint errors

Co-authored-by: bgravenorst <byron.gravenorst@consensys.net>
Co-authored-by: Nicolas MASSART <nicolas.massart@consensys.net>
  • Loading branch information
3 people authored Jan 4, 2022
1 parent 2a1d695 commit 31733da
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 62 deletions.
2 changes: 1 addition & 1 deletion docs/Concepts/schemes_curves.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: Proving schemes and curves

`gnark` supports two proving schemes [Groth16](https://eprint.iacr.org/2016/260.pdf) and
[PlonK](https://eprint.iacr.org/2019/953.pdf). These schemes can be instantiated with any of the
following four elliptic curves: *BN254*, *BLS12-381*, *BLS12-377* or *BW6-761*.
following elliptic curves: *BN254*, *BLS12-381*, *BLS12-377*, *BLS24-315*, *BW6-633* or *BW6-761*.

An ID is supplied to `gnark` to choose the proving scheme and the instantiating curve.

Expand Down
48 changes: 23 additions & 25 deletions docs/HowTo/debug_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,44 @@ witness.
the solver couldn't perform an operation needed to verify a constraint.
For example, a division by 0.

!!! tip
You can run the program with `-tags=debug` to display a more verbose stack trace.

### Print values

The easiest way to debug a circuit is to use `cs.Println()`, which behaves like `fmt.Println`, except
The easiest way to debug a circuit is to use `api.Println()`, which behaves like `fmt.Println`, except
it outputs the values when they are solved. For example:

```go
cs.Println("A.X", pubKey.A.X)
api.Println("A.X", pubKey.A.X)
```

!!! note
With solving errors and `cs.Println`, `gnark` outputs a stack trace which contain the exact line number to refer to in the circuit definition.
With solving errors and `api.Println`, `gnark` outputs a stack trace which contain the exact line number to refer to in the circuit definition.

## Test

You can implement tests as Go unit tests, in a `_test.go` file. For example:

```go
// assert object wrapping testing.T
assert := groth16.NewAssert(t)
assert := test.NewAssert(t)

// declare the circuit
var mimcCircuit Circuit

// compile the circuit into a R1CS
r1cs, err := frontend.Compile(ecc.BN254, backend.GROTH16, &mimcCircuit)
assert.NoError(err)

{
// assign invalid values to a witness, ensure the proof fails
var witness Circuit
witness.Hash.Assign(42)
witness.PreImage.Assign(42)
assert.ProverFailed(r1cs, &witness)
}

{
// assign valid values to a witness, ensure the proof is valid
var witness Circuit
witness.PreImage.Assign(35)
witness.Hash.Assign("16130099170765464552823636852555369511329944820189892919423002775646948828469")
assert.ProverSucceeded(r1cs, &witness)
}
var cubicCircuit Circuit

assert.ProverFailed(&cubicCircuit, &Circuit{
PreImage: frontend.Value(42),
Hash: frontend.Value(42),
})

assert.ProverSucceeded(&cubicCircuit, &Circuit{
PreImage: frontend.Value(35),
Hash: frontend.Value("16130099170765464552823636852555369511329944820189892919423002775646948828469"),
}, test.WithCurves(ecc.BN254))

```

See the [test package documentation](https://pkg.go.dev/github.com/consensys/gnark/test@v0.5.2) for more details.

In particular, the default behavior of the assert helper is to test the circuit across all supported curves and backends, ensure correct serialization, and cross-test the constraint system solver against a `big.Int` test execution engine.
15 changes: 6 additions & 9 deletions docs/HowTo/write/circuit_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ description: How to write a gnark circuit
As described in [Circuit structure](circuit_structure.md), `MyCircuit` implements:

```go
func Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error
func Define(api frontend.API) error
```

* `curveID` is injected at compile time to handle different code paths depending on the curve
(for example, hash functions like MiMC have variations depending on the `curveID`)
* `api` is the root object to manipulate when defining constraints.

* `cs` is the root object to manipulate when defining constraints.

Use `x² := cs.Mul(x, x)` to write $x \times x$. For example, to prove that we know the solution to
Use `x² := api.Mul(x, x)` to write $x \times x$. For example, to prove that we know the solution to
the cubic equation $x^3 + x + 5 = y$, write:

```go
x3 := cs.Mul(circuit.X, circuit.X, circuit.X)
cs.AssertIsEqual(circuit.Y, cs.Add(x3, circuit.X, 5))
x3 := api.Mul(circuit.X, circuit.X, circuit.X)
api.AssertIsEqual(circuit.Y, api.Add(x3, circuit.X, 5))
```

!!! info
Expand All @@ -29,7 +26,7 @@ cs.AssertIsEqual(circuit.Y, cs.Add(x3, circuit.X, 5))
This allows flexibility on the circuit definition side when coding, for example:
```go
cs.Mul(X, 2, cs.Add(Y, Z, 42))
api.Mul(X, 2, api.Add(Y, Z, 42))
```

Constants bigger than base field modulus will be reduced $\mod n$.
Expand Down
6 changes: 3 additions & 3 deletions docs/HowTo/write/circuit_structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A `gnark` circuit must implement the `frontend/Circuit` interface:
```go
type Circuit interface {
// Define declares the circuit's Constraints
Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error
Define(api frontend.API) error
}
```

Expand All @@ -26,13 +26,13 @@ type myComponent struct {
X frontend.Variable
}

func (circuit *MyCircuit) Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error {
func (circuit *MyCircuit) Define(api frontend.API) error {
// ... see Cicuit API section
}
```

At compile time, `frontend.Compile(...)` recursively parses the struct fields that contains
`frontend.Variable` to build the `frontend.ConstraintSystem`.
`frontend.Variable` to build the `frontend.constraintSystem`.

By default, a `frontend.Variable` has the `gnark:",secret"` visibility.

Expand Down
8 changes: 4 additions & 4 deletions docs/HowTo/write/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Use standard `for` loops inside a circuit definition.
!!! example "check that `X*X*X*X*X... == Y`"

```go
func (circuit *Circuit) Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error {
func (circuit *Circuit) Define(api frontend.API) error {
for i := 0; i < n; i++ {
circuit.X = cs.Mul(circuit.X, circuit.X)
circuit.X = api.Mul(circuit.X, circuit.X)
}
cs.AssertIsEqual(circuit.X, circuit.Y)
api.AssertIsEqual(circuit.X, circuit.Y)
return nil
}
```
Expand All @@ -31,7 +31,7 @@ this doesn't translate well in a *declarative* API to define the circuit, becaus
of the `frontend.Compile` method is an arithmetic representation that must encode the various
branches.

`gnark` offers `cs.Select(...)` API, which is similar to Prolog-like languages.
`gnark` offers `api.Select(...)` API, which is similar to Prolog-like languages.

```go
// Select if b is true, yields i1 else yields i2
Expand Down
14 changes: 7 additions & 7 deletions docs/HowTo/write/standard_library.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ We provide the following functions in `gnark/std`:
=== "MiMC hash"

```go
func (circuit *mimcCircuit) Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error {
func (circuit *mimcCircuit) Define(api frontend.API) error {
// ...
hFunc, _ := mimc.NewMiMC("seed", curveID)
hFunc, _ := mimc.NewMiMC("seed", api.Curve())
computedHash := hFunc.Hash(cs, circuit.Data)
// ...
}
Expand All @@ -31,8 +31,8 @@ We provide the following functions in `gnark/std`:
Message frontend.Variable `gnark:",public"`
}

func (circuit *eddsaCircuit) Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error {
edCurve, _ := twistededwards.NewEdCurve(curveID)
func (circuit *eddsaCircuit) Define(api frontend.API) error {
edCurve, _ := twistededwards.NewEdCurve(api.Curve())
circuit.PublicKey.Curve = edCurve

eddsa.Verify(cs, circuit.Signature, circuit.Message, circuit.PublicKey)
Expand All @@ -48,8 +48,8 @@ We provide the following functions in `gnark/std`:
Path, Helper []frontend.Variable
}

func (circuit *merkleCircuit) Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error {
hFunc, _ := mimc.NewMiMC("seed", curveID)
func (circuit *merkleCircuit) Define(api frontend.API) error {
hFunc, _ := mimc.NewMiMC("seed", api.Curve())
merkle.VerifyProof(cs, hFunc, circuit.RootHash, circuit.Path, circuit.Helper)
return nil
}
Expand All @@ -66,7 +66,7 @@ We provide the following functions in `gnark/std`:
Hash frontend.Variable
}

func (circuit *verifierCircuit) Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error {
func (circuit *verifierCircuit) Define(api frontend.API) error {
// pairing data
var pairingInfo sw.PairingContext
pairingInfo.Extension = fields.GetBLS377ExtensionFp12(cs)
Expand Down
18 changes: 9 additions & 9 deletions docs/Tutorials/eddsa.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ It also needs a `frontend.ConstraintSystem` object, on which the functions from
[gnark API](../HowTo/write/circuit_api.md) are called.

```go
func Verify(cs *frontend.ConstraintSystem, sig Signature, msg frontend.Variable, pubKey PublicKey) error {
func Verify(api frontend.API, sig Signature, msg frontend.Variable, pubKey PublicKey) error {
// ...
}
```
Expand All @@ -172,7 +172,7 @@ import (
"github.com/consensys/gnark/std/hash/mimc"
)
func Verify(cs *frontend.ConstraintSystem, sig Signature, msg frontend.Variable, pubKey PublicKey) error {
func Verify(api frontend.API, sig Signature, msg frontend.Variable, pubKey PublicKey) error {
// compute H(R, A, M)
data := []frontend.Variable{
Expand Down Expand Up @@ -235,11 +235,11 @@ Next, continue the implementation with the computation of the right-hand side:

!!! tip "Debugging"

You can print values using `cs.Println` that behaves like `fmt.Println`,
You can print values using `api.Println` that behaves like `fmt.Println`,
except it will output the values at proving time (when they are solved).

```go
cs.Println("A.X", pubKey.A.X)
api.Println("A.X", pubKey.A.X)
```

Until now, you have only used objects which are defined in the `gnark` standard library, for example,
Expand All @@ -251,8 +251,8 @@ Use the gnark API, to assert that the left-hand side is equal to the right-hand

```go
// ensures that lhs==rhs
cs.AssertIsEqual(lhs.X, rhs.X)
cs.AssertIsEqual(lhs.Y, rhs.Y)
api.AssertIsEqual(lhs.X, rhs.X)
api.AssertIsEqual(lhs.Y, rhs.Y)
```

!!! info
Expand Down Expand Up @@ -294,9 +294,9 @@ import (
"github.com/consensys/gnark-crypto/ecc"
)
func (circuit *eddsaCircuit) Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error {
func (circuit *eddsaCircuit) Define(api frontend.API) error {
params, err := twistededwards.NewEdCurve(curveID)
params, err := twistededwards.NewEdCurve(api.Curve())
if err != nil {
return err
}
Expand Down Expand Up @@ -419,5 +419,5 @@ Last step is to generate the proof and verify it.
```go
assert := groth16.NewAssert(t)
var witness Circuit
assert.ProverFailed(r1cs, &witness) // .ProverSucceeded
assert.ProverFailed(&circuit, &witness) // .ProverSucceeded
```
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ easily added.
}

// Define declares the circuit's constraints
func (circuit *Circuit) Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error {
func (circuit *Circuit) Define(api frontend.API) error {
// hash function
mimc, err := mimc.NewMiMC("seed", curveID)
mimc, err := mimc.NewMiMC("seed", api.Curve())

// specify constraints
// mimc(preImage) == hash
cs.AssertIsEqual(circuit.Hash, mimc.Hash(cs, circuit.PreImage))
api.AssertIsEqual(circuit.Hash, mimc.Hash(cs, circuit.PreImage))

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ copyright: gnark and its documentation are licensed under Apache 2.0 license
#extra project info and template customisation
extra:
content_vars:
gnark_version: "v0.4.0" # this is used in external URL (godoc, ...)
gnark_version: "v0.6.0" # this is used in external URL (godoc, ...)
logo_is_text: true
latest_version_warning:
url_contains: /latest/
Expand Down

0 comments on commit 31733da

Please sign in to comment.