g1.go 11 KB

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