bn256.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package bn256 implements a particular bilinear group at the 128-bit security level.
  5. //
  6. // Bilinear groups are the basis of many of the new cryptographic protocols
  7. // that have been proposed over the past decade. They consist of a triplet of
  8. // groups (G₁, G₂ and GT) such that there exists a function e(g₁ˣ,g₂ʸ)=gTˣʸ
  9. // (where gₓ is a generator of the respective group). That function is called
  10. // a pairing function.
  11. //
  12. // This package specifically implements the Optimal Ate pairing over a 256-bit
  13. // Barreto-Naehrig curve as described in
  14. // http://cryptojedi.org/papers/dclxvi-20100714.pdf. Its output is compatible
  15. // with the implementation described in that paper.
  16. package bn256
  17. import (
  18. "crypto/rand"
  19. "io"
  20. "math/big"
  21. )
  22. // BUG(agl): this implementation is not constant time.
  23. // TODO(agl): keep GF(p²) elements in Mongomery form.
  24. // G1 is an abstract cyclic group. The zero value is suitable for use as the
  25. // output of an operation, but cannot be used as an input.
  26. type G1 struct {
  27. p *curvePoint
  28. }
  29. // RandomG1 returns x and g₁ˣ where x is a random, non-zero number read from r.
  30. func RandomG1(r io.Reader) (*big.Int, *G1, error) {
  31. var k *big.Int
  32. var err error
  33. for {
  34. k, err = rand.Int(r, Order)
  35. if err != nil {
  36. return nil, nil, err
  37. }
  38. if k.Sign() > 0 {
  39. break
  40. }
  41. }
  42. return k, new(G1).ScalarBaseMult(k), nil
  43. }
  44. func (g *G1) String() string {
  45. return "bn256.G1" + g.p.String()
  46. }
  47. // ScalarBaseMult sets e to g*k where g is the generator of the group and
  48. // then returns e.
  49. func (e *G1) ScalarBaseMult(k *big.Int) *G1 {
  50. if e.p == nil {
  51. e.p = newCurvePoint(nil)
  52. }
  53. e.p.Mul(curveGen, k, new(bnPool))
  54. return e
  55. }
  56. // ScalarMult sets e to a*k and then returns e.
  57. func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 {
  58. if e.p == nil {
  59. e.p = newCurvePoint(nil)
  60. }
  61. e.p.Mul(a.p, k, new(bnPool))
  62. return e
  63. }
  64. // Add sets e to a+b and then returns e.
  65. // BUG(agl): this function is not complete: a==b fails.
  66. func (e *G1) Add(a, b *G1) *G1 {
  67. if e.p == nil {
  68. e.p = newCurvePoint(nil)
  69. }
  70. e.p.Add(a.p, b.p, new(bnPool))
  71. return e
  72. }
  73. // Neg sets e to -a and then returns e.
  74. func (e *G1) Neg(a *G1) *G1 {
  75. if e.p == nil {
  76. e.p = newCurvePoint(nil)
  77. }
  78. e.p.Negative(a.p)
  79. return e
  80. }
  81. // Marshal converts n to a byte slice.
  82. func (n *G1) Marshal() []byte {
  83. n.p.MakeAffine(nil)
  84. xBytes := new(big.Int).Mod(n.p.x, p).Bytes()
  85. yBytes := new(big.Int).Mod(n.p.y, p).Bytes()
  86. // Each value is a 256-bit number.
  87. const numBytes = 256 / 8
  88. ret := make([]byte, numBytes*2)
  89. copy(ret[1*numBytes-len(xBytes):], xBytes)
  90. copy(ret[2*numBytes-len(yBytes):], yBytes)
  91. return ret
  92. }
  93. // Unmarshal sets e to the result of converting the output of Marshal back into
  94. // a group element and then returns e.
  95. func (e *G1) Unmarshal(m []byte) (*G1, bool) {
  96. // Each value is a 256-bit number.
  97. const numBytes = 256 / 8
  98. if len(m) != 2*numBytes {
  99. return nil, false
  100. }
  101. if e.p == nil {
  102. e.p = newCurvePoint(nil)
  103. }
  104. e.p.x.SetBytes(m[0*numBytes : 1*numBytes])
  105. e.p.y.SetBytes(m[1*numBytes : 2*numBytes])
  106. if e.p.x.Sign() == 0 && e.p.y.Sign() == 0 {
  107. // This is the point at infinity.
  108. e.p.y.SetInt64(1)
  109. e.p.z.SetInt64(0)
  110. } else {
  111. e.p.z.SetInt64(1)
  112. if !e.p.IsOnCurve() {
  113. return nil, false
  114. }
  115. }
  116. return e, true
  117. }
  118. // G2 is an abstract cyclic group. The zero value is suitable for use as the
  119. // output of an operation, but cannot be used as an input.
  120. type G2 struct {
  121. p *twistPoint
  122. }
  123. // RandomG1 returns x and g₂ˣ where x is a random, non-zero number read from r.
  124. func RandomG2(r io.Reader) (*big.Int, *G2, error) {
  125. var k *big.Int
  126. var err error
  127. for {
  128. k, err = rand.Int(r, Order)
  129. if err != nil {
  130. return nil, nil, err
  131. }
  132. if k.Sign() > 0 {
  133. break
  134. }
  135. }
  136. return k, new(G2).ScalarBaseMult(k), nil
  137. }
  138. func (g *G2) String() string {
  139. return "bn256.G2" + g.p.String()
  140. }
  141. // ScalarBaseMult sets e to g*k where g is the generator of the group and
  142. // then returns out.
  143. func (e *G2) ScalarBaseMult(k *big.Int) *G2 {
  144. if e.p == nil {
  145. e.p = newTwistPoint(nil)
  146. }
  147. e.p.Mul(twistGen, k, new(bnPool))
  148. return e
  149. }
  150. // ScalarMult sets e to a*k and then returns e.
  151. func (e *G2) ScalarMult(a *G2, k *big.Int) *G2 {
  152. if e.p == nil {
  153. e.p = newTwistPoint(nil)
  154. }
  155. e.p.Mul(a.p, k, new(bnPool))
  156. return e
  157. }
  158. // Add sets e to a+b and then returns e.
  159. // BUG(agl): this function is not complete: a==b fails.
  160. func (e *G2) Add(a, b *G2) *G2 {
  161. if e.p == nil {
  162. e.p = newTwistPoint(nil)
  163. }
  164. e.p.Add(a.p, b.p, new(bnPool))
  165. return e
  166. }
  167. // Marshal converts n into a byte slice.
  168. func (n *G2) Marshal() []byte {
  169. n.p.MakeAffine(nil)
  170. xxBytes := new(big.Int).Mod(n.p.x.x, p).Bytes()
  171. xyBytes := new(big.Int).Mod(n.p.x.y, p).Bytes()
  172. yxBytes := new(big.Int).Mod(n.p.y.x, p).Bytes()
  173. yyBytes := new(big.Int).Mod(n.p.y.y, p).Bytes()
  174. // Each value is a 256-bit number.
  175. const numBytes = 256 / 8
  176. ret := make([]byte, numBytes*4)
  177. copy(ret[1*numBytes-len(xxBytes):], xxBytes)
  178. copy(ret[2*numBytes-len(xyBytes):], xyBytes)
  179. copy(ret[3*numBytes-len(yxBytes):], yxBytes)
  180. copy(ret[4*numBytes-len(yyBytes):], yyBytes)
  181. return ret
  182. }
  183. // Unmarshal sets e to the result of converting the output of Marshal back into
  184. // a group element and then returns e.
  185. func (e *G2) Unmarshal(m []byte) (*G2, bool) {
  186. // Each value is a 256-bit number.
  187. const numBytes = 256 / 8
  188. if len(m) != 4*numBytes {
  189. return nil, false
  190. }
  191. if e.p == nil {
  192. e.p = newTwistPoint(nil)
  193. }
  194. e.p.x.x.SetBytes(m[0*numBytes : 1*numBytes])
  195. e.p.x.y.SetBytes(m[1*numBytes : 2*numBytes])
  196. e.p.y.x.SetBytes(m[2*numBytes : 3*numBytes])
  197. e.p.y.y.SetBytes(m[3*numBytes : 4*numBytes])
  198. if e.p.x.x.Sign() == 0 &&
  199. e.p.x.y.Sign() == 0 &&
  200. e.p.y.x.Sign() == 0 &&
  201. e.p.y.y.Sign() == 0 {
  202. // This is the point at infinity.
  203. e.p.y.SetOne()
  204. e.p.z.SetZero()
  205. } else {
  206. e.p.z.SetOne()
  207. if !e.p.IsOnCurve() {
  208. println("X")
  209. return nil, false
  210. }
  211. }
  212. return e, true
  213. }
  214. // GT is an abstract cyclic group. The zero value is suitable for use as the
  215. // output of an operation, but cannot be used as an input.
  216. type GT struct {
  217. p *gfP12
  218. }
  219. func (g *GT) String() string {
  220. return "bn256.GT" + g.p.String()
  221. }
  222. // ScalarMult sets e to a*k and then returns e.
  223. func (e *GT) ScalarMult(a *GT, k *big.Int) *GT {
  224. if e.p == nil {
  225. e.p = newGFp12(nil)
  226. }
  227. e.p.Exp(a.p, k, new(bnPool))
  228. return e
  229. }
  230. // Add sets e to a+b and then returns e.
  231. func (e *GT) Add(a, b *GT) *GT {
  232. if e.p == nil {
  233. e.p = newGFp12(nil)
  234. }
  235. e.p.Mul(a.p, b.p, new(bnPool))
  236. return e
  237. }
  238. // Neg sets e to -a and then returns e.
  239. func (e *GT) Neg(a *GT) *GT {
  240. if e.p == nil {
  241. e.p = newGFp12(nil)
  242. }
  243. e.p.Invert(a.p, new(bnPool))
  244. return e
  245. }
  246. // Marshal converts n into a byte slice.
  247. func (n *GT) Marshal() []byte {
  248. n.p.Minimal()
  249. xxxBytes := n.p.x.x.x.Bytes()
  250. xxyBytes := n.p.x.x.y.Bytes()
  251. xyxBytes := n.p.x.y.x.Bytes()
  252. xyyBytes := n.p.x.y.y.Bytes()
  253. xzxBytes := n.p.x.z.x.Bytes()
  254. xzyBytes := n.p.x.z.y.Bytes()
  255. yxxBytes := n.p.y.x.x.Bytes()
  256. yxyBytes := n.p.y.x.y.Bytes()
  257. yyxBytes := n.p.y.y.x.Bytes()
  258. yyyBytes := n.p.y.y.y.Bytes()
  259. yzxBytes := n.p.y.z.x.Bytes()
  260. yzyBytes := n.p.y.z.y.Bytes()
  261. // Each value is a 256-bit number.
  262. const numBytes = 256 / 8
  263. ret := make([]byte, numBytes*12)
  264. copy(ret[1*numBytes-len(xxxBytes):], xxxBytes)
  265. copy(ret[2*numBytes-len(xxyBytes):], xxyBytes)
  266. copy(ret[3*numBytes-len(xyxBytes):], xyxBytes)
  267. copy(ret[4*numBytes-len(xyyBytes):], xyyBytes)
  268. copy(ret[5*numBytes-len(xzxBytes):], xzxBytes)
  269. copy(ret[6*numBytes-len(xzyBytes):], xzyBytes)
  270. copy(ret[7*numBytes-len(yxxBytes):], yxxBytes)
  271. copy(ret[8*numBytes-len(yxyBytes):], yxyBytes)
  272. copy(ret[9*numBytes-len(yyxBytes):], yyxBytes)
  273. copy(ret[10*numBytes-len(yyyBytes):], yyyBytes)
  274. copy(ret[11*numBytes-len(yzxBytes):], yzxBytes)
  275. copy(ret[12*numBytes-len(yzyBytes):], yzyBytes)
  276. return ret
  277. }
  278. // Unmarshal sets e to the result of converting the output of Marshal back into
  279. // a group element and then returns e.
  280. func (e *GT) Unmarshal(m []byte) (*GT, bool) {
  281. // Each value is a 256-bit number.
  282. const numBytes = 256 / 8
  283. if len(m) != 12*numBytes {
  284. return nil, false
  285. }
  286. if e.p == nil {
  287. e.p = newGFp12(nil)
  288. }
  289. e.p.x.x.x.SetBytes(m[0*numBytes : 1*numBytes])
  290. e.p.x.x.y.SetBytes(m[1*numBytes : 2*numBytes])
  291. e.p.x.y.x.SetBytes(m[2*numBytes : 3*numBytes])
  292. e.p.x.y.y.SetBytes(m[3*numBytes : 4*numBytes])
  293. e.p.x.z.x.SetBytes(m[4*numBytes : 5*numBytes])
  294. e.p.x.z.y.SetBytes(m[5*numBytes : 6*numBytes])
  295. e.p.y.x.x.SetBytes(m[6*numBytes : 7*numBytes])
  296. e.p.y.x.y.SetBytes(m[7*numBytes : 8*numBytes])
  297. e.p.y.y.x.SetBytes(m[8*numBytes : 9*numBytes])
  298. e.p.y.y.y.SetBytes(m[9*numBytes : 10*numBytes])
  299. e.p.y.z.x.SetBytes(m[10*numBytes : 11*numBytes])
  300. e.p.y.z.y.SetBytes(m[11*numBytes : 12*numBytes])
  301. return e, true
  302. }
  303. // Pair calculates an Optimal Ate pairing.
  304. func Pair(g1 *G1, g2 *G2) *GT {
  305. return &GT{optimalAte(g2.p, g1.p, new(bnPool))}
  306. }
  307. // bnPool implements a tiny cache of *big.Int objects that's used to reduce the
  308. // number of allocations made during processing.
  309. type bnPool struct {
  310. bns []*big.Int
  311. count int
  312. }
  313. func (pool *bnPool) Get() *big.Int {
  314. if pool == nil {
  315. return new(big.Int)
  316. }
  317. pool.count++
  318. l := len(pool.bns)
  319. if l == 0 {
  320. return new(big.Int)
  321. }
  322. bn := pool.bns[l-1]
  323. pool.bns = pool.bns[:l-1]
  324. return bn
  325. }
  326. func (pool *bnPool) Put(bn *big.Int) {
  327. if pool == nil {
  328. return
  329. }
  330. pool.bns = append(pool.bns, bn)
  331. pool.count--
  332. }
  333. func (pool *bnPool) Count() int {
  334. return pool.count
  335. }