1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 // twistPoint implements the elliptic curve y²=x³+3/ξ over GF(p²). Points are
12 // kept in Jacobian form and t=z² when valid. The group G₂ is the set of
13 // n-torsion points of this curve over GF(p²) (where n = Order)
14 type twistPoint struct {
19 bigFromBase10("6500054969564660373279643874235990574282535810762300357187714502686418407178"),
20 bigFromBase10("45500384786952622612957507119651934019977750675336102500314001518804928850249"),
23 // twistGen is the generator of group G₂.
24 var twistGen = &twistPoint{
26 bigFromBase10("21167961636542580255011770066570541300993051739349375019639421053990175267184"),
27 bigFromBase10("64746500191241794695844075326670126197795977525365406531717464316923369116492"),
30 bigFromBase10("20666913350058776956210519119118544732556678129809273996262322366050359951122"),
31 bigFromBase10("17778617556404439934652658462602675281523610326338642107814333856843981424549"),
43 func newTwistPoint(pool *bnPool) *twistPoint {
52 func (c *twistPoint) String() string {
53 return "(" + c.x.String() + ", " + c.y.String() + ", " + c.z.String() + ")"
56 func (c *twistPoint) Put(pool *bnPool) {
63 func (c *twistPoint) Set(a *twistPoint) {
70 // IsOnCurve returns true iff c is on the curve where c must be in affine form.
71 func (c *twistPoint) IsOnCurve() bool {
73 yy := newGFp2(pool).Square(c.y, pool)
74 xxx := newGFp2(pool).Square(c.x, pool)
75 xxx.Mul(xxx, c.x, pool)
79 return yy.x.Sign() == 0 && yy.y.Sign() == 0
82 func (c *twistPoint) SetInfinity() {
86 func (c *twistPoint) IsInfinity() bool {
90 func (c *twistPoint) Add(a, b *twistPoint, pool *bnPool) {
91 // For additional comments, see the same function in curve.go.
102 // See http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/addition/add-2007-bl.op3
103 z1z1 := newGFp2(pool).Square(a.z, pool)
104 z2z2 := newGFp2(pool).Square(b.z, pool)
105 u1 := newGFp2(pool).Mul(a.x, z2z2, pool)
106 u2 := newGFp2(pool).Mul(b.x, z1z1, pool)
108 t := newGFp2(pool).Mul(b.z, z2z2, pool)
109 s1 := newGFp2(pool).Mul(a.y, t, pool)
111 t.Mul(a.z, z1z1, pool)
112 s2 := newGFp2(pool).Mul(b.y, t, pool)
114 h := newGFp2(pool).Sub(u2, u1)
118 i := newGFp2(pool).Square(t, pool)
119 j := newGFp2(pool).Mul(h, i, pool)
123 if xEqual && yEqual {
127 r := newGFp2(pool).Add(t, t)
129 v := newGFp2(pool).Mul(u1, i, pool)
131 t4 := newGFp2(pool).Square(r, pool)
133 t6 := newGFp2(pool).Sub(t4, j)
137 t4.Mul(s1, j, pool) // t8
139 t4.Mul(r, t, pool) // t10
142 t.Add(a.z, b.z) // t11
143 t4.Square(t, pool) // t12
144 t.Sub(t4, z1z1) // t13
145 t4.Sub(t, z2z2) // t14
164 func (c *twistPoint) Double(a *twistPoint, pool *bnPool) {
165 // See http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/doubling/dbl-2009-l.op3
166 A := newGFp2(pool).Square(a.x, pool)
167 B := newGFp2(pool).Square(a.y, pool)
168 C := newGFp2(pool).Square(B, pool)
170 t := newGFp2(pool).Add(a.x, B)
171 t2 := newGFp2(pool).Square(t, pool)
174 d := newGFp2(pool).Add(t2, t2)
176 e := newGFp2(pool).Add(t, A)
177 f := newGFp2(pool).Square(e, pool)
189 t.Mul(a.y, a.z, pool)
202 func (c *twistPoint) Mul(a *twistPoint, scalar *big.Int, pool *bnPool) *twistPoint {
203 sum := newTwistPoint(pool)
205 t := newTwistPoint(pool)
207 for i := scalar.BitLen(); i >= 0; i-- {
209 if scalar.Bit(i) != 0 {
222 func (c *twistPoint) MakeAffine(pool *bnPool) *twistPoint {
227 zInv := newGFp2(pool).Invert(c.z, pool)
228 t := newGFp2(pool).Mul(c.y, zInv, pool)
229 zInv2 := newGFp2(pool).Square(zInv, pool)
230 c.y.Mul(t, zInv2, pool)
231 t.Mul(c.x, zInv2, pool)
243 func (c *twistPoint) Negative(a *twistPoint, pool *bnPool) {