1 /* fp_sqr_comba_6.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_SQR6
fp_sqr_comba6(fp_int * A,fp_int * B)25 int fp_sqr_comba6(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[12];
33 #else
34    fp_digit *b;
35 #endif
36 
37 #ifdef WOLFSSL_SMALL_STACK
38    b = (fp_digit*)XMALLOC(sizeof(fp_digit) * 12, 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    SQRADD2(a[1], a[5]); SQRADD2(a[2], a[4]); SQRADD(a[3], a[3]);
81    COMBA_STORE(b[6]);
82 
83    /* output 7 */
84    CARRY_FORWARD;
85    SQRADD2(a[2], a[5]); SQRADD2(a[3], a[4]);
86    COMBA_STORE(b[7]);
87 
88    /* output 8 */
89    CARRY_FORWARD;
90    SQRADD2(a[3], a[5]); SQRADD(a[4], a[4]);
91    COMBA_STORE(b[8]);
92 
93    /* output 9 */
94    CARRY_FORWARD;
95    SQRADD2(a[4], a[5]);
96    COMBA_STORE(b[9]);
97 
98    /* output 10 */
99    CARRY_FORWARD;
100    SQRADD(a[5], a[5]);
101    COMBA_STORE(b[10]);
102    COMBA_STORE2(b[11]);
103    COMBA_FINI;
104 
105    B->used = 12;
106    B->sign = FP_ZPOS;
107    XMEMCPY(B->dp, b, 12 * sizeof(fp_digit));
108    fp_clamp(B);
109 
110 #ifdef WOLFSSL_SMALL_STACK
111    XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
112 #endif
113    return FP_OKAY;
114 }
115 #endif
116 
117 
118