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 // Intel(R) Integrated Performance Primitives. Cryptography Primitives.
43 // Internal operations over GF(p) extension.
44 //
45 // Context:
46 // cpGFpxMultiExp()
47 //
48 */
49
50 #include "owncp.h"
51 #include "pcpbnumisc.h"
52 #include "pcpgfpxstuff.h"
53 #include "gsscramble.h"
54
55 //tbcd: temporary excluded: #include <assert.h>
56
GetIndex(const BNU_CHUNK_T * ppE[],int nItems,int nBit)57 static int GetIndex(const BNU_CHUNK_T* ppE[], int nItems, int nBit)
58 {
59 int shift = nBit%BYTESIZE;
60 int offset= nBit/BYTESIZE;
61 int index = 0;
62
63 int n;
64 for(n=nItems; n>0; n--) {
65 const Ipp8u* pE = ((Ipp8u*)ppE[n-1]) + offset;
66 Ipp8u e = pE[0];
67 index <<= 1;
68 index += (e>>shift) &1;
69 }
70 return index;
71 }
72
73
cpPrecomputeMultiExp(BNU_CHUNK_T * pTable,const BNU_CHUNK_T * ppA[],int nItems,gsModEngine * pGFEx)74 static void cpPrecomputeMultiExp(BNU_CHUNK_T* pTable, const BNU_CHUNK_T* ppA[], int nItems, gsModEngine* pGFEx)
75 {
76 gsModEngine* pBasicGFE = cpGFpBasic(pGFEx);
77
78 //int nPrecomputed = 1<<nItems;
79
80 /* length of element (BNU_CHUNK_T) */
81 int elmLen = GFP_FELEN(pGFEx);
82
83 /* get resource */
84 BNU_CHUNK_T* pT = cpGFpGetPool(1, pGFEx);
85 //tbcd: temporary excluded: assert(NULL!=pT);
86
87 /* pTable[0] = 1 */
88 cpGFpElementCopyPadd(pT, elmLen, GFP_MNT_R(pBasicGFE), GFP_FELEN(pBasicGFE));
89 //cpScramblePut(pTable+0, nPrecomputed, (Ipp8u*)pT, elmDataSize);
90 gsScramblePut(pTable, 0, pT, elmLen, nItems);
91 /* pTable[1] = A[0] */
92 //cpScramblePut(pTable+1, nPrecomputed, (Ipp8u*)(ppA[0]), elmDataSize);
93 gsScramblePut(pTable, 1, ppA[0], elmLen, nItems);
94
95 {
96 mod_mul mulF = GFP_METHOD(pGFEx)->mul; /* mul method */
97
98 int i, baseIdx;
99 for(i=1, baseIdx=2; i<nItems; i++, baseIdx*=2) {
100 /* pTable[baseIdx] = A[i] */
101 //cpScramblePut(pTable+baseIdx, nPrecomputed, (Ipp8u*)(ppA[i]), elmDataSize);
102 gsScramblePut(pTable, baseIdx, ppA[i], elmLen, nItems);
103
104 {
105 int nPasses = 1;
106 int step = baseIdx/2;
107
108 int k;
109 for(k=i-1; k>=0; k--) {
110 int tblIdx = baseIdx;
111
112 int n;
113 for(n=0; n<nPasses; n++, tblIdx+=2*step) {
114 /* use pre-computed value */
115 //cpScrambleGet((Ipp8u*)pT, elmDataSize, pTable+tblIdx, nPrecomputed);
116 gsScrambleGet(pT, elmLen, pTable, tblIdx, nItems);
117 mulF(pT, pT, ppA[k], pGFEx);
118 //cpScramblePut(pTable+tblIdx+step, nPrecomputed, (Ipp8u*)pT, elmDataSize);
119 gsScramblePut(pTable, tblIdx+step, pT, elmLen, nItems);
120 }
121
122 nPasses *= 2;
123 step /= 2;
124 }
125 }
126 }
127 }
128
129 /* release resourse */
130 cpGFpReleasePool(1, pGFEx);
131 }
132
133
cpGetMaxBitsizeExponent(const BNU_CHUNK_T * ppE[],int nsE[],int nItems)134 static int cpGetMaxBitsizeExponent(const BNU_CHUNK_T* ppE[], int nsE[], int nItems)
135 {
136 int n;
137 /* find out the longest exponent */
138 int expBitSize = BITSIZE_BNU(ppE[0], nsE[0]);
139 for(n=1; n<nItems; n++) {
140 expBitSize = IPP_MAX(expBitSize, BITSIZE_BNU(ppE[n], nsE[n]));
141 }
142 return expBitSize;
143 }
144
145 /* sscm version */
cpGFpxMultiExp(BNU_CHUNK_T * pR,const BNU_CHUNK_T * ppA[],const BNU_CHUNK_T * ppE[],int nsE[],int nItems,gsModEngine * pGFEx,Ipp8u * pScratchBuffer)146 BNU_CHUNK_T* cpGFpxMultiExp(BNU_CHUNK_T* pR, const BNU_CHUNK_T* ppA[], const BNU_CHUNK_T* ppE[], int nsE[], int nItems,
147 gsModEngine* pGFEx, Ipp8u* pScratchBuffer)
148 {
149 /* align scratch buffer */
150 BNU_CHUNK_T* pTable = (BNU_CHUNK_T*)( IPP_ALIGNED_PTR(pScratchBuffer, CACHE_LINE_SIZE) );
151 /* pre-compute table */
152 cpPrecomputeMultiExp(pTable, ppA, nItems, pGFEx);
153
154 {
155 mod_mul mulF = GFP_METHOD(pGFEx)->mul; /* mul and sqr methods and parameter */
156 mod_sqr sqrF = GFP_METHOD(pGFEx)->sqr;
157 int elmLen = GFP_FELEN(pGFEx);
158
159 /* find out the longest exponent */
160 int expBitSize = cpGetMaxBitsizeExponent(ppE, nsE, nItems);
161
162 /* allocate resource and copy expanded exponents into */
163 const BNU_CHUNK_T* ppExponent[IPP_MAX_EXPONENT_NUM];
164 {
165 int n;
166 for(n=0; n<nItems; n++) {
167 BNU_CHUNK_T* pData = cpGFpGetPool(1, pGFEx);
168 //tbcd: temporary excluded: assert(NULL!=pData);
169 cpGFpElementCopyPadd(pData, elmLen, ppE[n], nsE[n]);
170 ppExponent[n] = pData;
171 }
172 }
173
174 /* multiexponentiation */
175 {
176 /* get temporary */
177 BNU_CHUNK_T* pT = cpGFpGetPool(1, pGFEx);
178
179 /* init result */
180 int tblIdx = GetIndex(ppExponent, nItems, --expBitSize);
181 //cpScrambleGet((Ipp8u*)pR, elmDataSize, pScratchBuffer+tblIdx, nPrecomputed);
182 gsScrambleGet_sscm(pR, elmLen, pTable, tblIdx, nItems);
183
184 //tbcd: temporary excluded: assert(NULL!=pT);
185
186 /* compute the rest: square and multiply */
187 for(--expBitSize; expBitSize>=0; expBitSize--) {
188 sqrF(pR, pR, pGFEx);
189 tblIdx = GetIndex(ppExponent, nItems, expBitSize);
190 //cpScrambleGet((Ipp8u*)pT, elmDataSize, pScratchBuffer+tblIdx, nPrecomputed);
191 gsScrambleGet_sscm(pT, elmLen, pTable, tblIdx, nItems);
192 mulF(pR, pR, pT, pGFEx);
193 }
194
195 /* release resourse */
196 cpGFpReleasePool(1, pGFEx);
197 }
198
199 /* release resourse */
200 cpGFpReleasePool(nItems, pGFEx);
201
202 return pR;
203 }
204 }
205