381 lines
12 KiB
Go
381 lines
12 KiB
Go
// 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 several standard elliptic curves over prime
|
|
// fields.
|
|
package elliptic
|
|
|
|
// This package 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.
|
|
|
|
import (
|
|
"io"
|
|
"math/big"
|
|
"sync"
|
|
)
|
|
|
|
// A Curve represents a short-form Weierstrass curve with a=-3.
|
|
// See http://www.hyperelliptic.org/EFD/g1p/auto-shortw.html
|
|
type Curve struct {
|
|
P *big.Int // the order of the underlying field
|
|
N *big.Int // the order of the base point
|
|
B *big.Int // the constant of the curve equation
|
|
Gx, Gy *big.Int // (x,y) of the base point
|
|
BitSize int // the size of the underlying field
|
|
}
|
|
|
|
// IsOnCurve returns true if the given (x,y) lies on the curve.
|
|
func (curve *Curve) IsOnCurve(x, y *big.Int) bool {
|
|
// y² = x³ - 3x + b
|
|
y2 := new(big.Int).Mul(y, y)
|
|
y2.Mod(y2, curve.P)
|
|
|
|
x3 := new(big.Int).Mul(x, x)
|
|
x3.Mul(x3, x)
|
|
|
|
threeX := new(big.Int).Lsh(x, 1)
|
|
threeX.Add(threeX, x)
|
|
|
|
x3.Sub(x3, threeX)
|
|
x3.Add(x3, curve.B)
|
|
x3.Mod(x3, curve.P)
|
|
|
|
return x3.Cmp(y2) == 0
|
|
}
|
|
|
|
// affineFromJacobian reverses the Jacobian transform. See the comment at the
|
|
// top of the file.
|
|
func (curve *Curve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {
|
|
zinv := new(big.Int).ModInverse(z, curve.P)
|
|
zinvsq := new(big.Int).Mul(zinv, zinv)
|
|
|
|
xOut = new(big.Int).Mul(x, zinvsq)
|
|
xOut.Mod(xOut, curve.P)
|
|
zinvsq.Mul(zinvsq, zinv)
|
|
yOut = new(big.Int).Mul(y, zinvsq)
|
|
yOut.Mod(yOut, curve.P)
|
|
return
|
|
}
|
|
|
|
// Add returns the sum of (x1,y1) and (x2,y2)
|
|
func (curve *Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
|
|
z := new(big.Int).SetInt64(1)
|
|
return curve.affineFromJacobian(curve.addJacobian(x1, y1, z, x2, y2, z))
|
|
}
|
|
|
|
// addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and
|
|
// (x2, y2, z2) and returns their sum, also in Jacobian form.
|
|
func (curve *Curve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) {
|
|
// See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl
|
|
z1z1 := new(big.Int).Mul(z1, z1)
|
|
z1z1.Mod(z1z1, curve.P)
|
|
z2z2 := new(big.Int).Mul(z2, z2)
|
|
z2z2.Mod(z2z2, curve.P)
|
|
|
|
u1 := new(big.Int).Mul(x1, z2z2)
|
|
u1.Mod(u1, curve.P)
|
|
u2 := new(big.Int).Mul(x2, z1z1)
|
|
u2.Mod(u2, curve.P)
|
|
h := new(big.Int).Sub(u2, u1)
|
|
if h.Sign() == -1 {
|
|
h.Add(h, curve.P)
|
|
}
|
|
i := new(big.Int).Lsh(h, 1)
|
|
i.Mul(i, i)
|
|
j := new(big.Int).Mul(h, i)
|
|
|
|
s1 := new(big.Int).Mul(y1, z2)
|
|
s1.Mul(s1, z2z2)
|
|
s1.Mod(s1, curve.P)
|
|
s2 := new(big.Int).Mul(y2, z1)
|
|
s2.Mul(s2, z1z1)
|
|
s2.Mod(s2, curve.P)
|
|
r := new(big.Int).Sub(s2, s1)
|
|
if r.Sign() == -1 {
|
|
r.Add(r, curve.P)
|
|
}
|
|
r.Lsh(r, 1)
|
|
v := new(big.Int).Mul(u1, i)
|
|
|
|
x3 := new(big.Int).Set(r)
|
|
x3.Mul(x3, x3)
|
|
x3.Sub(x3, j)
|
|
x3.Sub(x3, v)
|
|
x3.Sub(x3, v)
|
|
x3.Mod(x3, curve.P)
|
|
|
|
y3 := new(big.Int).Set(r)
|
|
v.Sub(v, x3)
|
|
y3.Mul(y3, v)
|
|
s1.Mul(s1, j)
|
|
s1.Lsh(s1, 1)
|
|
y3.Sub(y3, s1)
|
|
y3.Mod(y3, curve.P)
|
|
|
|
z3 := new(big.Int).Add(z1, z2)
|
|
z3.Mul(z3, z3)
|
|
z3.Sub(z3, z1z1)
|
|
if z3.Sign() == -1 {
|
|
z3.Add(z3, curve.P)
|
|
}
|
|
z3.Sub(z3, z2z2)
|
|
if z3.Sign() == -1 {
|
|
z3.Add(z3, curve.P)
|
|
}
|
|
z3.Mul(z3, h)
|
|
z3.Mod(z3, curve.P)
|
|
|
|
return x3, y3, z3
|
|
}
|
|
|
|
// Double returns 2*(x,y)
|
|
func (curve *Curve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
|
|
z1 := new(big.Int).SetInt64(1)
|
|
return curve.affineFromJacobian(curve.doubleJacobian(x1, y1, z1))
|
|
}
|
|
|
|
// doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and
|
|
// returns its double, also in Jacobian form.
|
|
func (curve *Curve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) {
|
|
// See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
|
|
delta := new(big.Int).Mul(z, z)
|
|
delta.Mod(delta, curve.P)
|
|
gamma := new(big.Int).Mul(y, y)
|
|
gamma.Mod(gamma, curve.P)
|
|
alpha := new(big.Int).Sub(x, delta)
|
|
if alpha.Sign() == -1 {
|
|
alpha.Add(alpha, curve.P)
|
|
}
|
|
alpha2 := new(big.Int).Add(x, delta)
|
|
alpha.Mul(alpha, alpha2)
|
|
alpha2.Set(alpha)
|
|
alpha.Lsh(alpha, 1)
|
|
alpha.Add(alpha, alpha2)
|
|
|
|
beta := alpha2.Mul(x, gamma)
|
|
|
|
x3 := new(big.Int).Mul(alpha, alpha)
|
|
beta8 := new(big.Int).Lsh(beta, 3)
|
|
x3.Sub(x3, beta8)
|
|
for x3.Sign() == -1 {
|
|
x3.Add(x3, curve.P)
|
|
}
|
|
x3.Mod(x3, curve.P)
|
|
|
|
z3 := new(big.Int).Add(y, z)
|
|
z3.Mul(z3, z3)
|
|
z3.Sub(z3, gamma)
|
|
if z3.Sign() == -1 {
|
|
z3.Add(z3, curve.P)
|
|
}
|
|
z3.Sub(z3, delta)
|
|
if z3.Sign() == -1 {
|
|
z3.Add(z3, curve.P)
|
|
}
|
|
z3.Mod(z3, curve.P)
|
|
|
|
beta.Lsh(beta, 2)
|
|
beta.Sub(beta, x3)
|
|
if beta.Sign() == -1 {
|
|
beta.Add(beta, curve.P)
|
|
}
|
|
y3 := alpha.Mul(alpha, beta)
|
|
|
|
gamma.Mul(gamma, gamma)
|
|
gamma.Lsh(gamma, 3)
|
|
gamma.Mod(gamma, curve.P)
|
|
|
|
y3.Sub(y3, gamma)
|
|
if y3.Sign() == -1 {
|
|
y3.Add(y3, curve.P)
|
|
}
|
|
y3.Mod(y3, curve.P)
|
|
|
|
return x3, y3, z3
|
|
}
|
|
|
|
// ScalarMult returns k*(Bx,By) where k is a number in big-endian form.
|
|
func (curve *Curve) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int) {
|
|
// We have a slight problem in that the identity of the group (the
|
|
// point at infinity) cannot be represented in (x, y) form on a finite
|
|
// machine. Thus the standard add/double algorithm has to be tweaked
|
|
// slightly: our initial state is not the identity, but x, and we
|
|
// ignore the first true bit in |k|. If we don't find any true bits in
|
|
// |k|, then we return nil, nil, because we cannot return the identity
|
|
// element.
|
|
|
|
Bz := new(big.Int).SetInt64(1)
|
|
x := Bx
|
|
y := By
|
|
z := Bz
|
|
|
|
seenFirstTrue := false
|
|
for _, byte := range k {
|
|
for bitNum := 0; bitNum < 8; bitNum++ {
|
|
if seenFirstTrue {
|
|
x, y, z = curve.doubleJacobian(x, y, z)
|
|
}
|
|
if byte&0x80 == 0x80 {
|
|
if !seenFirstTrue {
|
|
seenFirstTrue = true
|
|
} else {
|
|
x, y, z = curve.addJacobian(Bx, By, Bz, x, y, z)
|
|
}
|
|
}
|
|
byte <<= 1
|
|
}
|
|
}
|
|
|
|
if !seenFirstTrue {
|
|
return nil, nil
|
|
}
|
|
|
|
return curve.affineFromJacobian(x, y, z)
|
|
}
|
|
|
|
// ScalarBaseMult returns k*G, where G is the base point of the group and k is
|
|
// an integer in big-endian form.
|
|
func (curve *Curve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
|
|
return curve.ScalarMult(curve.Gx, curve.Gy, k)
|
|
}
|
|
|
|
var mask = []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 *Curve) GenerateKey(rand io.Reader) (priv []byte, x, y *big.Int, err error) {
|
|
byteLen := (curve.BitSize + 7) >> 3
|
|
priv = make([]byte, byteLen)
|
|
|
|
for x == nil {
|
|
_, err = io.ReadFull(rand, priv)
|
|
if err != 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.
|
|
priv[0] &= mask[curve.BitSize%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.
|
|
priv[1] ^= 0x42
|
|
x, y = curve.ScalarBaseMult(priv)
|
|
}
|
|
return
|
|
}
|
|
|
|
// Marshal converts a point into the form specified in section 4.3.6 of ANSI
|
|
// X9.62.
|
|
func (curve *Curve) Marshal(x, y *big.Int) []byte {
|
|
byteLen := (curve.BitSize + 7) >> 3
|
|
|
|
ret := make([]byte, 1+2*byteLen)
|
|
ret[0] = 4 // uncompressed point
|
|
|
|
xBytes := x.Bytes()
|
|
copy(ret[1+byteLen-len(xBytes):], xBytes)
|
|
yBytes := y.Bytes()
|
|
copy(ret[1+2*byteLen-len(yBytes):], yBytes)
|
|
return ret
|
|
}
|
|
|
|
// Unmarshal converts a point, serialized by Marshal, into an x, y pair. On
|
|
// error, x = nil.
|
|
func (curve *Curve) Unmarshal(data []byte) (x, y *big.Int) {
|
|
byteLen := (curve.BitSize + 7) >> 3
|
|
if len(data) != 1+2*byteLen {
|
|
return
|
|
}
|
|
if data[0] != 4 { // uncompressed form
|
|
return
|
|
}
|
|
x = new(big.Int).SetBytes(data[1 : 1+byteLen])
|
|
y = new(big.Int).SetBytes(data[1+byteLen:])
|
|
return
|
|
}
|
|
|
|
var initonce sync.Once
|
|
var p224 *Curve
|
|
var p256 *Curve
|
|
var p384 *Curve
|
|
var p521 *Curve
|
|
|
|
func initAll() {
|
|
initP224()
|
|
initP256()
|
|
initP384()
|
|
initP521()
|
|
}
|
|
|
|
func initP224() {
|
|
// See FIPS 186-3, section D.2.2
|
|
p224 = new(Curve)
|
|
p224.P, _ = new(big.Int).SetString("26959946667150639794667015087019630673557916260026308143510066298881", 10)
|
|
p224.N, _ = new(big.Int).SetString("26959946667150639794667015087019625940457807714424391721682722368061", 10)
|
|
p224.B, _ = new(big.Int).SetString("b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4", 16)
|
|
p224.Gx, _ = new(big.Int).SetString("b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21", 16)
|
|
p224.Gy, _ = new(big.Int).SetString("bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34", 16)
|
|
p224.BitSize = 224
|
|
}
|
|
|
|
func initP256() {
|
|
// See FIPS 186-3, section D.2.3
|
|
p256 = new(Curve)
|
|
p256.P, _ = new(big.Int).SetString("115792089210356248762697446949407573530086143415290314195533631308867097853951", 10)
|
|
p256.N, _ = new(big.Int).SetString("115792089210356248762697446949407573529996955224135760342422259061068512044369", 10)
|
|
p256.B, _ = new(big.Int).SetString("5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", 16)
|
|
p256.Gx, _ = new(big.Int).SetString("6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", 16)
|
|
p256.Gy, _ = new(big.Int).SetString("4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", 16)
|
|
p256.BitSize = 256
|
|
}
|
|
|
|
func initP384() {
|
|
// See FIPS 186-3, section D.2.4
|
|
p384 = new(Curve)
|
|
p384.P, _ = new(big.Int).SetString("39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319", 10)
|
|
p384.N, _ = new(big.Int).SetString("39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643", 10)
|
|
p384.B, _ = new(big.Int).SetString("b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef", 16)
|
|
p384.Gx, _ = new(big.Int).SetString("aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7", 16)
|
|
p384.Gy, _ = new(big.Int).SetString("3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f", 16)
|
|
p384.BitSize = 384
|
|
}
|
|
|
|
func initP521() {
|
|
// See FIPS 186-3, section D.2.5
|
|
p521 = new(Curve)
|
|
p521.P, _ = new(big.Int).SetString("6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151", 10)
|
|
p521.N, _ = new(big.Int).SetString("6864797660130609714981900799081393217269435300143305409394463459185543183397655394245057746333217197532963996371363321113864768612440380340372808892707005449", 10)
|
|
p521.B, _ = new(big.Int).SetString("051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", 16)
|
|
p521.Gx, _ = new(big.Int).SetString("c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", 16)
|
|
p521.Gy, _ = new(big.Int).SetString("11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", 16)
|
|
p521.BitSize = 521
|
|
}
|
|
|
|
// P224 returns a Curve which implements P-224 (see FIPS 186-3, section D.2.2)
|
|
func P224() *Curve {
|
|
initonce.Do(initAll)
|
|
return p224
|
|
}
|
|
|
|
// P256 returns a Curve which implements P-256 (see FIPS 186-3, section D.2.3)
|
|
func P256() *Curve {
|
|
initonce.Do(initAll)
|
|
return p256
|
|
}
|
|
|
|
// P384 returns a Curve which implements P-384 (see FIPS 186-3, section D.2.4)
|
|
func P384() *Curve {
|
|
initonce.Do(initAll)
|
|
return p384
|
|
}
|
|
|
|
// P256 returns a Curve which implements P-521 (see FIPS 186-3, section D.2.5)
|
|
func P521() *Curve {
|
|
initonce.Do(initAll)
|
|
return p521
|
|
}
|