1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
9 // CurveParams contains the parameters of an elliptic curve and also provides
10 // a generic, non-constant time implementation of Curve.
12 // Note: Custom curves (those not returned by P224(), P256(), P384(), and P521())
13 // are not guaranteed to provide any security property.
14 type CurveParams struct {
15 P *big.Int // the order of the underlying field
16 N *big.Int // the order of the base point
17 B *big.Int // the constant of the curve equation
18 Gx, Gy *big.Int // (x,y) of the base point
19 BitSize int // the size of the underlying field
20 Name string // the canonical name of the curve
23 func (curve *CurveParams) Params() *CurveParams {
27 // CurveParams operates, internally, on Jacobian coordinates. For a given
28 // (x, y) position on the curve, the Jacobian coordinates are (x1, y1, z1)
29 // where x = x1/z1² and y = y1/z1³. The greatest speedups come when the whole
30 // calculation can be performed within the transform (as in ScalarMult and
31 // ScalarBaseMult). But even for Add and Double, it's faster to apply and
32 // reverse the transform than to operate in affine coordinates.
34 // polynomial returns x³ - 3x + b.
35 func (curve *CurveParams) polynomial(x *big.Int) *big.Int {
36 x3 := new(big.Int).Mul(x, x)
39 threeX := new(big.Int).Lsh(x, 1)
49 // IsOnCurve implements Curve.IsOnCurve.
51 // Note: the CurveParams methods are not guaranteed to
52 // provide any security property. For ECDH, use the crypto/ecdh package.
53 // For ECDSA, use the crypto/ecdsa package with a Curve value returned directly
54 // from P224(), P256(), P384(), or P521().
55 func (curve *CurveParams) IsOnCurve(x, y *big.Int) bool {
56 // If there is a dedicated constant-time implementation for this curve operation,
57 // use that instead of the generic one.
58 if specific, ok := matchesSpecificCurve(curve); ok {
59 return specific.IsOnCurve(x, y)
62 if x.Sign() < 0 || x.Cmp(curve.P) >= 0 ||
63 y.Sign() < 0 || y.Cmp(curve.P) >= 0 {
68 y2 := new(big.Int).Mul(y, y)
71 return curve.polynomial(x).Cmp(y2) == 0
74 // zForAffine returns a Jacobian Z value for the affine point (x, y). If x and
75 // y are zero, it assumes that they represent the point at infinity because (0,
76 // 0) is not on the any of the curves handled here.
77 func zForAffine(x, y *big.Int) *big.Int {
79 if x.Sign() != 0 || y.Sign() != 0 {
85 // affineFromJacobian reverses the Jacobian transform. See the comment at the
86 // top of the file. If the point is ∞ it returns 0, 0.
87 func (curve *CurveParams) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {
89 return new(big.Int), new(big.Int)
92 zinv := new(big.Int).ModInverse(z, curve.P)
93 zinvsq := new(big.Int).Mul(zinv, zinv)
95 xOut = new(big.Int).Mul(x, zinvsq)
96 xOut.Mod(xOut, curve.P)
97 zinvsq.Mul(zinvsq, zinv)
98 yOut = new(big.Int).Mul(y, zinvsq)
99 yOut.Mod(yOut, curve.P)
103 // Add implements Curve.Add.
105 // Note: the CurveParams methods are not guaranteed to
106 // provide any security property. For ECDH, use the crypto/ecdh package.
107 // For ECDSA, use the crypto/ecdsa package with a Curve value returned directly
108 // from P224(), P256(), P384(), or P521().
109 func (curve *CurveParams) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
110 // If there is a dedicated constant-time implementation for this curve operation,
111 // use that instead of the generic one.
112 if specific, ok := matchesSpecificCurve(curve); ok {
113 return specific.Add(x1, y1, x2, y2)
115 panicIfNotOnCurve(curve, x1, y1)
116 panicIfNotOnCurve(curve, x2, y2)
118 z1 := zForAffine(x1, y1)
119 z2 := zForAffine(x2, y2)
120 return curve.affineFromJacobian(curve.addJacobian(x1, y1, z1, x2, y2, z2))
123 // addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and
124 // (x2, y2, z2) and returns their sum, also in Jacobian form.
125 func (curve *CurveParams) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) {
126 // See https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl
127 x3, y3, z3 := new(big.Int), new(big.Int), new(big.Int)
141 z1z1 := new(big.Int).Mul(z1, z1)
142 z1z1.Mod(z1z1, curve.P)
143 z2z2 := new(big.Int).Mul(z2, z2)
144 z2z2.Mod(z2z2, curve.P)
146 u1 := new(big.Int).Mul(x1, z2z2)
148 u2 := new(big.Int).Mul(x2, z1z1)
150 h := new(big.Int).Sub(u2, u1)
151 xEqual := h.Sign() == 0
155 i := new(big.Int).Lsh(h, 1)
157 j := new(big.Int).Mul(h, i)
159 s1 := new(big.Int).Mul(y1, z2)
162 s2 := new(big.Int).Mul(y2, z1)
165 r := new(big.Int).Sub(s2, s1)
169 yEqual := r.Sign() == 0
170 if xEqual && yEqual {
171 return curve.doubleJacobian(x1, y1, z1)
174 v := new(big.Int).Mul(u1, i)
201 // Double implements Curve.Double.
203 // Note: the CurveParams methods are not guaranteed to
204 // provide any security property. For ECDH, use the crypto/ecdh package.
205 // For ECDSA, use the crypto/ecdsa package with a Curve value returned directly
206 // from P224(), P256(), P384(), or P521().
207 func (curve *CurveParams) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
208 // If there is a dedicated constant-time implementation for this curve operation,
209 // use that instead of the generic one.
210 if specific, ok := matchesSpecificCurve(curve); ok {
211 return specific.Double(x1, y1)
213 panicIfNotOnCurve(curve, x1, y1)
215 z1 := zForAffine(x1, y1)
216 return curve.affineFromJacobian(curve.doubleJacobian(x1, y1, z1))
219 // doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and
220 // returns its double, also in Jacobian form.
221 func (curve *CurveParams) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) {
222 // See https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
223 delta := new(big.Int).Mul(z, z)
224 delta.Mod(delta, curve.P)
225 gamma := new(big.Int).Mul(y, y)
226 gamma.Mod(gamma, curve.P)
227 alpha := new(big.Int).Sub(x, delta)
228 if alpha.Sign() == -1 {
229 alpha.Add(alpha, curve.P)
231 alpha2 := new(big.Int).Add(x, delta)
232 alpha.Mul(alpha, alpha2)
235 alpha.Add(alpha, alpha2)
237 beta := alpha2.Mul(x, gamma)
239 x3 := new(big.Int).Mul(alpha, alpha)
240 beta8 := new(big.Int).Lsh(beta, 3)
241 beta8.Mod(beta8, curve.P)
248 z3 := new(big.Int).Add(y, z)
262 if beta.Sign() == -1 {
263 beta.Add(beta, curve.P)
265 y3 := alpha.Mul(alpha, beta)
267 gamma.Mul(gamma, gamma)
269 gamma.Mod(gamma, curve.P)
280 // ScalarMult implements Curve.ScalarMult.
282 // Note: the CurveParams methods are not guaranteed to
283 // provide any security property. For ECDH, use the crypto/ecdh package.
284 // For ECDSA, use the crypto/ecdsa package with a Curve value returned directly
285 // from P224(), P256(), P384(), or P521().
286 func (curve *CurveParams) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int) {
287 // If there is a dedicated constant-time implementation for this curve operation,
288 // use that instead of the generic one.
289 if specific, ok := matchesSpecificCurve(curve); ok {
290 return specific.ScalarMult(Bx, By, k)
292 panicIfNotOnCurve(curve, Bx, By)
294 Bz := new(big.Int).SetInt64(1)
295 x, y, z := new(big.Int), new(big.Int), new(big.Int)
297 for _, byte := range k {
298 for bitNum := 0; bitNum < 8; bitNum++ {
299 x, y, z = curve.doubleJacobian(x, y, z)
300 if byte&0x80 == 0x80 {
301 x, y, z = curve.addJacobian(Bx, By, Bz, x, y, z)
307 return curve.affineFromJacobian(x, y, z)
310 // ScalarBaseMult implements Curve.ScalarBaseMult.
312 // Note: the CurveParams methods are not guaranteed to
313 // provide any security property. For ECDH, use the crypto/ecdh package.
314 // For ECDSA, use the crypto/ecdsa package with a Curve value returned directly
315 // from P224(), P256(), P384(), or P521().
316 func (curve *CurveParams) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
317 // If there is a dedicated constant-time implementation for this curve operation,
318 // use that instead of the generic one.
319 if specific, ok := matchesSpecificCurve(curve); ok {
320 return specific.ScalarBaseMult(k)
323 return curve.ScalarMult(curve.Gx, curve.Gy, k)
326 func matchesSpecificCurve(params *CurveParams) (Curve, bool) {
327 for _, c := range []Curve{p224, p256, p384, p521} {
328 if params == c.Params() {