1 /* fp_sqr_comba_8.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_SQR8
fp_sqr_comba8(fp_int * A,fp_int * B)25 int fp_sqr_comba8(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[16];
33 #else
34    fp_digit *b;
35 #endif
36 
37 #ifdef WOLFSSL_SMALL_STACK
38    b = (fp_digit*)XMALLOC(sizeof(fp_digit) * 16, 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[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[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    SQRADD2(a[3], a[7]); SQRADD2(a[4], a[6]); SQRADD(a[5], a[5]);
101    COMBA_STORE(b[10]);
102 
103    /* output 11 */
104    CARRY_FORWARD;
105    SQRADD2(a[4], a[7]); SQRADD2(a[5], a[6]);
106    COMBA_STORE(b[11]);
107 
108    /* output 12 */
109    CARRY_FORWARD;
110    SQRADD2(a[5], a[7]); SQRADD(a[6], a[6]);
111    COMBA_STORE(b[12]);
112 
113    /* output 13 */
114    CARRY_FORWARD;
115    SQRADD2(a[6], a[7]);
116    COMBA_STORE(b[13]);
117 
118    /* output 14 */
119    CARRY_FORWARD;
120    SQRADD(a[7], a[7]);
121    COMBA_STORE(b[14]);
122    COMBA_STORE2(b[15]);
123    COMBA_FINI;
124 
125    B->used = 16;
126    B->sign = FP_ZPOS;
127    XMEMCPY(B->dp, b, 16 * sizeof(fp_digit));
128    fp_clamp(B);
129 
130 #ifdef WOLFSSL_SMALL_STACK
131    XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
132 #endif
133    return FP_OKAY;
134 }
135 #endif
136 
137 
138