crypto/weakcryptospi/source/bigint/mont.cpp
changeset 29 ece3df019add
parent 19 cd501b96611d
equal deleted inserted replaced
19:cd501b96611d 29:ece3df019add
    54 
    54 
    55 CMontgomeryStructure::CMontgomeryStructure()
    55 CMontgomeryStructure::CMontgomeryStructure()
    56 	{
    56 	{
    57 	}
    57 	}
    58 
    58 
    59 TInteger& CMontgomeryStructure::ConvertIn(TInteger& aInteger) const
    59 TInteger& CMontgomeryStructure::ConvertInL(TInteger& aInteger) const
    60 	{
    60 	{
    61 	aInteger <<= WordsToBits(iModulus.Size());
    61 	aInteger <<= WordsToBits(iModulus.Size());
    62 	aInteger %= iModulus;
    62 	aInteger %= iModulus;
    63 	return aInteger;
    63 	return aInteger;
    64 	}
    64 	}
    97 
    97 
    98 const TInteger& CMontgomeryStructure::SquareL(const TInteger& aA) const
    98 const TInteger& CMontgomeryStructure::SquareL(const TInteger& aA) const
    99 	{
    99 	{
   100 	RInteger a = RInteger::NewL(aA);
   100 	RInteger a = RInteger::NewL(aA);
   101 	CleanupStack::PushL(a);
   101 	CleanupStack::PushL(a);
   102 	DoSquareL(iResult, ConvertIn(a));
   102 	DoSquareL(iResult, ConvertInL(a));
   103 	ConvertOutL(iResult);
   103 	ConvertOutL(iResult);
   104 	CleanupStack::PopAndDestroy(&a);
   104 	CleanupStack::PopAndDestroy(&a);
   105 	return iResult;
   105 	return iResult;
   106 	}
   106 	}
   107 
   107 
   136 	// 1.1 Precomputation
   136 	// 1.1 Precomputation
   137 	// g1 <- g
   137 	// g1 <- g
   138 	// g2 <- g^2
   138 	// g2 <- g^2
   139 	RInteger g2 = RInteger::NewL(aBase);
   139 	RInteger g2 = RInteger::NewL(aBase);
   140 	CleanupStack::PushL(g2);
   140 	CleanupStack::PushL(g2);
   141 	ConvertIn(g2);
   141 	ConvertInL(g2);
   142 	//ConvertIn can shrink g2, because we call DoSquare on g2, g2 must be the same size as the modulus
   142 	//ConvertInL can shrink g2, because we call DoSquare on g2, g2 must be the same size as the modulus
   143 	g2.CleanGrowL(iModulus.Size());
   143 	g2.CleanGrowL(iModulus.Size());
   144 	RInteger g1 = RInteger::NewL(g2);
   144 	RInteger g1 = RInteger::NewL(g2);
   145 	CleanupStack::PushL(g1);
   145 	CleanupStack::PushL(g1);
   146 	DoSquareL(g2, g2);
   146 	DoSquareL(g2, g2);
   147 
   147 
   162 		}
   162 		}
   163 	
   163 	
   164 	// 2 A <- 1, i <- t
   164 	// 2 A <- 1, i <- t
   165 	RInteger temp = RInteger::NewL(TInteger::One());
   165 	RInteger temp = RInteger::NewL(TInteger::One());
   166 	CleanupStack::PushL(temp);
   166 	CleanupStack::PushL(temp);
   167 	ConvertIn(temp);
   167 	ConvertInL(temp);
   168 
   168 
   169 	RInteger& A = iResult;
   169 	RInteger& A = iResult;
   170 	//Set A to one converted in for this modulus without changing the memory size of A (iResult)
   170 	//Set A to one converted in for this modulus without changing the memory size of A (iResult)
   171 	A.CopyL(temp, EFalse); 
   171 	A.CopyL(temp, EFalse); 
   172 	CleanupStack::PopAndDestroy(&temp);
   172 	CleanupStack::PopAndDestroy(&temp);
   212 const TInteger& CMontgomeryStructure::ReduceL(
   212 const TInteger& CMontgomeryStructure::ReduceL(
   213 	const TInteger& aInteger) const
   213 	const TInteger& aInteger) const
   214 	{
   214 	{
   215 	RInteger temp = RInteger::NewL(aInteger);
   215 	RInteger temp = RInteger::NewL(aInteger);
   216 	CleanupStack::PushL(temp);
   216 	CleanupStack::PushL(temp);
   217 	ConvertIn(temp);
   217 	ConvertInL(temp);
   218 	iResult.CopyL(ConvertOutL(temp), EFalse);
   218 	iResult.CopyL(ConvertOutL(temp), EFalse);
   219 	CleanupStack::PopAndDestroy(&temp);
   219 	CleanupStack::PopAndDestroy(&temp);
   220 	return iResult;
   220 	return iResult;
   221 	}
   221 	}
   222 
   222 
   233 	{
   233 	{
   234 	RInteger a = RInteger::NewL(aA);
   234 	RInteger a = RInteger::NewL(aA);
   235 	CleanupStack::PushL(a);
   235 	CleanupStack::PushL(a);
   236 	RInteger b = RInteger::NewL(aB);
   236 	RInteger b = RInteger::NewL(aB);
   237 	CleanupStack::PushL(b);
   237 	CleanupStack::PushL(b);
   238 	DoMultiplyL(iResult, ConvertIn(a), ConvertIn(b));
   238 	DoMultiplyL(iResult, ConvertInL(a), ConvertInL(b));
   239 	ConvertOutL(iResult);
   239 	ConvertOutL(iResult);
   240 	CleanupStack::PopAndDestroy(&b); 
   240 	CleanupStack::PopAndDestroy(&b); 
   241 	CleanupStack::PopAndDestroy(&a); 
   241 	CleanupStack::PopAndDestroy(&a); 
   242 	return iResult;
   242 	return iResult;
   243 	}
   243 	}