scrm-go

bitelliptic.go 11KB

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