github.com/arjunbeliever/ignite@v0.0.0-20220406110515-46bbbbec2587/crypto/bn256/cloudflare/twist.go (about) 1 package bn256 2 3 import ( 4 "math/big" 5 ) 6 7 // twistPoint implements the elliptic curve y²=x³+3/ξ over GF(p²). Points are 8 // kept in Jacobian form and t=z² when valid. The group G₂ is the set of 9 // n-torsion points of this curve over GF(p²) (where n = Order) 10 type twistPoint struct { 11 x, y, z, t gfP2 12 } 13 14 var twistB = &gfP2{ 15 gfP{0x38e7ecccd1dcff67, 0x65f0b37d93ce0d3e, 0xd749d0dd22ac00aa, 0x0141b9ce4a688d4d}, 16 gfP{0x3bf938e377b802a8, 0x020b1b273633535d, 0x26b7edf049755260, 0x2514c6324384a86d}, 17 } 18 19 // twistGen is the generator of group G₂. 20 var twistGen = &twistPoint{ 21 gfP2{ 22 gfP{0xafb4737da84c6140, 0x6043dd5a5802d8c4, 0x09e950fc52a02f86, 0x14fef0833aea7b6b}, 23 gfP{0x8e83b5d102bc2026, 0xdceb1935497b0172, 0xfbb8264797811adf, 0x19573841af96503b}, 24 }, 25 gfP2{ 26 gfP{0x64095b56c71856ee, 0xdc57f922327d3cbb, 0x55f935be33351076, 0x0da4a0e693fd6482}, 27 gfP{0x619dfa9d886be9f6, 0xfe7fd297f59e9b78, 0xff9e1a62231b7dfe, 0x28fd7eebae9e4206}, 28 }, 29 gfP2{*newGFp(0), *newGFp(1)}, 30 gfP2{*newGFp(0), *newGFp(1)}, 31 } 32 33 func (c *twistPoint) String() string { 34 c.MakeAffine() 35 x, y := gfP2Decode(&c.x), gfP2Decode(&c.y) 36 return "(" + x.String() + ", " + y.String() + ")" 37 } 38 39 func (c *twistPoint) Set(a *twistPoint) { 40 c.x.Set(&a.x) 41 c.y.Set(&a.y) 42 c.z.Set(&a.z) 43 c.t.Set(&a.t) 44 } 45 46 // IsOnCurve returns true iff c is on the curve. 47 func (c *twistPoint) IsOnCurve() bool { 48 c.MakeAffine() 49 if c.IsInfinity() { 50 return true 51 } 52 53 y2, x3 := &gfP2{}, &gfP2{} 54 y2.Square(&c.y) 55 x3.Square(&c.x).Mul(x3, &c.x).Add(x3, twistB) 56 57 if *y2 != *x3 { 58 return false 59 } 60 cneg := &twistPoint{} 61 cneg.Mul(c, Order) 62 return cneg.z.IsZero() 63 } 64 65 func (c *twistPoint) SetInfinity() { 66 c.x.SetZero() 67 c.y.SetOne() 68 c.z.SetZero() 69 c.t.SetZero() 70 } 71 72 func (c *twistPoint) IsInfinity() bool { 73 return c.z.IsZero() 74 } 75 76 func (c *twistPoint) Add(a, b *twistPoint) { 77 // For additional comments, see the same function in curve.go. 78 79 if a.IsInfinity() { 80 c.Set(b) 81 return 82 } 83 if b.IsInfinity() { 84 c.Set(a) 85 return 86 } 87 88 // See http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/addition/add-2007-bl.op3 89 z12 := (&gfP2{}).Square(&a.z) 90 z22 := (&gfP2{}).Square(&b.z) 91 u1 := (&gfP2{}).Mul(&a.x, z22) 92 u2 := (&gfP2{}).Mul(&b.x, z12) 93 94 t := (&gfP2{}).Mul(&b.z, z22) 95 s1 := (&gfP2{}).Mul(&a.y, t) 96 97 t.Mul(&a.z, z12) 98 s2 := (&gfP2{}).Mul(&b.y, t) 99 100 h := (&gfP2{}).Sub(u2, u1) 101 xEqual := h.IsZero() 102 103 t.Add(h, h) 104 i := (&gfP2{}).Square(t) 105 j := (&gfP2{}).Mul(h, i) 106 107 t.Sub(s2, s1) 108 yEqual := t.IsZero() 109 if xEqual && yEqual { 110 c.Double(a) 111 return 112 } 113 r := (&gfP2{}).Add(t, t) 114 115 v := (&gfP2{}).Mul(u1, i) 116 117 t4 := (&gfP2{}).Square(r) 118 t.Add(v, v) 119 t6 := (&gfP2{}).Sub(t4, j) 120 c.x.Sub(t6, t) 121 122 t.Sub(v, &c.x) // t7 123 t4.Mul(s1, j) // t8 124 t6.Add(t4, t4) // t9 125 t4.Mul(r, t) // t10 126 c.y.Sub(t4, t6) 127 128 t.Add(&a.z, &b.z) // t11 129 t4.Square(t) // t12 130 t.Sub(t4, z12) // t13 131 t4.Sub(t, z22) // t14 132 c.z.Mul(t4, h) 133 } 134 135 func (c *twistPoint) Double(a *twistPoint) { 136 // See http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/doubling/dbl-2009-l.op3 137 A := (&gfP2{}).Square(&a.x) 138 B := (&gfP2{}).Square(&a.y) 139 C := (&gfP2{}).Square(B) 140 141 t := (&gfP2{}).Add(&a.x, B) 142 t2 := (&gfP2{}).Square(t) 143 t.Sub(t2, A) 144 t2.Sub(t, C) 145 d := (&gfP2{}).Add(t2, t2) 146 t.Add(A, A) 147 e := (&gfP2{}).Add(t, A) 148 f := (&gfP2{}).Square(e) 149 150 t.Add(d, d) 151 c.x.Sub(f, t) 152 153 t.Add(C, C) 154 t2.Add(t, t) 155 t.Add(t2, t2) 156 c.y.Sub(d, &c.x) 157 t2.Mul(e, &c.y) 158 c.y.Sub(t2, t) 159 160 t.Mul(&a.y, &a.z) 161 c.z.Add(t, t) 162 } 163 164 func (c *twistPoint) Mul(a *twistPoint, scalar *big.Int) { 165 sum, t := &twistPoint{}, &twistPoint{} 166 167 for i := scalar.BitLen(); i >= 0; i-- { 168 t.Double(sum) 169 if scalar.Bit(i) != 0 { 170 sum.Add(t, a) 171 } else { 172 sum.Set(t) 173 } 174 } 175 176 c.Set(sum) 177 } 178 179 func (c *twistPoint) MakeAffine() { 180 if c.z.IsOne() { 181 return 182 } else if c.z.IsZero() { 183 c.x.SetZero() 184 c.y.SetOne() 185 c.t.SetZero() 186 return 187 } 188 189 zInv := (&gfP2{}).Invert(&c.z) 190 t := (&gfP2{}).Mul(&c.y, zInv) 191 zInv2 := (&gfP2{}).Square(zInv) 192 c.y.Mul(t, zInv2) 193 t.Mul(&c.x, zInv2) 194 c.x.Set(t) 195 c.z.SetOne() 196 c.t.SetOne() 197 } 198 199 func (c *twistPoint) Neg(a *twistPoint) { 200 c.x.Set(&a.x) 201 c.y.Neg(&a.y) 202 c.z.Set(&a.z) 203 c.t.SetZero() 204 }