Browse Source

Add CLI

ed255-patch-1
arnaucube 4 years ago
parent
commit
3f5f8e2318
4 changed files with 191 additions and 12 deletions
  1. +2
    -0
      .github/workflows/main.yml
  2. +1
    -1
      .gitignore
  3. +61
    -11
      README.md
  4. +127
    -0
      cli/cli.go

+ 2
- 0
.github/workflows/main.yml

@ -27,4 +27,6 @@ jobs:
- name: Compile circuits and execute Go tests
run: |
sh ./compile-circuits.sh
go run cli/cli.go -prove -provingkey=testdata/small/proving_key.json -witness=testdata/small/witness.json -proof=testdata/small/proof.json -public=testdata/small/public.json
go run cli/cli.go -prove -provingkey=testdata/big/proving_key.json -witness=testdata/big/witness.json -proof=testdata/big/proof.json -public=testdata/big/public.json
go test ./...

+ 1
- 1
.gitignore

@ -4,4 +4,4 @@ testdata/*/*.cpp
testdata/*/*.sym
testdata/*/*.r1cs
!testdata/*/input.json
cli/*.json

+ 61
- 11
README.md

@ -7,26 +7,76 @@ Using [bn256](https://github.com/ethereum/go-ethereum/tree/master/crypto/bn256/c
### Example
- Generate Proof
```go
// read ProvingKey & Witness files
provingKeyJson, _ := ioutil.ReadFile("testdata/provingkey.json")
witnessJson, _ := ioutil.ReadFile("testdata/witness.json")
provingKeyJson, _ := ioutil.ReadFile("../testdata/small/proving_key.json")
witnessJson, _ := ioutil.ReadFile("../testdata/small/witness.json")
// parse Proving Key
pk, _ := circomprover.ParseProvingKey(provingKeyJson)
pk, _ := parsers.ParsePk(provingKeyJson)
// parse Witness
w, _ := circomprover.ParseWitness(witnessJson)
w, _ := parsers.ParseWitness(witnessJson)
// generate the proof
proof, pubSignals, err := circomprover.GenerateProof(pk, w)
assert.Nil(t, err)
proofStr, err := circomprover.ProofToString(proof)
assert.Nil(t, err)
publicStr, err := json.Marshal(circomprover.ArrayBigIntToString(pubSignals)
assert.Nil(t, err)
proof, pubSignals, _ := GenerateProof(pk, w)
// print proof & publicSignals
proofStr, _ := parsers.ProofToJson(proof)
publicStr, _ := json.Marshal(parsers.ArrayBigIntToString(pubSignals))
fmt.Println(proofStr)
fmt.Println(publicStr)
```
- Verify Proof
```go
// read proof & verificationKey & publicSignals
proofJson, _ := ioutil.ReadFile("../testdata/big/proof.json")
vkJson, _ := ioutil.ReadFile("../testdata/big/verification_key.json")
publicJson, _ := ioutil.ReadFile("../testdata/big/public.json")
// parse proof & verificationKey & publicSignals
public, _ := parsers.ParsePublicSignals(publicJson)
proof, _ := parsers.ParseProof(proofJson)
vk, _ := parsers.ParseVk(vkJson)
// verify the proof with the given verificationKey & publicSignals
v := Verify(vk, proof, public)
fmt.Println(v)
```
## CLI
From the `cli` directory:
- Show options
```
> go run cli.go -help
go-circom-prover-verifier
v0.0.1
Usage of /tmp/go-build620318239/b001/exe/cli:
-proof string
proof path (default "proof.json")
-prove
prover mode
-provingkey string
provingKey path (default "proving_key.json")
-public string
public signals path (default "public.json")
-verificationkey string
verificationKey path (default "verification_key.json")
-verify
verifier mode
-witness string
witness path (default "witness.json")
```
- Prove
```
> go run cli.go -prove -provingkey=../testdata/small/proving_key.json -witness=../testdata/small/witness.json
```
- Verify
```
> go run cli.go -verify -verificationkey=../testdata/small/verification_key.json
```

+ 127
- 0
cli/cli.go

@ -0,0 +1,127 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"github.com/iden3/go-circom-prover-verifier/parsers"
"github.com/iden3/go-circom-prover-verifier/prover"
"github.com/iden3/go-circom-prover-verifier/verifier"
)
const version = "v0.0.1"
func main() {
fmt.Println("go-circom-prover-verifier")
fmt.Println(" ", version)
prove := flag.Bool("prove", false, "prover mode")
verify := flag.Bool("verify", false, "verifier mode")
provingKeyPath := flag.String("provingkey", "proving_key.json", "provingKey path")
witnessPath := flag.String("witness", "witness.json", "witness path")
proofPath := flag.String("proof", "proof.json", "proof path")
verificationKeyPath := flag.String("verificationkey", "verification_key.json", "verificationKey path")
publicPath := flag.String("public", "public.json", "public signals path")
flag.Parse()
if *prove {
err := cmdProve(*provingKeyPath, *witnessPath, *proofPath, *publicPath)
if err != nil {
fmt.Println("Error:", err)
}
return
} else if *verify {
err := cmdVerify(*proofPath, *verificationKeyPath, *publicPath)
if err != nil {
fmt.Println("Error:", err)
}
return
}
fmt.Println("use -help for the list of commands")
}
func cmdProve(provingKeyPath, witnessPath, proofPath, publicPath string) error {
fmt.Println("zkSNARK Groth16 prover")
provingKeyJson, err := ioutil.ReadFile(provingKeyPath)
if err != nil {
return err
}
pk, err := parsers.ParsePk(provingKeyJson)
if err != nil {
return err
}
witnessJson, err := ioutil.ReadFile(witnessPath)
if err != nil {
return err
}
w, err := parsers.ParseWitness(witnessJson)
if err != nil {
return err
}
proof, pubSignals, err := prover.GenerateProof(pk, w)
if err != nil {
return err
}
proofStr, err := parsers.ProofToJson(proof)
if err != nil {
return err
}
// write output
err = ioutil.WriteFile(proofPath, proofStr, 0644)
if err != nil {
return err
}
publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals))
if err != nil {
return err
}
err = ioutil.WriteFile(publicPath, publicStr, 0644)
if err != nil {
return err
}
fmt.Println("Proof stored at:", proofPath)
fmt.Println("PublicSignals stored at:", publicPath)
return nil
}
func cmdVerify(proofPath, verificationKeyPath, publicPath string) error {
fmt.Println("zkSNARK Groth16 verifier")
proofJson, err := ioutil.ReadFile(proofPath)
if err != nil {
return err
}
vkJson, err := ioutil.ReadFile(verificationKeyPath)
if err != nil {
return err
}
publicJson, err := ioutil.ReadFile(publicPath)
if err != nil {
return err
}
public, err := parsers.ParsePublicSignals(publicJson)
if err != nil {
return err
}
proof, err := parsers.ParseProof(proofJson)
if err != nil {
return err
}
vk, err := parsers.ParseVk(vkJson)
if err != nil {
return err
}
v := verifier.Verify(vk, proof, public)
fmt.Println("verification:", v)
return nil
}

Loading…
Cancel
Save