github.com/aquanetwork/aquachain@v1.7.8/crypto/secp256k1/curve.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Copyright 2011 ThePiachu. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // * The name of ThePiachu may not be used to endorse or promote products 18 // derived from this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32 // +build gccgo cgo !nocgo 33 34 package secp256k1 35 36 import ( 37 "crypto/elliptic" 38 "math/big" 39 "unsafe" 40 41 "gitlab.com/aquachain/aquachain/common/math" 42 ) 43 44 /* 45 #include "libsecp256k1/include/secp256k1.h" 46 extern int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, const unsigned char *point, const unsigned char *scalar); 47 */ 48 import "C" 49 50 // This code is from https://github.com/ThePiachu/GoBit and implements 51 // several Koblitz elliptic curves over prime fields. 52 // 53 // The curve methods, internally, on Jacobian coordinates. For a given 54 // (x, y) position on the curve, the Jacobian coordinates are (x1, y1, 55 // z1) where x = x1/z1² and y = y1/z1³. The greatest speedups come 56 // when the whole calculation can be performed within the transform 57 // (as in ScalarMult and ScalarBaseMult). But even for Add and Double, 58 // it's faster to apply and reverse the transform than to operate in 59 // affine coordinates. 60 61 // A BitCurve represents a Koblitz Curve with a=0. 62 // See http://www.hyperelliptic.org/EFD/g1p/auto-shortw.html 63 type BitCurve struct { 64 P *big.Int // the order of the underlying field 65 N *big.Int // the order of the base point 66 B *big.Int // the constant of the BitCurve equation 67 Gx, Gy *big.Int // (x,y) of the base point 68 BitSize int // the size of the underlying field 69 } 70 71 func (BitCurve *BitCurve) Params() *elliptic.CurveParams { 72 return &elliptic.CurveParams{ 73 P: BitCurve.P, 74 N: BitCurve.N, 75 B: BitCurve.B, 76 Gx: BitCurve.Gx, 77 Gy: BitCurve.Gy, 78 BitSize: BitCurve.BitSize, 79 } 80 } 81 82 // IsOnBitCurve returns true if the given (x,y) lies on the BitCurve. 83 func (BitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool { 84 // y² = x³ + b 85 y2 := new(big.Int).Mul(y, y) //y² 86 y2.Mod(y2, BitCurve.P) //y²%P 87 88 x3 := new(big.Int).Mul(x, x) //x² 89 x3.Mul(x3, x) //x³ 90 91 x3.Add(x3, BitCurve.B) //x³+B 92 x3.Mod(x3, BitCurve.P) //(x³+B)%P 93 94 return x3.Cmp(y2) == 0 95 } 96 97 //TODO: double check if the function is okay 98 // affineFromJacobian reverses the Jacobian transform. See the comment at the 99 // top of the file. 100 func (BitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) { 101 if zinv := new(big.Int).ModInverse(z, BitCurve.P); zinv != nil { 102 zinvsq := new(big.Int).Mul(zinv, zinv) 103 104 xOut = new(big.Int).Mul(x, zinvsq) 105 xOut.Mod(xOut, BitCurve.P) 106 zinvsq.Mul(zinvsq, zinv) 107 yOut = new(big.Int).Mul(y, zinvsq) 108 yOut.Mod(yOut, BitCurve.P) 109 } 110 return 111 } 112 113 // Add returns the sum of (x1,y1) and (x2,y2) 114 func (BitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) { 115 z := new(big.Int).SetInt64(1) 116 return BitCurve.affineFromJacobian(BitCurve.addJacobian(x1, y1, z, x2, y2, z)) 117 } 118 119 // addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and 120 // (x2, y2, z2) and returns their sum, also in Jacobian form. 121 func (BitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) { 122 // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl 123 z1z1 := new(big.Int).Mul(z1, z1) 124 z1z1.Mod(z1z1, BitCurve.P) 125 z2z2 := new(big.Int).Mul(z2, z2) 126 z2z2.Mod(z2z2, BitCurve.P) 127 128 u1 := new(big.Int).Mul(x1, z2z2) 129 u1.Mod(u1, BitCurve.P) 130 u2 := new(big.Int).Mul(x2, z1z1) 131 u2.Mod(u2, BitCurve.P) 132 h := new(big.Int).Sub(u2, u1) 133 if h.Sign() == -1 { 134 h.Add(h, BitCurve.P) 135 } 136 i := new(big.Int).Lsh(h, 1) 137 i.Mul(i, i) 138 j := new(big.Int).Mul(h, i) 139 140 s1 := new(big.Int).Mul(y1, z2) 141 s1.Mul(s1, z2z2) 142 s1.Mod(s1, BitCurve.P) 143 s2 := new(big.Int).Mul(y2, z1) 144 s2.Mul(s2, z1z1) 145 s2.Mod(s2, BitCurve.P) 146 r := new(big.Int).Sub(s2, s1) 147 if r.Sign() == -1 { 148 r.Add(r, BitCurve.P) 149 } 150 r.Lsh(r, 1) 151 v := new(big.Int).Mul(u1, i) 152 153 x3 := new(big.Int).Set(r) 154 x3.Mul(x3, x3) 155 x3.Sub(x3, j) 156 x3.Sub(x3, v) 157 x3.Sub(x3, v) 158 x3.Mod(x3, BitCurve.P) 159 160 y3 := new(big.Int).Set(r) 161 v.Sub(v, x3) 162 y3.Mul(y3, v) 163 s1.Mul(s1, j) 164 s1.Lsh(s1, 1) 165 y3.Sub(y3, s1) 166 y3.Mod(y3, BitCurve.P) 167 168 z3 := new(big.Int).Add(z1, z2) 169 z3.Mul(z3, z3) 170 z3.Sub(z3, z1z1) 171 if z3.Sign() == -1 { 172 z3.Add(z3, BitCurve.P) 173 } 174 z3.Sub(z3, z2z2) 175 if z3.Sign() == -1 { 176 z3.Add(z3, BitCurve.P) 177 } 178 z3.Mul(z3, h) 179 z3.Mod(z3, BitCurve.P) 180 181 return x3, y3, z3 182 } 183 184 // Double returns 2*(x,y) 185 func (BitCurve *BitCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) { 186 z1 := new(big.Int).SetInt64(1) 187 return BitCurve.affineFromJacobian(BitCurve.doubleJacobian(x1, y1, z1)) 188 } 189 190 // doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and 191 // returns its double, also in Jacobian form. 192 func (BitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) { 193 // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l 194 195 a := new(big.Int).Mul(x, x) //X1² 196 b := new(big.Int).Mul(y, y) //Y1² 197 c := new(big.Int).Mul(b, b) //B² 198 199 d := new(big.Int).Add(x, b) //X1+B 200 d.Mul(d, d) //(X1+B)² 201 d.Sub(d, a) //(X1+B)²-A 202 d.Sub(d, c) //(X1+B)²-A-C 203 d.Mul(d, big.NewInt(2)) //2*((X1+B)²-A-C) 204 205 e := new(big.Int).Mul(big.NewInt(3), a) //3*A 206 f := new(big.Int).Mul(e, e) //E² 207 208 x3 := new(big.Int).Mul(big.NewInt(2), d) //2*D 209 x3.Sub(f, x3) //F-2*D 210 x3.Mod(x3, BitCurve.P) 211 212 y3 := new(big.Int).Sub(d, x3) //D-X3 213 y3.Mul(e, y3) //E*(D-X3) 214 y3.Sub(y3, new(big.Int).Mul(big.NewInt(8), c)) //E*(D-X3)-8*C 215 y3.Mod(y3, BitCurve.P) 216 217 z3 := new(big.Int).Mul(y, z) //Y1*Z1 218 z3.Mul(big.NewInt(2), z3) //3*Y1*Z1 219 z3.Mod(z3, BitCurve.P) 220 221 return x3, y3, z3 222 } 223 224 func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) { 225 // Ensure scalar is exactly 32 bytes. We pad always, even if 226 // scalar is 32 bytes long, to avoid a timing side channel. 227 if len(scalar) > 32 { 228 panic("can't handle scalars > 256 bits") 229 } 230 // NOTE: potential timing issue 231 padded := make([]byte, 32) 232 copy(padded[32-len(scalar):], scalar) 233 scalar = padded 234 235 // Do the multiplication in C, updating point. 236 point := make([]byte, 64) 237 math.ReadBits(Bx, point[:32]) 238 math.ReadBits(By, point[32:]) 239 pointPtr := (*C.uchar)(unsafe.Pointer(&point[0])) 240 scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0])) 241 res := C.secp256k1_ext_scalar_mul(context, pointPtr, scalarPtr) 242 243 // Unpack the result and clear temporaries. 244 x := new(big.Int).SetBytes(point[:32]) 245 y := new(big.Int).SetBytes(point[32:]) 246 for i := range point { 247 point[i] = 0 248 } 249 for i := range padded { 250 scalar[i] = 0 251 } 252 if res != 1 { 253 return nil, nil 254 } 255 return x, y 256 } 257 258 // ScalarBaseMult returns k*G, where G is the base point of the group and k is 259 // an integer in big-endian form. 260 func (BitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) { 261 return BitCurve.ScalarMult(BitCurve.Gx, BitCurve.Gy, k) 262 } 263 264 // Marshal converts a point into the form specified in section 4.3.6 of ANSI 265 // X9.62. 266 func (BitCurve *BitCurve) Marshal(x, y *big.Int) []byte { 267 byteLen := (BitCurve.BitSize + 7) >> 3 268 ret := make([]byte, 1+2*byteLen) 269 ret[0] = 4 // uncompressed point flag 270 math.ReadBits(x, ret[1:1+byteLen]) 271 math.ReadBits(y, ret[1+byteLen:]) 272 return ret 273 } 274 275 // Unmarshal converts a point, serialised by Marshal, into an x, y pair. On 276 // error, x = nil. 277 func (BitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int) { 278 byteLen := (BitCurve.BitSize + 7) >> 3 279 if len(data) != 1+2*byteLen { 280 return 281 } 282 if data[0] != 4 { // uncompressed form 283 return 284 } 285 x = new(big.Int).SetBytes(data[1 : 1+byteLen]) 286 y = new(big.Int).SetBytes(data[1+byteLen:]) 287 return 288 } 289 290 var theCurve = new(BitCurve) 291 292 func init() { 293 // See SEC 2 section 2.7.1 294 // curve parameters taken from: 295 // http://www.secg.org/collateral/sec2_final.pdf 296 theCurve.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16) 297 theCurve.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16) 298 theCurve.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000000000000000000000000000007", 16) 299 theCurve.Gx, _ = new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16) 300 theCurve.Gy, _ = new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16) 301 theCurve.BitSize = 256 302 } 303 304 // S256 returns a BitCurve which implements secp256k1. 305 func S256() *BitCurve { 306 return theCurve 307 }