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.

245 lines
5.7 KiB

  1. <% function addS1S2() { %>
  2. xor rdx, rdx
  3. mov edx, eax
  4. add edx, ecx
  5. jo add_manageOverflow ; rsi already is the 64bits result
  6. mov [rdi], rdx ; not necessary to adjust so just save and return
  7. ret
  8. add_manageOverflow: ; Do the operation in 64 bits
  9. push rsi
  10. movsx rsi, eax
  11. movsx rdx, ecx
  12. add rsi, rdx
  13. call rawCopyS2L
  14. pop rsi
  15. ret
  16. <% } %>
  17. <% function addL1S2() { %>
  18. add rsi, 8
  19. movsx rdx, ecx
  20. add rdi, 8
  21. cmp rdx, 0
  22. <% const rawAddLabel = global.tmpLabel() %>
  23. jns <%= rawAddLabel %>
  24. neg rdx
  25. call rawSubLS
  26. sub rdi, 8
  27. sub rsi, 8
  28. ret
  29. <%= rawAddLabel %>:
  30. call rawAddLS
  31. sub rdi, 8
  32. sub rsi, 8
  33. ret
  34. <% } %>
  35. <% function addS1L2() { %>
  36. lea rsi, [rdx + 8]
  37. movsx rdx, eax
  38. add rdi, 8
  39. cmp rdx, 0
  40. <% const rawAddLabel = global.tmpLabel() %>
  41. jns <%= rawAddLabel %>
  42. neg rdx
  43. call rawSubLS
  44. sub rdi, 8
  45. sub rsi, 8
  46. ret
  47. <%= rawAddLabel %>:
  48. call rawAddLS
  49. sub rdi, 8
  50. sub rsi, 8
  51. ret
  52. <% } %>
  53. <% function addL1L2() { %>
  54. add rdi, 8
  55. add rsi, 8
  56. add rdx, 8
  57. call rawAddLL
  58. sub rdi, 8
  59. sub rsi, 8
  60. ret
  61. <% } %>
  62. ;;;;;;;;;;;;;;;;;;;;;;
  63. ; add
  64. ;;;;;;;;;;;;;;;;;;;;;;
  65. ; Adds two elements of any kind
  66. ; Params:
  67. ; rsi <= Pointer to element 1
  68. ; rdx <= Pointer to element 2
  69. ; rdi <= Pointer to result
  70. ; Modified Registers:
  71. ; r8, r9, 10, r11, rax, rcx
  72. ;;;;;;;;;;;;;;;;;;;;;;
  73. <%=name%>_add:
  74. mov rax, [rsi]
  75. mov rcx, [rdx]
  76. bt rax, 63 ; Check if is short first operand
  77. jc add_l1
  78. bt rcx, 63 ; Check if is short second operand
  79. jc add_s1l2
  80. add_s1s2: ; Both operands are short
  81. <%= addS1S2() %>
  82. add_l1:
  83. bt rcx, 63 ; Check if is short second operand
  84. jc add_l1l2
  85. ;;;;;;;;
  86. add_l1s2:
  87. bt rax, 62 ; check if montgomery first
  88. jc add_l1ms2
  89. add_l1ns2:
  90. <%= global.setTypeDest("0x80"); %>
  91. <%= addL1S2(); %>
  92. add_l1ms2:
  93. bt rcx, 62 ; check if montgomery second
  94. jc add_l1ms2m
  95. add_l1ms2n:
  96. <%= global.setTypeDest("0xC0"); %>
  97. <%= global.toMont_b() %>
  98. <%= addL1L2() %>
  99. add_l1ms2m:
  100. <%= global.setTypeDest("0xC0"); %>
  101. <%= addL1L2() %>
  102. ;;;;;;;;
  103. add_s1l2:
  104. bt rcx, 62 ; check if montgomery first
  105. jc add_s1l2m
  106. add_s1l2n:
  107. <%= global.setTypeDest("0x80"); %>
  108. <%= addS1L2(); %>
  109. add_s1l2m:
  110. bt rax, 62 ; check if montgomery second
  111. jc add_s1ml2m
  112. add_s1nl2m:
  113. <%= global.setTypeDest("0xC0"); %>
  114. <%= global.toMont_a() %>
  115. <%= addL1L2() %>
  116. add_s1ml2m:
  117. <%= global.setTypeDest("0xC0"); %>
  118. <%= addL1L2() %>
  119. ;;;;
  120. add_l1l2:
  121. bt rax, 62 ; check if montgomery first
  122. jc add_l1ml2
  123. add_l1nl2:
  124. bt rcx, 62 ; check if montgomery second
  125. jc add_l1nl2m
  126. add_l1nl2n:
  127. <%= global.setTypeDest("0x80"); %>
  128. <%= addL1L2() %>
  129. add_l1nl2m:
  130. <%= global.setTypeDest("0xC0"); %>
  131. <%= global.toMont_a(); %>
  132. <%= addL1L2() %>
  133. add_l1ml2:
  134. bt rcx, 62 ; check if montgomery seconf
  135. jc add_l1ml2m
  136. add_l1ml2n:
  137. <%= global.setTypeDest("0xC0"); %>
  138. <%= global.toMont_b(); %>
  139. <%= addL1L2() %>
  140. add_l1ml2m:
  141. <%= global.setTypeDest("0xC0"); %>
  142. <%= addL1L2() %>
  143. ;;;;;;;;;;;;;;;;;;;;;;
  144. ; rawAddLL
  145. ;;;;;;;;;;;;;;;;;;;;;;
  146. ; Adds two elements of type long
  147. ; Params:
  148. ; rsi <= Pointer to the long data of element 1
  149. ; rdx <= Pointer to the long data of element 2
  150. ; rdi <= Pointer to the long data of result
  151. ; Modified Registers:
  152. ; rax
  153. ;;;;;;;;;;;;;;;;;;;;;;
  154. rawAddLL:
  155. ; Add component by component with carry
  156. <% for (let i=0; i<n64; i++) { %>
  157. mov rax, [rsi + <%=i*8%>]
  158. <%= i==0 ? "add" : "adc" %> rax, [rdx + <%=i*8%>]
  159. mov [rdi + <%=i*8%>], rax
  160. <% } %>
  161. jc rawAddLL_sq ; if overflow, substract q
  162. ; Compare with q
  163. <% for (let i=0; i<n64; i++) { %>
  164. <% if (i>0) { %>
  165. mov rax, [rdi + <%= (n64-i-1)*8 %>]
  166. <% } %>
  167. cmp rax, [q + <%= (n64-i-1)*8 %>]
  168. jc rawAddLL_done ; q is bigget so done.
  169. jnz rawAddLL_sq ; q is lower
  170. <% } %>
  171. ; If equal substract q
  172. rawAddLL_sq:
  173. <% for (let i=0; i<n64; i++) { %>
  174. mov rax, [q + <%=i*8%>]
  175. <%= i==0 ? "sub" : "sbb" %> [rdi + <%=i*8%>], rax
  176. <% } %>
  177. rawAddLL_done:
  178. ret
  179. ;;;;;;;;;;;;;;;;;;;;;;
  180. ; rawAddLS
  181. ;;;;;;;;;;;;;;;;;;;;;;
  182. ; Adds two elements of type long
  183. ; Params:
  184. ; rdi <= Pointer to the long data of result
  185. ; rsi <= Pointer to the long data of element 1
  186. ; rdx <= Value to be added
  187. ;;;;;;;;;;;;;;;;;;;;;;
  188. rawAddLS:
  189. ; Add component by component with carry
  190. add rdx, [rsi]
  191. mov [rdi] ,rdx
  192. <% for (let i=1; i<n64; i++) { %>
  193. mov rdx, 0
  194. adc rdx, [rsi + <%=i*8%>]
  195. mov [rdi + <%=i*8%>], rdx
  196. <% } %>
  197. jc rawAddLS_sq ; if overflow, substract q
  198. ; Compare with q
  199. <% for (let i=0; i<n64; i++) { %>
  200. mov rax, [rdi + <%= (n64-i-1)*8 %>]
  201. cmp rax, [q + <%= (n64-i-1)*8 %>]
  202. jc rawAddLS_done ; q is bigget so done.
  203. jnz rawAddLS_sq ; q is lower
  204. <% } %>
  205. ; If equal substract q
  206. rawAddLS_sq:
  207. <% for (let i=0; i<n64; i++) { %>
  208. mov rax, [q + <%=i*8%>]
  209. <%= i==0 ? "sub" : "sbb" %> [rdi + <%=i*8%>], rax
  210. <% } %>
  211. rawAddLS_done:
  212. ret