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.

171 lines
3.6 KiB

  1. package bn128
  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.One(), fq6.F.One()}
  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 substraction 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. res := fq6.Zero()
  85. rem := e
  86. exp := base
  87. for !bytes.Equal(rem.Bytes(), big.NewInt(int64(0)).Bytes()) {
  88. // if rem % 2 == 1
  89. if bytes.Equal(new(big.Int).Rem(rem, big.NewInt(int64(2))).Bytes(), big.NewInt(int64(1)).Bytes()) {
  90. res = fq6.Add(res, exp)
  91. }
  92. exp = fq6.Double(exp)
  93. rem = rem.Rsh(rem, 1) // rem = rem >> 1
  94. }
  95. return res
  96. }
  97. // Inverse returns the inverse on the Fq6
  98. func (fq6 Fq6) Inverse(a [3][2]*big.Int) [3][2]*big.Int {
  99. t0 := fq6.F.Square(a[0])
  100. t1 := fq6.F.Square(a[1])
  101. t2 := fq6.F.Square(a[2])
  102. t3 := fq6.F.Mul(a[0], a[1])
  103. t4 := fq6.F.Mul(a[0], a[2])
  104. t5 := fq6.F.Mul(a[1], a[2])
  105. c0 := fq6.F.Sub(t0, fq6.mulByNonResidue(t5))
  106. c1 := fq6.F.Sub(fq6.mulByNonResidue(t2), t3)
  107. c2 := fq6.F.Sub(t1, t4)
  108. t6 := fq6.F.Inverse(
  109. fq6.F.Add(
  110. fq6.F.Mul(a[0], c0),
  111. fq6.mulByNonResidue(
  112. fq6.F.Add(
  113. fq6.F.Mul(a[2], c1),
  114. fq6.F.Mul(a[1], c2)))))
  115. return [3][2]*big.Int{
  116. fq6.F.Mul(t6, c0),
  117. fq6.F.Mul(t6, c1),
  118. fq6.F.Mul(t6, c2),
  119. }
  120. }
  121. // Div performs a division on the Fq6
  122. func (fq6 Fq6) Div(a, b [3][2]*big.Int) [3][2]*big.Int {
  123. return fq6.Mul(a, fq6.Inverse(b))
  124. }
  125. // Square performs a square operation on the Fq6
  126. func (fq6 Fq6) Square(a [3][2]*big.Int) [3][2]*big.Int {
  127. s0 := fq6.F.Square(a[0])
  128. ab := fq6.F.Mul(a[0], a[1])
  129. s1 := fq6.F.Add(ab, ab)
  130. s2 := fq6.F.Square(
  131. fq6.F.Add(
  132. fq6.F.Sub(a[0], a[1]),
  133. a[2]))
  134. bc := fq6.F.Mul(a[1], a[2])
  135. s3 := fq6.F.Add(bc, bc)
  136. s4 := fq6.F.Square(a[2])
  137. return [3][2]*big.Int{
  138. fq6.F.Add(
  139. s0,
  140. fq6.mulByNonResidue(s3)),
  141. fq6.F.Add(
  142. s1,
  143. fq6.mulByNonResidue(s4)),
  144. fq6.F.Sub(
  145. fq6.F.Add(
  146. fq6.F.Add(s1, s2),
  147. s3),
  148. fq6.F.Add(s0, s4)),
  149. }
  150. }