x448_ref.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2014-2015 Cryptography Research, Inc.
  4. // Copyright (c) 2015 Yawning Angel.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. package x448
  24. // This should really use 64 bit limbs, but Go is fucking retarded and doesn't
  25. // have __(u)int128_t, so the 32 bit code it is, at a hefty performance
  26. // penalty. Fuck my life, I'm going to have to bust out PeachPy to get this
  27. // to go fast aren't I.
  28. const (
  29. wBits = 32
  30. lBits = (wBits * 7 / 8)
  31. x448Limbs = (448 / lBits)
  32. lMask = (1 << lBits) - 1
  33. )
  34. type limbUint uint32
  35. type limbSint int32
  36. type gf struct {
  37. limb [x448Limbs]uint32
  38. }
  39. var zero = gf{[x448Limbs]uint32{0}}
  40. var one = gf{[x448Limbs]uint32{1}}
  41. var p = gf{[x448Limbs]uint32{
  42. lMask, lMask, lMask, lMask, lMask, lMask, lMask, lMask,
  43. lMask - 1, lMask, lMask, lMask, lMask, lMask, lMask, lMask,
  44. }}
  45. // cpy copies x = y.
  46. func (x *gf) cpy(y *gf) {
  47. // for i, v := range y.limb {
  48. // x.limb[i] = v
  49. // }
  50. copy(x.limb[:], y.limb[:])
  51. }
  52. // mul multiplies c = a * b. (PERF)
  53. func (c *gf) mul(a, b *gf) {
  54. var aa gf
  55. aa.cpy(a)
  56. //
  57. // This is *by far* the most CPU intesive routine in the code.
  58. //
  59. // var accum [x448Limbs]uint64
  60. // for i, bv := range b.limb {
  61. // for j, aav := range aa.limb {
  62. // accum[(i+j)%x448Limbs] += (uint64)(bv) * (uint64)(aav)
  63. // }
  64. // aa.limb[(x448Limbs-1-i)^(x448Limbs/2)] += aa.limb[x448Limbs-1-i]
  65. // }
  66. // So fucking stupid that this is actually a fairly massive gain.
  67. var accum0, accum1, accum2, accum3, accum4, accum5, accum6, accum7, accum8, accum9, accum10, accum11, accum12, accum13, accum14, accum15 uint64
  68. var bv uint64
  69. bv = (uint64)(b.limb[0])
  70. accum0 += bv * (uint64)(aa.limb[0])
  71. accum1 += bv * (uint64)(aa.limb[1])
  72. accum2 += bv * (uint64)(aa.limb[2])
  73. accum3 += bv * (uint64)(aa.limb[3])
  74. accum4 += bv * (uint64)(aa.limb[4])
  75. accum5 += bv * (uint64)(aa.limb[5])
  76. accum6 += bv * (uint64)(aa.limb[6])
  77. accum7 += bv * (uint64)(aa.limb[7])
  78. accum8 += bv * (uint64)(aa.limb[8])
  79. accum9 += bv * (uint64)(aa.limb[9])
  80. accum10 += bv * (uint64)(aa.limb[10])
  81. accum11 += bv * (uint64)(aa.limb[11])
  82. accum12 += bv * (uint64)(aa.limb[12])
  83. accum13 += bv * (uint64)(aa.limb[13])
  84. accum14 += bv * (uint64)(aa.limb[14])
  85. accum15 += bv * (uint64)(aa.limb[15])
  86. aa.limb[(x448Limbs-1-0)^(x448Limbs/2)] += aa.limb[x448Limbs-1-0]
  87. bv = (uint64)(b.limb[1])
  88. accum1 += bv * (uint64)(aa.limb[0])
  89. accum2 += bv * (uint64)(aa.limb[1])
  90. accum3 += bv * (uint64)(aa.limb[2])
  91. accum4 += bv * (uint64)(aa.limb[3])
  92. accum5 += bv * (uint64)(aa.limb[4])
  93. accum6 += bv * (uint64)(aa.limb[5])
  94. accum7 += bv * (uint64)(aa.limb[6])
  95. accum8 += bv * (uint64)(aa.limb[7])
  96. accum9 += bv * (uint64)(aa.limb[8])
  97. accum10 += bv * (uint64)(aa.limb[9])
  98. accum11 += bv * (uint64)(aa.limb[10])
  99. accum12 += bv * (uint64)(aa.limb[11])
  100. accum13 += bv * (uint64)(aa.limb[12])
  101. accum14 += bv * (uint64)(aa.limb[13])
  102. accum15 += bv * (uint64)(aa.limb[14])
  103. accum0 += bv * (uint64)(aa.limb[15])
  104. aa.limb[(x448Limbs-1-1)^(x448Limbs/2)] += aa.limb[x448Limbs-1-1]
  105. bv = (uint64)(b.limb[2])
  106. accum2 += bv * (uint64)(aa.limb[0])
  107. accum3 += bv * (uint64)(aa.limb[1])
  108. accum4 += bv * (uint64)(aa.limb[2])
  109. accum5 += bv * (uint64)(aa.limb[3])
  110. accum6 += bv * (uint64)(aa.limb[4])
  111. accum7 += bv * (uint64)(aa.limb[5])
  112. accum8 += bv * (uint64)(aa.limb[6])
  113. accum9 += bv * (uint64)(aa.limb[7])
  114. accum10 += bv * (uint64)(aa.limb[8])
  115. accum11 += bv * (uint64)(aa.limb[9])
  116. accum12 += bv * (uint64)(aa.limb[10])
  117. accum13 += bv * (uint64)(aa.limb[11])
  118. accum14 += bv * (uint64)(aa.limb[12])
  119. accum15 += bv * (uint64)(aa.limb[13])
  120. accum0 += bv * (uint64)(aa.limb[14])
  121. accum1 += bv * (uint64)(aa.limb[15])
  122. aa.limb[(x448Limbs-1-2)^(x448Limbs/2)] += aa.limb[x448Limbs-1-2]
  123. bv = (uint64)(b.limb[3])
  124. accum3 += bv * (uint64)(aa.limb[0])
  125. accum4 += bv * (uint64)(aa.limb[1])
  126. accum5 += bv * (uint64)(aa.limb[2])
  127. accum6 += bv * (uint64)(aa.limb[3])
  128. accum7 += bv * (uint64)(aa.limb[4])
  129. accum8 += bv * (uint64)(aa.limb[5])
  130. accum9 += bv * (uint64)(aa.limb[6])
  131. accum10 += bv * (uint64)(aa.limb[7])
  132. accum11 += bv * (uint64)(aa.limb[8])
  133. accum12 += bv * (uint64)(aa.limb[9])
  134. accum13 += bv * (uint64)(aa.limb[10])
  135. accum14 += bv * (uint64)(aa.limb[11])
  136. accum15 += bv * (uint64)(aa.limb[12])
  137. accum0 += bv * (uint64)(aa.limb[13])
  138. accum1 += bv * (uint64)(aa.limb[14])
  139. accum2 += bv * (uint64)(aa.limb[15])
  140. aa.limb[(x448Limbs-1-3)^(x448Limbs/2)] += aa.limb[x448Limbs-1-3]
  141. bv = (uint64)(b.limb[4])
  142. accum4 += bv * (uint64)(aa.limb[0])
  143. accum5 += bv * (uint64)(aa.limb[1])
  144. accum6 += bv * (uint64)(aa.limb[2])
  145. accum7 += bv * (uint64)(aa.limb[3])
  146. accum8 += bv * (uint64)(aa.limb[4])
  147. accum9 += bv * (uint64)(aa.limb[5])
  148. accum10 += bv * (uint64)(aa.limb[6])
  149. accum11 += bv * (uint64)(aa.limb[7])
  150. accum12 += bv * (uint64)(aa.limb[8])
  151. accum13 += bv * (uint64)(aa.limb[9])
  152. accum14 += bv * (uint64)(aa.limb[10])
  153. accum15 += bv * (uint64)(aa.limb[11])
  154. accum0 += bv * (uint64)(aa.limb[12])
  155. accum1 += bv * (uint64)(aa.limb[13])
  156. accum2 += bv * (uint64)(aa.limb[14])
  157. accum3 += bv * (uint64)(aa.limb[15])
  158. aa.limb[(x448Limbs-1-4)^(x448Limbs/2)] += aa.limb[x448Limbs-1-4]
  159. bv = (uint64)(b.limb[5])
  160. accum5 += bv * (uint64)(aa.limb[0])
  161. accum6 += bv * (uint64)(aa.limb[1])
  162. accum7 += bv * (uint64)(aa.limb[2])
  163. accum8 += bv * (uint64)(aa.limb[3])
  164. accum9 += bv * (uint64)(aa.limb[4])
  165. accum10 += bv * (uint64)(aa.limb[5])
  166. accum11 += bv * (uint64)(aa.limb[6])
  167. accum12 += bv * (uint64)(aa.limb[7])
  168. accum13 += bv * (uint64)(aa.limb[8])
  169. accum14 += bv * (uint64)(aa.limb[9])
  170. accum15 += bv * (uint64)(aa.limb[10])
  171. accum0 += bv * (uint64)(aa.limb[11])
  172. accum1 += bv * (uint64)(aa.limb[12])
  173. accum2 += bv * (uint64)(aa.limb[13])
  174. accum3 += bv * (uint64)(aa.limb[14])
  175. accum4 += bv * (uint64)(aa.limb[15])
  176. aa.limb[(x448Limbs-1-5)^(x448Limbs/2)] += aa.limb[x448Limbs-1-5]
  177. bv = (uint64)(b.limb[6])
  178. accum6 += bv * (uint64)(aa.limb[0])
  179. accum7 += bv * (uint64)(aa.limb[1])
  180. accum8 += bv * (uint64)(aa.limb[2])
  181. accum9 += bv * (uint64)(aa.limb[3])
  182. accum10 += bv * (uint64)(aa.limb[4])
  183. accum11 += bv * (uint64)(aa.limb[5])
  184. accum12 += bv * (uint64)(aa.limb[6])
  185. accum13 += bv * (uint64)(aa.limb[7])
  186. accum14 += bv * (uint64)(aa.limb[8])
  187. accum15 += bv * (uint64)(aa.limb[9])
  188. accum0 += bv * (uint64)(aa.limb[10])
  189. accum1 += bv * (uint64)(aa.limb[11])
  190. accum2 += bv * (uint64)(aa.limb[12])
  191. accum3 += bv * (uint64)(aa.limb[13])
  192. accum4 += bv * (uint64)(aa.limb[14])
  193. accum5 += bv * (uint64)(aa.limb[15])
  194. aa.limb[(x448Limbs-1-6)^(x448Limbs/2)] += aa.limb[x448Limbs-1-6]
  195. bv = (uint64)(b.limb[7])
  196. accum7 += bv * (uint64)(aa.limb[0])
  197. accum8 += bv * (uint64)(aa.limb[1])
  198. accum9 += bv * (uint64)(aa.limb[2])
  199. accum10 += bv * (uint64)(aa.limb[3])
  200. accum11 += bv * (uint64)(aa.limb[4])
  201. accum12 += bv * (uint64)(aa.limb[5])
  202. accum13 += bv * (uint64)(aa.limb[6])
  203. accum14 += bv * (uint64)(aa.limb[7])
  204. accum15 += bv * (uint64)(aa.limb[8])
  205. accum0 += bv * (uint64)(aa.limb[9])
  206. accum1 += bv * (uint64)(aa.limb[10])
  207. accum2 += bv * (uint64)(aa.limb[11])
  208. accum3 += bv * (uint64)(aa.limb[12])
  209. accum4 += bv * (uint64)(aa.limb[13])
  210. accum5 += bv * (uint64)(aa.limb[14])
  211. accum6 += bv * (uint64)(aa.limb[15])
  212. aa.limb[(x448Limbs-1-7)^(x448Limbs/2)] += aa.limb[x448Limbs-1-7]
  213. bv = (uint64)(b.limb[8])
  214. accum8 += bv * (uint64)(aa.limb[0])
  215. accum9 += bv * (uint64)(aa.limb[1])
  216. accum10 += bv * (uint64)(aa.limb[2])
  217. accum11 += bv * (uint64)(aa.limb[3])
  218. accum12 += bv * (uint64)(aa.limb[4])
  219. accum13 += bv * (uint64)(aa.limb[5])
  220. accum14 += bv * (uint64)(aa.limb[6])
  221. accum15 += bv * (uint64)(aa.limb[7])
  222. accum0 += bv * (uint64)(aa.limb[8])
  223. accum1 += bv * (uint64)(aa.limb[9])
  224. accum2 += bv * (uint64)(aa.limb[10])
  225. accum3 += bv * (uint64)(aa.limb[11])
  226. accum4 += bv * (uint64)(aa.limb[12])
  227. accum5 += bv * (uint64)(aa.limb[13])
  228. accum6 += bv * (uint64)(aa.limb[14])
  229. accum7 += bv * (uint64)(aa.limb[15])
  230. aa.limb[(x448Limbs-1-8)^(x448Limbs/2)] += aa.limb[x448Limbs-1-8]
  231. bv = (uint64)(b.limb[9])
  232. accum9 += bv * (uint64)(aa.limb[0])
  233. accum10 += bv * (uint64)(aa.limb[1])
  234. accum11 += bv * (uint64)(aa.limb[2])
  235. accum12 += bv * (uint64)(aa.limb[3])
  236. accum13 += bv * (uint64)(aa.limb[4])
  237. accum14 += bv * (uint64)(aa.limb[5])
  238. accum15 += bv * (uint64)(aa.limb[6])
  239. accum0 += bv * (uint64)(aa.limb[7])
  240. accum1 += bv * (uint64)(aa.limb[8])
  241. accum2 += bv * (uint64)(aa.limb[9])
  242. accum3 += bv * (uint64)(aa.limb[10])
  243. accum4 += bv * (uint64)(aa.limb[11])
  244. accum5 += bv * (uint64)(aa.limb[12])
  245. accum6 += bv * (uint64)(aa.limb[13])
  246. accum7 += bv * (uint64)(aa.limb[14])
  247. accum8 += bv * (uint64)(aa.limb[15])
  248. aa.limb[(x448Limbs-1-9)^(x448Limbs/2)] += aa.limb[x448Limbs-1-9]
  249. bv = (uint64)(b.limb[10])
  250. accum10 += bv * (uint64)(aa.limb[0])
  251. accum11 += bv * (uint64)(aa.limb[1])
  252. accum12 += bv * (uint64)(aa.limb[2])
  253. accum13 += bv * (uint64)(aa.limb[3])
  254. accum14 += bv * (uint64)(aa.limb[4])
  255. accum15 += bv * (uint64)(aa.limb[5])
  256. accum0 += bv * (uint64)(aa.limb[6])
  257. accum1 += bv * (uint64)(aa.limb[7])
  258. accum2 += bv * (uint64)(aa.limb[8])
  259. accum3 += bv * (uint64)(aa.limb[9])
  260. accum4 += bv * (uint64)(aa.limb[10])
  261. accum5 += bv * (uint64)(aa.limb[11])
  262. accum6 += bv * (uint64)(aa.limb[12])
  263. accum7 += bv * (uint64)(aa.limb[13])
  264. accum8 += bv * (uint64)(aa.limb[14])
  265. accum9 += bv * (uint64)(aa.limb[15])
  266. aa.limb[(x448Limbs-1-10)^(x448Limbs/2)] += aa.limb[x448Limbs-1-10]
  267. bv = (uint64)(b.limb[11])
  268. accum11 += bv * (uint64)(aa.limb[0])
  269. accum12 += bv * (uint64)(aa.limb[1])
  270. accum13 += bv * (uint64)(aa.limb[2])
  271. accum14 += bv * (uint64)(aa.limb[3])
  272. accum15 += bv * (uint64)(aa.limb[4])
  273. accum0 += bv * (uint64)(aa.limb[5])
  274. accum1 += bv * (uint64)(aa.limb[6])
  275. accum2 += bv * (uint64)(aa.limb[7])
  276. accum3 += bv * (uint64)(aa.limb[8])
  277. accum4 += bv * (uint64)(aa.limb[9])
  278. accum5 += bv * (uint64)(aa.limb[10])
  279. accum6 += bv * (uint64)(aa.limb[11])
  280. accum7 += bv * (uint64)(aa.limb[12])
  281. accum8 += bv * (uint64)(aa.limb[13])
  282. accum9 += bv * (uint64)(aa.limb[14])
  283. accum10 += bv * (uint64)(aa.limb[15])
  284. aa.limb[(x448Limbs-1-11)^(x448Limbs/2)] += aa.limb[x448Limbs-1-11]
  285. bv = (uint64)(b.limb[12])
  286. accum12 += bv * (uint64)(aa.limb[0])
  287. accum13 += bv * (uint64)(aa.limb[1])
  288. accum14 += bv * (uint64)(aa.limb[2])
  289. accum15 += bv * (uint64)(aa.limb[3])
  290. accum0 += bv * (uint64)(aa.limb[4])
  291. accum1 += bv * (uint64)(aa.limb[5])
  292. accum2 += bv * (uint64)(aa.limb[6])
  293. accum3 += bv * (uint64)(aa.limb[7])
  294. accum4 += bv * (uint64)(aa.limb[8])
  295. accum5 += bv * (uint64)(aa.limb[9])
  296. accum6 += bv * (uint64)(aa.limb[10])
  297. accum7 += bv * (uint64)(aa.limb[11])
  298. accum8 += bv * (uint64)(aa.limb[12])
  299. accum9 += bv * (uint64)(aa.limb[13])
  300. accum10 += bv * (uint64)(aa.limb[14])
  301. accum11 += bv * (uint64)(aa.limb[15])
  302. aa.limb[(x448Limbs-1-12)^(x448Limbs/2)] += aa.limb[x448Limbs-1-12]
  303. bv = (uint64)(b.limb[13])
  304. accum13 += bv * (uint64)(aa.limb[0])
  305. accum14 += bv * (uint64)(aa.limb[1])
  306. accum15 += bv * (uint64)(aa.limb[2])
  307. accum0 += bv * (uint64)(aa.limb[3])
  308. accum1 += bv * (uint64)(aa.limb[4])
  309. accum2 += bv * (uint64)(aa.limb[5])
  310. accum3 += bv * (uint64)(aa.limb[6])
  311. accum4 += bv * (uint64)(aa.limb[7])
  312. accum5 += bv * (uint64)(aa.limb[8])
  313. accum6 += bv * (uint64)(aa.limb[9])
  314. accum7 += bv * (uint64)(aa.limb[10])
  315. accum8 += bv * (uint64)(aa.limb[11])
  316. accum9 += bv * (uint64)(aa.limb[12])
  317. accum10 += bv * (uint64)(aa.limb[13])
  318. accum11 += bv * (uint64)(aa.limb[14])
  319. accum12 += bv * (uint64)(aa.limb[15])
  320. aa.limb[(x448Limbs-1-13)^(x448Limbs/2)] += aa.limb[x448Limbs-1-13]
  321. bv = (uint64)(b.limb[14])
  322. accum14 += bv * (uint64)(aa.limb[0])
  323. accum15 += bv * (uint64)(aa.limb[1])
  324. accum0 += bv * (uint64)(aa.limb[2])
  325. accum1 += bv * (uint64)(aa.limb[3])
  326. accum2 += bv * (uint64)(aa.limb[4])
  327. accum3 += bv * (uint64)(aa.limb[5])
  328. accum4 += bv * (uint64)(aa.limb[6])
  329. accum5 += bv * (uint64)(aa.limb[7])
  330. accum6 += bv * (uint64)(aa.limb[8])
  331. accum7 += bv * (uint64)(aa.limb[9])
  332. accum8 += bv * (uint64)(aa.limb[10])
  333. accum9 += bv * (uint64)(aa.limb[11])
  334. accum10 += bv * (uint64)(aa.limb[12])
  335. accum11 += bv * (uint64)(aa.limb[13])
  336. accum12 += bv * (uint64)(aa.limb[14])
  337. accum13 += bv * (uint64)(aa.limb[15])
  338. aa.limb[(x448Limbs-1-14)^(x448Limbs/2)] += aa.limb[x448Limbs-1-14]
  339. bv = (uint64)(b.limb[15])
  340. accum15 += bv * (uint64)(aa.limb[0])
  341. accum0 += bv * (uint64)(aa.limb[1])
  342. accum1 += bv * (uint64)(aa.limb[2])
  343. accum2 += bv * (uint64)(aa.limb[3])
  344. accum3 += bv * (uint64)(aa.limb[4])
  345. accum4 += bv * (uint64)(aa.limb[5])
  346. accum5 += bv * (uint64)(aa.limb[6])
  347. accum6 += bv * (uint64)(aa.limb[7])
  348. accum7 += bv * (uint64)(aa.limb[8])
  349. accum8 += bv * (uint64)(aa.limb[9])
  350. accum9 += bv * (uint64)(aa.limb[10])
  351. accum10 += bv * (uint64)(aa.limb[11])
  352. accum11 += bv * (uint64)(aa.limb[12])
  353. accum12 += bv * (uint64)(aa.limb[13])
  354. accum13 += bv * (uint64)(aa.limb[14])
  355. accum14 += bv * (uint64)(aa.limb[15])
  356. aa.limb[(x448Limbs-1-15)^(x448Limbs/2)] += aa.limb[x448Limbs-1-15]
  357. // accum[x448Limbs-1] += accum[x448Limbs-2] >> lBits
  358. // accum[x448Limbs-2] &= lMask
  359. // accum[x448Limbs/2] += accum[x448Limbs-1] >> lBits
  360. accum15 += accum14 >> lBits
  361. accum14 &= lMask
  362. accum8 += accum15 >> lBits
  363. // for j := uint(0); j < x448Limbs; j++ {
  364. // accum[j] += accum[(j-1)%x448Limbs] >> lBits
  365. // accum[(j-1)%x448Limbs] &= lMask
  366. // }
  367. accum0 += accum15 >> lBits
  368. accum15 &= lMask
  369. accum1 += accum0 >> lBits
  370. accum0 &= lMask
  371. accum2 += accum1 >> lBits
  372. accum1 &= lMask
  373. accum3 += accum2 >> lBits
  374. accum2 &= lMask
  375. accum4 += accum3 >> lBits
  376. accum3 &= lMask
  377. accum5 += accum4 >> lBits
  378. accum4 &= lMask
  379. accum6 += accum5 >> lBits
  380. accum5 &= lMask
  381. accum7 += accum6 >> lBits
  382. accum6 &= lMask
  383. accum8 += accum7 >> lBits
  384. accum7 &= lMask
  385. accum9 += accum8 >> lBits
  386. accum8 &= lMask
  387. accum10 += accum9 >> lBits
  388. accum9 &= lMask
  389. accum11 += accum10 >> lBits
  390. accum10 &= lMask
  391. accum12 += accum11 >> lBits
  392. accum11 &= lMask
  393. accum13 += accum12 >> lBits
  394. accum12 &= lMask
  395. accum14 += accum13 >> lBits
  396. accum13 &= lMask
  397. accum15 += accum14 >> lBits
  398. accum14 &= lMask
  399. // for j, accv := range accum {
  400. // c.limb[j] = (uint32)(accv)
  401. // }
  402. c.limb[0] = (uint32)(accum0)
  403. c.limb[1] = (uint32)(accum1)
  404. c.limb[2] = (uint32)(accum2)
  405. c.limb[3] = (uint32)(accum3)
  406. c.limb[4] = (uint32)(accum4)
  407. c.limb[5] = (uint32)(accum5)
  408. c.limb[6] = (uint32)(accum6)
  409. c.limb[7] = (uint32)(accum7)
  410. c.limb[8] = (uint32)(accum8)
  411. c.limb[9] = (uint32)(accum9)
  412. c.limb[10] = (uint32)(accum10)
  413. c.limb[11] = (uint32)(accum11)
  414. c.limb[12] = (uint32)(accum12)
  415. c.limb[13] = (uint32)(accum13)
  416. c.limb[14] = (uint32)(accum14)
  417. c.limb[15] = (uint32)(accum15)
  418. }
  419. // sqr squares (c = x * x). Just calls multiply. (PERF)
  420. func (c *gf) sqr(x *gf) {
  421. c.mul(x, x)
  422. }
  423. // isqrt inverse square roots (y = 1/sqrt(x)), using an addition chain.
  424. func (y *gf) isqrt(x *gf) {
  425. var a, b, c gf
  426. c.sqr(x)
  427. // XXX/Yawning, could unroll, but this is called only once.
  428. // STEP(b,x,1);
  429. b.mul(x, &c)
  430. c.cpy(&b)
  431. for i := 0; i < 1; i++ {
  432. c.sqr(&c)
  433. }
  434. // STEP(b,x,3);
  435. b.mul(x, &c)
  436. c.cpy(&b)
  437. for i := 0; i < 3; i++ {
  438. c.sqr(&c)
  439. }
  440. //STEP(a,b,3);
  441. a.mul(&b, &c)
  442. c.cpy(&a)
  443. for i := 0; i < 3; i++ {
  444. c.sqr(&c)
  445. }
  446. // STEP(a,b,9);
  447. a.mul(&b, &c)
  448. c.cpy(&a)
  449. for i := 0; i < 9; i++ {
  450. c.sqr(&c)
  451. }
  452. // STEP(b,a,1);
  453. b.mul(&a, &c)
  454. c.cpy(&b)
  455. for i := 0; i < 1; i++ {
  456. c.sqr(&c)
  457. }
  458. // STEP(a,x,18);
  459. a.mul(x, &c)
  460. c.cpy(&a)
  461. for i := 0; i < 18; i++ {
  462. c.sqr(&c)
  463. }
  464. // STEP(a,b,37);
  465. a.mul(&b, &c)
  466. c.cpy(&a)
  467. for i := 0; i < 37; i++ {
  468. c.sqr(&c)
  469. }
  470. // STEP(b,a,37);
  471. b.mul(&a, &c)
  472. c.cpy(&b)
  473. for i := 0; i < 37; i++ {
  474. c.sqr(&c)
  475. }
  476. // STEP(b,a,111);
  477. b.mul(&a, &c)
  478. c.cpy(&b)
  479. for i := 0; i < 111; i++ {
  480. c.sqr(&c)
  481. }
  482. // STEP(a,b,1);
  483. a.mul(&b, &c)
  484. c.cpy(&a)
  485. for i := 0; i < 1; i++ {
  486. c.sqr(&c)
  487. }
  488. // STEP(b,x,223);
  489. b.mul(x, &c)
  490. c.cpy(&b)
  491. for i := 0; i < 223; i++ {
  492. c.sqr(&c)
  493. }
  494. y.mul(&a, &c)
  495. }
  496. // inv inverses (y = 1/x).
  497. func (y *gf) inv(x *gf) {
  498. var z, w gf
  499. z.sqr(x) // x^2
  500. w.isqrt(&z) // +- 1/sqrt(x^2) = +- 1/x
  501. z.sqr(&w) // 1/x^2
  502. w.mul(x, &z) // 1/x
  503. y.cpy(&w)
  504. }
  505. // reduce weakly reduces mod p
  506. func (x *gf) reduce() {
  507. x.limb[x448Limbs/2] += x.limb[x448Limbs-1] >> lBits
  508. // for j := uint(0); j < x448Limbs; j++ {
  509. // x.limb[j] += x.limb[(j-1)%x448Limbs] >> lBits
  510. // x.limb[(j-1)%x448Limbs] &= lMask
  511. // }
  512. x.limb[0] += x.limb[15] >> lBits
  513. x.limb[15] &= lMask
  514. x.limb[1] += x.limb[0] >> lBits
  515. x.limb[0] &= lMask
  516. x.limb[2] += x.limb[1] >> lBits
  517. x.limb[1] &= lMask
  518. x.limb[3] += x.limb[2] >> lBits
  519. x.limb[2] &= lMask
  520. x.limb[4] += x.limb[3] >> lBits
  521. x.limb[3] &= lMask
  522. x.limb[5] += x.limb[4] >> lBits
  523. x.limb[4] &= lMask
  524. x.limb[6] += x.limb[5] >> lBits
  525. x.limb[5] &= lMask
  526. x.limb[7] += x.limb[6] >> lBits
  527. x.limb[6] &= lMask
  528. x.limb[8] += x.limb[7] >> lBits
  529. x.limb[7] &= lMask
  530. x.limb[9] += x.limb[8] >> lBits
  531. x.limb[8] &= lMask
  532. x.limb[10] += x.limb[9] >> lBits
  533. x.limb[9] &= lMask
  534. x.limb[11] += x.limb[10] >> lBits
  535. x.limb[10] &= lMask
  536. x.limb[12] += x.limb[11] >> lBits
  537. x.limb[11] &= lMask
  538. x.limb[13] += x.limb[12] >> lBits
  539. x.limb[12] &= lMask
  540. x.limb[14] += x.limb[13] >> lBits
  541. x.limb[13] &= lMask
  542. x.limb[15] += x.limb[14] >> lBits
  543. x.limb[14] &= lMask
  544. }
  545. // add adds mod p. Conservatively always weak-reduces. (PERF)
  546. func (x *gf) add(y, z *gf) {
  547. // for i, yv := range y.limb {
  548. // x.limb[i] = yv + z.limb[i]
  549. // }
  550. x.limb[0] = y.limb[0] + z.limb[0]
  551. x.limb[1] = y.limb[1] + z.limb[1]
  552. x.limb[2] = y.limb[2] + z.limb[2]
  553. x.limb[3] = y.limb[3] + z.limb[3]
  554. x.limb[4] = y.limb[4] + z.limb[4]
  555. x.limb[5] = y.limb[5] + z.limb[5]
  556. x.limb[6] = y.limb[6] + z.limb[6]
  557. x.limb[7] = y.limb[7] + z.limb[7]
  558. x.limb[8] = y.limb[8] + z.limb[8]
  559. x.limb[9] = y.limb[9] + z.limb[9]
  560. x.limb[10] = y.limb[10] + z.limb[10]
  561. x.limb[11] = y.limb[11] + z.limb[11]
  562. x.limb[12] = y.limb[12] + z.limb[12]
  563. x.limb[13] = y.limb[13] + z.limb[13]
  564. x.limb[14] = y.limb[14] + z.limb[14]
  565. x.limb[15] = y.limb[15] + z.limb[15]
  566. x.reduce()
  567. }
  568. // sub subtracts mod p. Conservatively always weak-reduces. (PERF)
  569. func (x *gf) sub(y, z *gf) {
  570. // for i, yv := range y.limb {
  571. // x.limb[i] = yv - z.limb[i] + 2*p.limb[i]
  572. // }
  573. x.limb[0] = y.limb[0] - z.limb[0] + 2*lMask
  574. x.limb[1] = y.limb[1] - z.limb[1] + 2*lMask
  575. x.limb[2] = y.limb[2] - z.limb[2] + 2*lMask
  576. x.limb[3] = y.limb[3] - z.limb[3] + 2*lMask
  577. x.limb[4] = y.limb[4] - z.limb[4] + 2*lMask
  578. x.limb[5] = y.limb[5] - z.limb[5] + 2*lMask
  579. x.limb[6] = y.limb[6] - z.limb[6] + 2*lMask
  580. x.limb[7] = y.limb[7] - z.limb[7] + 2*lMask
  581. x.limb[8] = y.limb[8] - z.limb[8] + 2*(lMask-1)
  582. x.limb[9] = y.limb[9] - z.limb[9] + 2*lMask
  583. x.limb[10] = y.limb[10] - z.limb[10] + 2*lMask
  584. x.limb[11] = y.limb[11] - z.limb[11] + 2*lMask
  585. x.limb[12] = y.limb[12] - z.limb[12] + 2*lMask
  586. x.limb[13] = y.limb[13] - z.limb[13] + 2*lMask
  587. x.limb[14] = y.limb[14] - z.limb[14] + 2*lMask
  588. x.limb[15] = y.limb[15] - z.limb[15] + 2*lMask
  589. x.reduce()
  590. }
  591. // condSwap swaps x and y in constant time.
  592. func (x *gf) condSwap(y *gf, swap limbUint) {
  593. // for i, xv := range x.limb {
  594. // s := (xv ^ y.limb[i]) & (uint32)(swap) // Sort of dumb, oh well.
  595. // x.limb[i] ^= s
  596. // y.limb[i] ^= s
  597. // }
  598. var s uint32
  599. s = (x.limb[0] ^ y.limb[0]) & (uint32)(swap)
  600. x.limb[0] ^= s
  601. y.limb[0] ^= s
  602. s = (x.limb[1] ^ y.limb[1]) & (uint32)(swap)
  603. x.limb[1] ^= s
  604. y.limb[1] ^= s
  605. s = (x.limb[2] ^ y.limb[2]) & (uint32)(swap)
  606. x.limb[2] ^= s
  607. y.limb[2] ^= s
  608. s = (x.limb[3] ^ y.limb[3]) & (uint32)(swap)
  609. x.limb[3] ^= s
  610. y.limb[3] ^= s
  611. s = (x.limb[4] ^ y.limb[4]) & (uint32)(swap)
  612. x.limb[4] ^= s
  613. y.limb[4] ^= s
  614. s = (x.limb[5] ^ y.limb[5]) & (uint32)(swap)
  615. x.limb[5] ^= s
  616. y.limb[5] ^= s
  617. s = (x.limb[6] ^ y.limb[6]) & (uint32)(swap)
  618. x.limb[6] ^= s
  619. y.limb[6] ^= s
  620. s = (x.limb[7] ^ y.limb[7]) & (uint32)(swap)
  621. x.limb[7] ^= s
  622. y.limb[7] ^= s
  623. s = (x.limb[8] ^ y.limb[8]) & (uint32)(swap)
  624. x.limb[8] ^= s
  625. y.limb[8] ^= s
  626. s = (x.limb[9] ^ y.limb[9]) & (uint32)(swap)
  627. x.limb[9] ^= s
  628. y.limb[9] ^= s
  629. s = (x.limb[10] ^ y.limb[10]) & (uint32)(swap)
  630. x.limb[10] ^= s
  631. y.limb[10] ^= s
  632. s = (x.limb[11] ^ y.limb[11]) & (uint32)(swap)
  633. x.limb[11] ^= s
  634. y.limb[11] ^= s
  635. s = (x.limb[12] ^ y.limb[12]) & (uint32)(swap)
  636. x.limb[12] ^= s
  637. y.limb[12] ^= s
  638. s = (x.limb[13] ^ y.limb[13]) & (uint32)(swap)
  639. x.limb[13] ^= s
  640. y.limb[13] ^= s
  641. s = (x.limb[14] ^ y.limb[14]) & (uint32)(swap)
  642. x.limb[14] ^= s
  643. y.limb[14] ^= s
  644. s = (x.limb[15] ^ y.limb[15]) & (uint32)(swap)
  645. x.limb[15] ^= s
  646. y.limb[15] ^= s
  647. }
  648. // mlw multiplies by a signed int. NOT CONSTANT TIME wrt the sign of the int,
  649. // but that's ok because it's only ever called with w = -edwardsD. Just uses
  650. // a full multiply. (PERF)
  651. func (a *gf) mlw(b *gf, w int) {
  652. if w > 0 {
  653. ww := gf{[x448Limbs]uint32{(uint32)(w)}}
  654. a.mul(b, &ww)
  655. } else {
  656. // This branch is *NEVER* taken with the current code.
  657. panic("mul called with negative w")
  658. ww := gf{[x448Limbs]uint32{(uint32)(-w)}}
  659. a.mul(b, &ww)
  660. a.sub(&zero, a)
  661. }
  662. }
  663. // canon canonicalizes.
  664. func (a *gf) canon() {
  665. a.reduce()
  666. // Subtract p with borrow.
  667. var carry int64
  668. for i, v := range a.limb {
  669. carry = carry + (int64)(v) - (int64)(p.limb[i])
  670. a.limb[i] = (uint32)(carry & lMask)
  671. carry >>= lBits
  672. }
  673. addback := carry
  674. carry = 0
  675. // Add it back.
  676. for i, v := range a.limb {
  677. carry = carry + (int64)(v) + (int64)(p.limb[i]&(uint32)(addback))
  678. a.limb[i] = uint32(carry & lMask)
  679. carry >>= lBits
  680. }
  681. }
  682. // deser deserializes into the limb representation.
  683. func (s *gf) deser(ser *[x448Bytes]byte) {
  684. var buf uint64
  685. bits := uint(0)
  686. k := 0
  687. for i, v := range ser {
  688. buf |= (uint64)(v) << bits
  689. for bits += 8; (bits >= lBits || i == x448Bytes-1) && k < x448Limbs; bits, buf = bits-lBits, buf>>lBits {
  690. s.limb[k] = (uint32)(buf & lMask)
  691. k++
  692. }
  693. }
  694. }
  695. // ser serializes into byte representation.
  696. func (a *gf) ser(ser *[x448Bytes]byte) {
  697. a.canon()
  698. k := 0
  699. bits := uint(0)
  700. var buf uint64
  701. for i, v := range a.limb {
  702. buf |= (uint64)(v) << bits
  703. for bits += lBits; (bits >= 8 || i == x448Limbs-1) && k < x448Bytes; bits, buf = bits-8, buf>>8 {
  704. ser[k] = (byte)(buf)
  705. k++
  706. }
  707. }
  708. }
  709. func init() {
  710. if x448Limbs != 16 {
  711. panic("x448Limbs != 16, unrolled loops likely broken")
  712. }
  713. }