bn256.go 11 KB

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