curve.go 11 KB

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