1 /*******************************************************************************
2 * Copyright 2016-2018 Intel Corporation
3 * All Rights Reserved.
4 *
5 * If this  software was obtained  under the  Intel Simplified  Software License,
6 * the following terms apply:
7 *
8 * The source code,  information  and material  ("Material") contained  herein is
9 * owned by Intel Corporation or its  suppliers or licensors,  and  title to such
10 * Material remains with Intel  Corporation or its  suppliers or  licensors.  The
11 * Material  contains  proprietary  information  of  Intel or  its suppliers  and
12 * licensors.  The Material is protected by  worldwide copyright  laws and treaty
13 * provisions.  No part  of  the  Material   may  be  used,  copied,  reproduced,
14 * modified, published,  uploaded, posted, transmitted,  distributed or disclosed
15 * in any way without Intel's prior express written permission.  No license under
16 * any patent,  copyright or other  intellectual property rights  in the Material
17 * is granted to  or  conferred  upon  you,  either   expressly,  by implication,
18 * inducement,  estoppel  or  otherwise.  Any  license   under such  intellectual
19 * property rights must be express and approved by Intel in writing.
20 *
21 * Unless otherwise agreed by Intel in writing,  you may not remove or alter this
22 * notice or  any  other  notice   embedded  in  Materials  by  Intel  or Intel's
23 * suppliers or licensors in any way.
24 *
25 *
26 * If this  software  was obtained  under the  Apache License,  Version  2.0 (the
27 * "License"), the following terms apply:
28 *
29 * You may  not use this  file except  in compliance  with  the License.  You may
30 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
31 *
32 *
33 * Unless  required  by   applicable  law  or  agreed  to  in  writing,  software
34 * distributed under the License  is distributed  on an  "AS IS"  BASIS,  WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 *
37 * See the   License  for the   specific  language   governing   permissions  and
38 * limitations under the License.
39 *******************************************************************************/
40 
41 /*
42 //     Intel(R) Integrated Performance Primitives. Cryptography Primitives.
43 //     GF(p^d) methods, if binomial generator
44 //
45 */
46 #include "owncp.h"
47 
48 #include "pcpgfpxmethod_binom_mulc.h"
49 #include "pcpgfpxmethod_com.h"
50 
51 //tbcd: temporary excluded: #include <assert.h>
52 
53 /*
54 // Multiplication in GF(p^2), if field polynomial: g(x) = x^2 + beta  => binominal
55 */
cpGFpxMul_p2_binom(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,const BNU_CHUNK_T * pB,gsEngine * pGFEx)56 static BNU_CHUNK_T* cpGFpxMul_p2_binom(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsEngine* pGFEx)
57 {
58    gsEngine* pGroundGFE = GFP_PARENT(pGFEx);
59    int groundElemLen = GFP_FELEN(pGroundGFE);
60 
61    mod_mul mulF = GFP_METHOD(pGroundGFE)->mul;
62    mod_add addF = GFP_METHOD(pGroundGFE)->add;
63    mod_sub subF = GFP_METHOD(pGroundGFE)->sub;
64 
65    const BNU_CHUNK_T* pA0 = pA;
66    const BNU_CHUNK_T* pA1 = pA+groundElemLen;
67 
68    const BNU_CHUNK_T* pB0 = pB;
69    const BNU_CHUNK_T* pB1 = pB+groundElemLen;
70 
71    BNU_CHUNK_T* pR0 = pR;
72    BNU_CHUNK_T* pR1 = pR+groundElemLen;
73 
74    BNU_CHUNK_T* t0 = cpGFpGetPool(4, pGroundGFE);
75    BNU_CHUNK_T* t1 = t0+groundElemLen;
76    BNU_CHUNK_T* t2 = t1+groundElemLen;
77    BNU_CHUNK_T* t3 = t2+groundElemLen;
78    //tbcd: temporary excluded: assert(NULL!=t0);
79 
80    #if defined GS_DBG
81    BNU_CHUNK_T* arg0 = cpGFpGetPool(1, pGroundGFE);
82    BNU_CHUNK_T* arg1 = cpGFpGetPool(1, pGroundGFE);
83    #endif
84    #if defined GS_DBG
85    cpGFpxGet(arg0, groundElemLen, pA0, pGroundGFE);
86    cpGFpxGet(arg1, groundElemLen, pB0, pGroundGFE);
87    #endif
88 
89    mulF(t0, pA0, pB0, pGroundGFE);    /* t0 = a[0]*b[0] */
90 
91    #if defined GS_DBG
92    cpGFpxGet(arg0, groundElemLen, pA1, pGroundGFE);
93    cpGFpxGet(arg1, groundElemLen, pB1, pGroundGFE);
94    #endif
95 
96    mulF(t1, pA1, pB1, pGroundGFE);    /* t1 = a[1]*b[1] */
97    addF(t2, pA0, pA1, pGroundGFE);    /* t2 = a[0]+a[1] */
98    addF(t3, pB0, pB1, pGroundGFE);    /* t3 = b[0]+b[1] */
99 
100    #if defined GS_DBG
101    cpGFpxGet(arg0, groundElemLen, t2, pGroundGFE);
102    cpGFpxGet(arg1, groundElemLen, t3, pGroundGFE);
103    #endif
104 
105    mulF(pR1, t2,  t3, pGroundGFE);    /* r[1] = (a[0]+a[1]) * (b[0]+b[1]) */
106    subF(pR1, pR1, t0, pGroundGFE);    /* r[1] -= a[0]*b[0]) + a[1]*b[1] */
107    subF(pR1, pR1, t1, pGroundGFE);
108 
109    cpGFpxMul_G0(t1, t1, pGFEx);
110    subF(pR0, t0, t1, pGroundGFE);
111 
112    #if defined GS_DBG
113    cpGFpReleasePool(2, pGroundGFE);
114    #endif
115 
116    cpGFpReleasePool(4, pGroundGFE);
117    return pR;
118 }
119 
120 /*
121 // Squaring in GF(p^2), if field polynomial: g(x) = x^2 + beta  => binominal
122 */
cpGFpxSqr_p2_binom(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFEx)123 static BNU_CHUNK_T* cpGFpxSqr_p2_binom(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFEx)
124 {
125    gsEngine* pGroundGFE = GFP_PARENT(pGFEx);
126    int groundElemLen = GFP_FELEN(pGroundGFE);
127 
128    mod_mul mulF = GFP_METHOD(pGroundGFE)->mul;
129    mod_sqr sqrF = GFP_METHOD(pGroundGFE)->sqr;
130    mod_add addF = GFP_METHOD(pGroundGFE)->add;
131    mod_sub subF = GFP_METHOD(pGroundGFE)->sub;
132 
133    const BNU_CHUNK_T* pA0 = pA;
134    const BNU_CHUNK_T* pA1 = pA+groundElemLen;
135 
136    BNU_CHUNK_T* pR0 = pR;
137    BNU_CHUNK_T* pR1 = pR+groundElemLen;
138 
139    BNU_CHUNK_T* t0 = cpGFpGetPool(3, pGroundGFE);
140    BNU_CHUNK_T* t1 = t0+groundElemLen;
141    BNU_CHUNK_T* u0 = t1+groundElemLen;
142    //tbcd: temporary excluded: assert(NULL!=t0);
143 
144    #if defined GS_DBG
145    BNU_CHUNK_T* arg0 = cpGFpGetPool(1, pGroundGFE);
146    BNU_CHUNK_T* arg1 = cpGFpGetPool(1, pGroundGFE);
147    #endif
148    #if defined GS_DBG
149    cpGFpxGet(arg0, groundElemLen, pA0, pGroundGFE);
150    cpGFpxGet(arg1, groundElemLen, pA1, pGroundGFE);
151    #endif
152 
153    mulF(u0, pA0, pA1, pGroundGFE); /* u0 = a[0]*a[1] */
154    sqrF(t0, pA0, pGroundGFE);      /* t0 = a[0]*a[0] */
155    sqrF(t1, pA1, pGroundGFE);      /* t1 = a[1]*a[1] */
156    cpGFpxMul_G0(t1, t1, pGFEx);
157    subF(pR0, t0, t1, pGroundGFE);
158    addF(pR1, u0, u0, pGroundGFE);  /* r[1] = 2*a[0]*a[1] */
159 
160    #if defined GS_DBG
161    cpGFpReleasePool(2, pGroundGFE);
162    #endif
163 
164    cpGFpReleasePool(3, pGroundGFE);
165    return pR;
166 }
167 
168 /*
169 // return specific polynomi alarith methods
170 // polynomial - deg 2 binomial
171 */
gsPolyArith_binom2(void)172 static gsModMethod* gsPolyArith_binom2(void)
173 {
174    static gsModMethod m = {
175       cpGFpxEncode_com,
176       cpGFpxDecode_com,
177       cpGFpxMul_p2_binom,
178       cpGFpxSqr_p2_binom,
179       NULL,
180       cpGFpxAdd_com,
181       cpGFpxSub_com,
182       cpGFpxNeg_com,
183       cpGFpxDiv2_com,
184       cpGFpxMul2_com,
185       cpGFpxMul3_com,
186       //cpGFpxInv
187    };
188    return &m;
189 }
190 
191 /*F*
192 // Name: ippsGFpxMethod_binom2
193 //
194 // Purpose: Returns a reference to the implementation of arithmetic operations over GF(pd).
195 //
196 // Returns:          pointer to a structure containing
197 //                   an implementation of arithmetic operations over GF(pd)
198 //                   g(x) = x^2 - a0, a0 from GF(p)
199 //
200 //
201 *F*/
202 
203 IPPFUN( const IppsGFpMethod*, ippsGFpxMethod_binom2, (void) )
204 {
205    static IppsGFpMethod method = {
206       cpID_Binom,
207       2,
208       NULL,
209       NULL
210    };
211    method.arith = gsPolyArith_binom2();
212    return &method;
213 }
214