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.

184 lines
4.1 KiB

  1. package r1csqap
  2. import (
  3. "math/big"
  4. "github.com/arnaucube/go-snark/fields"
  5. )
  6. func Transpose(matrix [][]*big.Int) [][]*big.Int {
  7. var r [][]*big.Int
  8. for i := 0; i < len(matrix[0]); i++ {
  9. var row []*big.Int
  10. for j := 0; j < len(matrix); j++ {
  11. row = append(row, matrix[j][i])
  12. }
  13. r = append(r, row)
  14. }
  15. return r
  16. }
  17. func ArrayOfBigZeros(num int) []*big.Int {
  18. bigZero := big.NewInt(int64(0))
  19. var r []*big.Int
  20. for i := 0; i < num; i++ {
  21. r = append(r, bigZero)
  22. }
  23. return r
  24. }
  25. type PolynomialField struct {
  26. F fields.Fq
  27. }
  28. func NewPolynomialField(f fields.Fq) PolynomialField {
  29. return PolynomialField{
  30. f,
  31. }
  32. }
  33. func (pf PolynomialField) Mul(a, b []*big.Int) []*big.Int {
  34. r := ArrayOfBigZeros(len(a) + len(b) - 1)
  35. for i := 0; i < len(a); i++ {
  36. for j := 0; j < len(b); j++ {
  37. r[i+j] = pf.F.Add(
  38. r[i+j],
  39. pf.F.Mul(a[i], b[j]))
  40. }
  41. }
  42. return r
  43. }
  44. func (pf PolynomialField) Div(a, b []*big.Int) ([]*big.Int, []*big.Int) {
  45. // https://en.wikipedia.org/wiki/Division_algorithm
  46. r := ArrayOfBigZeros(len(a) - len(b) + 1)
  47. rem := a
  48. for len(rem) >= len(b) {
  49. l := pf.F.Div(rem[len(rem)-1], b[len(b)-1])
  50. pos := len(rem) - len(b)
  51. r[pos] = l
  52. aux := ArrayOfBigZeros(pos)
  53. aux1 := append(aux, l)
  54. aux2 := pf.Sub(rem, pf.Mul(b, aux1))
  55. rem = aux2[:len(aux2)-1]
  56. }
  57. return r, rem
  58. }
  59. func max(a, b int) int {
  60. if a > b {
  61. return a
  62. }
  63. return b
  64. }
  65. func (pf PolynomialField) Add(a, b []*big.Int) []*big.Int {
  66. r := ArrayOfBigZeros(max(len(a), len(b)))
  67. for i := 0; i < len(a); i++ {
  68. r[i] = pf.F.Add(r[i], a[i])
  69. }
  70. for i := 0; i < len(b); i++ {
  71. r[i] = pf.F.Add(r[i], b[i])
  72. }
  73. return r
  74. }
  75. func (pf PolynomialField) Sub(a, b []*big.Int) []*big.Int {
  76. r := ArrayOfBigZeros(max(len(a), len(b)))
  77. for i := 0; i < len(a); i++ {
  78. r[i] = pf.F.Add(r[i], a[i])
  79. }
  80. for i := 0; i < len(b); i++ {
  81. r[i] = pf.F.Sub(r[i], b[i])
  82. }
  83. return r
  84. }
  85. func (pf PolynomialField) Eval(v []*big.Int, x *big.Int) *big.Int {
  86. r := big.NewInt(int64(0))
  87. for i := 0; i < len(v); i++ {
  88. xi := pf.F.Exp(x, big.NewInt(int64(i)))
  89. elem := pf.F.Mul(v[i], xi)
  90. r = pf.F.Add(r, elem)
  91. }
  92. return r
  93. }
  94. func (pf PolynomialField) NewPolZeroAt(pointPos, totalPoints int, height *big.Int) []*big.Int {
  95. fac := 1
  96. for i := 1; i < totalPoints+1; i++ {
  97. if i != pointPos {
  98. fac = fac * (pointPos - i)
  99. }
  100. }
  101. facBig := big.NewInt(int64(fac))
  102. hf := pf.F.Div(height, facBig)
  103. r := []*big.Int{hf}
  104. for i := 1; i < totalPoints+1; i++ {
  105. if i != pointPos {
  106. ineg := big.NewInt(int64(-i))
  107. b1 := big.NewInt(int64(1))
  108. r = pf.Mul(r, []*big.Int{ineg, b1})
  109. }
  110. }
  111. return r
  112. }
  113. func (pf PolynomialField) LagrangeInterpolation(v []*big.Int) []*big.Int {
  114. // https://en.wikipedia.org/wiki/Lagrange_polynomial
  115. var r []*big.Int
  116. for i := 0; i < len(v); i++ {
  117. r = pf.Add(r, pf.NewPolZeroAt(i+1, len(v), v[i]))
  118. }
  119. //
  120. return r
  121. }
  122. func (pf PolynomialField) R1CSToQAP(a, b, c [][]*big.Int) ([][]*big.Int, [][]*big.Int, [][]*big.Int, []*big.Int) {
  123. aT := Transpose(a)
  124. bT := Transpose(b)
  125. cT := Transpose(c)
  126. var alphas [][]*big.Int
  127. for i := 0; i < len(aT); i++ {
  128. alphas = append(alphas, pf.LagrangeInterpolation(aT[i]))
  129. }
  130. var betas [][]*big.Int
  131. for i := 0; i < len(bT); i++ {
  132. betas = append(betas, pf.LagrangeInterpolation(bT[i]))
  133. }
  134. var gammas [][]*big.Int
  135. for i := 0; i < len(cT); i++ {
  136. gammas = append(gammas, pf.LagrangeInterpolation(cT[i]))
  137. }
  138. z := []*big.Int{big.NewInt(int64(1))}
  139. for i := 1; i < len(aT[0])+1; i++ {
  140. ineg := big.NewInt(int64(-i))
  141. b1 := big.NewInt(int64(1))
  142. z = pf.Mul(z, []*big.Int{ineg, b1})
  143. }
  144. return alphas, betas, gammas, z
  145. }
  146. func (pf PolynomialField) CombinePolynomials(r []*big.Int, ap, bp, cp [][]*big.Int) ([]*big.Int, []*big.Int, []*big.Int, []*big.Int) {
  147. var alpha []*big.Int
  148. for i := 0; i < len(r); i++ {
  149. m := pf.Mul([]*big.Int{r[i]}, ap[i])
  150. alpha = pf.Add(alpha, m)
  151. }
  152. var beta []*big.Int
  153. for i := 0; i < len(r); i++ {
  154. m := pf.Mul([]*big.Int{r[i]}, bp[i])
  155. beta = pf.Add(beta, m)
  156. }
  157. var gamma []*big.Int
  158. for i := 0; i < len(r); i++ {
  159. m := pf.Mul([]*big.Int{r[i]}, cp[i])
  160. gamma = pf.Add(gamma, m)
  161. }
  162. px := pf.Sub(pf.Mul(alpha, beta), gamma)
  163. return alpha, beta, gamma, px
  164. }
  165. func (pf PolynomialField) DivisorPolinomial(px, z []*big.Int) []*big.Int {
  166. quo, _ := pf.Div(px, z)
  167. return quo
  168. }