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. //
  23. //func add(x ,k):
  24. // z = k * x
  25. // out = do(x) + mul(x,z)
  26. //
  27. //func main(x,z):
  28. // out = do(z) + add(x,x)
  29. //
  30. //func mul(a,b):
  31. // out = a * b
  32. //`
  33. flat := `
  34. func mul(a,b):
  35. out = a * b
  36. func main(a):
  37. b = a * a
  38. c = 4 - b
  39. d = 5 * c
  40. out = mul(d,b) / mul(b,b)
  41. `
  42. //flat := `
  43. //func main(a,b):
  44. // c = a + b
  45. // e = c - a
  46. // f = e + b
  47. // g = f + 2
  48. // out = g * a
  49. //`
  50. parser := NewParser(strings.NewReader(flat))
  51. program, err := parser.Parse()
  52. if err != nil {
  53. panic(err)
  54. }
  55. fmt.Println("\n unreduced")
  56. fmt.Println(flat)
  57. program.BuildConstraintTrees()
  58. for k, v := range program.functions {
  59. fmt.Println(k)
  60. PrintTree(v.root)
  61. }
  62. fmt.Println("\nReduced gates")
  63. //PrintTree(froots["mul"])
  64. gates := program.ReduceCombinedTree()
  65. for _, g := range gates {
  66. fmt.Printf("\n %v", g)
  67. }
  68. fmt.Println("generating R1CS")
  69. a, b, c := program.GenerateReducedR1CS(gates)
  70. fmt.Println(a)
  71. fmt.Println(b)
  72. fmt.Println(c)
  73. a1 := big.NewInt(int64(6))
  74. //a2 := big.NewInt(int64(5))
  75. inputs := []*big.Int{a1}
  76. w := program.CalculateWitness(inputs)
  77. fmt.Println("witness")
  78. fmt.Println(w)
  79. }