btcec.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Copyright 2011 ThePiachu. All rights reserved.
  3. // Copyright 2013-2014 The btcsuite developers
  4. // Use of this source code is governed by an ISC
  5. // license that can be found in the LICENSE file.
  6. package btcec
  7. // References:
  8. // [SECG]: Recommended Elliptic Curve Domain Parameters
  9. // http://www.secg.org/sec2-v2.pdf
  10. //
  11. // [GECC]: Guide to Elliptic Curve Cryptography (Hankerson, Menezes, Vanstone)
  12. // This package operates, internally, on Jacobian coordinates. For a given
  13. // (x, y) position on the curve, the Jacobian coordinates are (x1, y1, z1)
  14. // where x = x1/z1² and y = y1/z1³. The greatest speedups come when the whole
  15. // calculation can be performed within the transform (as in ScalarMult and
  16. // ScalarBaseMult). But even for Add and Double, it's faster to apply and
  17. // reverse the transform than to operate in affine coordinates.
  18. import (
  19. "crypto/elliptic"
  20. "math/big"
  21. "sync"
  22. )
  23. var (
  24. // fieldOne is simply the integer 1 in field representation. It is
  25. // used to avoid needing to create it multiple times during the internal
  26. // arithmetic.
  27. fieldOne = new(fieldVal).SetInt(1)
  28. )
  29. // KoblitzCurve supports a koblitz curve implementation that fits the ECC Curve
  30. // interface from crypto/elliptic.
  31. type KoblitzCurve struct {
  32. *elliptic.CurveParams
  33. q *big.Int
  34. H int // cofactor of the curve.
  35. halfOrder *big.Int // half the order N
  36. // byteSize is simply the bit size / 8 and is provided for convenience
  37. // since it is calculated repeatedly.
  38. byteSize int
  39. // bytePoints
  40. bytePoints *[32][256][3]fieldVal
  41. // The next 6 values are used specifically for endomorphism
  42. // optimizations in ScalarMult.
  43. // lambda must fulfill lambda^3 = 1 mod N where N is the order of G.
  44. lambda *big.Int
  45. // beta must fulfill beta^3 = 1 mod P where P is the prime field of the
  46. // curve.
  47. beta *fieldVal
  48. // See the EndomorphismVectors in gensecp256k1.go to see how these are
  49. // derived.
  50. a1 *big.Int
  51. b1 *big.Int
  52. a2 *big.Int
  53. b2 *big.Int
  54. }
  55. // Params returns the parameters for the curve.
  56. func (curve *KoblitzCurve) Params() *elliptic.CurveParams {
  57. return curve.CurveParams
  58. }
  59. // bigAffineToField takes an affine point (x, y) as big integers and converts
  60. // it to an affine point as field values.
  61. func (curve *KoblitzCurve) bigAffineToField(x, y *big.Int) (*fieldVal, *fieldVal) {
  62. x3, y3 := new(fieldVal), new(fieldVal)
  63. x3.SetByteSlice(x.Bytes())
  64. y3.SetByteSlice(y.Bytes())
  65. return x3, y3
  66. }
  67. // fieldJacobianToBigAffine takes a Jacobian point (x, y, z) as field values and
  68. // converts it to an affine point as big integers.
  69. func (curve *KoblitzCurve) fieldJacobianToBigAffine(x, y, z *fieldVal) (*big.Int, *big.Int) {
  70. // Inversions are expensive and both point addition and point doubling
  71. // are faster when working with points that have a z value of one. So,
  72. // if the point needs to be converted to affine, go ahead and normalize
  73. // the point itself at the same time as the calculation is the same.
  74. var zInv, tempZ fieldVal
  75. zInv.Set(z).Inverse() // zInv = Z^-1
  76. tempZ.SquareVal(&zInv) // tempZ = Z^-2
  77. x.Mul(&tempZ) // X = X/Z^2 (mag: 1)
  78. y.Mul(tempZ.Mul(&zInv)) // Y = Y/Z^3 (mag: 1)
  79. z.SetInt(1) // Z = 1 (mag: 1)
  80. // Normalize the x and y values.
  81. x.Normalize()
  82. y.Normalize()
  83. // Convert the field values for the now affine point to big.Ints.
  84. x3, y3 := new(big.Int), new(big.Int)
  85. x3.SetBytes(x.Bytes()[:])
  86. y3.SetBytes(y.Bytes()[:])
  87. return x3, y3
  88. }
  89. // IsOnCurve returns boolean if the point (x,y) is on the curve.
  90. // Part of the elliptic.Curve interface. This function differs from the
  91. // crypto/elliptic algorithm since a = 0 not -3.
  92. func (curve *KoblitzCurve) IsOnCurve(x, y *big.Int) bool {
  93. // Convert big ints to field values for faster arithmetic.
  94. fx, fy := curve.bigAffineToField(x, y)
  95. // Elliptic curve equation for secp256k1 is: y^2 = x^3 + 7
  96. y2 := new(fieldVal).SquareVal(fy).Normalize()
  97. result := new(fieldVal).SquareVal(fx).Mul(fx).AddInt(7).Normalize()
  98. return y2.Equals(result)
  99. }
  100. // addZ1AndZ2EqualsOne adds two Jacobian points that are already known to have
  101. // z values of 1 and stores the result in (x3, y3, z3). That is to say
  102. // (x1, y1, 1) + (x2, y2, 1) = (x3, y3, z3). It performs faster addition than
  103. // the generic add routine since less arithmetic is needed due to the ability to
  104. // avoid the z value multiplications.
  105. func (curve *KoblitzCurve) addZ1AndZ2EqualsOne(x1, y1, z1, x2, y2, x3, y3, z3 *fieldVal) {
  106. // To compute the point addition efficiently, this implementation splits
  107. // the equation into intermediate elements which are used to minimize
  108. // the number of field multiplications using the method shown at:
  109. // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-mmadd-2007-bl
  110. //
  111. // In particular it performs the calculations using the following:
  112. // H = X2-X1, HH = H^2, I = 4*HH, J = H*I, r = 2*(Y2-Y1), V = X1*I
  113. // X3 = r^2-J-2*V, Y3 = r*(V-X3)-2*Y1*J, Z3 = 2*H
  114. //
  115. // This results in a cost of 4 field multiplications, 2 field squarings,
  116. // 6 field additions, and 5 integer multiplications.
  117. // When the x coordinates are the same for two points on the curve, the
  118. // y coordinates either must be the same, in which case it is point
  119. // doubling, or they are opposite and the result is the point at
  120. // infinity per the group law for elliptic curve cryptography.
  121. x1.Normalize()
  122. y1.Normalize()
  123. x2.Normalize()
  124. y2.Normalize()
  125. if x1.Equals(x2) {
  126. if y1.Equals(y2) {
  127. // Since x1 == x2 and y1 == y2, point doubling must be
  128. // done, otherwise the addition would end up dividing
  129. // by zero.
  130. curve.doubleJacobian(x1, y1, z1, x3, y3, z3)
  131. return
  132. }
  133. // Since x1 == x2 and y1 == -y2, the sum is the point at
  134. // infinity per the group law.
  135. x3.SetInt(0)
  136. y3.SetInt(0)
  137. z3.SetInt(0)
  138. return
  139. }
  140. // Calculate X3, Y3, and Z3 according to the intermediate elements
  141. // breakdown above.
  142. var h, i, j, r, v fieldVal
  143. var negJ, neg2V, negX3 fieldVal
  144. h.Set(x1).Negate(1).Add(x2) // H = X2-X1 (mag: 3)
  145. i.SquareVal(&h).MulInt(4) // I = 4*H^2 (mag: 4)
  146. j.Mul2(&h, &i) // J = H*I (mag: 1)
  147. r.Set(y1).Negate(1).Add(y2).MulInt(2) // r = 2*(Y2-Y1) (mag: 6)
  148. v.Mul2(x1, &i) // V = X1*I (mag: 1)
  149. negJ.Set(&j).Negate(1) // negJ = -J (mag: 2)
  150. neg2V.Set(&v).MulInt(2).Negate(2) // neg2V = -(2*V) (mag: 3)
  151. x3.Set(&r).Square().Add(&negJ).Add(&neg2V) // X3 = r^2-J-2*V (mag: 6)
  152. negX3.Set(x3).Negate(6) // negX3 = -X3 (mag: 7)
  153. j.Mul(y1).MulInt(2).Negate(2) // J = -(2*Y1*J) (mag: 3)
  154. y3.Set(&v).Add(&negX3).Mul(&r).Add(&j) // Y3 = r*(V-X3)-2*Y1*J (mag: 4)
  155. z3.Set(&h).MulInt(2) // Z3 = 2*H (mag: 6)
  156. // Normalize the resulting field values to a magnitude of 1 as needed.
  157. x3.Normalize()
  158. y3.Normalize()
  159. z3.Normalize()
  160. }
  161. // addZ1EqualsZ2 adds two Jacobian points that are already known to have the
  162. // same z value and stores the result in (x3, y3, z3). That is to say
  163. // (x1, y1, z1) + (x2, y2, z1) = (x3, y3, z3). It performs faster addition than
  164. // the generic add routine since less arithmetic is needed due to the known
  165. // equivalence.
  166. func (curve *KoblitzCurve) addZ1EqualsZ2(x1, y1, z1, x2, y2, x3, y3, z3 *fieldVal) {
  167. // To compute the point addition efficiently, this implementation splits
  168. // the equation into intermediate elements which are used to minimize
  169. // the number of field multiplications using a slightly modified version
  170. // of the method shown at:
  171. // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-mmadd-2007-bl
  172. //
  173. // In particular it performs the calculations using the following:
  174. // A = X2-X1, B = A^2, C=Y2-Y1, D = C^2, E = X1*B, F = X2*B
  175. // X3 = D-E-F, Y3 = C*(E-X3)-Y1*(F-E), Z3 = Z1*A
  176. //
  177. // This results in a cost of 5 field multiplications, 2 field squarings,
  178. // 9 field additions, and 0 integer multiplications.
  179. // When the x coordinates are the same for two points on the curve, the
  180. // y coordinates either must be the same, in which case it is point
  181. // doubling, or they are opposite and the result is the point at
  182. // infinity per the group law for elliptic curve cryptography.
  183. x1.Normalize()
  184. y1.Normalize()
  185. x2.Normalize()
  186. y2.Normalize()
  187. if x1.Equals(x2) {
  188. if y1.Equals(y2) {
  189. // Since x1 == x2 and y1 == y2, point doubling must be
  190. // done, otherwise the addition would end up dividing
  191. // by zero.
  192. curve.doubleJacobian(x1, y1, z1, x3, y3, z3)
  193. return
  194. }
  195. // Since x1 == x2 and y1 == -y2, the sum is the point at
  196. // infinity per the group law.
  197. x3.SetInt(0)
  198. y3.SetInt(0)
  199. z3.SetInt(0)
  200. return
  201. }
  202. // Calculate X3, Y3, and Z3 according to the intermediate elements
  203. // breakdown above.
  204. var a, b, c, d, e, f fieldVal
  205. var negX1, negY1, negE, negX3 fieldVal
  206. negX1.Set(x1).Negate(1) // negX1 = -X1 (mag: 2)
  207. negY1.Set(y1).Negate(1) // negY1 = -Y1 (mag: 2)
  208. a.Set(&negX1).Add(x2) // A = X2-X1 (mag: 3)
  209. b.SquareVal(&a) // B = A^2 (mag: 1)
  210. c.Set(&negY1).Add(y2) // C = Y2-Y1 (mag: 3)
  211. d.SquareVal(&c) // D = C^2 (mag: 1)
  212. e.Mul2(x1, &b) // E = X1*B (mag: 1)
  213. negE.Set(&e).Negate(1) // negE = -E (mag: 2)
  214. f.Mul2(x2, &b) // F = X2*B (mag: 1)
  215. x3.Add2(&e, &f).Negate(3).Add(&d) // X3 = D-E-F (mag: 5)
  216. negX3.Set(x3).Negate(5).Normalize() // negX3 = -X3 (mag: 1)
  217. y3.Set(y1).Mul(f.Add(&negE)).Negate(3) // Y3 = -(Y1*(F-E)) (mag: 4)
  218. y3.Add(e.Add(&negX3).Mul(&c)) // Y3 = C*(E-X3)+Y3 (mag: 5)
  219. z3.Mul2(z1, &a) // Z3 = Z1*A (mag: 1)
  220. // Normalize the resulting field values to a magnitude of 1 as needed.
  221. x3.Normalize()
  222. y3.Normalize()
  223. }
  224. // addZ2EqualsOne adds two Jacobian points when the second point is already
  225. // known to have a z value of 1 (and the z value for the first point is not 1)
  226. // and stores the result in (x3, y3, z3). That is to say (x1, y1, z1) +
  227. // (x2, y2, 1) = (x3, y3, z3). It performs faster addition than the generic
  228. // add routine since less arithmetic is needed due to the ability to avoid
  229. // multiplications by the second point's z value.
  230. func (curve *KoblitzCurve) addZ2EqualsOne(x1, y1, z1, x2, y2, x3, y3, z3 *fieldVal) {
  231. // To compute the point addition efficiently, this implementation splits
  232. // the equation into intermediate elements which are used to minimize
  233. // the number of field multiplications using the method shown at:
  234. // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl
  235. //
  236. // In particular it performs the calculations using the following:
  237. // Z1Z1 = Z1^2, U2 = X2*Z1Z1, S2 = Y2*Z1*Z1Z1, H = U2-X1, HH = H^2,
  238. // I = 4*HH, J = H*I, r = 2*(S2-Y1), V = X1*I
  239. // X3 = r^2-J-2*V, Y3 = r*(V-X3)-2*Y1*J, Z3 = (Z1+H)^2-Z1Z1-HH
  240. //
  241. // This results in a cost of 7 field multiplications, 4 field squarings,
  242. // 9 field additions, and 4 integer multiplications.
  243. // When the x coordinates are the same for two points on the curve, the
  244. // y coordinates either must be the same, in which case it is point
  245. // doubling, or they are opposite and the result is the point at
  246. // infinity per the group law for elliptic curve cryptography. Since
  247. // any number of Jacobian coordinates can represent the same affine
  248. // point, the x and y values need to be converted to like terms. Due to
  249. // the assumption made for this function that the second point has a z
  250. // value of 1 (z2=1), the first point is already "converted".
  251. var z1z1, u2, s2 fieldVal
  252. x1.Normalize()
  253. y1.Normalize()
  254. z1z1.SquareVal(z1) // Z1Z1 = Z1^2 (mag: 1)
  255. u2.Set(x2).Mul(&z1z1).Normalize() // U2 = X2*Z1Z1 (mag: 1)
  256. s2.Set(y2).Mul(&z1z1).Mul(z1).Normalize() // S2 = Y2*Z1*Z1Z1 (mag: 1)
  257. if x1.Equals(&u2) {
  258. if y1.Equals(&s2) {
  259. // Since x1 == x2 and y1 == y2, point doubling must be
  260. // done, otherwise the addition would end up dividing
  261. // by zero.
  262. curve.doubleJacobian(x1, y1, z1, x3, y3, z3)
  263. return
  264. }
  265. // Since x1 == x2 and y1 == -y2, the sum is the point at
  266. // infinity per the group law.
  267. x3.SetInt(0)
  268. y3.SetInt(0)
  269. z3.SetInt(0)
  270. return
  271. }
  272. // Calculate X3, Y3, and Z3 according to the intermediate elements
  273. // breakdown above.
  274. var h, hh, i, j, r, rr, v fieldVal
  275. var negX1, negY1, negX3 fieldVal
  276. negX1.Set(x1).Negate(1) // negX1 = -X1 (mag: 2)
  277. h.Add2(&u2, &negX1) // H = U2-X1 (mag: 3)
  278. hh.SquareVal(&h) // HH = H^2 (mag: 1)
  279. i.Set(&hh).MulInt(4) // I = 4 * HH (mag: 4)
  280. j.Mul2(&h, &i) // J = H*I (mag: 1)
  281. negY1.Set(y1).Negate(1) // negY1 = -Y1 (mag: 2)
  282. r.Set(&s2).Add(&negY1).MulInt(2) // r = 2*(S2-Y1) (mag: 6)
  283. rr.SquareVal(&r) // rr = r^2 (mag: 1)
  284. v.Mul2(x1, &i) // V = X1*I (mag: 1)
  285. x3.Set(&v).MulInt(2).Add(&j).Negate(3) // X3 = -(J+2*V) (mag: 4)
  286. x3.Add(&rr) // X3 = r^2+X3 (mag: 5)
  287. negX3.Set(x3).Negate(5) // negX3 = -X3 (mag: 6)
  288. y3.Set(y1).Mul(&j).MulInt(2).Negate(2) // Y3 = -(2*Y1*J) (mag: 3)
  289. y3.Add(v.Add(&negX3).Mul(&r)) // Y3 = r*(V-X3)+Y3 (mag: 4)
  290. z3.Add2(z1, &h).Square() // Z3 = (Z1+H)^2 (mag: 1)
  291. z3.Add(z1z1.Add(&hh).Negate(2)) // Z3 = Z3-(Z1Z1+HH) (mag: 4)
  292. // Normalize the resulting field values to a magnitude of 1 as needed.
  293. x3.Normalize()
  294. y3.Normalize()
  295. z3.Normalize()
  296. }
  297. // addGeneric adds two Jacobian points (x1, y1, z1) and (x2, y2, z2) without any
  298. // assumptions about the z values of the two points and stores the result in
  299. // (x3, y3, z3). That is to say (x1, y1, z1) + (x2, y2, z2) = (x3, y3, z3). It
  300. // is the slowest of the add routines due to requiring the most arithmetic.
  301. func (curve *KoblitzCurve) addGeneric(x1, y1, z1, x2, y2, z2, x3, y3, z3 *fieldVal) {
  302. // To compute the point addition efficiently, this implementation splits
  303. // the equation into intermediate elements which are used to minimize
  304. // the number of field multiplications using the method shown at:
  305. // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
  306. //
  307. // In particular it performs the calculations using the following:
  308. // Z1Z1 = Z1^2, Z2Z2 = Z2^2, U1 = X1*Z2Z2, U2 = X2*Z1Z1, S1 = Y1*Z2*Z2Z2
  309. // S2 = Y2*Z1*Z1Z1, H = U2-U1, I = (2*H)^2, J = H*I, r = 2*(S2-S1)
  310. // V = U1*I
  311. // X3 = r^2-J-2*V, Y3 = r*(V-X3)-2*S1*J, Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2)*H
  312. //
  313. // This results in a cost of 11 field multiplications, 5 field squarings,
  314. // 9 field additions, and 4 integer multiplications.
  315. // When the x coordinates are the same for two points on the curve, the
  316. // y coordinates either must be the same, in which case it is point
  317. // doubling, or they are opposite and the result is the point at
  318. // infinity. Since any number of Jacobian coordinates can represent the
  319. // same affine point, the x and y values need to be converted to like
  320. // terms.
  321. var z1z1, z2z2, u1, u2, s1, s2 fieldVal
  322. z1z1.SquareVal(z1) // Z1Z1 = Z1^2 (mag: 1)
  323. z2z2.SquareVal(z2) // Z2Z2 = Z2^2 (mag: 1)
  324. u1.Set(x1).Mul(&z2z2).Normalize() // U1 = X1*Z2Z2 (mag: 1)
  325. u2.Set(x2).Mul(&z1z1).Normalize() // U2 = X2*Z1Z1 (mag: 1)
  326. s1.Set(y1).Mul(&z2z2).Mul(z2).Normalize() // S1 = Y1*Z2*Z2Z2 (mag: 1)
  327. s2.Set(y2).Mul(&z1z1).Mul(z1).Normalize() // S2 = Y2*Z1*Z1Z1 (mag: 1)
  328. if u1.Equals(&u2) {
  329. if s1.Equals(&s2) {
  330. // Since x1 == x2 and y1 == y2, point doubling must be
  331. // done, otherwise the addition would end up dividing
  332. // by zero.
  333. curve.doubleJacobian(x1, y1, z1, x3, y3, z3)
  334. return
  335. }
  336. // Since x1 == x2 and y1 == -y2, the sum is the point at
  337. // infinity per the group law.
  338. x3.SetInt(0)
  339. y3.SetInt(0)
  340. z3.SetInt(0)
  341. return
  342. }
  343. // Calculate X3, Y3, and Z3 according to the intermediate elements
  344. // breakdown above.
  345. var h, i, j, r, rr, v fieldVal
  346. var negU1, negS1, negX3 fieldVal
  347. negU1.Set(&u1).Negate(1) // negU1 = -U1 (mag: 2)
  348. h.Add2(&u2, &negU1) // H = U2-U1 (mag: 3)
  349. i.Set(&h).MulInt(2).Square() // I = (2*H)^2 (mag: 2)
  350. j.Mul2(&h, &i) // J = H*I (mag: 1)
  351. negS1.Set(&s1).Negate(1) // negS1 = -S1 (mag: 2)
  352. r.Set(&s2).Add(&negS1).MulInt(2) // r = 2*(S2-S1) (mag: 6)
  353. rr.SquareVal(&r) // rr = r^2 (mag: 1)
  354. v.Mul2(&u1, &i) // V = U1*I (mag: 1)
  355. x3.Set(&v).MulInt(2).Add(&j).Negate(3) // X3 = -(J+2*V) (mag: 4)
  356. x3.Add(&rr) // X3 = r^2+X3 (mag: 5)
  357. negX3.Set(x3).Negate(5) // negX3 = -X3 (mag: 6)
  358. y3.Mul2(&s1, &j).MulInt(2).Negate(2) // Y3 = -(2*S1*J) (mag: 3)
  359. y3.Add(v.Add(&negX3).Mul(&r)) // Y3 = r*(V-X3)+Y3 (mag: 4)
  360. z3.Add2(z1, z2).Square() // Z3 = (Z1+Z2)^2 (mag: 1)
  361. z3.Add(z1z1.Add(&z2z2).Negate(2)) // Z3 = Z3-(Z1Z1+Z2Z2) (mag: 4)
  362. z3.Mul(&h) // Z3 = Z3*H (mag: 1)
  363. // Normalize the resulting field values to a magnitude of 1 as needed.
  364. x3.Normalize()
  365. y3.Normalize()
  366. }
  367. // addJacobian adds the passed Jacobian points (x1, y1, z1) and (x2, y2, z2)
  368. // together and stores the result in (x3, y3, z3).
  369. func (curve *KoblitzCurve) addJacobian(x1, y1, z1, x2, y2, z2, x3, y3, z3 *fieldVal) {
  370. // A point at infinity is the identity according to the group law for
  371. // elliptic curve cryptography. Thus, ∞ + P = P and P + ∞ = P.
  372. if (x1.IsZero() && y1.IsZero()) || z1.IsZero() {
  373. x3.Set(x2)
  374. y3.Set(y2)
  375. z3.Set(z2)
  376. return
  377. }
  378. if (x2.IsZero() && y2.IsZero()) || z2.IsZero() {
  379. x3.Set(x1)
  380. y3.Set(y1)
  381. z3.Set(z1)
  382. return
  383. }
  384. // Faster point addition can be achieved when certain assumptions are
  385. // met. For example, when both points have the same z value, arithmetic
  386. // on the z values can be avoided. This section thus checks for these
  387. // conditions and calls an appropriate add function which is accelerated
  388. // by using those assumptions.
  389. z1.Normalize()
  390. z2.Normalize()
  391. isZ1One := z1.Equals(fieldOne)
  392. isZ2One := z2.Equals(fieldOne)
  393. switch {
  394. case isZ1One && isZ2One:
  395. curve.addZ1AndZ2EqualsOne(x1, y1, z1, x2, y2, x3, y3, z3)
  396. return
  397. case z1.Equals(z2):
  398. curve.addZ1EqualsZ2(x1, y1, z1, x2, y2, x3, y3, z3)
  399. return
  400. case isZ2One:
  401. curve.addZ2EqualsOne(x1, y1, z1, x2, y2, x3, y3, z3)
  402. return
  403. }
  404. // None of the above assumptions are true, so fall back to generic
  405. // point addition.
  406. curve.addGeneric(x1, y1, z1, x2, y2, z2, x3, y3, z3)
  407. }
  408. // Add returns the sum of (x1,y1) and (x2,y2). Part of the elliptic.Curve
  409. // interface.
  410. func (curve *KoblitzCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
  411. // A point at infinity is the identity according to the group law for
  412. // elliptic curve cryptography. Thus, ∞ + P = P and P + ∞ = P.
  413. if x1.Sign() == 0 && y1.Sign() == 0 {
  414. return x2, y2
  415. }
  416. if x2.Sign() == 0 && y2.Sign() == 0 {
  417. return x1, y1
  418. }
  419. // Convert the affine coordinates from big integers to field values
  420. // and do the point addition in Jacobian projective space.
  421. fx1, fy1 := curve.bigAffineToField(x1, y1)
  422. fx2, fy2 := curve.bigAffineToField(x2, y2)
  423. fx3, fy3, fz3 := new(fieldVal), new(fieldVal), new(fieldVal)
  424. fOne := new(fieldVal).SetInt(1)
  425. curve.addJacobian(fx1, fy1, fOne, fx2, fy2, fOne, fx3, fy3, fz3)
  426. // Convert the Jacobian coordinate field values back to affine big
  427. // integers.
  428. return curve.fieldJacobianToBigAffine(fx3, fy3, fz3)
  429. }
  430. // doubleZ1EqualsOne performs point doubling on the passed Jacobian point
  431. // when the point is already known to have a z value of 1 and stores
  432. // the result in (x3, y3, z3). That is to say (x3, y3, z3) = 2*(x1, y1, 1). It
  433. // performs faster point doubling than the generic routine since less arithmetic
  434. // is needed due to the ability to avoid multiplication by the z value.
  435. func (curve *KoblitzCurve) doubleZ1EqualsOne(x1, y1, x3, y3, z3 *fieldVal) {
  436. // This function uses the assumptions that z1 is 1, thus the point
  437. // doubling formulas reduce to:
  438. //
  439. // X3 = (3*X1^2)^2 - 8*X1*Y1^2
  440. // Y3 = (3*X1^2)*(4*X1*Y1^2 - X3) - 8*Y1^4
  441. // Z3 = 2*Y1
  442. //
  443. // To compute the above efficiently, this implementation splits the
  444. // equation into intermediate elements which are used to minimize the
  445. // number of field multiplications in favor of field squarings which
  446. // are roughly 35% faster than field multiplications with the current
  447. // implementation at the time this was written.
  448. //
  449. // This uses a slightly modified version of the method shown at:
  450. // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-mdbl-2007-bl
  451. //
  452. // In particular it performs the calculations using the following:
  453. // A = X1^2, B = Y1^2, C = B^2, D = 2*((X1+B)^2-A-C)
  454. // E = 3*A, F = E^2, X3 = F-2*D, Y3 = E*(D-X3)-8*C
  455. // Z3 = 2*Y1
  456. //
  457. // This results in a cost of 1 field multiplication, 5 field squarings,
  458. // 6 field additions, and 5 integer multiplications.
  459. var a, b, c, d, e, f fieldVal
  460. z3.Set(y1).MulInt(2) // Z3 = 2*Y1 (mag: 2)
  461. a.SquareVal(x1) // A = X1^2 (mag: 1)
  462. b.SquareVal(y1) // B = Y1^2 (mag: 1)
  463. c.SquareVal(&b) // C = B^2 (mag: 1)
  464. b.Add(x1).Square() // B = (X1+B)^2 (mag: 1)
  465. d.Set(&a).Add(&c).Negate(2) // D = -(A+C) (mag: 3)
  466. d.Add(&b).MulInt(2) // D = 2*(B+D)(mag: 8)
  467. e.Set(&a).MulInt(3) // E = 3*A (mag: 3)
  468. f.SquareVal(&e) // F = E^2 (mag: 1)
  469. x3.Set(&d).MulInt(2).Negate(16) // X3 = -(2*D) (mag: 17)
  470. x3.Add(&f) // X3 = F+X3 (mag: 18)
  471. f.Set(x3).Negate(18).Add(&d).Normalize() // F = D-X3 (mag: 1)
  472. y3.Set(&c).MulInt(8).Negate(8) // Y3 = -(8*C) (mag: 9)
  473. y3.Add(f.Mul(&e)) // Y3 = E*F+Y3 (mag: 10)
  474. // Normalize the field values back to a magnitude of 1.
  475. x3.Normalize()
  476. y3.Normalize()
  477. z3.Normalize()
  478. }
  479. // doubleGeneric performs point doubling on the passed Jacobian point without
  480. // any assumptions about the z value and stores the result in (x3, y3, z3).
  481. // That is to say (x3, y3, z3) = 2*(x1, y1, z1). It is the slowest of the point
  482. // doubling routines due to requiring the most arithmetic.
  483. func (curve *KoblitzCurve) doubleGeneric(x1, y1, z1, x3, y3, z3 *fieldVal) {
  484. // Point doubling formula for Jacobian coordinates for the secp256k1
  485. // curve:
  486. // X3 = (3*X1^2)^2 - 8*X1*Y1^2
  487. // Y3 = (3*X1^2)*(4*X1*Y1^2 - X3) - 8*Y1^4
  488. // Z3 = 2*Y1*Z1
  489. //
  490. // To compute the above efficiently, this implementation splits the
  491. // equation into intermediate elements which are used to minimize the
  492. // number of field multiplications in favor of field squarings which
  493. // are roughly 35% faster than field multiplications with the current
  494. // implementation at the time this was written.
  495. //
  496. // This uses a slightly modified version of the method shown at:
  497. // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
  498. //
  499. // In particular it performs the calculations using the following:
  500. // A = X1^2, B = Y1^2, C = B^2, D = 2*((X1+B)^2-A-C)
  501. // E = 3*A, F = E^2, X3 = F-2*D, Y3 = E*(D-X3)-8*C
  502. // Z3 = 2*Y1*Z1
  503. //
  504. // This results in a cost of 1 field multiplication, 5 field squarings,
  505. // 6 field additions, and 5 integer multiplications.
  506. var a, b, c, d, e, f fieldVal
  507. z3.Mul2(y1, z1).MulInt(2) // Z3 = 2*Y1*Z1 (mag: 2)
  508. a.SquareVal(x1) // A = X1^2 (mag: 1)
  509. b.SquareVal(y1) // B = Y1^2 (mag: 1)
  510. c.SquareVal(&b) // C = B^2 (mag: 1)
  511. b.Add(x1).Square() // B = (X1+B)^2 (mag: 1)
  512. d.Set(&a).Add(&c).Negate(2) // D = -(A+C) (mag: 3)
  513. d.Add(&b).MulInt(2) // D = 2*(B+D)(mag: 8)
  514. e.Set(&a).MulInt(3) // E = 3*A (mag: 3)
  515. f.SquareVal(&e) // F = E^2 (mag: 1)
  516. x3.Set(&d).MulInt(2).Negate(16) // X3 = -(2*D) (mag: 17)
  517. x3.Add(&f) // X3 = F+X3 (mag: 18)
  518. f.Set(x3).Negate(18).Add(&d).Normalize() // F = D-X3 (mag: 1)
  519. y3.Set(&c).MulInt(8).Negate(8) // Y3 = -(8*C) (mag: 9)
  520. y3.Add(f.Mul(&e)) // Y3 = E*F+Y3 (mag: 10)
  521. // Normalize the field values back to a magnitude of 1.
  522. x3.Normalize()
  523. y3.Normalize()
  524. z3.Normalize()
  525. }
  526. // doubleJacobian doubles the passed Jacobian point (x1, y1, z1) and stores the
  527. // result in (x3, y3, z3).
  528. func (curve *KoblitzCurve) doubleJacobian(x1, y1, z1, x3, y3, z3 *fieldVal) {
  529. // Doubling a point at infinity is still infinity.
  530. if y1.IsZero() || z1.IsZero() {
  531. x3.SetInt(0)
  532. y3.SetInt(0)
  533. z3.SetInt(0)
  534. return
  535. }
  536. // Slightly faster point doubling can be achieved when the z value is 1
  537. // by avoiding the multiplication on the z value. This section calls
  538. // a point doubling function which is accelerated by using that
  539. // assumption when possible.
  540. if z1.Normalize().Equals(fieldOne) {
  541. curve.doubleZ1EqualsOne(x1, y1, x3, y3, z3)
  542. return
  543. }
  544. // Fall back to generic point doubling which works with arbitrary z
  545. // values.
  546. curve.doubleGeneric(x1, y1, z1, x3, y3, z3)
  547. }
  548. // Double returns 2*(x1,y1). Part of the elliptic.Curve interface.
  549. func (curve *KoblitzCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
  550. if y1.Sign() == 0 {
  551. return new(big.Int), new(big.Int)
  552. }
  553. // Convert the affine coordinates from big integers to field values
  554. // and do the point doubling in Jacobian projective space.
  555. fx1, fy1 := curve.bigAffineToField(x1, y1)
  556. fx3, fy3, fz3 := new(fieldVal), new(fieldVal), new(fieldVal)
  557. fOne := new(fieldVal).SetInt(1)
  558. curve.doubleJacobian(fx1, fy1, fOne, fx3, fy3, fz3)
  559. // Convert the Jacobian coordinate field values back to affine big
  560. // integers.
  561. return curve.fieldJacobianToBigAffine(fx3, fy3, fz3)
  562. }
  563. // splitK returns a balanced length-two representation of k and their signs.
  564. // This is algorithm 3.74 from [GECC].
  565. //
  566. // One thing of note about this algorithm is that no matter what c1 and c2 are,
  567. // the final equation of k = k1 + k2 * lambda (mod n) will hold. This is
  568. // provable mathematically due to how a1/b1/a2/b2 are computed.
  569. //
  570. // c1 and c2 are chosen to minimize the max(k1,k2).
  571. func (curve *KoblitzCurve) splitK(k []byte) ([]byte, []byte, int, int) {
  572. // All math here is done with big.Int, which is slow.
  573. // At some point, it might be useful to write something similar to
  574. // fieldVal but for N instead of P as the prime field if this ends up
  575. // being a bottleneck.
  576. bigIntK := new(big.Int)
  577. c1, c2 := new(big.Int), new(big.Int)
  578. tmp1, tmp2 := new(big.Int), new(big.Int)
  579. k1, k2 := new(big.Int), new(big.Int)
  580. bigIntK.SetBytes(k)
  581. // c1 = round(b2 * k / n) from step 4.
  582. // Rounding isn't really necessary and costs too much, hence skipped
  583. c1.Mul(curve.b2, bigIntK)
  584. c1.Div(c1, curve.N)
  585. // c2 = round(b1 * k / n) from step 4 (sign reversed to optimize one step)
  586. // Rounding isn't really necessary and costs too much, hence skipped
  587. c2.Mul(curve.b1, bigIntK)
  588. c2.Div(c2, curve.N)
  589. // k1 = k - c1 * a1 - c2 * a2 from step 5 (note c2's sign is reversed)
  590. tmp1.Mul(c1, curve.a1)
  591. tmp2.Mul(c2, curve.a2)
  592. k1.Sub(bigIntK, tmp1)
  593. k1.Add(k1, tmp2)
  594. // k2 = - c1 * b1 - c2 * b2 from step 5 (note c2's sign is reversed)
  595. tmp1.Mul(c1, curve.b1)
  596. tmp2.Mul(c2, curve.b2)
  597. k2.Sub(tmp2, tmp1)
  598. // Note Bytes() throws out the sign of k1 and k2. This matters
  599. // since k1 and/or k2 can be negative. Hence, we pass that
  600. // back separately.
  601. return k1.Bytes(), k2.Bytes(), k1.Sign(), k2.Sign()
  602. }
  603. // moduloReduce reduces k from more than 32 bytes to 32 bytes and under. This
  604. // is done by doing a simple modulo curve.N. We can do this since G^N = 1 and
  605. // thus any other valid point on the elliptic curve has the same order.
  606. func (curve *KoblitzCurve) moduloReduce(k []byte) []byte {
  607. // Since the order of G is curve.N, we can use a much smaller number
  608. // by doing modulo curve.N
  609. if len(k) > curve.byteSize {
  610. // Reduce k by performing modulo curve.N.
  611. tmpK := new(big.Int).SetBytes(k)
  612. tmpK.Mod(tmpK, curve.N)
  613. return tmpK.Bytes()
  614. }
  615. return k
  616. }
  617. // NAF takes a positive integer k and returns the Non-Adjacent Form (NAF) as two
  618. // byte slices. The first is where 1s will be. The second is where -1s will
  619. // be. NAF is convenient in that on average, only 1/3rd of its values are
  620. // non-zero. This is algorithm 3.30 from [GECC].
  621. //
  622. // Essentially, this makes it possible to minimize the number of operations
  623. // since the resulting ints returned will be at least 50% 0s.
  624. func NAF(k []byte) ([]byte, []byte) {
  625. // The essence of this algorithm is that whenever we have consecutive 1s
  626. // in the binary, we want to put a -1 in the lowest bit and get a bunch
  627. // of 0s up to the highest bit of consecutive 1s. This is due to this
  628. // identity:
  629. // 2^n + 2^(n-1) + 2^(n-2) + ... + 2^(n-k) = 2^(n+1) - 2^(n-k)
  630. //
  631. // The algorithm thus may need to go 1 more bit than the length of the
  632. // bits we actually have, hence bits being 1 bit longer than was
  633. // necessary. Since we need to know whether adding will cause a carry,
  634. // we go from right-to-left in this addition.
  635. var carry, curIsOne, nextIsOne bool
  636. // these default to zero
  637. retPos := make([]byte, len(k)+1)
  638. retNeg := make([]byte, len(k)+1)
  639. for i := len(k) - 1; i >= 0; i-- {
  640. curByte := k[i]
  641. for j := uint(0); j < 8; j++ {
  642. curIsOne = curByte&1 == 1
  643. if j == 7 {
  644. if i == 0 {
  645. nextIsOne = false
  646. } else {
  647. nextIsOne = k[i-1]&1 == 1
  648. }
  649. } else {
  650. nextIsOne = curByte&2 == 2
  651. }
  652. if carry {
  653. if curIsOne {
  654. // This bit is 1, so continue to carry
  655. // and don't need to do anything.
  656. } else {
  657. // We've hit a 0 after some number of
  658. // 1s.
  659. if nextIsOne {
  660. // Start carrying again since
  661. // a new sequence of 1s is
  662. // starting.
  663. retNeg[i+1] += 1 << j
  664. } else {
  665. // Stop carrying since 1s have
  666. // stopped.
  667. carry = false
  668. retPos[i+1] += 1 << j
  669. }
  670. }
  671. } else if curIsOne {
  672. if nextIsOne {
  673. // If this is the start of at least 2
  674. // consecutive 1s, set the current one
  675. // to -1 and start carrying.
  676. retNeg[i+1] += 1 << j
  677. carry = true
  678. } else {
  679. // This is a singleton, not consecutive
  680. // 1s.
  681. retPos[i+1] += 1 << j
  682. }
  683. }
  684. curByte >>= 1
  685. }
  686. }
  687. if carry {
  688. retPos[0] = 1
  689. return retPos, retNeg
  690. }
  691. return retPos[1:], retNeg[1:]
  692. }
  693. // ScalarMult returns k*(Bx, By) where k is a big endian integer.
  694. // Part of the elliptic.Curve interface.
  695. func (curve *KoblitzCurve) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int) {
  696. // Point Q = ∞ (point at infinity).
  697. qx, qy, qz := new(fieldVal), new(fieldVal), new(fieldVal)
  698. // Decompose K into k1 and k2 in order to halve the number of EC ops.
  699. // See Algorithm 3.74 in [GECC].
  700. k1, k2, signK1, signK2 := curve.splitK(curve.moduloReduce(k))
  701. // The main equation here to remember is:
  702. // k * P = k1 * P + k2 * ϕ(P)
  703. //
  704. // P1 below is P in the equation, P2 below is ϕ(P) in the equation
  705. p1x, p1y := curve.bigAffineToField(Bx, By)
  706. p1yNeg := new(fieldVal).NegateVal(p1y, 1)
  707. p1z := new(fieldVal).SetInt(1)
  708. // NOTE: ϕ(x,y) = (βx,y). The Jacobian z coordinate is 1, so this math
  709. // goes through.
  710. p2x := new(fieldVal).Mul2(p1x, curve.beta)
  711. p2y := new(fieldVal).Set(p1y)
  712. p2yNeg := new(fieldVal).NegateVal(p2y, 1)
  713. p2z := new(fieldVal).SetInt(1)
  714. // Flip the positive and negative values of the points as needed
  715. // depending on the signs of k1 and k2. As mentioned in the equation
  716. // above, each of k1 and k2 are multiplied by the respective point.
  717. // Since -k * P is the same thing as k * -P, and the group law for
  718. // elliptic curves states that P(x, y) = -P(x, -y), it's faster and
  719. // simplifies the code to just make the point negative.
  720. if signK1 == -1 {
  721. p1y, p1yNeg = p1yNeg, p1y
  722. }
  723. if signK2 == -1 {
  724. p2y, p2yNeg = p2yNeg, p2y
  725. }
  726. // NAF versions of k1 and k2 should have a lot more zeros.
  727. //
  728. // The Pos version of the bytes contain the +1s and the Neg versions
  729. // contain the -1s.
  730. k1PosNAF, k1NegNAF := NAF(k1)
  731. k2PosNAF, k2NegNAF := NAF(k2)
  732. k1Len := len(k1PosNAF)
  733. k2Len := len(k2PosNAF)
  734. m := k1Len
  735. if m < k2Len {
  736. m = k2Len
  737. }
  738. // Add left-to-right using the NAF optimization. See algorithm 3.77
  739. // from [GECC]. This should be faster overall since there will be a lot
  740. // more instances of 0, hence reducing the number of Jacobian additions
  741. // at the cost of 1 possible extra doubling.
  742. var k1BytePos, k1ByteNeg, k2BytePos, k2ByteNeg byte
  743. for i := 0; i < m; i++ {
  744. // Since we're going left-to-right, pad the front with 0s.
  745. if i < m-k1Len {
  746. k1BytePos = 0
  747. k1ByteNeg = 0
  748. } else {
  749. k1BytePos = k1PosNAF[i-(m-k1Len)]
  750. k1ByteNeg = k1NegNAF[i-(m-k1Len)]
  751. }
  752. if i < m-k2Len {
  753. k2BytePos = 0
  754. k2ByteNeg = 0
  755. } else {
  756. k2BytePos = k2PosNAF[i-(m-k2Len)]
  757. k2ByteNeg = k2NegNAF[i-(m-k2Len)]
  758. }
  759. for j := 7; j >= 0; j-- {
  760. // Q = 2 * Q
  761. curve.doubleJacobian(qx, qy, qz, qx, qy, qz)
  762. if k1BytePos&0x80 == 0x80 {
  763. curve.addJacobian(qx, qy, qz, p1x, p1y, p1z,
  764. qx, qy, qz)
  765. } else if k1ByteNeg&0x80 == 0x80 {
  766. curve.addJacobian(qx, qy, qz, p1x, p1yNeg, p1z,
  767. qx, qy, qz)
  768. }
  769. if k2BytePos&0x80 == 0x80 {
  770. curve.addJacobian(qx, qy, qz, p2x, p2y, p2z,
  771. qx, qy, qz)
  772. } else if k2ByteNeg&0x80 == 0x80 {
  773. curve.addJacobian(qx, qy, qz, p2x, p2yNeg, p2z,
  774. qx, qy, qz)
  775. }
  776. k1BytePos <<= 1
  777. k1ByteNeg <<= 1
  778. k2BytePos <<= 1
  779. k2ByteNeg <<= 1
  780. }
  781. }
  782. // Convert the Jacobian coordinate field values back to affine big.Ints.
  783. return curve.fieldJacobianToBigAffine(qx, qy, qz)
  784. }
  785. // ScalarBaseMult returns k*G where G is the base point of the group and k is a
  786. // big endian integer.
  787. // Part of the elliptic.Curve interface.
  788. func (curve *KoblitzCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
  789. newK := curve.moduloReduce(k)
  790. diff := len(curve.bytePoints) - len(newK)
  791. // Point Q = ∞ (point at infinity).
  792. qx, qy, qz := new(fieldVal), new(fieldVal), new(fieldVal)
  793. // curve.bytePoints has all 256 byte points for each 8-bit window. The
  794. // strategy is to add up the byte points. This is best understood by
  795. // expressing k in base-256 which it already sort of is.
  796. // Each "digit" in the 8-bit window can be looked up using bytePoints
  797. // and added together.
  798. for i, byteVal := range newK {
  799. p := curve.bytePoints[diff+i][byteVal]
  800. curve.addJacobian(qx, qy, qz, &p[0], &p[1], &p[2], qx, qy, qz)
  801. }
  802. return curve.fieldJacobianToBigAffine(qx, qy, qz)
  803. }
  804. // QPlus1Div4 returns the Q+1/4 constant for the curve for use in calculating
  805. // square roots via exponention.
  806. func (curve *KoblitzCurve) QPlus1Div4() *big.Int {
  807. return curve.q
  808. }
  809. var initonce sync.Once
  810. var secp256k1 KoblitzCurve
  811. func initAll() {
  812. initS256()
  813. }
  814. // fromHex converts the passed hex string into a big integer pointer and will
  815. // panic is there is an error. This is only provided for the hard-coded
  816. // constants so errors in the source code can bet detected. It will only (and
  817. // must only) be called for initialization purposes.
  818. func fromHex(s string) *big.Int {
  819. r, ok := new(big.Int).SetString(s, 16)
  820. if !ok {
  821. panic("invalid hex in source file: " + s)
  822. }
  823. return r
  824. }
  825. func initS256() {
  826. // Curve parameters taken from [SECG] section 2.4.1.
  827. secp256k1.CurveParams = new(elliptic.CurveParams)
  828. secp256k1.P = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F")
  829. secp256k1.N = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141")
  830. secp256k1.B = fromHex("0000000000000000000000000000000000000000000000000000000000000007")
  831. secp256k1.Gx = fromHex("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798")
  832. secp256k1.Gy = fromHex("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8")
  833. secp256k1.BitSize = 256
  834. secp256k1.q = new(big.Int).Div(new(big.Int).Add(secp256k1.P,
  835. big.NewInt(1)), big.NewInt(4))
  836. secp256k1.H = 1
  837. secp256k1.halfOrder = new(big.Int).Rsh(secp256k1.N, 1)
  838. // Provided for convenience since this gets computed repeatedly.
  839. secp256k1.byteSize = secp256k1.BitSize / 8
  840. // Deserialize and set the pre-computed table used to accelerate scalar
  841. // base multiplication. This is hard-coded data, so any errors are
  842. // panics because it means something is wrong in the source code.
  843. if err := loadS256BytePoints(); err != nil {
  844. panic(err)
  845. }
  846. // Next 6 constants are from Hal Finney's bitcointalk.org post:
  847. // https://bitcointalk.org/index.php?topic=3238.msg45565#msg45565
  848. // May he rest in peace.
  849. //
  850. // They have also been independently derived from the code in the
  851. // EndomorphismVectors function in gensecp256k1.go.
  852. secp256k1.lambda = fromHex("5363AD4CC05C30E0A5261C028812645A122E22EA20816678DF02967C1B23BD72")
  853. secp256k1.beta = new(fieldVal).SetHex("7AE96A2B657C07106E64479EAC3434E99CF0497512F58995C1396C28719501EE")
  854. secp256k1.a1 = fromHex("3086D221A7D46BCDE86C90E49284EB15")
  855. secp256k1.b1 = fromHex("-E4437ED6010E88286F547FA90ABFE4C3")
  856. secp256k1.a2 = fromHex("114CA50F7A8E2F3F657C1108D9D44CFD8")
  857. secp256k1.b2 = fromHex("3086D221A7D46BCDE86C90E49284EB15")
  858. // Alternatively, we can use the parameters below, however, they seem
  859. // to be about 8% slower.
  860. // secp256k1.lambda = fromHex("AC9C52B33FA3CF1F5AD9E3FD77ED9BA4A880B9FC8EC739C2E0CFC810B51283CE")
  861. // secp256k1.beta = new(fieldVal).SetHex("851695D49A83F8EF919BB86153CBCB16630FB68AED0A766A3EC693D68E6AFA40")
  862. // secp256k1.a1 = fromHex("E4437ED6010E88286F547FA90ABFE4C3")
  863. // secp256k1.b1 = fromHex("-3086D221A7D46BCDE86C90E49284EB15")
  864. // secp256k1.a2 = fromHex("3086D221A7D46BCDE86C90E49284EB15")
  865. // secp256k1.b2 = fromHex("114CA50F7A8E2F3F657C1108D9D44CFD8")
  866. }
  867. // S256 returns a Curve which implements secp256k1.
  868. func S256() *KoblitzCurve {
  869. initonce.Do(initAll)
  870. return &secp256k1
  871. }