@ -1,8 +1,12 @@ |
|||||
module gocircomprover |
|
||||
|
module go-circom-prover-verifier |
||||
|
|
||||
go 1.13 |
|
||||
|
go 1.14 |
||||
|
|
||||
|
replace github.com/iden3/go-circom-prover-verifier => ./ |
||||
|
|
||||
require ( |
require ( |
||||
github.com/ethereum/go-ethereum v1.9.12 |
github.com/ethereum/go-ethereum v1.9.12 |
||||
|
github.com/iden3/go-circom-prover-verifier v0.0.0-00010101000000-000000000000 |
||||
github.com/stretchr/testify v1.5.1 |
github.com/stretchr/testify v1.5.1 |
||||
|
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 |
||||
) |
) |
@ -1,4 +1,4 @@ |
|||||
package gocircomprover |
|
||||
|
package parsers |
||||
|
|
||||
import ( |
import ( |
||||
"testing" |
"testing" |
@ -1,4 +1,4 @@ |
|||||
package gocircomprover |
|
||||
|
package prover |
||||
|
|
||||
import ( |
import ( |
||||
"bytes" |
"bytes" |
@ -1,4 +1,4 @@ |
|||||
package gocircomprover |
|
||||
|
package prover |
||||
|
|
||||
import ( |
import ( |
||||
"math" |
"math" |
@ -0,0 +1,106 @@ |
|||||
|
package prover |
||||
|
|
||||
|
import ( |
||||
|
"encoding/json" |
||||
|
"io/ioutil" |
||||
|
"math/big" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/iden3/go-circom-prover-verifier/parsers" |
||||
|
"github.com/iden3/go-circom-prover-verifier/types" |
||||
|
"github.com/iden3/go-circom-prover-verifier/verifier" |
||||
|
"github.com/stretchr/testify/assert" |
||||
|
"github.com/stretchr/testify/require" |
||||
|
) |
||||
|
|
||||
|
func TestSmallCircuitGenerateProf(t *testing.T) { |
||||
|
provingKeyJson, err := ioutil.ReadFile("../testdata/small/proving_key.json") |
||||
|
require.Nil(t, err) |
||||
|
pk, err := parsers.ParsePk(provingKeyJson) |
||||
|
require.Nil(t, err) |
||||
|
|
||||
|
witnessJson, err := ioutil.ReadFile("../testdata/small/witness.json") |
||||
|
require.Nil(t, err) |
||||
|
w, err := parsers.ParseWitness(witnessJson) |
||||
|
require.Nil(t, err) |
||||
|
|
||||
|
assert.Equal(t, types.Witness{big.NewInt(1), big.NewInt(33), big.NewInt(3), big.NewInt(11)}, w) |
||||
|
|
||||
|
proof, pubSignals, err := GenerateProof(pk, w) |
||||
|
assert.Nil(t, err) |
||||
|
|
||||
|
proofStr, err := parsers.ProofToJson(proof) |
||||
|
assert.Nil(t, err) |
||||
|
|
||||
|
err = ioutil.WriteFile("../testdata/small/proof.json", proofStr, 0644) |
||||
|
assert.Nil(t, err) |
||||
|
publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals)) |
||||
|
assert.Nil(t, err) |
||||
|
err = ioutil.WriteFile("../testdata/small/public.json", publicStr, 0644) |
||||
|
assert.Nil(t, err) |
||||
|
|
||||
|
// verify the proof
|
||||
|
vkJson, err := ioutil.ReadFile("../testdata/small/verification_key.json") |
||||
|
require.Nil(t, err) |
||||
|
vk, err := parsers.ParseVk(vkJson) |
||||
|
require.Nil(t, err) |
||||
|
|
||||
|
v := verifier.Verify(vk, proof, pubSignals) |
||||
|
assert.True(t, v) |
||||
|
|
||||
|
// to verify the proof with snarkjs:
|
||||
|
// snarkjs verify --vk testdata/small/verification_key.json -p testdata/small/proof.json --pub testdata/small/public.json
|
||||
|
} |
||||
|
|
||||
|
func TestBigCircuitGenerateProf(t *testing.T) { |
||||
|
provingKeyJson, err := ioutil.ReadFile("../testdata/big/proving_key.json") |
||||
|
require.Nil(t, err) |
||||
|
pk, err := parsers.ParsePk(provingKeyJson) |
||||
|
require.Nil(t, err) |
||||
|
|
||||
|
witnessJson, err := ioutil.ReadFile("../testdata/big/witness.json") |
||||
|
require.Nil(t, err) |
||||
|
w, err := parsers.ParseWitness(witnessJson) |
||||
|
require.Nil(t, err) |
||||
|
|
||||
|
proof, pubSignals, err := GenerateProof(pk, w) |
||||
|
assert.Nil(t, err) |
||||
|
|
||||
|
proofStr, err := parsers.ProofToJson(proof) |
||||
|
assert.Nil(t, err) |
||||
|
|
||||
|
err = ioutil.WriteFile("../testdata/big/proof.json", proofStr, 0644) |
||||
|
assert.Nil(t, err) |
||||
|
publicStr, err := json.Marshal(parsers.ArrayBigIntToString(pubSignals)) |
||||
|
assert.Nil(t, err) |
||||
|
err = ioutil.WriteFile("../testdata/big/public.json", publicStr, 0644) |
||||
|
assert.Nil(t, err) |
||||
|
|
||||
|
// verify the proof
|
||||
|
vkJson, err := ioutil.ReadFile("../testdata/big/verification_key.json") |
||||
|
require.Nil(t, err) |
||||
|
vk, err := parsers.ParseVk(vkJson) |
||||
|
require.Nil(t, err) |
||||
|
|
||||
|
v := verifier.Verify(vk, proof, pubSignals) |
||||
|
assert.True(t, v) |
||||
|
|
||||
|
// to verify the proof with snarkjs:
|
||||
|
// snarkjs verify --vk testdata/big/verification_key.json -p testdata/big/proof.json --pub testdata/big/public.json
|
||||
|
} |
||||
|
|
||||
|
func BenchmarkGenerateProof(b *testing.B) { |
||||
|
provingKeyJson, err := ioutil.ReadFile("../testdata/big/proving_key.json") |
||||
|
require.Nil(b, err) |
||||
|
pk, err := parsers.ParsePk(provingKeyJson) |
||||
|
require.Nil(b, err) |
||||
|
|
||||
|
witnessJson, err := ioutil.ReadFile("../testdata/big/witness.json") |
||||
|
require.Nil(b, err) |
||||
|
w, err := parsers.ParseWitness(witnessJson) |
||||
|
require.Nil(b, err) |
||||
|
|
||||
|
for i := 0; i < b.N; i++ { |
||||
|
GenerateProof(pk, w) |
||||
|
} |
||||
|
} |
@ -1,103 +0,0 @@ |
|||||
package gocircomprover |
|
||||
|
|
||||
import ( |
|
||||
"encoding/json" |
|
||||
"io/ioutil" |
|
||||
"math/big" |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/stretchr/testify/assert" |
|
||||
"github.com/stretchr/testify/require" |
|
||||
) |
|
||||
|
|
||||
func TestSmallCircuitGenerateProf(t *testing.T) { |
|
||||
provingKeyJson, err := ioutil.ReadFile("testdata/small/proving_key.json") |
|
||||
require.Nil(t, err) |
|
||||
pk, err := ParsePk(provingKeyJson) |
|
||||
require.Nil(t, err) |
|
||||
|
|
||||
witnessJson, err := ioutil.ReadFile("testdata/small/witness.json") |
|
||||
require.Nil(t, err) |
|
||||
w, err := ParseWitness(witnessJson) |
|
||||
require.Nil(t, err) |
|
||||
|
|
||||
assert.Equal(t, Witness{big.NewInt(1), big.NewInt(33), big.NewInt(3), big.NewInt(11)}, w) |
|
||||
|
|
||||
proof, pubSignals, err := GenerateProof(pk, w) |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
proofStr, err := ProofToJson(proof) |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
err = ioutil.WriteFile("testdata/small/proof.json", proofStr, 0644) |
|
||||
assert.Nil(t, err) |
|
||||
publicStr, err := json.Marshal(ArrayBigIntToString(pubSignals)) |
|
||||
assert.Nil(t, err) |
|
||||
err = ioutil.WriteFile("testdata/small/public.json", publicStr, 0644) |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
// verify the proof
|
|
||||
vkJson, err := ioutil.ReadFile("testdata/small/verification_key.json") |
|
||||
require.Nil(t, err) |
|
||||
vk, err := ParseVk(vkJson) |
|
||||
require.Nil(t, err) |
|
||||
|
|
||||
v := Verify(vk, proof, pubSignals) |
|
||||
assert.True(t, v) |
|
||||
|
|
||||
// to verify the proof with snarkjs:
|
|
||||
// snarkjs verify --vk testdata/small/verification_key.json -p testdata/small/proof.json --pub testdata/small/public.json
|
|
||||
} |
|
||||
|
|
||||
func TestBigCircuitGenerateProf(t *testing.T) { |
|
||||
provingKeyJson, err := ioutil.ReadFile("testdata/big/proving_key.json") |
|
||||
require.Nil(t, err) |
|
||||
pk, err := ParsePk(provingKeyJson) |
|
||||
require.Nil(t, err) |
|
||||
|
|
||||
witnessJson, err := ioutil.ReadFile("testdata/big/witness.json") |
|
||||
require.Nil(t, err) |
|
||||
w, err := ParseWitness(witnessJson) |
|
||||
require.Nil(t, err) |
|
||||
|
|
||||
proof, pubSignals, err := GenerateProof(pk, w) |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
proofStr, err := ProofToJson(proof) |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
err = ioutil.WriteFile("testdata/big/proof.json", proofStr, 0644) |
|
||||
assert.Nil(t, err) |
|
||||
publicStr, err := json.Marshal(ArrayBigIntToString(pubSignals)) |
|
||||
assert.Nil(t, err) |
|
||||
err = ioutil.WriteFile("testdata/big/public.json", publicStr, 0644) |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
// verify the proof
|
|
||||
vkJson, err := ioutil.ReadFile("testdata/big/verification_key.json") |
|
||||
require.Nil(t, err) |
|
||||
vk, err := ParseVk(vkJson) |
|
||||
require.Nil(t, err) |
|
||||
|
|
||||
v := Verify(vk, proof, pubSignals) |
|
||||
assert.True(t, v) |
|
||||
|
|
||||
// to verify the proof with snarkjs:
|
|
||||
// snarkjs verify --vk testdata/big/verification_key.json -p testdata/big/proof.json --pub testdata/big/public.json
|
|
||||
} |
|
||||
|
|
||||
func BenchmarkGenerateProof(b *testing.B) { |
|
||||
provingKeyJson, err := ioutil.ReadFile("testdata/big/proving_key.json") |
|
||||
require.Nil(b, err) |
|
||||
pk, err := ParsePk(provingKeyJson) |
|
||||
require.Nil(b, err) |
|
||||
|
|
||||
witnessJson, err := ioutil.ReadFile("testdata/big/witness.json") |
|
||||
require.Nil(b, err) |
|
||||
w, err := ParseWitness(witnessJson) |
|
||||
require.Nil(b, err) |
|
||||
|
|
||||
for i := 0; i < b.N; i++ { |
|
||||
GenerateProof(pk, w) |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,46 @@ |
|||||
|
package types |
||||
|
|
||||
|
import ( |
||||
|
"math/big" |
||||
|
|
||||
|
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare" |
||||
|
) |
||||
|
|
||||
|
// Proof is the data structure of the Groth16 zkSNARK proof
|
||||
|
type Proof struct { |
||||
|
A *bn256.G1 |
||||
|
B *bn256.G2 |
||||
|
C *bn256.G1 |
||||
|
} |
||||
|
|
||||
|
// Pk holds the data structure of the ProvingKey
|
||||
|
type Pk struct { |
||||
|
A []*bn256.G1 |
||||
|
B2 []*bn256.G2 |
||||
|
B1 []*bn256.G1 |
||||
|
C []*bn256.G1 |
||||
|
NVars int |
||||
|
NPublic int |
||||
|
VkAlpha1 *bn256.G1 |
||||
|
VkDelta1 *bn256.G1 |
||||
|
VkBeta1 *bn256.G1 |
||||
|
VkBeta2 *bn256.G2 |
||||
|
VkDelta2 *bn256.G2 |
||||
|
HExps []*bn256.G1 |
||||
|
DomainSize int |
||||
|
PolsA []map[int]*big.Int |
||||
|
PolsB []map[int]*big.Int |
||||
|
PolsC []map[int]*big.Int |
||||
|
} |
||||
|
|
||||
|
// Witness contains the witness
|
||||
|
type Witness []*big.Int |
||||
|
|
||||
|
// Vk is the Verification Key data structure
|
||||
|
type Vk struct { |
||||
|
Alpha *bn256.G1 |
||||
|
Beta *bn256.G2 |
||||
|
Gamma *bn256.G2 |
||||
|
Delta *bn256.G2 |
||||
|
IC []*bn256.G1 |
||||
|
} |
@ -0,0 +1,50 @@ |
|||||
|
package verifier |
||||
|
|
||||
|
import ( |
||||
|
"io/ioutil" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/iden3/go-circom-prover-verifier/parsers" |
||||
|
|
||||
|
"github.com/stretchr/testify/assert" |
||||
|
"github.com/stretchr/testify/require" |
||||
|
) |
||||
|
|
||||
|
func TestVerify1(t *testing.T) { |
||||
|
proofJson, err := ioutil.ReadFile("../testdata/big/proof.json") |
||||
|
require.Nil(t, err) |
||||
|
vkJson, err := ioutil.ReadFile("../testdata/big/verification_key.json") |
||||
|
require.Nil(t, err) |
||||
|
publicJson, err := ioutil.ReadFile("../testdata/big/public.json") |
||||
|
require.Nil(t, err) |
||||
|
|
||||
|
public, err := parsers.ParsePublicSignals(publicJson) |
||||
|
require.Nil(t, err) |
||||
|
proof, err := parsers.ParseProof(proofJson) |
||||
|
require.Nil(t, err) |
||||
|
vk, err := parsers.ParseVk(vkJson) |
||||
|
require.Nil(t, err) |
||||
|
|
||||
|
v := Verify(vk, proof, public) |
||||
|
assert.True(t, v) |
||||
|
} |
||||
|
|
||||
|
func BenchmarkVerify(b *testing.B) { |
||||
|
proofJson, err := ioutil.ReadFile("../testdata/big/proof.json") |
||||
|
require.Nil(b, err) |
||||
|
vkJson, err := ioutil.ReadFile("../testdata/big/verification_key.json") |
||||
|
require.Nil(b, err) |
||||
|
publicJson, err := ioutil.ReadFile("../testdata/big/public.json") |
||||
|
require.Nil(b, err) |
||||
|
|
||||
|
public, err := parsers.ParsePublicSignals(publicJson) |
||||
|
require.Nil(b, err) |
||||
|
proof, err := parsers.ParseProof(proofJson) |
||||
|
require.Nil(b, err) |
||||
|
vk, err := parsers.ParseVk(vkJson) |
||||
|
require.Nil(b, err) |
||||
|
|
||||
|
for i := 0; i < b.N; i++ { |
||||
|
Verify(vk, proof, public) |
||||
|
} |
||||
|
} |
@ -1,48 +0,0 @@ |
|||||
package gocircomprover |
|
||||
|
|
||||
import ( |
|
||||
"io/ioutil" |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/stretchr/testify/assert" |
|
||||
"github.com/stretchr/testify/require" |
|
||||
) |
|
||||
|
|
||||
func TestVerify1(t *testing.T) { |
|
||||
proofJson, err := ioutil.ReadFile("testdata/big/proof.json") |
|
||||
require.Nil(t, err) |
|
||||
vkJson, err := ioutil.ReadFile("testdata/big/verification_key.json") |
|
||||
require.Nil(t, err) |
|
||||
publicJson, err := ioutil.ReadFile("testdata/big/public.json") |
|
||||
require.Nil(t, err) |
|
||||
|
|
||||
public, err := ParsePublicSignals(publicJson) |
|
||||
require.Nil(t, err) |
|
||||
proof, err := ParseProof(proofJson) |
|
||||
require.Nil(t, err) |
|
||||
vk, err := ParseVk(vkJson) |
|
||||
require.Nil(t, err) |
|
||||
|
|
||||
v := Verify(vk, proof, public) |
|
||||
assert.True(t, v) |
|
||||
} |
|
||||
|
|
||||
func BenchmarkVerify(b *testing.B) { |
|
||||
proofJson, err := ioutil.ReadFile("testdata/big/proof.json") |
|
||||
require.Nil(b, err) |
|
||||
vkJson, err := ioutil.ReadFile("testdata/big/verification_key.json") |
|
||||
require.Nil(b, err) |
|
||||
publicJson, err := ioutil.ReadFile("testdata/big/public.json") |
|
||||
require.Nil(b, err) |
|
||||
|
|
||||
public, err := ParsePublicSignals(publicJson) |
|
||||
require.Nil(b, err) |
|
||||
proof, err := ParseProof(proofJson) |
|
||||
require.Nil(b, err) |
|
||||
vk, err := ParseVk(vkJson) |
|
||||
require.Nil(b, err) |
|
||||
|
|
||||
for i := 0; i < b.N; i++ { |
|
||||
Verify(vk, proof, public) |
|
||||
} |
|
||||
} |
|