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