1 /* fp_sqr_comba_12.i
2 *
3 * Copyright (C) 2006-2021 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL.
6 *
7 * wolfSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * wolfSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20 */
21
22
23
24 #ifdef TFM_SQR12
fp_sqr_comba12(fp_int * A,fp_int * B)25 int fp_sqr_comba12(fp_int *A, fp_int *B)
26 {
27 fp_digit *a, c0, c1, c2, sc0 = 0, sc1 = 0, sc2 = 0;
28 #ifdef TFM_ISO
29 fp_word tt;
30 #endif
31 #ifndef WOLFSSL_SMALL_STACK
32 fp_digit b[24];
33 #else
34 fp_digit *b;
35 #endif
36
37 #ifdef WOLFSSL_SMALL_STACK
38 b = (fp_digit*)XMALLOC(sizeof(fp_digit) * 24, NULL, DYNAMIC_TYPE_TMP_BUFFER);
39 if (b == NULL)
40 return FP_MEM;
41 #endif
42
43 a = A->dp;
44 COMBA_START;
45
46 /* clear carries */
47 CLEAR_CARRY;
48
49 /* output 0 */
50 SQRADD(a[0],a[0]);
51 COMBA_STORE(b[0]);
52
53 /* output 1 */
54 CARRY_FORWARD;
55 SQRADD2(a[0], a[1]);
56 COMBA_STORE(b[1]);
57
58 /* output 2 */
59 CARRY_FORWARD;
60 SQRADD2(a[0], a[2]); SQRADD(a[1], a[1]);
61 COMBA_STORE(b[2]);
62
63 /* output 3 */
64 CARRY_FORWARD;
65 SQRADD2(a[0], a[3]); SQRADD2(a[1], a[2]);
66 COMBA_STORE(b[3]);
67
68 /* output 4 */
69 CARRY_FORWARD;
70 SQRADD2(a[0], a[4]); SQRADD2(a[1], a[3]); SQRADD(a[2], a[2]);
71 COMBA_STORE(b[4]);
72
73 /* output 5 */
74 CARRY_FORWARD;
75 SQRADDSC(a[0], a[5]); SQRADDAC(a[1], a[4]); SQRADDAC(a[2], a[3]); SQRADDDB;
76 COMBA_STORE(b[5]);
77
78 /* output 6 */
79 CARRY_FORWARD;
80 SQRADDSC(a[0], a[6]); SQRADDAC(a[1], a[5]); SQRADDAC(a[2], a[4]); SQRADDDB; SQRADD(a[3], a[3]);
81 COMBA_STORE(b[6]);
82
83 /* output 7 */
84 CARRY_FORWARD;
85 SQRADDSC(a[0], a[7]); SQRADDAC(a[1], a[6]); SQRADDAC(a[2], a[5]); SQRADDAC(a[3], a[4]); SQRADDDB;
86 COMBA_STORE(b[7]);
87
88 /* output 8 */
89 CARRY_FORWARD;
90 SQRADDSC(a[0], a[8]); SQRADDAC(a[1], a[7]); SQRADDAC(a[2], a[6]); SQRADDAC(a[3], a[5]); SQRADDDB; SQRADD(a[4], a[4]);
91 COMBA_STORE(b[8]);
92
93 /* output 9 */
94 CARRY_FORWARD;
95 SQRADDSC(a[0], a[9]); SQRADDAC(a[1], a[8]); SQRADDAC(a[2], a[7]); SQRADDAC(a[3], a[6]); SQRADDAC(a[4], a[5]); SQRADDDB;
96 COMBA_STORE(b[9]);
97
98 /* output 10 */
99 CARRY_FORWARD;
100 SQRADDSC(a[0], a[10]); SQRADDAC(a[1], a[9]); SQRADDAC(a[2], a[8]); SQRADDAC(a[3], a[7]); SQRADDAC(a[4], a[6]); SQRADDDB; SQRADD(a[5], a[5]);
101 COMBA_STORE(b[10]);
102
103 /* output 11 */
104 CARRY_FORWARD;
105 SQRADDSC(a[0], a[11]); SQRADDAC(a[1], a[10]); SQRADDAC(a[2], a[9]); SQRADDAC(a[3], a[8]); SQRADDAC(a[4], a[7]); SQRADDAC(a[5], a[6]); SQRADDDB;
106 COMBA_STORE(b[11]);
107
108 /* output 12 */
109 CARRY_FORWARD;
110 SQRADDSC(a[1], a[11]); SQRADDAC(a[2], a[10]); SQRADDAC(a[3], a[9]); SQRADDAC(a[4], a[8]); SQRADDAC(a[5], a[7]); SQRADDDB; SQRADD(a[6], a[6]);
111 COMBA_STORE(b[12]);
112
113 /* output 13 */
114 CARRY_FORWARD;
115 SQRADDSC(a[2], a[11]); SQRADDAC(a[3], a[10]); SQRADDAC(a[4], a[9]); SQRADDAC(a[5], a[8]); SQRADDAC(a[6], a[7]); SQRADDDB;
116 COMBA_STORE(b[13]);
117
118 /* output 14 */
119 CARRY_FORWARD;
120 SQRADDSC(a[3], a[11]); SQRADDAC(a[4], a[10]); SQRADDAC(a[5], a[9]); SQRADDAC(a[6], a[8]); SQRADDDB; SQRADD(a[7], a[7]);
121 COMBA_STORE(b[14]);
122
123 /* output 15 */
124 CARRY_FORWARD;
125 SQRADDSC(a[4], a[11]); SQRADDAC(a[5], a[10]); SQRADDAC(a[6], a[9]); SQRADDAC(a[7], a[8]); SQRADDDB;
126 COMBA_STORE(b[15]);
127
128 /* output 16 */
129 CARRY_FORWARD;
130 SQRADDSC(a[5], a[11]); SQRADDAC(a[6], a[10]); SQRADDAC(a[7], a[9]); SQRADDDB; SQRADD(a[8], a[8]);
131 COMBA_STORE(b[16]);
132
133 /* output 17 */
134 CARRY_FORWARD;
135 SQRADDSC(a[6], a[11]); SQRADDAC(a[7], a[10]); SQRADDAC(a[8], a[9]); SQRADDDB;
136 COMBA_STORE(b[17]);
137
138 /* output 18 */
139 CARRY_FORWARD;
140 SQRADD2(a[7], a[11]); SQRADD2(a[8], a[10]); SQRADD(a[9], a[9]);
141 COMBA_STORE(b[18]);
142
143 /* output 19 */
144 CARRY_FORWARD;
145 SQRADD2(a[8], a[11]); SQRADD2(a[9], a[10]);
146 COMBA_STORE(b[19]);
147
148 /* output 20 */
149 CARRY_FORWARD;
150 SQRADD2(a[9], a[11]); SQRADD(a[10], a[10]);
151 COMBA_STORE(b[20]);
152
153 /* output 21 */
154 CARRY_FORWARD;
155 SQRADD2(a[10], a[11]);
156 COMBA_STORE(b[21]);
157
158 /* output 22 */
159 CARRY_FORWARD;
160 SQRADD(a[11], a[11]);
161 COMBA_STORE(b[22]);
162 COMBA_STORE2(b[23]);
163 COMBA_FINI;
164
165 B->used = 24;
166 B->sign = FP_ZPOS;
167 XMEMCPY(B->dp, b, 24 * sizeof(fp_digit));
168 fp_clamp(B);
169
170 #ifdef WOLFSSL_SMALL_STACK
171 XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
172 #endif
173 return FP_OKAY;
174 }
175 #endif
176
177
178