diff --git a/.gitignore b/.gitignore index 21807d4..dc786ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.Backup cli/compiledcircuit.json -cli/inputs.json +cli/privateInputs.json +cli/publicInputs.json cli/proofs.json cli/test.circuit cli/trustedsetup.json diff --git a/README.md b/README.md index 619aae8..c4c46c7 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,63 @@ Current implementation status: - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/r1csqap?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/r1csqap) R1CS to QAP (more details: https://github.com/arnaucube/go-snark/tree/master/r1csqap) - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler +### CLI usage + +#### Compile circuit +Having a circuit file `test.circuit`: +``` +func test(private s0, public s1): + s2 = s0 * s0 + s3 = s2 * s0 + s4 = s3 + s0 + s5 = s4 + 5 + equals(s1, s5) + out = 1 * 1 +``` +And a private inputs file `privateInputs.json` +``` +[ + 3 +] +``` +And a public inputs file `publicInputs.json` +``` +[ + 35 +] +``` + +In the command line, execute: +``` +> ./go-snark-cli compile test.circuit +``` + +This will output the `compiledcircuit.json` file. + +#### Trusted Setup +Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`: +``` +> ./go-snark-cli trustedsetup +``` +This will create the file `trustedsetup.json` with the TrustedSetup data, and also a `toxic.json` file, with the parameters to delete from the `Trusted Setup`. + + +#### Generate Proofs +Assumming that we have the `compiledcircuit.json`, `trustedsetup.json`, `privateInputs.json` and the `publicInputs.json` we can now generate the `Proofs` with the following command: +``` +> ./go-snark-cli genproofs +``` + +This will store the file `proofs.json`, that contains all the SNARK proofs. + +#### Verify Proofs +Having the `proofs.json`, `compiledcircuit.json`, `trustedsetup.json` `publicInputs.json` files, we can now verify the `Pairings` of the proofs, in order to verify the proofs. +``` +> ./go-snark-cli verify +``` +This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified. + + ### Library usage Warning: not finished. @@ -101,62 +158,6 @@ publicSignalsVerif := []*big.Int{b35Verif} assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true)) ``` -### CLI usage - -#### Compile circuit -Having a circuit file `test.circuit`: -``` -func test(private s0, public s1): - s2 = s0 * s0 - s3 = s2 * s0 - s4 = s3 + s0 - s5 = s4 + 5 - equals(s1, s5) - out = 1 * 1 -``` -And a private inputs file `privateInputs.json` -``` -[ - 3 -] -``` -And a public inputs file `publicInputs.json` -``` -[ - 35 -] -``` - -In the command line, execute: -``` -> go-snark-cli compile test.circuit -``` - -This will output the `compiledcircuit.json` file. - -#### Trusted Setup -Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`: -``` -> go-snark-cli trustedsetup -``` -This will create the file `trustedsetup.json` with the TrustedSetup data, and also a `toxic.json` file, with the parameters to delete from the `Trusted Setup`. - - -#### Generate Proofs -Assumming that we have the `compiledcircuit.json`, `trustedsetup.json`, `privateInputs.json` and the `publicInputs.json` we can now generate the `Proofs` with the following command: -``` -> go-snark-cli genproofs -``` - -This will store the file `proofs.json`, that contains all the SNARK proofs. - -#### Verify Proofs -Having the `proofs.json`, `compiledcircuit.json`, `trustedsetup.json` `publicInputs.json` files, we can now verify the `Pairings` of the proofs, in order to verify the proofs. -``` -> go-snark-cli verify -``` -This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified. - ## Test diff --git a/build-cli.sh b/build-cli.sh new file mode 100644 index 0000000..daef039 --- /dev/null +++ b/build-cli.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +cd cli && go build +mv ./cli ../go-snark-cli diff --git a/cli/main.go b/cli/main.go index 8d4b5b0..0777b46 100644 --- a/cli/main.go +++ b/cli/main.go @@ -53,7 +53,7 @@ var commands = []cli.Command{ func main() { app := cli.NewApp() app.Name = "go-snarks-cli" - app.Version = "0.1.0-alpha" + app.Version = "0.0.1-alpha" app.Flags = []cli.Flag{ cli.StringFlag{Name: "config"}, } @@ -170,14 +170,20 @@ func TrustedSetup(context *cli.Context) error { json.Unmarshal([]byte(string(compiledcircuitFile)), &circuit) panicErr(err) - // read inputs file - inputsFile, err := ioutil.ReadFile("inputs.json") + // read privateInputs file + privateInputsFile, err := ioutil.ReadFile("privateInputs.json") panicErr(err) + // read publicInputs file + publicInputsFile, err := ioutil.ReadFile("publicInputs.json") + panicErr(err) + // parse inputs from inputsFile - // var inputs []*big.Int var inputs circuitcompiler.Inputs - json.Unmarshal([]byte(string(inputsFile)), &inputs) + err = json.Unmarshal([]byte(string(privateInputsFile)), &inputs.Private) panicErr(err) + err = json.Unmarshal([]byte(string(publicInputsFile)), &inputs.Public) + panicErr(err) + // calculate wittness w, err := circuit.CalculateWitness(inputs.Private, inputs.Public) panicErr(err) @@ -245,23 +251,22 @@ func GenerateProofs(context *cli.Context) error { // calculate wittness w, err := circuit.CalculateWitness(inputs.Private, inputs.Public) panicErr(err) - fmt.Println("\nwitness", w) + fmt.Println("witness", w) // flat code to R1CS - // a, b, c := circuit.GenerateR1CS() a := circuit.R1CS.A b := circuit.R1CS.B c := circuit.R1CS.C // R1CS to QAP - alphas, betas, gammas, zx := snark.Utils.PF.R1CSToQAP(a, b, c) + alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c) _, _, _, px := snark.Utils.PF.CombinePolynomials(w, alphas, betas, gammas) - hx := snark.Utils.PF.DivisorPolynomial(px, zx) + hx := snark.Utils.PF.DivisorPolynomial(px, trustedsetup.Pk.Z) fmt.Println(circuit) fmt.Println(trustedsetup.G1T) fmt.Println(hx) fmt.Println(w) - proof, err := snark.GenerateProofs(circuit, trustedsetup, hx, w) + proof, err := snark.GenerateProofs(circuit, trustedsetup, w, px) panicErr(err) fmt.Println("\n proofs:") diff --git a/go-snark-cli b/go-snark-cli new file mode 100755 index 0000000..cc5e06b Binary files /dev/null and b/go-snark-cli differ