|
| 1 | +// This package provides the interface and custom implementation of elliptic curve operations. |
| 2 | +// It defines interfaces and structures to perform standard elliptic curve cryptographic operations |
| 3 | +// such as point addition, scalar multiplication, and checking whether a point lies on the curve. |
| 4 | +// we can create own custom curve or wrap official curve to follow the interface of this package. |
| 5 | + |
| 6 | +package curve |
| 7 | + |
| 8 | +import ( |
| 9 | + "crypto/elliptic" |
| 10 | + "math/big" |
| 11 | + |
| 12 | + "github.com/emmansun/gmsm/sm2" |
| 13 | +) |
| 14 | + |
| 15 | +type CurveParams struct { |
| 16 | + P *big.Int // the order of the underlying field |
| 17 | + N *big.Int // the order of the base point |
| 18 | + A *big.Int // the constant of the curve equation |
| 19 | + B *big.Int // the constant of the curve equation |
| 20 | + Gx, Gy *big.Int // (x,y) of the base point |
| 21 | + BitSize int // the size of the underlying field |
| 22 | + Name string // the canonical name of the curve |
| 23 | +} |
| 24 | + |
| 25 | +type Curve interface { |
| 26 | + Params() *CurveParams |
| 27 | + IsOnCurve(x, y *big.Int) bool |
| 28 | + Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int) |
| 29 | + ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int) |
| 30 | + ScalarBaseMult(k []byte) (x, y *big.Int) |
| 31 | +} |
| 32 | + |
| 33 | +type CustomStandardCurve struct { |
| 34 | + *CurveParams |
| 35 | +} |
| 36 | + |
| 37 | +func (c *CustomStandardCurve) Params() *CurveParams { |
| 38 | + return c.CurveParams |
| 39 | +} |
| 40 | + |
| 41 | +func (c *CustomStandardCurve) IsOnCurve(x, y *big.Int) bool { |
| 42 | + if x.Sign() == 0 && y.Sign() == 0 { |
| 43 | + return true // infinite point |
| 44 | + } |
| 45 | + |
| 46 | + // calculate y² mod p |
| 47 | + ySquare := new(big.Int).Exp(y, big.NewInt(2), c.P) |
| 48 | + |
| 49 | + // calculate x³ + ax + b mod p |
| 50 | + x3 := new(big.Int).Exp(x, big.NewInt(3), c.P) |
| 51 | + ax := new(big.Int).Mul(c.A, x) |
| 52 | + ax.Mod(ax, c.P) |
| 53 | + |
| 54 | + rhs := new(big.Int).Add(x3, ax) |
| 55 | + rhs.Add(rhs, c.B) |
| 56 | + rhs.Mod(rhs, c.P) |
| 57 | + |
| 58 | + return ySquare.Cmp(rhs) == 0 |
| 59 | +} |
| 60 | + |
| 61 | +func (c *CustomStandardCurve) Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int) { |
| 62 | + if x1.Sign() == 0 && y1.Sign() == 0 { |
| 63 | + return x2, y2 |
| 64 | + } |
| 65 | + |
| 66 | + if x2.Sign() == 0 && y2.Sign() == 0 { |
| 67 | + return x1, y1 |
| 68 | + } |
| 69 | + |
| 70 | + // handle inverse point (P + (-P) = infinity point) |
| 71 | + if x1.Cmp(x2) == 0 && y1.Cmp(new(big.Int).Sub(c.P, y2)) == 0 { |
| 72 | + return new(big.Int), new(big.Int) |
| 73 | + } |
| 74 | + |
| 75 | + var lambda *big.Int |
| 76 | + |
| 77 | + // point doubling (P == Q) |
| 78 | + if x1.Cmp(x2) == 0 && y1.Cmp(y2) == 0 { |
| 79 | + // λ = (3x² + a) / (2y) mod p |
| 80 | + num := new(big.Int).Mul(big.NewInt(3), new(big.Int).Exp(x1, big.NewInt(2), nil)) |
| 81 | + num.Add(num, c.A) |
| 82 | + num.Mod(num, c.P) |
| 83 | + |
| 84 | + den := new(big.Int).Mul(big.NewInt(2), y1) |
| 85 | + den.Mod(den, c.P) |
| 86 | + |
| 87 | + // calculate modular inverse |
| 88 | + denInv := new(big.Int).ModInverse(den, c.P) |
| 89 | + lambda = new(big.Int).Mul(num, denInv) |
| 90 | + lambda.Mod(lambda, c.P) |
| 91 | + } else { |
| 92 | + // normal addition (P ≠ Q) |
| 93 | + // λ = (y₂ - y₁) / (x₂ - x₁) mod p |
| 94 | + num := new(big.Int).Sub(y2, y1) |
| 95 | + num.Mod(num, c.P) |
| 96 | + |
| 97 | + den := new(big.Int).Sub(x2, x1) |
| 98 | + den.Mod(den, c.P) |
| 99 | + |
| 100 | + denInv := new(big.Int).ModInverse(den, c.P) |
| 101 | + lambda = new(big.Int).Mul(num, denInv) |
| 102 | + lambda.Mod(lambda, c.P) |
| 103 | + } |
| 104 | + |
| 105 | + // calculate x₃ = λ² - x₁ - x₂ mod p |
| 106 | + x3 := new(big.Int).Exp(lambda, big.NewInt(2), nil) |
| 107 | + x3.Sub(x3, x1) |
| 108 | + x3.Sub(x3, x2) |
| 109 | + x3.Mod(x3, c.P) |
| 110 | + |
| 111 | + // calculate y₃ = λ(x₁ - x₃) - y₁ mod p |
| 112 | + y3 := new(big.Int).Sub(x1, x3) |
| 113 | + y3.Mul(y3, lambda) |
| 114 | + y3.Sub(y3, y1) |
| 115 | + y3.Mod(y3, c.P) |
| 116 | + |
| 117 | + |
| 118 | + return x3, y3 |
| 119 | +} |
| 120 | + |
| 121 | +func (c *CustomStandardCurve) ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int) { |
| 122 | + x = new(big.Int) |
| 123 | + y = new(big.Int) |
| 124 | + |
| 125 | + kCopy := new(big.Int).SetBytes(k) |
| 126 | + |
| 127 | + // binary expansion (Montgomery ladder algorithm) |
| 128 | + for kCopy.Sign() > 0 { |
| 129 | + if kCopy.Bit(0) == 1 { |
| 130 | + x, y = c.Add(x, y, x1, y1) |
| 131 | + } |
| 132 | + |
| 133 | + x1, y1 = c.Add(x1, y1, x1, y1) // point doubling |
| 134 | + kCopy.Rsh(kCopy, 1) // right shift by one bit |
| 135 | + } |
| 136 | + |
| 137 | + return x, y |
| 138 | +} |
| 139 | + |
| 140 | +func (c *CustomStandardCurve) ScalarBaseMult(k []byte) (x, y *big.Int) { |
| 141 | + return c.ScalarMult(c.Gx, c.Gy, k) |
| 142 | +} |
| 143 | + |
| 144 | +func NewCustomSM2Curve() *CustomStandardCurve { |
| 145 | + p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16) |
| 146 | + a, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", 16) |
| 147 | + b, _ := new(big.Int).SetString("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16) |
| 148 | + n, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16) |
| 149 | + |
| 150 | + gx, _ := new(big.Int).SetString("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", 16) |
| 151 | + gy, _ := new(big.Int).SetString("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", 16) |
| 152 | + |
| 153 | + return &CustomStandardCurve{ |
| 154 | + &CurveParams{ |
| 155 | + P: p, |
| 156 | + N: n, |
| 157 | + A: a, |
| 158 | + B: b, |
| 159 | + Gx: gx, |
| 160 | + Gy: gy, |
| 161 | + BitSize: 256, |
| 162 | + Name: "Custom SM2", |
| 163 | + }, |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func NewCustomSecp256k1Curve() *CustomStandardCurve { |
| 168 | + p, _ := new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16) |
| 169 | + a, _ := new(big.Int).SetString("000000000000000000000000000000000000000000000000000000000000", 16) |
| 170 | + b, _ := new(big.Int).SetString("000000000000000000000000000000000000000000000000000000000007", 16) |
| 171 | + n, _ := new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16) |
| 172 | + |
| 173 | + gx, _ := new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16) |
| 174 | + gy, _ := new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16) |
| 175 | + |
| 176 | + return &CustomStandardCurve{ |
| 177 | + &CurveParams{ |
| 178 | + P: p, |
| 179 | + N: n, |
| 180 | + A: a, |
| 181 | + B: b, |
| 182 | + Gx: gx, |
| 183 | + Gy: gy, |
| 184 | + BitSize: 256, |
| 185 | + Name: "Custom Secp256k1", |
| 186 | + }, |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +type OfficialSM2Curve struct { |
| 191 | + curve elliptic.Curve |
| 192 | +} |
| 193 | + |
| 194 | +func (c *OfficialSM2Curve) Params() *CurveParams { |
| 195 | + A := new(big.Int).Mod(big.NewInt(-3), c.curve.Params().P) |
| 196 | + |
| 197 | + return &CurveParams{ |
| 198 | + P: c.curve.Params().P, |
| 199 | + N: c.curve.Params().N, |
| 200 | + A: A, |
| 201 | + B: c.curve.Params().B, |
| 202 | + Gx: c.curve.Params().Gx, |
| 203 | + Gy: c.curve.Params().Gy, |
| 204 | + BitSize: c.curve.Params().BitSize, |
| 205 | + Name: c.curve.Params().Name, |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +func (c *OfficialSM2Curve) IsOnCurve(x, y *big.Int) bool { |
| 210 | + return c.curve.IsOnCurve(x, y) |
| 211 | +} |
| 212 | + |
| 213 | +func (c *OfficialSM2Curve) Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int) { |
| 214 | + return c.curve.Add(x1, y1, x2, y2) |
| 215 | +} |
| 216 | + |
| 217 | +func (c *OfficialSM2Curve) ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int) { |
| 218 | + return c.curve.ScalarMult(x1, y1, k) |
| 219 | +} |
| 220 | + |
| 221 | +func (c *OfficialSM2Curve) ScalarBaseMult(k []byte) (x, y *big.Int) { |
| 222 | + return c.curve.ScalarBaseMult(k) |
| 223 | +} |
| 224 | + |
| 225 | +func NewOfficialSM2Curve() *OfficialSM2Curve { |
| 226 | + return &OfficialSM2Curve{ |
| 227 | + curve: sm2.P256(), |
| 228 | + } |
| 229 | +} |
0 commit comments