fp2.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 fp2Temp struct {
  22. t [4]*fe
  23. }
  24. type fp2 struct {
  25. fp2Temp
  26. }
  27. func newFp2Temp() fp2Temp {
  28. t := [4]*fe{}
  29. for i := 0; i < len(t); i++ {
  30. t[i] = &fe{}
  31. }
  32. return fp2Temp{t}
  33. }
  34. func newFp2() *fp2 {
  35. t := newFp2Temp()
  36. return &fp2{t}
  37. }
  38. func (e *fp2) fromBytes(in []byte) (*fe2, error) {
  39. if len(in) != 96 {
  40. return nil, errors.New("length of input string should be 96 bytes")
  41. }
  42. c1, err := fromBytes(in[:48])
  43. if err != nil {
  44. return nil, err
  45. }
  46. c0, err := fromBytes(in[48:])
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &fe2{*c0, *c1}, nil
  51. }
  52. func (e *fp2) toBytes(a *fe2) []byte {
  53. out := make([]byte, 96)
  54. copy(out[:48], toBytes(&a[1]))
  55. copy(out[48:], toBytes(&a[0]))
  56. return out
  57. }
  58. func (e *fp2) new() *fe2 {
  59. return new(fe2).zero()
  60. }
  61. func (e *fp2) zero() *fe2 {
  62. return new(fe2).zero()
  63. }
  64. func (e *fp2) one() *fe2 {
  65. return new(fe2).one()
  66. }
  67. func (e *fp2) add(c, a, b *fe2) {
  68. add(&c[0], &a[0], &b[0])
  69. add(&c[1], &a[1], &b[1])
  70. }
  71. func (e *fp2) addAssign(a, b *fe2) {
  72. addAssign(&a[0], &b[0])
  73. addAssign(&a[1], &b[1])
  74. }
  75. func (e *fp2) ladd(c, a, b *fe2) {
  76. ladd(&c[0], &a[0], &b[0])
  77. ladd(&c[1], &a[1], &b[1])
  78. }
  79. func (e *fp2) double(c, a *fe2) {
  80. double(&c[0], &a[0])
  81. double(&c[1], &a[1])
  82. }
  83. func (e *fp2) doubleAssign(a *fe2) {
  84. doubleAssign(&a[0])
  85. doubleAssign(&a[1])
  86. }
  87. func (e *fp2) ldouble(c, a *fe2) {
  88. ldouble(&c[0], &a[0])
  89. ldouble(&c[1], &a[1])
  90. }
  91. func (e *fp2) sub(c, a, b *fe2) {
  92. sub(&c[0], &a[0], &b[0])
  93. sub(&c[1], &a[1], &b[1])
  94. }
  95. func (e *fp2) subAssign(c, a *fe2) {
  96. subAssign(&c[0], &a[0])
  97. subAssign(&c[1], &a[1])
  98. }
  99. func (e *fp2) neg(c, a *fe2) {
  100. neg(&c[0], &a[0])
  101. neg(&c[1], &a[1])
  102. }
  103. func (e *fp2) mul(c, a, b *fe2) {
  104. t := e.t
  105. mul(t[1], &a[0], &b[0])
  106. mul(t[2], &a[1], &b[1])
  107. add(t[0], &a[0], &a[1])
  108. add(t[3], &b[0], &b[1])
  109. sub(&c[0], t[1], t[2])
  110. addAssign(t[1], t[2])
  111. mul(t[0], t[0], t[3])
  112. sub(&c[1], t[0], t[1])
  113. }
  114. func (e *fp2) mulAssign(a, b *fe2) {
  115. t := e.t
  116. mul(t[1], &a[0], &b[0])
  117. mul(t[2], &a[1], &b[1])
  118. add(t[0], &a[0], &a[1])
  119. add(t[3], &b[0], &b[1])
  120. sub(&a[0], t[1], t[2])
  121. addAssign(t[1], t[2])
  122. mul(t[0], t[0], t[3])
  123. sub(&a[1], t[0], t[1])
  124. }
  125. func (e *fp2) square(c, a *fe2) {
  126. t := e.t
  127. ladd(t[0], &a[0], &a[1])
  128. sub(t[1], &a[0], &a[1])
  129. ldouble(t[2], &a[0])
  130. mul(&c[0], t[0], t[1])
  131. mul(&c[1], t[2], &a[1])
  132. }
  133. func (e *fp2) squareAssign(a *fe2) {
  134. t := e.t
  135. ladd(t[0], &a[0], &a[1])
  136. sub(t[1], &a[0], &a[1])
  137. ldouble(t[2], &a[0])
  138. mul(&a[0], t[0], t[1])
  139. mul(&a[1], t[2], &a[1])
  140. }
  141. func (e *fp2) mulByNonResidue(c, a *fe2) {
  142. t := e.t
  143. sub(t[0], &a[0], &a[1])
  144. add(&c[1], &a[0], &a[1])
  145. c[0].set(t[0])
  146. }
  147. func (e *fp2) mulByB(c, a *fe2) {
  148. t := e.t
  149. double(t[0], &a[0])
  150. double(t[1], &a[1])
  151. doubleAssign(t[0])
  152. doubleAssign(t[1])
  153. sub(&c[0], t[0], t[1])
  154. add(&c[1], t[0], t[1])
  155. }
  156. func (e *fp2) inverse(c, a *fe2) {
  157. t := e.t
  158. square(t[0], &a[0])
  159. square(t[1], &a[1])
  160. addAssign(t[0], t[1])
  161. inverse(t[0], t[0])
  162. mul(&c[0], &a[0], t[0])
  163. mul(t[0], t[0], &a[1])
  164. neg(&c[1], t[0])
  165. }
  166. func (e *fp2) mulByFq(c, a *fe2, b *fe) {
  167. mul(&c[0], &a[0], b)
  168. mul(&c[1], &a[1], b)
  169. }
  170. func (e *fp2) exp(c, a *fe2, s *big.Int) {
  171. z := e.one()
  172. for i := s.BitLen() - 1; i >= 0; i-- {
  173. e.square(z, z)
  174. if s.Bit(i) == 1 {
  175. e.mul(z, z, a)
  176. }
  177. }
  178. c.set(z)
  179. }
  180. func (e *fp2) frobeniusMap(c, a *fe2, power uint) {
  181. c[0].set(&a[0])
  182. if power%2 == 1 {
  183. neg(&c[1], &a[1])
  184. return
  185. }
  186. c[1].set(&a[1])
  187. }
  188. func (e *fp2) frobeniusMapAssign(a *fe2, power uint) {
  189. if power%2 == 1 {
  190. neg(&a[1], &a[1])
  191. return
  192. }
  193. }
  194. func (e *fp2) sqrt(c, a *fe2) bool {
  195. u, x0, a1, alpha := &fe2{}, &fe2{}, &fe2{}, &fe2{}
  196. u.set(a)
  197. e.exp(a1, a, pMinus3Over4)
  198. e.square(alpha, a1)
  199. e.mul(alpha, alpha, a)
  200. e.mul(x0, a1, a)
  201. if alpha.equal(negativeOne2) {
  202. neg(&c[0], &x0[1])
  203. c[1].set(&x0[0])
  204. return true
  205. }
  206. e.add(alpha, alpha, e.one())
  207. e.exp(alpha, alpha, pMinus1Over2)
  208. e.mul(c, alpha, x0)
  209. e.square(alpha, c)
  210. return alpha.equal(u)
  211. }
  212. func (e *fp2) isQuadraticNonResidue(a *fe2) bool {
  213. // https://github.com/leovt/constructible/wiki/Taking-Square-Roots-in-quadratic-extension-Fields
  214. c0, c1 := new(fe), new(fe)
  215. square(c0, &a[0])
  216. square(c1, &a[1])
  217. add(c1, c1, c0)
  218. return isQuadraticNonResidue(c1)
  219. }