bn256.go 10 KB

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