// Copyright 2010 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.
// Package elliptic implements the standard NIST P-224, P-256, P-384, and P-521// elliptic curves over prime fields.
package ellipticimport ()// A Curve represents a short-form Weierstrass curve with a=-3.//// The behavior of Add, Double, and ScalarMult when the input is not a point on// the curve is undefined.//// Note that the conventional point at infinity (0, 0) is not considered on the// curve, although it can be returned by Add, Double, ScalarMult, or// ScalarBaseMult (but not the Unmarshal or UnmarshalCompressed functions).typeCurveinterface {// Params returns the parameters for the curve.Params() *CurveParams// IsOnCurve reports whether the given (x,y) lies on the curve.IsOnCurve(x, y *big.Int) bool// Add returns the sum of (x1,y1) and (x2,y2)Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int)// Double returns 2*(x,y)Double(x1, y1 *big.Int) (x, y *big.Int)// ScalarMult returns k*(Bx,By) where k is a number in big-endian form.ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int)// ScalarBaseMult returns k*G, where G is the base point of the group // and k is an integer in big-endian form.ScalarBaseMult(k []byte) (x, y *big.Int)}func ( *CurveParams, ...Curve) (Curve, bool) {for , := range {if == .Params() {return , true } }returnnil, false}// CurveParams contains the parameters of an elliptic curve and also provides// a generic, non-constant time implementation of Curve.typeCurveParamsstruct {P *big.Int// the order of the underlying fieldN *big.Int// the order of the base pointB *big.Int// the constant of the curve equationGx, Gy *big.Int// (x,y) of the base pointBitSizeint// the size of the underlying fieldNamestring// the canonical name of the curve}func ( *CurveParams) () *CurveParams {return}// CurveParams operates, internally, on Jacobian coordinates. For a given// (x, y) position on the curve, the Jacobian coordinates are (x1, y1, z1)// where x = x1/z1² and y = y1/z1³. The greatest speedups come when the whole// calculation can be performed within the transform (as in ScalarMult and// ScalarBaseMult). But even for Add and Double, it's faster to apply and// reverse the transform than to operate in affine coordinates.// polynomial returns x³ - 3x + b.func ( *CurveParams) ( *big.Int) *big.Int { := new(big.Int).Mul(, ) .Mul(, ) := new(big.Int).Lsh(, 1) .Add(, ) .Sub(, ) .Add(, .B) .Mod(, .P)return}func ( *CurveParams) (, *big.Int) bool {// If there is a dedicated constant-time implementation for this curve operation, // use that instead of the generic one.if , := matchesSpecificCurve(, p224, p384, p521); {return .IsOnCurve(, ) }if .Sign() < 0 || .Cmp(.P) >= 0 || .Sign() < 0 || .Cmp(.P) >= 0 {returnfalse }// y² = x³ - 3x + b := new(big.Int).Mul(, ) .Mod(, .P)return .polynomial().Cmp() == 0}// zForAffine returns a Jacobian Z value for the affine point (x, y). If x and// y are zero, it assumes that they represent the point at infinity because (0,// 0) is not on the any of the curves handled here.func (, *big.Int) *big.Int { := new(big.Int)if .Sign() != 0 || .Sign() != 0 { .SetInt64(1) }return}// affineFromJacobian reverses the Jacobian transform. See the comment at the// top of the file. If the point is ∞ it returns 0, 0.func ( *CurveParams) (, , *big.Int) (, *big.Int) {if .Sign() == 0 {returnnew(big.Int), new(big.Int) } := new(big.Int).ModInverse(, .P) := new(big.Int).Mul(, ) = new(big.Int).Mul(, ) .Mod(, .P) .Mul(, ) = new(big.Int).Mul(, ) .Mod(, .P)return}func ( *CurveParams) (, , , *big.Int) (*big.Int, *big.Int) {// If there is a dedicated constant-time implementation for this curve operation, // use that instead of the generic one.if , := matchesSpecificCurve(, p224, p384, p521); {return .Add(, , , ) } := zForAffine(, ) := zForAffine(, )return .affineFromJacobian(.addJacobian(, , , , , ))}// addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and// (x2, y2, z2) and returns their sum, also in Jacobian form.func ( *CurveParams) (, , , , , *big.Int) (*big.Int, *big.Int, *big.Int) {// See https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl , , := new(big.Int), new(big.Int), new(big.Int)if .Sign() == 0 { .Set() .Set() .Set()return , , }if .Sign() == 0 { .Set() .Set() .Set()return , , } := new(big.Int).Mul(, ) .Mod(, .P) := new(big.Int).Mul(, ) .Mod(, .P) := new(big.Int).Mul(, ) .Mod(, .P) := new(big.Int).Mul(, ) .Mod(, .P) := new(big.Int).Sub(, ) := .Sign() == 0if .Sign() == -1 { .Add(, .P) } := new(big.Int).Lsh(, 1) .Mul(, ) := new(big.Int).Mul(, ) := new(big.Int).Mul(, ) .Mul(, ) .Mod(, .P) := new(big.Int).Mul(, ) .Mul(, ) .Mod(, .P) := new(big.Int).Sub(, )if .Sign() == -1 { .Add(, .P) } := .Sign() == 0if && {return .doubleJacobian(, , ) } .Lsh(, 1) := new(big.Int).Mul(, ) .Set() .Mul(, ) .Sub(, ) .Sub(, ) .Sub(, ) .Mod(, .P) .Set() .Sub(, ) .Mul(, ) .Mul(, ) .Lsh(, 1) .Sub(, ) .Mod(, .P) .Add(, ) .Mul(, ) .Sub(, ) .Sub(, ) .Mul(, ) .Mod(, .P)return , , }func ( *CurveParams) (, *big.Int) (*big.Int, *big.Int) {// If there is a dedicated constant-time implementation for this curve operation, // use that instead of the generic one.if , := matchesSpecificCurve(, p224, p384, p521); {return .Double(, ) } := zForAffine(, )return .affineFromJacobian(.doubleJacobian(, , ))}// doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and// returns its double, also in Jacobian form.func ( *CurveParams) (, , *big.Int) (*big.Int, *big.Int, *big.Int) {// See https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b := new(big.Int).Mul(, ) .Mod(, .P) := new(big.Int).Mul(, ) .Mod(, .P) := new(big.Int).Sub(, )if .Sign() == -1 { .Add(, .P) } := new(big.Int).Add(, ) .Mul(, ) .Set() .Lsh(, 1) .Add(, ) := .Mul(, ) := new(big.Int).Mul(, ) := new(big.Int).Lsh(, 3) .Mod(, .P) .Sub(, )if .Sign() == -1 { .Add(, .P) } .Mod(, .P) := new(big.Int).Add(, ) .Mul(, ) .Sub(, )if .Sign() == -1 { .Add(, .P) } .Sub(, )if .Sign() == -1 { .Add(, .P) } .Mod(, .P) .Lsh(, 2) .Sub(, )if .Sign() == -1 { .Add(, .P) } := .Mul(, ) .Mul(, ) .Lsh(, 3) .Mod(, .P) .Sub(, )if .Sign() == -1 { .Add(, .P) } .Mod(, .P)return , , }func ( *CurveParams) (, *big.Int, []byte) (*big.Int, *big.Int) {// If there is a dedicated constant-time implementation for this curve operation, // use that instead of the generic one.if , := matchesSpecificCurve(, p224, p256, p384, p521); {return .ScalarMult(, , ) } := new(big.Int).SetInt64(1) , , := new(big.Int), new(big.Int), new(big.Int)for , := range {for := 0; < 8; ++ { , , = .doubleJacobian(, , )if &0x80 == 0x80 { , , = .addJacobian(, , , , , ) } <<= 1 } }return .affineFromJacobian(, , )}func ( *CurveParams) ( []byte) (*big.Int, *big.Int) {// If there is a dedicated constant-time implementation for this curve operation, // use that instead of the generic one.if , := matchesSpecificCurve(, p224, p256, p384, p521); {return .ScalarBaseMult() }return .ScalarMult(.Gx, .Gy, )}varmask = []byte{0xff, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f}// GenerateKey returns a public/private key pair. The private key is// generated using the given reader, which must return random data.func ( Curve, io.Reader) ( []byte, , *big.Int, error) { := .Params().N := .BitLen() := ( + 7) / 8 = make([]byte, )for == nil { _, = io.ReadFull(, )if != nil {return }// We have to mask off any excess bits in the case that the size of the // underlying field is not a whole number of bytes. [0] &= mask[%8]// This is because, in tests, rand will return all zeros and we don't // want to get the point at infinity and loop forever. [1] ^= 0x42// If the scalar is out of range, sample another random number.ifnew(big.Int).SetBytes().Cmp() >= 0 {continue } , = .ScalarBaseMult() }return}// Marshal converts a point on the curve into the uncompressed form specified in// SEC 1, Version 2.0, Section 2.3.3. If the point is not on the curve (or is// the conventional point at infinity), the behavior is undefined.func ( Curve, , *big.Int) []byte { := (.Params().BitSize + 7) / 8 := make([]byte, 1+2*) [0] = 4// uncompressed point .FillBytes([1 : 1+]) .FillBytes([1+ : 1+2*])return}// MarshalCompressed converts a point on the curve into the compressed form// specified in SEC 1, Version 2.0, Section 2.3.3. If the point is not on the// curve (or is the conventional point at infinity), the behavior is undefined.func ( Curve, , *big.Int) []byte { := (.Params().BitSize + 7) / 8 := make([]byte, 1+) [0] = byte(.Bit(0)) | 2 .FillBytes([1:])return}// Unmarshal converts a point, serialized by Marshal, into an x, y pair. It is// an error if the point is not in uncompressed form, is not on the curve, or is// the point at infinity. On error, x = nil.func ( Curve, []byte) (, *big.Int) { := (.Params().BitSize + 7) / 8iflen() != 1+2* {returnnil, nil }if [0] != 4 { // uncompressed formreturnnil, nil } := .Params().P = new(big.Int).SetBytes([1 : 1+]) = new(big.Int).SetBytes([1+:])if .Cmp() >= 0 || .Cmp() >= 0 {returnnil, nil }if !.IsOnCurve(, ) {returnnil, nil }return}// UnmarshalCompressed converts a point, serialized by MarshalCompressed, into// an x, y pair. It is an error if the point is not in compressed form, is not// on the curve, or is the point at infinity. On error, x = nil.func ( Curve, []byte) (, *big.Int) { := (.Params().BitSize + 7) / 8iflen() != 1+ {returnnil, nil }if [0] != 2 && [0] != 3 { // compressed formreturnnil, nil } := .Params().P = new(big.Int).SetBytes([1:])if .Cmp() >= 0 {returnnil, nil }// y² = x³ - 3x + b = .Params().polynomial() = .ModSqrt(, )if == nil {returnnil, nil }ifbyte(.Bit(0)) != [0]&1 { .Neg().Mod(, ) }if !.IsOnCurve(, ) {returnnil, nil }return}varinitoncesync.Oncefunc () {initP224()initP256()initP384()initP521()}// P224 returns a Curve which implements NIST P-224 (FIPS 186-3, section D.2.2),// also known as secp224r1. The CurveParams.Name of this Curve is "P-224".//// Multiple invocations of this function will return the same value, so it can// be used for equality checks and switch statements.//// The cryptographic operations are implemented using constant-time algorithms.func () Curve {initonce.Do(initAll)returnp224}// P256 returns a Curve which implements NIST P-256 (FIPS 186-3, section D.2.3),// also known as secp256r1 or prime256v1. The CurveParams.Name of this Curve is// "P-256".//// Multiple invocations of this function will return the same value, so it can// be used for equality checks and switch statements.//// ScalarMult and ScalarBaseMult are implemented using constant-time algorithms.func () Curve {initonce.Do(initAll)returnp256}// P384 returns a Curve which implements NIST P-384 (FIPS 186-3, section D.2.4),// also known as secp384r1. The CurveParams.Name of this Curve is "P-384".//// Multiple invocations of this function will return the same value, so it can// be used for equality checks and switch statements.//// The cryptographic operations are implemented using constant-time algorithms.func () Curve {initonce.Do(initAll)returnp384}// P521 returns a Curve which implements NIST P-521 (FIPS 186-3, section D.2.5),// also known as secp521r1. The CurveParams.Name of this Curve is "P-521".//// Multiple invocations of this function will return the same value, so it can// be used for equality checks and switch statements.//// The cryptographic operations are implemented using constant-time algorithms.func () Curve {initonce.Do(initAll)returnp521}
The pages are generated with Goldsv0.4.9. (GOOS=linux GOARCH=amd64)