forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 9
/
debug_test.go
221 lines (188 loc) · 5.94 KB
/
debug_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
package gnark_test
import (
"bytes"
"math/big"
"testing"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/backend/plonk"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/consensys/gnark/frontend/cs/scs"
"github.com/consensys/gnark/test"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)
// -------------------------------------------------------------------------------------------------
// test println (non regression)
type printlnCircuit struct {
A, B frontend.Variable
}
func (circuit *printlnCircuit) Define(api frontend.API) error {
c := api.Add(circuit.A, circuit.B)
api.Println(c, "is the addition")
d := api.Mul(circuit.A, c)
api.Println(d, new(big.Int).SetInt64(42))
bs := api.ToBinary(circuit.B, 10)
api.Println("bits", bs[3])
api.Println("circuit", circuit)
nb := api.Mul(bs[1], 2)
api.AssertIsBoolean(nb) // this will fail
m := api.Mul(circuit.A, circuit.B)
api.Println("m", m) // this should not be resolved
return nil
}
func TestPrintln(t *testing.T) {
assert := require.New(t)
var circuit, witness printlnCircuit
witness.A = 2
witness.B = 11
var expected bytes.Buffer
expected.WriteString("debug_test.go:28 > 13 is the addition\n")
expected.WriteString("debug_test.go:30 > 26 42\n")
expected.WriteString("debug_test.go:32 > bits 1\n")
expected.WriteString("debug_test.go:33 > circuit {A: 2, B: 11}\n")
expected.WriteString("debug_test.go:37 > m .*\n")
{
trace, _ := getGroth16Trace(&circuit, &witness)
assert.Regexp(expected.String(), trace)
}
{
trace, _ := getPlonkTrace(&circuit, &witness)
assert.Regexp(expected.String(), trace)
}
}
// -------------------------------------------------------------------------------------------------
// Div by 0
type divBy0Trace struct {
A, B, C frontend.Variable
}
func (circuit *divBy0Trace) Define(api frontend.API) error {
d := api.Add(circuit.B, circuit.C)
api.Div(circuit.A, d)
return nil
}
func TestTraceDivBy0(t *testing.T) {
assert := require.New(t)
var circuit, witness divBy0Trace
witness.A = 2
witness.B = -2
witness.C = 2
{
_, err := getGroth16Trace(&circuit, &witness)
assert.Error(err)
assert.Contains(err.Error(), "constraint #0 is not satisfied: [div] 2/(-2 + 2) == <unsolved>")
assert.Contains(err.Error(), "(*divBy0Trace).Define")
assert.Contains(err.Error(), "debug_test.go:")
}
{
_, err := getPlonkTrace(&circuit, &witness)
assert.Error(err)
assert.Contains(err.Error(), "constraint #1 is not satisfied: [inverse] 1/0 < ∞")
assert.Contains(err.Error(), "(*divBy0Trace).Define")
assert.Contains(err.Error(), "debug_test.go:")
}
}
// -------------------------------------------------------------------------------------------------
// Not Equal
type notEqualTrace struct {
A, B, C frontend.Variable
}
func (circuit *notEqualTrace) Define(api frontend.API) error {
d := api.Add(circuit.B, circuit.C)
api.AssertIsEqual(circuit.A, d)
return nil
}
func TestTraceNotEqual(t *testing.T) {
assert := require.New(t)
var circuit, witness notEqualTrace
witness.A = 1
witness.B = 24
witness.C = 42
{
_, err := getGroth16Trace(&circuit, &witness)
assert.Error(err)
assert.Contains(err.Error(), "constraint #0 is not satisfied: [assertIsEqual] 1 == (24 + 42)")
assert.Contains(err.Error(), "(*notEqualTrace).Define")
assert.Contains(err.Error(), "debug_test.go:")
}
{
_, err := getPlonkTrace(&circuit, &witness)
assert.Error(err)
assert.Contains(err.Error(), "constraint #1 is not satisfied: [assertIsEqual] 1 + -66 == 0")
assert.Contains(err.Error(), "(*notEqualTrace).Define")
assert.Contains(err.Error(), "debug_test.go:")
}
}
// -------------------------------------------------------------------------------------------------
// Not boolean
type notBooleanTrace struct {
B, C frontend.Variable
}
func (circuit *notBooleanTrace) Define(api frontend.API) error {
d := api.Add(circuit.B, circuit.C)
api.AssertIsBoolean(d)
return nil
}
func TestTraceNotBoolean(t *testing.T) {
assert := require.New(t)
var circuit, witness notBooleanTrace
// witness.A = 1
witness.B = 24
witness.C = 42
{
_, err := getGroth16Trace(&circuit, &witness)
assert.Error(err)
assert.Contains(err.Error(), "constraint #0 is not satisfied: [assertIsBoolean] (24 + 42) == (0|1)")
assert.Contains(err.Error(), "(*notBooleanTrace).Define")
assert.Contains(err.Error(), "debug_test.go:")
}
{
_, err := getPlonkTrace(&circuit, &witness)
assert.Error(err)
assert.Contains(err.Error(), "constraint #1 is not satisfied: [assertIsBoolean] 66 == (0|1)")
assert.Contains(err.Error(), "(*notBooleanTrace).Define")
assert.Contains(err.Error(), "debug_test.go:")
}
}
func getPlonkTrace(circuit, w frontend.Circuit) (string, error) {
ccs, err := frontend.Compile(ecc.BN254, scs.NewBuilder, circuit)
if err != nil {
return "", err
}
srs, err := test.NewKZGSRS(ccs)
if err != nil {
return "", err
}
pk, _, err := plonk.Setup(ccs, srs)
if err != nil {
return "", err
}
var buf bytes.Buffer
sw, err := frontend.NewWitness(w, ecc.BN254)
if err != nil {
return "", err
}
log := zerolog.New(&zerolog.ConsoleWriter{Out: &buf, NoColor: true, PartsExclude: []string{zerolog.LevelFieldName, zerolog.TimestampFieldName}})
_, err = plonk.Prove(ccs, pk, sw, backend.WithCircuitLogger(log))
return buf.String(), err
}
func getGroth16Trace(circuit, w frontend.Circuit) (string, error) {
ccs, err := frontend.Compile(ecc.BN254, r1cs.NewBuilder, circuit)
if err != nil {
return "", err
}
pk, err := groth16.DummySetup(ccs)
if err != nil {
return "", err
}
var buf bytes.Buffer
sw, err := frontend.NewWitness(w, ecc.BN254)
if err != nil {
return "", err
}
log := zerolog.New(&zerolog.ConsoleWriter{Out: &buf, NoColor: true, PartsExclude: []string{zerolog.LevelFieldName, zerolog.TimestampFieldName}})
_, err = groth16.Prove(ccs, pk, sw, backend.WithCircuitLogger(log))
return buf.String(), err
}