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.

176 lines
4.6 KiB

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