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.

84 lines
1.7 KiB

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