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 43 // Cryptographic Primitives (ippCP) 44 // GF(p) extension internal 45 // 46 */ 47 48 #if !defined(_PCP_GFPEXT_H_) 49 #define _PCP_GFPEXT_H_ 50 51 #include "pcpgfpstuff.h" 52 53 54 /* GF(p^d) pool */ 55 #define GFPX_PESIZE(pGF) GFP_FELEN((pGF)) 56 #define GFPX_POOL_SIZE (14) //(8) /* Number of temporary variables in pool */ 57 58 /* address of ground field element inside expanded field element */ 59 #define GFPX_IDX_ELEMENT(pxe, idx, eleSize) ((pxe)+(eleSize)*(idx)) 60 61 62 __INLINE int degree(const BNU_CHUNK_T* pE, const gsModEngine* pGFEx) 63 { 64 int groundElemLen = GFP_FELEN(GFP_PARENT(pGFEx)); 65 int deg; 66 for(deg=GFP_EXTDEGREE(pGFEx)-1; deg>=0; deg-- ) { 67 if(!GFP_IS_ZERO(pE+groundElemLen*deg, groundElemLen)) break; 68 } 69 return deg; 70 } 71 72 __INLINE gsModEngine* cpGFpBasic(const gsModEngine* pGFEx) 73 { 74 while( !GFP_IS_BASIC(pGFEx) ) { 75 pGFEx = GFP_PARENT(pGFEx); 76 } 77 return (gsModEngine*)pGFEx; 78 } 79 __INLINE int cpGFpBasicDegreeExtension(const gsModEngine* pGFEx) 80 { 81 int degree = GFP_EXTDEGREE(pGFEx); 82 while( !GFP_IS_BASIC(pGFEx) ) { 83 pGFEx = GFP_PARENT(pGFEx); 84 degree *= GFP_EXTDEGREE(pGFEx); 85 } 86 return degree; 87 } 88 89 /* convert external data (Ipp32u) => internal element (BNU_CHUNK_T) representation 90 returns length of element (in BNU_CHUNK_T) 91 */ 92 __INLINE int cpGFpxCopyToChunk(BNU_CHUNK_T* pElm, const Ipp32u* pA, int nsA, const gsModEngine* pGFEx) 93 { 94 gsModEngine* pBasicGFE = cpGFpBasic(pGFEx); 95 int basicExtension = cpGFpBasicDegreeExtension(pGFEx); 96 int basicElmLen32 = GFP_FELEN32(pBasicGFE); 97 int basicElmLen = GFP_FELEN(pBasicGFE); 98 int deg; 99 for(deg=0; deg<basicExtension && nsA>0; deg++, nsA -= basicElmLen32) { 100 int srcLen = IPP_MIN(nsA, basicElmLen32); 101 ZEXPAND_COPY_BNU((Ipp32u*)pElm, basicElmLen*(int)(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u)), pA,srcLen); 102 pElm += basicElmLen; 103 pA += basicElmLen32; 104 } 105 return basicElmLen*deg; 106 } 107 108 /* convert internal element (BNU_CHUNK_T) => external data (Ipp32u) representation 109 returns length of data (in Ipp32u) 110 */ 111 __INLINE int cpGFpxCopyFromChunk(Ipp32u* pA, const BNU_CHUNK_T* pElm, const gsModEngine* pGFEx) 112 { 113 gsModEngine* pBasicGFE = cpGFpBasic(pGFEx); 114 int basicExtension = cpGFpBasicDegreeExtension(pGFEx); 115 int basicElmLen32 = GFP_FELEN32(pBasicGFE); 116 int basicElmLen = GFP_FELEN(pBasicGFE); 117 int deg; 118 for(deg=0; deg<basicExtension; deg++) { 119 COPY_BNU(pA, (Ipp32u*)pElm, basicElmLen32); 120 pA += basicElmLen32; 121 pElm += basicElmLen; 122 } 123 return basicElmLen32*deg; 124 } 125 126 127 #define cpGFpxRand OWNAPI(cpGFpxRand) 128 BNU_CHUNK_T* cpGFpxRand(BNU_CHUNK_T* pR, gsModEngine* pGFEx, IppBitSupplier rndFunc, void* pRndParam); 129 130 #define cpGFpxSet OWNAPI(cpGFpxSet) 131 BNU_CHUNK_T* cpGFpxSet (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pDataA, int nsA, gsModEngine* pGFEx); 132 133 #define cpGFpxGet OWNAPI(cpGFpxGet) 134 BNU_CHUNK_T* cpGFpxGet (BNU_CHUNK_T* pDataA, int nsA, const BNU_CHUNK_T* pR, gsModEngine* pGFEx); 135 136 #define cpGFpxSetPolyTerm OWNAPI(cpGFpxSetPolyTerm) 137 BNU_CHUNK_T* cpGFpxSetPolyTerm (BNU_CHUNK_T* pR, int deg, const BNU_CHUNK_T* pDataA, int nsA, gsModEngine* pGFEx); 138 139 #define cpGFpxGetPolyTerm OWNAPI(cpGFpxGetPolyTerm) 140 BNU_CHUNK_T* cpGFpxGetPolyTerm (BNU_CHUNK_T* pDataA, int nsA, const BNU_CHUNK_T* pR, int deg, gsModEngine* pGFEx); 141 142 #define cpGFpxAdd OWNAPI(cpGFpxAdd) 143 BNU_CHUNK_T* cpGFpxAdd (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsModEngine* pGFEx); 144 145 #define cpGFpxSub OWNAPI(cpGFpxSub) 146 BNU_CHUNK_T* cpGFpxSub (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsModEngine* pGFEx); 147 148 #define cpGFpxMul OWNAPI(cpGFpxMul) 149 BNU_CHUNK_T* cpGFpxMul (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsModEngine* pGFEx); 150 151 #define cpGFpxSqr OWNAPI(cpGFpxSqr) 152 BNU_CHUNK_T* cpGFpxSqr (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsModEngine* pGFEx); 153 154 #define cpGFpxAdd_GFE OWNAPI(cpGFpxAdd_GFE) 155 BNU_CHUNK_T* cpGFpxAdd_GFE (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pGroundB, gsModEngine* pGFEx); 156 157 #define cpGFpxSub_GFE OWNAPI(cpGFpxSub_GFE) 158 BNU_CHUNK_T* cpGFpxSub_GFE (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pGroundB, gsModEngine* pGFEx); 159 160 #define cpGFpxMul_GFE OWNAPI(cpGFpxMul_GFE) 161 BNU_CHUNK_T* cpGFpxMul_GFE (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pGroundB, gsModEngine* pGFEx); 162 163 #define cpGFpGetOptimalWinSize OWNAPI(cpGFpGetOptimalWinSize) 164 int cpGFpGetOptimalWinSize(int bitsize); 165 166 #define cpGFpxExp OWNAPI(cpGFpxExp) 167 BNU_CHUNK_T* cpGFpxExp (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pE, int nsE, gsModEngine* pGFEx, Ipp8u* pScratchBuffer); 168 169 #define cpGFpxMultiExp OWNAPI(cpGFpxMultiExp) 170 BNU_CHUNK_T* cpGFpxMultiExp(BNU_CHUNK_T* pR, const BNU_CHUNK_T* ppA[], const BNU_CHUNK_T* ppE[], int nsE[], int nItems, 171 gsModEngine* pGFEx, Ipp8u* pScratchBuffer); 172 173 #define cpGFpxConj OWNAPI(cpGFpxConj) 174 BNU_CHUNK_T* cpGFpxConj(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsModEngine* pGFEx); 175 176 #define cpGFpxNeg OWNAPI(cpGFpxNeg) 177 BNU_CHUNK_T* cpGFpxNeg (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsModEngine* pGFEx); 178 179 #define cpGFpxInv OWNAPI(cpGFpxInv) 180 BNU_CHUNK_T* cpGFpxInv (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsModEngine* pGFEx); 181 182 #define cpGFpxHalve OWNAPI(cpGFpxHalve) 183 BNU_CHUNK_T* cpGFpxHalve (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsModEngine* pGFEx); 184 185 #define InitGFpxCtx OWNAPI(InitGFpxCtx) 186 void InitGFpxCtx(const IppsGFpState* pGroundGF, int extDeg, const IppsGFpMethod* method, IppsGFpState* pGFpx); 187 188 #endif /* _PCP_GFPEXT_H_ */ 189