x448_ref.go 22 KB

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