github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/crypto/bn256/cloudflare/curve.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package bn256
    19  
    20  import (
    21  	"math/big"
    22  )
    23  
    24  // curvePoint implements the elliptic curve y²=x³+3. Points are kept in Jacobian
    25  // form and t=z² when valid. G₁ is the set of points of this curve on GF(p).
    26  type curvePoint struct {
    27  	x, y, z, t gfP
    28  }
    29  
    30  var curveB = newGFp(3)
    31  
    32  // curveGen is the generator of G₁.
    33  var curveGen = &curvePoint{
    34  	x: *newGFp(1),
    35  	y: *newGFp(2),
    36  	z: *newGFp(1),
    37  	t: *newGFp(1),
    38  }
    39  
    40  func (c *curvePoint) String() string {
    41  	c.MakeAffine()
    42  	x, y := &gfP{}, &gfP{}
    43  	montDecode(x, &c.x)
    44  	montDecode(y, &c.y)
    45  	return "(" + x.String() + ", " + y.String() + ")"
    46  }
    47  
    48  func (c *curvePoint) Set(a *curvePoint) {
    49  	c.x.Set(&a.x)
    50  	c.y.Set(&a.y)
    51  	c.z.Set(&a.z)
    52  	c.t.Set(&a.t)
    53  }
    54  
    55  // IsOnCurve returns true iff c is on the curve.
    56  func (c *curvePoint) IsOnCurve() bool {
    57  	c.MakeAffine()
    58  	if c.IsInfinity() {
    59  		return true
    60  	}
    61  
    62  	y2, x3 := &gfP{}, &gfP{}
    63  	gfpMul(y2, &c.y, &c.y)
    64  	gfpMul(x3, &c.x, &c.x)
    65  	gfpMul(x3, x3, &c.x)
    66  	gfpAdd(x3, x3, curveB)
    67  
    68  	return *y2 == *x3
    69  }
    70  
    71  func (c *curvePoint) SetInfinity() {
    72  	c.x = gfP{0}
    73  	c.y = *newGFp(1)
    74  	c.z = gfP{0}
    75  	c.t = gfP{0}
    76  }
    77  
    78  func (c *curvePoint) IsInfinity() bool {
    79  	return c.z == gfP{0}
    80  }
    81  
    82  func (c *curvePoint) Add(a, b *curvePoint) {
    83  	if a.IsInfinity() {
    84  		c.Set(b)
    85  		return
    86  	}
    87  	if b.IsInfinity() {
    88  		c.Set(a)
    89  		return
    90  	}
    91  
    92  	// See http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/addition/add-2007-bl.op3
    93  
    94  	// Normalize the points by replacing a = [x1:y1:z1] and b = [x2:y2:z2]
    95  	// by [u1:s1:z1·z2] and [u2:s2:z1·z2]
    96  	// where u1 = x1·z2², s1 = y1·z2³ and u1 = x2·z1², s2 = y2·z1³
    97  	z12, z22 := &gfP{}, &gfP{}
    98  	gfpMul(z12, &a.z, &a.z)
    99  	gfpMul(z22, &b.z, &b.z)
   100  
   101  	u1, u2 := &gfP{}, &gfP{}
   102  	gfpMul(u1, &a.x, z22)
   103  	gfpMul(u2, &b.x, z12)
   104  
   105  	t, s1 := &gfP{}, &gfP{}
   106  	gfpMul(t, &b.z, z22)
   107  	gfpMul(s1, &a.y, t)
   108  
   109  	s2 := &gfP{}
   110  	gfpMul(t, &a.z, z12)
   111  	gfpMul(s2, &b.y, t)
   112  
   113  	// Compute x = (2h)²(s²-u1-u2)
   114  	// where s = (s2-s1)/(u2-u1) is the slope of the line through
   115  	// (u1,s1) and (u2,s2). The extra factor 2h = 2(u2-u1) comes from the value of z below.
   116  	// This is also:
   117  	// 4(s2-s1)² - 4h²(u1+u2) = 4(s2-s1)² - 4h³ - 4h²(2u1)
   118  	//                        = r² - j - 2v
   119  	// with the notations below.
   120  	h := &gfP{}
   121  	gfpSub(h, u2, u1)
   122  	xEqual := *h == gfP{0}
   123  
   124  	gfpAdd(t, h, h)
   125  	// i = 4h²
   126  	i := &gfP{}
   127  	gfpMul(i, t, t)
   128  	// j = 4h³
   129  	j := &gfP{}
   130  	gfpMul(j, h, i)
   131  
   132  	gfpSub(t, s2, s1)
   133  	yEqual := *t == gfP{0}
   134  	if xEqual && yEqual {
   135  		c.Double(a)
   136  		return
   137  	}
   138  	r := &gfP{}
   139  	gfpAdd(r, t, t)
   140  
   141  	v := &gfP{}
   142  	gfpMul(v, u1, i)
   143  
   144  	// t4 = 4(s2-s1)²
   145  	t4, t6 := &gfP{}, &gfP{}
   146  	gfpMul(t4, r, r)
   147  	gfpAdd(t, v, v)
   148  	gfpSub(t6, t4, j)
   149  
   150  	gfpSub(&c.x, t6, t)
   151  
   152  	// Set y = -(2h)³(s1 + s*(x/4h²-u1))
   153  	// This is also
   154  	// y = - 2·s1·j - (s2-s1)(2x - 2i·u1) = r(v-x) - 2·s1·j
   155  	gfpSub(t, v, &c.x) // t7
   156  	gfpMul(t4, s1, j)  // t8
   157  	gfpAdd(t6, t4, t4) // t9
   158  	gfpMul(t4, r, t)   // t10
   159  	gfpSub(&c.y, t4, t6)
   160  
   161  	// Set z = 2(u2-u1)·z1·z2 = 2h·z1·z2
   162  	gfpAdd(t, &a.z, &b.z) // t11
   163  	gfpMul(t4, t, t)      // t12
   164  	gfpSub(t, t4, z12)    // t13
   165  	gfpSub(t4, t, z22)    // t14
   166  	gfpMul(&c.z, t4, h)
   167  }
   168  
   169  func (c *curvePoint) Double(a *curvePoint) {
   170  	// See http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/doubling/dbl-2009-l.op3
   171  	A, B, C := &gfP{}, &gfP{}, &gfP{}
   172  	gfpMul(A, &a.x, &a.x)
   173  	gfpMul(B, &a.y, &a.y)
   174  	gfpMul(C, B, B)
   175  
   176  	t, t2 := &gfP{}, &gfP{}
   177  	gfpAdd(t, &a.x, B)
   178  	gfpMul(t2, t, t)
   179  	gfpSub(t, t2, A)
   180  	gfpSub(t2, t, C)
   181  
   182  	d, e, f := &gfP{}, &gfP{}, &gfP{}
   183  	gfpAdd(d, t2, t2)
   184  	gfpAdd(t, A, A)
   185  	gfpAdd(e, t, A)
   186  	gfpMul(f, e, e)
   187  
   188  	gfpAdd(t, d, d)
   189  	gfpSub(&c.x, f, t)
   190  
   191  	gfpAdd(t, C, C)
   192  	gfpAdd(t2, t, t)
   193  	gfpAdd(t, t2, t2)
   194  	gfpSub(&c.y, d, &c.x)
   195  	gfpMul(t2, e, &c.y)
   196  	gfpSub(&c.y, t2, t)
   197  
   198  	gfpMul(t, &a.y, &a.z)
   199  	gfpAdd(&c.z, t, t)
   200  }
   201  
   202  func (c *curvePoint) Mul(a *curvePoint, scalar *big.Int) {
   203  	precomp := [1 << 2]*curvePoint{nil, {}, {}, {}}
   204  	precomp[1].Set(a)
   205  	precomp[2].Set(a)
   206  	gfpMul(&precomp[2].x, &precomp[2].x, xiTo2PSquaredMinus2Over3)
   207  	precomp[3].Add(precomp[1], precomp[2])
   208  
   209  	multiScalar := curveLattice.Multi(scalar)
   210  
   211  	sum := &curvePoint{}
   212  	sum.SetInfinity()
   213  	t := &curvePoint{}
   214  
   215  	for i := len(multiScalar) - 1; i >= 0; i-- {
   216  		t.Double(sum)
   217  		if multiScalar[i] == 0 {
   218  			sum.Set(t)
   219  		} else {
   220  			sum.Add(t, precomp[multiScalar[i]])
   221  		}
   222  	}
   223  	c.Set(sum)
   224  }
   225  
   226  func (c *curvePoint) MakeAffine() {
   227  	if c.z == *newGFp(1) {
   228  		return
   229  	} else if c.z == *newGFp(0) {
   230  		c.x = gfP{0}
   231  		c.y = *newGFp(1)
   232  		c.t = gfP{0}
   233  		return
   234  	}
   235  
   236  	zInv := &gfP{}
   237  	zInv.Invert(&c.z)
   238  
   239  	t, zInv2 := &gfP{}, &gfP{}
   240  	gfpMul(t, &c.y, zInv)
   241  	gfpMul(zInv2, zInv, zInv)
   242  
   243  	gfpMul(&c.x, &c.x, zInv2)
   244  	gfpMul(&c.y, t, zInv2)
   245  
   246  	c.z = *newGFp(1)
   247  	c.t = *newGFp(1)
   248  }
   249  
   250  func (c *curvePoint) Neg(a *curvePoint) {
   251  	c.x.Set(&a.x)
   252  	gfpNeg(&c.y, &a.y)
   253  	c.z.Set(&a.z)
   254  	c.t = gfP{0}
   255  }