15cc0c1b0f2650aeb61bee82d66a11b0604d2d56
[platform/upstream/nettle.git] / arm / ecc-224-modp.asm
1 C arm/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         .arm
35
36 define(<RP>, <r1>)
37 define(<H>, <r0>) C Overlaps unused modulo argument
38
39 define(<T0>, <r2>)
40 define(<T1>, <r3>)
41 define(<T2>, <r4>)
42 define(<T3>, <r5>)
43 define(<T4>, <r6>)
44 define(<T5>, <r7>)
45 define(<T6>, <r8>)
46 define(<N3>, <r10>)
47 define(<L0>, <r11>)
48 define(<L1>, <r12>)
49 define(<L2>, <lr>)
50
51         C ecc_224_modp (const struct ecc_modulo *m, mp_limb_t *rp)
52         .text
53         .align 2
54
55 PROLOGUE(nettle_ecc_224_modp)
56         push    {r4,r5,r6,r7,r8,r10,r11,lr}
57
58         add     L2, RP, #28
59         ldm     L2, {T0,T1,T2,T3,T4,T5,T6}
60         mov     H, #0
61
62         adds    T0, T0, T4
63         adcs    T1, T1, T5
64         adcs    T2, T2, T6
65         adc     H, H, #0
66
67         C This switch from adcs to sbcs takes carry into account with
68         C correct sign, but it always subtracts 1 too much. We arrange
69         C to also add B^7 + 1 below, so the effect is adding p. This
70         C addition of p also ensures that the result never is
71         C negative.
72
73         sbcs    N3, T3, T0
74         sbcs    T4, T4, T1
75         sbcs    T5, T5, T2
76         sbcs    T6, T6, H
77         mov     H, #1           C This is the B^7
78         sbc     H, #0
79         subs    T6, T6, T3
80         sbc     H, #0
81
82         C Now subtract from low half
83         ldm     RP!, {L0,L1,L2}
84
85         C Clear carry, with the sbcs, this is the 1.
86         adds    RP, #0
87
88         sbcs    T0, L0, T0
89         sbcs    T1, L1, T1
90         sbcs    T2, L2, T2
91         ldm     RP!, {T3,L0,L1,L2}
92         sbcs    T3, T3, N3
93         sbcs    T4, L0, T4
94         sbcs    T5, L1, T5
95         sbcs    T6, L2, T6
96         rsc     H, H, #0
97
98         C Now -2 <= H <= 0 is the borrow, so subtract (B^3 - 1) |H|
99         C Use (B^3 - 1) H = <H, H, H> if -1 <=H <= 0, and
100         C     (B^3 - 1) H = <1,B-1, B-1, B-2> if H = -2
101         subs    T0, T0, H
102         asr     L1, H, #1
103         sbcs    T1, T1, L1
104         eor     H, H, L1
105         sbcs    T2, T2, L1
106         sbcs    T3, T3, H
107         sbcs    T4, T4, #0
108         sbcs    T5, T5, #0
109         sbcs    T6, T6, #0
110         sbcs    H, H, H
111
112         C Final borrow, subtract (B^3 - 1) |H|
113         subs    T0, T0, H
114         sbcs    T1, T1, H
115         sbcs    T2, T2, H
116         sbcs    T3, T3, #0
117         sbcs    T4, T4, #0
118         sbcs    T5, T5, #0
119         sbcs    T6, T6, #0
120
121         stmdb   RP, {T0,T1,T2,T3,T4,T5,T6}
122
123         pop     {r4,r5,r6,r7,r8,r10,r11,pc}
124 EPILOGUE(nettle_ecc_224_modp)