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.

161 lines
4.1 KiB

  1. package r1csqap
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/big"
  6. "testing"
  7. "github.com/arnaucube/go-snark/fields"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestTranspose(t *testing.T) {
  11. b0 := big.NewInt(int64(0))
  12. b1 := big.NewInt(int64(1))
  13. bFive := big.NewInt(int64(5))
  14. a := [][]*big.Int{
  15. []*big.Int{b0, b1, b0, b0, b0, b0},
  16. []*big.Int{b0, b0, b0, b1, b0, b0},
  17. []*big.Int{b0, b1, b0, b0, b1, b0},
  18. []*big.Int{bFive, b0, b0, b0, b0, b1},
  19. }
  20. aT := Transpose(a)
  21. assert.Equal(t, aT, [][]*big.Int{
  22. []*big.Int{b0, b0, b0, bFive},
  23. []*big.Int{b1, b0, b1, b0},
  24. []*big.Int{b0, b0, b0, b0},
  25. []*big.Int{b0, b1, b0, b0},
  26. []*big.Int{b0, b0, b1, b0},
  27. []*big.Int{b0, b0, b0, b1},
  28. })
  29. }
  30. func TestPol(t *testing.T) {
  31. b0 := big.NewInt(int64(0))
  32. b1 := big.NewInt(int64(1))
  33. b3 := big.NewInt(int64(3))
  34. b4 := big.NewInt(int64(4))
  35. b5 := big.NewInt(int64(5))
  36. b6 := big.NewInt(int64(6))
  37. b16 := big.NewInt(int64(16))
  38. a := []*big.Int{b1, b0, b5}
  39. b := []*big.Int{b3, b0, b1}
  40. // new Finite Field
  41. r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  42. assert.True(nil, ok)
  43. f := fields.NewFq(r)
  44. // new Polynomial Field
  45. pf := NewPolynomialField(f)
  46. // polynomial multiplication
  47. o := pf.Mul(a, b)
  48. assert.Equal(t, o, []*big.Int{b3, b0, b16, b0, b5})
  49. // polynomial addition
  50. o = pf.Add(a, b)
  51. assert.Equal(t, o, []*big.Int{b4, b0, b6})
  52. // polynomial subtraction
  53. o1 := pf.Sub(a, b)
  54. o2 := pf.Sub(b, a)
  55. o = pf.Add(o1, o2)
  56. assert.True(t, bytes.Equal(b0.Bytes(), o[0].Bytes()))
  57. assert.True(t, bytes.Equal(b0.Bytes(), o[1].Bytes()))
  58. assert.True(t, bytes.Equal(b0.Bytes(), o[2].Bytes()))
  59. c := []*big.Int{b5, b6, b1}
  60. d := []*big.Int{b1, b3}
  61. o = pf.Sub(c, d)
  62. assert.Equal(t, o, []*big.Int{b4, b3, b1})
  63. // NewPolZeroAt
  64. o = pf.NewPolZeroAt(3, 4, b4)
  65. assert.Equal(t, pf.Eval(o, big.NewInt(3)), b4)
  66. o = pf.NewPolZeroAt(2, 4, b3)
  67. assert.Equal(t, pf.Eval(o, big.NewInt(2)), b3)
  68. }
  69. func TestLagrangeInterpolation(t *testing.T) {
  70. // new Finite Field
  71. r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  72. assert.True(nil, ok)
  73. f := fields.NewFq(r)
  74. // new Polynomial Field
  75. pf := NewPolynomialField(f)
  76. b0 := big.NewInt(int64(0))
  77. b5 := big.NewInt(int64(5))
  78. a := []*big.Int{b0, b0, b0, b5}
  79. alpha := pf.LagrangeInterpolation(a)
  80. assert.Equal(t, pf.Eval(alpha, big.NewInt(int64(4))), b5)
  81. aux := pf.Eval(alpha, big.NewInt(int64(3))).Int64()
  82. assert.Equal(t, aux, int64(0))
  83. }
  84. func TestR1CSToQAP(t *testing.T) {
  85. // new Finite Field
  86. r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  87. assert.True(nil, ok)
  88. f := fields.NewFq(r)
  89. // new Polynomial Field
  90. pf := NewPolynomialField(f)
  91. b0 := big.NewInt(int64(0))
  92. b1 := big.NewInt(int64(1))
  93. b3 := big.NewInt(int64(3))
  94. b5 := big.NewInt(int64(5))
  95. b9 := big.NewInt(int64(9))
  96. b27 := big.NewInt(int64(27))
  97. b30 := big.NewInt(int64(30))
  98. b35 := big.NewInt(int64(35))
  99. a := [][]*big.Int{
  100. []*big.Int{b0, b1, b0, b0, b0, b0},
  101. []*big.Int{b0, b0, b0, b1, b0, b0},
  102. []*big.Int{b0, b1, b0, b0, b1, b0},
  103. []*big.Int{b5, b0, b0, b0, b0, b1},
  104. }
  105. b := [][]*big.Int{
  106. []*big.Int{b0, b1, b0, b0, b0, b0},
  107. []*big.Int{b0, b1, b0, b0, b0, b0},
  108. []*big.Int{b1, b0, b0, b0, b0, b0},
  109. []*big.Int{b1, b0, b0, b0, b0, b0},
  110. }
  111. c := [][]*big.Int{
  112. []*big.Int{b0, b0, b0, b1, b0, b0},
  113. []*big.Int{b0, b0, b0, b0, b1, b0},
  114. []*big.Int{b0, b0, b0, b0, b0, b1},
  115. []*big.Int{b0, b0, b1, b0, b0, b0},
  116. }
  117. alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
  118. fmt.Println(alphas)
  119. fmt.Println(betas)
  120. fmt.Println(gammas)
  121. fmt.Print("Z(x): ")
  122. fmt.Println(zx)
  123. w := []*big.Int{b1, b3, b35, b9, b27, b30}
  124. ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
  125. fmt.Println(ax)
  126. fmt.Println(bx)
  127. fmt.Println(cx)
  128. fmt.Println(px)
  129. hx := pf.DivisorPolinomial(px, zx)
  130. fmt.Println(hx)
  131. // hx==px/zx so px==hx*zx
  132. assert.Equal(t, px, pf.Mul(hx, zx))
  133. // p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
  134. abc := pf.Sub(pf.Mul(ax, bx), cx)
  135. assert.Equal(t, abc, px)
  136. hz := pf.Mul(hx, zx)
  137. assert.Equal(t, abc, hz)
  138. }