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.

192 lines
4.1 KiB

  1. package fields
  2. import (
  3. "bytes"
  4. "math/big"
  5. )
  6. // Fq6 is Field 6
  7. type Fq6 struct {
  8. F Fq2
  9. NonResidue [2]*big.Int
  10. }
  11. // NewFq6 generates a new Fq6
  12. func NewFq6(f Fq2, nonResidue [2]*big.Int) Fq6 {
  13. fq6 := Fq6{
  14. f,
  15. nonResidue,
  16. }
  17. return fq6
  18. }
  19. // Zero returns a Zero value on the Fq6
  20. func (fq6 Fq6) Zero() [3][2]*big.Int {
  21. return [3][2]*big.Int{fq6.F.Zero(), fq6.F.Zero(), fq6.F.Zero()}
  22. }
  23. // One returns a One value on the Fq6
  24. func (fq6 Fq6) One() [3][2]*big.Int {
  25. return [3][2]*big.Int{fq6.F.One(), fq6.F.Zero(), fq6.F.Zero()}
  26. }
  27. func (fq6 Fq6) mulByNonResidue(a [2]*big.Int) [2]*big.Int {
  28. return fq6.F.Mul(fq6.NonResidue, a)
  29. }
  30. // Add performs an addition on the Fq6
  31. func (fq6 Fq6) Add(a, b [3][2]*big.Int) [3][2]*big.Int {
  32. return [3][2]*big.Int{
  33. fq6.F.Add(a[0], b[0]),
  34. fq6.F.Add(a[1], b[1]),
  35. fq6.F.Add(a[2], b[2]),
  36. }
  37. }
  38. func (fq6 Fq6) Double(a [3][2]*big.Int) [3][2]*big.Int {
  39. return fq6.Add(a, a)
  40. }
  41. // Sub performs a subtraction on the Fq6
  42. func (fq6 Fq6) Sub(a, b [3][2]*big.Int) [3][2]*big.Int {
  43. return [3][2]*big.Int{
  44. fq6.F.Sub(a[0], b[0]),
  45. fq6.F.Sub(a[1], b[1]),
  46. fq6.F.Sub(a[2], b[2]),
  47. }
  48. }
  49. // Neg performs a negation on the Fq6
  50. func (fq6 Fq6) Neg(a [3][2]*big.Int) [3][2]*big.Int {
  51. return fq6.Sub(fq6.Zero(), a)
  52. }
  53. // Mul performs a multiplication on the Fq6
  54. func (fq6 Fq6) Mul(a, b [3][2]*big.Int) [3][2]*big.Int {
  55. v0 := fq6.F.Mul(a[0], b[0])
  56. v1 := fq6.F.Mul(a[1], b[1])
  57. v2 := fq6.F.Mul(a[2], b[2])
  58. return [3][2]*big.Int{
  59. fq6.F.Add(
  60. v0,
  61. fq6.mulByNonResidue(
  62. fq6.F.Sub(
  63. fq6.F.Mul(
  64. fq6.F.Add(a[1], a[2]),
  65. fq6.F.Add(b[1], b[2])),
  66. fq6.F.Add(v1, v2)))),
  67. fq6.F.Add(
  68. fq6.F.Sub(
  69. fq6.F.Mul(
  70. fq6.F.Add(a[0], a[1]),
  71. fq6.F.Add(b[0], b[1])),
  72. fq6.F.Add(v0, v1)),
  73. fq6.mulByNonResidue(v2)),
  74. fq6.F.Add(
  75. fq6.F.Sub(
  76. fq6.F.Mul(
  77. fq6.F.Add(a[0], a[2]),
  78. fq6.F.Add(b[0], b[2])),
  79. fq6.F.Add(v0, v2)),
  80. v1),
  81. }
  82. }
  83. func (fq6 Fq6) MulScalar(base [3][2]*big.Int, e *big.Int) [3][2]*big.Int {
  84. // for more possible implementations see g2.go file, at the function g2.MulScalar()
  85. res := fq6.Zero()
  86. rem := e
  87. exp := base
  88. for !bytes.Equal(rem.Bytes(), big.NewInt(int64(0)).Bytes()) {
  89. // if rem % 2 == 1
  90. if bytes.Equal(new(big.Int).Rem(rem, big.NewInt(int64(2))).Bytes(), big.NewInt(int64(1)).Bytes()) {
  91. res = fq6.Add(res, exp)
  92. }
  93. exp = fq6.Double(exp)
  94. rem = rem.Rsh(rem, 1) // rem = rem >> 1
  95. }
  96. return res
  97. }
  98. // Inverse returns the inverse on the Fq6
  99. func (fq6 Fq6) Inverse(a [3][2]*big.Int) [3][2]*big.Int {
  100. t0 := fq6.F.Square(a[0])
  101. t1 := fq6.F.Square(a[1])
  102. t2 := fq6.F.Square(a[2])
  103. t3 := fq6.F.Mul(a[0], a[1])
  104. t4 := fq6.F.Mul(a[0], a[2])
  105. t5 := fq6.F.Mul(a[1], a[2])
  106. c0 := fq6.F.Sub(t0, fq6.mulByNonResidue(t5))
  107. c1 := fq6.F.Sub(fq6.mulByNonResidue(t2), t3)
  108. c2 := fq6.F.Sub(t1, t4)
  109. t6 := fq6.F.Inverse(
  110. fq6.F.Add(
  111. fq6.F.Mul(a[0], c0),
  112. fq6.mulByNonResidue(
  113. fq6.F.Add(
  114. fq6.F.Mul(a[2], c1),
  115. fq6.F.Mul(a[1], c2)))))
  116. return [3][2]*big.Int{
  117. fq6.F.Mul(t6, c0),
  118. fq6.F.Mul(t6, c1),
  119. fq6.F.Mul(t6, c2),
  120. }
  121. }
  122. // Div performs a division on the Fq6
  123. func (fq6 Fq6) Div(a, b [3][2]*big.Int) [3][2]*big.Int {
  124. return fq6.Mul(a, fq6.Inverse(b))
  125. }
  126. // Square performs a square operation on the Fq6
  127. func (fq6 Fq6) Square(a [3][2]*big.Int) [3][2]*big.Int {
  128. s0 := fq6.F.Square(a[0])
  129. ab := fq6.F.Mul(a[0], a[1])
  130. s1 := fq6.F.Add(ab, ab)
  131. s2 := fq6.F.Square(
  132. fq6.F.Add(
  133. fq6.F.Sub(a[0], a[1]),
  134. a[2]))
  135. bc := fq6.F.Mul(a[1], a[2])
  136. s3 := fq6.F.Add(bc, bc)
  137. s4 := fq6.F.Square(a[2])
  138. return [3][2]*big.Int{
  139. fq6.F.Add(
  140. s0,
  141. fq6.mulByNonResidue(s3)),
  142. fq6.F.Add(
  143. s1,
  144. fq6.mulByNonResidue(s4)),
  145. fq6.F.Sub(
  146. fq6.F.Add(
  147. fq6.F.Add(s1, s2),
  148. s3),
  149. fq6.F.Add(s0, s4)),
  150. }
  151. }
  152. func (fq6 Fq6) Affine(a [3][2]*big.Int) [3][2]*big.Int {
  153. return [3][2]*big.Int{
  154. fq6.F.Affine(a[0]),
  155. fq6.F.Affine(a[1]),
  156. fq6.F.Affine(a[2]),
  157. }
  158. }
  159. func (fq6 Fq6) Equal(a, b [3][2]*big.Int) bool {
  160. return fq6.F.Equal(a[0], b[0]) && fq6.F.Equal(a[1], b[1]) && fq6.F.Equal(a[2], b[2])
  161. }
  162. func (fq6 Fq6) Copy(a [3][2]*big.Int) [3][2]*big.Int {
  163. return [3][2]*big.Int{
  164. fq6.F.Copy(a[0]),
  165. fq6.F.Copy(a[1]),
  166. fq6.F.Copy(a[2]),
  167. }
  168. }