g2.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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"
  20. "math/big"
  21. )
  22. // PointG2 is type for point in G2.
  23. // PointG2 is both used for Affine and Jacobian point representation.
  24. // If z is equal to one the point is considered as in affine form.
  25. type PointG2 [3]fe2
  26. // Set copies valeus of one point to another.
  27. func (p *PointG2) Set(p2 *PointG2) *PointG2 {
  28. p[0].set(&p2[0])
  29. p[1].set(&p2[1])
  30. p[2].set(&p2[2])
  31. return p
  32. }
  33. // Zero returns G2 point in point at infinity representation
  34. func (p *PointG2) Zero() *PointG2 {
  35. p[0].zero()
  36. p[1].one()
  37. p[2].zero()
  38. return p
  39. }
  40. type tempG2 struct {
  41. t [9]*fe2
  42. }
  43. // G2 is struct for G2 group.
  44. type G2 struct {
  45. f *fp2
  46. tempG2
  47. }
  48. // NewG2 constructs a new G2 instance.
  49. func NewG2() *G2 {
  50. return newG2(nil)
  51. }
  52. func newG2(f *fp2) *G2 {
  53. if f == nil {
  54. f = newFp2()
  55. }
  56. t := newTempG2()
  57. return &G2{f, t}
  58. }
  59. func newTempG2() tempG2 {
  60. t := [9]*fe2{}
  61. for i := 0; i < 9; i++ {
  62. t[i] = &fe2{}
  63. }
  64. return tempG2{t}
  65. }
  66. // Q returns group order in big.Int.
  67. func (g *G2) Q() *big.Int {
  68. return new(big.Int).Set(q)
  69. }
  70. func (g *G2) fromBytesUnchecked(in []byte) (*PointG2, error) {
  71. p0, err := g.f.fromBytes(in[:96])
  72. if err != nil {
  73. return nil, err
  74. }
  75. p1, err := g.f.fromBytes(in[96:])
  76. if err != nil {
  77. return nil, err
  78. }
  79. p2 := new(fe2).one()
  80. return &PointG2{*p0, *p1, *p2}, nil
  81. }
  82. // FromBytes constructs a new point given uncompressed byte input.
  83. // FromBytes does not take zcash flags into account.
  84. // Byte input expected to be larger than 96 bytes.
  85. // First 192 bytes should be concatenation of x and y values
  86. // Point (0, 0) is considered as infinity.
  87. func (g *G2) FromBytes(in []byte) (*PointG2, error) {
  88. if len(in) != 192 {
  89. return nil, errors.New("input string should be equal or larger than 192")
  90. }
  91. p0, err := g.f.fromBytes(in[:96])
  92. if err != nil {
  93. return nil, err
  94. }
  95. p1, err := g.f.fromBytes(in[96:])
  96. if err != nil {
  97. return nil, err
  98. }
  99. // check if given input points to infinity
  100. if p0.isZero() && p1.isZero() {
  101. return g.Zero(), nil
  102. }
  103. p2 := new(fe2).one()
  104. p := &PointG2{*p0, *p1, *p2}
  105. if !g.IsOnCurve(p) {
  106. return nil, errors.New("point is not on curve")
  107. }
  108. return p, nil
  109. }
  110. // DecodePoint given encoded (x, y) coordinates in 256 bytes returns a valid G1 Point.
  111. func (g *G2) DecodePoint(in []byte) (*PointG2, error) {
  112. if len(in) != 256 {
  113. return nil, errors.New("invalid g2 point length")
  114. }
  115. pointBytes := make([]byte, 192)
  116. x0Bytes, err := decodeFieldElement(in[:64])
  117. if err != nil {
  118. return nil, err
  119. }
  120. x1Bytes, err := decodeFieldElement(in[64:128])
  121. if err != nil {
  122. return nil, err
  123. }
  124. y0Bytes, err := decodeFieldElement(in[128:192])
  125. if err != nil {
  126. return nil, err
  127. }
  128. y1Bytes, err := decodeFieldElement(in[192:])
  129. if err != nil {
  130. return nil, err
  131. }
  132. copy(pointBytes[:48], x1Bytes)
  133. copy(pointBytes[48:96], x0Bytes)
  134. copy(pointBytes[96:144], y1Bytes)
  135. copy(pointBytes[144:192], y0Bytes)
  136. return g.FromBytes(pointBytes)
  137. }
  138. // ToBytes serializes a point into bytes in uncompressed form,
  139. // does not take zcash flags into account,
  140. // returns (0, 0) if point is infinity.
  141. func (g *G2) ToBytes(p *PointG2) []byte {
  142. out := make([]byte, 192)
  143. if g.IsZero(p) {
  144. return out
  145. }
  146. g.Affine(p)
  147. copy(out[:96], g.f.toBytes(&p[0]))
  148. copy(out[96:], g.f.toBytes(&p[1]))
  149. return out
  150. }
  151. // EncodePoint encodes a point into 256 bytes.
  152. func (g *G2) EncodePoint(p *PointG2) []byte {
  153. // outRaw is 96 bytes
  154. outRaw := g.ToBytes(p)
  155. out := make([]byte, 256)
  156. // encode x
  157. copy(out[16:16+48], outRaw[48:96])
  158. copy(out[80:80+48], outRaw[:48])
  159. // encode y
  160. copy(out[144:144+48], outRaw[144:])
  161. copy(out[208:208+48], outRaw[96:144])
  162. return out
  163. }
  164. // New creates a new G2 Point which is equal to zero in other words point at infinity.
  165. func (g *G2) New() *PointG2 {
  166. return new(PointG2).Zero()
  167. }
  168. // Zero returns a new G2 Point which is equal to point at infinity.
  169. func (g *G2) Zero() *PointG2 {
  170. return new(PointG2).Zero()
  171. }
  172. // One returns a new G2 Point which is equal to generator point.
  173. func (g *G2) One() *PointG2 {
  174. p := &PointG2{}
  175. return p.Set(&g2One)
  176. }
  177. // IsZero returns true if given point is equal to zero.
  178. func (g *G2) IsZero(p *PointG2) bool {
  179. return p[2].isZero()
  180. }
  181. // Equal checks if given two G2 point is equal in their affine form.
  182. func (g *G2) Equal(p1, p2 *PointG2) bool {
  183. if g.IsZero(p1) {
  184. return g.IsZero(p2)
  185. }
  186. if g.IsZero(p2) {
  187. return g.IsZero(p1)
  188. }
  189. t := g.t
  190. g.f.square(t[0], &p1[2])
  191. g.f.square(t[1], &p2[2])
  192. g.f.mul(t[2], t[0], &p2[0])
  193. g.f.mul(t[3], t[1], &p1[0])
  194. g.f.mul(t[0], t[0], &p1[2])
  195. g.f.mul(t[1], t[1], &p2[2])
  196. g.f.mul(t[1], t[1], &p1[1])
  197. g.f.mul(t[0], t[0], &p2[1])
  198. return t[0].equal(t[1]) && t[2].equal(t[3])
  199. }
  200. // InCorrectSubgroup checks whether given point is in correct subgroup.
  201. func (g *G2) InCorrectSubgroup(p *PointG2) bool {
  202. tmp := &PointG2{}
  203. g.MulScalar(tmp, p, q)
  204. return g.IsZero(tmp)
  205. }
  206. // IsOnCurve checks a G2 point is on curve.
  207. func (g *G2) IsOnCurve(p *PointG2) bool {
  208. if g.IsZero(p) {
  209. return true
  210. }
  211. t := g.t
  212. g.f.square(t[0], &p[1])
  213. g.f.square(t[1], &p[0])
  214. g.f.mul(t[1], t[1], &p[0])
  215. g.f.square(t[2], &p[2])
  216. g.f.square(t[3], t[2])
  217. g.f.mul(t[2], t[2], t[3])
  218. g.f.mul(t[2], b2, t[2])
  219. g.f.add(t[1], t[1], t[2])
  220. return t[0].equal(t[1])
  221. }
  222. // IsAffine checks a G2 point whether it is in affine form.
  223. func (g *G2) IsAffine(p *PointG2) bool {
  224. return p[2].isOne()
  225. }
  226. // Affine calculates affine form of given G2 point.
  227. func (g *G2) Affine(p *PointG2) *PointG2 {
  228. if g.IsZero(p) {
  229. return p
  230. }
  231. if !g.IsAffine(p) {
  232. t := g.t
  233. g.f.inverse(t[0], &p[2])
  234. g.f.square(t[1], t[0])
  235. g.f.mul(&p[0], &p[0], t[1])
  236. g.f.mul(t[0], t[0], t[1])
  237. g.f.mul(&p[1], &p[1], t[0])
  238. p[2].one()
  239. }
  240. return p
  241. }
  242. // Add adds two G2 points p1, p2 and assigns the result to point at first argument.
  243. func (g *G2) Add(r, p1, p2 *PointG2) *PointG2 {
  244. // http://www.hyperelliptic.org/EFD/gp/auto-shortw-jacobian-0.html#addition-add-2007-bl
  245. if g.IsZero(p1) {
  246. return r.Set(p2)
  247. }
  248. if g.IsZero(p2) {
  249. return r.Set(p1)
  250. }
  251. t := g.t
  252. g.f.square(t[7], &p1[2])
  253. g.f.mul(t[1], &p2[0], t[7])
  254. g.f.mul(t[2], &p1[2], t[7])
  255. g.f.mul(t[0], &p2[1], t[2])
  256. g.f.square(t[8], &p2[2])
  257. g.f.mul(t[3], &p1[0], t[8])
  258. g.f.mul(t[4], &p2[2], t[8])
  259. g.f.mul(t[2], &p1[1], t[4])
  260. if t[1].equal(t[3]) {
  261. if t[0].equal(t[2]) {
  262. return g.Double(r, p1)
  263. }
  264. return r.Zero()
  265. }
  266. g.f.sub(t[1], t[1], t[3])
  267. g.f.double(t[4], t[1])
  268. g.f.square(t[4], t[4])
  269. g.f.mul(t[5], t[1], t[4])
  270. g.f.sub(t[0], t[0], t[2])
  271. g.f.double(t[0], t[0])
  272. g.f.square(t[6], t[0])
  273. g.f.sub(t[6], t[6], t[5])
  274. g.f.mul(t[3], t[3], t[4])
  275. g.f.double(t[4], t[3])
  276. g.f.sub(&r[0], t[6], t[4])
  277. g.f.sub(t[4], t[3], &r[0])
  278. g.f.mul(t[6], t[2], t[5])
  279. g.f.double(t[6], t[6])
  280. g.f.mul(t[0], t[0], t[4])
  281. g.f.sub(&r[1], t[0], t[6])
  282. g.f.add(t[0], &p1[2], &p2[2])
  283. g.f.square(t[0], t[0])
  284. g.f.sub(t[0], t[0], t[7])
  285. g.f.sub(t[0], t[0], t[8])
  286. g.f.mul(&r[2], t[0], t[1])
  287. return r
  288. }
  289. // Double doubles a G2 point p and assigns the result to the point at first argument.
  290. func (g *G2) Double(r, p *PointG2) *PointG2 {
  291. // http://www.hyperelliptic.org/EFD/gp/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
  292. if g.IsZero(p) {
  293. return r.Set(p)
  294. }
  295. t := g.t
  296. g.f.square(t[0], &p[0])
  297. g.f.square(t[1], &p[1])
  298. g.f.square(t[2], t[1])
  299. g.f.add(t[1], &p[0], t[1])
  300. g.f.square(t[1], t[1])
  301. g.f.sub(t[1], t[1], t[0])
  302. g.f.sub(t[1], t[1], t[2])
  303. g.f.double(t[1], t[1])
  304. g.f.double(t[3], t[0])
  305. g.f.add(t[0], t[3], t[0])
  306. g.f.square(t[4], t[0])
  307. g.f.double(t[3], t[1])
  308. g.f.sub(&r[0], t[4], t[3])
  309. g.f.sub(t[1], t[1], &r[0])
  310. g.f.double(t[2], t[2])
  311. g.f.double(t[2], t[2])
  312. g.f.double(t[2], t[2])
  313. g.f.mul(t[0], t[0], t[1])
  314. g.f.sub(t[1], t[0], t[2])
  315. g.f.mul(t[0], &p[1], &p[2])
  316. r[1].set(t[1])
  317. g.f.double(&r[2], t[0])
  318. return r
  319. }
  320. // Neg negates a G2 point p and assigns the result to the point at first argument.
  321. func (g *G2) Neg(r, p *PointG2) *PointG2 {
  322. r[0].set(&p[0])
  323. g.f.neg(&r[1], &p[1])
  324. r[2].set(&p[2])
  325. return r
  326. }
  327. // Sub subtracts two G2 points p1, p2 and assigns the result to point at first argument.
  328. func (g *G2) Sub(c, a, b *PointG2) *PointG2 {
  329. d := &PointG2{}
  330. g.Neg(d, b)
  331. g.Add(c, a, d)
  332. return c
  333. }
  334. // MulScalar multiplies a point by given scalar value in big.Int and assigns the result to point at first argument.
  335. func (g *G2) MulScalar(c, p *PointG2, e *big.Int) *PointG2 {
  336. q, n := &PointG2{}, &PointG2{}
  337. n.Set(p)
  338. l := e.BitLen()
  339. for i := 0; i < l; i++ {
  340. if e.Bit(i) == 1 {
  341. g.Add(q, q, n)
  342. }
  343. g.Double(n, n)
  344. }
  345. return c.Set(q)
  346. }
  347. // ClearCofactor maps given a G2 point to correct subgroup
  348. func (g *G2) ClearCofactor(p *PointG2) {
  349. g.MulScalar(p, p, cofactorEFFG2)
  350. }
  351. // MultiExp calculates multi exponentiation. Given pairs of G2 point and scalar values
  352. // (P_0, e_0), (P_1, e_1), ... (P_n, e_n) calculates r = e_0 * P_0 + e_1 * P_1 + ... + e_n * P_n
  353. // Length of points and scalars are expected to be equal, otherwise an error is returned.
  354. // Result is assigned to point at first argument.
  355. func (g *G2) MultiExp(r *PointG2, points []*PointG2, powers []*big.Int) (*PointG2, error) {
  356. if len(points) != len(powers) {
  357. return nil, errors.New("point and scalar vectors should be in same length")
  358. }
  359. var c uint32 = 3
  360. if len(powers) >= 32 {
  361. c = uint32(math.Ceil(math.Log10(float64(len(powers)))))
  362. }
  363. bucketSize, numBits := (1<<c)-1, uint32(g.Q().BitLen())
  364. windows := make([]*PointG2, numBits/c+1)
  365. bucket := make([]*PointG2, bucketSize)
  366. acc, sum := g.New(), g.New()
  367. for i := 0; i < bucketSize; i++ {
  368. bucket[i] = g.New()
  369. }
  370. mask := (uint64(1) << c) - 1
  371. j := 0
  372. var cur uint32
  373. for cur <= numBits {
  374. acc.Zero()
  375. bucket = make([]*PointG2, (1<<c)-1)
  376. for i := 0; i < len(bucket); i++ {
  377. bucket[i] = g.New()
  378. }
  379. for i := 0; i < len(powers); i++ {
  380. s0 := powers[i].Uint64()
  381. index := uint(s0 & mask)
  382. if index != 0 {
  383. g.Add(bucket[index-1], bucket[index-1], points[i])
  384. }
  385. powers[i] = new(big.Int).Rsh(powers[i], uint(c))
  386. }
  387. sum.Zero()
  388. for i := len(bucket) - 1; i >= 0; i-- {
  389. g.Add(sum, sum, bucket[i])
  390. g.Add(acc, acc, sum)
  391. }
  392. windows[j] = g.New()
  393. windows[j].Set(acc)
  394. j++
  395. cur += c
  396. }
  397. acc.Zero()
  398. for i := len(windows) - 1; i >= 0; i-- {
  399. for j := uint32(0); j < c; j++ {
  400. g.Double(acc, acc)
  401. }
  402. g.Add(acc, acc, windows[i])
  403. }
  404. return r.Set(acc), nil
  405. }
  406. // MapToCurve given a byte slice returns a valid G2 point.
  407. // This mapping function implements the Simplified Shallue-van de Woestijne-Ulas method.
  408. // https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-05#section-6.6.2
  409. // Input byte slice should be a valid field element, otherwise an error is returned.
  410. func (g *G2) MapToCurve(in []byte) (*PointG2, error) {
  411. fp2 := g.f
  412. u, err := fp2.fromBytes(in)
  413. if err != nil {
  414. return nil, err
  415. }
  416. x, y := swuMapG2(fp2, u)
  417. isogenyMapG2(fp2, x, y)
  418. z := new(fe2).one()
  419. q := &PointG2{*x, *y, *z}
  420. g.ClearCofactor(q)
  421. return g.Affine(q), nil
  422. }