gitee.com/lh-her-team/common@v1.5.1/crypto/hibe/hibe_amd64/hibe/bn256/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 y2, x3 := &gfP2{}, &gfP2{} 53 y2.Square(&c.y) 54 x3.Square(&c.x).Mul(x3, &c.x).Add(x3, twistB) 55 if *y2 != *x3 { 56 return false 57 } 58 cneg := &twistPoint{} 59 cneg.Mul(c, Order) 60 return cneg.z.IsZero() 61 } 62 63 func (c *twistPoint) SetInfinity() { 64 c.x.SetZero() 65 c.y.SetOne() 66 c.z.SetZero() 67 c.t.SetZero() 68 } 69 70 func (c *twistPoint) IsInfinity() bool { 71 return c.z.IsZero() 72 } 73 74 func (c *twistPoint) Add(a, b *twistPoint) { 75 // For additional comments, see the same function in curve.go. 76 if a.IsInfinity() { 77 c.Set(b) 78 return 79 } 80 if b.IsInfinity() { 81 c.Set(a) 82 return 83 } 84 // See http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/addition/add-2007-bl.op3 85 z12 := (&gfP2{}).Square(&a.z) 86 z22 := (&gfP2{}).Square(&b.z) 87 u1 := (&gfP2{}).Mul(&a.x, z22) 88 u2 := (&gfP2{}).Mul(&b.x, z12) 89 t := (&gfP2{}).Mul(&b.z, z22) 90 s1 := (&gfP2{}).Mul(&a.y, t) 91 t.Mul(&a.z, z12) 92 s2 := (&gfP2{}).Mul(&b.y, t) 93 h := (&gfP2{}).Sub(u2, u1) 94 xEqual := h.IsZero() 95 t.Add(h, h) 96 i := (&gfP2{}).Square(t) 97 j := (&gfP2{}).Mul(h, i) 98 t.Sub(s2, s1) 99 yEqual := t.IsZero() 100 if xEqual && yEqual { 101 c.Double(a) 102 return 103 } 104 r := (&gfP2{}).Add(t, t) 105 v := (&gfP2{}).Mul(u1, i) 106 t4 := (&gfP2{}).Square(r) 107 t.Add(v, v) 108 t6 := (&gfP2{}).Sub(t4, j) 109 c.x.Sub(t6, t) 110 t.Sub(v, &c.x) // t7 111 t4.Mul(s1, j) // t8 112 t6.Add(t4, t4) // t9 113 t4.Mul(r, t) // t10 114 c.y.Sub(t4, t6) 115 t.Add(&a.z, &b.z) // t11 116 t4.Square(t) // t12 117 t.Sub(t4, z12) // t13 118 t4.Sub(t, z22) // t14 119 c.z.Mul(t4, h) 120 } 121 122 func (c *twistPoint) Double(a *twistPoint) { 123 // See http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/doubling/dbl-2009-l.op3 124 A := (&gfP2{}).Square(&a.x) 125 B := (&gfP2{}).Square(&a.y) 126 C := (&gfP2{}).Square(B) 127 t := (&gfP2{}).Add(&a.x, B) 128 t2 := (&gfP2{}).Square(t) 129 t.Sub(t2, A) 130 t2.Sub(t, C) 131 d := (&gfP2{}).Add(t2, t2) 132 t.Add(A, A) 133 e := (&gfP2{}).Add(t, A) 134 f := (&gfP2{}).Square(e) 135 t.Add(d, d) 136 c.x.Sub(f, t) 137 t.Add(C, C) 138 t2.Add(t, t) 139 t.Add(t2, t2) 140 c.y.Sub(d, &c.x) 141 t2.Mul(e, &c.y) 142 c.y.Sub(t2, t) 143 t.Mul(&a.y, &a.z) 144 c.z.Add(t, t) 145 } 146 147 func (c *twistPoint) Mul(a *twistPoint, scalar *big.Int) { 148 sum, t := &twistPoint{}, &twistPoint{} 149 for i := scalar.BitLen(); i >= 0; i-- { 150 t.Double(sum) 151 if scalar.Bit(i) != 0 { 152 sum.Add(t, a) 153 } else { 154 sum.Set(t) 155 } 156 } 157 c.Set(sum) 158 } 159 160 func (c *twistPoint) MakeAffine() { 161 if c.z.IsOne() { 162 return 163 } else if c.z.IsZero() { 164 c.x.SetZero() 165 c.y.SetOne() 166 c.t.SetZero() 167 return 168 } 169 zInv := (&gfP2{}).Invert(&c.z) 170 t := (&gfP2{}).Mul(&c.y, zInv) 171 zInv2 := (&gfP2{}).Square(zInv) 172 c.y.Mul(t, zInv2) 173 t.Mul(&c.x, zInv2) 174 c.x.Set(t) 175 c.z.SetOne() 176 c.t.SetOne() 177 } 178 179 func (c *twistPoint) Neg(a *twistPoint) { 180 c.x.Set(&a.x) 181 c.y.Neg(&a.y) 182 c.z.Set(&a.z) 183 c.t.SetZero() 184 }