mirror of
https://github.com/arnaucube/go-snark-study.git
synced 2026-02-02 17:26:41 +01:00
update cli, update readme
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,6 +1,7 @@
|
|||||||
*.Backup
|
*.Backup
|
||||||
cli/compiledcircuit.json
|
cli/compiledcircuit.json
|
||||||
cli/inputs.json
|
cli/privateInputs.json
|
||||||
|
cli/publicInputs.json
|
||||||
cli/proofs.json
|
cli/proofs.json
|
||||||
cli/test.circuit
|
cli/test.circuit
|
||||||
cli/trustedsetup.json
|
cli/trustedsetup.json
|
||||||
|
|||||||
113
README.md
113
README.md
@@ -36,6 +36,63 @@ Current implementation status:
|
|||||||
- [](https://godoc.org/github.com/arnaucube/go-snark/r1csqap) R1CS to QAP (more details: https://github.com/arnaucube/go-snark/tree/master/r1csqap)
|
- [](https://godoc.org/github.com/arnaucube/go-snark/r1csqap) R1CS to QAP (more details: https://github.com/arnaucube/go-snark/tree/master/r1csqap)
|
||||||
- [](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
|
- [](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
|
### Library usage
|
||||||
Warning: not finished.
|
Warning: not finished.
|
||||||
|
|
||||||
@@ -101,62 +158,6 @@ publicSignalsVerif := []*big.Int{b35Verif}
|
|||||||
assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
|
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
|
## Test
|
||||||
|
|||||||
4
build-cli.sh
Normal file
4
build-cli.sh
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
cd cli && go build
|
||||||
|
mv ./cli ../go-snark-cli
|
||||||
25
cli/main.go
25
cli/main.go
@@ -53,7 +53,7 @@ var commands = []cli.Command{
|
|||||||
func main() {
|
func main() {
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Name = "go-snarks-cli"
|
app.Name = "go-snarks-cli"
|
||||||
app.Version = "0.1.0-alpha"
|
app.Version = "0.0.1-alpha"
|
||||||
app.Flags = []cli.Flag{
|
app.Flags = []cli.Flag{
|
||||||
cli.StringFlag{Name: "config"},
|
cli.StringFlag{Name: "config"},
|
||||||
}
|
}
|
||||||
@@ -170,14 +170,20 @@ func TrustedSetup(context *cli.Context) error {
|
|||||||
json.Unmarshal([]byte(string(compiledcircuitFile)), &circuit)
|
json.Unmarshal([]byte(string(compiledcircuitFile)), &circuit)
|
||||||
panicErr(err)
|
panicErr(err)
|
||||||
|
|
||||||
// read inputs file
|
// read privateInputs file
|
||||||
inputsFile, err := ioutil.ReadFile("inputs.json")
|
privateInputsFile, err := ioutil.ReadFile("privateInputs.json")
|
||||||
panicErr(err)
|
panicErr(err)
|
||||||
|
// read publicInputs file
|
||||||
|
publicInputsFile, err := ioutil.ReadFile("publicInputs.json")
|
||||||
|
panicErr(err)
|
||||||
|
|
||||||
// parse inputs from inputsFile
|
// parse inputs from inputsFile
|
||||||
// var inputs []*big.Int
|
|
||||||
var inputs circuitcompiler.Inputs
|
var inputs circuitcompiler.Inputs
|
||||||
json.Unmarshal([]byte(string(inputsFile)), &inputs)
|
err = json.Unmarshal([]byte(string(privateInputsFile)), &inputs.Private)
|
||||||
panicErr(err)
|
panicErr(err)
|
||||||
|
err = json.Unmarshal([]byte(string(publicInputsFile)), &inputs.Public)
|
||||||
|
panicErr(err)
|
||||||
|
|
||||||
// calculate wittness
|
// calculate wittness
|
||||||
w, err := circuit.CalculateWitness(inputs.Private, inputs.Public)
|
w, err := circuit.CalculateWitness(inputs.Private, inputs.Public)
|
||||||
panicErr(err)
|
panicErr(err)
|
||||||
@@ -245,23 +251,22 @@ func GenerateProofs(context *cli.Context) error {
|
|||||||
// calculate wittness
|
// calculate wittness
|
||||||
w, err := circuit.CalculateWitness(inputs.Private, inputs.Public)
|
w, err := circuit.CalculateWitness(inputs.Private, inputs.Public)
|
||||||
panicErr(err)
|
panicErr(err)
|
||||||
fmt.Println("\nwitness", w)
|
fmt.Println("witness", w)
|
||||||
|
|
||||||
// flat code to R1CS
|
// flat code to R1CS
|
||||||
// a, b, c := circuit.GenerateR1CS()
|
|
||||||
a := circuit.R1CS.A
|
a := circuit.R1CS.A
|
||||||
b := circuit.R1CS.B
|
b := circuit.R1CS.B
|
||||||
c := circuit.R1CS.C
|
c := circuit.R1CS.C
|
||||||
// R1CS to QAP
|
// 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)
|
_, _, _, 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(circuit)
|
||||||
fmt.Println(trustedsetup.G1T)
|
fmt.Println(trustedsetup.G1T)
|
||||||
fmt.Println(hx)
|
fmt.Println(hx)
|
||||||
fmt.Println(w)
|
fmt.Println(w)
|
||||||
proof, err := snark.GenerateProofs(circuit, trustedsetup, hx, w)
|
proof, err := snark.GenerateProofs(circuit, trustedsetup, w, px)
|
||||||
panicErr(err)
|
panicErr(err)
|
||||||
|
|
||||||
fmt.Println("\n proofs:")
|
fmt.Println("\n proofs:")
|
||||||
|
|||||||
BIN
go-snark-cli
Executable file
BIN
go-snark-cli
Executable file
Binary file not shown.
Reference in New Issue
Block a user