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, where er is expected_result
27 flat := `
28 func test(private x, public er):
29 aux = x*x
30 y = aux*x
31 z = x + y
32 res = z + 5
33 equals(er, res)
34 out = 1
35 `
36 parser := NewParser(strings.NewReader(flat))
37 circuit, err := parser.Parse()
38 assert.Nil(t, err)
39 fmt.Println("circuit parsed: ", circuit)
40
41 // flat code to R1CS
42 fmt.Println("generating R1CS from flat code")
43 a, b, c := circuit.GenerateR1CS()
44 fmt.Println("private inputs: ", circuit.PrivateInputs)
45 fmt.Println("public inputs: ", circuit.PublicInputs)
46
47 fmt.Println("signals:", circuit.Signals)
48
49 // expected result
50 // b0 := big.NewInt(int64(0))
51 // b1 := big.NewInt(int64(1))
52 // b5 := big.NewInt(int64(5))
53 // aExpected := [][]*big.Int{
54 // []*big.Int{b0, b0, b1, b0, b0, b0},
55 // []*big.Int{b0, b0, b0, b1, b0, b0},
56 // []*big.Int{b0, b0, b1, b0, b1, b0},
57 // []*big.Int{b5, b0, b0, b0, b0, b1},
58 // }
59 // bExpected := [][]*big.Int{
60 // []*big.Int{b0, b0, b1, b0, b0, b0},
61 // []*big.Int{b0, b0, b1, b0, b0, b0},
62 // []*big.Int{b1, b0, b0, b0, b0, b0},
63 // []*big.Int{b1, b0, b0, b0, b0, b0},
64 // }
65 // cExpected := [][]*big.Int{
66 // []*big.Int{b0, b0, b0, b1, b0, b0},
67 // []*big.Int{b0, b0, b0, b0, b1, b0},
68 // []*big.Int{b0, b0, b0, b0, b0, b1},
69 // []*big.Int{b0, b1, b0, b0, b0, b0},
70 // }
71 //
72 // assert.Equal(t, aExpected, a)
73 // assert.Equal(t, bExpected, b)
74 // assert.Equal(t, cExpected, c)
75 fmt.Println(a)
76 fmt.Println(b)
77 fmt.Println(c)
78
79 b3 := big.NewInt(int64(3))
80 privateInputs := []*big.Int{b3}
81 b35 := big.NewInt(int64(35))
82 publicInputs := []*big.Int{b35}
83 // Calculate Witness
84 w, err := circuit.CalculateWitness(privateInputs, publicInputs)
85 assert.Nil(t, err)
86 fmt.Println("w", w)
87}