| 1 |
package bitcurves |
| 2 |
|
| 3 |
// Copyright 2010 The Go Authors. All rights reserved. |
| 4 |
// Copyright 2011 ThePiachu. All rights reserved. |
| 5 |
// Use of this source code is governed by a BSD-style |
| 6 |
// license that can be found in the LICENSE file. |
| 7 |
|
| 8 |
// Package bitelliptic implements several Koblitz elliptic curves over prime |
| 9 |
// fields. |
| 10 |
|
| 11 |
// This package operates, internally, on Jacobian coordinates. For a given |
| 12 |
// (x, y) position on the curve, the Jacobian coordinates are (x1, y1, z1) |
| 13 |
// where x = x1/z1² and y = y1/z1³. The greatest speedups come when the whole |
| 14 |
// calculation can be performed within the transform (as in ScalarMult and |
| 15 |
// ScalarBaseMult). But even for Add and Double, it's faster to apply and |
| 16 |
// reverse the transform than to operate in affine coordinates. |
| 17 |
|
| 18 |
import ( |
| 19 |
"crypto/elliptic" |
| 20 |
"io" |
| 21 |
"math/big" |
| 22 |
"sync" |
| 23 |
) |
| 24 |
|
| 25 |
// A BitCurve represents a Koblitz Curve with a=0. |
| 26 |
// See http://www.hyperelliptic.org/EFD/g1p/auto-shortw.html |
| 27 |
type BitCurve struct { |
| 28 |
Name string |
| 29 |
P *big.Int // the order of the underlying field |
| 30 |
N *big.Int // the order of the base point |
| 31 |
B *big.Int // the constant of the BitCurve equation |
| 32 |
Gx, Gy *big.Int // (x,y) of the base point |
| 33 |
BitSize int // the size of the underlying field |
| 34 |
} |
| 35 |
|
| 36 |
// Params returns the parameters of the given BitCurve (see BitCurve struct) |
| 37 |
func (bitCurve *BitCurve) Params() (cp *elliptic.CurveParams) { |
| 38 |
cp = new(elliptic.CurveParams) |
| 39 |
cp.Name = bitCurve.Name |
| 40 |
cp.P = bitCurve.P |
| 41 |
cp.N = bitCurve.N |
| 42 |
cp.Gx = bitCurve.Gx |
| 43 |
cp.Gy = bitCurve.Gy |
| 44 |
cp.BitSize = bitCurve.BitSize |
| 45 |
return cp |
| 46 |
} |
| 47 |
|
| 48 |
// IsOnCurve returns true if the given (x,y) lies on the BitCurve. |
| 49 |
func (bitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool { |
| 50 |
// y² = x³ + b |
| 51 |
y2 := new(big.Int).Mul(y, y) //y² |
| 52 |
y2.Mod(y2, bitCurve.P) //y²%P |
| 53 |
|
| 54 |
x3 := new(big.Int).Mul(x, x) //x² |
| 55 |
x3.Mul(x3, x) //x³ |
| 56 |
|
| 57 |
x3.Add(x3, bitCurve.B) //x³+B |
| 58 |
x3.Mod(x3, bitCurve.P) //(x³+B)%P |
| 59 |
|
| 60 |
return x3.Cmp(y2) == 0 |
| 61 |
} |
| 62 |
|
| 63 |
// affineFromJacobian reverses the Jacobian transform. See the comment at the |
| 64 |
// top of the file. |
| 65 |
func (bitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) { |
| 66 |
if z.Cmp(big.NewInt(0)) == 0 { |
| 67 |
panic("bitcurve: Can't convert to affine with Jacobian Z = 0") |
| 68 |
} |
| 69 |
// x = YZ^2 mod P |
| 70 |
zinv := new(big.Int).ModInverse(z, bitCurve.P) |
| 71 |
zinvsq := new(big.Int).Mul(zinv, zinv) |
| 72 |
|
| 73 |
xOut = new(big.Int).Mul(x, zinvsq) |
| 74 |
xOut.Mod(xOut, bitCurve.P) |
| 75 |
// y = YZ^3 mod P |
| 76 |
zinvsq.Mul(zinvsq, zinv) |
| 77 |
yOut = new(big.Int).Mul(y, zinvsq) |
| 78 |
yOut.Mod(yOut, bitCurve.P) |
| 79 |
return xOut, yOut |
| 80 |
} |
| 81 |
|
| 82 |
// Add returns the sum of (x1,y1) and (x2,y2) |
| 83 |
func (bitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) { |
| 84 |
z := new(big.Int).SetInt64(1) |
| 85 |
x, y, z := bitCurve.addJacobian(x1, y1, z, x2, y2, z) |
| 86 |
return bitCurve.affineFromJacobian(x, y, z) |
| 87 |
} |
| 88 |
|
| 89 |
// addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and |
| 90 |
// (x2, y2, z2) and returns their sum, also in Jacobian form. |
| 91 |
func (bitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) { |
| 92 |
// See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl |
| 93 |
z1z1 := new(big.Int).Mul(z1, z1) |
| 94 |
z1z1.Mod(z1z1, bitCurve.P) |
| 95 |
z2z2 := new(big.Int).Mul(z2, z2) |
| 96 |
z2z2.Mod(z2z2, bitCurve.P) |
| 97 |
|
| 98 |
u1 := new(big.Int).Mul(x1, z2z2) |
| 99 |
u1.Mod(u1, bitCurve.P) |
| 100 |
u2 := new(big.Int).Mul(x2, z1z1) |
| 101 |
u2.Mod(u2, bitCurve.P) |
| 102 |
h := new(big.Int).Sub(u2, u1) |
| 103 |
if h.Sign() == -1 { |
| 104 |
h.Add(h, bitCurve.P) |
| 105 |
} |
| 106 |
i := new(big.Int).Lsh(h, 1) |
| 107 |
i.Mul(i, i) |
| 108 |
j := new(big.Int).Mul(h, i) |
| 109 |
|
| 110 |
s1 := new(big.Int).Mul(y1, z2) |
| 111 |
s1.Mul(s1, z2z2) |
| 112 |
s1.Mod(s1, bitCurve.P) |
| 113 |
s2 := new(big.Int).Mul(y2, z1) |
| 114 |
s2.Mul(s2, z1z1) |
| 115 |
s2.Mod(s2, bitCurve.P) |
| 116 |
r := new(big.Int).Sub(s2, s1) |
| 117 |
if r.Sign() == -1 { |
| 118 |
r.Add(r, bitCurve.P) |
| 119 |
} |
| 120 |
r.Lsh(r, 1) |
| 121 |
v := new(big.Int).Mul(u1, i) |
| 122 |
|
| 123 |
x3 := new(big.Int).Set(r) |
| 124 |
x3.Mul(x3, x3) |
| 125 |
x3.Sub(x3, j) |
| 126 |
x3.Sub(x3, v) |
| 127 |
x3.Sub(x3, v) |
| 128 |
x3.Mod(x3, bitCurve.P) |
| 129 |
|
| 130 |
y3 := new(big.Int).Set(r) |
| 131 |
v.Sub(v, x3) |
| 132 |
y3.Mul(y3, v) |
| 133 |
s1.Mul(s1, j) |
| 134 |
s1.Lsh(s1, 1) |
| 135 |
y3.Sub(y3, s1) |
| 136 |
y3.Mod(y3, bitCurve.P) |
| 137 |
|
| 138 |
z3 := new(big.Int).Add(z1, z2) |
| 139 |
z3.Mul(z3, z3) |
| 140 |
z3.Sub(z3, z1z1) |
| 141 |
if z3.Sign() == -1 { |
| 142 |
z3.Add(z3, bitCurve.P) |
| 143 |
} |
| 144 |
z3.Sub(z3, z2z2) |
| 145 |
if z3.Sign() == -1 { |
| 146 |
z3.Add(z3, bitCurve.P) |
| 147 |
} |
| 148 |
z3.Mul(z3, h) |
| 149 |
z3.Mod(z3, bitCurve.P) |
| 150 |
|
| 151 |
return x3, y3, z3 |
| 152 |
} |
| 153 |
|
| 154 |
// Double returns 2*(x,y) |
| 155 |
func (bitCurve *BitCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) { |
| 156 |
z1 := new(big.Int).SetInt64(1) |
| 157 |
return bitCurve.affineFromJacobian(bitCurve.doubleJacobian(x1, y1, z1)) |
| 158 |
} |
| 159 |
|
| 160 |
// doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and |
| 161 |
// returns its double, also in Jacobian form. |
| 162 |
func (bitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) { |
| 163 |
// See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l |
| 164 |
|
| 165 |
a := new(big.Int).Mul(x, x) //X1² |
| 166 |
b := new(big.Int).Mul(y, y) //Y1² |
| 167 |
c := new(big.Int).Mul(b, b) //B² |
| 168 |
|
| 169 |
d := new(big.Int).Add(x, b) //X1+B |
| 170 |
d.Mul(d, d) //(X1+B)² |
| 171 |
d.Sub(d, a) //(X1+B)²-A |
| 172 |
d.Sub(d, c) //(X1+B)²-A-C |
| 173 |
d.Mul(d, big.NewInt(2)) //2*((X1+B)²-A-C) |
| 174 |
|
| 175 |
e := new(big.Int).Mul(big.NewInt(3), a) //3*A |
| 176 |
f := new(big.Int).Mul(e, e) //E² |
| 177 |
|
| 178 |
x3 := new(big.Int).Mul(big.NewInt(2), d) //2*D |
| 179 |
x3.Sub(f, x3) //F-2*D |
| 180 |
x3.Mod(x3, bitCurve.P) |
| 181 |
|
| 182 |
y3 := new(big.Int).Sub(d, x3) //D-X3 |
| 183 |
y3.Mul(e, y3) //E*(D-X3) |
| 184 |
y3.Sub(y3, new(big.Int).Mul(big.NewInt(8), c)) //E*(D-X3)-8*C |
| 185 |
y3.Mod(y3, bitCurve.P) |
| 186 |
|
| 187 |
z3 := new(big.Int).Mul(y, z) //Y1*Z1 |
| 188 |
z3.Mul(big.NewInt(2), z3) //3*Y1*Z1 |
| 189 |
z3.Mod(z3, bitCurve.P) |
| 190 |
|
| 191 |
return x3, y3, z3 |
| 192 |
} |
| 193 |
|
| 194 |
// TODO: double check if it is okay |
| 195 |
// ScalarMult returns k*(Bx,By) where k is a number in big-endian form. |
| 196 |
func (bitCurve *BitCurve) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int) { |
| 197 |
// We have a slight problem in that the identity of the group (the |
| 198 |
// point at infinity) cannot be represented in (x, y) form on a finite |
| 199 |
// machine. Thus the standard add/double algorithm has to be tweaked |
| 200 |
// slightly: our initial state is not the identity, but x, and we |
| 201 |
// ignore the first true bit in |k|. If we don't find any true bits in |
| 202 |
// |k|, then we return nil, nil, because we cannot return the identity |
| 203 |
// element. |
| 204 |
|
| 205 |
Bz := new(big.Int).SetInt64(1) |
| 206 |
x := Bx |
| 207 |
y := By |
| 208 |
z := Bz |
| 209 |
|
| 210 |
seenFirstTrue := false |
| 211 |
for _, byte := range k { |
| 212 |
for bitNum := 0; bitNum < 8; bitNum++ { |
| 213 |
if seenFirstTrue { |
| 214 |
x, y, z = bitCurve.doubleJacobian(x, y, z) |
| 215 |
} |
| 216 |
if byte&0x80 == 0x80 { |
| 217 |
if !seenFirstTrue { |
| 218 |
seenFirstTrue = true |
| 219 |
} else { |
| 220 |
x, y, z = bitCurve.addJacobian(Bx, By, Bz, x, y, z) |
| 221 |
} |
| 222 |
} |
| 223 |
byte <<= 1 |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if !seenFirstTrue { |
| 228 |
return nil, nil |
| 229 |
} |
| 230 |
|
| 231 |
return bitCurve.affineFromJacobian(x, y, z) |
| 232 |
} |
| 233 |
|
| 234 |
// ScalarBaseMult returns k*G, where G is the base point of the group and k is |
| 235 |
// an integer in big-endian form. |
| 236 |
func (bitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) { |
| 237 |
return bitCurve.ScalarMult(bitCurve.Gx, bitCurve.Gy, k) |
| 238 |
} |
| 239 |
|
| 240 |
var mask = []byte{0xff, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f} |
| 241 |
|
| 242 |
// TODO: double check if it is okay |
| 243 |
// GenerateKey returns a public/private key pair. The private key is generated |
| 244 |
// using the given reader, which must return random data. |
| 245 |
func (bitCurve *BitCurve) GenerateKey(rand io.Reader) (priv []byte, x, y *big.Int, err error) { |
| 246 |
byteLen := (bitCurve.BitSize + 7) >> 3 |
| 247 |
priv = make([]byte, byteLen) |
| 248 |
|
| 249 |
for x == nil { |
| 250 |
_, err = io.ReadFull(rand, priv) |
| 251 |
if err != nil { |
| 252 |
return |
| 253 |
} |
| 254 |
// We have to mask off any excess bits in the case that the size of the |
| 255 |
// underlying field is not a whole number of bytes. |
| 256 |
priv[0] &= mask[bitCurve.BitSize%8] |
| 257 |
// This is because, in tests, rand will return all zeros and we don't |
| 258 |
// want to get the point at infinity and loop forever. |
| 259 |
priv[1] ^= 0x42 |
| 260 |
x, y = bitCurve.ScalarBaseMult(priv) |
| 261 |
} |
| 262 |
return |
| 263 |
} |
| 264 |
|
| 265 |
// Marshal converts a point into the form specified in section 4.3.6 of ANSI |
| 266 |
// X9.62. |
| 267 |
func (bitCurve *BitCurve) Marshal(x, y *big.Int) []byte { |
| 268 |
byteLen := (bitCurve.BitSize + 7) >> 3 |
| 269 |
|
| 270 |
ret := make([]byte, 1+2*byteLen) |
| 271 |
ret[0] = 4 // uncompressed point |
| 272 |
|
| 273 |
xBytes := x.Bytes() |
| 274 |
copy(ret[1+byteLen-len(xBytes):], xBytes) |
| 275 |
yBytes := y.Bytes() |
| 276 |
copy(ret[1+2*byteLen-len(yBytes):], yBytes) |
| 277 |
return ret |
| 278 |
} |
| 279 |
|
| 280 |
// Unmarshal converts a point, serialised by Marshal, into an x, y pair. On |
| 281 |
// error, x = nil. |
| 282 |
func (bitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int) { |
| 283 |
byteLen := (bitCurve.BitSize + 7) >> 3 |
| 284 |
if len(data) != 1+2*byteLen { |
| 285 |
return |
| 286 |
} |
| 287 |
if data[0] != 4 { // uncompressed form |
| 288 |
return |
| 289 |
} |
| 290 |
x = new(big.Int).SetBytes(data[1 : 1+byteLen]) |
| 291 |
y = new(big.Int).SetBytes(data[1+byteLen:]) |
| 292 |
return |
| 293 |
} |
| 294 |
|
| 295 |
//curve parameters taken from: |
| 296 |
//http://www.secg.org/collateral/sec2_final.pdf |
| 297 |
|
| 298 |
var initonce sync.Once |
| 299 |
var secp160k1 *BitCurve |
| 300 |
var secp192k1 *BitCurve |
| 301 |
var secp224k1 *BitCurve |
| 302 |
var secp256k1 *BitCurve |
| 303 |
|
| 304 |
func initAll() { |
| 305 |
initS160() |
| 306 |
initS192() |
| 307 |
initS224() |
| 308 |
initS256() |
| 309 |
} |
| 310 |
|
| 311 |
func initS160() { |
| 312 |
// See SEC 2 section 2.4.1 |
| 313 |
secp160k1 = new(BitCurve) |
| 314 |
secp160k1.Name = "secp160k1" |
| 315 |
secp160k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73", 16) |
| 316 |
secp160k1.N, _ = new(big.Int).SetString("0100000000000000000001B8FA16DFAB9ACA16B6B3", 16) |
| 317 |
secp160k1.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000007", 16) |
| 318 |
secp160k1.Gx, _ = new(big.Int).SetString("3B4C382CE37AA192A4019E763036F4F5DD4D7EBB", 16) |
| 319 |
secp160k1.Gy, _ = new(big.Int).SetString("938CF935318FDCED6BC28286531733C3F03C4FEE", 16) |
| 320 |
secp160k1.BitSize = 160 |
| 321 |
} |
| 322 |
|
| 323 |
func initS192() { |
| 324 |
// See SEC 2 section 2.5.1 |
| 325 |
secp192k1 = new(BitCurve) |
| 326 |
secp192k1.Name = "secp192k1" |
| 327 |
secp192k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37", 16) |
| 328 |
secp192k1.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D", 16) |
| 329 |
secp192k1.B, _ = new(big.Int).SetString("000000000000000000000000000000000000000000000003", 16) |
| 330 |
secp192k1.Gx, _ = new(big.Int).SetString("DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D", 16) |
| 331 |
secp192k1.Gy, _ = new(big.Int).SetString("9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D", 16) |
| 332 |
secp192k1.BitSize = 192 |
| 333 |
} |
| 334 |
|
| 335 |
func initS224() { |
| 336 |
// See SEC 2 section 2.6.1 |
| 337 |
secp224k1 = new(BitCurve) |
| 338 |
secp224k1.Name = "secp224k1" |
| 339 |
secp224k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D", 16) |
| 340 |
secp224k1.N, _ = new(big.Int).SetString("010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7", 16) |
| 341 |
secp224k1.B, _ = new(big.Int).SetString("00000000000000000000000000000000000000000000000000000005", 16) |
| 342 |
secp224k1.Gx, _ = new(big.Int).SetString("A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C", 16) |
| 343 |
secp224k1.Gy, _ = new(big.Int).SetString("7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5", 16) |
| 344 |
secp224k1.BitSize = 224 |
| 345 |
} |
| 346 |
|
| 347 |
func initS256() { |
| 348 |
// See SEC 2 section 2.7.1 |
| 349 |
secp256k1 = new(BitCurve) |
| 350 |
secp256k1.Name = "secp256k1" |
| 351 |
secp256k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16) |
| 352 |
secp256k1.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16) |
| 353 |
secp256k1.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000000000000000000000000000007", 16) |
| 354 |
secp256k1.Gx, _ = new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16) |
| 355 |
secp256k1.Gy, _ = new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16) |
| 356 |
secp256k1.BitSize = 256 |
| 357 |
} |
| 358 |
|
| 359 |
// S160 returns a BitCurve which implements secp160k1 (see SEC 2 section 2.4.1) |
| 360 |
func S160() *BitCurve { |
| 361 |
initonce.Do(initAll) |
| 362 |
return secp160k1 |
| 363 |
} |
| 364 |
|
| 365 |
// S192 returns a BitCurve which implements secp192k1 (see SEC 2 section 2.5.1) |
| 366 |
func S192() *BitCurve { |
| 367 |
initonce.Do(initAll) |
| 368 |
return secp192k1 |
| 369 |
} |
| 370 |
|
| 371 |
// S224 returns a BitCurve which implements secp224k1 (see SEC 2 section 2.6.1) |
| 372 |
func S224() *BitCurve { |
| 373 |
initonce.Do(initAll) |
| 374 |
return secp224k1 |
| 375 |
} |
| 376 |
|
| 377 |
// S256 returns a BitCurve which implements bitcurves (see SEC 2 section 2.7.1) |
| 378 |
func S256() *BitCurve { |
| 379 |
initonce.Do(initAll) |
| 380 |
return secp256k1 |
| 381 |
} |