mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-22 02:06:18 +08:00
239 lines
6.1 KiB
Go
239 lines
6.1 KiB
Go
package sm9
|
|
|
|
import "math/big"
|
|
|
|
// CurveParams contains the parameters of an elliptic curve and also provides
|
|
// a generic, non-constant time implementation of Curve.
|
|
type CurveParams 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
|
|
Name string // the canonical name of the curve
|
|
}
|
|
|
|
func (curve *CurveParams) Params() *CurveParams {
|
|
return curve
|
|
}
|
|
|
|
// 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³ + b.
|
|
func (curve *CurveParams) polynomial(x *big.Int) *big.Int {
|
|
x3 := new(big.Int).Mul(x, x)
|
|
x3.Mul(x3, x)
|
|
|
|
x3.Add(x3, curve.B)
|
|
x3.Mod(x3, curve.P)
|
|
|
|
return x3
|
|
}
|
|
|
|
func (curve *CurveParams) IsOnCurve(x, y *big.Int) bool {
|
|
if x.Sign() < 0 || x.Cmp(curve.P) >= 0 ||
|
|
y.Sign() < 0 || y.Cmp(curve.P) >= 0 {
|
|
return false
|
|
}
|
|
|
|
// y² = x³ + b
|
|
y2 := new(big.Int).Mul(y, y)
|
|
y2.Mod(y2, curve.P)
|
|
|
|
return curve.polynomial(x).Cmp(y2) == 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 zForAffine(x, y *big.Int) *big.Int {
|
|
z := new(big.Int)
|
|
if x.Sign() != 0 || y.Sign() != 0 {
|
|
z.SetInt64(1)
|
|
}
|
|
return z
|
|
}
|
|
|
|
// affineFromJacobian reverses the Jacobian transform. See the comment at the
|
|
// top of the file. If the point is ∞ it returns 0, 0.
|
|
func (curve *CurveParams) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {
|
|
if z.Sign() == 0 {
|
|
return new(big.Int), new(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
|
|
}
|
|
|
|
func (curve *CurveParams) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
|
|
z1 := zForAffine(x1, y1)
|
|
z2 := zForAffine(x2, y2)
|
|
return curve.affineFromJacobian(curve.addJacobian(x1, y1, z1, x2, y2, z2))
|
|
}
|
|
|
|
// addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and
|
|
// (x2, y2, z2) and returns their sum, also in Jacobian form.
|
|
func (curve *CurveParams) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) {
|
|
// See https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
|
|
x3, y3, z3 := new(big.Int), new(big.Int), new(big.Int)
|
|
if z1.Sign() == 0 {
|
|
x3.Set(x2)
|
|
y3.Set(y2)
|
|
z3.Set(z2)
|
|
return x3, y3, z3
|
|
}
|
|
if z2.Sign() == 0 {
|
|
x3.Set(x1)
|
|
y3.Set(y1)
|
|
z3.Set(z1)
|
|
return x3, y3, z3
|
|
}
|
|
|
|
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)
|
|
xEqual := h.Sign() == 0
|
|
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)
|
|
}
|
|
yEqual := r.Sign() == 0
|
|
if xEqual && yEqual {
|
|
return curve.doubleJacobian(x1, y1, z1)
|
|
}
|
|
r.Lsh(r, 1)
|
|
v := new(big.Int).Mul(u1, i)
|
|
|
|
x3.Set(r)
|
|
x3.Mul(x3, x3)
|
|
x3.Sub(x3, j)
|
|
x3.Sub(x3, v)
|
|
x3.Sub(x3, v)
|
|
x3.Mod(x3, curve.P)
|
|
|
|
y3.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.Add(z1, z2)
|
|
z3.Mul(z3, z3)
|
|
z3.Sub(z3, z1z1)
|
|
z3.Sub(z3, z2z2)
|
|
z3.Mul(z3, h)
|
|
z3.Mod(z3, curve.P)
|
|
|
|
return x3, y3, z3
|
|
}
|
|
|
|
func (curve *CurveParams) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
|
|
z1 := zForAffine(x1, y1)
|
|
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 *CurveParams) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) {
|
|
// See http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/doubling/dbl-2009-l.op3
|
|
a := new(big.Int).Mul(x, x)
|
|
a.Mod(a, curve.P)
|
|
b := new(big.Int).Mul(y, y)
|
|
b.Mod(b, curve.P)
|
|
c := new(big.Int).Mul(b, b)
|
|
c.Mod(c, curve.P)
|
|
|
|
d := new(big.Int).Add(x, b)
|
|
d.Mul(d, d)
|
|
d.Sub(d, a)
|
|
d.Sub(d, c)
|
|
d.Lsh(d, 1)
|
|
if d.Sign() < 0 {
|
|
d.Add(d, curve.P)
|
|
} else {
|
|
d.Mod(d, curve.P)
|
|
}
|
|
|
|
e := new(big.Int).Lsh(a, 1)
|
|
e.Add(e, a)
|
|
f := new(big.Int).Mul(e, e)
|
|
x3 := new(big.Int).Lsh(d, 1)
|
|
x3.Sub(f, x3)
|
|
if x3.Sign() < 0 {
|
|
x3.Add(x3, curve.P)
|
|
} else {
|
|
x3.Mod(x3, curve.P)
|
|
}
|
|
|
|
y3 := new(big.Int).Sub(d, x3)
|
|
y3.Mul(y3, e)
|
|
c.Lsh(c, 3)
|
|
y3.Sub(y3, c)
|
|
if y3.Sign() < 0 {
|
|
y3.Add(y3, curve.P)
|
|
} else {
|
|
y3.Mod(y3, curve.P)
|
|
}
|
|
|
|
z3 := new(big.Int).Mul(y, z)
|
|
z3.Lsh(z3, 1)
|
|
z3.Mod(z3, curve.P)
|
|
|
|
return x3, y3, z3
|
|
}
|
|
|
|
func (curve *CurveParams) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int) {
|
|
Bz := new(big.Int).SetInt64(1)
|
|
x, y, z := new(big.Int), new(big.Int), new(big.Int)
|
|
|
|
for _, byte := range k {
|
|
for bitNum := 0; bitNum < 8; bitNum++ {
|
|
x, y, z = curve.doubleJacobian(x, y, z)
|
|
if byte&0x80 == 0x80 {
|
|
x, y, z = curve.addJacobian(Bx, By, Bz, x, y, z)
|
|
}
|
|
byte <<= 1
|
|
}
|
|
}
|
|
|
|
return curve.affineFromJacobian(x, y, z)
|
|
}
|
|
|
|
func (curve *CurveParams) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
|
|
return curve.ScalarMult(curve.Gx, curve.Gy, k)
|
|
}
|