github.com/dotlike13/wemix30_go@v1.8.23/crypto/bn256/cloudflare/optate.go (about) 1 package bn256 2 3 func lineFunctionAdd(r, p *twistPoint, q *curvePoint, r2 *gfP2) (a, b, c *gfP2, rOut *twistPoint) { 4 // See the mixed addition algorithm from "Faster Computation of the 5 // Tate Pairing", http://arxiv.org/pdf/0904.0854v3.pdf 6 B := (&gfP2{}).Mul(&p.x, &r.t) 7 8 D := (&gfP2{}).Add(&p.y, &r.z) 9 D.Square(D).Sub(D, r2).Sub(D, &r.t).Mul(D, &r.t) 10 11 H := (&gfP2{}).Sub(B, &r.x) 12 I := (&gfP2{}).Square(H) 13 14 E := (&gfP2{}).Add(I, I) 15 E.Add(E, E) 16 17 J := (&gfP2{}).Mul(H, E) 18 19 L1 := (&gfP2{}).Sub(D, &r.y) 20 L1.Sub(L1, &r.y) 21 22 V := (&gfP2{}).Mul(&r.x, E) 23 24 rOut = &twistPoint{} 25 rOut.x.Square(L1).Sub(&rOut.x, J).Sub(&rOut.x, V).Sub(&rOut.x, V) 26 27 rOut.z.Add(&r.z, H).Square(&rOut.z).Sub(&rOut.z, &r.t).Sub(&rOut.z, I) 28 29 t := (&gfP2{}).Sub(V, &rOut.x) 30 t.Mul(t, L1) 31 t2 := (&gfP2{}).Mul(&r.y, J) 32 t2.Add(t2, t2) 33 rOut.y.Sub(t, t2) 34 35 rOut.t.Square(&rOut.z) 36 37 t.Add(&p.y, &rOut.z).Square(t).Sub(t, r2).Sub(t, &rOut.t) 38 39 t2.Mul(L1, &p.x) 40 t2.Add(t2, t2) 41 a = (&gfP2{}).Sub(t2, t) 42 43 c = (&gfP2{}).MulScalar(&rOut.z, &q.y) 44 c.Add(c, c) 45 46 b = (&gfP2{}).Neg(L1) 47 b.MulScalar(b, &q.x).Add(b, b) 48 49 return 50 } 51 52 func lineFunctionDouble(r *twistPoint, q *curvePoint) (a, b, c *gfP2, rOut *twistPoint) { 53 // See the doubling algorithm for a=0 from "Faster Computation of the 54 // Tate Pairing", http://arxiv.org/pdf/0904.0854v3.pdf 55 A := (&gfP2{}).Square(&r.x) 56 B := (&gfP2{}).Square(&r.y) 57 C := (&gfP2{}).Square(B) 58 59 D := (&gfP2{}).Add(&r.x, B) 60 D.Square(D).Sub(D, A).Sub(D, C).Add(D, D) 61 62 E := (&gfP2{}).Add(A, A) 63 E.Add(E, A) 64 65 G := (&gfP2{}).Square(E) 66 67 rOut = &twistPoint{} 68 rOut.x.Sub(G, D).Sub(&rOut.x, D) 69 70 rOut.z.Add(&r.y, &r.z).Square(&rOut.z).Sub(&rOut.z, B).Sub(&rOut.z, &r.t) 71 72 rOut.y.Sub(D, &rOut.x).Mul(&rOut.y, E) 73 t := (&gfP2{}).Add(C, C) 74 t.Add(t, t).Add(t, t) 75 rOut.y.Sub(&rOut.y, t) 76 77 rOut.t.Square(&rOut.z) 78 79 t.Mul(E, &r.t).Add(t, t) 80 b = (&gfP2{}).Neg(t) 81 b.MulScalar(b, &q.x) 82 83 a = (&gfP2{}).Add(&r.x, E) 84 a.Square(a).Sub(a, A).Sub(a, G) 85 t.Add(B, B).Add(t, t) 86 a.Sub(a, t) 87 88 c = (&gfP2{}).Mul(&rOut.z, &r.t) 89 c.Add(c, c).MulScalar(c, &q.y) 90 91 return 92 } 93 94 func mulLine(ret *gfP12, a, b, c *gfP2) { 95 a2 := &gfP6{} 96 a2.y.Set(a) 97 a2.z.Set(b) 98 a2.Mul(a2, &ret.x) 99 t3 := (&gfP6{}).MulScalar(&ret.y, c) 100 101 t := (&gfP2{}).Add(b, c) 102 t2 := &gfP6{} 103 t2.y.Set(a) 104 t2.z.Set(t) 105 ret.x.Add(&ret.x, &ret.y) 106 107 ret.y.Set(t3) 108 109 ret.x.Mul(&ret.x, t2).Sub(&ret.x, a2).Sub(&ret.x, &ret.y) 110 a2.MulTau(a2) 111 ret.y.Add(&ret.y, a2) 112 } 113 114 // sixuPlus2NAF is 6u+2 in non-adjacent form. 115 var sixuPlus2NAF = []int8{0, 0, 0, 1, 0, 1, 0, -1, 0, 0, 1, -1, 0, 0, 1, 0, 116 0, 1, 1, 0, -1, 0, 0, 1, 0, -1, 0, 0, 0, 0, 1, 1, 117 1, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 118 1, 0, 0, -1, 0, 0, 0, 1, 1, 0, -1, 0, 0, 1, 0, 1, 1} 119 120 // miller implements the Miller loop for calculating the Optimal Ate pairing. 121 // See algorithm 1 from http://cryptojedi.org/papers/dclxvi-20100714.pdf 122 func miller(q *twistPoint, p *curvePoint) *gfP12 { 123 ret := (&gfP12{}).SetOne() 124 125 aAffine := &twistPoint{} 126 aAffine.Set(q) 127 aAffine.MakeAffine() 128 129 bAffine := &curvePoint{} 130 bAffine.Set(p) 131 bAffine.MakeAffine() 132 133 minusA := &twistPoint{} 134 minusA.Neg(aAffine) 135 136 r := &twistPoint{} 137 r.Set(aAffine) 138 139 r2 := (&gfP2{}).Square(&aAffine.y) 140 141 for i := len(sixuPlus2NAF) - 1; i > 0; i-- { 142 a, b, c, newR := lineFunctionDouble(r, bAffine) 143 if i != len(sixuPlus2NAF)-1 { 144 ret.Square(ret) 145 } 146 147 mulLine(ret, a, b, c) 148 r = newR 149 150 switch sixuPlus2NAF[i-1] { 151 case 1: 152 a, b, c, newR = lineFunctionAdd(r, aAffine, bAffine, r2) 153 case -1: 154 a, b, c, newR = lineFunctionAdd(r, minusA, bAffine, r2) 155 default: 156 continue 157 } 158 159 mulLine(ret, a, b, c) 160 r = newR 161 } 162 163 // In order to calculate Q1 we have to convert q from the sextic twist 164 // to the full GF(p^12) group, apply the Frobenius there, and convert 165 // back. 166 // 167 // The twist isomorphism is (x', y') -> (xω², yω³). If we consider just 168 // x for a moment, then after applying the Frobenius, we have x̄ω^(2p) 169 // where x̄ is the conjugate of x. If we are going to apply the inverse 170 // isomorphism we need a value with a single coefficient of ω² so we 171 // rewrite this as x̄ω^(2p-2)ω². ξ⁶ = ω and, due to the construction of 172 // p, 2p-2 is a multiple of six. Therefore we can rewrite as 173 // x̄ξ^((p-1)/3)ω² and applying the inverse isomorphism eliminates the 174 // ω². 175 // 176 // A similar argument can be made for the y value. 177 178 q1 := &twistPoint{} 179 q1.x.Conjugate(&aAffine.x).Mul(&q1.x, xiToPMinus1Over3) 180 q1.y.Conjugate(&aAffine.y).Mul(&q1.y, xiToPMinus1Over2) 181 q1.z.SetOne() 182 q1.t.SetOne() 183 184 // For Q2 we are applying the p² Frobenius. The two conjugations cancel 185 // out and we are left only with the factors from the isomorphism. In 186 // the case of x, we end up with a pure number which is why 187 // xiToPSquaredMinus1Over3 is ∈ GF(p). With y we get a factor of -1. We 188 // ignore this to end up with -Q2. 189 190 minusQ2 := &twistPoint{} 191 minusQ2.x.MulScalar(&aAffine.x, xiToPSquaredMinus1Over3) 192 minusQ2.y.Set(&aAffine.y) 193 minusQ2.z.SetOne() 194 minusQ2.t.SetOne() 195 196 r2.Square(&q1.y) 197 a, b, c, newR := lineFunctionAdd(r, q1, bAffine, r2) 198 mulLine(ret, a, b, c) 199 r = newR 200 201 r2.Square(&minusQ2.y) 202 a, b, c, newR = lineFunctionAdd(r, minusQ2, bAffine, r2) 203 mulLine(ret, a, b, c) 204 r = newR 205 206 return ret 207 } 208 209 // finalExponentiation computes the (p¹²-1)/Order-th power of an element of 210 // GF(p¹²) to obtain an element of GT (steps 13-15 of algorithm 1 from 211 // http://cryptojedi.org/papers/dclxvi-20100714.pdf) 212 func finalExponentiation(in *gfP12) *gfP12 { 213 t1 := &gfP12{} 214 215 // This is the p^6-Frobenius 216 t1.x.Neg(&in.x) 217 t1.y.Set(&in.y) 218 219 inv := &gfP12{} 220 inv.Invert(in) 221 t1.Mul(t1, inv) 222 223 t2 := (&gfP12{}).FrobeniusP2(t1) 224 t1.Mul(t1, t2) 225 226 fp := (&gfP12{}).Frobenius(t1) 227 fp2 := (&gfP12{}).FrobeniusP2(t1) 228 fp3 := (&gfP12{}).Frobenius(fp2) 229 230 fu := (&gfP12{}).Exp(t1, u) 231 fu2 := (&gfP12{}).Exp(fu, u) 232 fu3 := (&gfP12{}).Exp(fu2, u) 233 234 y3 := (&gfP12{}).Frobenius(fu) 235 fu2p := (&gfP12{}).Frobenius(fu2) 236 fu3p := (&gfP12{}).Frobenius(fu3) 237 y2 := (&gfP12{}).FrobeniusP2(fu2) 238 239 y0 := &gfP12{} 240 y0.Mul(fp, fp2).Mul(y0, fp3) 241 242 y1 := (&gfP12{}).Conjugate(t1) 243 y5 := (&gfP12{}).Conjugate(fu2) 244 y3.Conjugate(y3) 245 y4 := (&gfP12{}).Mul(fu, fu2p) 246 y4.Conjugate(y4) 247 248 y6 := (&gfP12{}).Mul(fu3, fu3p) 249 y6.Conjugate(y6) 250 251 t0 := (&gfP12{}).Square(y6) 252 t0.Mul(t0, y4).Mul(t0, y5) 253 t1.Mul(y3, y5).Mul(t1, t0) 254 t0.Mul(t0, y2) 255 t1.Square(t1).Mul(t1, t0).Square(t1) 256 t0.Mul(t1, y1) 257 t1.Mul(t1, y0) 258 t0.Square(t0).Mul(t0, t1) 259 260 return t0 261 } 262 263 func optimalAte(a *twistPoint, b *curvePoint) *gfP12 { 264 e := miller(a, b) 265 ret := finalExponentiation(e) 266 267 if a.IsInfinity() || b.IsInfinity() { 268 ret.SetOne() 269 } 270 return ret 271 }