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

Refactor: gurvy -> gnark-crypto #65

Merged
merged 13 commits into from
Apr 8, 2021
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ src="banner_gnark.png">

- [x] BLS377
- [x] BLS381
- [x] BN256
- [x] BN254
- [x] BW761

## Getting started
Expand Down Expand Up @@ -68,7 +68,7 @@ Examples are located in `/examples`.
// Circuit must be implemented by user-defined circuits
type Circuit interface {
// Define declares the circuit's Constraints
Define(curveID gurvy.ID, cs *ConstraintSystem) error
Define(curveID ecc.ID, cs *ConstraintSystem) error
}
```

Expand Down Expand Up @@ -101,7 +101,7 @@ func (circuit *CubicCircuit) Define(curveID gurvy.ID, cs *frontend.ConstraintSys
var circuit CubicCircuit

// compiles our circuit into a R1CS
r1cs, err := frontend.Compile(gurvy.BN256, backend.GROTH16, &circuit)
r1cs, err := frontend.Compile(ecc.BN254, backend.GROTH16, &circuit)
```
Using struct tags attributes (similarly to `json` or `xml` encoders in Golang), `frontend.Compile()` will parse the circuit structure and allocate the user secret and public inputs [TODO add godoc link for details].

Expand Down Expand Up @@ -162,7 +162,7 @@ Currently gnark provides the following components (see `gnark/std`):

* The Mimc hash function
* Merkle tree (binary, without domain separation)
* Twisted Edwards curve arithmetic (for bn256 and bls381)
* Twisted Edwards curve arithmetic (for bn254 and bls381)
* Signature (eddsa aglorithm, following https://tools.ietf.org/html/rfc8032)
* Groth16 verifier (1 layer recursive SNARK with BW761)

Expand All @@ -172,9 +172,9 @@ It is difficult to *fairly* and precisely compare benchmarks between libraries.

Here are our measurements for the **Prover**. These benchmarks ran on a AWS c5a.24xlarge instance, with hyperthreading disabled.

The same circuit (computing 2^(2^x)) is benchmarked using `gnark`, `bellman` (bls381, ZCash), `bellman_ce` (bn256, matterlabs).
The same circuit (computing 2^(2^x)) is benchmarked using `gnark`, `bellman` (bls381, ZCash), `bellman_ce` (bn254, matterlabs).

### BN256
### BN254

| nb constraints | 100000|32000000|64000000|
| -------- | --------| -------- | -------- |
Expand Down
32 changes: 16 additions & 16 deletions backend/groth16/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"testing"

"github.com/consensys/gnark/frontend"
backend_bls377 "github.com/consensys/gnark/internal/backend/bls377/cs"
witness_bls377 "github.com/consensys/gnark/internal/backend/bls377/witness"
backend_bls381 "github.com/consensys/gnark/internal/backend/bls381/cs"
witness_bls381 "github.com/consensys/gnark/internal/backend/bls381/witness"
backend_bn256 "github.com/consensys/gnark/internal/backend/bn256/cs"
witness_bn256 "github.com/consensys/gnark/internal/backend/bn256/witness"
backend_bw761 "github.com/consensys/gnark/internal/backend/bw761/cs"
witness_bw761 "github.com/consensys/gnark/internal/backend/bw761/witness"
backend_bls12377 "github.com/consensys/gnark/internal/backend/bls12-377/cs"
witness_bls12377 "github.com/consensys/gnark/internal/backend/bls12-377/witness"
backend_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/cs"
witness_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/witness"
backend_bn254 "github.com/consensys/gnark/internal/backend/bn254/cs"
witness_bn254 "github.com/consensys/gnark/internal/backend/bn254/witness"
backend_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/cs"
witness_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/witness"
gnarkio "github.com/consensys/gnark/io"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -147,26 +147,26 @@ func (assert *Assert) SolvingFailed(r1cs frontend.CompiledConstraintSystem, witn
// returns nil if it succeeds, error otherwise.
func IsSolved(r1cs frontend.CompiledConstraintSystem, witness frontend.Circuit) error {
switch _r1cs := r1cs.(type) {
case *backend_bls377.R1CS:
w := witness_bls377.Witness{}
case *backend_bls12377.R1CS:
w := witness_bls12377.Witness{}
if err := w.FromFullAssignment(witness); err != nil {
return err
}
return _r1cs.IsSolved(w)
case *backend_bls381.R1CS:
w := witness_bls381.Witness{}
case *backend_bls12381.R1CS:
w := witness_bls12381.Witness{}
if err := w.FromFullAssignment(witness); err != nil {
return err
}
return _r1cs.IsSolved(w)
case *backend_bn256.R1CS:
w := witness_bn256.Witness{}
case *backend_bn254.R1CS:
w := witness_bn254.Witness{}
if err := w.FromFullAssignment(witness); err != nil {
return err
}
return _r1cs.IsSolved(w)
case *backend_bw761.R1CS:
w := witness_bw761.Witness{}
case *backend_bw6761.R1CS:
w := witness_bw6761.Witness{}
if err := w.FromFullAssignment(witness); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions backend/groth16/bellman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"encoding/binary"
"testing"

"github.com/consensys/gurvy"
"github.com/consensys/gurvy/bn256/fr"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestVerifyBellmanProof(t *testing.T) {
},
} {
// decode verifying key
vk := NewVerifyingKey(gurvy.BLS381)
vk := NewVerifyingKey(ecc.BLS12_381)

vkBytes, err := base64.StdEncoding.DecodeString(test.vk)
require.NoError(t, err)
Expand All @@ -95,7 +95,7 @@ func TestVerifyBellmanProof(t *testing.T) {
proofBytes, err := base64.StdEncoding.DecodeString(test.proof)
require.NoError(t, err)

proof := NewProof(gurvy.BLS381)
proof := NewProof(ecc.BLS12_381)
_, err = proof.ReadFrom(bytes.NewReader(proofBytes))
require.NoError(t, err)

Expand Down
20 changes: 10 additions & 10 deletions backend/groth16/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ package groth16
import (
"strings"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/frontend"
backend_bls381 "github.com/consensys/gnark/internal/backend/bls381/cs"
witness_bls381 "github.com/consensys/gnark/internal/backend/bls381/witness"
backend_bn256 "github.com/consensys/gnark/internal/backend/bn256/cs"
witness_bn256 "github.com/consensys/gnark/internal/backend/bn256/witness"
"github.com/consensys/gurvy"
backend_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/cs"
witness_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/witness"
backend_bn254 "github.com/consensys/gnark/internal/backend/bn254/cs"
witness_bn254 "github.com/consensys/gnark/internal/backend/bn254/witness"
)

func Fuzz(data []byte) int {
curves := []gurvy.ID{gurvy.BN256, gurvy.BLS381}
curves := []ecc.ID{ecc.BN254, ecc.BLS12_381}
for _, curveID := range curves {
ccs, nbAssertions := frontend.CsFuzzed(data, curveID)
_, s, p := ccs.GetNbVariables()
wSize := s + p - 1
ccs.SetLoggerOutput(nil)
switch _r1cs := ccs.(type) {
case *backend_bls381.R1CS:
w := make(witness_bls381.Witness, wSize)
case *backend_bls12381.R1CS:
w := make(witness_bls12381.Witness, wSize)
// make w random
err := _r1cs.IsSolved(w)
if nbAssertions == 0 && err != nil && !strings.Contains(err.Error(), "couldn't solve computational constraint") {
panic("no assertions, yet solving resulted in an error.")
}
case *backend_bn256.R1CS:
w := make(witness_bn256.Witness, wSize)
case *backend_bn254.R1CS:
w := make(witness_bn254.Witness, wSize)
// make w random
err := _r1cs.IsSolved(w)
if nbAssertions == 0 && err != nil && !strings.Contains(err.Error(), "couldn't solve computational constraint") {
Expand Down
Loading