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