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.

88 lines
2.5 KiB

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