GoogleGit

blob: ba673f80ca6f9454180bb06c56ee41e6a7a6c555 [file] [log] [blame]
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package elliptic implements several standard elliptic curves over prime
  5. // fields.
  6. package elliptic
  7. // This package operates, internally, on Jacobian coordinates. For a given
  8. // (x, y) position on the curve, the Jacobian coordinates are (x1, y1, z1)
  9. // where x = x1/z1² and y = y1/z1³. The greatest speedups come when the whole
  10. // calculation can be performed within the transform (as in ScalarMult and
  11. // ScalarBaseMult). But even for Add and Double, it's faster to apply and
  12. // reverse the transform than to operate in affine coordinates.
  13. import (
  14. "io"
  15. "math/big"
  16. "sync"
  17. )
  18. // A Curve represents a short-form Weierstrass curve with a=-3.
  19. // See http://www.hyperelliptic.org/EFD/g1p/auto-shortw.html
  20. type Curve interface {
  21. // Params returns the parameters for the curve.
  22. Params() *CurveParams
  23. // IsOnCurve returns true if the given (x,y) lies on the curve.
  24. IsOnCurve(x, y *big.Int) bool
  25. // Add returns the sum of (x1,y1) and (x2,y2)
  26. Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int)
  27. // Double returns 2*(x,y)
  28. Double(x1, y1 *big.Int) (x, y *big.Int)
  29. // ScalarMult returns k*(Bx,By) where k is a number in big-endian form.
  30. ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int)
  31. // ScalarBaseMult returns k*G, where G is the base point of the group
  32. // and k is an integer in big-endian form.
  33. ScalarBaseMult(k []byte) (x, y *big.Int)
  34. }
  35. // CurveParams contains the parameters of an elliptic curve and also provides
  36. // a generic, non-constant time implementation of Curve.
  37. type CurveParams struct {
  38. P *big.Int // the order of the underlying field
  39. N *big.Int // the order of the base point
  40. B *big.Int // the constant of the curve equation
  41. Gx, Gy *big.Int // (x,y) of the base point
  42. BitSize int // the size of the underlying field
  43. }
  44. func (curve *CurveParams) Params() *CurveParams {
  45. return curve
  46. }
  47. func (curve *CurveParams) IsOnCurve(x, y *big.Int) bool {
  48. // y² = x³ - 3x + b
  49. y2 := new(big.Int).Mul(y, y)
  50. y2.Mod(y2, curve.P)
  51. x3 := new(big.Int).Mul(x, x)
  52. x3.Mul(x3, x)
  53. threeX := new(big.Int).Lsh(x, 1)
  54. threeX.Add(threeX, x)
  55. x3.Sub(x3, threeX)
  56. x3.Add(x3, curve.B)
  57. x3.Mod(x3, curve.P)
  58. return x3.Cmp(y2) == 0
  59. }
  60. // zForAffine returns a Jacobian Z value for the affine point (x, y). If x and
  61. // y are zero, it assumes that they represent the point at infinity because (0,
  62. // 0) is not on the any of the curves handled here.
  63. func zForAffine(x, y *big.Int) *big.Int {
  64. z := new(big.Int)
  65. if x.Sign() != 0 || y.Sign() != 0 {
  66. z.SetInt64(1)
  67. }
  68. return z
  69. }
  70. // affineFromJacobian reverses the Jacobian transform. See the comment at the
  71. // top of the file. If the point is ∞ it returns 0, 0.
  72. func (curve *CurveParams) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {
  73. if z.Sign() == 0 {
  74. return new(big.Int), new(big.Int)
  75. }
  76. zinv := new(big.Int).ModInverse(z, curve.P)
  77. zinvsq := new(big.Int).Mul(zinv, zinv)
  78. xOut = new(big.Int).Mul(x, zinvsq)
  79. xOut.Mod(xOut, curve.P)
  80. zinvsq.Mul(zinvsq, zinv)
  81. yOut = new(big.Int).Mul(y, zinvsq)
  82. yOut.Mod(yOut, curve.P)
  83. return
  84. }
  85. func (curve *CurveParams) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
  86. z1 := zForAffine(x1, y1)
  87. z2 := zForAffine(x2, y2)
  88. return curve.affineFromJacobian(curve.addJacobian(x1, y1, z1, x2, y2, z2))
  89. }
  90. // addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and
  91. // (x2, y2, z2) and returns their sum, also in Jacobian form.
  92. func (curve *CurveParams) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) {
  93. // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl
  94. x3, y3, z3 := new(big.Int), new(big.Int), new(big.Int)
  95. if z1.Sign() == 0 {
  96. x3.Set(x2)
  97. y3.Set(y2)
  98. z3.Set(z2)
  99. return x3, y3, z3
  100. }
  101. if z2.Sign() == 0 {
  102. x3.Set(x1)
  103. y3.Set(y1)
  104. z3.Set(z1)
  105. return x3, y3, z3
  106. }
  107. z1z1 := new(big.Int).Mul(z1, z1)
  108. z1z1.Mod(z1z1, curve.P)
  109. z2z2 := new(big.Int).Mul(z2, z2)
  110. z2z2.Mod(z2z2, curve.P)
  111. u1 := new(big.Int).Mul(x1, z2z2)
  112. u1.Mod(u1, curve.P)
  113. u2 := new(big.Int).Mul(x2, z1z1)
  114. u2.Mod(u2, curve.P)
  115. h := new(big.Int).Sub(u2, u1)
  116. xEqual := h.Sign() == 0
  117. if h.Sign() == -1 {
  118. h.Add(h, curve.P)
  119. }
  120. i := new(big.Int).Lsh(h, 1)
  121. i.Mul(i, i)
  122. j := new(big.Int).Mul(h, i)
  123. s1 := new(big.Int).Mul(y1, z2)
  124. s1.Mul(s1, z2z2)
  125. s1.Mod(s1, curve.P)
  126. s2 := new(big.Int).Mul(y2, z1)
  127. s2.Mul(s2, z1z1)
  128. s2.Mod(s2, curve.P)
  129. r := new(big.Int).Sub(s2, s1)
  130. if r.Sign() == -1 {
  131. r.Add(r, curve.P)
  132. }
  133. yEqual := r.Sign() == 0
  134. if xEqual && yEqual {
  135. return curve.doubleJacobian(x1, y1, z1)
  136. }
  137. r.Lsh(r, 1)
  138. v := new(big.Int).Mul(u1, i)
  139. x3.Set(r)
  140. x3.Mul(x3, x3)
  141. x3.Sub(x3, j)
  142. x3.Sub(x3, v)
  143. x3.Sub(x3, v)
  144. x3.Mod(x3, curve.P)
  145. y3.Set(r)
  146. v.Sub(v, x3)
  147. y3.Mul(y3, v)
  148. s1.Mul(s1, j)
  149. s1.Lsh(s1, 1)
  150. y3.Sub(y3, s1)
  151. y3.Mod(y3, curve.P)
  152. z3.Add(z1, z2)
  153. z3.Mul(z3, z3)
  154. z3.Sub(z3, z1z1)
  155. z3.Sub(z3, z2z2)
  156. z3.Mul(z3, h)
  157. z3.Mod(z3, curve.P)
  158. return x3, y3, z3
  159. }
  160. func (curve *CurveParams) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
  161. z1 := zForAffine(x1, y1)
  162. return curve.affineFromJacobian(curve.doubleJacobian(x1, y1, z1))
  163. }
  164. // doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and
  165. // returns its double, also in Jacobian form.
  166. func (curve *CurveParams) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) {
  167. // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
  168. delta := new(big.Int).Mul(z, z)
  169. delta.Mod(delta, curve.P)
  170. gamma := new(big.Int).Mul(y, y)
  171. gamma.Mod(gamma, curve.P)
  172. alpha := new(big.Int).Sub(x, delta)
  173. if alpha.Sign() == -1 {
  174. alpha.Add(alpha, curve.P)
  175. }
  176. alpha2 := new(big.Int).Add(x, delta)
  177. alpha.Mul(alpha, alpha2)
  178. alpha2.Set(alpha)
  179. alpha.Lsh(alpha, 1)
  180. alpha.Add(alpha, alpha2)
  181. beta := alpha2.Mul(x, gamma)
  182. x3 := new(big.Int).Mul(alpha, alpha)
  183. beta8 := new(big.Int).Lsh(beta, 3)
  184. x3.Sub(x3, beta8)
  185. for x3.Sign() == -1 {
  186. x3.Add(x3, curve.P)
  187. }
  188. x3.Mod(x3, curve.P)
  189. z3 := new(big.Int).Add(y, z)
  190. z3.Mul(z3, z3)
  191. z3.Sub(z3, gamma)
  192. if z3.Sign() == -1 {
  193. z3.Add(z3, curve.P)
  194. }
  195. z3.Sub(z3, delta)
  196. if z3.Sign() == -1 {
  197. z3.Add(z3, curve.P)
  198. }
  199. z3.Mod(z3, curve.P)
  200. beta.Lsh(beta, 2)
  201. beta.Sub(beta, x3)
  202. if beta.Sign() == -1 {
  203. beta.Add(beta, curve.P)
  204. }
  205. y3 := alpha.Mul(alpha, beta)
  206. gamma.Mul(gamma, gamma)
  207. gamma.Lsh(gamma, 3)
  208. gamma.Mod(gamma, curve.P)
  209. y3.Sub(y3, gamma)
  210. if y3.Sign() == -1 {
  211. y3.Add(y3, curve.P)
  212. }
  213. y3.Mod(y3, curve.P)
  214. return x3, y3, z3
  215. }
  216. func (curve *CurveParams) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int) {
  217. Bz := new(big.Int).SetInt64(1)
  218. x, y, z := new(big.Int), new(big.Int), new(big.Int)
  219. for _, byte := range k {
  220. for bitNum := 0; bitNum < 8; bitNum++ {
  221. x, y, z = curve.doubleJacobian(x, y, z)
  222. if byte&0x80 == 0x80 {
  223. x, y, z = curve.addJacobian(Bx, By, Bz, x, y, z)
  224. }
  225. byte <<= 1
  226. }
  227. }
  228. return curve.affineFromJacobian(x, y, z)
  229. }
  230. func (curve *CurveParams) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
  231. return curve.ScalarMult(curve.Gx, curve.Gy, k)
  232. }
  233. var mask = []byte{0xff, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f}
  234. // GenerateKey returns a public/private key pair. The private key is
  235. // generated using the given reader, which must return random data.
  236. func GenerateKey(curve Curve, rand io.Reader) (priv []byte, x, y *big.Int, err error) {
  237. bitSize := curve.Params().BitSize
  238. byteLen := (bitSize + 7) >> 3
  239. priv = make([]byte, byteLen)
  240. for x == nil {
  241. _, err = io.ReadFull(rand, priv)
  242. if err != nil {
  243. return
  244. }
  245. // We have to mask off any excess bits in the case that the size of the
  246. // underlying field is not a whole number of bytes.
  247. priv[0] &= mask[bitSize%8]
  248. // This is because, in tests, rand will return all zeros and we don't
  249. // want to get the point at infinity and loop forever.
  250. priv[1] ^= 0x42
  251. x, y = curve.ScalarBaseMult(priv)
  252. }
  253. return
  254. }
  255. // Marshal converts a point into the form specified in section 4.3.6 of ANSI X9.62.
  256. func Marshal(curve Curve, x, y *big.Int) []byte {
  257. byteLen := (curve.Params().BitSize + 7) >> 3
  258. ret := make([]byte, 1+2*byteLen)
  259. ret[0] = 4 // uncompressed point
  260. xBytes := x.Bytes()
  261. copy(ret[1+byteLen-len(xBytes):], xBytes)
  262. yBytes := y.Bytes()
  263. copy(ret[1+2*byteLen-len(yBytes):], yBytes)
  264. return ret
  265. }
  266. // Unmarshal converts a point, serialized by Marshal, into an x, y pair. On error, x = nil.
  267. func Unmarshal(curve Curve, data []byte) (x, y *big.Int) {
  268. byteLen := (curve.Params().BitSize + 7) >> 3
  269. if len(data) != 1+2*byteLen {
  270. return
  271. }
  272. if data[0] != 4 { // uncompressed form
  273. return
  274. }
  275. x = new(big.Int).SetBytes(data[1 : 1+byteLen])
  276. y = new(big.Int).SetBytes(data[1+byteLen:])
  277. return
  278. }
  279. var initonce sync.Once
  280. var p384 *CurveParams
  281. var p521 *CurveParams
  282. func initAll() {
  283. initP224()
  284. initP256()
  285. initP384()
  286. initP521()
  287. }
  288. func initP384() {
  289. // See FIPS 186-3, section D.2.4
  290. p384 = new(CurveParams)
  291. p384.P, _ = new(big.Int).SetString("39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319", 10)
  292. p384.N, _ = new(big.Int).SetString("39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643", 10)
  293. p384.B, _ = new(big.Int).SetString("b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef", 16)
  294. p384.Gx, _ = new(big.Int).SetString("aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7", 16)
  295. p384.Gy, _ = new(big.Int).SetString("3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f", 16)
  296. p384.BitSize = 384
  297. }
  298. func initP521() {
  299. // See FIPS 186-3, section D.2.5
  300. p521 = new(CurveParams)
  301. p521.P, _ = new(big.Int).SetString("6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151", 10)
  302. p521.N, _ = new(big.Int).SetString("6864797660130609714981900799081393217269435300143305409394463459185543183397655394245057746333217197532963996371363321113864768612440380340372808892707005449", 10)
  303. p521.B, _ = new(big.Int).SetString("051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", 16)
  304. p521.Gx, _ = new(big.Int).SetString("c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", 16)
  305. p521.Gy, _ = new(big.Int).SetString("11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", 16)
  306. p521.BitSize = 521
  307. }
  308. // P256 returns a Curve which implements P-256 (see FIPS 186-3, section D.2.3)
  309. func P256() Curve {
  310. initonce.Do(initAll)
  311. return p256
  312. }
  313. // P384 returns a Curve which implements P-384 (see FIPS 186-3, section D.2.4)
  314. func P384() Curve {
  315. initonce.Do(initAll)
  316. return p384
  317. }
  318. // P521 returns a Curve which implements P-521 (see FIPS 186-3, section D.2.5)
  319. func P521() Curve {
  320. initonce.Do(initAll)
  321. return p521
  322. }