bn256.go 9.6 KB

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