diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7760594..f8fc68e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,6 +23,6 @@ jobs: - name: Compile circuits and execute Go tests run: | cd testdata && sh ./compile-circuits.sh && cd .. - go run cli/cli.go -prove -provingkey=testdata/circuit1k/proving_key.json -witness=testdata/circuit1k/witness.json -proof=testdata/circuit1k/proof.json -public=testdata/circuit1k/public.json - go run cli/cli.go -prove -provingkey=testdata/circuit5k/proving_key.json -witness=testdata/circuit5k/witness.json -proof=testdata/circuit5k/proof.json -public=testdata/circuit5k/public.json + go run cli/cli.go -prove -pk=testdata/circuit1k/proving_key.json -witness=testdata/circuit1k/witness.json -proof=testdata/circuit1k/proof.json -public=testdata/circuit1k/public.json + go run cli/cli.go -prove -pk=testdata/circuit5k/proving_key.json -witness=testdata/circuit5k/witness.json -proof=testdata/circuit5k/proof.json -public=testdata/circuit5k/public.json go test ./... diff --git a/cli/cli.go b/cli/cli.go index c4723e6..8c2b88a 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -21,12 +21,14 @@ func main() { prove := flag.Bool("prove", false, "prover mode") verify := flag.Bool("verify", false, "verifier mode") + convert := flag.Bool("convert", false, "convert mode, to convert between proving_key.json to proving_key.go.bin") - provingKeyPath := flag.String("provingkey", "proving_key.json", "provingKey path") + provingKeyPath := flag.String("pk", "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") + verificationKeyPath := flag.String("vk", "verification_key.json", "verificationKey path") publicPath := flag.String("public", "public.json", "public signals path") + provingKeyBinPath := flag.String("pkbin", "proving_key.go.bin", "provingKey Bin path") flag.Parse() @@ -42,6 +44,12 @@ func main() { fmt.Println("Error:", err) } os.Exit(0) + } else if *convert { + err := cmdConvert(*provingKeyPath, *provingKeyBinPath) + if err != nil { + fmt.Println("Error:", err) + } + os.Exit(0) } flag.PrintDefaults() } @@ -133,3 +141,25 @@ func cmdVerify(proofPath, verificationKeyPath, publicPath string) error { fmt.Println("verification:", v) return nil } + +func cmdConvert(provingKeyPath, provingKeyBinPath string) error { + fmt.Println("Convertion tool") + + provingKeyJson, err := ioutil.ReadFile(provingKeyPath) + if err != nil { + return err + } + pk, err := parsers.ParsePk(provingKeyJson) + if err != nil { + return err + } + + fmt.Printf("Converting proving key json (%s)\nto go proving key binary (%s)\n", provingKeyPath, provingKeyBinPath) + pkGBin, err := parsers.PkToGoBin(pk) + if err != nil { + return err + } + err = ioutil.WriteFile(provingKeyBinPath, pkGBin, 0644) + + return nil +} diff --git a/parsers/parsers_test.go b/parsers/parsers_test.go index bba5869..96df2f8 100644 --- a/parsers/parsers_test.go +++ b/parsers/parsers_test.go @@ -253,11 +253,11 @@ func testGoCircomPkFormat(t *testing.T, circuit string) { pkGBin, err := PkToGoBin(pk) require.Nil(t, err) - err = ioutil.WriteFile("../testdata/"+circuit+"/proving_key_go.bin", pkGBin, 0644) + err = ioutil.WriteFile("../testdata/"+circuit+"/proving_key.go.bin", pkGBin, 0644) assert.Nil(t, err) // parse ProvingKeyGo - pkGoBinFile, err := os.Open("../testdata/" + circuit + "/proving_key_go.bin") + pkGoBinFile, err := os.Open("../testdata/" + circuit + "/proving_key.go.bin") require.Nil(t, err) defer pkGoBinFile.Close() pkG, err := ParsePkGoBin(pkGoBinFile) @@ -291,7 +291,7 @@ func benchmarkParsePk(b *testing.B, circuit string) { require.Nil(b, err) defer pkBinFile.Close() - pkGoBinFile, err := os.Open("../testdata/" + circuit + "/proving_key_go.bin") + pkGoBinFile, err := os.Open("../testdata/" + circuit + "/proving_key.go.bin") require.Nil(b, err) defer pkGoBinFile.Close() diff --git a/prover/prover_test.go b/prover/prover_test.go index e7fed45..4e20784 100644 --- a/prover/prover_test.go +++ b/prover/prover_test.go @@ -22,7 +22,7 @@ func TestCircuitsGenerateProof(t *testing.T) { } func testCircuitGenerateProof(t *testing.T, circuit string) { - // using json provingKey file + // Using json provingKey file: // provingKeyJson, err := ioutil.ReadFile("../testdata/" + circuit + "/proving_key.json") // require.Nil(t, err) // pk, err := parsers.ParsePk(provingKeyJson) @@ -32,11 +32,18 @@ func testCircuitGenerateProof(t *testing.T, circuit string) { // w, err := parsers.ParseWitness(witnessJson) // require.Nil(t, err) - // using bin provingKey file - pkBinFile, err := os.Open("../testdata/" + circuit + "/proving_key.bin") + // Using bin provingKey file: + // pkBinFile, err := os.Open("../testdata/" + circuit + "/proving_key.bin") + // require.Nil(t, err) + // defer pkBinFile.Close() + // pk, err := parsers.ParsePkBin(pkBinFile) + // require.Nil(t, err) + + // Using go bin provingKey file: + pkGoBinFile, err := os.Open("../testdata/" + circuit + "/proving_key.go.bin") require.Nil(t, err) - defer pkBinFile.Close() - pk, err := parsers.ParsePkBin(pkBinFile) + defer pkGoBinFile.Close() + pk, err := parsers.ParsePkGoBin(pkGoBinFile) require.Nil(t, err) witnessBinFile, err := os.Open("../testdata/" + circuit + "/witness.bin") diff --git a/testdata/clean-gereated-files.sh b/testdata/clean-gereated-files.sh index db667be..79e1efb 100755 --- a/testdata/clean-gereated-files.sh +++ b/testdata/clean-gereated-files.sh @@ -7,3 +7,4 @@ rm */*.cpp rm */*.sym rm */*.r1cs rm */*.sol +rm */*.bin diff --git a/testdata/compile-circuits.sh b/testdata/compile-circuits.sh index 1199a8f..5f24ce6 100755 --- a/testdata/compile-circuits.sh +++ b/testdata/compile-circuits.sh @@ -45,19 +45,23 @@ compile_and_ts_and_witness cd ../ -echo "convert witness & pk of circuit1k to bin" +echo "convert witness & pk of circuit1k to bin & go bin" node node_modules/wasmsnark/tools/buildwitness.js -i circuit1k/witness.json -o circuit1k/witness.bin node node_modules/wasmsnark/tools/buildpkey.js -i circuit1k/proving_key.json -o circuit1k/proving_key.bin +go run ../cli/cli.go -convert -pk circuit1k/proving_key.json -pkbin circuit1k/proving_key.go.bin -echo "convert witness & pk of circuit5k to bin" +echo "convert witness & pk of circuit5k to bin & go bin" node node_modules/wasmsnark/tools/buildwitness.js -i circuit5k/witness.json -o circuit5k/witness.bin node node_modules/wasmsnark/tools/buildpkey.js -i circuit5k/proving_key.json -o circuit5k/proving_key.bin +go run ../cli/cli.go -convert -pk circuit5k/proving_key.json -pkbin circuit5k/proving_key.go.bin -# echo "convert witness & pk of circuit10k to bin" +# echo "convert witness & pk of circuit10k to bin & go bin" # node node_modules/wasmsnark/tools/buildwitness.js -i circuit10k/witness.json -o circuit10k/witness.bin # node node_modules/wasmsnark/tools/buildpkey.js -i circuit10k/proving_key.json -o circuit10k/proving_key.bin +# go run ../cli/cli.go -convert -pk circuit10k/proving_key.json -pkbin circuit10k/proving_key.go.bin # -# echo "convert witness & pk of circuit20k to bin" +# echo "convert witness & pk of circuit20k to bin & go bin" # node node_modules/wasmsnark/tools/buildwitness.js -i circuit20k/witness.json -o circuit20k/witness.bin # node node_modules/wasmsnark/tools/buildpkey.js -i circuit20k/proving_key.json -o circuit20k/proving_key.bin +# go run ../cli/cli.go -convert -pk circuit20k/proving_key.json -pkbin circuit20k/proving_key.go.bin