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.

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