p256.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /*
  2. Copyright Suzhou Tongji Fintech Research Institute 2017 All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sm2
  14. import (
  15. "crypto/elliptic"
  16. "math/big"
  17. "sync"
  18. )
  19. /** 学习标准库p256的优化方法实现sm2的快速版本
  20. * 标准库的p256的代码实现有些晦涩难懂,当然sm2的同样如此,有兴趣的大家可以研究研究,最后神兽压阵。。。
  21. *
  22. * ━━━━━━animal━━━━━━
  23. *    ┏┓   ┏┓
  24. *   ┏┛┻━━━┛┻┓
  25. *   ┃       ┃
  26. *   ┃   ━   ┃
  27. *   ┃ ┳┛ ┗┳ ┃
  28. *   ┃       ┃
  29. *   ┃   ┻   ┃
  30. *   ┃       ┃
  31. *   ┗━┓   ┏━┛
  32. *    ┃   ┃
  33. *    ┃   ┃
  34. *    ┃   ┗━━━┓
  35. *  ┃     ┣┓
  36. *   ┃     ┏┛
  37. *    ┗┓┓┏━┳┓┏┛
  38. *    ┃┫┫ ┃┫┫
  39. *    ┗┻┛ ┗┻┛
  40. *
  41. * ━━━━━Kawaii ━━━━━━
  42. */
  43. type sm2P256Curve struct {
  44. RInverse *big.Int
  45. *elliptic.CurveParams
  46. a, b, gx, gy sm2P256FieldElement
  47. }
  48. var initonce sync.Once
  49. var sm2P256 sm2P256Curve
  50. type sm2P256FieldElement [9]uint32
  51. type sm2P256LargeFieldElement [17]uint64
  52. const (
  53. bottom28Bits = 0xFFFFFFF
  54. bottom29Bits = 0x1FFFFFFF
  55. )
  56. func initP256Sm2() {
  57. sm2P256.CurveParams = &elliptic.CurveParams{Name: "SM2-P-256"} // sm2
  58. A, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", 16)
  59. //SM2椭 椭 圆 曲 线 公 钥 密 码 算 法 推 荐 曲 线 参 数
  60. sm2P256.P, _ = new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16)
  61. sm2P256.N, _ = new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16)
  62. sm2P256.B, _ = new(big.Int).SetString("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16)
  63. sm2P256.Gx, _ = new(big.Int).SetString("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", 16)
  64. sm2P256.Gy, _ = new(big.Int).SetString("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", 16)
  65. sm2P256.RInverse, _ = new(big.Int).SetString("7ffffffd80000002fffffffe000000017ffffffe800000037ffffffc80000002", 16)
  66. sm2P256.BitSize = 256
  67. sm2P256FromBig(&sm2P256.a, A)
  68. sm2P256FromBig(&sm2P256.gx, sm2P256.Gx)
  69. sm2P256FromBig(&sm2P256.gy, sm2P256.Gy)
  70. sm2P256FromBig(&sm2P256.b, sm2P256.B)
  71. }
  72. func P256Sm2() elliptic.Curve {
  73. initonce.Do(initP256Sm2)
  74. return sm2P256
  75. }
  76. func (curve sm2P256Curve) Params() *elliptic.CurveParams {
  77. return sm2P256.CurveParams
  78. }
  79. // y^2 = x^3 + ax + b
  80. func (curve sm2P256Curve) IsOnCurve(X, Y *big.Int) bool {
  81. var a, x, y, y2, x3 sm2P256FieldElement
  82. sm2P256FromBig(&x, X)
  83. sm2P256FromBig(&y, Y)
  84. sm2P256Square(&x3, &x) // x3 = x ^ 2
  85. sm2P256Mul(&x3, &x3, &x) // x3 = x ^ 2 * x
  86. sm2P256Mul(&a, &curve.a, &x) // a = a * x
  87. sm2P256Add(&x3, &x3, &a)
  88. sm2P256Add(&x3, &x3, &curve.b)
  89. sm2P256Square(&y2, &y) // y2 = y ^ 2
  90. return sm2P256ToBig(&x3).Cmp(sm2P256ToBig(&y2)) == 0
  91. }
  92. func zForAffine(x, y *big.Int) *big.Int {
  93. z := new(big.Int)
  94. if x.Sign() != 0 || y.Sign() != 0 {
  95. z.SetInt64(1)
  96. }
  97. return z
  98. }
  99. func (curve sm2P256Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
  100. var X1, Y1, Z1, X2, Y2, Z2, X3, Y3, Z3 sm2P256FieldElement
  101. z1 := zForAffine(x1, y1)
  102. z2 := zForAffine(x2, y2)
  103. sm2P256FromBig(&X1, x1)
  104. sm2P256FromBig(&Y1, y1)
  105. sm2P256FromBig(&Z1, z1)
  106. sm2P256FromBig(&X2, x2)
  107. sm2P256FromBig(&Y2, y2)
  108. sm2P256FromBig(&Z2, z2)
  109. sm2P256PointAdd(&X1, &Y1, &Z1, &X2, &Y2, &Z2, &X3, &Y3, &Z3)
  110. return sm2P256ToAffine(&X3, &Y3, &Z3)
  111. }
  112. func (curve sm2P256Curve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
  113. var X1, Y1, Z1 sm2P256FieldElement
  114. z1 := zForAffine(x1, y1)
  115. sm2P256FromBig(&X1, x1)
  116. sm2P256FromBig(&Y1, y1)
  117. sm2P256FromBig(&Z1, z1)
  118. sm2P256PointDouble(&X1, &Y1, &Z1, &X1, &Y1, &Z1)
  119. return sm2P256ToAffine(&X1, &Y1, &Z1)
  120. }
  121. func (curve sm2P256Curve) ScalarMult(x1, y1 *big.Int, k []byte) (*big.Int, *big.Int) {
  122. var X, Y, Z, X1, Y1 sm2P256FieldElement
  123. sm2P256FromBig(&X1, x1)
  124. sm2P256FromBig(&Y1, y1)
  125. scalar := sm2GenrateWNaf(k)
  126. scalarReversed := WNafReversed(scalar)
  127. sm2P256ScalarMult(&X, &Y, &Z, &X1, &Y1, scalarReversed)
  128. return sm2P256ToAffine(&X, &Y, &Z)
  129. }
  130. func (curve sm2P256Curve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
  131. var scalarReversed [32]byte
  132. var X, Y, Z sm2P256FieldElement
  133. sm2P256GetScalar(&scalarReversed, k)
  134. sm2P256ScalarBaseMult(&X, &Y, &Z, &scalarReversed)
  135. return sm2P256ToAffine(&X, &Y, &Z)
  136. }
  137. var sm2P256Precomputed = [9 * 2 * 15 * 2]uint32{
  138. 0x830053d, 0x328990f, 0x6c04fe1, 0xc0f72e5, 0x1e19f3c, 0x666b093, 0x175a87b, 0xec38276, 0x222cf4b,
  139. 0x185a1bba, 0x354e593, 0x1295fac1, 0xf2bc469, 0x47c60fa, 0xc19b8a9, 0xf63533e, 0x903ae6b, 0xc79acba,
  140. 0x15b061a4, 0x33e020b, 0xdffb34b, 0xfcf2c8, 0x16582e08, 0x262f203, 0xfb34381, 0xa55452, 0x604f0ff,
  141. 0x41f1f90, 0xd64ced2, 0xee377bf, 0x75f05f0, 0x189467ae, 0xe2244e, 0x1e7700e8, 0x3fbc464, 0x9612d2e,
  142. 0x1341b3b8, 0xee84e23, 0x1edfa5b4, 0x14e6030, 0x19e87be9, 0x92f533c, 0x1665d96c, 0x226653e, 0xa238d3e,
  143. 0xf5c62c, 0x95bb7a, 0x1f0e5a41, 0x28789c3, 0x1f251d23, 0x8726609, 0xe918910, 0x8096848, 0xf63d028,
  144. 0x152296a1, 0x9f561a8, 0x14d376fb, 0x898788a, 0x61a95fb, 0xa59466d, 0x159a003d, 0x1ad1698, 0x93cca08,
  145. 0x1b314662, 0x706e006, 0x11ce1e30, 0x97b710, 0x172fbc0d, 0x8f50158, 0x11c7ffe7, 0xd182cce, 0xc6ad9e8,
  146. 0x12ea31b2, 0xc4e4f38, 0x175b0d96, 0xec06337, 0x75a9c12, 0xb001fdf, 0x93e82f5, 0x34607de, 0xb8035ed,
  147. 0x17f97924, 0x75cf9e6, 0xdceaedd, 0x2529924, 0x1a10c5ff, 0xb1a54dc, 0x19464d8, 0x2d1997, 0xde6a110,
  148. 0x1e276ee5, 0x95c510c, 0x1aca7c7a, 0xfe48aca, 0x121ad4d9, 0xe4132c6, 0x8239b9d, 0x40ea9cd, 0x816c7b,
  149. 0x632d7a4, 0xa679813, 0x5911fcf, 0x82b0f7c, 0x57b0ad5, 0xbef65, 0xd541365, 0x7f9921f, 0xc62e7a,
  150. 0x3f4b32d, 0x58e50e1, 0x6427aed, 0xdcdda67, 0xe8c2d3e, 0x6aa54a4, 0x18df4c35, 0x49a6a8e, 0x3cd3d0c,
  151. 0xd7adf2, 0xcbca97, 0x1bda5f2d, 0x3258579, 0x606b1e6, 0x6fc1b5b, 0x1ac27317, 0x503ca16, 0xa677435,
  152. 0x57bc73, 0x3992a42, 0xbab987b, 0xfab25eb, 0x128912a4, 0x90a1dc4, 0x1402d591, 0x9ffbcfc, 0xaa48856,
  153. 0x7a7c2dc, 0xcefd08a, 0x1b29bda6, 0xa785641, 0x16462d8c, 0x76241b7, 0x79b6c3b, 0x204ae18, 0xf41212b,
  154. 0x1f567a4d, 0xd6ce6db, 0xedf1784, 0x111df34, 0x85d7955, 0x55fc189, 0x1b7ae265, 0xf9281ac, 0xded7740,
  155. 0xf19468b, 0x83763bb, 0x8ff7234, 0x3da7df8, 0x9590ac3, 0xdc96f2a, 0x16e44896, 0x7931009, 0x99d5acc,
  156. 0x10f7b842, 0xaef5e84, 0xc0310d7, 0xdebac2c, 0x2a7b137, 0x4342344, 0x19633649, 0x3a10624, 0x4b4cb56,
  157. 0x1d809c59, 0xac007f, 0x1f0f4bcd, 0xa1ab06e, 0xc5042cf, 0x82c0c77, 0x76c7563, 0x22c30f3, 0x3bf1568,
  158. 0x7a895be, 0xfcca554, 0x12e90e4c, 0x7b4ab5f, 0x13aeb76b, 0x5887e2c, 0x1d7fe1e3, 0x908c8e3, 0x95800ee,
  159. 0xb36bd54, 0xf08905d, 0x4e73ae8, 0xf5a7e48, 0xa67cb0, 0x50e1067, 0x1b944a0a, 0xf29c83a, 0xb23cfb9,
  160. 0xbe1db1, 0x54de6e8, 0xd4707f2, 0x8ebcc2d, 0x2c77056, 0x1568ce4, 0x15fcc849, 0x4069712, 0xe2ed85f,
  161. 0x2c5ff09, 0x42a6929, 0x628e7ea, 0xbd5b355, 0xaf0bd79, 0xaa03699, 0xdb99816, 0x4379cef, 0x81d57b,
  162. 0x11237f01, 0xe2a820b, 0xfd53b95, 0x6beb5ee, 0x1aeb790c, 0xe470d53, 0x2c2cfee, 0x1c1d8d8, 0xa520fc4,
  163. 0x1518e034, 0xa584dd4, 0x29e572b, 0xd4594fc, 0x141a8f6f, 0x8dfccf3, 0x5d20ba3, 0x2eb60c3, 0x9f16eb0,
  164. 0x11cec356, 0xf039f84, 0x1b0990c1, 0xc91e526, 0x10b65bae, 0xf0616e8, 0x173fa3ff, 0xec8ccf9, 0xbe32790,
  165. 0x11da3e79, 0xe2f35c7, 0x908875c, 0xdacf7bd, 0x538c165, 0x8d1487f, 0x7c31aed, 0x21af228, 0x7e1689d,
  166. 0xdfc23ca, 0x24f15dc, 0x25ef3c4, 0x35248cd, 0x99a0f43, 0xa4b6ecc, 0xd066b3, 0x2481152, 0x37a7688,
  167. 0x15a444b6, 0xb62300c, 0x4b841b, 0xa655e79, 0xd53226d, 0xbeb348a, 0x127f3c2, 0xb989247, 0x71a277d,
  168. 0x19e9dfcb, 0xb8f92d0, 0xe2d226c, 0x390a8b0, 0x183cc462, 0x7bd8167, 0x1f32a552, 0x5e02db4, 0xa146ee9,
  169. 0x1a003957, 0x1c95f61, 0x1eeec155, 0x26f811f, 0xf9596ba, 0x3082bfb, 0x96df083, 0x3e3a289, 0x7e2d8be,
  170. 0x157a63e0, 0x99b8941, 0x1da7d345, 0xcc6cd0, 0x10beed9a, 0x48e83c0, 0x13aa2e25, 0x7cad710, 0x4029988,
  171. 0x13dfa9dd, 0xb94f884, 0x1f4adfef, 0xb88543, 0x16f5f8dc, 0xa6a67f4, 0x14e274e2, 0x5e56cf4, 0x2f24ef,
  172. 0x1e9ef967, 0xfe09bad, 0xfe079b3, 0xcc0ae9e, 0xb3edf6d, 0x3e961bc, 0x130d7831, 0x31043d6, 0xba986f9,
  173. 0x1d28055, 0x65240ca, 0x4971fa3, 0x81b17f8, 0x11ec34a5, 0x8366ddc, 0x1471809, 0xfa5f1c6, 0xc911e15,
  174. 0x8849491, 0xcf4c2e2, 0x14471b91, 0x39f75be, 0x445c21e, 0xf1585e9, 0x72cc11f, 0x4c79f0c, 0xe5522e1,
  175. 0x1874c1ee, 0x4444211, 0x7914884, 0x3d1b133, 0x25ba3c, 0x4194f65, 0x1c0457ef, 0xac4899d, 0xe1fa66c,
  176. 0x130a7918, 0x9b8d312, 0x4b1c5c8, 0x61ccac3, 0x18c8aa6f, 0xe93cb0a, 0xdccb12c, 0xde10825, 0x969737d,
  177. 0xf58c0c3, 0x7cee6a9, 0xc2c329a, 0xc7f9ed9, 0x107b3981, 0x696a40e, 0x152847ff, 0x4d88754, 0xb141f47,
  178. 0x5a16ffe, 0x3a7870a, 0x18667659, 0x3b72b03, 0xb1c9435, 0x9285394, 0xa00005a, 0x37506c, 0x2edc0bb,
  179. 0x19afe392, 0xeb39cac, 0x177ef286, 0xdf87197, 0x19f844ed, 0x31fe8, 0x15f9bfd, 0x80dbec, 0x342e96e,
  180. 0x497aced, 0xe88e909, 0x1f5fa9ba, 0x530a6ee, 0x1ef4e3f1, 0x69ffd12, 0x583006d, 0x2ecc9b1, 0x362db70,
  181. 0x18c7bdc5, 0xf4bb3c5, 0x1c90b957, 0xf067c09, 0x9768f2b, 0xf73566a, 0x1939a900, 0x198c38a, 0x202a2a1,
  182. 0x4bbf5a6, 0x4e265bc, 0x1f44b6e7, 0x185ca49, 0xa39e81b, 0x24aff5b, 0x4acc9c2, 0x638bdd3, 0xb65b2a8,
  183. 0x6def8be, 0xb94537a, 0x10b81dee, 0xe00ec55, 0x2f2cdf7, 0xc20622d, 0x2d20f36, 0xe03c8c9, 0x898ea76,
  184. 0x8e3921b, 0x8905bff, 0x1e94b6c8, 0xee7ad86, 0x154797f2, 0xa620863, 0x3fbd0d9, 0x1f3caab, 0x30c24bd,
  185. 0x19d3892f, 0x59c17a2, 0x1ab4b0ae, 0xf8714ee, 0x90c4098, 0xa9c800d, 0x1910236b, 0xea808d3, 0x9ae2f31,
  186. 0x1a15ad64, 0xa48c8d1, 0x184635a4, 0xb725ef1, 0x11921dcc, 0x3f866df, 0x16c27568, 0xbdf580a, 0xb08f55c,
  187. 0x186ee1c, 0xb1627fa, 0x34e82f6, 0x933837e, 0xf311be5, 0xfedb03b, 0x167f72cd, 0xa5469c0, 0x9c82531,
  188. 0xb92a24b, 0x14fdc8b, 0x141980d1, 0xbdc3a49, 0x7e02bb1, 0xaf4e6dd, 0x106d99e1, 0xd4616fc, 0x93c2717,
  189. 0x1c0a0507, 0xc6d5fed, 0x9a03d8b, 0xa1d22b0, 0x127853e3, 0xc4ac6b8, 0x1a048cf7, 0x9afb72c, 0x65d485d,
  190. 0x72d5998, 0xe9fa744, 0xe49e82c, 0x253cf80, 0x5f777ce, 0xa3799a5, 0x17270cbb, 0xc1d1ef0, 0xdf74977,
  191. 0x114cb859, 0xfa8e037, 0xb8f3fe5, 0xc734cc6, 0x70d3d61, 0xeadac62, 0x12093dd0, 0x9add67d, 0x87200d6,
  192. 0x175bcbb, 0xb29b49f, 0x1806b79c, 0x12fb61f, 0x170b3a10, 0x3aaf1cf, 0xa224085, 0x79d26af, 0x97759e2,
  193. 0x92e19f1, 0xb32714d, 0x1f00d9f1, 0xc728619, 0x9e6f627, 0xe745e24, 0x18ea4ace, 0xfc60a41, 0x125f5b2,
  194. 0xc3cf512, 0x39ed486, 0xf4d15fa, 0xf9167fd, 0x1c1f5dd5, 0xc21a53e, 0x1897930, 0x957a112, 0x21059a0,
  195. 0x1f9e3ddc, 0xa4dfced, 0x8427f6f, 0x726fbe7, 0x1ea658f8, 0x2fdcd4c, 0x17e9b66f, 0xb2e7c2e, 0x39923bf,
  196. 0x1bae104, 0x3973ce5, 0xc6f264c, 0x3511b84, 0x124195d7, 0x11996bd, 0x20be23d, 0xdc437c4, 0x4b4f16b,
  197. 0x11902a0, 0x6c29cc9, 0x1d5ffbe6, 0xdb0b4c7, 0x10144c14, 0x2f2b719, 0x301189, 0x2343336, 0xa0bf2ac,
  198. }
  199. func sm2P256GetScalar(b *[32]byte, a []byte) {
  200. var scalarBytes []byte
  201. n := new(big.Int).SetBytes(a)
  202. if n.Cmp(sm2P256.N) >= 0 {
  203. n.Mod(n, sm2P256.N)
  204. scalarBytes = n.Bytes()
  205. } else {
  206. scalarBytes = a
  207. }
  208. for i, v := range scalarBytes {
  209. b[len(scalarBytes)-(1+i)] = v
  210. }
  211. }
  212. func sm2P256PointAddMixed(xOut, yOut, zOut, x1, y1, z1, x2, y2 *sm2P256FieldElement) {
  213. var z1z1, z1z1z1, s2, u2, h, i, j, r, rr, v, tmp sm2P256FieldElement
  214. sm2P256Square(&z1z1, z1)
  215. sm2P256Add(&tmp, z1, z1)
  216. sm2P256Mul(&u2, x2, &z1z1)
  217. sm2P256Mul(&z1z1z1, z1, &z1z1)
  218. sm2P256Mul(&s2, y2, &z1z1z1)
  219. sm2P256Sub(&h, &u2, x1)
  220. sm2P256Add(&i, &h, &h)
  221. sm2P256Square(&i, &i)
  222. sm2P256Mul(&j, &h, &i)
  223. sm2P256Sub(&r, &s2, y1)
  224. sm2P256Add(&r, &r, &r)
  225. sm2P256Mul(&v, x1, &i)
  226. sm2P256Mul(zOut, &tmp, &h)
  227. sm2P256Square(&rr, &r)
  228. sm2P256Sub(xOut, &rr, &j)
  229. sm2P256Sub(xOut, xOut, &v)
  230. sm2P256Sub(xOut, xOut, &v)
  231. sm2P256Sub(&tmp, &v, xOut)
  232. sm2P256Mul(yOut, &tmp, &r)
  233. sm2P256Mul(&tmp, y1, &j)
  234. sm2P256Sub(yOut, yOut, &tmp)
  235. sm2P256Sub(yOut, yOut, &tmp)
  236. }
  237. // sm2P256CopyConditional sets out=in if mask = 0xffffffff in constant time.
  238. //
  239. // On entry: mask is either 0 or 0xffffffff.
  240. func sm2P256CopyConditional(out, in *sm2P256FieldElement, mask uint32) {
  241. for i := 0; i < 9; i++ {
  242. tmp := mask & (in[i] ^ out[i])
  243. out[i] ^= tmp
  244. }
  245. }
  246. // sm2P256SelectAffinePoint sets {out_x,out_y} to the index'th entry of table.
  247. // On entry: index < 16, table[0] must be zero.
  248. func sm2P256SelectAffinePoint(xOut, yOut *sm2P256FieldElement, table []uint32, index uint32) {
  249. for i := range xOut {
  250. xOut[i] = 0
  251. }
  252. for i := range yOut {
  253. yOut[i] = 0
  254. }
  255. for i := uint32(1); i < 16; i++ {
  256. mask := i ^ index
  257. mask |= mask >> 2
  258. mask |= mask >> 1
  259. mask &= 1
  260. mask--
  261. for j := range xOut {
  262. xOut[j] |= table[0] & mask
  263. table = table[1:]
  264. }
  265. for j := range yOut {
  266. yOut[j] |= table[0] & mask
  267. table = table[1:]
  268. }
  269. }
  270. }
  271. // sm2P256SelectJacobianPoint sets {out_x,out_y,out_z} to the index'th entry of
  272. // table.
  273. // On entry: index < 16, table[0] must be zero.
  274. func sm2P256SelectJacobianPoint(xOut, yOut, zOut *sm2P256FieldElement, table *[16][3]sm2P256FieldElement, index uint32) {
  275. for i := range xOut {
  276. xOut[i] = 0
  277. }
  278. for i := range yOut {
  279. yOut[i] = 0
  280. }
  281. for i := range zOut {
  282. zOut[i] = 0
  283. }
  284. // The implicit value at index 0 is all zero. We don't need to perform that
  285. // iteration of the loop because we already set out_* to zero.
  286. for i := uint32(1); i < 16; i++ {
  287. mask := i ^ index
  288. mask |= mask >> 2
  289. mask |= mask >> 1
  290. mask &= 1
  291. mask--
  292. for j := range xOut {
  293. xOut[j] |= table[i][0][j] & mask
  294. }
  295. for j := range yOut {
  296. yOut[j] |= table[i][1][j] & mask
  297. }
  298. for j := range zOut {
  299. zOut[j] |= table[i][2][j] & mask
  300. }
  301. }
  302. }
  303. // sm2P256GetBit returns the bit'th bit of scalar.
  304. func sm2P256GetBit(scalar *[32]uint8, bit uint) uint32 {
  305. return uint32(((scalar[bit>>3]) >> (bit & 7)) & 1)
  306. }
  307. // sm2P256ScalarBaseMult sets {xOut,yOut,zOut} = scalar*G where scalar is a
  308. // little-endian number. Note that the value of scalar must be less than the
  309. // order of the group.
  310. func sm2P256ScalarBaseMult(xOut, yOut, zOut *sm2P256FieldElement, scalar *[32]uint8) {
  311. nIsInfinityMask := ^uint32(0)
  312. var px, py, tx, ty, tz sm2P256FieldElement
  313. var pIsNoninfiniteMask, mask, tableOffset uint32
  314. for i := range xOut {
  315. xOut[i] = 0
  316. }
  317. for i := range yOut {
  318. yOut[i] = 0
  319. }
  320. for i := range zOut {
  321. zOut[i] = 0
  322. }
  323. // The loop adds bits at positions 0, 64, 128 and 192, followed by
  324. // positions 32,96,160 and 224 and does this 32 times.
  325. for i := uint(0); i < 32; i++ {
  326. if i != 0 {
  327. sm2P256PointDouble(xOut, yOut, zOut, xOut, yOut, zOut)
  328. }
  329. tableOffset = 0
  330. for j := uint(0); j <= 32; j += 32 {
  331. bit0 := sm2P256GetBit(scalar, 31-i+j)
  332. bit1 := sm2P256GetBit(scalar, 95-i+j)
  333. bit2 := sm2P256GetBit(scalar, 159-i+j)
  334. bit3 := sm2P256GetBit(scalar, 223-i+j)
  335. index := bit0 | (bit1 << 1) | (bit2 << 2) | (bit3 << 3)
  336. sm2P256SelectAffinePoint(&px, &py, sm2P256Precomputed[tableOffset:], index)
  337. tableOffset += 30 * 9
  338. // Since scalar is less than the order of the group, we know that
  339. // {xOut,yOut,zOut} != {px,py,1}, unless both are zero, which we handle
  340. // below.
  341. sm2P256PointAddMixed(&tx, &ty, &tz, xOut, yOut, zOut, &px, &py)
  342. // The result of pointAddMixed is incorrect if {xOut,yOut,zOut} is zero
  343. // (a.k.a. the point at infinity). We handle that situation by
  344. // copying the point from the table.
  345. sm2P256CopyConditional(xOut, &px, nIsInfinityMask)
  346. sm2P256CopyConditional(yOut, &py, nIsInfinityMask)
  347. sm2P256CopyConditional(zOut, &sm2P256Factor[1], nIsInfinityMask)
  348. // Equally, the result is also wrong if the point from the table is
  349. // zero, which happens when the index is zero. We handle that by
  350. // only copying from {tx,ty,tz} to {xOut,yOut,zOut} if index != 0.
  351. pIsNoninfiniteMask = nonZeroToAllOnes(index)
  352. mask = pIsNoninfiniteMask & ^nIsInfinityMask
  353. sm2P256CopyConditional(xOut, &tx, mask)
  354. sm2P256CopyConditional(yOut, &ty, mask)
  355. sm2P256CopyConditional(zOut, &tz, mask)
  356. // If p was not zero, then n is now non-zero.
  357. nIsInfinityMask &^= pIsNoninfiniteMask
  358. }
  359. }
  360. }
  361. func sm2P256PointToAffine(xOut, yOut, x, y, z *sm2P256FieldElement) {
  362. var zInv, zInvSq sm2P256FieldElement
  363. zz := sm2P256ToBig(z)
  364. zz.ModInverse(zz, sm2P256.P)
  365. sm2P256FromBig(&zInv, zz)
  366. sm2P256Square(&zInvSq, &zInv)
  367. sm2P256Mul(xOut, x, &zInvSq)
  368. sm2P256Mul(&zInv, &zInv, &zInvSq)
  369. sm2P256Mul(yOut, y, &zInv)
  370. }
  371. func sm2P256ToAffine(x, y, z *sm2P256FieldElement) (xOut, yOut *big.Int) {
  372. var xx, yy sm2P256FieldElement
  373. sm2P256PointToAffine(&xx, &yy, x, y, z)
  374. return sm2P256ToBig(&xx), sm2P256ToBig(&yy)
  375. }
  376. var sm2P256Factor = []sm2P256FieldElement{
  377. sm2P256FieldElement{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
  378. sm2P256FieldElement{0x2, 0x0, 0x1FFFFF00, 0x7FF, 0x0, 0x0, 0x0, 0x2000000, 0x0},
  379. sm2P256FieldElement{0x4, 0x0, 0x1FFFFE00, 0xFFF, 0x0, 0x0, 0x0, 0x4000000, 0x0},
  380. sm2P256FieldElement{0x6, 0x0, 0x1FFFFD00, 0x17FF, 0x0, 0x0, 0x0, 0x6000000, 0x0},
  381. sm2P256FieldElement{0x8, 0x0, 0x1FFFFC00, 0x1FFF, 0x0, 0x0, 0x0, 0x8000000, 0x0},
  382. sm2P256FieldElement{0xA, 0x0, 0x1FFFFB00, 0x27FF, 0x0, 0x0, 0x0, 0xA000000, 0x0},
  383. sm2P256FieldElement{0xC, 0x0, 0x1FFFFA00, 0x2FFF, 0x0, 0x0, 0x0, 0xC000000, 0x0},
  384. sm2P256FieldElement{0xE, 0x0, 0x1FFFF900, 0x37FF, 0x0, 0x0, 0x0, 0xE000000, 0x0},
  385. sm2P256FieldElement{0x10, 0x0, 0x1FFFF800, 0x3FFF, 0x0, 0x0, 0x0, 0x0, 0x01},
  386. }
  387. func sm2P256Scalar(b *sm2P256FieldElement, a int) {
  388. sm2P256Mul(b, b, &sm2P256Factor[a])
  389. }
  390. // (x3, y3, z3) = (x1, y1, z1) + (x2, y2, z2)
  391. func sm2P256PointAdd(x1, y1, z1, x2, y2, z2, x3, y3, z3 *sm2P256FieldElement) {
  392. var u1, u2, z22, z12, z23, z13, s1, s2, h, h2, r, r2, tm sm2P256FieldElement
  393. if sm2P256ToBig(z1).Sign() == 0 {
  394. sm2P256Dup(x3, x2)
  395. sm2P256Dup(y3, y2)
  396. sm2P256Dup(z3, z2)
  397. return
  398. }
  399. if sm2P256ToBig(z2).Sign() == 0 {
  400. sm2P256Dup(x3, x1)
  401. sm2P256Dup(y3, y1)
  402. sm2P256Dup(z3, z1)
  403. return
  404. }
  405. sm2P256Square(&z12, z1) // z12 = z1 ^ 2
  406. sm2P256Square(&z22, z2) // z22 = z2 ^ 2
  407. sm2P256Mul(&z13, &z12, z1) // z13 = z1 ^ 3
  408. sm2P256Mul(&z23, &z22, z2) // z23 = z2 ^ 3
  409. sm2P256Mul(&u1, x1, &z22) // u1 = x1 * z2 ^ 2
  410. sm2P256Mul(&u2, x2, &z12) // u2 = x2 * z1 ^ 2
  411. sm2P256Mul(&s1, y1, &z23) // s1 = y1 * z2 ^ 3
  412. sm2P256Mul(&s2, y2, &z13) // s2 = y2 * z1 ^ 3
  413. if sm2P256ToBig(&u1).Cmp(sm2P256ToBig(&u2)) == 0 &&
  414. sm2P256ToBig(&s1).Cmp(sm2P256ToBig(&s2)) == 0 {
  415. sm2P256PointDouble(x1, y1, z1, x1, y1, z1)
  416. }
  417. sm2P256Sub(&h, &u2, &u1) // h = u2 - u1
  418. sm2P256Sub(&r, &s2, &s1) // r = s2 - s1
  419. sm2P256Square(&r2, &r) // r2 = r ^ 2
  420. sm2P256Square(&h2, &h) // h2 = h ^ 2
  421. sm2P256Mul(&tm, &h2, &h) // tm = h ^ 3
  422. sm2P256Sub(x3, &r2, &tm)
  423. sm2P256Mul(&tm, &u1, &h2)
  424. sm2P256Scalar(&tm, 2) // tm = 2 * (u1 * h ^ 2)
  425. sm2P256Sub(x3, x3, &tm) // x3 = r ^ 2 - h ^ 3 - 2 * u1 * h ^ 2
  426. sm2P256Mul(&tm, &u1, &h2) // tm = u1 * h ^ 2
  427. sm2P256Sub(&tm, &tm, x3) // tm = u1 * h ^ 2 - x3
  428. sm2P256Mul(y3, &r, &tm)
  429. sm2P256Mul(&tm, &h2, &h) // tm = h ^ 3
  430. sm2P256Mul(&tm, &tm, &s1) // tm = s1 * h ^ 3
  431. sm2P256Sub(y3, y3, &tm) // y3 = r * (u1 * h ^ 2 - x3) - s1 * h ^ 3
  432. sm2P256Mul(z3, z1, z2)
  433. sm2P256Mul(z3, z3, &h) // z3 = z1 * z3 * h
  434. }
  435. // (x3, y3, z3) = (x1, y1, z1)- (x2, y2, z2)
  436. func sm2P256PointSub(x1, y1, z1, x2, y2, z2, x3, y3, z3 *sm2P256FieldElement) {
  437. var u1, u2, z22, z12, z23, z13, s1, s2, h, h2, r, r2, tm sm2P256FieldElement
  438. y:=sm2P256ToBig(y2)
  439. zero:=new(big.Int).SetInt64(0)
  440. y.Sub(zero,y)
  441. sm2P256FromBig(y2,y)
  442. if sm2P256ToBig(z1).Sign() == 0 {
  443. sm2P256Dup(x3, x2)
  444. sm2P256Dup(y3, y2)
  445. sm2P256Dup(z3, z2)
  446. return
  447. }
  448. if sm2P256ToBig(z2).Sign() == 0 {
  449. sm2P256Dup(x3, x1)
  450. sm2P256Dup(y3, y1)
  451. sm2P256Dup(z3, z1)
  452. return
  453. }
  454. sm2P256Square(&z12, z1) // z12 = z1 ^ 2
  455. sm2P256Square(&z22, z2) // z22 = z2 ^ 2
  456. sm2P256Mul(&z13, &z12, z1) // z13 = z1 ^ 3
  457. sm2P256Mul(&z23, &z22, z2) // z23 = z2 ^ 3
  458. sm2P256Mul(&u1, x1, &z22) // u1 = x1 * z2 ^ 2
  459. sm2P256Mul(&u2, x2, &z12) // u2 = x2 * z1 ^ 2
  460. sm2P256Mul(&s1, y1, &z23) // s1 = y1 * z2 ^ 3
  461. sm2P256Mul(&s2, y2, &z13) // s2 = y2 * z1 ^ 3
  462. if sm2P256ToBig(&u1).Cmp(sm2P256ToBig(&u2)) == 0 &&
  463. sm2P256ToBig(&s1).Cmp(sm2P256ToBig(&s2)) == 0 {
  464. sm2P256PointDouble(x1, y1, z1, x1, y1, z1)
  465. }
  466. sm2P256Sub(&h, &u2, &u1) // h = u2 - u1
  467. sm2P256Sub(&r, &s2, &s1) // r = s2 - s1
  468. sm2P256Square(&r2, &r) // r2 = r ^ 2
  469. sm2P256Square(&h2, &h) // h2 = h ^ 2
  470. sm2P256Mul(&tm, &h2, &h) // tm = h ^ 3
  471. sm2P256Sub(x3, &r2, &tm)
  472. sm2P256Mul(&tm, &u1, &h2)
  473. sm2P256Scalar(&tm, 2) // tm = 2 * (u1 * h ^ 2)
  474. sm2P256Sub(x3, x3, &tm) // x3 = r ^ 2 - h ^ 3 - 2 * u1 * h ^ 2
  475. sm2P256Mul(&tm, &u1, &h2) // tm = u1 * h ^ 2
  476. sm2P256Sub(&tm, &tm, x3) // tm = u1 * h ^ 2 - x3
  477. sm2P256Mul(y3, &r, &tm)
  478. sm2P256Mul(&tm, &h2, &h) // tm = h ^ 3
  479. sm2P256Mul(&tm, &tm, &s1) // tm = s1 * h ^ 3
  480. sm2P256Sub(y3, y3, &tm) // y3 = r * (u1 * h ^ 2 - x3) - s1 * h ^ 3
  481. sm2P256Mul(z3, z1, z2)
  482. sm2P256Mul(z3, z3, &h) // z3 = z1 * z3 * h
  483. }
  484. func sm2P256PointDouble(x3, y3, z3, x, y, z *sm2P256FieldElement) {
  485. var s, m, m2, x2, y2, z2, z4, y4, az4 sm2P256FieldElement
  486. sm2P256Square(&x2, x) // x2 = x ^ 2
  487. sm2P256Square(&y2, y) // y2 = y ^ 2
  488. sm2P256Square(&z2, z) // z2 = z ^ 2
  489. sm2P256Square(&z4, z) // z4 = z ^ 2
  490. sm2P256Mul(&z4, &z4, z) // z4 = z ^ 3
  491. sm2P256Mul(&z4, &z4, z) // z4 = z ^ 4
  492. sm2P256Square(&y4, y) // y4 = y ^ 2
  493. sm2P256Mul(&y4, &y4, y) // y4 = y ^ 3
  494. sm2P256Mul(&y4, &y4, y) // y4 = y ^ 4
  495. sm2P256Scalar(&y4, 8) // y4 = 8 * y ^ 4
  496. sm2P256Mul(&s, x, &y2)
  497. sm2P256Scalar(&s, 4) // s = 4 * x * y ^ 2
  498. sm2P256Dup(&m, &x2)
  499. sm2P256Scalar(&m, 3)
  500. sm2P256Mul(&az4, &sm2P256.a, &z4)
  501. sm2P256Add(&m, &m, &az4) // m = 3 * x ^ 2 + a * z ^ 4
  502. sm2P256Square(&m2, &m) // m2 = m ^ 2
  503. sm2P256Add(z3, y, z)
  504. sm2P256Square(z3, z3)
  505. sm2P256Sub(z3, z3, &z2)
  506. sm2P256Sub(z3, z3, &y2) // z' = (y + z) ^2 - z ^ 2 - y ^ 2
  507. sm2P256Sub(x3, &m2, &s)
  508. sm2P256Sub(x3, x3, &s) // x' = m2 - 2 * s
  509. sm2P256Sub(y3, &s, x3)
  510. sm2P256Mul(y3, y3, &m)
  511. sm2P256Sub(y3, y3, &y4) // y' = m * (s - x') - 8 * y ^ 4
  512. }
  513. // p256Zero31 is 0 mod p.
  514. var sm2P256Zero31 = sm2P256FieldElement{0x7FFFFFF8, 0x3FFFFFFC, 0x800003FC, 0x3FFFDFFC, 0x7FFFFFFC, 0x3FFFFFFC, 0x7FFFFFFC, 0x37FFFFFC, 0x7FFFFFFC}
  515. // c = a + b
  516. func sm2P256Add(c, a, b *sm2P256FieldElement) {
  517. carry := uint32(0)
  518. for i := 0; ; i++ {
  519. c[i] = a[i] + b[i]
  520. c[i] += carry
  521. carry = c[i] >> 29
  522. c[i] &= bottom29Bits
  523. i++
  524. if i == 9 {
  525. break
  526. }
  527. c[i] = a[i] + b[i]
  528. c[i] += carry
  529. carry = c[i] >> 28
  530. c[i] &= bottom28Bits
  531. }
  532. sm2P256ReduceCarry(c, carry)
  533. }
  534. // c = a - b
  535. func sm2P256Sub(c, a, b *sm2P256FieldElement) {
  536. var carry uint32
  537. for i := 0; ; i++ {
  538. c[i] = a[i] - b[i]
  539. c[i] += sm2P256Zero31[i]
  540. c[i] += carry
  541. carry = c[i] >> 29
  542. c[i] &= bottom29Bits
  543. i++
  544. if i == 9 {
  545. break
  546. }
  547. c[i] = a[i] - b[i]
  548. c[i] += sm2P256Zero31[i]
  549. c[i] += carry
  550. carry = c[i] >> 28
  551. c[i] &= bottom28Bits
  552. }
  553. sm2P256ReduceCarry(c, carry)
  554. }
  555. // c = a * b
  556. func sm2P256Mul(c, a, b *sm2P256FieldElement) {
  557. var tmp sm2P256LargeFieldElement
  558. tmp[0] = uint64(a[0]) * uint64(b[0])
  559. tmp[1] = uint64(a[0])*(uint64(b[1])<<0) +
  560. uint64(a[1])*(uint64(b[0])<<0)
  561. tmp[2] = uint64(a[0])*(uint64(b[2])<<0) +
  562. uint64(a[1])*(uint64(b[1])<<1) +
  563. uint64(a[2])*(uint64(b[0])<<0)
  564. tmp[3] = uint64(a[0])*(uint64(b[3])<<0) +
  565. uint64(a[1])*(uint64(b[2])<<0) +
  566. uint64(a[2])*(uint64(b[1])<<0) +
  567. uint64(a[3])*(uint64(b[0])<<0)
  568. tmp[4] = uint64(a[0])*(uint64(b[4])<<0) +
  569. uint64(a[1])*(uint64(b[3])<<1) +
  570. uint64(a[2])*(uint64(b[2])<<0) +
  571. uint64(a[3])*(uint64(b[1])<<1) +
  572. uint64(a[4])*(uint64(b[0])<<0)
  573. tmp[5] = uint64(a[0])*(uint64(b[5])<<0) +
  574. uint64(a[1])*(uint64(b[4])<<0) +
  575. uint64(a[2])*(uint64(b[3])<<0) +
  576. uint64(a[3])*(uint64(b[2])<<0) +
  577. uint64(a[4])*(uint64(b[1])<<0) +
  578. uint64(a[5])*(uint64(b[0])<<0)
  579. tmp[6] = uint64(a[0])*(uint64(b[6])<<0) +
  580. uint64(a[1])*(uint64(b[5])<<1) +
  581. uint64(a[2])*(uint64(b[4])<<0) +
  582. uint64(a[3])*(uint64(b[3])<<1) +
  583. uint64(a[4])*(uint64(b[2])<<0) +
  584. uint64(a[5])*(uint64(b[1])<<1) +
  585. uint64(a[6])*(uint64(b[0])<<0)
  586. tmp[7] = uint64(a[0])*(uint64(b[7])<<0) +
  587. uint64(a[1])*(uint64(b[6])<<0) +
  588. uint64(a[2])*(uint64(b[5])<<0) +
  589. uint64(a[3])*(uint64(b[4])<<0) +
  590. uint64(a[4])*(uint64(b[3])<<0) +
  591. uint64(a[5])*(uint64(b[2])<<0) +
  592. uint64(a[6])*(uint64(b[1])<<0) +
  593. uint64(a[7])*(uint64(b[0])<<0)
  594. // tmp[8] has the greatest value but doesn't overflow. See logic in
  595. // p256Square.
  596. tmp[8] = uint64(a[0])*(uint64(b[8])<<0) +
  597. uint64(a[1])*(uint64(b[7])<<1) +
  598. uint64(a[2])*(uint64(b[6])<<0) +
  599. uint64(a[3])*(uint64(b[5])<<1) +
  600. uint64(a[4])*(uint64(b[4])<<0) +
  601. uint64(a[5])*(uint64(b[3])<<1) +
  602. uint64(a[6])*(uint64(b[2])<<0) +
  603. uint64(a[7])*(uint64(b[1])<<1) +
  604. uint64(a[8])*(uint64(b[0])<<0)
  605. tmp[9] = uint64(a[1])*(uint64(b[8])<<0) +
  606. uint64(a[2])*(uint64(b[7])<<0) +
  607. uint64(a[3])*(uint64(b[6])<<0) +
  608. uint64(a[4])*(uint64(b[5])<<0) +
  609. uint64(a[5])*(uint64(b[4])<<0) +
  610. uint64(a[6])*(uint64(b[3])<<0) +
  611. uint64(a[7])*(uint64(b[2])<<0) +
  612. uint64(a[8])*(uint64(b[1])<<0)
  613. tmp[10] = uint64(a[2])*(uint64(b[8])<<0) +
  614. uint64(a[3])*(uint64(b[7])<<1) +
  615. uint64(a[4])*(uint64(b[6])<<0) +
  616. uint64(a[5])*(uint64(b[5])<<1) +
  617. uint64(a[6])*(uint64(b[4])<<0) +
  618. uint64(a[7])*(uint64(b[3])<<1) +
  619. uint64(a[8])*(uint64(b[2])<<0)
  620. tmp[11] = uint64(a[3])*(uint64(b[8])<<0) +
  621. uint64(a[4])*(uint64(b[7])<<0) +
  622. uint64(a[5])*(uint64(b[6])<<0) +
  623. uint64(a[6])*(uint64(b[5])<<0) +
  624. uint64(a[7])*(uint64(b[4])<<0) +
  625. uint64(a[8])*(uint64(b[3])<<0)
  626. tmp[12] = uint64(a[4])*(uint64(b[8])<<0) +
  627. uint64(a[5])*(uint64(b[7])<<1) +
  628. uint64(a[6])*(uint64(b[6])<<0) +
  629. uint64(a[7])*(uint64(b[5])<<1) +
  630. uint64(a[8])*(uint64(b[4])<<0)
  631. tmp[13] = uint64(a[5])*(uint64(b[8])<<0) +
  632. uint64(a[6])*(uint64(b[7])<<0) +
  633. uint64(a[7])*(uint64(b[6])<<0) +
  634. uint64(a[8])*(uint64(b[5])<<0)
  635. tmp[14] = uint64(a[6])*(uint64(b[8])<<0) +
  636. uint64(a[7])*(uint64(b[7])<<1) +
  637. uint64(a[8])*(uint64(b[6])<<0)
  638. tmp[15] = uint64(a[7])*(uint64(b[8])<<0) +
  639. uint64(a[8])*(uint64(b[7])<<0)
  640. tmp[16] = uint64(a[8]) * (uint64(b[8]) << 0)
  641. sm2P256ReduceDegree(c, &tmp)
  642. }
  643. // b = a * a
  644. func sm2P256Square(b, a *sm2P256FieldElement) {
  645. var tmp sm2P256LargeFieldElement
  646. tmp[0] = uint64(a[0]) * uint64(a[0])
  647. tmp[1] = uint64(a[0]) * (uint64(a[1]) << 1)
  648. tmp[2] = uint64(a[0])*(uint64(a[2])<<1) +
  649. uint64(a[1])*(uint64(a[1])<<1)
  650. tmp[3] = uint64(a[0])*(uint64(a[3])<<1) +
  651. uint64(a[1])*(uint64(a[2])<<1)
  652. tmp[4] = uint64(a[0])*(uint64(a[4])<<1) +
  653. uint64(a[1])*(uint64(a[3])<<2) +
  654. uint64(a[2])*uint64(a[2])
  655. tmp[5] = uint64(a[0])*(uint64(a[5])<<1) +
  656. uint64(a[1])*(uint64(a[4])<<1) +
  657. uint64(a[2])*(uint64(a[3])<<1)
  658. tmp[6] = uint64(a[0])*(uint64(a[6])<<1) +
  659. uint64(a[1])*(uint64(a[5])<<2) +
  660. uint64(a[2])*(uint64(a[4])<<1) +
  661. uint64(a[3])*(uint64(a[3])<<1)
  662. tmp[7] = uint64(a[0])*(uint64(a[7])<<1) +
  663. uint64(a[1])*(uint64(a[6])<<1) +
  664. uint64(a[2])*(uint64(a[5])<<1) +
  665. uint64(a[3])*(uint64(a[4])<<1)
  666. // tmp[8] has the greatest value of 2**61 + 2**60 + 2**61 + 2**60 + 2**60,
  667. // which is < 2**64 as required.
  668. tmp[8] = uint64(a[0])*(uint64(a[8])<<1) +
  669. uint64(a[1])*(uint64(a[7])<<2) +
  670. uint64(a[2])*(uint64(a[6])<<1) +
  671. uint64(a[3])*(uint64(a[5])<<2) +
  672. uint64(a[4])*uint64(a[4])
  673. tmp[9] = uint64(a[1])*(uint64(a[8])<<1) +
  674. uint64(a[2])*(uint64(a[7])<<1) +
  675. uint64(a[3])*(uint64(a[6])<<1) +
  676. uint64(a[4])*(uint64(a[5])<<1)
  677. tmp[10] = uint64(a[2])*(uint64(a[8])<<1) +
  678. uint64(a[3])*(uint64(a[7])<<2) +
  679. uint64(a[4])*(uint64(a[6])<<1) +
  680. uint64(a[5])*(uint64(a[5])<<1)
  681. tmp[11] = uint64(a[3])*(uint64(a[8])<<1) +
  682. uint64(a[4])*(uint64(a[7])<<1) +
  683. uint64(a[5])*(uint64(a[6])<<1)
  684. tmp[12] = uint64(a[4])*(uint64(a[8])<<1) +
  685. uint64(a[5])*(uint64(a[7])<<2) +
  686. uint64(a[6])*uint64(a[6])
  687. tmp[13] = uint64(a[5])*(uint64(a[8])<<1) +
  688. uint64(a[6])*(uint64(a[7])<<1)
  689. tmp[14] = uint64(a[6])*(uint64(a[8])<<1) +
  690. uint64(a[7])*(uint64(a[7])<<1)
  691. tmp[15] = uint64(a[7]) * (uint64(a[8]) << 1)
  692. tmp[16] = uint64(a[8]) * uint64(a[8])
  693. sm2P256ReduceDegree(b, &tmp)
  694. }
  695. // nonZeroToAllOnes returns:
  696. // 0xffffffff for 0 < x <= 2**31
  697. // 0 for x == 0 or x > 2**31.
  698. func nonZeroToAllOnes(x uint32) uint32 {
  699. return ((x - 1) >> 31) - 1
  700. }
  701. var sm2P256Carry = [8 * 9]uint32{
  702. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  703. 0x2, 0x0, 0x1FFFFF00, 0x7FF, 0x0, 0x0, 0x0, 0x2000000, 0x0,
  704. 0x4, 0x0, 0x1FFFFE00, 0xFFF, 0x0, 0x0, 0x0, 0x4000000, 0x0,
  705. 0x6, 0x0, 0x1FFFFD00, 0x17FF, 0x0, 0x0, 0x0, 0x6000000, 0x0,
  706. 0x8, 0x0, 0x1FFFFC00, 0x1FFF, 0x0, 0x0, 0x0, 0x8000000, 0x0,
  707. 0xA, 0x0, 0x1FFFFB00, 0x27FF, 0x0, 0x0, 0x0, 0xA000000, 0x0,
  708. 0xC, 0x0, 0x1FFFFA00, 0x2FFF, 0x0, 0x0, 0x0, 0xC000000, 0x0,
  709. 0xE, 0x0, 0x1FFFF900, 0x37FF, 0x0, 0x0, 0x0, 0xE000000, 0x0,
  710. }
  711. // carry < 2 ^ 3
  712. func sm2P256ReduceCarry(a *sm2P256FieldElement, carry uint32) {
  713. a[0] += sm2P256Carry[carry*9+0]
  714. a[2] += sm2P256Carry[carry*9+2]
  715. a[3] += sm2P256Carry[carry*9+3]
  716. a[7] += sm2P256Carry[carry*9+7]
  717. }
  718. func sm2P256ReduceDegree(a *sm2P256FieldElement, b *sm2P256LargeFieldElement) {
  719. var tmp [18]uint32
  720. var carry, x, xMask uint32
  721. // tmp
  722. // 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 ...
  723. // 29 | 28 | 29 | 28 | 29 | 28 | 29 | 28 | 29 | 28 | 29 ...
  724. tmp[0] = uint32(b[0]) & bottom29Bits
  725. tmp[1] = uint32(b[0]) >> 29
  726. tmp[1] |= (uint32(b[0]>>32) << 3) & bottom28Bits
  727. tmp[1] += uint32(b[1]) & bottom28Bits
  728. carry = tmp[1] >> 28
  729. tmp[1] &= bottom28Bits
  730. for i := 2; i < 17; i++ {
  731. tmp[i] = (uint32(b[i-2] >> 32)) >> 25
  732. tmp[i] += (uint32(b[i-1])) >> 28
  733. tmp[i] += (uint32(b[i-1]>>32) << 4) & bottom29Bits
  734. tmp[i] += uint32(b[i]) & bottom29Bits
  735. tmp[i] += carry
  736. carry = tmp[i] >> 29
  737. tmp[i] &= bottom29Bits
  738. i++
  739. if i == 17 {
  740. break
  741. }
  742. tmp[i] = uint32(b[i-2]>>32) >> 25
  743. tmp[i] += uint32(b[i-1]) >> 29
  744. tmp[i] += ((uint32(b[i-1] >> 32)) << 3) & bottom28Bits
  745. tmp[i] += uint32(b[i]) & bottom28Bits
  746. tmp[i] += carry
  747. carry = tmp[i] >> 28
  748. tmp[i] &= bottom28Bits
  749. }
  750. tmp[17] = uint32(b[15]>>32) >> 25
  751. tmp[17] += uint32(b[16]) >> 29
  752. tmp[17] += uint32(b[16]>>32) << 3
  753. tmp[17] += carry
  754. for i := 0; ; i += 2 {
  755. tmp[i+1] += tmp[i] >> 29
  756. x = tmp[i] & bottom29Bits
  757. tmp[i] = 0
  758. if x > 0 {
  759. set4 := uint32(0)
  760. set7 := uint32(0)
  761. xMask = nonZeroToAllOnes(x)
  762. tmp[i+2] += (x << 7) & bottom29Bits
  763. tmp[i+3] += x >> 22
  764. if tmp[i+3] < 0x10000000 {
  765. set4 = 1
  766. tmp[i+3] += 0x10000000 & xMask
  767. tmp[i+3] -= (x << 10) & bottom28Bits
  768. } else {
  769. tmp[i+3] -= (x << 10) & bottom28Bits
  770. }
  771. if tmp[i+4] < 0x20000000 {
  772. tmp[i+4] += 0x20000000 & xMask
  773. tmp[i+4] -= set4 // 借位
  774. tmp[i+4] -= x >> 18
  775. if tmp[i+5] < 0x10000000 {
  776. tmp[i+5] += 0x10000000 & xMask
  777. tmp[i+5] -= 1 // 借位
  778. if tmp[i+6] < 0x20000000 {
  779. set7 = 1
  780. tmp[i+6] += 0x20000000 & xMask
  781. tmp[i+6] -= 1 // 借位
  782. } else {
  783. tmp[i+6] -= 1 // 借位
  784. }
  785. } else {
  786. tmp[i+5] -= 1
  787. }
  788. } else {
  789. tmp[i+4] -= set4 // 借位
  790. tmp[i+4] -= x >> 18
  791. }
  792. if tmp[i+7] < 0x10000000 {
  793. tmp[i+7] += 0x10000000 & xMask
  794. tmp[i+7] -= set7
  795. tmp[i+7] -= (x << 24) & bottom28Bits
  796. tmp[i+8] += (x << 28) & bottom29Bits
  797. if tmp[i+8] < 0x20000000 {
  798. tmp[i+8] += 0x20000000 & xMask
  799. tmp[i+8] -= 1
  800. tmp[i+8] -= x >> 4
  801. tmp[i+9] += ((x >> 1) - 1) & xMask
  802. } else {
  803. tmp[i+8] -= 1
  804. tmp[i+8] -= x >> 4
  805. tmp[i+9] += (x >> 1) & xMask
  806. }
  807. } else {
  808. tmp[i+7] -= set7 // 借位
  809. tmp[i+7] -= (x << 24) & bottom28Bits
  810. tmp[i+8] += (x << 28) & bottom29Bits
  811. if tmp[i+8] < 0x20000000 {
  812. tmp[i+8] += 0x20000000 & xMask
  813. tmp[i+8] -= x >> 4
  814. tmp[i+9] += ((x >> 1) - 1) & xMask
  815. } else {
  816. tmp[i+8] -= x >> 4
  817. tmp[i+9] += (x >> 1) & xMask
  818. }
  819. }
  820. }
  821. if i+1 == 9 {
  822. break
  823. }
  824. tmp[i+2] += tmp[i+1] >> 28
  825. x = tmp[i+1] & bottom28Bits
  826. tmp[i+1] = 0
  827. if x > 0 {
  828. set5 := uint32(0)
  829. set8 := uint32(0)
  830. set9 := uint32(0)
  831. xMask = nonZeroToAllOnes(x)
  832. tmp[i+3] += (x << 7) & bottom28Bits
  833. tmp[i+4] += x >> 21
  834. if tmp[i+4] < 0x20000000 {
  835. set5 = 1
  836. tmp[i+4] += 0x20000000 & xMask
  837. tmp[i+4] -= (x << 11) & bottom29Bits
  838. } else {
  839. tmp[i+4] -= (x << 11) & bottom29Bits
  840. }
  841. if tmp[i+5] < 0x10000000 {
  842. tmp[i+5] += 0x10000000 & xMask
  843. tmp[i+5] -= set5 // 借位
  844. tmp[i+5] -= x >> 18
  845. if tmp[i+6] < 0x20000000 {
  846. tmp[i+6] += 0x20000000 & xMask
  847. tmp[i+6] -= 1 // 借位
  848. if tmp[i+7] < 0x10000000 {
  849. set8 = 1
  850. tmp[i+7] += 0x10000000 & xMask
  851. tmp[i+7] -= 1 // 借位
  852. } else {
  853. tmp[i+7] -= 1 // 借位
  854. }
  855. } else {
  856. tmp[i+6] -= 1 // 借位
  857. }
  858. } else {
  859. tmp[i+5] -= set5 // 借位
  860. tmp[i+5] -= x >> 18
  861. }
  862. if tmp[i+8] < 0x20000000 {
  863. set9 = 1
  864. tmp[i+8] += 0x20000000 & xMask
  865. tmp[i+8] -= set8
  866. tmp[i+8] -= (x << 25) & bottom29Bits
  867. } else {
  868. tmp[i+8] -= set8
  869. tmp[i+8] -= (x << 25) & bottom29Bits
  870. }
  871. if tmp[i+9] < 0x10000000 {
  872. tmp[i+9] += 0x10000000 & xMask
  873. tmp[i+9] -= set9 // 借位
  874. tmp[i+9] -= x >> 4
  875. tmp[i+10] += (x - 1) & xMask
  876. } else {
  877. tmp[i+9] -= set9 // 借位
  878. tmp[i+9] -= x >> 4
  879. tmp[i+10] += x & xMask
  880. }
  881. }
  882. }
  883. carry = uint32(0)
  884. for i := 0; i < 8; i++ {
  885. a[i] = tmp[i+9]
  886. a[i] += carry
  887. a[i] += (tmp[i+10] << 28) & bottom29Bits
  888. carry = a[i] >> 29
  889. a[i] &= bottom29Bits
  890. i++
  891. a[i] = tmp[i+9] >> 1
  892. a[i] += carry
  893. carry = a[i] >> 28
  894. a[i] &= bottom28Bits
  895. }
  896. a[8] = tmp[17]
  897. a[8] += carry
  898. carry = a[8] >> 29
  899. a[8] &= bottom29Bits
  900. sm2P256ReduceCarry(a, carry)
  901. }
  902. // b = a
  903. func sm2P256Dup(b, a *sm2P256FieldElement) {
  904. *b = *a
  905. }
  906. // X = a * R mod P
  907. func sm2P256FromBig(X *sm2P256FieldElement, a *big.Int) {
  908. x := new(big.Int).Lsh(a, 257)
  909. x.Mod(x, sm2P256.P)
  910. for i := 0; i < 9; i++ {
  911. if bits := x.Bits(); len(bits) > 0 {
  912. X[i] = uint32(bits[0]) & bottom29Bits
  913. } else {
  914. X[i] = 0
  915. }
  916. x.Rsh(x, 29)
  917. i++
  918. if i == 9 {
  919. break
  920. }
  921. if bits := x.Bits(); len(bits) > 0 {
  922. X[i] = uint32(bits[0]) & bottom28Bits
  923. } else {
  924. X[i] = 0
  925. }
  926. x.Rsh(x, 28)
  927. }
  928. }
  929. // X = r * R mod P
  930. // r = X * R' mod P
  931. func sm2P256ToBig(X *sm2P256FieldElement) *big.Int {
  932. r, tm := new(big.Int), new(big.Int)
  933. r.SetInt64(int64(X[8]))
  934. for i := 7; i >= 0; i-- {
  935. if (i & 1) == 0 {
  936. r.Lsh(r, 29)
  937. } else {
  938. r.Lsh(r, 28)
  939. }
  940. tm.SetInt64(int64(X[i]))
  941. r.Add(r, tm)
  942. }
  943. r.Mul(r, sm2P256.RInverse)
  944. r.Mod(r, sm2P256.P)
  945. return r
  946. }
  947. func WNafReversed(wnaf []int8) []int8 {
  948. wnafRev := make([]int8, len(wnaf), len(wnaf))
  949. for i, v := range wnaf {
  950. wnafRev[len(wnaf)-(1+i)] = v
  951. }
  952. return wnafRev
  953. }
  954. func sm2GenrateWNaf(b []byte) []int8 {
  955. n:= new(big.Int).SetBytes(b)
  956. var k *big.Int
  957. if n.Cmp(sm2P256.N) >= 0 {
  958. n.Mod(n, sm2P256.N)
  959. k = n
  960. } else {
  961. k = n
  962. }
  963. wnaf := make([]int8, k.BitLen()+1, k.BitLen()+1)
  964. if k.Sign() == 0 {
  965. return wnaf
  966. }
  967. var width, pow2, sign int
  968. width, pow2, sign = 4, 16, 8
  969. var mask int64 = 15
  970. var carry bool
  971. var length, pos int
  972. for pos <= k.BitLen() {
  973. if k.Bit(pos) == boolToUint(carry) {
  974. pos++
  975. continue
  976. }
  977. k.Rsh(k, uint(pos))
  978. var digit int
  979. digit = int(k.Int64() & mask)
  980. if carry {
  981. digit++
  982. }
  983. carry = (digit & sign) != 0
  984. if carry {
  985. digit -= pow2
  986. }
  987. length += pos
  988. wnaf[length] = int8(digit)
  989. pos = int(width)
  990. }
  991. if len(wnaf) > length+1 {
  992. t := make([]int8, length+1, length+1)
  993. copy(t, wnaf[0:length+1])
  994. wnaf = t
  995. }
  996. return wnaf
  997. }
  998. func boolToUint(b bool) uint {
  999. if b {
  1000. return 1
  1001. }
  1002. return 0
  1003. }
  1004. func abs(a int8) uint32{
  1005. if a<0 {
  1006. return uint32(-a)
  1007. }
  1008. return uint32(a)
  1009. }
  1010. func sm2P256ScalarMult(xOut, yOut, zOut, x, y *sm2P256FieldElement, scalar []int8) {
  1011. var precomp [16][3]sm2P256FieldElement
  1012. var px, py, pz, tx, ty, tz sm2P256FieldElement
  1013. var nIsInfinityMask, index, pIsNoninfiniteMask, mask uint32
  1014. // We precompute 0,1,2,... times {x,y}.
  1015. precomp[1][0] = *x
  1016. precomp[1][1] = *y
  1017. precomp[1][2] = sm2P256Factor[1]
  1018. for i := 2; i < 8; i += 2 {
  1019. sm2P256PointDouble(&precomp[i][0], &precomp[i][1], &precomp[i][2], &precomp[i/2][0], &precomp[i/2][1], &precomp[i/2][2])
  1020. sm2P256PointAddMixed(&precomp[i+1][0], &precomp[i+1][1], &precomp[i+1][2], &precomp[i][0], &precomp[i][1], &precomp[i][2], x, y)
  1021. }
  1022. for i := range xOut {
  1023. xOut[i] = 0
  1024. }
  1025. for i := range yOut {
  1026. yOut[i] = 0
  1027. }
  1028. for i := range zOut {
  1029. zOut[i] = 0
  1030. }
  1031. nIsInfinityMask = ^uint32(0)
  1032. var zeroes int16
  1033. for i := 0; i<len(scalar); i++ {
  1034. if scalar[i] ==0{
  1035. zeroes++
  1036. continue
  1037. }
  1038. if(zeroes>0){
  1039. for ;zeroes>0;zeroes-- {
  1040. sm2P256PointDouble(xOut, yOut, zOut, xOut, yOut, zOut)
  1041. }
  1042. }
  1043. index = abs(scalar[i])
  1044. sm2P256PointDouble(xOut, yOut, zOut, xOut, yOut, zOut)
  1045. sm2P256SelectJacobianPoint(&px, &py, &pz, &precomp, index)
  1046. if scalar[i] > 0 {
  1047. sm2P256PointAdd(xOut, yOut, zOut, &px, &py, &pz, &tx, &ty, &tz)
  1048. } else {
  1049. sm2P256PointSub(xOut, yOut, zOut, &px, &py, &pz, &tx, &ty, &tz)
  1050. }
  1051. sm2P256CopyConditional(xOut, &px, nIsInfinityMask)
  1052. sm2P256CopyConditional(yOut, &py, nIsInfinityMask)
  1053. sm2P256CopyConditional(zOut, &pz, nIsInfinityMask)
  1054. pIsNoninfiniteMask = nonZeroToAllOnes(index)
  1055. mask = pIsNoninfiniteMask & ^nIsInfinityMask
  1056. sm2P256CopyConditional(xOut, &tx, mask)
  1057. sm2P256CopyConditional(yOut, &ty, mask)
  1058. sm2P256CopyConditional(zOut, &tz, mask)
  1059. nIsInfinityMask &^= pIsNoninfiniteMask
  1060. }
  1061. if(zeroes>0){
  1062. for ;zeroes>0;zeroes-- {
  1063. sm2P256PointDouble(xOut, yOut, zOut, xOut, yOut, zOut)
  1064. }
  1065. }
  1066. }