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.

73 lines
1.8 KiB

  1. package proof
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/arnaucube/go-snark/fields"
  7. )
  8. func TestR1CSToQAP(t *testing.T) {
  9. // new Finite Field
  10. r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  11. assert.True(nil, ok)
  12. f := fields.NewFq(r)
  13. Utils.FqR = f
  14. // new Polynomial Field
  15. Utils.PF = fields.NewPF(f)
  16. b0 := big.NewInt(int64(0))
  17. b1 := big.NewInt(int64(1))
  18. b3 := big.NewInt(int64(3))
  19. b5 := big.NewInt(int64(5))
  20. b9 := big.NewInt(int64(9))
  21. b27 := big.NewInt(int64(27))
  22. b30 := big.NewInt(int64(30))
  23. b35 := big.NewInt(int64(35))
  24. a := [][]*big.Int{
  25. []*big.Int{b0, b1, b0, b0, b0, b0},
  26. []*big.Int{b0, b0, b0, b1, b0, b0},
  27. []*big.Int{b0, b1, b0, b0, b1, b0},
  28. []*big.Int{b5, b0, b0, b0, b0, b1},
  29. }
  30. b := [][]*big.Int{
  31. []*big.Int{b0, b1, b0, b0, b0, b0},
  32. []*big.Int{b0, b1, b0, b0, b0, b0},
  33. []*big.Int{b1, b0, b0, b0, b0, b0},
  34. []*big.Int{b1, b0, b0, b0, b0, b0},
  35. }
  36. c := [][]*big.Int{
  37. []*big.Int{b0, b0, b0, b1, b0, b0},
  38. []*big.Int{b0, b0, b0, b0, b1, b0},
  39. []*big.Int{b0, b0, b0, b0, b0, b1},
  40. []*big.Int{b0, b0, b1, b0, b0, b0},
  41. }
  42. alphas, betas, gammas, zx := R1CSToQAP(a, b, c)
  43. // fmt.Println(alphas)
  44. // fmt.Println(betas)
  45. // fmt.Println(gammas)
  46. // fmt.Print("Z(x): ")
  47. // fmt.Println(zx)
  48. w := []*big.Int{b1, b3, b35, b9, b27, b30}
  49. ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  50. // fmt.Println(ax)
  51. // fmt.Println(bx)
  52. // fmt.Println(cx)
  53. // fmt.Println(px)
  54. hx := Utils.PF.DivisorPolynomial(px, zx)
  55. // fmt.Println(hx)
  56. // hx==px/zx so px==hx*zx
  57. assert.Equal(t, px, Utils.PF.Mul(hx, zx))
  58. // p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
  59. abc := Utils.PF.Sub(Utils.PF.Mul(ax, bx), cx)
  60. assert.Equal(t, abc, px)
  61. hz := Utils.PF.Mul(hx, zx)
  62. assert.Equal(t, abc, hz)
  63. }