fp12.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package bls12381
  17. import (
  18. "errors"
  19. "math/big"
  20. )
  21. type fp12 struct {
  22. fp12temp
  23. fp6 *fp6
  24. }
  25. type fp12temp struct {
  26. t2 [9]*fe2
  27. t6 [5]*fe6
  28. t12 *fe12
  29. }
  30. func newFp12Temp() fp12temp {
  31. t2 := [9]*fe2{}
  32. t6 := [5]*fe6{}
  33. for i := 0; i < len(t2); i++ {
  34. t2[i] = &fe2{}
  35. }
  36. for i := 0; i < len(t6); i++ {
  37. t6[i] = &fe6{}
  38. }
  39. return fp12temp{t2, t6, &fe12{}}
  40. }
  41. func newFp12(fp6 *fp6) *fp12 {
  42. t := newFp12Temp()
  43. if fp6 == nil {
  44. return &fp12{t, newFp6(nil)}
  45. }
  46. return &fp12{t, fp6}
  47. }
  48. func (e *fp12) fp2() *fp2 {
  49. return e.fp6.fp2
  50. }
  51. func (e *fp12) fromBytes(in []byte) (*fe12, error) {
  52. if len(in) != 576 {
  53. return nil, errors.New("input string should be larger than 96 bytes")
  54. }
  55. fp6 := e.fp6
  56. c1, err := fp6.fromBytes(in[:288])
  57. if err != nil {
  58. return nil, err
  59. }
  60. c0, err := fp6.fromBytes(in[288:])
  61. if err != nil {
  62. return nil, err
  63. }
  64. return &fe12{*c0, *c1}, nil
  65. }
  66. func (e *fp12) toBytes(a *fe12) []byte {
  67. fp6 := e.fp6
  68. out := make([]byte, 576)
  69. copy(out[:288], fp6.toBytes(&a[1]))
  70. copy(out[288:], fp6.toBytes(&a[0]))
  71. return out
  72. }
  73. func (e *fp12) new() *fe12 {
  74. return new(fe12)
  75. }
  76. func (e *fp12) zero() *fe12 {
  77. return new(fe12)
  78. }
  79. func (e *fp12) one() *fe12 {
  80. return new(fe12).one()
  81. }
  82. func (e *fp12) add(c, a, b *fe12) {
  83. fp6 := e.fp6
  84. fp6.add(&c[0], &a[0], &b[0])
  85. fp6.add(&c[1], &a[1], &b[1])
  86. }
  87. func (e *fp12) double(c, a *fe12) {
  88. fp6 := e.fp6
  89. fp6.double(&c[0], &a[0])
  90. fp6.double(&c[1], &a[1])
  91. }
  92. func (e *fp12) sub(c, a, b *fe12) {
  93. fp6 := e.fp6
  94. fp6.sub(&c[0], &a[0], &b[0])
  95. fp6.sub(&c[1], &a[1], &b[1])
  96. }
  97. func (e *fp12) neg(c, a *fe12) {
  98. fp6 := e.fp6
  99. fp6.neg(&c[0], &a[0])
  100. fp6.neg(&c[1], &a[1])
  101. }
  102. func (e *fp12) conjugate(c, a *fe12) {
  103. fp6 := e.fp6
  104. c[0].set(&a[0])
  105. fp6.neg(&c[1], &a[1])
  106. }
  107. func (e *fp12) square(c, a *fe12) {
  108. fp6, t := e.fp6, e.t6
  109. fp6.add(t[0], &a[0], &a[1])
  110. fp6.mul(t[2], &a[0], &a[1])
  111. fp6.mulByNonResidue(t[1], &a[1])
  112. fp6.addAssign(t[1], &a[0])
  113. fp6.mulByNonResidue(t[3], t[2])
  114. fp6.mulAssign(t[0], t[1])
  115. fp6.subAssign(t[0], t[2])
  116. fp6.sub(&c[0], t[0], t[3])
  117. fp6.double(&c[1], t[2])
  118. }
  119. func (e *fp12) cyclotomicSquare(c, a *fe12) {
  120. t, fp2 := e.t2, e.fp2()
  121. e.fp4Square(t[3], t[4], &a[0][0], &a[1][1])
  122. fp2.sub(t[2], t[3], &a[0][0])
  123. fp2.doubleAssign(t[2])
  124. fp2.add(&c[0][0], t[2], t[3])
  125. fp2.add(t[2], t[4], &a[1][1])
  126. fp2.doubleAssign(t[2])
  127. fp2.add(&c[1][1], t[2], t[4])
  128. e.fp4Square(t[3], t[4], &a[1][0], &a[0][2])
  129. e.fp4Square(t[5], t[6], &a[0][1], &a[1][2])
  130. fp2.sub(t[2], t[3], &a[0][1])
  131. fp2.doubleAssign(t[2])
  132. fp2.add(&c[0][1], t[2], t[3])
  133. fp2.add(t[2], t[4], &a[1][2])
  134. fp2.doubleAssign(t[2])
  135. fp2.add(&c[1][2], t[2], t[4])
  136. fp2.mulByNonResidue(t[3], t[6])
  137. fp2.add(t[2], t[3], &a[1][0])
  138. fp2.doubleAssign(t[2])
  139. fp2.add(&c[1][0], t[2], t[3])
  140. fp2.sub(t[2], t[5], &a[0][2])
  141. fp2.doubleAssign(t[2])
  142. fp2.add(&c[0][2], t[2], t[5])
  143. }
  144. func (e *fp12) mul(c, a, b *fe12) {
  145. t, fp6 := e.t6, e.fp6
  146. fp6.mul(t[1], &a[0], &b[0])
  147. fp6.mul(t[2], &a[1], &b[1])
  148. fp6.add(t[0], t[1], t[2])
  149. fp6.mulByNonResidue(t[2], t[2])
  150. fp6.add(t[3], t[1], t[2])
  151. fp6.add(t[1], &a[0], &a[1])
  152. fp6.add(t[2], &b[0], &b[1])
  153. fp6.mulAssign(t[1], t[2])
  154. c[0].set(t[3])
  155. fp6.sub(&c[1], t[1], t[0])
  156. }
  157. func (e *fp12) mulAssign(a, b *fe12) {
  158. t, fp6 := e.t6, e.fp6
  159. fp6.mul(t[1], &a[0], &b[0])
  160. fp6.mul(t[2], &a[1], &b[1])
  161. fp6.add(t[0], t[1], t[2])
  162. fp6.mulByNonResidue(t[2], t[2])
  163. fp6.add(t[3], t[1], t[2])
  164. fp6.add(t[1], &a[0], &a[1])
  165. fp6.add(t[2], &b[0], &b[1])
  166. fp6.mulAssign(t[1], t[2])
  167. a[0].set(t[3])
  168. fp6.sub(&a[1], t[1], t[0])
  169. }
  170. func (e *fp12) fp4Square(c0, c1, a0, a1 *fe2) {
  171. t, fp2 := e.t2, e.fp2()
  172. fp2.square(t[0], a0)
  173. fp2.square(t[1], a1)
  174. fp2.mulByNonResidue(t[2], t[1])
  175. fp2.add(c0, t[2], t[0])
  176. fp2.add(t[2], a0, a1)
  177. fp2.squareAssign(t[2])
  178. fp2.subAssign(t[2], t[0])
  179. fp2.sub(c1, t[2], t[1])
  180. }
  181. func (e *fp12) inverse(c, a *fe12) {
  182. fp6, t := e.fp6, e.t6
  183. fp6.square(t[0], &a[0])
  184. fp6.square(t[1], &a[1])
  185. fp6.mulByNonResidue(t[1], t[1])
  186. fp6.sub(t[1], t[0], t[1])
  187. fp6.inverse(t[0], t[1])
  188. fp6.mul(&c[0], &a[0], t[0])
  189. fp6.mulAssign(t[0], &a[1])
  190. fp6.neg(&c[1], t[0])
  191. }
  192. func (e *fp12) mulBy014Assign(a *fe12, c0, c1, c4 *fe2) {
  193. fp2, fp6, t, t2 := e.fp2(), e.fp6, e.t6, e.t2[0]
  194. fp6.mulBy01(t[0], &a[0], c0, c1)
  195. fp6.mulBy1(t[1], &a[1], c4)
  196. fp2.add(t2, c1, c4)
  197. fp6.add(t[2], &a[1], &a[0])
  198. fp6.mulBy01Assign(t[2], c0, t2)
  199. fp6.subAssign(t[2], t[0])
  200. fp6.sub(&a[1], t[2], t[1])
  201. fp6.mulByNonResidue(t[1], t[1])
  202. fp6.add(&a[0], t[1], t[0])
  203. }
  204. func (e *fp12) exp(c, a *fe12, s *big.Int) {
  205. z := e.one()
  206. for i := s.BitLen() - 1; i >= 0; i-- {
  207. e.square(z, z)
  208. if s.Bit(i) == 1 {
  209. e.mul(z, z, a)
  210. }
  211. }
  212. c.set(z)
  213. }
  214. func (e *fp12) cyclotomicExp(c, a *fe12, s *big.Int) {
  215. z := e.one()
  216. for i := s.BitLen() - 1; i >= 0; i-- {
  217. e.cyclotomicSquare(z, z)
  218. if s.Bit(i) == 1 {
  219. e.mul(z, z, a)
  220. }
  221. }
  222. c.set(z)
  223. }
  224. func (e *fp12) frobeniusMap(c, a *fe12, power uint) {
  225. fp6 := e.fp6
  226. fp6.frobeniusMap(&c[0], &a[0], power)
  227. fp6.frobeniusMap(&c[1], &a[1], power)
  228. switch power {
  229. case 0:
  230. return
  231. case 6:
  232. fp6.neg(&c[1], &c[1])
  233. default:
  234. fp6.mulByBaseField(&c[1], &c[1], &frobeniusCoeffs12[power])
  235. }
  236. }
  237. func (e *fp12) frobeniusMapAssign(a *fe12, power uint) {
  238. fp6 := e.fp6
  239. fp6.frobeniusMapAssign(&a[0], power)
  240. fp6.frobeniusMapAssign(&a[1], power)
  241. switch power {
  242. case 0:
  243. return
  244. case 6:
  245. fp6.neg(&a[1], &a[1])
  246. default:
  247. fp6.mulByBaseField(&a[1], &a[1], &frobeniusCoeffs12[power])
  248. }
  249. }