chacha-ia64.pl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov, @dot-asm, initially for use with OpenSSL.
  5. # ====================================================================
  6. #
  7. # ChaCha20 for Itanium.
  8. #
  9. # March 2019
  10. #
  11. # Itanium 9xxx, which has pair of shifters, manages to process one byte
  12. # in 9.3 cycles. This aligns perfectly with theoretical estimate.
  13. # On the other hand, pre-9000 CPU has single shifter and each extr/dep
  14. # pairs below takes additional cycle. Then final input->xor->output
  15. # pass runs slower than expected... Overall result is 15.6 cpb, two
  16. # cycles more than theoretical estimate.
  17. $output = pop;
  18. open STDOUT, ">$output" if $output;
  19. my @k = map("r$_",(16..31));
  20. my @x = map("r$_",(38..53));
  21. my @y = map("r$_",(8..11));
  22. my @z = map("r$_",(15,35..37));
  23. my ($out,$inp,$len,$key,$counter) = map("r$_",(32..36));
  24. $code.=<<___;
  25. #if defined(_HPUX_SOURCE)
  26. # if !defined(_LP64)
  27. # define ADDP addp4
  28. # else
  29. # define ADDP add
  30. # endif
  31. #else
  32. # define ADDP add
  33. #endif
  34. .text
  35. .global ChaCha20_ctr32#
  36. .proc ChaCha20_ctr32#
  37. .align 32
  38. ChaCha20_ctr32:
  39. .prologue
  40. .save ar.pfs,r2
  41. { .mmi; alloc r2=ar.pfs,5,17,0,0
  42. ADDP @k[11]=4,$key
  43. .save ar.lc,r3
  44. mov r3=ar.lc }
  45. { .mmi; ADDP $key=0,$key
  46. ADDP $counter=0,$counter
  47. .save pr,r14
  48. mov r14=pr };;
  49. .body
  50. { .mlx; ld4 @k[4]=[$key],8
  51. movl @k[0]=0x61707865 }
  52. { .mlx; ld4 @k[5]=[@k[11]],8
  53. movl @k[1]=0x3320646e };;
  54. { .mlx; ld4 @k[6]=[$key],8
  55. movl @k[2]=0x79622d32 }
  56. { .mlx; ld4 @k[7]=[@k[11]],8
  57. movl @k[3]=0x6b206574 };;
  58. { .mmi; ld4 @k[8]=[$key],8
  59. ld4 @k[9]=[@k[11]],8
  60. add @k[15]=4,$counter };;
  61. { .mmi; ld4 @k[10]=[$key]
  62. ld4 @k[11]=[@k[11]]
  63. mov @x[0]=@k[0] };;
  64. { .mmi; ld4 @k[12]=[$counter],8
  65. ld4 @k[13]=[@k[15]],8
  66. mov @x[1]=@k[1] };;
  67. { .mmi; ld4 @k[14]=[$counter]
  68. ld4 @k[15]=[@k[15]]
  69. mov @x[2]=@k[2] }
  70. { .mmi; mov @x[3]=@k[3]
  71. mov @x[4]=@k[4]
  72. mov @x[5]=@k[5] };;
  73. { .mmi; mov @x[6]=@k[6]
  74. mov @x[7]=@k[7]
  75. mov @x[8]=@k[8] }
  76. { .mmi; mov @x[9]=@k[9]
  77. mov @x[10]=@k[10]
  78. mov @x[11]=@k[11] }
  79. { .mmi; mov @x[12]=@k[12]
  80. mov @x[13]=@k[13]
  81. mov @x[14]=@k[14] };;
  82. .Loop_outer:
  83. { .mii; mov @x[15]=@k[15]
  84. mov ar.lc=9
  85. mov ar.ec=1 }
  86. { .mmb; cmp.geu p6,p0=64,$len
  87. sub @z[1]=64,$len
  88. brp.loop.imp .Loop_top,.Loop_end-16 };;
  89. .Loop_top:
  90. ___
  91. sub ROUND {
  92. my ($a0,$b0,$c0,$d0)=@_;
  93. my ($a1,$b1,$c1,$d1)=map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
  94. my ($a2,$b2,$c2,$d2)=map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
  95. my ($a3,$b3,$c3,$d3)=map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
  96. $code.=<<___;
  97. { .mmi; add @x[$a0]=@x[$a0],@x[$b0]
  98. add @x[$a1]=@x[$a1],@x[$b1]
  99. add @x[$a2]=@x[$a2],@x[$b2] };;
  100. { .mmi; add @x[$a3]=@x[$a3],@x[$b3]
  101. xor @x[$d0]=@x[$d0],@x[$a0]
  102. xor @x[$d1]=@x[$d1],@x[$a1] };;
  103. { .mmi; xor @x[$d2]=@x[$d2],@x[$a2]
  104. xor @x[$d3]=@x[$d3],@x[$a3]
  105. extr.u @y[0]=@x[$d0],16,16 };;
  106. { .mii; extr.u @y[1]=@x[$d1],16,16
  107. dep @x[$d0]=@x[$d0],@y[0],16,16 };;
  108. { .mii; add @x[$c0]=@x[$c0],@x[$d0]
  109. extr.u @y[2]=@x[$d2],16,16
  110. dep @x[$d1]=@x[$d1],@y[1],16,16 };;
  111. { .mii; add @x[$c1]=@x[$c1],@x[$d1]
  112. xor @x[$b0]=@x[$b0],@x[$c0]
  113. extr.u @y[3]=@x[$d3],16,16 };;
  114. { .mii; xor @x[$b1]=@x[$b1],@x[$c1]
  115. dep @x[$d2]=@x[$d2],@y[2],16,16
  116. dep @x[$d3]=@x[$d3],@y[3],16,16 };;
  117. { .mmi; add @x[$c2]=@x[$c2],@x[$d2]
  118. add @x[$c3]=@x[$c3],@x[$d3]
  119. extr.u @y[0]=@x[$b0],20,12 };;
  120. { .mmi; xor @x[$b2]=@x[$b2],@x[$c2]
  121. xor @x[$b3]=@x[$b3],@x[$c3]
  122. dep.z @x[$b0]=@x[$b0],12,20 };;
  123. { .mii; or @x[$b0]=@x[$b0],@y[0]
  124. extr.u @y[1]=@x[$b1],20,12
  125. dep.z @x[$b1]=@x[$b1],12,20 };;
  126. { .mii; add @x[$a0]=@x[$a0],@x[$b0]
  127. extr.u @y[2]=@x[$b2],20,12
  128. extr.u @y[3]=@x[$b3],20,12 }
  129. { .mii; or @x[$b1]=@x[$b1],@y[1]
  130. dep.z @x[$b2]=@x[$b2],12,20
  131. dep.z @x[$b3]=@x[$b3],12,20 };;
  132. { .mmi; or @x[$b2]=@x[$b2],@y[2]
  133. or @x[$b3]=@x[$b3],@y[3]
  134. add @x[$a1]=@x[$a1],@x[$b1] };;
  135. { .mmi; add @x[$a2]=@x[$a2],@x[$b2]
  136. add @x[$a3]=@x[$a3],@x[$b3]
  137. xor @x[$d0]=@x[$d0],@x[$a0] };;
  138. { .mii; xor @x[$d1]=@x[$d1],@x[$a1]
  139. extr.u @y[0]=@x[$d0],24,8
  140. dep.z @x[$d0]=@x[$d0],8,24 };;
  141. { .mii; or @x[$d0]=@x[$d0],@y[0]
  142. extr.u @y[1]=@x[$d1],24,8
  143. dep.z @x[$d1]=@x[$d1],8,24 };;
  144. { .mmi; or @x[$d1]=@x[$d1],@y[1]
  145. xor @x[$d2]=@x[$d2],@x[$a2]
  146. xor @x[$d3]=@x[$d3],@x[$a3] };;
  147. { .mii; add @x[$c0]=@x[$c0],@x[$d0]
  148. extr.u @y[2]=@x[$d2],24,8
  149. dep.z @x[$d2]=@x[$d2],8,24 };;
  150. { .mii; xor @x[$b0]=@x[$b0],@x[$c0]
  151. extr.u @y[3]=@x[$d3],24,8
  152. dep.z @x[$d3]=@x[$d3],8,24 };;
  153. { .mmi; or @x[$d2]=@x[$d2],@y[2]
  154. or @x[$d3]=@x[$d3],@y[3]
  155. extr.u @y[0]=@x[$b0],25,7 };;
  156. { .mmi; add @x[$c1]=@x[$c1],@x[$d1]
  157. add @x[$c2]=@x[$c2],@x[$d2]
  158. dep.z @x[$b0]=@x[$b0],7,25 };;
  159. { .mmi; xor @x[$b1]=@x[$b1],@x[$c1]
  160. xor @x[$b2]=@x[$b2],@x[$c2]
  161. add @x[$c3]=@x[$c3],@x[$d3] };;
  162. { .mii; xor @x[$b3]=@x[$b3],@x[$c3]
  163. extr.u @y[1]=@x[$b1],25,7
  164. dep.z @x[$b1]=@x[$b1],7,25 };;
  165. { .mii; or @x[$b0]=@x[$b0],@y[0]
  166. extr.u @y[2]=@x[$b2],25,7
  167. dep.z @x[$b2]=@x[$b2],7,25 };;
  168. { .mii; or @x[$b1]=@x[$b1],@y[1]
  169. extr.u @y[3]=@x[$b3],25,7
  170. dep.z @x[$b3]=@x[$b3],7,25 };;
  171. ___
  172. $code.=<<___ if ($d0 == 12);
  173. { .mmi; or @x[$b2]=@x[$b2],@y[2]
  174. or @x[$b3]=@x[$b3],@y[3]
  175. mov @z[0]=-1 };;
  176. ___
  177. $code.=<<___ if ($d0 == 15);
  178. { .mmb; or @x[$b2]=@x[$b2],@y[2]
  179. or @x[$b3]=@x[$b3],@y[3]
  180. br.ctop.sptk .Loop_top };;
  181. ___
  182. }
  183. &ROUND(0, 4, 8, 12);
  184. &ROUND(0, 5, 10, 15);
  185. $code.=<<___;
  186. .Loop_end:
  187. { .mmi; add @x[0]=@x[0],@k[0]
  188. add @x[1]=@x[1],@k[1]
  189. (p6) shr.u @z[0]=@z[0],@z[1] }
  190. { .mmb; add @x[2]=@x[2],@k[2]
  191. add @x[3]=@x[3],@k[3]
  192. clrrrb.pr };;
  193. { .mmi; add @x[4]=@x[4],@k[4]
  194. add @x[5]=@x[5],@k[5]
  195. add @x[6]=@x[6],@k[6] }
  196. { .mmi; add @x[7]=@x[7],@k[7]
  197. add @x[8]=@x[8],@k[8]
  198. add @x[9]=@x[9],@k[9] }
  199. { .mmi; add @x[10]=@x[10],@k[10]
  200. add @x[11]=@x[11],@k[11]
  201. add @x[12]=@x[12],@k[12] }
  202. { .mmi; add @x[13]=@x[13],@k[13]
  203. add @x[14]=@x[14],@k[14]
  204. add @x[15]=@x[15],@k[15] }
  205. { .mmi; add @k[12]=1,@k[12] // next counter
  206. mov pr=@z[0],0x1ffff };;
  207. //////////////////////////////////////////////////////////////////
  208. // Each predicate bit corresponds to byte to be processed. Note
  209. // that p0 is wired to 1, but it works out, because there always
  210. // is at least one byte to process...
  211. { .mmi; (p0) ld1 @z[0]=[$inp],1
  212. shr.u @y[1]=@x[0],8 };;
  213. { .mmi; (p1) ld1 @z[1]=[$inp],1
  214. (p2) shr.u @y[2]=@x[0],16 };;
  215. { .mmi; (p2) ld1 @z[2]=[$inp],1
  216. (p0) xor @z[0]=@z[0],@x[0]
  217. (p3) shr.u @y[3]=@x[0],24 };;
  218. ___
  219. for(my $i0=0; $i0<60; $i0+=4) {
  220. my ($i1, $i2, $i3, $i4, $i5, $i6, $i7) = map($i0+$_,(1..7));
  221. my $k = $i0/4+1;
  222. $code.=<<___;
  223. { .mmi; (p$i3) ld1 @z[3]=[$inp],1
  224. (p$i0) st1 [$out]=@z[0],1
  225. (p$i1) xor @z[1]=@z[1],@y[1] };;
  226. { .mmi; (p$i4) ld1 @z[0]=[$inp],1
  227. (p$i5) shr.u @y[1]=@x[$k],8 }
  228. { .mmi; (p$i1) st1 [$out]=@z[1],1
  229. (p$i2) xor @z[2]=@z[2],@y[2]
  230. (p1) mov @x[$k-1]=@k[$k-1] };;
  231. { .mfi; (p$i5) ld1 @z[1]=[$inp],1
  232. (p$i6) shr.u @y[2]=@x[$k],16 }
  233. { .mfi; (p$i2) st1 [$out]=@z[2],1
  234. (p$i3) xor @z[3]=@z[3],@y[3] };;
  235. { .mfi; (p$i6) ld1 @z[2]=[$inp],1
  236. (p$i7) shr.u @y[3]=@x[$k],24 }
  237. ___
  238. $code.=<<___ if ($i0==0); # p1,p2 are available for reuse in first round
  239. { .mmi; (p$i3) st1 [$out]=@z[3],1
  240. (p$i4) xor @z[0]=@z[0],@x[$k]
  241. cmp.ltu p1,p2=64,$len };;
  242. ___
  243. $code.=<<___ if ($i0>0);
  244. { .mfi; (p$i3) st1 [$out]=@z[3],1
  245. (p$i4) xor @z[0]=@z[0],@x[$k] };;
  246. ___
  247. }
  248. $code.=<<___;
  249. { .mmi; (p63) ld1 @z[3]=[$inp],1
  250. (p60) st1 [$out]=@z[0],1
  251. (p61) xor @z[1]=@z[1],@y[1] };;
  252. { .mmi; (p61) st1 [$out]=@z[1],1
  253. (p62) xor @z[2]=@z[2],@y[2] };;
  254. { .mmi; (p62) st1 [$out]=@z[2],1
  255. (p63) xor @z[3]=@z[3],@y[3]
  256. (p2) mov ar.lc=r3 };;
  257. { .mib; (p63) st1 [$out]=@z[3],1
  258. (p1) add $len=-64,$len
  259. (p1) br.dptk.many .Loop_outer };;
  260. { .mmi; mov @k[4]=0 // wipe key material
  261. mov @k[5]=0
  262. mov @k[6]=0 }
  263. { .mmi; mov @k[7]=0
  264. mov @k[8]=0
  265. mov @k[9]=0 }
  266. { .mmi; mov @k[10]=0
  267. mov @k[11]=0
  268. mov @k[12]=0 }
  269. { .mmi; mov @k[13]=0
  270. mov @k[14]=0
  271. mov @k[15]=0 }
  272. { .mib; mov pr=r14,0x1ffff
  273. br.ret.sptk.many b0 };;
  274. .endp ChaCha20_ctr32#
  275. stringz "ChaCha20 for IA64, CRYPTOGAMS by \@dot-asm"
  276. ___
  277. print $code;
  278. close STDOUT;