25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

417 satır
9.7 KiB

  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. // Each value is a 256-bit number.
  88. const numBytes = 256 / 8
  89. if e.p.IsInfinity() {
  90. return make([]byte, numBytes*2)
  91. }
  92. e.p.MakeAffine(nil)
  93. xBytes := new(big.Int).Mod(e.p.x, p).Bytes()
  94. yBytes := new(big.Int).Mod(e.p.y, p).Bytes()
  95. ret := make([]byte, numBytes*2)
  96. copy(ret[1*numBytes-len(xBytes):], xBytes)
  97. copy(ret[2*numBytes-len(yBytes):], yBytes)
  98. return ret
  99. }
  100. // Unmarshal sets e to the result of converting the output of Marshal back into
  101. // a group element and then returns e.
  102. func (e *G1) Unmarshal(m []byte) (*G1, bool) {
  103. // Each value is a 256-bit number.
  104. const numBytes = 256 / 8
  105. if len(m) != 2*numBytes {
  106. return nil, false
  107. }
  108. if e.p == nil {
  109. e.p = newCurvePoint(nil)
  110. }
  111. e.p.x.SetBytes(m[0*numBytes : 1*numBytes])
  112. e.p.y.SetBytes(m[1*numBytes : 2*numBytes])
  113. if e.p.x.Sign() == 0 && e.p.y.Sign() == 0 {
  114. // This is the point at infinity.
  115. e.p.y.SetInt64(1)
  116. e.p.z.SetInt64(0)
  117. e.p.t.SetInt64(0)
  118. } else {
  119. e.p.z.SetInt64(1)
  120. e.p.t.SetInt64(1)
  121. if !e.p.IsOnCurve() {
  122. return nil, false
  123. }
  124. }
  125. return e, true
  126. }
  127. // G2 is an abstract cyclic group. The zero value is suitable for use as the
  128. // output of an operation, but cannot be used as an input.
  129. type G2 struct {
  130. p *twistPoint
  131. }
  132. // RandomG1 returns x and g₂ˣ where x is a random, non-zero number read from r.
  133. func RandomG2(r io.Reader) (*big.Int, *G2, error) {
  134. var k *big.Int
  135. var err error
  136. for {
  137. k, err = rand.Int(r, Order)
  138. if err != nil {
  139. return nil, nil, err
  140. }
  141. if k.Sign() > 0 {
  142. break
  143. }
  144. }
  145. return k, new(G2).ScalarBaseMult(k), nil
  146. }
  147. func (e *G2) String() string {
  148. return "bn256.G2" + e.p.String()
  149. }
  150. // ScalarBaseMult sets e to g*k where g is the generator of the group and
  151. // then returns out.
  152. func (e *G2) ScalarBaseMult(k *big.Int) *G2 {
  153. if e.p == nil {
  154. e.p = newTwistPoint(nil)
  155. }
  156. e.p.Mul(twistGen, k, new(bnPool))
  157. return e
  158. }
  159. // ScalarMult sets e to a*k and then returns e.
  160. func (e *G2) ScalarMult(a *G2, k *big.Int) *G2 {
  161. if e.p == nil {
  162. e.p = newTwistPoint(nil)
  163. }
  164. e.p.Mul(a.p, k, new(bnPool))
  165. return e
  166. }
  167. // Add sets e to a+b and then returns e.
  168. // BUG(agl): this function is not complete: a==b fails.
  169. func (e *G2) Add(a, b *G2) *G2 {
  170. if e.p == nil {
  171. e.p = newTwistPoint(nil)
  172. }
  173. e.p.Add(a.p, b.p, new(bnPool))
  174. return e
  175. }
  176. // Marshal converts n into a byte slice.
  177. func (n *G2) Marshal() []byte {
  178. // Each value is a 256-bit number.
  179. const numBytes = 256 / 8
  180. if n.p.IsInfinity() {
  181. return make([]byte, numBytes*4)
  182. }
  183. n.p.MakeAffine(nil)
  184. xxBytes := new(big.Int).Mod(n.p.x.x, p).Bytes()
  185. xyBytes := new(big.Int).Mod(n.p.x.y, p).Bytes()
  186. yxBytes := new(big.Int).Mod(n.p.y.x, p).Bytes()
  187. yyBytes := new(big.Int).Mod(n.p.y.y, p).Bytes()
  188. ret := make([]byte, numBytes*4)
  189. copy(ret[1*numBytes-len(xxBytes):], xxBytes)
  190. copy(ret[2*numBytes-len(xyBytes):], xyBytes)
  191. copy(ret[3*numBytes-len(yxBytes):], yxBytes)
  192. copy(ret[4*numBytes-len(yyBytes):], yyBytes)
  193. return ret
  194. }
  195. // Unmarshal sets e to the result of converting the output of Marshal back into
  196. // a group element and then returns e.
  197. func (e *G2) Unmarshal(m []byte) (*G2, bool) {
  198. // Each value is a 256-bit number.
  199. const numBytes = 256 / 8
  200. if len(m) != 4*numBytes {
  201. return nil, false
  202. }
  203. if e.p == nil {
  204. e.p = newTwistPoint(nil)
  205. }
  206. e.p.x.x.SetBytes(m[0*numBytes : 1*numBytes])
  207. e.p.x.y.SetBytes(m[1*numBytes : 2*numBytes])
  208. e.p.y.x.SetBytes(m[2*numBytes : 3*numBytes])
  209. e.p.y.y.SetBytes(m[3*numBytes : 4*numBytes])
  210. if e.p.x.x.Sign() == 0 &&
  211. e.p.x.y.Sign() == 0 &&
  212. e.p.y.x.Sign() == 0 &&
  213. e.p.y.y.Sign() == 0 {
  214. // This is the point at infinity.
  215. e.p.y.SetOne()
  216. e.p.z.SetZero()
  217. e.p.t.SetZero()
  218. } else {
  219. e.p.z.SetOne()
  220. e.p.t.SetOne()
  221. if !e.p.IsOnCurve() {
  222. return nil, false
  223. }
  224. }
  225. return e, true
  226. }
  227. // GT is an abstract cyclic group. The zero value is suitable for use as the
  228. // output of an operation, but cannot be used as an input.
  229. type GT struct {
  230. p *gfP12
  231. }
  232. func (g *GT) String() string {
  233. return "bn256.GT" + g.p.String()
  234. }
  235. // ScalarMult sets e to a*k and then returns e.
  236. func (e *GT) ScalarMult(a *GT, k *big.Int) *GT {
  237. if e.p == nil {
  238. e.p = newGFp12(nil)
  239. }
  240. e.p.Exp(a.p, k, new(bnPool))
  241. return e
  242. }
  243. // Add sets e to a+b and then returns e.
  244. func (e *GT) Add(a, b *GT) *GT {
  245. if e.p == nil {
  246. e.p = newGFp12(nil)
  247. }
  248. e.p.Mul(a.p, b.p, new(bnPool))
  249. return e
  250. }
  251. // Neg sets e to -a and then returns e.
  252. func (e *GT) Neg(a *GT) *GT {
  253. if e.p == nil {
  254. e.p = newGFp12(nil)
  255. }
  256. e.p.Invert(a.p, new(bnPool))
  257. return e
  258. }
  259. // Marshal converts n into a byte slice.
  260. func (n *GT) Marshal() []byte {
  261. n.p.Minimal()
  262. xxxBytes := n.p.x.x.x.Bytes()
  263. xxyBytes := n.p.x.x.y.Bytes()
  264. xyxBytes := n.p.x.y.x.Bytes()
  265. xyyBytes := n.p.x.y.y.Bytes()
  266. xzxBytes := n.p.x.z.x.Bytes()
  267. xzyBytes := n.p.x.z.y.Bytes()
  268. yxxBytes := n.p.y.x.x.Bytes()
  269. yxyBytes := n.p.y.x.y.Bytes()
  270. yyxBytes := n.p.y.y.x.Bytes()
  271. yyyBytes := n.p.y.y.y.Bytes()
  272. yzxBytes := n.p.y.z.x.Bytes()
  273. yzyBytes := n.p.y.z.y.Bytes()
  274. // Each value is a 256-bit number.
  275. const numBytes = 256 / 8
  276. ret := make([]byte, numBytes*12)
  277. copy(ret[1*numBytes-len(xxxBytes):], xxxBytes)
  278. copy(ret[2*numBytes-len(xxyBytes):], xxyBytes)
  279. copy(ret[3*numBytes-len(xyxBytes):], xyxBytes)
  280. copy(ret[4*numBytes-len(xyyBytes):], xyyBytes)
  281. copy(ret[5*numBytes-len(xzxBytes):], xzxBytes)
  282. copy(ret[6*numBytes-len(xzyBytes):], xzyBytes)
  283. copy(ret[7*numBytes-len(yxxBytes):], yxxBytes)
  284. copy(ret[8*numBytes-len(yxyBytes):], yxyBytes)
  285. copy(ret[9*numBytes-len(yyxBytes):], yyxBytes)
  286. copy(ret[10*numBytes-len(yyyBytes):], yyyBytes)
  287. copy(ret[11*numBytes-len(yzxBytes):], yzxBytes)
  288. copy(ret[12*numBytes-len(yzyBytes):], yzyBytes)
  289. return ret
  290. }
  291. // Unmarshal sets e to the result of converting the output of Marshal back into
  292. // a group element and then returns e.
  293. func (e *GT) Unmarshal(m []byte) (*GT, bool) {
  294. // Each value is a 256-bit number.
  295. const numBytes = 256 / 8
  296. if len(m) != 12*numBytes {
  297. return nil, false
  298. }
  299. if e.p == nil {
  300. e.p = newGFp12(nil)
  301. }
  302. e.p.x.x.x.SetBytes(m[0*numBytes : 1*numBytes])
  303. e.p.x.x.y.SetBytes(m[1*numBytes : 2*numBytes])
  304. e.p.x.y.x.SetBytes(m[2*numBytes : 3*numBytes])
  305. e.p.x.y.y.SetBytes(m[3*numBytes : 4*numBytes])
  306. e.p.x.z.x.SetBytes(m[4*numBytes : 5*numBytes])
  307. e.p.x.z.y.SetBytes(m[5*numBytes : 6*numBytes])
  308. e.p.y.x.x.SetBytes(m[6*numBytes : 7*numBytes])
  309. e.p.y.x.y.SetBytes(m[7*numBytes : 8*numBytes])
  310. e.p.y.y.x.SetBytes(m[8*numBytes : 9*numBytes])
  311. e.p.y.y.y.SetBytes(m[9*numBytes : 10*numBytes])
  312. e.p.y.z.x.SetBytes(m[10*numBytes : 11*numBytes])
  313. e.p.y.z.y.SetBytes(m[11*numBytes : 12*numBytes])
  314. return e, true
  315. }
  316. // Pair calculates an Optimal Ate pairing.
  317. func Pair(g1 *G1, g2 *G2) *GT {
  318. return &GT{optimalAte(g2.p, g1.p, new(bnPool))}
  319. }
  320. // bnPool implements a tiny cache of *big.Int objects that's used to reduce the
  321. // number of allocations made during processing.
  322. type bnPool struct {
  323. bns []*big.Int
  324. count int
  325. }
  326. func (pool *bnPool) Get() *big.Int {
  327. if pool == nil {
  328. return new(big.Int)
  329. }
  330. pool.count++
  331. l := len(pool.bns)
  332. if l == 0 {
  333. return new(big.Int)
  334. }
  335. bn := pool.bns[l-1]
  336. pool.bns = pool.bns[:l-1]
  337. return bn
  338. }
  339. func (pool *bnPool) Put(bn *big.Int) {
  340. if pool == nil {
  341. return
  342. }
  343. pool.bns = append(pool.bns, bn)
  344. pool.count--
  345. }
  346. func (pool *bnPool) Count() int {
  347. return pool.count
  348. }