github.com/cloudflare/circl@v1.5.0/ecc/fourq/curve.go (about)

     1  package fourq
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/cloudflare/circl/internal/conv"
     7  )
     8  
     9  // Size of scalars used for point multiplication.
    10  const Size = 32
    11  
    12  // Point represents an affine point of the curve. The identity is (0,1).
    13  type Point struct{ X, Y Fq }
    14  
    15  // CurveParams contains the parameters of the elliptic curve.
    16  type CurveParams struct {
    17  	Name string   // The canonical name of the curve.
    18  	P    *big.Int // The order of the underlying field Fp.
    19  	N    *big.Int // The order of the generator point.
    20  	G    Point    // This is the generator point.
    21  }
    22  
    23  // Params returns the parameters for the curve.
    24  func Params() *CurveParams {
    25  	params := CurveParams{Name: "FourQ"}
    26  	params.P = conv.Uint64Le2BigInt(prime[:])
    27  	params.N = conv.Uint64Le2BigInt(orderGenerator[:])
    28  	params.G.X = genX
    29  	params.G.Y = genY
    30  	return &params
    31  }
    32  
    33  // IsOnCurve reports whether the given P=(x,y) lies on the curve.
    34  func (P *Point) IsOnCurve() bool {
    35  	var _P pointR1
    36  	P.toR1(&_P)
    37  	return _P.IsOnCurve()
    38  }
    39  
    40  // SetGenerator assigns to P the generator point G.
    41  func (P *Point) SetGenerator() { P.X = genX; P.Y = genY }
    42  
    43  // SetIdentity assigns to P the identity element.
    44  func (P *Point) SetIdentity() {
    45  	var _P pointR1
    46  	_P.SetIdentity()
    47  	P.fromR1(&_P)
    48  }
    49  
    50  // IsIdentity returns true if P is the identity element.
    51  func (P *Point) IsIdentity() bool {
    52  	var _P pointR1
    53  	P.toR1(&_P)
    54  	return _P.IsIdentity()
    55  }
    56  
    57  // Add calculates a point addition P = Q + R.
    58  func (P *Point) Add(Q, R *Point) {
    59  	var _Q, _R pointR1
    60  	var _R2 pointR2
    61  	Q.toR1(&_Q)
    62  	R.toR1(&_R)
    63  	_R2.FromR1(&_R)
    64  	_Q.add(&_R2)
    65  	P.fromR1(&_Q)
    66  }
    67  
    68  // ScalarMult calculates P = k*Q, where Q is an N-torsion point.
    69  func (P *Point) ScalarMult(k *[Size]byte, Q *Point) {
    70  	var _P, _Q pointR1
    71  	Q.toR1(&_Q)
    72  	_Q.ClearCofactor()
    73  	_P.ScalarMult(k, &_Q)
    74  	P.fromR1(&_P)
    75  }
    76  
    77  // ScalarBaseMult calculates P = k*G, where G is the generator point.
    78  func (P *Point) ScalarBaseMult(k *[Size]byte) {
    79  	var _P pointR1
    80  	_P.ScalarBaseMult(k)
    81  	P.fromR1(&_P)
    82  }
    83  
    84  func (P *Point) fromR1(Q *pointR1) {
    85  	Q.ToAffine()
    86  	P.X = Q.X
    87  	P.Y = Q.Y
    88  }
    89  
    90  func (P *Point) toR1(projP *pointR1) {
    91  	projP.X = P.X
    92  	projP.Y = P.Y
    93  	projP.Ta = P.X
    94  	projP.Tb = P.Y
    95  	projP.Z.setOne()
    96  }