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.

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