]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bigint/bigint_asm.S
quickfix(tm) applied
[avr-crypto-lib.git] / bigint / bigint_asm.S
1 /* bigint_asm.S */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "avr-asm-macros.S"
21 #include "bigint_adjust.S"
22 #include "bigint_add_u.S"
23
24
25 /******************************************************************************/
26 /*
27 void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
28         uint16_t i,j=0;
29         uint16_t t=0;
30         if(scale>dest->length_B)
31                 memset(dest->wordv+dest->length_B, 0, scale-dest->length_B);
32         for(i=scale; i<a->length_B+scale; ++i,++j){
33                 t = a->wordv[j] + t;
34                 if(dest->length_B>i){
35                         t += dest->wordv[i];
36                 }
37                 dest->wordv[i] = (uint8_t)t;
38                 t>>=8;
39         }
40         while(t){
41                 if(dest->length_B>i){
42                         t = dest->wordv[i] + t;
43                 }
44                 dest->wordv[i] = (uint8_t)t;
45                 t>>=8;
46                 ++i;
47         }
48         if(dest->length_B < i){
49                 dest->length_B = i;
50         }
51         bigint_adjust(dest);
52 }
53 */
54
55 DST_SIZE_0 = 22
56 DST_SIZE_1 = 23
57 SRC_SIZE_0 = 20
58 SRC_SIZE_1 = 23
59 SCALE_0    = 18
60 SCALE_1    = 19
61 DST_CTX_0  =  6
62 DST_CTX_1  =  7
63 SRC_CTX_0  =  8
64 SRC_CTX_1  =  9
65 TMP_0      = 10
66 TMP_1      = 11
67
68 .global bigint_add_scale_u
69
70
71 /******************************************************************************/
72 /******************************************************************************/
73 /******************************************************************************/
74
75 DST_LEN_0 = 22
76 DST_LEN_1 = 23
77 SRC_LEN_0 = 20
78 SRC_LEN_1 = 21
79 SCALE_0   = 18
80 SCALE_1   = 19
81 DST_CTX_0 =  6
82 DST_CTX_1 =  7
83 SRC_CTX_0 =  8
84 SRC_CTX_1 =  9
85 TMP_0     = 10
86 TMP_1     = 11
87
88 bigint_add_scale_u:
89         movw r30, r24 /* dest ptr */
90         movw r26, r22 /* src ptr */
91         movw r24, r20 /* scale */
92         movw SCALE_0, r24
93         ld r24, X+
94         ld r25, X+
95         adiw r24, 0
96         brne 10f
97         ret
98 10:
99         movw SRC_LEN_0, r24
100         push_range 6, 11
101         movw DST_CTX_0, r30
102         movw SRC_CTX_0, r26
103
104         /* pad dest with zeros to length of SRC_LENGTH + scale */
105         adiw r26, 1
106         ld TMP_0, X+
107         ld TMP_1, X+
108         movw r26, TMP_0 /* move SRC_WORDV to X */
109         ldd DST_LEN_0, Z+0
110         ldd DST_LEN_1, Z+1
111     ldd TMP_0, Z+3
112     ldd TMP_1, Z+4
113     movw r30, TMP_0 /* move DEST_WORDV to Z */
114         movw TMP_0, SCALE_0
115         sub TMP_0, DST_LEN_0
116         sbc TMP_1, DST_LEN_1
117         movw r24, TMP_0
118         brmi 40f /* no padding needed since DST_LEN > scale */
119         add r30, DST_LEN_0 /* add DST_LEN to Z (DEST_WORDV)*/
120         adc r31, DST_LEN_1
121         /* pad and copy src in front of dest */
122 10: /* padding loop */
123         sbiw r24, 1
124         brmi 11f
125         st Z+, r1
126         rjmp 10b
127 11:
128         /* start of copy */
129
130         movw r24, SRC_LEN_0
131
132 12: /* copy loop */
133         sbiw r24, 1
134         brmi 13f
135         ld TMP_0, X+
136         st Z+, TMP_0
137         rjmp 12b
138 13:
139         movw TMP_0, SCALE_0
140         add TMP_0, SRC_LEN_0
141         adc TMP_1, SRC_LEN_1
142         movw r30, DST_CTX_0
143         std Z+0, TMP_0
144         std Z+1, TMP_1
145         movw r24, r30
146 99:
147         pop_range 6, 11
148         rjmp bigint_adjust
149 40:
150     /* TODO */
151     /* Z points at DST_WORDV */
152     /* X points at SRC_WORDV */
153     /* r24:r25 contains scale - DST_LEN (negativ) */
154     /* set T bit if DST_LEN > SCR_LEN + scale */
155     clt
156     add r30, SCALE_0
157     adc r31, SCALE_1
158     add TMP_0, SRC_LEN_0
159     adc TMP_1, SRC_LEN_1
160         brpl 41f
161         set
162         /* DST_LEN > SRC_LEN + scale && DST_LEN > scale */
163         /*
164                +-------+-------+ SRC + scale
165            +------+------------+ DST
166         */
167         movw r24, SRC_LEN_0
168         rjmp 44f
169 41:
170         /* DST_LEN <= SRC_LEN + scale && DST_LEN > scale */
171         /*
172                +-------+-------+ SRC + scale
173                   +------------+ DST
174         */
175         com r24 /* negate r24:r25 */
176         com r25
177         adiw r24, 1
178 44:
179         clc
180 45:
181         dec r24
182         brpl 46f
183         dec r25
184         brmi 50f
185 46:     ld TMP_0, X+
186         ld TMP_1, Z
187         adc TMP_0, TMP_1
188         st Z+, TMP_0
189         rjmp 45b
190
191 50:
192     /* do the overhaning part */
193     rol r1
194     movw r24, r30
195     movw r30, DST_CTX_0
196     ldd TMP_0, Z+3
197     ldd TMP_1, Z+4
198     movw r30, TMP_0
199     add r30, DST_LEN_0
200     adc r31, DST_LEN_1
201     adiw r30, 1
202     st Z, r1
203     movw r30, r24
204     ror r1
205         brtc 60f
206 51:     brcc 53f
207 52:     ld TMP_0, Z
208         adc TMP_0, r1
209         st Z+, TMP_0
210         brcs 52b
211 53:
212     /* TODO */
213     movw r24, r30
214     movw r30, DST_CTX_0
215     ldd TMP_0, Z+3
216     ldd TMP_1, Z+4
217     sub r24, TMP_0
218     sbc r25, TMP_1
219     std Z+0, r24
220     std Z+1, r25
221     movw r24, r30
222     rjmp 99b
223
224 60: rol r1 /* backup carry */
225         movw r24, SRC_LEN_0
226     add r24, SCALE_0
227     adc r25, SCALE_1
228     sub r24, DST_LEN_0
229     sbc r25, DST_LEN_1
230     ror r1 /* restore carry */
231
232 61: dec r24
233     brpl 62f
234     dec r25
235     brmi 63f
236 62: ld TMP_0, X+
237     adc TMP_0, r1
238     st Z+, TMP_0
239     rjmp 61b
240 63:
241         brcc 53b
242         ldi r24, 1
243         st Z+, r24
244         rjmp 53b