forked from 0xbow-io/Veritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
veritas_test.go
261 lines (233 loc) · 5.08 KB
/
veritas_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package veritas
import (
"fmt"
"math/big"
"testing"
"github.com/test-go/testify/require"
)
var (
testProgA = Program{
Identity: "A",
Src: `
template A(ParamA, ParamB){
signal input in1;
signal input in2;
signal output out;
component b = B(ParamA, ParamB);
b.in1 <== in1;
b.in2 <== in2;
out <== b.out;
}
`,
}
testProgB = Program{
Identity: "B",
Src: `
template B(ParamA, ParamB){
signal input in1;
signal input in2;
signal output out;
var x = in1 * ParamA;
var b = in2 * ParamB;
out <== x + b;
}
`,
}
testProgC = Program{
Identity: "Undeclared_Symbol",
Src: `
template A(){
signal input in1;
signal input in2;
signal output out;
out <== in1 * in3;
}`,
}
testProgD = Program{
Identity: "UnderConstrained",
Src: `
template A(){
signal input in1;
signal input in2;
signal output out;
out <== 1;
}`,
}
)
func Test_Compile(t *testing.T) {
var (
public_inputs = "in1"
params = "5, 9"
lib = NewEmptyLibrary()
)
defer lib.Burn()
reports, err := lib.Compile(CircuitPkg{
TargetVersion: "2.0.0",
Field: "bn128",
Programs: []Program{
{
Identity: "main",
Src: fmt.Sprintf("component main {public[%s]}= A(%s);",
public_inputs, params),
},
testProgA, testProgB},
})
require.Nil(t, err)
require.Len(t, reports, 0)
}
func Test_Compile_Anon(t *testing.T) {
var (
lib = NewEmptyLibrary()
)
defer lib.Burn()
reports, err := lib.Compile(CircuitPkg{
TargetVersion: "2.2.0",
Field: "bn128",
Programs: []Program{
{
Identity: "main",
Src: "component main {public[in1]}= A();",
},
{
Identity: "A",
Src: `
template A(){
signal input in1;
signal output out;
out <== Anon()(in1);
}`,
},
},
})
require.Nil(t, err)
require.True(t, len(reports) > 0)
print(reports.String())
}
func Test_Compile_UndeclaredSymbol(t *testing.T) {
var (
public_inputs = "in1"
params = ""
lib = NewEmptyLibrary()
)
lib.Burn()
reports, err := lib.Compile(CircuitPkg{
TargetVersion: "2.0.0",
Field: "bn128",
Programs: []Program{
{
Identity: "main",
Src: fmt.Sprintf("component main {public[%s]}= A(%s);",
public_inputs, params),
},
testProgC},
})
require.Nil(t, err)
require.True(t, len(reports) > 0)
print(reports.String())
}
func Test_Compile_UnderConstrained(t *testing.T) {
var (
public_inputs = "in1"
params = ""
lib = NewEmptyLibrary()
)
defer lib.Burn()
reports, err := lib.Compile(CircuitPkg{
TargetVersion: "2.0.0",
Field: "bn128",
Programs: []Program{
{
Identity: "main",
Src: fmt.Sprintf("component main {public[%s]}= A(%s);",
public_inputs, params),
},
testProgD},
})
require.Nil(t, err)
require.True(t, len(reports) > 0)
print(reports.String())
}
func Test_Evaluation(t *testing.T) {
var (
lib = NewEmptyLibrary()
// A template
// that injects the expected value of x into the circuit
// and checks that the constraint is satisfied
progA = Program{
Identity: "Test",
Src: `
template Test(){
signal input a;
signal input ex;
signal output b;
var x = a*a;
x += 3;
0 === ex - x;
b <== 1;
}
`}
evaluator = func(a *big.Int) *big.Int {
const i = 2
var x = new(big.Int).Mul(a, a)
x.Add(x, big.NewInt(3))
return x
}
expectedSyms = []Symbol{
{
Symbol: "main.b",
Original: "1",
Witness: "1",
NodeID: "0",
Assignment: big.NewInt(1),
},
{
Symbol: "main.a",
Original: "2",
Witness: "2",
NodeID: "0",
},
{
Symbol: "main.ex",
Original: "3",
Witness: "3",
NodeID: "0",
},
}
)
defer lib.Burn()
reports, err := lib.Compile(CircuitPkg{
TargetVersion: "2.0.0",
Field: "bn128",
Programs: []Program{
{
Identity: "main",
Src: `component main {public[a, ex]}= Test();`,
},
progA},
})
require.Nil(t, err)
require.Len(t, reports, 0)
for i := 0; i < 10; i++ {
expectedSyms[2].Assignment = big.NewInt(0).Set(evaluator(big.NewInt(int64(i))))
expectedSyms[1].Assignment = big.NewInt(int64(i))
input := fmt.Sprintf(`{"a":%d, "ex":%s}`, i, expectedSyms[2].Assignment.String())
evaluation, err := lib.Evaluate([]byte(input))
// Execute the circuit
require.Nil(t, err)
require.NotNil(t, evaluation)
// Check for any reports
reports, err = lib.GetReports()
require.Nil(t, err)
require.Len(t, reports, 0)
// Check that constraints are satisfied
require.True(t, len(evaluation.SatisfiedConstraints()) > 0)
require.Len(t, evaluation.UnSatisfiedConstraints(), 0)
//ensure that all symbols are assigned to the correct witness value
evaluation.AssignWitToSym()
for _, sym := range expectedSyms {
val := evaluation.GetSymbolAssignment(&sym)
require.NotNil(t, val)
require.Equal(t, sym.Assignment, val)
}
}
}