github.com/consensys/gnark-crypto@v0.14.0/internal/generator/edwards/template/curve.go.tmpl (about) 1 2 import ( 3 "math/big" 4 "sync" 5 6 "github.com/consensys/gnark-crypto/ecc/{{.Name}}/fr" 7 8 {{- if .HasEndomorphism}} 9 "github.com/consensys/gnark-crypto/ecc" 10 {{- end}} 11 ) 12 13 // CurveParams curve parameters: ax^2 + y^2 = 1 + d*x^2*y^2 14 type CurveParams struct { 15 A, D fr.Element 16 Cofactor fr.Element 17 Order big.Int 18 Base PointAffine 19 20 {{- if .HasEndomorphism}} 21 // endomorphism 22 endo [2]fr.Element 23 lambda big.Int 24 glvBasis ecc.Lattice 25 {{- end}} 26 } 27 28 // GetEdwardsCurve returns the twisted Edwards curve on {{.Name}}/Fr 29 func GetEdwardsCurve() CurveParams { 30 initOnce.Do(initCurveParams) 31 // copy to keep Order private 32 var res CurveParams 33 34 res.A.Set(&curveParams.A) 35 res.D.Set(&curveParams.D) 36 res.Cofactor.Set(&curveParams.Cofactor) 37 res.Order.Set(&curveParams.Order) 38 res.Base.Set(&curveParams.Base) 39 40 {{- if .HasEndomorphism}} 41 res.endo[0].Set(&curveParams.endo[0]) 42 res.endo[1].Set(&curveParams.endo[1]) 43 res.lambda.Set(&curveParams.lambda) 44 res.glvBasis = curveParams.glvBasis // TODO @gbotrel do proper copy of that 45 {{- end}} 46 47 return res 48 } 49 50 51 var ( 52 initOnce sync.Once 53 curveParams CurveParams 54 ) 55 56 57 func initCurveParams() { 58 curveParams.A.SetString("{{.A}}") 59 curveParams.D.SetString("{{.D}}") 60 curveParams.Cofactor.SetString("{{.Cofactor}}") 61 curveParams.Order.SetString("{{.Order}}", 10) 62 63 curveParams.Base.X.SetString("{{.BaseX}}") 64 curveParams.Base.Y.SetString("{{.BaseY}}") 65 66 {{- if .HasEndomorphism}} 67 curveParams.endo[0].SetString("{{.Endo0}}") 68 curveParams.endo[1].SetString("{{.Endo1}}") 69 curveParams.lambda.SetString("{{.Lambda}}", 10) 70 ecc.PrecomputeLattice(&curveParams.Order, &curveParams.lambda, &curveParams.glvBasis) 71 {{- end}} 72 } 73 74 // mulByA multiplies fr.Element by curveParams.A 75 func mulByA(x *fr.Element) { 76 {{- if eq .A "-1"}} 77 x.Neg(x) 78 {{- else if eq .A "-5"}} 79 x.Neg(x) 80 fr.MulBy5(x) 81 {{- else }} 82 x.Mul(x, &curveParams.A) 83 {{- end}} 84 }