github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/crypto/secp256k1/curve.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package secp256k1 19 20 import ( 21 "crypto/elliptic" 22 "math/big" 23 "unsafe" 24 ) 25 26 /* 27 #include "libsecp256k1/include/secp256k1.h" 28 extern int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, const unsigned char *point, const unsigned char *scalar); 29 */ 30 import "C" 31 32 const ( 33 // number of bits in a big.Word 34 wordBits = 32 << (uint64(^big.Word(0)) >> 63) 35 // number of bytes in a big.Word 36 wordBytes = wordBits / 8 37 ) 38 39 // readBits encodes the absolute value of bigint as big-endian bytes. Callers 40 // must ensure that buf has enough space. If buf is too short the result will 41 // be incomplete. 42 func readBits(bigint *big.Int, buf []byte) { 43 i := len(buf) 44 for _, d := range bigint.Bits() { 45 for j := 0; j < wordBytes && i > 0; j++ { 46 i-- 47 buf[i] = byte(d) 48 d >>= 8 49 } 50 } 51 } 52 53 // This code is from https://github.com/ThePiachu/GoBit and implements 54 // several Koblitz elliptic curves over prime fields. 55 // 56 // The curve methods, internally, on Jacobian coordinates. For a given 57 // (x, y) position on the curve, the Jacobian coordinates are (x1, y1, 58 // z1) where x = x1/z1² and y = y1/z1³. The greatest speedups come 59 // when the whole calculation can be performed within the transform 60 // (as in ScalarMult and ScalarBaseMult). But even for Add and Double, 61 // it's faster to apply and reverse the transform than to operate in 62 // affine coordinates. 63 64 // A BitCurve represents a Koblitz Curve with a=0. 65 // See http://www.hyperelliptic.org/EFD/g1p/auto-shortw.html 66 type BitCurve struct { 67 P *big.Int // the order of the underlying field 68 N *big.Int // the order of the base point 69 B *big.Int // the constant of the BitCurve equation 70 Gx, Gy *big.Int // (x,y) of the base point 71 BitSize int // the size of the underlying field 72 } 73 74 func (BitCurve *BitCurve) Params() *elliptic.CurveParams { 75 return &elliptic.CurveParams{ 76 P: BitCurve.P, 77 N: BitCurve.N, 78 B: BitCurve.B, 79 Gx: BitCurve.Gx, 80 Gy: BitCurve.Gy, 81 BitSize: BitCurve.BitSize, 82 } 83 } 84 85 // IsOnCurve returns true if the given (x,y) lies on the BitCurve. 86 func (BitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool { 87 // y² = x³ + b 88 y2 := new(big.Int).Mul(y, y) //y² 89 y2.Mod(y2, BitCurve.P) //y²%P 90 91 x3 := new(big.Int).Mul(x, x) //x² 92 x3.Mul(x3, x) //x³ 93 94 x3.Add(x3, BitCurve.B) //x³+B 95 x3.Mod(x3, BitCurve.P) //(x³+B)%P 96 97 return x3.Cmp(y2) == 0 98 } 99 100 //TODO: double check if the function is okay 101 // affineFromJacobian reverses the Jacobian transform. See the comment at the 102 // top of the file. 103 func (BitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) { 104 zinv := new(big.Int).ModInverse(z, BitCurve.P) 105 zinvsq := new(big.Int).Mul(zinv, zinv) 106 107 xOut = new(big.Int).Mul(x, zinvsq) 108 xOut.Mod(xOut, BitCurve.P) 109 zinvsq.Mul(zinvsq, zinv) 110 yOut = new(big.Int).Mul(y, zinvsq) 111 yOut.Mod(yOut, BitCurve.P) 112 return 113 } 114 115 // Add returns the sum of (x1,y1) and (x2,y2) 116 func (BitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) { 117 z := new(big.Int).SetInt64(1) 118 return BitCurve.affineFromJacobian(BitCurve.addJacobian(x1, y1, z, x2, y2, z)) 119 } 120 121 // addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and 122 // (x2, y2, z2) and returns their sum, also in Jacobian form. 123 func (BitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) { 124 // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl 125 z1z1 := new(big.Int).Mul(z1, z1) 126 z1z1.Mod(z1z1, BitCurve.P) 127 z2z2 := new(big.Int).Mul(z2, z2) 128 z2z2.Mod(z2z2, BitCurve.P) 129 130 u1 := new(big.Int).Mul(x1, z2z2) 131 u1.Mod(u1, BitCurve.P) 132 u2 := new(big.Int).Mul(x2, z1z1) 133 u2.Mod(u2, BitCurve.P) 134 h := new(big.Int).Sub(u2, u1) 135 if h.Sign() == -1 { 136 h.Add(h, BitCurve.P) 137 } 138 i := new(big.Int).Lsh(h, 1) 139 i.Mul(i, i) 140 j := new(big.Int).Mul(h, i) 141 142 s1 := new(big.Int).Mul(y1, z2) 143 s1.Mul(s1, z2z2) 144 s1.Mod(s1, BitCurve.P) 145 s2 := new(big.Int).Mul(y2, z1) 146 s2.Mul(s2, z1z1) 147 s2.Mod(s2, BitCurve.P) 148 r := new(big.Int).Sub(s2, s1) 149 if r.Sign() == -1 { 150 r.Add(r, BitCurve.P) 151 } 152 r.Lsh(r, 1) 153 v := new(big.Int).Mul(u1, i) 154 155 x3 := new(big.Int).Set(r) 156 x3.Mul(x3, x3) 157 x3.Sub(x3, j) 158 x3.Sub(x3, v) 159 x3.Sub(x3, v) 160 x3.Mod(x3, BitCurve.P) 161 162 y3 := new(big.Int).Set(r) 163 v.Sub(v, x3) 164 y3.Mul(y3, v) 165 s1.Mul(s1, j) 166 s1.Lsh(s1, 1) 167 y3.Sub(y3, s1) 168 y3.Mod(y3, BitCurve.P) 169 170 z3 := new(big.Int).Add(z1, z2) 171 z3.Mul(z3, z3) 172 z3.Sub(z3, z1z1) 173 if z3.Sign() == -1 { 174 z3.Add(z3, BitCurve.P) 175 } 176 z3.Sub(z3, z2z2) 177 if z3.Sign() == -1 { 178 z3.Add(z3, BitCurve.P) 179 } 180 z3.Mul(z3, h) 181 z3.Mod(z3, BitCurve.P) 182 183 return x3, y3, z3 184 } 185 186 // Double returns 2*(x,y) 187 func (BitCurve *BitCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) { 188 z1 := new(big.Int).SetInt64(1) 189 return BitCurve.affineFromJacobian(BitCurve.doubleJacobian(x1, y1, z1)) 190 } 191 192 // doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and 193 // returns its double, also in Jacobian form. 194 func (BitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) { 195 // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l 196 197 a := new(big.Int).Mul(x, x) //X1² 198 b := new(big.Int).Mul(y, y) //Y1² 199 c := new(big.Int).Mul(b, b) //B² 200 201 d := new(big.Int).Add(x, b) //X1+B 202 d.Mul(d, d) //(X1+B)² 203 d.Sub(d, a) //(X1+B)²-A 204 d.Sub(d, c) //(X1+B)²-A-C 205 d.Mul(d, big.NewInt(2)) //2*((X1+B)²-A-C) 206 207 e := new(big.Int).Mul(big.NewInt(3), a) //3*A 208 f := new(big.Int).Mul(e, e) //E² 209 210 x3 := new(big.Int).Mul(big.NewInt(2), d) //2*D 211 x3.Sub(f, x3) //F-2*D 212 x3.Mod(x3, BitCurve.P) 213 214 y3 := new(big.Int).Sub(d, x3) //D-X3 215 y3.Mul(e, y3) //E*(D-X3) 216 y3.Sub(y3, new(big.Int).Mul(big.NewInt(8), c)) //E*(D-X3)-8*C 217 y3.Mod(y3, BitCurve.P) 218 219 z3 := new(big.Int).Mul(y, z) //Y1*Z1 220 z3.Mul(big.NewInt(2), z3) //3*Y1*Z1 221 z3.Mod(z3, BitCurve.P) 222 223 return x3, y3, z3 224 } 225 226 func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) { 227 // Ensure scalar is exactly 32 bytes. We pad always, even if 228 // scalar is 32 bytes long, to avoid a timing side channel. 229 if len(scalar) > 32 { 230 panic("can't handle scalars > 256 bits") 231 } 232 // NOTE: potential timing issue 233 padded := make([]byte, 32) 234 copy(padded[32-len(scalar):], scalar) 235 scalar = padded 236 237 // Do the multiplication in C, updating point. 238 point := make([]byte, 64) 239 readBits(Bx, point[:32]) 240 readBits(By, point[32:]) 241 242 pointPtr := (*C.uchar)(unsafe.Pointer(&point[0])) 243 scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0])) 244 res := C.secp256k1_ext_scalar_mul(context, pointPtr, scalarPtr) 245 246 // Unpack the result and clear temporaries. 247 x := new(big.Int).SetBytes(point[:32]) 248 y := new(big.Int).SetBytes(point[32:]) 249 for i := range point { 250 point[i] = 0 251 } 252 for i := range padded { 253 scalar[i] = 0 254 } 255 if res != 1 { 256 return nil, nil 257 } 258 return x, y 259 } 260 261 // ScalarBaseMult returns k*G, where G is the base point of the group and k is 262 // an integer in big-endian form. 263 func (BitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) { 264 return BitCurve.ScalarMult(BitCurve.Gx, BitCurve.Gy, k) 265 } 266 267 // Marshal converts a point into the form specified in section 4.3.6 of ANSI 268 // X9.62. 269 func (BitCurve *BitCurve) Marshal(x, y *big.Int) []byte { 270 byteLen := (BitCurve.BitSize + 7) >> 3 271 ret := make([]byte, 1+2*byteLen) 272 ret[0] = 4 // uncompressed point flag 273 readBits(x, ret[1:1+byteLen]) 274 readBits(y, ret[1+byteLen:]) 275 return ret 276 } 277 278 // Unmarshal converts a point, serialised by Marshal, into an x, y pair. On 279 // error, x = nil. 280 func (BitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int) { 281 byteLen := (BitCurve.BitSize + 7) >> 3 282 if len(data) != 1+2*byteLen { 283 return 284 } 285 if data[0] != 4 { // uncompressed form 286 return 287 } 288 x = new(big.Int).SetBytes(data[1 : 1+byteLen]) 289 y = new(big.Int).SetBytes(data[1+byteLen:]) 290 return 291 } 292 293 var theCurve = new(BitCurve) 294 295 func init() { 296 // See SEC 2 section 2.7.1 297 // curve parameters taken from: 298 // http://www.secg.org/sec2-v2.pdf 299 theCurve.P, _ = new(big.Int).SetString("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 0) 300 theCurve.N, _ = new(big.Int).SetString("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 0) 301 theCurve.B, _ = new(big.Int).SetString("0x0000000000000000000000000000000000000000000000000000000000000007", 0) 302 theCurve.Gx, _ = new(big.Int).SetString("0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 0) 303 theCurve.Gy, _ = new(big.Int).SetString("0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 0) 304 theCurve.BitSize = 256 305 } 306 307 // S256 returns a BitCurve which implements secp256k1. 308 func S256() *BitCurve { 309 return theCurve 310 }