📄 bch_hm.c
字号:
/* square root of polynomial */
/* */
/* Global variables: */
/* wSqrtTable - Acceleration table for square root */
/* */
/* ------------------------------------------------------------------------- */
static FLSNative ECC_SQRT( FLSNative nsA )
{
FLSNative nsi = 0 ;
FLSNative nsS = 0 ;
/* Calculate for each bit */
while ( nsi < 14 )
{
if ( 0x2000 & ( nsA << nsi ) ) nsS ^= wSqrtTable[nsi] ;
nsi++ ;
}
return nsS ;
}
/* ------------------------------------------------------------------------- */
/* */
/* */
/* function: FLSNative ECC_Affine4Convert(FLSNative *nsS) */
/* */
/* description: */
/* */
/* This function get polinome of 4 degree and transform it in */
/* affine polinome of 4 degree. The transformation is: Y=1/(X+T) */
/* */
/* Input: */
/* nsS - Finite field polynomial coeffcients */
/* Polinome coefficients: S[3]-X^4,S[2]-X^3,S[1]-X^2,S[0]-X */
/* (the constant is allways 1 is not transfered) */
/* */
/* Output: */
/* */
/* nsS - Polinome coefficients: S[3]-X^4,S[2]-X^2,S[1]-X^1, */
/* S[0]-Constant */
/* */
/* returned value: */
/* */
/* nsT - transform constant */
/* */
/* Warning: S[2] can't be Zero */
/* ------------------------------------------------------------------------- */
static FLSNative ECC_Affine4Convert(FLSNative *nsS)
{
/* Calculte T^2 */
FLSNative nsT2 = ECC_BCH_Mul(ECC_BCH_Inv(nsS[2]),nsS[0]) ;
FLSNative nsT = ECC_SQRT(nsT2) ; /* Calculte T=root of T2 */
FLSNative nsT3 = ECC_BCH_Mul(nsT2,nsT) ; /* Calculte T^3 */
FLSNative nsT4 = ECC_BCH_Mul(nsT2,nsT2) ; /* Calculte T^4 */
/* Coefficient of Y^2 */
FLSNative nsCNew = PAdd(ECC_BCH_Mul(nsS[2],nsT),nsS[1]) ;
FLSNative nsDNew = ECC_BCH_Mul(nsS[3],nsT4) ;
nsDNew=PAdd(ECC_BCH_Mul(nsS[2],nsT3),nsDNew);
nsDNew=PAdd(ECC_BCH_Mul(nsS[1],nsT2),nsDNew);
nsDNew=PAdd(ECC_BCH_Mul(nsS[0],nsT),nsDNew);
nsDNew=PAdd(1<<13,nsDNew); /* Coefficient of Y^4 */
nsS[0]=nsS[3]; /* Constant */
nsS[1]=nsS[2]; /* Coefficient of Y */
nsS[2]=nsCNew;
nsS[3]=nsDNew;
return nsT;
}
/* ------------------------------------------------------------------------- */
/* */
/* */
/* function: FLSNative ECC_Affine3Convert(FLSNative *nsS) */
/* */
/* description: */
/* */
/* This function get polinome of 3 degree and transform it in */
/* linear polinome of 4 degree. The transformation is: Y=(X+T) */
/* and H(Y)->H(Y)*Y */
/* */
/* Input: */
/* */
/* nsS - Polinome coefficients: S[2]-X^3,S[1]-X^2,S[0]-X */
/* (the constant is allways 1 is not transfered) */
/* */
/* Output: */
/* */
/* nsS - Polinome coefficients: S[3]-X^4,S[2]-X^2,S[1]-X^1,S[0]=0 */
/* T-transform constant */
/* */
/* returned value: */
/* */
/* nsT - transform constant */
/* */
/* Warning: S[2] can't be Zero */
/* ------------------------------------------------------------------------- */
static FLSNative ECC_Affine3Convert(FLSNative *nsS)
{
/* Calculate T */
FLSNative nsT = ECC_BCH_Mul(ECC_BCH_Inv(nsS[2]),nsS[1]);
nsS[3]=nsS[2]; /* After mutiply by Y^4=Y*Y^3 */
/* Coefficient of Y^2 */
nsS[2]=PAdd(ECC_BCH_Mul(nsS[1],nsT),nsS[0]);
nsS[1]=PAdd(ECC_BCH_Mul(nsS[0],nsT),1<<13); /* Coefficient of Y */
nsS[0]=0; /* Constant is allways Zero */
return nsT;
}
/* ------------------------------------------------------------------------- */
/* */
/* */
/* function: void ECC_TransposeMatrix(FLSNative* nsM, FLSNative nsConst) */
/* */
/* description: */
/* */
/* This function make transpose of matrix of size 14x14 + constant */
/* The constant set at bit 15 of register */
/* */
/* Input: */
/* nsM - Matrix, (size 14 FLSNative), Constant (size FLSNative) */
/* */
/* Output: */
/* */
/* nsM - Matrix, (size 14 FLSNative) */
/* */
/* ------------------------------------------------------------------------- */
static void ECC_TransposeMatrix(FLSNative* nsM, FLSNative nsConst)
{
FLSNative nsi = 0 ;
FLSNative nsTranspose[14],nsmask=0x2000, nsNewLine ;
while ( nsi < 14 )
{
nsNewLine = 0 ;
/* Move on the column of matrix and transfer to row */
if ( nsM[0] & nsmask ) nsNewLine |= 0x2000;
if ( nsM[1] & nsmask ) nsNewLine |= 0x1000;
if ( nsM[2] & nsmask ) nsNewLine |= 0x0800;
if ( nsM[3] & nsmask ) nsNewLine |= 0x0400;
if ( nsM[4] & nsmask ) nsNewLine |= 0x0200;
if ( nsM[5] & nsmask ) nsNewLine |= 0x0100;
if ( nsM[6] & nsmask ) nsNewLine |= 0x0080;
if ( nsM[7] & nsmask ) nsNewLine |= 0x0040;
if ( nsM[8] & nsmask ) nsNewLine |= 0x0020;
if ( nsM[9] & nsmask ) nsNewLine |= 0x0010;
if ( nsM[10] & nsmask ) nsNewLine |= 0x0008;
if ( nsM[11] & nsmask ) nsNewLine |= 0x0004;
if ( nsM[12] & nsmask ) nsNewLine |= 0x0002;
if ( nsM[13] & nsmask ) nsNewLine |= 0x0001;
/* Set the constant bit at bit 15 */
if ( nsConst & nsmask ) nsNewLine |= 0x8000;
nsTranspose[nsi] = nsNewLine ; /* Save row in Temporal matrix */
nsmask >>= 1 ; /* move to next bit */
nsi++ ;
}
for (nsi=0; nsi<14; nsi++) /* move Temporal matrix in Matrix */
nsM[nsi]=nsTranspose[nsi];
}
/* ------------------------------------------------------------------------- */
/* */
/* function: FLSNative ECC_DropZeros(FLSNative* nsM) */
/* */
/* description: */
/* */
/* This function remove zero lines from matrix and calculate number of lines */
/* */
/* Input: */
/* nsM - Matrix, (size 14 FLSNative) */
/* */
/* Output: */
/* */
/* nsM - Matrix, (size 14 FLSNative) */
/* */
/* Returned value: */
/* */
/* nsj - Number of relevant lines in matrix */
/* ------------------------------------------------------------------------- */
static FLSNative ECC_DropZeros(FLSNative* nsM)
{
FLSNative nsi = 0 ;
FLSNative nsj = 0 ;
while ( nsi < 14 ) /* move on all lines */
{
if( nsM[nsi] != 0 ) /* if line not equal to Zero */
{
nsM[nsj] = nsM[nsi] ;
nsj++; /* increase relevant line counter */
}
nsi++ ;
}
return nsj;
}
/* ------------------------------------------------------------------------- */
/* */
/* function: FLSNative AnsCalc (FLSNative nsA) */
/* */
/* description: */
/* This function calculate XOR of register nsA */
/* */
/* Input: */
/* nsA - Finite field polynomial */
/* */
/* Returned value: */
/* */
/* Parity bit, 0 if even, 1 if odd */
/* */
/* ------------------------------------------------------------------------- */
static FLSNative ECC_AnsCalc (FLSNative nsA)
{
nsA = PAdd(nsA,nsA>>8);
nsA = PAdd(nsA,nsA>>4);
nsA = PAdd(nsA,nsA>>2);
nsA = PAdd(nsA,nsA>>1);
return nsA & 1 ;
}
/* ------------------------------------------------------------------------- */
/* */
/* function: */
/* */
/* FLSNative FindRow(FLSNative* nsK, FLSNative nsStart, FLSNative bit) */
/* */
/* description: */
/* */
/* This function move on matrix lines from "Start" Line and find line in */
/* relevant place then make swap between found line and start line. */
/* */
/* Input: */
/* nsK - Matrix (size 14 int) */
/* nsStart - pointer to start line */
/* nsbit - bit mask */
/* */
/* Returned value: */
/* */
/* FLSNative, 1 - if line found, 0 - if line is not found */
/* */
/* ------------------------------------------------------------------------- */
static FLSNative ECC_FindRow(FLSNative* nsK, FLSNative nsStart, FLSNative bit)
{
FLSNative nsi = nsStart + 1 ;
FLSNative nsKStart = nsK[nsStart] ;
if(nsKStart & bit) /* if Start line is at place swap is not requred */
return 1;
while ( nsi < 14 ) /* Move untill end of the matrix */
{
if(nsK[nsi]&bit) /* if line found make swap */
{
nsK[nsStart] = nsK[nsi];
nsK[nsi]=nsKStart ;
return 1;
}
nsi++ ;
}
return 0; /* if line not found return zero */
}
/* ------------------------------------------------------------------------- */
/* */
/* function: */
/* */
/* FLSNative FindRoots(FLSNative* nsS,FLSNative nsNum) */
/* */
/* description: */
/* */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -