You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

439 lines
9.3 KiB

  1. <% function signL(reg, label_pos, label_neg) { %>
  2. <% for (let i=n64-1; i>=0; i--) { %>
  3. mov rax, [<%=reg%> + <%= 8+(i*8) %>]
  4. cmp [half + <%= (i*8) %>], rax ; comare with (q-1)/2
  5. jc <%=label_neg%> ; half<rax => e1-e2 is neg => e1 < e2
  6. <% if (i>0) { %>
  7. jnz <%=label_pos%> ; half>rax => e1 -e2 is pos => e1 > e2
  8. <% } else { %>
  9. jmp <%=label_pos%>
  10. <% } %>
  11. <% } %>
  12. <% } %>
  13. ;;;;;;;;;;;;;;;;;;;;;;
  14. ; rgt - Raw Greater Than
  15. ;;;;;;;;;;;;;;;;;;;;;;
  16. ; returns in ax 1 id *rsi > *rdx
  17. ; Params:
  18. ; rsi <= Pointer to element 1
  19. ; rdx <= Pointer to element 2
  20. ; rax <= Return 1 or 0
  21. ; Modified Registers:
  22. ; r8, r9, rax
  23. ;;;;;;;;;;;;;;;;;;;;;;
  24. <%=name%>_rgt:
  25. mov r8, [rsi]
  26. mov r9, [rdx]
  27. bt r8, 63 ; Check if is short first operand
  28. jc rgt_l1
  29. bt r9, 63 ; Check if is short second operand
  30. jc rgt_s1l2
  31. rgt_s1s2: ; Both operands are short
  32. cmp r8d, r9d
  33. jg rgt_ret1
  34. jmp rgt_ret0
  35. rgt_l1:
  36. bt r9, 63 ; Check if is short second operand
  37. jc rgt_l1l2
  38. ;;;;;;;;
  39. rgt_l1s2:
  40. bt r8, 62 ; check if montgomery first
  41. jc rgt_l1ms2
  42. rgt_l1ns2:
  43. <%= global.toLong_b() %>
  44. jmp rgtL1L2
  45. rgt_l1ms2:
  46. <%= global.toLong_b() %>
  47. <%= global.fromMont_a() %>
  48. jmp rgtL1L2
  49. ;;;;;;;;
  50. rgt_s1l2:
  51. bt r9, 62 ; check if montgomery second
  52. jc rgt_s1l2m
  53. rgt_s1l2n:
  54. <%= global.toLong_a() %>
  55. jmp rgtL1L2
  56. rgt_s1l2m:
  57. <%= global.toLong_a() %>
  58. <%= global.fromMont_b() %>
  59. jmp rgtL1L2
  60. ;;;;
  61. rgt_l1l2:
  62. bt r8, 62 ; check if montgomery first
  63. jc rgt_l1ml2
  64. rgt_l1nl2:
  65. bt r9, 62 ; check if montgomery second
  66. jc rgt_l1nl2m
  67. rgt_l1nl2n:
  68. jmp rgtL1L2
  69. rgt_l1nl2m:
  70. <%= global.fromMont_b() %>
  71. jmp rgtL1L2
  72. rgt_l1ml2:
  73. bt r9, 62 ; check if montgomery second
  74. jc rgt_l1ml2m
  75. rgt_l1ml2n:
  76. <%= global.fromMont_a() %>
  77. jmp rgtL1L2
  78. rgt_l1ml2m:
  79. <%= global.fromMont_a() %>
  80. <%= global.fromMont_b() %>
  81. jmp rgtL1L2
  82. ;;;;;;
  83. ; rgtL1L2
  84. ;;;;;;
  85. rgtL1L2:
  86. <%= signL("rsi", "rgtl1l2_p1", "rgtl1l2_n1") %>
  87. rgtl1l2_p1:
  88. <%= signL("rdx", "rgtRawL1L2", "rgt_ret1") %>
  89. rgtl1l2_n1:
  90. <%= signL("rdx", "rgt_ret0", "rgtRawL1L2") %>
  91. rgtRawL1L2:
  92. <% for (let i=n64-1; i>=0; i--) { %>
  93. mov rax, [rsi + <%= 8+(i*8) %>]
  94. cmp [rdx + <%= 8+(i*8) %>], rax ; comare with (q-1)/2
  95. jc rgt_ret1 ; rsi<rdx => 1st > 2nd
  96. <% if (i>0) { %>
  97. jnz rgt_ret0
  98. <% } %>
  99. <% } %>
  100. rgt_ret0:
  101. xor rax, rax
  102. ret
  103. rgt_ret1:
  104. mov rax, 1
  105. ret
  106. ;;;;;;;;;;;;;;;;;;;;;;
  107. ; rlt - Raw Less Than
  108. ;;;;;;;;;;;;;;;;;;;;;;
  109. ; returns in ax 1 id *rsi > *rdx
  110. ; Params:
  111. ; rsi <= Pointer to element 1
  112. ; rdx <= Pointer to element 2
  113. ; rax <= Return 1 or 0
  114. ; Modified Registers:
  115. ; r8, r9, rax
  116. ;;;;;;;;;;;;;;;;;;;;;;
  117. <%=name%>_rlt:
  118. mov r8, [rsi]
  119. mov r9, [rdx]
  120. bt r8, 63 ; Check if is short first operand
  121. jc rlt_l1
  122. bt r9, 63 ; Check if is short second operand
  123. jc rlt_s1l2
  124. rlt_s1s2: ; Both operands are short
  125. cmp r8d, r9d
  126. jl rlt_ret1
  127. jmp rlt_ret0
  128. rlt_l1:
  129. bt r9, 63 ; Check if is short second operand
  130. jc rlt_l1l2
  131. ;;;;;;;;
  132. rlt_l1s2:
  133. bt r8, 62 ; check if montgomery first
  134. jc rlt_l1ms2
  135. rlt_l1ns2:
  136. <%= global.toLong_b() %>
  137. jmp rltL1L2
  138. rlt_l1ms2:
  139. <%= global.toLong_b() %>
  140. <%= global.fromMont_a() %>
  141. jmp rltL1L2
  142. ;;;;;;;;
  143. rlt_s1l2:
  144. bt r9, 62 ; check if montgomery second
  145. jc rlt_s1l2m
  146. rlt_s1l2n:
  147. <%= global.toLong_a() %>
  148. jmp rltL1L2
  149. rlt_s1l2m:
  150. <%= global.toLong_a() %>
  151. <%= global.fromMont_b() %>
  152. jmp rltL1L2
  153. ;;;;
  154. rlt_l1l2:
  155. bt r8, 62 ; check if montgomery first
  156. jc rlt_l1ml2
  157. rlt_l1nl2:
  158. bt r9, 62 ; check if montgomery second
  159. jc rlt_l1nl2m
  160. rlt_l1nl2n:
  161. jmp rltL1L2
  162. rlt_l1nl2m:
  163. <%= global.fromMont_b() %>
  164. jmp rltL1L2
  165. rlt_l1ml2:
  166. bt r9, 62 ; check if montgomery second
  167. jc rlt_l1ml2m
  168. rlt_l1ml2n:
  169. <%= global.fromMont_a() %>
  170. jmp rltL1L2
  171. rlt_l1ml2m:
  172. <%= global.fromMont_a() %>
  173. <%= global.fromMont_b() %>
  174. jmp rltL1L2
  175. ;;;;;;
  176. ; rltL1L2
  177. ;;;;;;
  178. rltL1L2:
  179. <%= signL("rsi", "rltl1l2_p1", "rltl1l2_n1") %>
  180. rltl1l2_p1:
  181. <%= signL("rdx", "rltRawL1L2", "rlt_ret0") %>
  182. rltl1l2_n1:
  183. <%= signL("rdx", "rlt_ret1", "rltRawL1L2") %>
  184. rltRawL1L2:
  185. <% for (let i=n64-1; i>=0; i--) { %>
  186. mov rax, [rsi + <%= 8+(i*8) %>]
  187. cmp [rdx + <%= 8+(i*8) %>], rax ; comare with (q-1)/2
  188. jc rlt_ret0 ; rsi<rdx => 1st > 2nd
  189. jnz rlt_ret1
  190. <% } %>
  191. rlt_ret0:
  192. xor rax, rax
  193. ret
  194. rlt_ret1:
  195. mov rax, 1
  196. ret
  197. ;;;;;;;;;;;;;;;;;;;;;;
  198. ; req - Raw Eq
  199. ;;;;;;;;;;;;;;;;;;;;;;
  200. ; returns in ax 1 id *rsi == *rdx
  201. ; Params:
  202. ; rsi <= Pointer to element 1
  203. ; rdx <= Pointer to element 2
  204. ; rax <= Return 1 or 0
  205. ; Modified Registers:
  206. ; r8, r9, rax
  207. ;;;;;;;;;;;;;;;;;;;;;;
  208. <%=name%>_req:
  209. mov r8, [rsi]
  210. mov r9, [rdx]
  211. bt r8, 63 ; Check if is short first operand
  212. jc req_l1
  213. bt r9, 63 ; Check if is short second operand
  214. jc req_s1l2
  215. req_s1s2: ; Both operands are short
  216. cmp r8d, r9d
  217. je req_ret1
  218. jmp req_ret0
  219. req_l1:
  220. bt r9, 63 ; Check if is short second operand
  221. jc req_l1l2
  222. ;;;;;;;;
  223. req_l1s2:
  224. bt r8, 62 ; check if montgomery first
  225. jc req_l1ms2
  226. req_l1ns2:
  227. <%= global.toLong_b() %>
  228. jmp reqL1L2
  229. req_l1ms2:
  230. <%= global.toMont_b() %>
  231. jmp reqL1L2
  232. ;;;;;;;;
  233. req_s1l2:
  234. bt r9, 62 ; check if montgomery second
  235. jc req_s1l2m
  236. req_s1l2n:
  237. <%= global.toLong_a() %>
  238. jmp reqL1L2
  239. req_s1l2m:
  240. <%= global.toMont_a() %>
  241. jmp reqL1L2
  242. ;;;;
  243. req_l1l2:
  244. bt r8, 62 ; check if montgomery first
  245. jc req_l1ml2
  246. req_l1nl2:
  247. bt r9, 62 ; check if montgomery second
  248. jc req_l1nl2m
  249. req_l1nl2n:
  250. jmp reqL1L2
  251. req_l1nl2m:
  252. <%= global.toMont_a() %>
  253. jmp reqL1L2
  254. req_l1ml2:
  255. bt r9, 62 ; check if montgomery second
  256. jc req_l1ml2m
  257. req_l1ml2n:
  258. <%= global.toMont_b() %>
  259. jmp reqL1L2
  260. req_l1ml2m:
  261. jmp reqL1L2
  262. ;;;;;;
  263. ; eqL1L2
  264. ;;;;;;
  265. reqL1L2:
  266. <% for (let i=0; i<n64; i++) { %>
  267. mov rax, [rsi + <%= 8+(i*8) %>]
  268. cmp [rdx + <%= 8+(i*8) %>], rax
  269. jne req_ret0 ; rsi<rdi => 1st > 2nd
  270. <% } %>
  271. req_ret1:
  272. mov rax, 1
  273. ret
  274. req_ret0:
  275. xor rax, rax
  276. ret
  277. ;;;;;;;;;;;;;;;;;;;;;;
  278. ; gt
  279. ;;;;;;;;;;;;;;;;;;;;;;
  280. ; Compares two elements of any kind
  281. ; Params:
  282. ; rsi <= Pointer to element 1
  283. ; rdx <= Pointer to element 2
  284. ; rdi <= Pointer to result can be zero or one.
  285. ; Modified Registers:
  286. ; rax, rcx
  287. ;;;;;;;;;;;;;;;;;;;;;;
  288. <%=name%>_gt:
  289. call <%=name%>_rgt
  290. mov [rdi], rax
  291. ret
  292. ;;;;;;;;;;;;;;;;;;;;;;
  293. ; lt
  294. ;;;;;;;;;;;;;;;;;;;;;;
  295. ; Compares two elements of any kind
  296. ; Params:
  297. ; rsi <= Pointer to element 1
  298. ; rdx <= Pointer to element 2
  299. ; rdi <= Pointer to result can be zero or one.
  300. ; Modified Registers:
  301. ; rax, rcx
  302. ;;;;;;;;;;;;;;;;;;;;;;
  303. <%=name%>_lt:
  304. call <%=name%>_rlt
  305. mov [rdi], rax
  306. ret
  307. ;;;;;;;;;;;;;;;;;;;;;;
  308. ; eq
  309. ;;;;;;;;;;;;;;;;;;;;;;
  310. ; Compares two elements of any kind
  311. ; Params:
  312. ; rsi <= Pointer to element 1
  313. ; rdx <= Pointer to element 2
  314. ; rdi <= Pointer to result can be zero or one.
  315. ; Modified Registers:
  316. ; rax, rcx
  317. ;;;;;;;;;;;;;;;;;;;;;;
  318. <%=name%>_eq:
  319. call <%=name%>_req
  320. mov [rdi], rax
  321. ret
  322. ;;;;;;;;;;;;;;;;;;;;;;
  323. ; neq
  324. ;;;;;;;;;;;;;;;;;;;;;;
  325. ; Compares two elements of any kind
  326. ; Params:
  327. ; rsi <= Pointer to element 1
  328. ; rdx <= Pointer to element 2
  329. ; rdi <= Pointer to result can be zero or one.
  330. ; Modified Registers:
  331. ; rax, rcx
  332. ;;;;;;;;;;;;;;;;;;;;;;
  333. <%=name%>_neq:
  334. call <%=name%>_req
  335. xor rax, 1
  336. mov [rdi], rax
  337. ret
  338. ;;;;;;;;;;;;;;;;;;;;;;
  339. ; geq
  340. ;;;;;;;;;;;;;;;;;;;;;;
  341. ; Compares two elements of any kind
  342. ; Params:
  343. ; rsi <= Pointer to element 1
  344. ; rdx <= Pointer to element 2
  345. ; rdi <= Pointer to result can be zero or one.
  346. ; Modified Registers:
  347. ; rax, rcx
  348. ;;;;;;;;;;;;;;;;;;;;;;
  349. <%=name%>_geq:
  350. call <%=name%>_rlt
  351. xor rax, 1
  352. mov [rdi], rax
  353. ret
  354. ;;;;;;;;;;;;;;;;;;;;;;
  355. ; leq
  356. ;;;;;;;;;;;;;;;;;;;;;;
  357. ; Compares two elements of any kind
  358. ; Params:
  359. ; rsi <= Pointer to element 1
  360. ; rdx <= Pointer to element 2
  361. ; rdi <= Pointer to result can be zero or one.
  362. ; Modified Registers:
  363. ; rax, rcx
  364. ;;;;;;;;;;;;;;;;;;;;;;
  365. <%=name%>_leq:
  366. call <%=name%>_rgt
  367. xor rax, 1
  368. mov [rdi], rax
  369. ret