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.

186 lines
4.3 KiB

5 years ago
5 years ago
  1. package fields
  2. import (
  3. "bytes"
  4. "math/big"
  5. )
  6. // Transpose transposes the *big.Int matrix
  7. func Transpose(matrix [][]*big.Int) [][]*big.Int {
  8. var r [][]*big.Int
  9. for i := 0; i < len(matrix[0]); i++ {
  10. var row []*big.Int
  11. for j := 0; j < len(matrix); j++ {
  12. row = append(row, matrix[j][i])
  13. }
  14. r = append(r, row)
  15. }
  16. return r
  17. }
  18. // ArrayOfBigZeros creates a *big.Int array with n elements to zero
  19. func ArrayOfBigZeros(num int) []*big.Int {
  20. bigZero := big.NewInt(int64(0))
  21. var r []*big.Int
  22. for i := 0; i < num; i++ {
  23. r = append(r, bigZero)
  24. }
  25. return r
  26. }
  27. // BigArraysEqual is ...
  28. func BigArraysEqual(a, b []*big.Int) bool {
  29. if len(a) != len(b) {
  30. return false
  31. }
  32. for i := 0; i < len(a); i++ {
  33. if !bytes.Equal(a[i].Bytes(), b[i].Bytes()) {
  34. return false
  35. }
  36. }
  37. return true
  38. }
  39. // PF is the Polynomial over a Finite Field where the polynomial operations are performed
  40. type PF struct {
  41. F Fq
  42. }
  43. // NewPF creates a new PF with the given FiniteField
  44. func NewPF(f Fq) PF {
  45. return PF{
  46. f,
  47. }
  48. }
  49. // Mul multiplies two polinomials over the Finite Field
  50. func (pf PF) Mul(a, b []*big.Int) []*big.Int {
  51. r := ArrayOfBigZeros(len(a) + len(b) - 1)
  52. for i := 0; i < len(a); i++ {
  53. for j := 0; j < len(b); j++ {
  54. r[i+j] = pf.F.Add(
  55. r[i+j],
  56. pf.F.Mul(a[i], b[j]))
  57. }
  58. }
  59. return r
  60. }
  61. // Div divides two polinomials over the Finite Field, returning the result and the remainder
  62. func (pf PF) Div(a, b []*big.Int) ([]*big.Int, []*big.Int) {
  63. // https://en.wikipedia.org/wiki/Division_algorithm
  64. r := ArrayOfBigZeros(len(a) - len(b) + 1)
  65. rem := a
  66. for len(rem) >= len(b) {
  67. l := pf.F.Div(rem[len(rem)-1], b[len(b)-1])
  68. pos := len(rem) - len(b)
  69. r[pos] = l
  70. aux := ArrayOfBigZeros(pos)
  71. aux1 := append(aux, l)
  72. aux2 := pf.Sub(rem, pf.Mul(b, aux1))
  73. rem = aux2[:len(aux2)-1]
  74. }
  75. return r, rem
  76. }
  77. func max(a, b int) int {
  78. if a > b {
  79. return a
  80. }
  81. return b
  82. }
  83. // Add adds two polinomials over the Finite Field
  84. func (pf PF) Add(a, b []*big.Int) []*big.Int {
  85. r := ArrayOfBigZeros(max(len(a), len(b)))
  86. for i := 0; i < len(a); i++ {
  87. r[i] = pf.F.Add(r[i], a[i])
  88. }
  89. for i := 0; i < len(b); i++ {
  90. r[i] = pf.F.Add(r[i], b[i])
  91. }
  92. return r
  93. }
  94. // Sub subtracts two polinomials over the Finite Field
  95. func (pf PF) Sub(a, b []*big.Int) []*big.Int {
  96. r := ArrayOfBigZeros(max(len(a), len(b)))
  97. for i := 0; i < len(a); i++ {
  98. r[i] = pf.F.Add(r[i], a[i])
  99. }
  100. for i := 0; i < len(b); i++ {
  101. r[i] = pf.F.Sub(r[i], b[i])
  102. }
  103. return r
  104. }
  105. // Eval evaluates the polinomial over the Finite Field at the given value x
  106. func (pf PF) Eval(v []*big.Int, x *big.Int) *big.Int {
  107. r := big.NewInt(int64(0))
  108. for i := 0; i < len(v); i++ {
  109. xi := pf.F.Exp(x, big.NewInt(int64(i)))
  110. elem := pf.F.Mul(v[i], xi)
  111. r = pf.F.Add(r, elem)
  112. }
  113. return r
  114. }
  115. // NewPolZeroAt generates a new polynomial that has value zero at the given value
  116. func (pf PF) NewPolZeroAt(pointPos, totalPoints int, height *big.Int) []*big.Int {
  117. fac := 1
  118. for i := 1; i < totalPoints+1; i++ {
  119. if i != pointPos {
  120. fac = fac * (pointPos - i)
  121. }
  122. }
  123. facBig := big.NewInt(int64(fac))
  124. hf := pf.F.Div(height, facBig)
  125. r := []*big.Int{hf}
  126. for i := 1; i < totalPoints+1; i++ {
  127. if i != pointPos {
  128. ineg := big.NewInt(int64(-i))
  129. b1 := big.NewInt(int64(1))
  130. r = pf.Mul(r, []*big.Int{ineg, b1})
  131. }
  132. }
  133. return r
  134. }
  135. // LagrangeInterpolation performs the Lagrange Interpolation / Lagrange Polynomials operation
  136. func (pf PF) LagrangeInterpolation(v []*big.Int) []*big.Int {
  137. // https://en.wikipedia.org/wiki/Lagrange_polynomial
  138. var r []*big.Int
  139. for i := 0; i < len(v); i++ {
  140. r = pf.Add(r, pf.NewPolZeroAt(i+1, len(v), v[i]))
  141. }
  142. //
  143. return r
  144. }
  145. // CombinePolynomials combine the given polynomials arrays into one, also returns the P(x)
  146. func (pf PF) CombinePolynomials(r []*big.Int, ap, bp, cp [][]*big.Int) ([]*big.Int, []*big.Int, []*big.Int, []*big.Int) {
  147. var ax []*big.Int
  148. for i := 0; i < len(r); i++ {
  149. m := pf.Mul([]*big.Int{r[i]}, ap[i])
  150. ax = pf.Add(ax, m)
  151. }
  152. var bx []*big.Int
  153. for i := 0; i < len(r); i++ {
  154. m := pf.Mul([]*big.Int{r[i]}, bp[i])
  155. bx = pf.Add(bx, m)
  156. }
  157. var cx []*big.Int
  158. for i := 0; i < len(r); i++ {
  159. m := pf.Mul([]*big.Int{r[i]}, cp[i])
  160. cx = pf.Add(cx, m)
  161. }
  162. px := pf.Sub(pf.Mul(ax, bx), cx)
  163. return ax, bx, cx, px
  164. }
  165. // DivisorPolynomial returns the divisor polynomial given two polynomials
  166. func (pf PF) DivisorPolynomial(px, z []*big.Int) []*big.Int {
  167. quo, _ := pf.Div(px, z)
  168. return quo
  169. }