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

IDEN-242 integrate rapidsnark to prover-server #6

Merged
merged 16 commits into from
Apr 15, 2022
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
37 changes: 14 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
##
## Build
##
FROM golang:1.16-alpine as base

#ENV GOFLAGS="-mod=vendor"
#ENV CGO_ENABLED=1
# Build prover-server
FROM golang:1.17-alpine as base

WORKDIR /build

Expand All @@ -13,40 +8,34 @@ RUN apk add --no-cache --update git
COPY go.mod ./
COPY go.sum ./
RUN go mod download
#RUN go mod vendor

#COPY . .
COPY ./cmd ./cmd
COPY ./pkg ./pkg

RUN CGO_ENABLED=0 go build -o ./prover ./cmd/prover/prover.go


# Build an prover image
FROM node:14
# Main image
FROM node:16

ENV APP_USER=app
ENV APP_UID=1001
ENV DOCKER_GID=999

RUN apt-get update && apt-get install -y ca-certificates
RUN mkdir -p /home/$APP_USER
RUN adduser -u $APP_UID $APP_USER && chown -R $APP_USER:$APP_USER /home/$APP_USER
RUN addgroup --system --gid ${DOCKER_GID} docker
RUN addgroup ${APP_USER} docker
RUN rm rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*

RUN adduser -u $APP_UID $APP_USER --disabled-password --gecos "First Last,RoomNumber,WorkPhone,HomePhone"
OBrezhniev marked this conversation as resolved.
Show resolved Hide resolved

ENV NPM_CONFIG_PREFIX=/home/app/node/.npm-global
RUN npm install -g circom@latest
RUN npm install -g snarkjs@latest

ENV PATH=${PATH}:/home/app/node/.npm-global/bin

COPY ./configs /home/app/configs
COPY ./circuits /home/app/circuits
COPY ./js /home/app/js
COPY --from=base /build/prover /home/app/prover
COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
#COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

COPY ./configs /home/app/configs
COPY ./circuits /home/app/circuits
COPY ./js /home/app/js

RUN chown -R $APP_USER:$APP_USER /home/app

Expand All @@ -55,3 +44,5 @@ WORKDIR /home/app

# Command to run
ENTRYPOINT ["/home/app/prover"]

EXPOSE 8002
67 changes: 67 additions & 0 deletions Dockerfile-rapidsnark
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Build prover-server
FROM golang:1.17-alpine as base

WORKDIR /build

RUN apk add --no-cache --update git

COPY go.mod ./
COPY go.sum ./
RUN go mod download

COPY ./cmd ./cmd
COPY ./pkg ./pkg

RUN CGO_ENABLED=0 go build -o ./prover ./cmd/prover/prover.go


# Build rapidsnark
FROM node:16 as rapidsnark

RUN apt-get update && apt-get install -y build-essential libgmp-dev libsodium-dev nasm git && rm -rf /var/lib/apt/lists/*

# rapidsnark
RUN mkdir /rapidsnark
WORKDIR /rapidsnark
RUN git clone https://github.com/iden3/rapidsnark.git ./
RUN npm install
RUN git submodule init
RUN git submodule update
RUN npx task createFieldSources
RUN npx task buildProver


# Main image
FROM node:16

ENV APP_USER=app
ENV APP_UID=1001

RUN apt-get update && apt-get install -y ca-certificates libsodium-dev && rm -rf /var/lib/apt/lists/*

RUN adduser -u $APP_UID $APP_USER --disabled-password --gecos "First Last,RoomNumber,WorkPhone,HomePhone"

ENV NPM_CONFIG_PREFIX=/home/app/node/.npm-global
RUN npm install -g snarkjs@latest

ENV PATH=${PATH}:/home/app/node/.npm-global/bin

COPY --from=base /build/prover /home/app/prover
#COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

# rapidsnark
COPY --from=rapidsnark /rapidsnark/build/prover /usr/bin/rapidsnark

COPY ./configs/prover-rapidsnark.yaml /home/app/configs/prover.yaml
COPY ./circuits /home/app/circuits
COPY ./js /home/app/js

RUN chown -R $APP_USER:$APP_USER /home/app

USER app:app
WORKDIR /home/app

# Command to run
ENTRYPOINT ["/home/app/prover"]

EXPOSE 8002
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ Content-Type: application/json
}
```

## Docker images

Build and run container with snarkjs prover:
```bash
docker build -t prover-server .
docker run -it -p 8002:8002 prover-server
```

Build and run container with rapidsnark prover:
```bash
docker build -t prover-server-rapidsnark -f Dockerfile-rapidsnark .
docker run -it -p 8002:8002 prover-server-rapidsnark
```

## License

Expand Down
1 change: 1 addition & 0 deletions circuits/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*
5 changes: 3 additions & 2 deletions cmd/prover/prover.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package main

import (
"os"

"github.com/iden3/prover-server/pkg/app"
"github.com/iden3/prover-server/pkg/app/configs"
"github.com/iden3/prover-server/pkg/app/handlers"
"github.com/iden3/prover-server/pkg/log"
"os"
)

func main() {

config, err := configs.ReadConfigFromFile("prover")
if err != nil {
log.Errorw("cannot read issuer config storage", err)
log.Errorw("cannot read prover config storage", err)
os.Exit(1)
}

Expand Down
10 changes: 10 additions & 0 deletions configs/prover-rapidsnark.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Server configuration
server:
host: "localhost"
port: 8002
# Config options for prover
prover:
circuitsBasePath: "circuits"
useRapidsnark: true
log:
level: "debug"
1 change: 1 addition & 0 deletions configs/prover.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ server:
# Config options for prover
prover:
circuitsBasePath: "circuits"
useRapidsnark: false
log:
level: "debug"
25 changes: 22 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
module github.com/iden3/prover-server

go 1.14
go 1.17

require (
github.com/go-chi/chi v1.5.4
github.com/go-chi/cors v1.2.0
github.com/go-chi/render v1.0.1
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pkg/errors v0.9.1
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.19.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.3 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.19.1
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.3 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,11 @@ golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -295,11 +293,9 @@ golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
Expand Down
1 change: 1 addition & 0 deletions pkg/app/configs/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Config struct {
// ProverConfig contains only base path to circuits folder
type ProverConfig struct {
CircuitsBasePath string `mapstructure:"circuitsBasePath"`
UseRapidsnark bool `mapstructure:"useRapidsnark"`
}

// ReadConfigFromFile parse config file
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/handlers/zk.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (h *ZKHandler) GenerateProof(w http.ResponseWriter, r *http.Request) {
return
}

fullProof, err := proof.GenerateZkProof(r.Context(), circuitPath, req.Inputs)
fullProof, err := proof.GenerateZkProof(r.Context(), circuitPath, req.Inputs, h.ProverConfig.UseRapidsnark)

if err != nil {
rest.ErrorJSON(w, r, http.StatusInternalServerError, err, "can't generate proof", 0)
Expand Down
19 changes: 15 additions & 4 deletions pkg/proof/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/iden3/prover-server/pkg/log"
"github.com/pkg/errors"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"

"github.com/iden3/prover-server/pkg/log"
"github.com/pkg/errors"
)

// ZKInputs are inputs for proof generation
Expand All @@ -31,7 +32,7 @@ type FullProof struct {
}

// GenerateZkProof executes snarkjs groth16prove function and returns proof only if it's valid
func GenerateZkProof(ctx context.Context, circuitPath string, inputs ZKInputs) (*FullProof, error) {
func GenerateZkProof(ctx context.Context, circuitPath string, inputs ZKInputs, useRapidsnark bool) (*FullProof, error) {

if path.Clean(circuitPath) != circuitPath {
return nil, fmt.Errorf("illegal circuitPath")
Expand Down Expand Up @@ -103,7 +104,17 @@ func GenerateZkProof(ctx context.Context, circuitPath string, inputs ZKInputs) (
}

// generate proof
proveCmd := exec.Command("snarkjs", "groth16", "prove", circuitPath+"/circuit_final.zkey", wtnsFile.Name(), proofFile.Name(), publicFile.Name())
var execCommandName string
var execCommandParams []string
if useRapidsnark {
execCommandName = "rapidsnark"
} else {
execCommandName = "snarkjs"
execCommandParams = append(execCommandParams, "groth16", "prove")
}
execCommandParams = append(execCommandParams, circuitPath+"/circuit_final.zkey", wtnsFile.Name(), proofFile.Name(), publicFile.Name())
proveCmd := exec.Command(execCommandName, execCommandParams...)
log.WithContext(ctx).Debugf("used prover: %s", execCommandName)
OBrezhniev marked this conversation as resolved.
Show resolved Hide resolved
proveOut, err := proveCmd.CombinedOutput()
if err != nil {
log.WithContext(ctx).Errorw("failed to generate proof", "proveOut", string(proveOut))
Expand Down
2 changes: 1 addition & 1 deletion pkg/proof/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestGenerateZkProof(t *testing.T) {
inputs["claimsTreeRoot"] = "11255515925828835914226833076080606304832663418205815330307466449501910557227"
inputs["newIdState"] = "7004464227089968436898203591134773465254478000560982809053917599849148691274"

proof, err := GenerateZkProof(context.TODO(), "../../circuits/idState", inputs)
proof, err := GenerateZkProof(context.TODO(), "../../circuits/idState", inputs, false, "")
require.Empty(t, err)

proofJSON, _ := json.Marshal(proof)
Expand Down