6e818ad81d415807eb76daa4ca550be6b983ff62
[platform/upstream/nettle.git] / x86_64 / ecc-521-modp.asm
1 C x86_64/ecc-521-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-521-modp.asm"
34
35 GMP_NUMB_BITS(64)
36
37 define(<RP>, <%rsi>)
38 define(<U0>, <%rax>)
39 define(<U1>, <%rbx>)
40 define(<U2>, <%rcx>)
41 define(<U3>, <%rdx>)
42 define(<U4>, <%rbp>)
43 define(<U5>, <%rdi>)
44 define(<U6>, <%r8>)
45 define(<U7>, <%r9>)
46 define(<U8>, <%r10>)
47 define(<U9>, <%r11>)
48 define(<T0>, <%r12>)
49 define(<T1>, <%r13>)
50
51 PROLOGUE(nettle_ecc_521_modp)
52         W64_ENTRY(2, 0)
53         push    %rbx
54         push    %rbp
55         push    %r12
56         push    %r13
57
58         C Read top 17 limbs, shift left 55 bits
59         mov     72(RP), U1
60         mov     U1, U0
61         shl     $55, U0
62         shr     $9, U1
63
64         mov     80(RP), U2
65         mov     U2, T0
66         shr     $9, U2
67         shl     $55, T0
68         or      T0, U1
69
70         mov     88(RP), U3
71         mov     U3, T0
72         shr     $9, U3
73         shl     $55, T0
74         or      T0, U2
75
76         mov     96(RP), U4
77         mov     U4, T0
78         shr     $9, U4
79         shl     $55, T0
80         or      T0, U3
81
82         mov     104(RP), U5
83         mov     U5, T0
84         shr     $9, U5
85         shl     $55, T0
86         or      T0, U4
87
88         mov     112(RP), U6
89         mov     U6, T0
90         shr     $9, U6
91         shl     $55, T0
92         or      T0, U5
93
94         mov     120(RP), U7
95         mov     U7, T0
96         shr     $9, U7
97         shl     $55, T0
98         or      T0, U6
99
100         mov     128(RP), U8
101         mov     U8, T0
102         shr     $9, U8
103         shl     $55, T0
104         or      T0, U7
105
106         mov     136(RP), U9
107         mov     U9, T0
108         shr     $9, U9
109         shl     $55, T0
110         or      T0, U8
111
112         add       (RP), U0
113         adc      8(RP), U1
114         adc     16(RP), U2
115         adc     24(RP), U3
116         adc     32(RP), U4
117         adc     40(RP), U5
118         adc     48(RP), U6
119         adc     56(RP), U7
120         adc     64(RP), U8
121         adc     $0, U9
122
123         C Top limbs are <U9, U8>. Keep low 9 bits of 8, and fold the
124         C top bits (at most 65 bits).
125         mov     U8, T0
126         shr     $9, T0
127         and     $0x1ff, U8
128         mov     U9, T1
129         shl     $55, U9
130         shr     $9, T1
131         or      U9, T0
132
133         add     T0, U0
134         mov     U0, (RP)
135         adc     T1, U1
136         mov     U1, 8(RP)
137         adc     $0, U2
138         mov     U2, 16(RP)
139         adc     $0, U3
140         mov     U3, 24(RP)
141         adc     $0, U4
142         mov     U4, 32(RP)
143         adc     $0, U5
144         mov     U5, 40(RP)
145         adc     $0, U6
146         mov     U6, 48(RP)
147         adc     $0, U7
148         mov     U7, 56(RP)
149         adc     $0, U8
150         mov     U8, 64(RP)
151
152         pop     %r13
153         pop     %r12
154         pop     %rbp
155         pop     %rbx
156         W64_EXIT(2, 0)
157         ret
158 EPILOGUE(nettle_ecc_521_modp)