curve.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Copyright 2011 ThePiachu. All rights reserved.
  3. // Copyright 2015 Jeffrey Wilcke, Felix Lange, Gustav Simonsson. All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. // * The name of ThePiachu may not be used to endorse or promote products
  19. // derived from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. package secp256k1
  33. import (
  34. "crypto/elliptic"
  35. "math/big"
  36. )
  37. const (
  38. // number of bits in a big.Word
  39. wordBits = 32 << (uint64(^big.Word(0)) >> 63)
  40. // number of bytes in a big.Word
  41. wordBytes = wordBits / 8
  42. )
  43. // readBits encodes the absolute value of bigint as big-endian bytes. Callers
  44. // must ensure that buf has enough space. If buf is too short the result will
  45. // be incomplete.
  46. func readBits(bigint *big.Int, buf []byte) {
  47. i := len(buf)
  48. for _, d := range bigint.Bits() {
  49. for j := 0; j < wordBytes && i > 0; j++ {
  50. i--
  51. buf[i] = byte(d)
  52. d >>= 8
  53. }
  54. }
  55. }
  56. // This code is from https://github.com/ThePiachu/GoBit and implements
  57. // several Koblitz elliptic curves over prime fields.
  58. //
  59. // The curve methods, internally, on Jacobian coordinates. For a given
  60. // (x, y) position on the curve, the Jacobian coordinates are (x1, y1,
  61. // z1) where x = x1/z1² and y = y1/z1³. The greatest speedups come
  62. // when the whole calculation can be performed within the transform
  63. // (as in ScalarMult and ScalarBaseMult). But even for Add and Double,
  64. // it's faster to apply and reverse the transform than to operate in
  65. // affine coordinates.
  66. // A BitCurve represents a Koblitz Curve with a=0.
  67. // See http://www.hyperelliptic.org/EFD/g1p/auto-shortw.html
  68. type BitCurve struct {
  69. P *big.Int // the order of the underlying field
  70. N *big.Int // the order of the base point
  71. B *big.Int // the constant of the BitCurve equation
  72. Gx, Gy *big.Int // (x,y) of the base point
  73. BitSize int // the size of the underlying field
  74. }
  75. func (BitCurve *BitCurve) Params() *elliptic.CurveParams {
  76. return &elliptic.CurveParams{
  77. P: BitCurve.P,
  78. N: BitCurve.N,
  79. B: BitCurve.B,
  80. Gx: BitCurve.Gx,
  81. Gy: BitCurve.Gy,
  82. BitSize: BitCurve.BitSize,
  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. x3 := new(big.Int).Mul(x, x) //x²
  91. x3.Mul(x3, x) //x³
  92. x3.Add(x3, BitCurve.B) //x³+B
  93. x3.Mod(x3, BitCurve.P) //(x³+B)%P
  94. return x3.Cmp(y2) == 0
  95. }
  96. //TODO: double check if the function is okay
  97. // affineFromJacobian reverses the Jacobian transform. See the comment at the
  98. // top of the file.
  99. func (BitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {
  100. if z.Sign() == 0 {
  101. return new(big.Int), new(big.Int)
  102. }
  103. zinv := new(big.Int).ModInverse(z, BitCurve.P)
  104. zinvsq := new(big.Int).Mul(zinv, zinv)
  105. xOut = new(big.Int).Mul(x, zinvsq)
  106. xOut.Mod(xOut, BitCurve.P)
  107. zinvsq.Mul(zinvsq, zinv)
  108. yOut = new(big.Int).Mul(y, zinvsq)
  109. yOut.Mod(yOut, BitCurve.P)
  110. return
  111. }
  112. // Add returns the sum of (x1,y1) and (x2,y2)
  113. func (BitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
  114. // If one point is at infinity, return the other point.
  115. // Adding the point at infinity to any point will preserve the other point.
  116. if x1.Sign() == 0 && y1.Sign() == 0 {
  117. return x2, y2
  118. }
  119. if x2.Sign() == 0 && y2.Sign() == 0 {
  120. return x1, y1
  121. }
  122. z := new(big.Int).SetInt64(1)
  123. if x1.Cmp(x2) == 0 && y1.Cmp(y2) == 0 {
  124. return BitCurve.affineFromJacobian(BitCurve.doubleJacobian(x1, y1, z))
  125. }
  126. return BitCurve.affineFromJacobian(BitCurve.addJacobian(x1, y1, z, x2, y2, z))
  127. }
  128. // addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and
  129. // (x2, y2, z2) and returns their sum, also in Jacobian form.
  130. func (BitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) {
  131. // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
  132. z1z1 := new(big.Int).Mul(z1, z1)
  133. z1z1.Mod(z1z1, BitCurve.P)
  134. z2z2 := new(big.Int).Mul(z2, z2)
  135. z2z2.Mod(z2z2, BitCurve.P)
  136. u1 := new(big.Int).Mul(x1, z2z2)
  137. u1.Mod(u1, BitCurve.P)
  138. u2 := new(big.Int).Mul(x2, z1z1)
  139. u2.Mod(u2, BitCurve.P)
  140. h := new(big.Int).Sub(u2, u1)
  141. if h.Sign() == -1 {
  142. h.Add(h, BitCurve.P)
  143. }
  144. i := new(big.Int).Lsh(h, 1)
  145. i.Mul(i, i)
  146. j := new(big.Int).Mul(h, i)
  147. s1 := new(big.Int).Mul(y1, z2)
  148. s1.Mul(s1, z2z2)
  149. s1.Mod(s1, BitCurve.P)
  150. s2 := new(big.Int).Mul(y2, z1)
  151. s2.Mul(s2, z1z1)
  152. s2.Mod(s2, BitCurve.P)
  153. r := new(big.Int).Sub(s2, s1)
  154. if r.Sign() == -1 {
  155. r.Add(r, BitCurve.P)
  156. }
  157. r.Lsh(r, 1)
  158. v := new(big.Int).Mul(u1, i)
  159. x3 := new(big.Int).Set(r)
  160. x3.Mul(x3, x3)
  161. x3.Sub(x3, j)
  162. x3.Sub(x3, v)
  163. x3.Sub(x3, v)
  164. x3.Mod(x3, BitCurve.P)
  165. y3 := new(big.Int).Set(r)
  166. v.Sub(v, x3)
  167. y3.Mul(y3, v)
  168. s1.Mul(s1, j)
  169. s1.Lsh(s1, 1)
  170. y3.Sub(y3, s1)
  171. y3.Mod(y3, BitCurve.P)
  172. z3 := new(big.Int).Add(z1, z2)
  173. z3.Mul(z3, z3)
  174. z3.Sub(z3, z1z1)
  175. if z3.Sign() == -1 {
  176. z3.Add(z3, BitCurve.P)
  177. }
  178. z3.Sub(z3, z2z2)
  179. if z3.Sign() == -1 {
  180. z3.Add(z3, BitCurve.P)
  181. }
  182. z3.Mul(z3, h)
  183. z3.Mod(z3, BitCurve.P)
  184. return x3, y3, z3
  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. // doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and
  192. // returns its double, also in Jacobian form.
  193. func (BitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) {
  194. // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
  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. d := new(big.Int).Add(x, b) //X1+B
  199. d.Mul(d, d) //(X1+B)²
  200. d.Sub(d, a) //(X1+B)²-A
  201. d.Sub(d, c) //(X1+B)²-A-C
  202. d.Mul(d, big.NewInt(2)) //2*((X1+B)²-A-C)
  203. e := new(big.Int).Mul(big.NewInt(3), a) //3*A
  204. f := new(big.Int).Mul(e, e) //E²
  205. x3 := new(big.Int).Mul(big.NewInt(2), d) //2*D
  206. x3.Sub(f, x3) //F-2*D
  207. x3.Mod(x3, BitCurve.P)
  208. y3 := new(big.Int).Sub(d, x3) //D-X3
  209. y3.Mul(e, y3) //E*(D-X3)
  210. y3.Sub(y3, new(big.Int).Mul(big.NewInt(8), c)) //E*(D-X3)-8*C
  211. y3.Mod(y3, BitCurve.P)
  212. z3 := new(big.Int).Mul(y, z) //Y1*Z1
  213. z3.Mul(big.NewInt(2), z3) //3*Y1*Z1
  214. z3.Mod(z3, BitCurve.P)
  215. return x3, y3, z3
  216. }
  217. // ScalarBaseMult returns k*G, where G is the base point of the group and k is
  218. // an integer in big-endian form.
  219. func (BitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
  220. return BitCurve.ScalarMult(BitCurve.Gx, BitCurve.Gy, k)
  221. }
  222. // Marshal converts a point into the form specified in section 4.3.6 of ANSI
  223. // X9.62.
  224. func (BitCurve *BitCurve) Marshal(x, y *big.Int) []byte {
  225. byteLen := (BitCurve.BitSize + 7) >> 3
  226. ret := make([]byte, 1+2*byteLen)
  227. ret[0] = 4 // uncompressed point flag
  228. readBits(x, ret[1:1+byteLen])
  229. readBits(y, ret[1+byteLen:])
  230. return ret
  231. }
  232. // Unmarshal converts a point, serialised by Marshal, into an x, y pair. On
  233. // error, x = nil.
  234. func (BitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int) {
  235. byteLen := (BitCurve.BitSize + 7) >> 3
  236. if len(data) != 1+2*byteLen {
  237. return
  238. }
  239. if data[0] != 4 { // uncompressed form
  240. return
  241. }
  242. x = new(big.Int).SetBytes(data[1 : 1+byteLen])
  243. y = new(big.Int).SetBytes(data[1+byteLen:])
  244. return
  245. }
  246. var theCurve = new(BitCurve)
  247. func init() {
  248. // See SEC 2 section 2.7.1
  249. // curve parameters taken from:
  250. // http://www.secg.org/sec2-v2.pdf
  251. theCurve.P, _ = new(big.Int).SetString("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 0)
  252. theCurve.N, _ = new(big.Int).SetString("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 0)
  253. theCurve.B, _ = new(big.Int).SetString("0x0000000000000000000000000000000000000000000000000000000000000007", 0)
  254. theCurve.Gx, _ = new(big.Int).SetString("0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 0)
  255. theCurve.Gy, _ = new(big.Int).SetString("0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 0)
  256. theCurve.BitSize = 256
  257. }
  258. // S256 returns a BitCurve which implements secp256k1.
  259. func S256() *BitCurve {
  260. return theCurve
  261. }