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.

169 lines
3.3 KiB

  1. package bn128
  2. import (
  3. "bytes"
  4. "math/big"
  5. )
  6. type G1 struct {
  7. F Fq
  8. G [3]*big.Int
  9. }
  10. func NewG1(f Fq, g [2]*big.Int) G1 {
  11. var g1 G1
  12. g1.F = f
  13. g1.G = [3]*big.Int{
  14. g[0],
  15. g[1],
  16. g1.F.One(),
  17. }
  18. return g1
  19. }
  20. func (g1 G1) Zero() [2]*big.Int {
  21. return [2]*big.Int{g1.F.Zero(), g1.F.Zero()}
  22. }
  23. func (g1 G1) IsZero(p [3]*big.Int) bool {
  24. return g1.F.IsZero(p[2])
  25. }
  26. func (g1 G1) Add(p1, p2 [3]*big.Int) [3]*big.Int {
  27. // https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates
  28. // https://github.com/zcash/zcash/blob/master/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.cpp#L208
  29. // http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/addition/add-2007-bl.op3
  30. if g1.IsZero(p1) {
  31. return p2
  32. }
  33. if g1.IsZero(p2) {
  34. return p1
  35. }
  36. x1 := p1[0]
  37. y1 := p1[1]
  38. z1 := p1[2]
  39. x2 := p2[0]
  40. y2 := p2[1]
  41. z2 := p2[2]
  42. z1z1 := g1.F.Square(z1)
  43. z2z2 := g1.F.Square(z2)
  44. u1 := g1.F.Mul(x1, z2z2)
  45. u2 := g1.F.Mul(x2, z1z1)
  46. t0 := g1.F.Mul(z2, z2z2)
  47. s1 := g1.F.Mul(y1, t0)
  48. t1 := g1.F.Mul(z1, z1z1)
  49. s2 := g1.F.Mul(y2, t1)
  50. h := g1.F.Sub(u2, u1)
  51. t2 := g1.F.Add(h, h)
  52. i := g1.F.Square(t2)
  53. j := g1.F.Mul(h, i)
  54. t3 := g1.F.Sub(s2, s1)
  55. r := g1.F.Add(t3, t3)
  56. v := g1.F.Mul(u1, i)
  57. t4 := g1.F.Square(r)
  58. t5 := g1.F.Add(v, v)
  59. t6 := g1.F.Sub(t4, j)
  60. x3 := g1.F.Sub(t6, t5)
  61. t7 := g1.F.Sub(v, x3)
  62. t8 := g1.F.Mul(s1, j)
  63. t9 := g1.F.Add(t8, t8)
  64. t10 := g1.F.Mul(r, t7)
  65. y3 := g1.F.Sub(t10, t9)
  66. t11 := g1.F.Add(z1, z2)
  67. t12 := g1.F.Square(t11)
  68. t13 := g1.F.Sub(t12, z1z1)
  69. t14 := g1.F.Sub(t13, z2z2)
  70. z3 := g1.F.Mul(t14, h)
  71. return [3]*big.Int{x3, y3, z3}
  72. }
  73. func (g1 G1) Neg(p [3]*big.Int) [3]*big.Int {
  74. return [3]*big.Int{
  75. p[0],
  76. g1.F.Neg(p[1]),
  77. p[2],
  78. }
  79. }
  80. func (g1 G1) Sub(a, b [3]*big.Int) [3]*big.Int {
  81. return g1.Add(a, g1.Neg(b))
  82. }
  83. func (g1 G1) Double(p [3]*big.Int) [3]*big.Int {
  84. // https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates
  85. // http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/doubling/dbl-2009-l.op3
  86. // https://github.com/zcash/zcash/blob/master/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.cpp#L325
  87. if g1.IsZero(p) {
  88. return p
  89. }
  90. a := g1.F.Square(p[0])
  91. b := g1.F.Square(p[1])
  92. c := g1.F.Square(b)
  93. t0 := g1.F.Add(p[0], b)
  94. t1 := g1.F.Square(t0)
  95. t2 := g1.F.Sub(t1, a)
  96. t3 := g1.F.Sub(t2, c)
  97. d := g1.F.Double(t3)
  98. e := g1.F.Add(g1.F.Add(a, a), a) // e = 3*a
  99. f := g1.F.Square(e)
  100. t4 := g1.F.Double(d)
  101. x3 := g1.F.Sub(f, t4)
  102. t5 := g1.F.Sub(d, x3)
  103. twoC := g1.F.Add(c, c)
  104. fourC := g1.F.Add(twoC, twoC)
  105. t6 := g1.F.Add(fourC, fourC)
  106. t7 := g1.F.Mul(e, t5)
  107. y3 := g1.F.Sub(t7, t6)
  108. t8 := g1.F.Mul(p[1], p[2])
  109. z3 := g1.F.Double(t8)
  110. return [3]*big.Int{x3, y3, z3}
  111. }
  112. func (g1 G1) MulScalar(base [3]*big.Int, e *big.Int) [3]*big.Int {
  113. // res := g1.Zero()
  114. res := [3]*big.Int{g1.F.Zero(), g1.F.Zero(), g1.F.Zero()}
  115. rem := e
  116. exp := base
  117. for !bytes.Equal(rem.Bytes(), big.NewInt(int64(0)).Bytes()) {
  118. // if rem % 2 == 1
  119. if bytes.Equal(new(big.Int).Rem(rem, big.NewInt(int64(2))).Bytes(), big.NewInt(int64(1)).Bytes()) {
  120. res = g1.Add(res, exp)
  121. }
  122. exp = g1.Double(exp)
  123. rem = rem.Rsh(rem, 1) // rem = rem >> 1
  124. }
  125. return res
  126. }
  127. func (g1 G1) Affine(p [3]*big.Int) [2]*big.Int {
  128. if g1.IsZero(p) {
  129. return g1.Zero()
  130. }
  131. zinv := g1.F.Inverse(p[2])
  132. zinv2 := g1.F.Square(zinv)
  133. x := g1.F.Mul(p[0], zinv2)
  134. zinv3 := g1.F.Mul(zinv2, zinv)
  135. y := g1.F.Mul(p[1], zinv3)
  136. return [2]*big.Int{x, y}
  137. }