bn256.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // Package bn256 implements a particular bilinear group at the 128-bit security
  2. // level.
  3. //
  4. // Bilinear groups are the basis of many of the new cryptographic protocols that
  5. // have been proposed over the past decade. They consist of a triplet of groups
  6. // (G₁, G₂ and GT) such that there exists a function e(g₁ˣ,g₂ʸ)=gTˣʸ (where gₓ
  7. // is a generator of the respective group). That function is called a pairing
  8. // function.
  9. //
  10. // This package specifically implements the Optimal Ate pairing over a 256-bit
  11. // Barreto-Naehrig curve as described in
  12. // http://cryptojedi.org/papers/dclxvi-20100714.pdf. Its output is not
  13. // compatible with the implementation described in that paper, as different
  14. // parameters are chosen.
  15. //
  16. // (This package previously claimed to operate at a 128-bit security level.
  17. // However, recent improvements in attacks mean that is no longer true. See
  18. // https://moderncrypto.org/mail-archive/curves/2016/000740.html.)
  19. package bn256
  20. import (
  21. "crypto/rand"
  22. "errors"
  23. "io"
  24. "math/big"
  25. )
  26. func randomK(r io.Reader) (k *big.Int, err error) {
  27. for {
  28. k, err = rand.Int(r, Order)
  29. if err != nil || k.Sign() > 0 {
  30. return
  31. }
  32. }
  33. }
  34. // G1 is an abstract cyclic group. The zero value is suitable for use as the
  35. // output of an operation, but cannot be used as an input.
  36. type G1 struct {
  37. p *curvePoint
  38. }
  39. // RandomG1 returns x and g₁ˣ where x is a random, non-zero number read from r.
  40. func RandomG1(r io.Reader) (*big.Int, *G1, error) {
  41. k, err := randomK(r)
  42. if err != nil {
  43. return nil, nil, err
  44. }
  45. return k, new(G1).ScalarBaseMult(k), nil
  46. }
  47. func (g *G1) String() string {
  48. return "bn256.G1" + g.p.String()
  49. }
  50. // ScalarBaseMult sets e to g*k where g is the generator of the group and then
  51. // returns e.
  52. func (e *G1) ScalarBaseMult(k *big.Int) *G1 {
  53. if e.p == nil {
  54. e.p = &curvePoint{}
  55. }
  56. e.p.Mul(curveGen, k)
  57. return e
  58. }
  59. // ScalarMult sets e to a*k and then returns e.
  60. func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 {
  61. if e.p == nil {
  62. e.p = &curvePoint{}
  63. }
  64. e.p.Mul(a.p, k)
  65. return e
  66. }
  67. // Add sets e to a+b and then returns e.
  68. func (e *G1) Add(a, b *G1) *G1 {
  69. if e.p == nil {
  70. e.p = &curvePoint{}
  71. }
  72. e.p.Add(a.p, b.p)
  73. return e
  74. }
  75. // Neg sets e to -a and then returns e.
  76. func (e *G1) Neg(a *G1) *G1 {
  77. if e.p == nil {
  78. e.p = &curvePoint{}
  79. }
  80. e.p.Neg(a.p)
  81. return e
  82. }
  83. // Set sets e to a and then returns e.
  84. func (e *G1) Set(a *G1) *G1 {
  85. if e.p == nil {
  86. e.p = &curvePoint{}
  87. }
  88. e.p.Set(a.p)
  89. return e
  90. }
  91. // Marshal converts e to a byte slice.
  92. func (e *G1) Marshal() []byte {
  93. // Each value is a 256-bit number.
  94. const numBytes = 256 / 8
  95. if e.p == nil {
  96. e.p = &curvePoint{}
  97. }
  98. e.p.MakeAffine()
  99. ret := make([]byte, numBytes*2)
  100. if e.p.IsInfinity() {
  101. return ret
  102. }
  103. temp := &gfP{}
  104. montDecode(temp, &e.p.x)
  105. temp.Marshal(ret)
  106. montDecode(temp, &e.p.y)
  107. temp.Marshal(ret[numBytes:])
  108. return ret
  109. }
  110. // Unmarshal sets e to the result of converting the output of Marshal back into
  111. // a group element and then returns e.
  112. func (e *G1) Unmarshal(m []byte) ([]byte, error) {
  113. // Each value is a 256-bit number.
  114. const numBytes = 256 / 8
  115. if len(m) < 2*numBytes {
  116. return nil, errors.New("bn256: not enough data")
  117. }
  118. // Unmarshal the points and check their caps
  119. if e.p == nil {
  120. e.p = &curvePoint{}
  121. } else {
  122. e.p.x, e.p.y = gfP{0}, gfP{0}
  123. }
  124. var err error
  125. if err = e.p.x.Unmarshal(m); err != nil {
  126. return nil, err
  127. }
  128. if err = e.p.y.Unmarshal(m[numBytes:]); err != nil {
  129. return nil, err
  130. }
  131. // Encode into Montgomery form and ensure it's on the curve
  132. montEncode(&e.p.x, &e.p.x)
  133. montEncode(&e.p.y, &e.p.y)
  134. zero := gfP{0}
  135. if e.p.x == zero && e.p.y == zero {
  136. // This is the point at infinity.
  137. e.p.y = *newGFp(1)
  138. e.p.z = gfP{0}
  139. e.p.t = gfP{0}
  140. } else {
  141. e.p.z = *newGFp(1)
  142. e.p.t = *newGFp(1)
  143. if !e.p.IsOnCurve() {
  144. return nil, errors.New("bn256: malformed point")
  145. }
  146. }
  147. return m[2*numBytes:], nil
  148. }
  149. // G2 is an abstract cyclic group. The zero value is suitable for use as the
  150. // output of an operation, but cannot be used as an input.
  151. type G2 struct {
  152. p *twistPoint
  153. }
  154. // RandomG2 returns x and g₂ˣ where x is a random, non-zero number read from r.
  155. func RandomG2(r io.Reader) (*big.Int, *G2, error) {
  156. k, err := randomK(r)
  157. if err != nil {
  158. return nil, nil, err
  159. }
  160. return k, new(G2).ScalarBaseMult(k), nil
  161. }
  162. func (e *G2) String() string {
  163. return "bn256.G2" + e.p.String()
  164. }
  165. // ScalarBaseMult sets e to g*k where g is the generator of the group and then
  166. // returns out.
  167. func (e *G2) ScalarBaseMult(k *big.Int) *G2 {
  168. if e.p == nil {
  169. e.p = &twistPoint{}
  170. }
  171. e.p.Mul(twistGen, k)
  172. return e
  173. }
  174. // ScalarMult sets e to a*k and then returns e.
  175. func (e *G2) ScalarMult(a *G2, k *big.Int) *G2 {
  176. if e.p == nil {
  177. e.p = &twistPoint{}
  178. }
  179. e.p.Mul(a.p, k)
  180. return e
  181. }
  182. // Add sets e to a+b and then returns e.
  183. func (e *G2) Add(a, b *G2) *G2 {
  184. if e.p == nil {
  185. e.p = &twistPoint{}
  186. }
  187. e.p.Add(a.p, b.p)
  188. return e
  189. }
  190. // Neg sets e to -a and then returns e.
  191. func (e *G2) Neg(a *G2) *G2 {
  192. if e.p == nil {
  193. e.p = &twistPoint{}
  194. }
  195. e.p.Neg(a.p)
  196. return e
  197. }
  198. // Set sets e to a and then returns e.
  199. func (e *G2) Set(a *G2) *G2 {
  200. if e.p == nil {
  201. e.p = &twistPoint{}
  202. }
  203. e.p.Set(a.p)
  204. return e
  205. }
  206. // Marshal converts e into a byte slice.
  207. func (e *G2) Marshal() []byte {
  208. // Each value is a 256-bit number.
  209. const numBytes = 256 / 8
  210. if e.p == nil {
  211. e.p = &twistPoint{}
  212. }
  213. e.p.MakeAffine()
  214. ret := make([]byte, numBytes*4)
  215. if e.p.IsInfinity() {
  216. return ret
  217. }
  218. temp := &gfP{}
  219. montDecode(temp, &e.p.x.x)
  220. temp.Marshal(ret)
  221. montDecode(temp, &e.p.x.y)
  222. temp.Marshal(ret[numBytes:])
  223. montDecode(temp, &e.p.y.x)
  224. temp.Marshal(ret[2*numBytes:])
  225. montDecode(temp, &e.p.y.y)
  226. temp.Marshal(ret[3*numBytes:])
  227. return ret
  228. }
  229. // Unmarshal sets e to the result of converting the output of Marshal back into
  230. // a group element and then returns e.
  231. func (e *G2) Unmarshal(m []byte) ([]byte, error) {
  232. // Each value is a 256-bit number.
  233. const numBytes = 256 / 8
  234. if len(m) < 4*numBytes {
  235. return nil, errors.New("bn256: not enough data")
  236. }
  237. // Unmarshal the points and check their caps
  238. if e.p == nil {
  239. e.p = &twistPoint{}
  240. }
  241. var err error
  242. if err = e.p.x.x.Unmarshal(m); err != nil {
  243. return nil, err
  244. }
  245. if err = e.p.x.y.Unmarshal(m[numBytes:]); err != nil {
  246. return nil, err
  247. }
  248. if err = e.p.y.x.Unmarshal(m[2*numBytes:]); err != nil {
  249. return nil, err
  250. }
  251. if err = e.p.y.y.Unmarshal(m[3*numBytes:]); err != nil {
  252. return nil, err
  253. }
  254. // Encode into Montgomery form and ensure it's on the curve
  255. montEncode(&e.p.x.x, &e.p.x.x)
  256. montEncode(&e.p.x.y, &e.p.x.y)
  257. montEncode(&e.p.y.x, &e.p.y.x)
  258. montEncode(&e.p.y.y, &e.p.y.y)
  259. if e.p.x.IsZero() && e.p.y.IsZero() {
  260. // This is the point at infinity.
  261. e.p.y.SetOne()
  262. e.p.z.SetZero()
  263. e.p.t.SetZero()
  264. } else {
  265. e.p.z.SetOne()
  266. e.p.t.SetOne()
  267. if !e.p.IsOnCurve() {
  268. return nil, errors.New("bn256: malformed point")
  269. }
  270. }
  271. return m[4*numBytes:], nil
  272. }
  273. // GT is an abstract cyclic group. The zero value is suitable for use as the
  274. // output of an operation, but cannot be used as an input.
  275. type GT struct {
  276. p *gfP12
  277. }
  278. // Pair calculates an Optimal Ate pairing.
  279. func Pair(g1 *G1, g2 *G2) *GT {
  280. return &GT{optimalAte(g2.p, g1.p)}
  281. }
  282. // PairingCheck calculates the Optimal Ate pairing for a set of points.
  283. func PairingCheck(a []*G1, b []*G2) bool {
  284. acc := new(gfP12)
  285. acc.SetOne()
  286. for i := 0; i < len(a); i++ {
  287. if a[i].p.IsInfinity() || b[i].p.IsInfinity() {
  288. continue
  289. }
  290. acc.Mul(acc, miller(b[i].p, a[i].p))
  291. }
  292. return finalExponentiation(acc).IsOne()
  293. }
  294. // Miller applies Miller's algorithm, which is a bilinear function from the
  295. // source groups to F_p^12. Miller(g1, g2).Finalize() is equivalent to Pair(g1,
  296. // g2).
  297. func Miller(g1 *G1, g2 *G2) *GT {
  298. return &GT{miller(g2.p, g1.p)}
  299. }
  300. func (g *GT) String() string {
  301. return "bn256.GT" + g.p.String()
  302. }
  303. // ScalarMult sets e to a*k and then returns e.
  304. func (e *GT) ScalarMult(a *GT, k *big.Int) *GT {
  305. if e.p == nil {
  306. e.p = &gfP12{}
  307. }
  308. e.p.Exp(a.p, k)
  309. return e
  310. }
  311. // Add sets e to a+b and then returns e.
  312. func (e *GT) Add(a, b *GT) *GT {
  313. if e.p == nil {
  314. e.p = &gfP12{}
  315. }
  316. e.p.Mul(a.p, b.p)
  317. return e
  318. }
  319. // Neg sets e to -a and then returns e.
  320. func (e *GT) Neg(a *GT) *GT {
  321. if e.p == nil {
  322. e.p = &gfP12{}
  323. }
  324. e.p.Conjugate(a.p)
  325. return e
  326. }
  327. // Set sets e to a and then returns e.
  328. func (e *GT) Set(a *GT) *GT {
  329. if e.p == nil {
  330. e.p = &gfP12{}
  331. }
  332. e.p.Set(a.p)
  333. return e
  334. }
  335. // Finalize is a linear function from F_p^12 to GT.
  336. func (e *GT) Finalize() *GT {
  337. ret := finalExponentiation(e.p)
  338. e.p.Set(ret)
  339. return e
  340. }
  341. // Marshal converts e into a byte slice.
  342. func (e *GT) Marshal() []byte {
  343. // Each value is a 256-bit number.
  344. const numBytes = 256 / 8
  345. if e.p == nil {
  346. e.p = &gfP12{}
  347. e.p.SetOne()
  348. }
  349. ret := make([]byte, numBytes*12)
  350. temp := &gfP{}
  351. montDecode(temp, &e.p.x.x.x)
  352. temp.Marshal(ret)
  353. montDecode(temp, &e.p.x.x.y)
  354. temp.Marshal(ret[numBytes:])
  355. montDecode(temp, &e.p.x.y.x)
  356. temp.Marshal(ret[2*numBytes:])
  357. montDecode(temp, &e.p.x.y.y)
  358. temp.Marshal(ret[3*numBytes:])
  359. montDecode(temp, &e.p.x.z.x)
  360. temp.Marshal(ret[4*numBytes:])
  361. montDecode(temp, &e.p.x.z.y)
  362. temp.Marshal(ret[5*numBytes:])
  363. montDecode(temp, &e.p.y.x.x)
  364. temp.Marshal(ret[6*numBytes:])
  365. montDecode(temp, &e.p.y.x.y)
  366. temp.Marshal(ret[7*numBytes:])
  367. montDecode(temp, &e.p.y.y.x)
  368. temp.Marshal(ret[8*numBytes:])
  369. montDecode(temp, &e.p.y.y.y)
  370. temp.Marshal(ret[9*numBytes:])
  371. montDecode(temp, &e.p.y.z.x)
  372. temp.Marshal(ret[10*numBytes:])
  373. montDecode(temp, &e.p.y.z.y)
  374. temp.Marshal(ret[11*numBytes:])
  375. return ret
  376. }
  377. // Unmarshal sets e to the result of converting the output of Marshal back into
  378. // a group element and then returns e.
  379. func (e *GT) Unmarshal(m []byte) ([]byte, error) {
  380. // Each value is a 256-bit number.
  381. const numBytes = 256 / 8
  382. if len(m) < 12*numBytes {
  383. return nil, errors.New("bn256: not enough data")
  384. }
  385. if e.p == nil {
  386. e.p = &gfP12{}
  387. }
  388. var err error
  389. if err = e.p.x.x.x.Unmarshal(m); err != nil {
  390. return nil, err
  391. }
  392. if err = e.p.x.x.y.Unmarshal(m[numBytes:]); err != nil {
  393. return nil, err
  394. }
  395. if err = e.p.x.y.x.Unmarshal(m[2*numBytes:]); err != nil {
  396. return nil, err
  397. }
  398. if err = e.p.x.y.y.Unmarshal(m[3*numBytes:]); err != nil {
  399. return nil, err
  400. }
  401. if err = e.p.x.z.x.Unmarshal(m[4*numBytes:]); err != nil {
  402. return nil, err
  403. }
  404. if err = e.p.x.z.y.Unmarshal(m[5*numBytes:]); err != nil {
  405. return nil, err
  406. }
  407. if err = e.p.y.x.x.Unmarshal(m[6*numBytes:]); err != nil {
  408. return nil, err
  409. }
  410. if err = e.p.y.x.y.Unmarshal(m[7*numBytes:]); err != nil {
  411. return nil, err
  412. }
  413. if err = e.p.y.y.x.Unmarshal(m[8*numBytes:]); err != nil {
  414. return nil, err
  415. }
  416. if err = e.p.y.y.y.Unmarshal(m[9*numBytes:]); err != nil {
  417. return nil, err
  418. }
  419. if err = e.p.y.z.x.Unmarshal(m[10*numBytes:]); err != nil {
  420. return nil, err
  421. }
  422. if err = e.p.y.z.y.Unmarshal(m[11*numBytes:]); err != nil {
  423. return nil, err
  424. }
  425. montEncode(&e.p.x.x.x, &e.p.x.x.x)
  426. montEncode(&e.p.x.x.y, &e.p.x.x.y)
  427. montEncode(&e.p.x.y.x, &e.p.x.y.x)
  428. montEncode(&e.p.x.y.y, &e.p.x.y.y)
  429. montEncode(&e.p.x.z.x, &e.p.x.z.x)
  430. montEncode(&e.p.x.z.y, &e.p.x.z.y)
  431. montEncode(&e.p.y.x.x, &e.p.y.x.x)
  432. montEncode(&e.p.y.x.y, &e.p.y.x.y)
  433. montEncode(&e.p.y.y.x, &e.p.y.y.x)
  434. montEncode(&e.p.y.y.y, &e.p.y.y.y)
  435. montEncode(&e.p.y.z.x, &e.p.y.z.x)
  436. montEncode(&e.p.y.z.y, &e.p.y.z.y)
  437. return m[12*numBytes:], nil
  438. }