You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.3 KiB

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