forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
115 lines (100 loc) · 3.1 KB
/
integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright © 2020 ConsenSys
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/encoding/gob"
"github.com/consensys/gnark/internal/generators/testcircuits/circuits"
"github.com/consensys/gurvy"
)
func TestIntegration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration tests for circleCI")
}
// create temporary dir for integration test
parentDir := "./integration_test"
os.RemoveAll(parentDir)
defer os.RemoveAll(parentDir)
if err := os.MkdirAll(parentDir, 0700); err != nil {
t.Fatal(err)
}
// spv: setup, prove, verify
spv := func(name string, good, bad backend.Assignments) {
t.Log("circuit", name)
// path for files
fCircuit := filepath.Join(parentDir, name+".r1cs")
fPk := filepath.Join(parentDir, name+".pk")
fVk := filepath.Join(parentDir, name+".vk")
fProof := filepath.Join(parentDir, name+".proof")
fInputGood := filepath.Join(parentDir, name+".good.input")
fInputBad := filepath.Join(parentDir, name+".bad.input")
buildTags := "debug"
// 2: input files to disk
if err := good.WriteFile(fInputGood); err != nil {
t.Fatal(err)
}
if err := bad.WriteFile(fInputBad); err != nil {
t.Fatal(err)
}
// 3: run setup
{
cmd := exec.Command("go", "run", "-tags", buildTags, "main.go", "setup", fCircuit, "--pk", fPk, "--vk", fVk)
out, err := cmd.Output()
t.Log(string(out))
if err != nil {
t.Fatal(err)
}
}
pv := func(fInput string, expectedVerifyResult bool) {
// 4: run prove
{
cmd := exec.Command("go", "run", "-tags", buildTags, "main.go", "prove", fCircuit, "--pk", fPk, "--input", fInput, "--proof", fProof)
out, err := cmd.Output()
t.Log(string(out))
if expectedVerifyResult && err != nil {
// proving should pass
t.Fatal(err)
}
}
// 4: run verify
{
cmd := exec.Command("go", "run", "-tags", buildTags, "main.go", "verify", fProof, "--vk", fVk, "--input", fInput)
out, err := cmd.Output()
t.Log(string(out))
if expectedVerifyResult && err != nil {
t.Fatal(err)
} else if !expectedVerifyResult && err == nil {
t.Fatal("verify should have failed but apparently succeeded")
}
}
}
pv(fInputGood, true)
pv(fInputBad, false)
}
for name, circuit := range circuits.Circuits {
if name == "reference_large" {
// be nice with circleci.
continue
}
// serialize to disk
fCircuit := filepath.Join(parentDir, name+".r1cs")
if err := gob.Write(fCircuit, circuit.R1CS, gurvy.BN256); err != nil {
t.Fatal(err)
}
spv(name, circuit.Good, circuit.Bad)
}
}