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.

89 lines
1.5 KiB

  1. package circuitcompiler
  2. import (
  3. "fmt"
  4. "math/big"
  5. "strings"
  6. "testing"
  7. )
  8. func TestProgramm_BuildConstraintTree(t *testing.T) {
  9. line := "asdf asfd"
  10. line = strings.TrimFunc(line, func(i rune) bool { return isWhitespace(i) })
  11. fmt.Println(line)
  12. }
  13. func TestNewProgramm(t *testing.T) {
  14. flat := `
  15. func do(x):
  16. e = x * 5
  17. b = e * 6
  18. c = b * 7
  19. f = c * 1
  20. d = c * f
  21. out = d * mul(d,e)
  22. func add(x ,k):
  23. z = k * x
  24. out = do(x) + mul(x,z)
  25. func main(x,z):
  26. out = do(z) + add(x,x)
  27. func mul(a,b):
  28. out = a * b
  29. `
  30. //flat := `
  31. //func mul(a,b):
  32. // out = a * b
  33. //
  34. //func main(a):
  35. // b = a * a
  36. // c = 4 - b
  37. // d = 5 * c
  38. // out = d / mul(b,b)
  39. //`
  40. //flat := `
  41. //func main(a,b):
  42. // c = a + b
  43. // e = c - a
  44. // f = e + b
  45. // g = f + 2
  46. // out = g * a
  47. //`
  48. parser := NewParser(strings.NewReader(flat))
  49. program, err := parser.Parse()
  50. if err != nil {
  51. panic(err)
  52. }
  53. fmt.Println("\n unreduced")
  54. fmt.Println(flat)
  55. program.BuildConstraintTrees()
  56. for k, v := range program.functions {
  57. fmt.Println(k)
  58. PrintTree(v.root)
  59. }
  60. fmt.Println("\nReduced gates")
  61. //PrintTree(froots["mul"])
  62. gates := program.ReduceCombinedTree()
  63. for _, g := range gates {
  64. fmt.Printf("\n %v", g)
  65. }
  66. fmt.Println("generating R1CS")
  67. a, b, c := program.GenerateReducedR1CS(gates)
  68. fmt.Println(a)
  69. fmt.Println(b)
  70. fmt.Println(c)
  71. a1 := big.NewInt(int64(6))
  72. a2 := big.NewInt(int64(5))
  73. inputs := []*big.Int{a1, a2}
  74. w := program.CalculateWitness(inputs)
  75. fmt.Println("witness")
  76. fmt.Println(w)
  77. }