You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

409 lines
10 KiB

  1. package bn128
  2. import (
  3. "errors"
  4. "math/big"
  5. "github.com/arnaucube/go-snark/fields"
  6. )
  7. type Bn128 struct {
  8. Q *big.Int
  9. R *big.Int
  10. Gg1 [2]*big.Int
  11. Gg2 [2][2]*big.Int
  12. NonResidueFq2 *big.Int
  13. NonResidueFq6 [2]*big.Int
  14. Fq1 fields.Fq
  15. Fq2 fields.Fq2
  16. Fq6 fields.Fq6
  17. Fq12 fields.Fq12
  18. G1 G1
  19. G2 G2
  20. LoopCount *big.Int
  21. LoopCountNeg bool
  22. TwoInv *big.Int
  23. CoefB *big.Int
  24. TwistCoefB [2]*big.Int
  25. Twist [2]*big.Int
  26. FrobeniusCoeffsC11 *big.Int
  27. TwistMulByQX [2]*big.Int
  28. TwistMulByQY [2]*big.Int
  29. FinalExp *big.Int
  30. }
  31. func NewBn128() (Bn128, error) {
  32. var b Bn128
  33. q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10)
  34. if !ok {
  35. return b, errors.New("err with q")
  36. }
  37. b.Q = q
  38. r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  39. if !ok {
  40. return b, errors.New("err with r")
  41. }
  42. b.R = r
  43. b.Gg1 = [2]*big.Int{
  44. big.NewInt(int64(1)),
  45. big.NewInt(int64(2)),
  46. }
  47. g2_00, ok := new(big.Int).SetString("10857046999023057135944570762232829481370756359578518086990519993285655852781", 10)
  48. if !ok {
  49. return b, errors.New("err with g2_00")
  50. }
  51. g2_01, ok := new(big.Int).SetString("11559732032986387107991004021392285783925812861821192530917403151452391805634", 10)
  52. if !ok {
  53. return b, errors.New("err with g2_00")
  54. }
  55. g2_10, ok := new(big.Int).SetString("8495653923123431417604973247489272438418190587263600148770280649306958101930", 10)
  56. if !ok {
  57. return b, errors.New("err with g2_00")
  58. }
  59. g2_11, ok := new(big.Int).SetString("4082367875863433681332203403145435568316851327593401208105741076214120093531", 10)
  60. if !ok {
  61. return b, errors.New("err with g2_00")
  62. }
  63. b.Gg2 = [2][2]*big.Int{
  64. [2]*big.Int{
  65. g2_00,
  66. g2_01,
  67. },
  68. [2]*big.Int{
  69. g2_10,
  70. g2_11,
  71. },
  72. }
  73. b.Fq1 = fields.NewFq(q)
  74. b.NonResidueFq2, ok = new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i
  75. if !ok {
  76. return b, errors.New("err with nonResidueFq2")
  77. }
  78. b.NonResidueFq6 = [2]*big.Int{
  79. big.NewInt(int64(9)),
  80. big.NewInt(int64(1)),
  81. }
  82. b.Fq2 = fields.NewFq2(b.Fq1, b.NonResidueFq2)
  83. b.Fq6 = fields.NewFq6(b.Fq2, b.NonResidueFq6)
  84. b.Fq12 = fields.NewFq12(b.Fq6, b.Fq2, b.NonResidueFq6)
  85. b.G1 = NewG1(b.Fq1, b.Gg1)
  86. b.G2 = NewG2(b.Fq2, b.Gg2)
  87. err := b.preparePairing()
  88. if err != nil {
  89. return b, err
  90. }
  91. return b, nil
  92. }
  93. func (bn128 *Bn128) preparePairing() error {
  94. var ok bool
  95. bn128.LoopCount, ok = new(big.Int).SetString("29793968203157093288", 10)
  96. if !ok {
  97. return errors.New("err with LoopCount from string")
  98. }
  99. bn128.LoopCountNeg = false
  100. bn128.TwoInv = bn128.Fq1.Inverse(big.NewInt(int64(2)))
  101. bn128.CoefB = big.NewInt(int64(3))
  102. bn128.Twist = [2]*big.Int{
  103. big.NewInt(int64(9)),
  104. big.NewInt(int64(1)),
  105. }
  106. bn128.TwistCoefB = bn128.Fq2.MulScalar(bn128.Fq2.Inverse(bn128.Twist), bn128.CoefB)
  107. bn128.FrobeniusCoeffsC11, ok = new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10)
  108. if !ok {
  109. return errors.New("error parsing frobeniusCoeffsC11")
  110. }
  111. a, ok := new(big.Int).SetString("21575463638280843010398324269430826099269044274347216827212613867836435027261", 10)
  112. if !ok {
  113. return errors.New("error parsing a")
  114. }
  115. b, ok := new(big.Int).SetString("10307601595873709700152284273816112264069230130616436755625194854815875713954", 10)
  116. if !ok {
  117. return errors.New("error parsing b")
  118. }
  119. bn128.TwistMulByQX = [2]*big.Int{
  120. a,
  121. b,
  122. }
  123. a, ok = new(big.Int).SetString("2821565182194536844548159561693502659359617185244120367078079554186484126554", 10)
  124. if !ok {
  125. return errors.New("error parsing a")
  126. }
  127. b, ok = new(big.Int).SetString("3505843767911556378687030309984248845540243509899259641013678093033130930403", 10)
  128. if !ok {
  129. return errors.New("error parsing b")
  130. }
  131. bn128.TwistMulByQY = [2]*big.Int{
  132. a,
  133. b,
  134. }
  135. bn128.FinalExp, ok = new(big.Int).SetString("552484233613224096312617126783173147097382103762957654188882734314196910839907541213974502761540629817009608548654680343627701153829446747810907373256841551006201639677726139946029199968412598804882391702273019083653272047566316584365559776493027495458238373902875937659943504873220554161550525926302303331747463515644711876653177129578303191095900909191624817826566688241804408081892785725967931714097716709526092261278071952560171111444072049229123565057483750161460024353346284167282452756217662335528813519139808291170539072125381230815729071544861602750936964829313608137325426383735122175229541155376346436093930287402089517426973178917569713384748081827255472576937471496195752727188261435633271238710131736096299798168852925540549342330775279877006784354801422249722573783561685179618816480037695005515426162362431072245638324744480", 10)
  136. if !ok {
  137. return errors.New("error parsing finalExp")
  138. }
  139. return nil
  140. }
  141. func (bn128 Bn128) Pairing(p1 [3]*big.Int, p2 [3][2]*big.Int) ([2][3][2]*big.Int, error) {
  142. pre1 := bn128.PreComputeG1(p1)
  143. pre2, err := bn128.PreComputeG2(p2)
  144. if err != nil {
  145. return [2][3][2]*big.Int{}, err
  146. }
  147. r1 := bn128.MillerLoop(pre1, pre2)
  148. res := bn128.FinalExponentiation(r1)
  149. return res, nil
  150. }
  151. type AteG1Precomp struct {
  152. Px *big.Int
  153. Py *big.Int
  154. }
  155. func (bn128 Bn128) PreComputeG1(p [3]*big.Int) AteG1Precomp {
  156. pCopy := bn128.G1.Affine(p)
  157. res := AteG1Precomp{
  158. Px: pCopy[0],
  159. Py: pCopy[1],
  160. }
  161. return res
  162. }
  163. type EllCoeffs struct {
  164. Ell0 [2]*big.Int
  165. EllVW [2]*big.Int
  166. EllVV [2]*big.Int
  167. }
  168. type AteG2Precomp struct {
  169. Qx [2]*big.Int
  170. Qy [2]*big.Int
  171. Coeffs []EllCoeffs
  172. }
  173. func (bn128 Bn128) PreComputeG2(p [3][2]*big.Int) (AteG2Precomp, error) {
  174. qCopy := bn128.G2.Affine(p)
  175. res := AteG2Precomp{
  176. qCopy[0],
  177. qCopy[1],
  178. []EllCoeffs{},
  179. }
  180. r := [3][2]*big.Int{
  181. bn128.Fq2.Copy(qCopy[0]),
  182. bn128.Fq2.Copy(qCopy[1]),
  183. bn128.Fq2.One(),
  184. }
  185. var c EllCoeffs
  186. for i := bn128.LoopCount.BitLen() - 2; i >= 0; i-- {
  187. bit := bn128.LoopCount.Bit(i)
  188. c, r = bn128.DoublingStep(r)
  189. res.Coeffs = append(res.Coeffs, c)
  190. if bit == 1 {
  191. c, r = bn128.MixedAdditionStep(qCopy, r)
  192. res.Coeffs = append(res.Coeffs, c)
  193. }
  194. }
  195. q1 := bn128.G2.Affine(bn128.G2MulByQ(qCopy))
  196. if !bn128.Fq2.Equal(q1[2], bn128.Fq2.One()) {
  197. return res, errors.New("q1[2] != Fq2.One")
  198. }
  199. q2 := bn128.G2.Affine(bn128.G2MulByQ(q1))
  200. if !bn128.Fq2.Equal(q2[2], bn128.Fq2.One()) {
  201. return res, errors.New("q2[2] != Fq2.One")
  202. }
  203. if bn128.LoopCountNeg {
  204. r[1] = bn128.Fq2.Neg(r[1])
  205. }
  206. q2[1] = bn128.Fq2.Neg(q2[1])
  207. c, r = bn128.MixedAdditionStep(q1, r)
  208. res.Coeffs = append(res.Coeffs, c)
  209. c, r = bn128.MixedAdditionStep(q2, r)
  210. res.Coeffs = append(res.Coeffs, c)
  211. return res, nil
  212. }
  213. func (bn128 Bn128) DoublingStep(current [3][2]*big.Int) (EllCoeffs, [3][2]*big.Int) {
  214. x := current[0]
  215. y := current[1]
  216. z := current[2]
  217. a := bn128.Fq2.MulScalar(bn128.Fq2.Mul(x, y), bn128.TwoInv)
  218. b := bn128.Fq2.Square(y)
  219. c := bn128.Fq2.Square(z)
  220. d := bn128.Fq2.Add(c, bn128.Fq2.Add(c, c))
  221. e := bn128.Fq2.Mul(bn128.TwistCoefB, d)
  222. f := bn128.Fq2.Add(e, bn128.Fq2.Add(e, e))
  223. g := bn128.Fq2.MulScalar(bn128.Fq2.Add(b, f), bn128.TwoInv)
  224. h := bn128.Fq2.Sub(
  225. bn128.Fq2.Square(bn128.Fq2.Add(y, z)),
  226. bn128.Fq2.Add(b, c))
  227. i := bn128.Fq2.Sub(e, b)
  228. j := bn128.Fq2.Square(x)
  229. eSqr := bn128.Fq2.Square(e)
  230. current[0] = bn128.Fq2.Mul(a, bn128.Fq2.Sub(b, f))
  231. current[1] = bn128.Fq2.Sub(bn128.Fq2.Sub(bn128.Fq2.Square(g), eSqr),
  232. bn128.Fq2.Add(eSqr, eSqr))
  233. current[2] = bn128.Fq2.Mul(b, h)
  234. res := EllCoeffs{
  235. Ell0: bn128.Fq2.Mul(i, bn128.Twist),
  236. EllVW: bn128.Fq2.Neg(h),
  237. EllVV: bn128.Fq2.Add(j, bn128.Fq2.Add(j, j)),
  238. }
  239. return res, current
  240. }
  241. func (bn128 Bn128) MixedAdditionStep(base, current [3][2]*big.Int) (EllCoeffs, [3][2]*big.Int) {
  242. x1 := current[0]
  243. y1 := current[1]
  244. z1 := current[2]
  245. x2 := base[0]
  246. y2 := base[1]
  247. d := bn128.Fq2.Sub(x1, bn128.Fq2.Mul(x2, z1))
  248. e := bn128.Fq2.Sub(y1, bn128.Fq2.Mul(y2, z1))
  249. f := bn128.Fq2.Square(d)
  250. g := bn128.Fq2.Square(e)
  251. h := bn128.Fq2.Mul(d, f)
  252. i := bn128.Fq2.Mul(x1, f)
  253. j := bn128.Fq2.Sub(
  254. bn128.Fq2.Add(h, bn128.Fq2.Mul(z1, g)),
  255. bn128.Fq2.Add(i, i))
  256. current[0] = bn128.Fq2.Mul(d, j)
  257. current[1] = bn128.Fq2.Sub(
  258. bn128.Fq2.Mul(e, bn128.Fq2.Sub(i, j)),
  259. bn128.Fq2.Mul(h, y1))
  260. current[2] = bn128.Fq2.Mul(z1, h)
  261. coef := EllCoeffs{
  262. Ell0: bn128.Fq2.Mul(
  263. bn128.Twist,
  264. bn128.Fq2.Sub(
  265. bn128.Fq2.Mul(e, x2),
  266. bn128.Fq2.Mul(d, y2))),
  267. EllVW: d,
  268. EllVV: bn128.Fq2.Neg(e),
  269. }
  270. return coef, current
  271. }
  272. func (bn128 Bn128) G2MulByQ(p [3][2]*big.Int) [3][2]*big.Int {
  273. fmx := [2]*big.Int{
  274. p[0][0],
  275. bn128.Fq1.Mul(p[0][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)),
  276. }
  277. fmy := [2]*big.Int{
  278. p[1][0],
  279. bn128.Fq1.Mul(p[1][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)),
  280. }
  281. fmz := [2]*big.Int{
  282. p[2][0],
  283. bn128.Fq1.Mul(p[2][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)),
  284. }
  285. return [3][2]*big.Int{
  286. bn128.Fq2.Mul(bn128.TwistMulByQX, fmx),
  287. bn128.Fq2.Mul(bn128.TwistMulByQY, fmy),
  288. fmz,
  289. }
  290. }
  291. func (bn128 Bn128) MillerLoop(pre1 AteG1Precomp, pre2 AteG2Precomp) [2][3][2]*big.Int {
  292. // https://cryptojedi.org/papers/dclxvi-20100714.pdf
  293. // https://eprint.iacr.org/2008/096.pdf
  294. idx := 0
  295. var c EllCoeffs
  296. f := bn128.Fq12.One()
  297. for i := bn128.LoopCount.BitLen() - 2; i >= 0; i-- {
  298. bit := bn128.LoopCount.Bit(i)
  299. c = pre2.Coeffs[idx]
  300. idx++
  301. f = bn128.Fq12.Square(f)
  302. f = bn128.MulBy024(f,
  303. c.Ell0,
  304. bn128.Fq2.MulScalar(c.EllVW, pre1.Py),
  305. bn128.Fq2.MulScalar(c.EllVV, pre1.Px))
  306. if bit == 1 {
  307. c = pre2.Coeffs[idx]
  308. idx++
  309. f = bn128.MulBy024(
  310. f,
  311. c.Ell0,
  312. bn128.Fq2.MulScalar(c.EllVW, pre1.Py),
  313. bn128.Fq2.MulScalar(c.EllVV, pre1.Px))
  314. }
  315. }
  316. if bn128.LoopCountNeg {
  317. f = bn128.Fq12.Inverse(f)
  318. }
  319. c = pre2.Coeffs[idx]
  320. idx++
  321. f = bn128.MulBy024(
  322. f,
  323. c.Ell0,
  324. bn128.Fq2.MulScalar(c.EllVW, pre1.Py),
  325. bn128.Fq2.MulScalar(c.EllVV, pre1.Px))
  326. c = pre2.Coeffs[idx]
  327. idx++
  328. f = bn128.MulBy024(
  329. f,
  330. c.Ell0,
  331. bn128.Fq2.MulScalar(c.EllVW, pre1.Py),
  332. bn128.Fq2.MulScalar(c.EllVV, pre1.Px))
  333. return f
  334. }
  335. func (bn128 Bn128) MulBy024(a [2][3][2]*big.Int, ell0, ellVW, ellVV [2]*big.Int) [2][3][2]*big.Int {
  336. b := [2][3][2]*big.Int{
  337. [3][2]*big.Int{
  338. ell0,
  339. bn128.Fq2.Zero(),
  340. ellVV,
  341. },
  342. [3][2]*big.Int{
  343. bn128.Fq2.Zero(),
  344. ellVW,
  345. bn128.Fq2.Zero(),
  346. },
  347. }
  348. return bn128.Fq12.Mul(a, b)
  349. }
  350. func (bn128 Bn128) FinalExponentiation(r [2][3][2]*big.Int) [2][3][2]*big.Int {
  351. res := bn128.Fq12.Exp(r, bn128.FinalExp)
  352. return res
  353. }