1 /*******************************************************************************
2 * Copyright 2010-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 //
43 // Purpose:
44 // Intel(R) Integrated Performance Primitives. Cryptography Primitives.
45 // Internal EC over GF(p^m) basic Definitions & Function Prototypes
46 //
47 // Context:
48 // gfec_point_add()
49 //
50 */
51
52 #include "owndefs.h"
53 #include "owncp.h"
54 #include "pcpgfpecstuff.h"
55 #include "pcpmask_ct.h"
56
57
58 #if ( ECP_PROJECTIVE_COORD == JACOBIAN )
59 /*
60 // S1 = y1*z2^3
61 // S2 = y2*z1^3
62 //
63 // U1 = x1*z2^2
64 // U2 = x2*z1^2
65
66 // R = S2-S1
67 // H = U2-U1
68 //
69 // x3 = -H^3 -2*U1*H^2 +R2
70 // y3 = -S1*H^3 +R*(U1*H^2 -x3)
71 // z3 = z1*z2*H
72 //
73 // complexity = 4s+12m
74 */
75
gfec_point_add(BNU_CHUNK_T * pRdata,const BNU_CHUNK_T * pPdata,const BNU_CHUNK_T * pQdata,IppsGFpECState * pEC)76 void gfec_point_add(BNU_CHUNK_T* pRdata, const BNU_CHUNK_T* pPdata, const BNU_CHUNK_T* pQdata, IppsGFpECState* pEC)
77 {
78 IppsGFpState* pGF = ECP_GFP(pEC);
79 gsModEngine* pGFE = GFP_PMA(pGF);
80 int elemLen = GFP_FELEN(pGFE);
81
82 mod_sub sub = GFP_METHOD(pGFE)->sub; /* gf sub */
83 mod_mul2 mul2= GFP_METHOD(pGFE)->mul2; /* gf mul2 */
84 mod_mul mul = GFP_METHOD(pGFE)->mul; /* gf mul */
85 mod_sqr sqr = GFP_METHOD(pGFE)->sqr; /* gf sqr */
86
87 /* coordinates of P */
88 const BNU_CHUNK_T* px1 = pPdata;
89 const BNU_CHUNK_T* py1 = pPdata+elemLen;
90 const BNU_CHUNK_T* pz1 = pPdata+2*elemLen;
91
92 /* coordinates of Q */
93 const BNU_CHUNK_T* px2 = pQdata;
94 const BNU_CHUNK_T* py2 = pQdata+elemLen;
95 const BNU_CHUNK_T* pz2 = pQdata+2*elemLen;
96
97 BNU_CHUNK_T inftyP = GFPE_IS_ZERO_CT(pz1, elemLen);
98 BNU_CHUNK_T inftyQ = GFPE_IS_ZERO_CT(pz2, elemLen);
99
100 /* get temporary from top of EC point pool */
101 BNU_CHUNK_T* U1 = pEC->pPool;
102 BNU_CHUNK_T* U2 = U1 + elemLen;
103 BNU_CHUNK_T* S1 = U2 + elemLen;
104 BNU_CHUNK_T* S2 = S1 + elemLen;
105 BNU_CHUNK_T* H = S2 + elemLen;
106 BNU_CHUNK_T* R = H + elemLen;
107
108 BNU_CHUNK_T* pRx = R + elemLen; /* temporary result */
109 BNU_CHUNK_T* pRy = pRx+ elemLen;
110 BNU_CHUNK_T* pRz = pRy+ elemLen;
111
112 mul(S1, py1, pz2, pGFE); // S1 = Y1*Z2
113 sqr(U1, pz2, pGFE); // U1 = Z2^2
114
115 mul(S2, py2, pz1, pGFE); // S2 = Y2*Z1
116 sqr(U2, pz1, pGFE); // U2 = Z1^2
117
118 mul(S1, S1, U1, pGFE); // S1 = Y1*Z2^3
119 mul(S2, S2, U2, pGFE); // S2 = Y2*Z1^3
120
121 mul(U1, px1, U1, pGFE); // U1 = X1*Z2^2
122 mul(U2, px2, U2, pGFE); // U2 = X2*Z1^2
123
124 sub(R, S2, S1, pGFE); // R = S2-S1
125 sub(H, U2, U1, pGFE); // H = U2-U1
126
127 {
128 BNU_CHUNK_T mask_zeroH = GFPE_IS_ZERO_CT(H, elemLen);
129 BNU_CHUNK_T mask = mask_zeroH & ~inftyP & ~inftyQ;
130 if(mask) {
131 if( GFPE_IS_ZERO_CT(R, elemLen) )
132 gfec_point_double(pRdata, pPdata, pEC);
133 else
134 cpGFpElementPadd(pRdata, 3*elemLen, 0);
135 return;
136 }
137 }
138
139 mul(pRz, pz1, pz2, pGFE); // Z3 = Z1*Z2
140 sqr(U2, H, pGFE); // U2 = H^2
141 mul(pRz, pRz, H, pGFE); // Z3 = (Z1*Z2)*H
142 sqr(S2, R, pGFE); // S2 = R^2
143 mul(H, H, U2, pGFE); // H = H^3
144
145 mul(U1, U1, U2, pGFE); // U1 = U1*H^2
146 sub(pRx, S2, H, pGFE); // X3 = R^2 - H^3
147 mul2(U2, U1, pGFE); // U2 = 2*U1*H^2
148 mul(S1, S1, H, pGFE); // S1 = S1*H^3
149 sub(pRx, pRx, U2, pGFE); // X3 = (R^2 - H^3) -2*U1*H^2
150
151 sub(pRy, U1, pRx, pGFE); // Y3 = R*(U1*H^2 - X3) -S1*H^3
152 mul(pRy, pRy, R, pGFE);
153 sub(pRy, pRy, S1, pGFE);
154
155 cpMaskedReplace_ct(pRx, px2, elemLen*3, inftyP);
156 cpMaskedReplace_ct(pRx, px1, elemLen*3, inftyQ);
157
158 cpGFpElementCopy(pRdata, pRx, 3*elemLen);
159 }
160 #endif
161
162