Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / x86_64 / ecc-256-redc.asm
1 C nettle, low-level cryptographics library
2 C
3 C Copyright (C) 2013 Niels Möller
4 C
5 C The nettle library is free software; you can redistribute it and/or modify
6 C it under the terms of the GNU Lesser General Public License as published by
7 C the Free Software Foundation; either version 2.1 of the License, or (at your
8 C option) any later version.
9 C
10 C The nettle library is distributed in the hope that it will be useful, but
11 C WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 C or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
13 C License for more details.
14 C
15 C You should have received a copy of the GNU Lesser General Public License
16 C along with the nettle library; see the file COPYING.LIB.  If not, write to
17 C the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 C MA 02111-1301, USA.
19
20         .file "ecc-256-redc.asm"
21
22 define(<RP>, <%rsi>)
23 define(<U0>, <%rdi>) C Overlaps unused ecc input
24 define(<U1>, <%rcx>)
25 define(<U2>, <%rax>)
26 define(<U3>, <%rdx>)
27 define(<U4>, <%r8>)
28 define(<U5>, <%r9>)
29 define(<U6>, <%r10>)
30 define(<F0>, <%r11>)
31 define(<F1>, <%r12>)
32 define(<F2>, <%rbx>)
33 define(<F3>, <%rbp>)
34
35 C FOLD(x), sets (F3,F2,F1,F0)  <-- (x << 224) - (x << 128) - (x<<32)
36 define(<FOLD>, <
37         mov     $1, F2
38         mov     $1, F3
39         shl     <$>32, F2
40         shr     <$>32, F3
41         xor     F0,F0
42         xor     F1,F1
43         sub     F2, F0
44         sbb     F3, F1
45         sbb     $1, F2
46         sbb     <$>0, F3
47 >)
48 PROLOGUE(nettle_ecc_256_redc)
49         W64_ENTRY(2, 0)
50         C save all registers that need to be saved
51         push    %rbx
52         push    %rbp
53         push    %r12
54
55         mov     (RP), U0
56         FOLD(U0)
57         mov     8(RP), U1
58         mov     16(RP), U2
59         mov     24(RP), U3
60         sub     F0, U1
61         sbb     F1, U2
62         sbb     F2, U3
63         sbb     F3, U0          C Add in later
64
65         FOLD(U1)
66         mov     32(RP), U4
67         sub     F0, U2
68         sbb     F1, U3
69         sbb     F2, U4
70         sbb     F3, U1
71
72         FOLD(U2)
73         mov     40(RP), U5
74         sub     F0, U3
75         sbb     F1, U4
76         sbb     F2, U5
77         sbb     F3, U2
78
79         FOLD(U3)
80         mov     48(RP), U6
81         sub     F0, U4
82         sbb     F1, U5
83         sbb     F2, U6
84         sbb     F3, U3
85
86         add     U4, U0
87         adc     U5, U1
88         adc     U6, U2
89         adc     56(RP), U3
90
91         C If carry, we need to add in
92         C 2^256 - p = <0xfffffffe, 0xff..ff, 0xffffffff00000000, 1>
93         sbb     F2, F2
94         mov     F2, F0
95         mov     F2, F1
96         mov     XREG(F2), XREG(F3)
97         neg     F0
98         shl     $32, F1
99         and     $-2, XREG(F3)
100
101         add     F0, U0
102         mov     U0, (RP)
103         adc     F1, U1
104         mov     U1, 8(RP)
105         adc     F2, U2
106         mov     U2, 16(RP)
107         adc     F3, U3
108
109         mov     U3, 24(RP)
110
111         pop     %r12
112         pop     %rbp
113         pop     %rbx
114         W64_EXIT(2, 0)
115         ret
116 EPILOGUE(nettle_ecc_256_redc)