Beispiel #1
0
void PrimeSieve::DoSieve()
{
	BuildPrimeTable();

	const unsigned int maxSieveSize = 32768;
	unsigned int sieveSize = STDMIN(Integer(maxSieveSize), (m_last-m_first)/m_step+1).ConvertToLong();

	m_sieve.clear();
	m_sieve.resize(sieveSize, false);

	if (m_delta == 0)
	{
		for (unsigned int i = 0; i < primeTableSize; ++i)
			SieveSingle(m_sieve, primeTable[i], m_first, m_step, m_step.InverseMod(primeTable[i]));
	}
	else
	{
		assert(m_step%2==0);
		Integer qFirst = (m_first-m_delta) >> 1;
		Integer halfStep = m_step >> 1;
		for (unsigned int i = 0; i < primeTableSize; ++i)
		{
			word p = primeTable[i];
			word stepInv = m_step.InverseMod(p);
			SieveSingle(m_sieve, p, m_first, m_step, stepInv);

			word halfStepInv = 2*stepInv < p ? 2*stepInv : 2*stepInv-p;
			SieveSingle(m_sieve, p, qFirst, halfStep, halfStepInv);
		}
	}
}
/*
 * function invertRabin		: This function invert the Rabin permutation
 * param tpPtr				: The pointer to the Rabin object 
 * param element			: The element to invert
 */
JNIEXPORT jlong JNICALL Java_edu_biu_scapi_primitives_trapdoorPermutation_cryptopp_CryptoPpRabinPermutation_invertRabin
  (JNIEnv *env, jobject, jlong tpPtr, jlong element) {
	  // Random Number Generator
	  AutoSeededRandomPool rng;
	  Utils utils;

	  //get the Integer value to invert
	  Integer x = *(Integer*) element;

	
	  //invert
	  ((InvertibleRabinFunction *) tpPtr) ->DoQuickSanityCheck();
	  Integer mod = ((InvertibleRabinFunction *) tpPtr) -> GetModulus();
	  ModularArithmetic modn(mod);
	  Integer p = ((InvertibleRabinFunction *) tpPtr)->GetPrime1();
	  Integer q = ((InvertibleRabinFunction *) tpPtr)->GetPrime2();
	  Integer cp=x % p;
	  Integer cq=x % q;
	  cp = ModularSquareRoot(cp, p);
	  cq = ModularSquareRoot(cq, q);

	  Integer v =p.InverseMod(q);
      Integer u = ((InvertibleRabinFunction *) tpPtr)->GetMultiplicativeInverseOfPrime2ModPrime1();

	  Integer onep = modn.Multiply(u,q);
 
	  Integer oneq = modn.Multiply(v,p);
	  
 	  Integer outp1 = modn.Multiply(onep,cp);
      Integer outp2 = modn.Multiply(onep,p-cp);
      Integer outq1 = modn.Multiply(oneq,cq);
 
	  Integer outq2 = modn.Multiply(oneq,q-cq);
 
	  Integer out = (outp1 + outq1)% mod;
      if ((Jacobi(out%p, p) == 1) && (Jacobi(out%q, q) == 1)){
 
		return (jlong) utils.getPointerToInteger(out);
	  }
	  
	  out = (outp1 + outq2)%mod;
      if ((Jacobi(out%p, p) == 1) && (Jacobi(out%q, q) == 1))
 		return (jlong) utils.getPointerToInteger(out);
	  
	  out = (outp2 + outq1)%mod;
	  if ((Jacobi(out%p, p) == 1) && (Jacobi(out%q, q) == 1))
		  return (jlong) utils.getPointerToInteger(out);
	  
	  out = (outp2 + outq2)%mod;
	  if ((Jacobi(out%p, p) == 1) && (Jacobi(out%q, q) == 1))
		  return (jlong) utils.getPointerToInteger(out);
 
	  //If none of the above cases are true then retun a pointer to the Integer 0.
	  out = 0;
	  return (jlong) utils.getPointerToInteger(out);	

}