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

Pairing gadget, new circuit API #26

Merged
merged 51 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
9ca0c99
move select in merkle tree verification
ThomasPiellard May 20, 2020
5cdb8cb
started field extension for the pairing gadget
ThomasPiellard May 25, 2020
19d0d14
wip tests field extension gadgets
ThomasPiellard May 27, 2020
257f15e
update go.mod
ThomasPiellard May 27, 2020
4ed7af1
update config.yml
ThomasPiellard May 27, 2020
ad63db2
frobenius + custom pairing tools (mulByV...) tested for the tower
ThomasPiellard May 28, 2020
28fb627
tests for g1, g2 add and double OK, + tests for inverse in Fp2,Fp6,Fp12
ThomasPiellard May 29, 2020
7cfa940
pairing gadget: line Eval OK + final expo OK
ThomasPiellard Jun 1, 2020
aa527d9
pairing gadget done, test OK
ThomasPiellard Jun 2, 2020
060eacc
pairing gadget: added affine version to lower the number of constraints
ThomasPiellard Jun 3, 2020
4079c7a
groth16/: made ParsePublicInput public, final groth16 circuit impleme…
ThomasPiellard Jun 17, 2020
e20482b
points to gurvy@develop, addition of final circuit for groth16"
ThomasPiellard Jun 17, 2020
370950d
merge master
ThomasPiellard Jun 17, 2020
b094c71
udpated version (v0.2.0-alpha)
ThomasPiellard Jun 17, 2020
8ecc2dd
added backend for bw761; gnark points to gurvy@develop for bw761
ThomasPiellard Jun 17, 2020
12fd746
pcd test circuit implemented, currently 386k constraints for groth16 …
ThomasPiellard Jun 18, 2020
f85a267
Merge branch 'master' into pairing_gadget
ThomasPiellard Jun 18, 2020
e9516e0
updated version
ThomasPiellard Jun 18, 2020
0b2e396
recursive snark test passes on bw761
ThomasPiellard Jun 22, 2020
fa4f6e3
shrunk down nb constraints for Fp12 Mul (458->276), mostly due to Kar…
ThomasPiellard Jun 26, 2020
32aed41
fixed wrong preimage/public hash pair in the groth16 verifier on bw761
ThomasPiellard Jul 3, 2020
631917f
refactored cubic.go with new API using interfaces to streamline circu…
gbotrel Jul 7, 2020
f360cd6
refactored rollup.go with new API using interfaces to streamline circ…
gbotrel Jul 7, 2020
e57325b
frontend: added Circuit and CircuitVariable interfaces. Can now assig…
gbotrel Jul 8, 2020
5167317
fix previous commit: added missing files
gbotrel Jul 8, 2020
fbf6dee
refactor: checkpoint
gbotrel Jul 8, 2020
bef7258
refactor wip: removing these typed backend Casts and New from user de…
gbotrel Jul 9, 2020
bebc681
refactor wip: removing these typed backend Casts and New from user de…
gbotrel Jul 9, 2020
7695efa
refactor wip: user defined circuits and gadget can avoid importing ty…
gbotrel Jul 9, 2020
d56fc8a
benchmark is back
gbotrel Jul 9, 2020
caf8ead
removed CircuitVariable interface and use concrete type Variable instead
gbotrel Jul 10, 2020
d56bbea
refactored mimc example with new API
gbotrel Jul 10, 2020
001d51b
examples/exponentiate: rewrote circuit with new API
gbotrel Jul 10, 2020
b65e56e
refactor: remove Gadget suffix from many structs
gbotrel Jul 10, 2020
77cd5b1
make golint a little happier
gbotrel Jul 10, 2020
0bc00cc
minor code cleanup
gbotrel Jul 10, 2020
75fd155
minor code cleanup
gbotrel Jul 10, 2020
54fb740
internal/templates: added missing explicit import in R1CS template
gbotrel Jul 10, 2020
4902576
backend/: groth16 now uses directly test circuits without deprecated …
gbotrel Jul 10, 2020
8a8b600
gnark: input file is now json. accepts 0x hex and decimal repr for as…
gbotrel Jul 13, 2020
a0f9911
frontend:/ renamed LinearCombination.Constraint to Variable
gbotrel Jul 13, 2020
8640147
clean up some TODOs
gbotrel Jul 13, 2020
f421730
backend/groth16: added DummySetup that just return a random proving k…
gbotrel Jul 14, 2020
133cde3
circleci: new workflow with more insight on unit tests
gbotrel Jul 14, 2020
9bb0266
updated README.md
gbotrel Jul 14, 2020
ac280b4
frontend/: added test to parse struct tags in circuit_tags_test.go
gbotrel Jul 14, 2020
4b162ee
frontend:/ replaced LinearExpression by simple variables in mustBeLes…
gbotrel Jul 15, 2020
5ad4cfd
groth16: fix possible starvation issue in Prover -- there existed a w…
gbotrel Jul 15, 2020
ea6c7e2
bump version to v0.2.1-alpha
ThomasPiellard Jun 22, 2020
002bd98
Merge branch 'develop' into gadget_cleanup
gbotrel Jul 15, 2020
b544602
saving files help.
gbotrel Jul 15, 2020
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
11 changes: 6 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ jobs:
- image: circleci/golang:latest
steps:
- checkout
- run: mkdir -p /tmp/test-results
- restore_cache:
keys:
- go-mod-v1-{{ checksum "go.sum" }}
- run: if [[ -n $(gofmt -l .) ]]; then echo "Please run gofmt"; exit 1; fi
- run: go vet -tags bls377 -v ./...
- run: go vet -tags bls381 -v ./...
- run: go vet -tags bn256 -v ./...
- run: go vet -v ./...
- run: go test -v ./cmd/ -run=VersionIsGenerated # ensure version is generated
- run: go get golang.org/x/tools/cmd/goimports
- run: go generate ./...
- run: go test -v -short -tags debug ./...
- run: gotestsum --junitfile /tmp/test-results/results.xml -- ./... -short -v -tags debug
- store_test_results:
path: /tmp/test-results
- save_cache:
key: go-mod-v1-{{ checksum "go.sum" }}
paths:
- "/go/pkg/mod"
- "/go/pkg/mod"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ tasks.txt
*.r1cs
*.log

**/benchmark

# generated files during integratrion tests
integratrion_test/**

Expand Down
96 changes: 80 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ src="banner_gnark.png">
- [x] BLS377
- [x] BLS381
- [x] BN256
- [x] BW761

## Getting started

Expand Down Expand Up @@ -53,12 +54,15 @@ To install for use as a Go package:
4. Run `gnark prove circuit.r1cs --pk circuit.pk --input input`to generate a proof
5. Run `gnark verify circuit.proof --vk circuit.vk --input input.public` to verify a proof

Note that, currently, the input file has a simple csv-like format:
```csv
secret, x, 3
public, y, 35
Note that, currently (and it may change), the input file has a the following JSON format:
```json
{
"x":"3",
"y":"0xdeff12"
}
```


Using the `gnark` CLI tool is **optional**. Developers may expose circuits through gRPC or REST APIs, export to Solidity, chose their serialization formats, etc. This is ongoing work on our side, but new feature suggestions or PR are welcome.

### Examples and `gnark` usage
Expand All @@ -69,24 +73,84 @@ Run `gnark --help` for a list of available commands.

#### /examples/cubic_equation

1. To define a circuit, one must implement the `frontend.Circuit` interface:

```golang
// Circuit must be implemented by user-defined circuits
type Circuit interface {
// Define declares the circuit's constraints
Define(ctx *Context, cs *CS) error

// PostInit is called by frontend.Compile() after the automatic initialization of Variable
// In some cases, we may have custom allocations to do (foreign keys, alias in constraints, ...)
PostInit(ctx *Context) error
}
```

2. Here is what `x**3 + x + 5 = y` looks like

```golang
// x**3 + x + 5 y
func main() {
// create root constraint system
circuit := cs.New()
type CubicCircuit struct {
// tagging a variable is optional
// default uses variable name and secret visibility.
X frontend.Variable `gnark:"x"`
Y frontend.Variable `gnark:"y, public"`
}

// declare secret and public inputs
x := circuit.SECRET_INPUT("x")
y := circuit.PUBLIC_INPUT("y")
func (circuit *CubicCircuit) Define(ctx *frontend.Context, cs *frontend.CS) error {
// x**3 + x + 5 == y
x3 := cs.MUL(circuit.X, circuit.X, circuit.X)
cs.MUSTBE_EQ(circuit.Y, cs.ADD(x3, circuit.X, 5))

// specify constraints
x3 := circuit.MUL(x, x, x)
circuit.MUSTBE_EQ(y, circuit.ADD(x3, x, 5))
// we can tag a variable for testing and / or debugging purposes, it has no impact on performances
x3.Tag("x^3")

circuit.Write("circuit.r1cs")
return nil
}
```

3. The circuit is then compiled (into a R1CS)

```golang
var cubicCircuit CubicCircuit
// init context
ctx := frontend.NewContext(gurvy.BN256)
// add key values to context, usable by circuit and all components
// ex: ctx.Set(rho, new(big.Int).Set("..."))

// compiles our circuit into a R1CS
r1cs, err := frontend.Compile(ctx, &cubicCircuit)
```

Note that in most cases, the user don't need to *allocate* inputs (here X, Y) and it's done by the `frontend.Compile()` method using the struct tags attributes, similarly to `json` or `xml` encoders in Golang.

4. The circuit can be tested like so:
```golang
{
cubicCircuit.X.Assign(42)
cubicCircuit.Y.Assign(42)

assert.NotSolved(r1cs, &cubicCircuit)
}

{
cubicCircuit.X.Assign(3)
cubicCircuit.Y.Assign(35)
expectedValues := make(map[string]interface{})
expectedValues["x^3"] = 27
expectedValues["x"] = 3
assert.Solved(r1cs, &cubicCircuit, expectedValues)
}
```

5. The APIs to call Groth16 algorithms:
```golang
pk, vk := groth16.Setup(r1cs)
proof, err := groth16.Prove(r1cs, pk, solution)
err := groth16.Verify(proof, vk, solution)
```

6. Using the CLI
```
cd examples/cubic_equation
go run cubic.go
Expand All @@ -96,7 +160,6 @@ gnark verify circuit.proof --vk circuit.vk --input input.public
```



### API vs DSL

While several ZKP projects chose to develop their own language and compiler for the *frontend*, we designed a high-level API, in plain Go.
Expand All @@ -120,6 +183,7 @@ Currently gnark provides the following gadgets:
* Merkle tree (binary, without domain separation)
* Twisted Edwards curve arithmetic (for bn256 and bls381)
* Signature (eddsa aglorithm, following https://tools.ietf.org/html/rfc8032)
* Groth16 verifier (1 layer recursive SNARK with BW761)

## Benchmarks

Expand Down
125 changes: 0 additions & 125 deletions backend/assignment.go

This file was deleted.

30 changes: 0 additions & 30 deletions backend/assignment_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions backend/bls377/fft.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading