arnaucube
arnaucube/go-snarkPublic Self-hosted repository
go-snark/circuitcompiler/circuit_test.go
circuitcompiler/circuit_test.goRaw
 1package circuitcompiler
 2
 3import (
 4	"math/big"
 5	"strings"
 6	"testing"
 7
 8	"github.com/stretchr/testify/assert"
 9)
10
11func TestCircuitParser(t *testing.T) {
12	// y = x^3 + x + 5
13	flat := `
14	func test(private s0, public s1):
15		s2 = s0 * s0
16		s3 = s2 * s0
17		s4 = s3 + s0
18		s5 = s4 + 5
19		equals(s1, s5)
20		out = 1 * 1
21	`
22	parser := NewParser(strings.NewReader(flat))
23	circuit, err := parser.Parse()
24	assert.Nil(t, err)
25
26	// flat code to R1CS
27	a, b, c := circuit.GenerateR1CS()
28	assert.Equal(t, "s0", circuit.PrivateInputs[0])
29	assert.Equal(t, "s1", circuit.PublicInputs[0])
30
31	assert.Equal(t, []string{"one", "s1", "s0", "s2", "s3", "s4", "s5", "out"}, circuit.Signals)
32
33	// expected result
34	b0 := big.NewInt(int64(0))
35	b1 := big.NewInt(int64(1))
36	b5 := big.NewInt(int64(5))
37	aExpected := [][]*big.Int{
38		[]*big.Int{b0, b0, b1, b0, b0, b0, b0, b0},
39		[]*big.Int{b0, b0, b0, b1, b0, b0, b0, b0},
40		[]*big.Int{b0, b0, b1, b0, b1, b0, b0, b0},
41		[]*big.Int{b5, b0, b0, b0, b0, b1, b0, b0},
42		[]*big.Int{b0, b0, b0, b0, b0, b0, b1, b0},
43		[]*big.Int{b0, b1, b0, b0, b0, b0, b0, b0},
44		[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
45	}
46	bExpected := [][]*big.Int{
47		[]*big.Int{b0, b0, b1, b0, b0, b0, b0, b0},
48		[]*big.Int{b0, b0, b1, b0, b0, b0, b0, b0},
49		[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
50		[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
51		[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
52		[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
53		[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
54	}
55	cExpected := [][]*big.Int{
56		[]*big.Int{b0, b0, b0, b1, b0, b0, b0, b0},
57		[]*big.Int{b0, b0, b0, b0, b1, b0, b0, b0},
58		[]*big.Int{b0, b0, b0, b0, b0, b1, b0, b0},
59		[]*big.Int{b0, b0, b0, b0, b0, b0, b1, b0},
60		[]*big.Int{b0, b1, b0, b0, b0, b0, b0, b0},
61		[]*big.Int{b0, b0, b0, b0, b0, b0, b1, b0},
62		[]*big.Int{b0, b0, b0, b0, b0, b0, b0, b1},
63	}
64
65	assert.Equal(t, aExpected, a)
66	assert.Equal(t, bExpected, b)
67	assert.Equal(t, cExpected, c)
68
69	b3 := big.NewInt(int64(3))
70	privateInputs := []*big.Int{b3}
71	b35 := big.NewInt(int64(35))
72	publicInputs := []*big.Int{b35}
73	// Calculate Witness
74	w, err := circuit.CalculateWitness(privateInputs, publicInputs)
75	assert.Nil(t, err)
76	b9 := big.NewInt(int64(9))
77	b27 := big.NewInt(int64(27))
78	b30 := big.NewInt(int64(30))
79	wExpected := []*big.Int{b1, b35, b3, b9, b27, b30, b35, b1}
80	assert.Equal(t, wExpected, w)
81
82	// circuitJson, _ := json.Marshal(circuit)
83	// fmt.Println("circuit:", string(circuitJson))
84
85	assert.Equal(t, circuit.NPublic, 1)
86	assert.Equal(t, len(circuit.PublicInputs), 1)
87	assert.Equal(t, len(circuit.PrivateInputs), 1)
88}