github.com/amazechain/amc@v0.1.3/common/crypto/bn256/cloudflare/curve.go (about)

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