libraries/WM_Libraries/Platform/Common/crypto/wm_crypto_hard.c
int rsaMontExptMod(pstm_int *a, pstm_int *e, pstm_int *n, pstm_int *res)
{
	int i = 0;
	u32 k = 0, mc = 0;
	u8 monmulFlag = 0;
	pstm_int R, X, Y;
	pstm_init(NULL, &X);
	pstm_init(NULL, &Y);
	pstm_init(NULL, &R);
	k = n->used * DIGIT_BIT;//pstm_count_bits(n);
	pstm_set(&Y, k);
	pstm_set(&X, 2);
	pstm_exptmod(NULL, &X, &Y, n, &R); //R = 2^k % n
	//pstm_set(&Y, 1);
	pstm_mulmod(NULL, a, &R, n, &X); //X = A * R
	pstm_copy(&R, &Y);
	
	rsaCalMc(&mc, n->dp[0]);
	rsaMonMulSetLen(n->used);
	rsaMonMulWriteMc(mc);
	rsaMulModWrite('M', n);
	rsaMulModWrite('B', &X);
	rsaMulModWrite('A', &Y);
	k = pstm_count_bits(e);
	for(i=k-1;i>=0;i--){
		//montMulMod(&Y, &Y, n, &Y);
		//if(pstm_get_bit(e, i))
		//	montMulMod(&Y, &X, n, &Y);
            if(monmulFlag == 0)
            {
                rsaMonMulAA();
                monmulFlag = 1;
            }
            else
            {
                rsaMonMulDD();
                monmulFlag = 0;
            }

            if(pstm_get_bit(e, i))
            {
                if(monmulFlag == 0)
                {
                    rsaMonMulAB();
                    monmulFlag = 1;
                }
                else
                {
                    rsaMonMulBD();
                    monmulFlag = 0;
                }
            }
	}
	pstm_set(&R, 1);
	rsaMulModWrite('B', &R);
	//montMulMod(&Y, &R, n, res);
	if(monmulFlag == 0)
	{
	    rsaMonMulAB();
	    rsaMulModRead('D', res);
	}
	else
	{
	    rsaMonMulBD();
	    rsaMulModRead('A', res);
	}
	pstm_clamp(res);
	pstm_clear(&X);
	pstm_clear(&Y);
	pstm_clear(&R);
	return 0;
}
/*     res = a^e % n      DIGIT_BIT must be 32, or you need to re calc the RSAN value and the mc value  */
res = a^e % n      DIGIT_BIT must be 32, or you need to re calc the RSAN value and the mc value
res = a^e % n DIGIT_BIT must be 32 or you need to re calc the RSAN value and the mc value
int rsaMontExptMod(pstm_int *a, pstm_int *e, pstm_int *n, pstm_int *res)
{
	int i = 0;
	u32 k = 0, mc = 0;
	u8 monmulFlag = 0;
	pstm_int R, X, Y;
	pstm_init(NULL, &X);
	pstm_init(NULL, &Y);
	pstm_init(NULL, &R);
	k = n->used * DIGIT_BIT;
	pstm_set(&Y, k);
	pstm_set(&X, 2);
	pstm_exptmod(NULL, &X, &Y, n, &R); 
	pstm_mulmod(NULL, a, &R, n, &X); 
	pstm_copy(&R, &Y);
	rsaCalMc(&mc, n->dp[0]);
	rsaMonMulSetLen(n->used);
	rsaMonMulWriteMc(mc);
	rsaMulModWrite('M', n);
	rsaMulModWrite('B', &X);
	rsaMulModWrite('A', &Y);
	k = pstm_count_bits(e);
	for(i=k-1;i>=0;i--){
            if(monmulFlag == 0)
            {
                rsaMonMulAA();
                monmulFlag = 1;
            }
            else
            {
                rsaMonMulDD();
                monmulFlag = 0;
            }
            if(pstm_get_bit(e, i))
            {
                if(monmulFlag == 0)
                {
                    rsaMonMulAB();
                    monmulFlag = 1;
                }
                else
                {
                    rsaMonMulBD();
                    monmulFlag = 0;
                }
            }
	}
	pstm_set(&R, 1);
	rsaMulModWrite('B', &R);
	if(monmulFlag == 0)
	{
	    rsaMonMulAB();
	    rsaMulModRead('D', res);
	}
	else
	{
	    rsaMonMulBD();
	    rsaMulModRead('A', res);
	}
	pstm_clamp(res);
	pstm_clear(&X);
	pstm_clear(&Y);
	pstm_clear(&R);
	return 0;
}
int rsaMontExptMod ( pstm_int * a , pstm_int * e , pstm_int * n , pstm_int * res ) { int i = 0 ; u32 k = 0 , mc = 0 ; u8 monmulFlag = 0 ; pstm_int R , X , Y ; pstm_init ( NULL , & X ) ; pstm_init ( NULL , & Y ) ; pstm_init ( NULL , & R ) ; k = n -> used * DIGIT_BIT ; pstm_set ( & Y , k ) ; pstm_set ( & X , 2 ) ; pstm_exptmod ( NULL , & X , & Y , n , & R ) ; pstm_mulmod ( NULL , a , & R , n , & X ) ; pstm_copy ( & R , & Y ) ; rsaCalMc ( & mc , n -> dp [ 0 ] ) ; rsaMonMulSetLen ( n -> used ) ; rsaMonMulWriteMc ( mc ) ; rsaMulModWrite ( ' ' , n ) ; rsaMulModWrite ( ' ' , & X ) ; rsaMulModWrite ( ' ' , & Y ) ; k = pstm_count_bits ( e ) ; for ( i = k - 1 ; i >= 0 ; i -- ) { if ( monmulFlag == 0 ) { rsaMonMulAA ( ) ; monmulFlag = 1 ; } else { rsaMonMulDD ( ) ; monmulFlag = 0 ; } if ( pstm_get_bit ( e , i ) ) { if ( monmulFlag == 0 ) { rsaMonMulAB ( ) ; monmulFlag = 1 ; } else { rsaMonMulBD ( ) ; monmulFlag = 0 ; } } } pstm_set ( & R , 1 ) ; rsaMulModWrite ( ' ' , & R ) ; if ( monmulFlag == 0 ) { rsaMonMulAB ( ) ; rsaMulModRead ( ' ' , res ) ; } else { rsaMonMulBD ( ) ; rsaMulModRead ( ' ' , res ) ; } pstm_clamp ( res ) ; pstm_clear ( & X ) ; pstm_clear ( & Y ) ; pstm_clear ( & R ) ; return 0 ; }
res = a^e % n      DIGIT_BIT must be 32, or you need to re calc the RSAN value and the mc value
res = a^e % n DIGIT_BIT must be 32 or you need to re calc the RSAN value and the mc value
//pstm_count_bits(n); //R = 2^k % n //pstm_set(&Y, 1); //X = A * R //montMulMod(&Y, &Y, n, &Y); //if(pstm_get_bit(e, i)) //	montMulMod(&Y, &X, n, &Y); //montMulMod(&Y, &R, n, res);