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.

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