github.com/consensys/gnark-crypto@v0.14.0/internal/generator/config/curve.go (about)

     1  package config
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/consensys/gnark-crypto/field/generator/config"
     7  )
     8  
     9  // Curve describes parameters of the curve useful for the template
    10  type Curve struct {
    11  	Name         string
    12  	CurvePackage string
    13  	Package      string // current package being generated
    14  	EnumID       string
    15  	FpModulus    string
    16  	FrModulus    string
    17  
    18  	Fp           *config.FieldConfig
    19  	Fr           *config.FieldConfig
    20  	FpUnusedBits int
    21  
    22  	FpInfo, FrInfo Field
    23  	G1             Point
    24  	G2             Point
    25  
    26  	HashE1 HashSuite
    27  	HashE2 HashSuite
    28  }
    29  
    30  type TwistedEdwardsCurve struct {
    31  	Name    string
    32  	Package string
    33  	EnumID  string
    34  
    35  	A, D, Cofactor, Order, BaseX, BaseY string
    36  
    37  	// set if endomorphism
    38  	HasEndomorphism bool
    39  	Endo0, Endo1    string
    40  	Lambda          string
    41  }
    42  
    43  type Field struct {
    44  	Bits    int
    45  	Bytes   int
    46  	Modulus func() *big.Int
    47  }
    48  
    49  func (c Curve) Equal(other Curve) bool {
    50  	return c.Name == other.Name
    51  }
    52  
    53  type Point struct {
    54  	CoordType        string
    55  	CoordExtDegree   uint8 // value n, such that q = pⁿ
    56  	CoordExtRoot     int64 // value a, such that the field is Fp[X]/(Xⁿ - a)
    57  	PointName        string
    58  	GLV              bool     // scalar multiplication using GLV
    59  	CofactorCleaning bool     // flag telling if the Cofactor cleaning is available
    60  	CRange           []int    // multiexp bucket method: generate inner methods (with const arrays) for each c
    61  	Projective       bool     // generate projective coordinates
    62  	A                []string //A linear coefficient in Weierstrass form
    63  	B                []string //B constant term in Weierstrass form
    64  }
    65  
    66  var Curves []Curve
    67  var TwistedEdwardsCurves []TwistedEdwardsCurve
    68  
    69  func defaultCRange() []int {
    70  	// default range for C values in the multiExp
    71  	return []int{4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
    72  }
    73  
    74  func addCurve(c *Curve) {
    75  	// init FpInfo and FrInfo
    76  	c.FpInfo = newFieldInfo(c.FpModulus)
    77  	c.FrInfo = newFieldInfo(c.FrModulus)
    78  	Curves = append(Curves, *c)
    79  }
    80  
    81  func addTwistedEdwardCurve(c *TwistedEdwardsCurve) {
    82  	TwistedEdwardsCurves = append(TwistedEdwardsCurves, *c)
    83  }
    84  
    85  func newFieldInfo(modulus string) Field {
    86  	var F Field
    87  	var bModulus big.Int
    88  	if _, ok := bModulus.SetString(modulus, 10); !ok {
    89  		panic("invalid modulus " + modulus)
    90  	}
    91  
    92  	F.Bits = bModulus.BitLen()
    93  	F.Bytes = (F.Bits + 7) / 8
    94  	F.Modulus = func() *big.Int { return new(big.Int).Set(&bModulus) }
    95  	return F
    96  }
    97  
    98  type FieldDependency struct {
    99  	FieldPackagePath string
   100  	ElementType      string
   101  	FieldPackageName string
   102  }