Skip to content

Commit

Permalink
add plonk backend (#50)
Browse files Browse the repository at this point in the history
* add plonk backend

* update docker deploy workflow

* update book deploy workflow
  • Loading branch information
dB2510 authored Feb 29, 2024
1 parent 40c86d4 commit 437f0a2
Show file tree
Hide file tree
Showing 24 changed files with 528 additions and 161 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ name: book
on:
push:
branches: [main]
pull_request:
branches: [main]
merge_group:

jobs:
# test:
Expand Down
10 changes: 0 additions & 10 deletions .github/workflows/build-push-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
#on:
# push:
# branches:
# - *
# tags:
# - 'v*'

on:
push:
branches: [main]
pull_request:
branches: [main]
merge_group:

name: Build and Publish Docker Image
jobs:
Expand Down
19 changes: 12 additions & 7 deletions book/perf/mbp/crop.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
## Crop
| Original Size | Final Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) |
|---|---|---|---|---|
| 1000x1000 | 10x10 | 0.958040 | 16.992575 | 164 |
| 1000x1000 | 100x100 | 0.935300 | 21.655094 | 164 |
| 1000x1000 | 250x250 | 1.041068 | 33.970356 | 164 |
| 1000x1000 | 500x500 | 1.775267 | 65.551002 | 164 |
| 1000x1000 | 750x750 | 2.288349 | 111.907515 | 164 |
| Original Size | Final Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) | Backend |
|---|---|---|---|---|---|
| 1000x1000 | 10x10 | 0.833237 | 15.970879 | 164 | groth16 |
| 1000x1000 | 100x100 | 0.807472 | 19.188925 | 164 | groth16 |
| 1000x1000 | 250x250 | 0.885950 | 31.114879 | 164 | groth16 |
| 1000x1000 | 500x500 | 1.166375 | 58.741483 | 164 | groth16 |
| 1000x1000 | 750x750 | 1.584546 | 103.079183 | 164 | groth16 |
| 1000x1000 | 10x10 | 0.790772 | 1.330478 | 552 | plonk |
| 1000x1000 | 100x100 | 0.745351 | 6.189850 | 552 | plonk |
| 1000x1000 | 250x250 | 0.826726 | 47.114148 | 552 | plonk |
| 1000x1000 | 500x500 | 1.086315 | 204.406259 | 552 | plonk |
| 1000x1000 | 750x750 | 2.057538 | 420.913409 | 552 | plonk |
19 changes: 12 additions & 7 deletions book/perf/mbp/flip-horizontal.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
## Flip horizontal
| Original Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) |
|---|---|---|---|
| 10x10 | 0.001320 | 0.030627 | 164 |
| 100x100 | 0.078610 | 1.834790 | 164 |
| 250x250 | 0.238217 | 11.219355 | 164 |
| 500x500 | 0.600707 | 45.394261 | 164 |
| 750x750 | 1.425706 | 102.858330 | 164 |
| Original Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) | Backend |
|---|---|---|---|---|
| 10x10 | 0.000339 | 0.032807 | 164 | groth16 |
| 100x100 | 0.025165 | 1.779391 | 164 | groth16 |
| 250x250 | 0.151705 | 11.252345 | 164 | groth16 |
| 500x500 | 0.573539 | 44.454817 | 164 | groth16 |
| 750x750 | 1.286226 | 97.416250 | 164 | groth16 |
| 10x10 | 0.000364 | 0.209765 | 552 | plonk |
| 100x100 | 0.024890 | 5.139028 | 552 | plonk |
| 250x250 | 0.129589 | 45.555273 | 552 | plonk |
| 500x500 | 0.529516 | 196.448008 | 552 | plonk |
| 750x750 | 1.277747 | 418.509681 | 552 | plonk |
18 changes: 12 additions & 6 deletions book/perf/mbp/flip-vertical.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
## Flip vertical
| Original Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) |
|---|---|---|---|
| 10x10 | 0.000803 | 0.032929 | 164 |
| 100x100 | 0.032572 | 1.919375 | 164 |
| 250x250 | 0.189386 | 11.282747 | 164 |
| 500x500 | 0.615048 | 45.408805 | 164 |
| Original Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) | Backend |
|---|---|---|---|---|
| 10x10 | 0.000438 | 0.032108 | 164 | groth16 |
| 100x100 | 0.025263 | 1.780565 | 164 | groth16 |
| 250x250 | 0.151733 | 11.315902 | 164 | groth16 |
| 500x500 | 0.577561 | 44.370405 | 164 | groth16 |
| 750x750 | 1.271395 | 98.629211 | 164 | groth16 |
| 10x10 | 0.000337 | 0.212584 | 552 | plonk |
| 100x100 | 0.023697 | 5.108784 | 552 | plonk |
| 250x250 | 0.128550 | 46.555842 | 552 | plonk |
| 500x500 | 0.516148 | 196.040625 | 552 | plonk |
| 750x750 | 1.302722 | 423.434726 | 552 | plonk |
19 changes: 12 additions & 7 deletions book/perf/mbp/rotate180.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
## Rotate 180
| Original Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) |
|---|---|---|---|
| 10x10 | 0.000402 | 0.002164 | 164 |
| 100x100 | 0.025682 | 0.057075 | 164 |
| 250x250 | 0.160375 | 0.404695 | 164 |
| 500x500 | 0.639341 | 1.382990 | 164 |
| 750x750 | 1.454608 | 2.622660 | 164 |
| Original Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) | Backend |
|---|---|---|---|---|
| 10x10 | 0.000377 | 0.032683 | 164 | groth16|
| 100x100 | 0.025277 | 1.788267 | 164 | groth16|
| 250x250 | 0.153462 | 11.111391 | 164 | groth16|
| 500x500 | 0.580021 | 45.384939 | 164 | groth16|
| 750x750 | 1.331276 | 94.734796 | 164 | groth16|
| 10x10 | 0.000338 | 0.199782 | 552 | plonk|
| 100x100 | 0.022096 | 5.186410 | 552 | plonk|
| 250x250 | 0.130622 | 45.474365 | 552 | plonk|
| 500x500 | 0.513307 | 194.285966 | 552 | plonk|
| 750x750 | 1.142496 | 425.788482 | 552 | plonk|
19 changes: 12 additions & 7 deletions book/perf/mbp/rotate270.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
## Rotate 270
| Original Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) |
|---|---|---|---|
| 10x10 | 0.000381 | 0.043469 | 164 |
| 100x100 | 0.027019 | 2.001985 | 164 |
| 250x250 | 0.163741 | 11.755546 | 164 |
| 500x500 | 0.638988 | 52.032664 | 164 |
| 750x750 | 1.689164 | 104.419625 | 164 |
| Original Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) | Backend |
|---|---|---|---|---|
| 10x10 | 0.000451 | 0.033596 | 164 | groth16 |
| 100x100 | 0.025271 | 1.744991 | 164 | groth16 |
| 250x250 | 0.151591 | 11.086174 | 164 | groth16 |
| 500x500 | 0.594607 | 43.656017 | 164 | groth16 |
| 750x750 | 1.334827 | 96.234792 | 164 | groth16 |
| 10x10 | 0.000254 | 0.205031 | 552 | plonk |
| 100x100 | 0.020022 | 5.238268 | 552 | plonk |
| 250x250 | 0.131477 | 45.467208 | 552 | plonk |
| 500x500 | 0.511738 | 194.538589 | 552 | plonk |
| 750x750 | 1.168737 | 409.672420 | 552 | plonk |
19 changes: 12 additions & 7 deletions book/perf/mbp/rotate90.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
## Rotate 90
| Original Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) |
|---|---|---|---|
| 10x10 | 0.000647 | 0.036080 | 164 |
| 100x100 | 0.045029 | 1.777236 | 164 |
| 250x250 | 0.205750 | 12.221867 | 164 |
| 500x500 | 0.685251 | 46.958753 | 164 |
| 750x750 | 1.645099 | 100.396243 | 164 |
| Original Size | Circuit compilation (s) | Proving time (s) | Proof size (bytes) | Backend |
|---|---|---|---|---|
| 10x10 | 0.000434 | 0.033605 | 164 | groth16 |
| 100x100 | 0.028184 | 1.699871 | 164 | groth16 |
| 250x250 | 0.171229 | 10.918394 | 164 | groth16 |
| 500x500 | 0.647757 | 43.215272 | 164 | groth16 |
| 750x750 | 1.421753 | 94.541931 | 164 | groth16 |
| 10x10 | 0.000247 | 0.200214 | 552 | plonk |
| 100x100 | 0.020475 | 5.128606 | 552 | plonk |
| 250x250 | 0.134876 | 45.082718 | 552 | plonk |
| 500x500 | 0.558636 | 196.608304 | 552 | plonk |
| 750x750 | 1.592956 | 421.920696 | 552 | plonk |
19 changes: 8 additions & 11 deletions cmd/brighten.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/consensys/gnark/std/math/cmp"
"github.com/spf13/cobra"
"io"
"os"
"path"
"time"
Expand All @@ -27,6 +27,7 @@ type brightenConfig struct {
brighteningFactor int // TODO(xenowits): Convert it to floating-point
proofDir string
markdownFile string
backend string
}

// newBrightenCmd returns a new cobra.Command for brightening an image by a brightening factor.
Expand All @@ -51,6 +52,7 @@ func bindBrightenFlags(cmd *cobra.Command, conf *brightenConfig) {
cmd.Flags().StringVar(&conf.finalImg, "final-image", "", "The path to the final image. Supported image formats: PNG.")
cmd.Flags().StringVar(&conf.proofDir, "proof-dir", "", "The path to the proof directory.")
cmd.Flags().IntVar(&conf.brighteningFactor, "brightening-factor", 2, "The factor with which image is brightened.")
cmd.Flags().StringVar(&conf.backend, "backend", "groth16", "The proving backend used for generating the proofs.")
}

// proveBrighten generates the zk proof of brightening an image by a brightening factor.
Expand Down Expand Up @@ -85,7 +87,7 @@ func proveBrighten(config brightenConfig) error {
return err
}

proof, vk, circuitCompilationDuration, provingDuration, err := generateBrightenProof(originalPixels, finalPixels)
proof, vk, circuitCompilationDuration, provingDuration, err := generateBrightenProof(config.backend, originalPixels, finalPixels)
if err != nil {
return err
}
Expand Down Expand Up @@ -141,7 +143,7 @@ func proveBrighten(config brightenConfig) error {
}

// generateBrightenProof returns the zk proof of brightening an image by a brightening factor.
func generateBrightenProof(original, brightened [][][]uint8) (groth16.Proof, groth16.VerifyingKey, time.Duration, time.Duration, error) {
func generateBrightenProof(backend string, original, brightened [][][]uint8) (io.WriterTo, io.WriterTo, time.Duration, time.Duration, error) {
var circuit brightenCircuit
circuit.Original = make([][][]frontend.Variable, len(original)) // First dimension
for i := range original {
Expand All @@ -160,9 +162,9 @@ func generateBrightenProof(original, brightened [][][]uint8) (groth16.Proof, gro
}

t0 := time.Now()
cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
cs, err := compileCircuit(backend, &circuit)
if err != nil {
panic(err)
return nil, nil, 0, 0, err
}

fmt.Println("Brighten compilation time:", time.Since(t0).Seconds())
Expand All @@ -177,12 +179,7 @@ func generateBrightenProof(original, brightened [][][]uint8) (groth16.Proof, gro
return nil, nil, 0, 0, err
}

pk, vk, err := groth16.Setup(cs)
if err != nil {
return nil, nil, 0, 0, err
}

proof, err := groth16.Prove(cs, pk, witness)
proof, vk, err := generateProofByBackend(backend, cs, witness)
if err != nil {
return nil, nil, 0, 0, err
}
Expand Down
62 changes: 61 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package cmd

import (
"errors"
"fmt"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/backend/plonk"
"github.com/consensys/gnark/backend/witness"
"github.com/consensys/gnark/constraint"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/consensys/gnark/frontend/cs/scs"
"github.com/consensys/gnark/test"
"github.com/spf13/cobra"
"io"
)

// New returns a new cobra command that handles stackr aggregator commands and subcommands.
// New returns a new cobra command that handles maya cli commands and subcommands.
func New() *cobra.Command {
return newRootCmd(
newProveCmd(
Expand Down Expand Up @@ -63,3 +75,51 @@ func newVerifyCmd(cmds ...*cobra.Command) *cobra.Command {

return root
}

func compileCircuit(backend string, circuit frontend.Circuit) (constraint.ConstraintSystem, error) {
switch backend {
case "groth16":
return frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, circuit)
case "plonk": // TODO(dhruv): add plonkfri when its serialisation is supported.
return frontend.Compile(ecc.BN254.ScalarField(), scs.NewBuilder, circuit)
default:
return nil, errors.New(fmt.Sprintf("invalid backend, %s", backend))
}
}

func generateProofByBackend(backend string, cs constraint.ConstraintSystem, witness witness.Witness) (io.WriterTo, io.WriterTo, error) {
switch backend {
case "groth16":
pk, vk, err := groth16.Setup(cs)
if err != nil {
return nil, nil, err
}

proof, err := groth16.Prove(cs, pk, witness)
if err != nil {
return nil, nil, err
}

return proof, vk, nil
case "plonk":
// TODO(dhruv): replace this with actual trusted setup ceremony.
kzgSrs, err := test.NewKZGSRS(cs)
if err != nil {
return nil, nil, err
}

pk, vk, err := plonk.Setup(cs, kzgSrs)
if err != nil {
return nil, nil, err
}

proof, err := plonk.Prove(cs, pk, witness)
if err != nil {
return nil, nil, err
}

return proof, vk, nil
default:
return nil, nil, errors.New(fmt.Sprintf("invalid backend, %s", backend))
}
}
21 changes: 9 additions & 12 deletions cmd/crop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/spf13/cobra"
"image/png"
"io"
Expand All @@ -24,6 +23,7 @@ type cropConfig struct {
heightStartNew int
proofDir string
markdownFile string
backend string
}

// newCropCmd returns a new cobra.Command for cropping.
Expand All @@ -49,6 +49,7 @@ func bindFlags(cmd *cobra.Command, conf *cropConfig) {
cmd.Flags().IntVar(&conf.widthStartNew, "width-start-new", 0, "The Original-coordinate for the top-left corner of the cropped image, relative to the original image's width.")
cmd.Flags().IntVar(&conf.heightStartNew, "height-start-new", 0, "The Cropped-coordinate for the top-left corner of the cropped image, relative to the original image's height.")
cmd.Flags().StringVar(&conf.proofDir, "proof-dir", "", "The path to the proof directory.")
cmd.Flags().StringVar(&conf.backend, "backend", "groth16", "The proving backend used for generating the proofs.")
}

// proveCrop generates the zk proof of crop transformation.
Expand Down Expand Up @@ -82,7 +83,7 @@ func proveCrop(config cropConfig) error {
return err
}

proof, vk, circuitCompilationDuration, provingDuration, err := generateProof(finalPixels, originalPixels)
proof, vk, circuitCompilationDuration, provingDuration, err := generateCropProof(config.backend, finalPixels, originalPixels)
if err != nil {
return err
}
Expand All @@ -107,14 +108,15 @@ func proveCrop(config cropConfig) error {
}
defer mdFile.Close()

if _, err = fmt.Fprintf(mdFile, "| %s | %s | %f | %f | %d |\n",
if _, err = fmt.Fprintf(mdFile, "| %s | %s | %f | %f | %d | %s |\n",
fmt.Sprintf("%dx%d", len(originalPixels),
len(originalPixels[0])),
fmt.Sprintf("%dx%d", len(finalPixels),
len(finalPixels[0])),
circuitCompilationDuration.Seconds(),
provingDuration.Seconds(),
n,
config.backend,
); err != nil {
return err
}
Expand Down Expand Up @@ -162,8 +164,8 @@ func convertImgToPixels(file io.Reader) ([][][]uint8, error) {
return pixels, nil
}

// generateProof returns the proof of crop transformation.
func generateProof(cropped, original [][][]uint8) (groth16.Proof, groth16.VerifyingKey, time.Duration, time.Duration, error) {
// generateCropProof returns the proof of crop transformation.
func generateCropProof(backend string, cropped, original [][][]uint8) (io.WriterTo, io.WriterTo, time.Duration, time.Duration, error) {
var circuit CropCircuit
circuit.Original = make([][][]frontend.Variable, len(original)) // First dimension
for i := range original {
Expand All @@ -182,7 +184,7 @@ func generateProof(cropped, original [][][]uint8) (groth16.Proof, groth16.Verify
}

t0 := time.Now()
cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
cs, err := compileCircuit(backend, &circuit)
if err != nil {
panic(err)
}
Expand All @@ -199,12 +201,7 @@ func generateProof(cropped, original [][][]uint8) (groth16.Proof, groth16.Verify
return nil, nil, 0, 0, err
}

pk, vk, err := groth16.Setup(cs)
if err != nil {
return nil, nil, 0, 0, err
}

proof, err := groth16.Prove(cs, pk, witness)
proof, vk, err := generateProofByBackend(backend, cs, witness)
if err != nil {
return nil, nil, 0, 0, err
}
Expand Down
Loading

0 comments on commit 437f0a2

Please sign in to comment.