arnaucube
arnaucube/go-snarkPublic Self-hosted repository
go-snark/circuitcompiler/circuit_test.go
circuitcompiler/circuit_test.goRaw
 1package circuitcompiler
 2
 3import (
 4	"fmt"
 5	"math/big"
 6	"strings"
 7	"testing"
 8
 9	"github.com/stretchr/testify/assert"
10)
11
12func TestCircuitParser(t *testing.T) {
13	/*
14		input:
15		def test():
16			y = x**3
17			return x + y + 5
18
19		flattened:
20			m1 = s1 * s1
21			m2 = m1 * s1
22			m3 = m2 + s1
23			out = m3 + 5
24	*/
25
26	// flat code
27	flat := `
28	func test(x):
29		aux = x*x
30		y = aux*x
31		z = x + y
32		out = z + 5
33	`
34	parser := NewParser(strings.NewReader(flat))
35	circuit, err := parser.Parse()
36	assert.Nil(t, err)
37	fmt.Println(circuit)
38
39	// flat code to R1CS
40	fmt.Println("generating R1CS from flat code")
41	a, b, c := circuit.GenerateR1CS()
42	fmt.Print("function with inputs: ")
43	fmt.Println(circuit.Inputs)
44
45	fmt.Print("signals: ")
46	fmt.Println(circuit.Signals)
47
48	// expected result
49	b0 := big.NewInt(int64(0))
50	b1 := big.NewInt(int64(1))
51	b5 := big.NewInt(int64(5))
52	aExpected := [][]*big.Int{
53		[]*big.Int{b0, b1, b0, b0, b0, b0},
54		[]*big.Int{b0, b0, b0, b1, b0, b0},
55		[]*big.Int{b0, b1, b0, b0, b1, b0},
56		[]*big.Int{b5, b0, b0, b0, b0, b1},
57	}
58	bExpected := [][]*big.Int{
59		[]*big.Int{b0, b1, b0, b0, b0, b0},
60		[]*big.Int{b0, b1, b0, b0, b0, b0},
61		[]*big.Int{b1, b0, b0, b0, b0, b0},
62		[]*big.Int{b1, b0, b0, b0, b0, b0},
63	}
64	cExpected := [][]*big.Int{
65		[]*big.Int{b0, b0, b0, b1, b0, b0},
66		[]*big.Int{b0, b0, b0, b0, b1, b0},
67		[]*big.Int{b0, b0, b0, b0, b0, b1},
68		[]*big.Int{b0, b0, b1, b0, b0, b0},
69	}
70
71	assert.Equal(t, aExpected, a)
72	assert.Equal(t, bExpected, b)
73	assert.Equal(t, cExpected, c)
74	fmt.Println(a)
75	fmt.Println(b)
76	fmt.Println(c)
77}