| 7 | .text |
| 8 | .globl beeu_mod_inverse_vartime |
| 9 | .hidden beeu_mod_inverse_vartime |
| 10 | .type beeu_mod_inverse_vartime, %function |
| 11 | .align 4 |
| 12 | beeu_mod_inverse_vartime: |
| 13 | // Reserve enough space for 14 8-byte registers on the stack |
| 14 | // in the first stp call for x29, x30. |
| 15 | // Then store the remaining callee-saved registers. |
| 16 | // |
| 17 | // | x29 | x30 | x19 | x20 | ... | x27 | x28 | x0 | x2 | |
| 18 | // ^ ^ |
| 19 | // sp <------------------- 112 bytes ----------------> old sp |
| 20 | // x29 (FP) |
| 21 | // |
| 22 | AARCH64_SIGN_LINK_REGISTER |
| 23 | stp x29,x30,[sp,#-112]! |
| 24 | add x29,sp,#0 |
| 25 | stp x19,x20,[sp,#16] |
| 26 | stp x21,x22,[sp,#32] |
| 27 | stp x23,x24,[sp,#48] |
| 28 | stp x25,x26,[sp,#64] |
| 29 | stp x27,x28,[sp,#80] |
| 30 | stp x0,x2,[sp,#96] |
| 31 |
| 32 | // B = b3..b0 := a |
| 33 | ldp x25,x26,[x1] |
| 34 | ldp x27,x28,[x1,#16] |
| 35 |
| 36 | // n3..n0 := n |
| 37 | // Note: the value of input params are changed in the following. |
| 38 | ldp x0,x1,[x2] |
| 39 | ldp x2,x30,[x2,#16] |
| 40 |
| 41 | // A = a3..a0 := n |
| 42 | mov x21, x0 |
| 43 | mov x22, x1 |
| 44 | mov x23, x2 |
| 45 | mov x24, x30 |
| 46 |
| 47 | // X = x4..x0 := 1 |
| 48 | mov x3, #1 |
| 49 | eor x4, x4, x4 |
| 50 | eor x5, x5, x5 |
| 51 | eor x6, x6, x6 |
| 52 | eor x7, x7, x7 |
| 53 |
| 54 | // Y = y4..y0 := 0 |
| 55 | eor x8, x8, x8 |
| 56 | eor x9, x9, x9 |
| 57 | eor x10, x10, x10 |
| 58 | eor x11, x11, x11 |
| 59 | eor x12, x12, x12 |
| 60 |
| 61 | .Lbeeu_loop: |
| 62 | // if B == 0, jump to .Lbeeu_loop_end |
| 63 | orr x14, x25, x26 |
| 64 | orr x14, x14, x27 |
| 65 |
| 66 | // reverse the bit order of x25. This is needed for clz after this macro |
| 67 | rbit x15, x25 |
| 68 |
| 69 | orr x14, x14, x28 |
| 70 | cbz x14,.Lbeeu_loop_end |
| 71 |
| 72 |
| 73 | // 0 < B < |n|, |
| 74 | // 0 < A <= |n|, |
| 75 | // (1) X*a == B (mod |n|), |
| 76 | // (2) (-1)*Y*a == A (mod |n|) |
| 77 |
| 78 | // Now divide B by the maximum possible power of two in the |
| 79 | // integers, and divide X by the same value mod |n|. |
| 80 | // When we're done, (1) still holds. |
| 81 |
| 82 | // shift := number of trailing 0s in x25 |
| 83 | // ( = number of leading 0s in x15; see the "rbit" instruction in TEST_B_ZERO) |
| 84 | clz x13, x15 |
| 85 |
| 86 | // If there is no shift, goto shift_A_Y |
| 87 | cbz x13, .Lbeeu_shift_A_Y |
| 88 |
| 89 | // Shift B right by "x13" bits |
| 90 | neg x14, x13 |
| 91 | lsr x25, x25, x13 |
| 92 | lsl x15, x26, x14 |
| 93 |
| 94 | lsr x26, x26, x13 |
| 95 | lsl x19, x27, x14 |
| 96 |
| 97 | orr x25, x25, x15 |
| 98 |
| 99 | lsr x27, x27, x13 |
| 100 | lsl x20, x28, x14 |
| 101 |
| 102 | orr x26, x26, x19 |
| 103 |
| 104 | lsr x28, x28, x13 |
| 105 |
| 106 | orr x27, x27, x20 |
| 107 |
| 108 |
| 109 | // Shift X right by "x13" bits, adding n whenever X becomes odd. |
| 110 | // x13--; |
| 111 | // x14 := 0; needed in the addition to the most significant word in SHIFT1 |
| 112 | eor x14, x14, x14 |
| 113 | .Lbeeu_shift_loop_X: |
| 114 | tbz x3, #0, .Lshift1_0 |
| 115 | adds x3, x3, x0 |
| 116 | adcs x4, x4, x1 |
| 117 | adcs x5, x5, x2 |
| 118 | adcs x6, x6, x30 |
| 119 | adc x7, x7, x14 |
| 120 | .Lshift1_0: |
| 121 | // var0 := [var1|var0]<64..1>; |
| 122 | // i.e. concatenate var1 and var0, |
| 123 | // extract bits <64..1> from the resulting 128-bit value |
| 124 | // and put them in var0 |
| 125 | extr x3, x4, x3, #1 |
| 126 | extr x4, x5, x4, #1 |
| 127 | extr x5, x6, x5, #1 |
| 128 | extr x6, x7, x6, #1 |
| 129 | lsr x7, x7, #1 |
| 130 |
| 131 | subs x13, x13, #1 |
| 132 | bne .Lbeeu_shift_loop_X |
| 133 |
| 134 | // Note: the steps above perform the same sequence as in p256_beeu-x86_64-asm.pl |
| 135 | // with the following differences: |
| 136 | // - "x13" is set directly to the number of trailing 0s in B |
| 137 | // (using rbit and clz instructions) |
| 138 | // - The loop is only used to call SHIFT1(X) |
| 139 | // and x13 is decreased while executing the X loop. |
| 140 | // - SHIFT256(B, x13) is performed before right-shifting X; they are independent |
| 141 |
| 142 | .Lbeeu_shift_A_Y: |
| 143 | // Same for A and Y. |
| 144 | // Afterwards, (2) still holds. |
| 145 | // Reverse the bit order of x21 |
| 146 | // x13 := number of trailing 0s in x21 (= number of leading 0s in x15) |
| 147 | rbit x15, x21 |
| 148 | clz x13, x15 |
| 149 |
| 150 | // If there is no shift, goto |B-A|, X+Y update |
| 151 | cbz x13, .Lbeeu_update_B_X_or_A_Y |
| 152 |
| 153 | // Shift A right by "x13" bits |
| 154 | neg x14, x13 |
| 155 | lsr x21, x21, x13 |
| 156 | lsl x15, x22, x14 |
| 157 |
| 158 | lsr x22, x22, x13 |
| 159 | lsl x19, x23, x14 |
| 160 |
| 161 | orr x21, x21, x15 |
| 162 |
| 163 | lsr x23, x23, x13 |
| 164 | lsl x20, x24, x14 |
| 165 |
| 166 | orr x22, x22, x19 |
| 167 |
| 168 | lsr x24, x24, x13 |
| 169 |
| 170 | orr x23, x23, x20 |
| 171 |
| 172 |
| 173 | // Shift Y right by "x13" bits, adding n whenever Y becomes odd. |
| 174 | // x13--; |
| 175 | // x14 := 0; needed in the addition to the most significant word in SHIFT1 |
| 176 | eor x14, x14, x14 |
| 177 | .Lbeeu_shift_loop_Y: |
| 178 | tbz x8, #0, .Lshift1_1 |
| 179 | adds x8, x8, x0 |
| 180 | adcs x9, x9, x1 |
| 181 | adcs x10, x10, x2 |
| 182 | adcs x11, x11, x30 |
| 183 | adc x12, x12, x14 |
| 184 | .Lshift1_1: |
| 185 | // var0 := [var1|var0]<64..1>; |
| 186 | // i.e. concatenate var1 and var0, |
| 187 | // extract bits <64..1> from the resulting 128-bit value |
| 188 | // and put them in var0 |
| 189 | extr x8, x9, x8, #1 |
| 190 | extr x9, x10, x9, #1 |
| 191 | extr x10, x11, x10, #1 |
| 192 | extr x11, x12, x11, #1 |
| 193 | lsr x12, x12, #1 |
| 194 |
| 195 | subs x13, x13, #1 |
| 196 | bne .Lbeeu_shift_loop_Y |
| 197 |
| 198 | .Lbeeu_update_B_X_or_A_Y: |
| 199 | // Try T := B - A; if cs, continue with B > A (cs: carry set = no borrow) |
| 200 | // Note: this is a case of unsigned arithmetic, where T fits in 4 64-bit words |
| 201 | // without taking a sign bit if generated. The lack of a carry would |
| 202 | // indicate a negative result. See, for example, |
| 203 | // https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/condition-codes-1-condition-flags-and-codes |
| 204 | subs x14, x25, x21 |
| 205 | sbcs x15, x26, x22 |
| 206 | sbcs x19, x27, x23 |
| 207 | sbcs x20, x28, x24 |
| 208 | bcs .Lbeeu_B_greater_than_A |
| 209 |
| 210 | // Else A > B => |
| 211 | // A := A - B; Y := Y + X; goto beginning of the loop |
| 212 | subs x21, x21, x25 |
| 213 | sbcs x22, x22, x26 |
| 214 | sbcs x23, x23, x27 |
| 215 | sbcs x24, x24, x28 |
| 216 |
| 217 | adds x8, x8, x3 |
| 218 | adcs x9, x9, x4 |
| 219 | adcs x10, x10, x5 |
| 220 | adcs x11, x11, x6 |
| 221 | adc x12, x12, x7 |
| 222 | b .Lbeeu_loop |
| 223 |
| 224 | .Lbeeu_B_greater_than_A: |
| 225 | // Continue with B > A => |
| 226 | // B := B - A; X := X + Y; goto beginning of the loop |
| 227 | mov x25, x14 |
| 228 | mov x26, x15 |
| 229 | mov x27, x19 |
| 230 | mov x28, x20 |
| 231 |
| 232 | adds x3, x3, x8 |
| 233 | adcs x4, x4, x9 |
| 234 | adcs x5, x5, x10 |
| 235 | adcs x6, x6, x11 |
| 236 | adc x7, x7, x12 |
| 237 | b .Lbeeu_loop |
| 238 |
| 239 | .Lbeeu_loop_end: |
| 240 | // The Euclid's algorithm loop ends when A == gcd(a,n); |
| 241 | // this would be 1, when a and n are co-prime (i.e. do not have a common factor). |
| 242 | // Since (-1)*Y*a == A (mod |n|), Y>0 |
| 243 | // then out = -Y mod n |
| 244 |
| 245 | // Verify that A = 1 ==> (-1)*Y*a = A = 1 (mod |n|) |
| 246 | // Is A-1 == 0? |
| 247 | // If not, fail. |
| 248 | sub x14, x21, #1 |
| 249 | orr x14, x14, x22 |
| 250 | orr x14, x14, x23 |
| 251 | orr x14, x14, x24 |
| 252 | cbnz x14, .Lbeeu_err |
| 253 |
| 254 | // If Y>n ==> Y:=Y-n |
| 255 | .Lbeeu_reduction_loop: |
| 256 | // x_i := y_i - n_i (X is no longer needed, use it as temp) |
| 257 | // (x14 = 0 from above) |
| 258 | subs x3, x8, x0 |
| 259 | sbcs x4, x9, x1 |
| 260 | sbcs x5, x10, x2 |
| 261 | sbcs x6, x11, x30 |
| 262 | sbcs x7, x12, x14 |
| 263 |
| 264 | // If result is non-negative (i.e., cs = carry set = no borrow), |
| 265 | // y_i := x_i; goto reduce again |
| 266 | // else |
| 267 | // y_i := y_i; continue |
| 268 | csel x8, x3, x8, cs |
| 269 | csel x9, x4, x9, cs |
| 270 | csel x10, x5, x10, cs |
| 271 | csel x11, x6, x11, cs |
| 272 | csel x12, x7, x12, cs |
| 273 | bcs .Lbeeu_reduction_loop |
| 274 |
| 275 | // Now Y < n (Y cannot be equal to n, since the inverse cannot be 0) |
| 276 | // out = -Y = n-Y |
| 277 | subs x8, x0, x8 |
| 278 | sbcs x9, x1, x9 |
| 279 | sbcs x10, x2, x10 |
| 280 | sbcs x11, x30, x11 |
| 281 |
| 282 | // Save Y in output (out (x0) was saved on the stack) |
| 283 | ldr x3, [sp,#96] |
| 284 | stp x8, x9, [x3] |
| 285 | stp x10, x11, [x3,#16] |
| 286 | // return 1 (success) |
| 287 | mov x0, #1 |
| 288 | b .Lbeeu_finish |
| 289 |
| 290 | .Lbeeu_err: |
| 291 | // return 0 (error) |
| 292 | eor x0, x0, x0 |
| 293 |
| 294 | .Lbeeu_finish: |
| 295 | // Restore callee-saved registers, except x0, x2 |
| 296 | add sp,x29,#0 |
| 297 | ldp x19,x20,[sp,#16] |
| 298 | ldp x21,x22,[sp,#32] |
| 299 | ldp x23,x24,[sp,#48] |
| 300 | ldp x25,x26,[sp,#64] |
| 301 | ldp x27,x28,[sp,#80] |
| 302 | ldp x29,x30,[sp],#112 |
| 303 |
| 304 | AARCH64_VALIDATE_LINK_REGISTER |
| 305 | ret |
| 306 | .size beeu_mod_inverse_vartime,.-beeu_mod_inverse_vartime |
| 307 | #endif // !OPENSSL_NO_ASM && defined(OPENSSL_AARCH64) && defined(__ELF__) |
| 308 |