07bd4003670515487726c96adc74289fe9970f50
[platform/upstream/nettle.git] / x86_64 / ecc-224-modp.asm
1 C x86_64/ecc-224-modp.asm
2
3 ifelse(<
4    Copyright (C) 2013 Niels Möller
5
6    This file is part of GNU Nettle.
7
8    GNU Nettle is free software: you can redistribute it and/or
9    modify it under the terms of either:
10
11      * the GNU Lesser General Public License as published by the Free
12        Software Foundation; either version 3 of the License, or (at your
13        option) any later version.
14
15    or
16
17      * the GNU General Public License as published by the Free
18        Software Foundation; either version 2 of the License, or (at your
19        option) any later version.
20
21    or both in parallel, as here.
22
23    GNU Nettle is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26    General Public License for more details.
27
28    You should have received copies of the GNU General Public License and
29    the GNU Lesser General Public License along with this program.  If
30    not, see http://www.gnu.org/licenses/.
31 >)
32
33         .file "ecc-224-modp.asm"
34
35 GMP_NUMB_BITS(64)
36
37 define(<RP>, <%rsi>)
38 define(<T0>, <%rdi>) C Overlaps unused modulo input
39 define(<T1>, <%rcx>)
40 define(<H0>, <%rax>)
41 define(<H1>, <%rdx>)
42 define(<H2>, <%r8>)
43 define(<F0>, <%r9>)
44 define(<F1>, <%r10>)
45 define(<F2>, <%r11>)
46
47         C ecc_224_modp (const struct ecc_modulo *m, mp_limb_t *rp)
48 PROLOGUE(nettle_ecc_224_modp)
49         W64_ENTRY(2, 0)
50         mov     48(RP), H0
51         mov     56(RP), H1
52         C Set (F2,F1,F0)  <--  (H1,H0) << 32
53         mov     H0, F0
54         mov     H0, F1
55         shl     $32, F0
56         shr     $32, F1
57         mov     H1, F2
58         mov     H1, T0
59         shl     $32, T0
60         shr     $32, F2
61         or      T0, F1
62
63         xor     H2, H2
64         mov     16(RP), T0
65         mov     24(RP), T1
66         sub     F0, T0
67         sbb     F1, T1
68         sbb     F2, H0
69         sbb     $0, H1          C No further borrow
70
71         adc     32(RP), H0
72         adc     40(RP), H1
73         adc     $0, H2
74
75         C Set (F2,F1,F0)  <--  (H2,H1,H0) << 32
76         C To free registers, add in T1, T0 as soon as H0, H1 have been copied
77         mov     H0, F0
78         mov     H0, F1
79         add     T0, H0
80         mov     H1, F2
81         mov     H1, T0
82         adc     T1, H1
83         mov     H2, T1
84         adc     $0, H2
85
86         C Shift 32 bits
87         shl     $32, F0
88         shr     $32, F1
89         shl     $32, T0
90         shr     $32, F2
91         shl     $32, T1
92         or      T0, F1
93         or      T1, F2
94
95         mov     (RP), T0
96         mov     8(RP), T1
97         sub     F0, T0
98         sbb     F1, T1
99         sbb     F2, H0
100         sbb     $0, H1
101         sbb     $0, H2
102
103         C We now have H2, H1, H0, T1, T0, with 33 bits left to reduce
104         C Set F0       <-- (H2, H1) >> 32
105         C Set (F2,F1)  <-- (H2, H1 & 0xffffffff00000000)
106         C H1  <--  H1 & 0xffffffff
107
108         mov     H1, F0
109         mov     H1, F1
110         mov     H2, F2
111         movl    XREG(H1), XREG(H1)      C Clears high 32 bits
112         sub     H1, F1                  C Clears low 32 bits
113         shr     $32, F0
114         shl     $32, H2
115         or      H2, F0
116
117         sub     F0, T0
118         sbb     $0, F1
119         sbb     $0, F2
120         add     F1, T1
121         adc     F2, H0
122         adc     $0, H1
123
124         mov     T0, (RP)
125         mov     T1, 8(RP)
126         mov     H0, 16(RP)
127         mov     H1, 24(RP)
128
129         W64_EXIT(2, 0)
130         ret
131 EPILOGUE(nettle_ecc_224_modp)