Ejemplo n.º 1
0
bool divmod_test()
{
   galois::GaloisFieldPolynomial gfp1(&gf,9,gfe);
   galois::GaloisFieldPolynomial gfp2(&gf,5,gfe2);
   galois::GaloisFieldPolynomial gfp3(&gf,4,gfe3);
   galois::GaloisFieldPolynomial gfp4(&gf,0);

   gfp4 = (gfp1 * gfp2) + gfp3;

   if(
      (gfp4 % gfp1 != gfp3) ||
      (gfp4 % gfp2 != gfp3)
     )
   {
      std::cout << "Div-Mod ERROR!"   << std::endl;
      std::cout << "gfp1(x) " << gfp1 << std::endl;
      std::cout << "gfp2(x) " << gfp2 << std::endl;
      std::cout << "gfp3(x) " << gfp3 << std::endl;
      std::cout << "gfp4(x) " << gfp3 << std::endl;
      return false;
   }

   return true;

}
Ejemplo n.º 2
0
void XTR_FindPrimesAndGenerator(RandomNumberGenerator &rng, Integer &p, Integer &q, GFP2Element &g, unsigned int pbits, unsigned int qbits)
{
	CRYPTOPP_ASSERT(qbits > 9);	// no primes exist for pbits = 10, qbits = 9
	CRYPTOPP_ASSERT(pbits > qbits);

	const Integer minQ = Integer::Power2(qbits - 1);
	const Integer maxQ = Integer::Power2(qbits) - 1;
	const Integer minP = Integer::Power2(pbits - 1);
	const Integer maxP = Integer::Power2(pbits) - 1;

top:

	Integer r1, r2;
	do
	{
		(void)q.Randomize(rng, minQ, maxQ, Integer::PRIME, 7, 12);
		// Solution always exists because q === 7 mod 12.
		(void)SolveModularQuadraticEquation(r1, r2, 1, -1, 1, q);
		// I believe k_i, r1 and r2 are being used slightly different than the
		// paper's algorithm. I believe it is leading to the failed asserts.
		// Just make the assert part of the condition.
		if(!p.Randomize(rng, minP, maxP, Integer::PRIME, CRT(rng.GenerateBit() ?
			r1 : r2, q, 2, 3, EuclideanMultiplicativeInverse(p, 3)), 3 * q)) { continue; }
	} while (((p % 3U) != 2) || (((p.Squared() - p + 1) % q).NotZero()));

	// CRYPTOPP_ASSERT((p % 3U) == 2);
	// CRYPTOPP_ASSERT(((p.Squared() - p + 1) % q).IsZero());

	GFP2_ONB<ModularArithmetic> gfp2(p);
	GFP2Element three = gfp2.ConvertIn(3), t;

	while (true)
	{
		g.c1.Randomize(rng, Integer::Zero(), p-1);
		g.c2.Randomize(rng, Integer::Zero(), p-1);
		t = XTR_Exponentiate(g, p+1, p);
		if (t.c1 == t.c2)
			continue;
		g = XTR_Exponentiate(g, (p.Squared()-p+1)/q, p);
		if (g != three)
			break;
	}

	if (XTR_Exponentiate(g, q, p) != three)
		goto top;

	// CRYPTOPP_ASSERT(XTR_Exponentiate(g, q, p) == three);
}
Ejemplo n.º 3
0
bool mod_zmodetest()
{
   galois::GaloisFieldPolynomial gfp1(&gf,9,gfe);
   galois::GaloisFieldPolynomial gfp2(&gf,2,gfez); // p(x) = x^2

   if((gfp1 % gfp2) != (gfp1 % 2))
   {
      std::cout << "Mod-ZMod ERROR!" << std::endl;
      std::cout << "gfp1(x) " << gfp1 << std::endl;
      std::cout << "gfp2(x) " << gfp2 << std::endl;
      return false;
   }

   return true;

}
Ejemplo n.º 4
0
bool XTR_DH::Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey) const
{
	GFP2Element w(otherPublicKey, PublicKeyLength());
	if (validateOtherPublicKey)
	{
		GFP2_ONB<ModularArithmetic> gfp2(m_p);
		GFP2Element three = gfp2.ConvertIn(3);
		if (w.c1.IsNegative() || w.c2.IsNegative() || w.c1 >= m_p || w.c2 >= m_p || w == three)
			return false;
		if (XTR_Exponentiate(w, m_q, m_p) != three)
			return false;
	}
	Integer s(privateKey, PrivateKeyLength());
	GFP2Element z = XTR_Exponentiate(w, s, m_p);
	z.Encode(agreedValue, AgreedValueLength());
	return true;
}
Ejemplo n.º 5
0
GFP2Element XTR_Exponentiate(const GFP2Element &b, const Integer &e, const Integer &p)
{
	unsigned int bitCount = e.BitCount();
	if (bitCount == 0)
		return GFP2Element(-3, -3);

	// find the lowest bit of e that is 1
	unsigned int lowest1bit;
	for (lowest1bit=0; e.GetBit(lowest1bit) == 0; lowest1bit++) {}

	GFP2_ONB<MontgomeryRepresentation> gfp2(p);
	GFP2Element c = gfp2.ConvertIn(b);
	GFP2Element cp = gfp2.PthPower(c);
	GFP2Element S[5] = {gfp2.ConvertIn(3), c, gfp2.SpecialOperation1(c)};

	// do all exponents bits except the lowest zeros starting from the top
	unsigned int i;
	for (i = e.BitCount() - 1; i>lowest1bit; i--)
	{
		if (e.GetBit(i))
		{
			gfp2.RaiseToPthPower(S[0]);
			gfp2.Accumulate(S[0], gfp2.SpecialOperation2(S[2], c, S[1]));
			S[1] = gfp2.SpecialOperation1(S[1]);
			S[2] = gfp2.SpecialOperation1(S[2]);
			S[0].swap(S[1]);
		}
		else
		{
			gfp2.RaiseToPthPower(S[2]);
			gfp2.Accumulate(S[2], gfp2.SpecialOperation2(S[0], cp, S[1]));
			S[1] = gfp2.SpecialOperation1(S[1]);
			S[0] = gfp2.SpecialOperation1(S[0]);
			S[2].swap(S[1]);
		}
	}

	// now do the lowest zeros
	while (i--)
		S[1] = gfp2.SpecialOperation1(S[1]);

	return gfp2.ConvertOut(S[1]);
}
Ejemplo n.º 6
0
bool shiftleft_test()
{

   galois::GaloisFieldPolynomial gfp1(&gf,9,gfe);
   galois::GaloisFieldPolynomial gfp2(&gf,0);

   gfp2 = gfp1 << 10;
   gfp2 = gfp2 >> 10;

   if(gfp2 != gfp1)
   {
      std::cout << "Shift Left ERROR!" << std::endl;
      std::cout << "gfp1(x) " << gfp1 << std::endl;
      std::cout << "gfp2(x) " << gfp2 << std::endl;
      return false;
   }

   return true;

}
Ejemplo n.º 7
0
bool addsub_test()
{

   galois::GaloisFieldPolynomial gfp1(&gf,9,gfe);
   galois::GaloisFieldPolynomial gfp2(&gf,5,gfe2);
   galois::GaloisFieldPolynomial gfp3(&gf,0);

   gfp3 = gfp1 + gfp2;
   gfp3 = gfp3 - gfp2;

   if (gfp1 != gfp3)
   {
      std::cout << "Add-Sub ERROR!"   << std::endl;
      std::cout << "gfp1(x) " << gfp1 << std::endl;
      std::cout << "gfp2(x) " << gfp2 << std::endl;
      std::cout << "gfp3(x) " << gfp3 << std::endl;
      return false;
   }

   return true;
}
Ejemplo n.º 8
0
bool muldiv_test()
{
   galois::GaloisFieldPolynomial gfp1(&gf,9,gfe);
   galois::GaloisFieldPolynomial gfp2(&gf,5,gfe2);
   galois::GaloisFieldPolynomial gfp3(&gf,0);

   gfp3 = gfp1 * gfp2;
   gfp3 = gfp3 / gfp2;

   if (gfp1 != gfp3)
   {
      std::cout << "Mul-Div ERROR!"   << std::endl;
      std::cout << "gfp1(x) " << gfp1 << std::endl;
      std::cout << "gfp2(x) " << gfp2 << std::endl;
      std::cout << "gfp3(x) " << gfp3 << std::endl;
      return false;
   }

   return true;

}
Ejemplo n.º 9
0
bool exp_test()
{
   galois::GaloisFieldPolynomial gfp1(&gf,9,gfe);
   galois::GaloisFieldPolynomial gfp2(&gf,0);

   gfp2 = gfp1 ^ 10;

   for (unsigned int i = 0; i < 10; i++)
   {
      gfp2 = gfp2 / gfp1;
   }

   if(gfp2 != gfp1)
   {
      std::cout << "Exponentiation ERROR!" << std::endl;
      std::cout << "gfp1(x) " << gfp1 << std::endl;
      std::cout << "gfp2(x) " << gfp2 << std::endl;
      return false;
   }

   return true;

}
Ejemplo n.º 10
0
void XTR_FindPrimesAndGenerator(RandomNumberGenerator &rng, Integer &p, Integer &q, GFP2Element &g, unsigned int pbits, unsigned int qbits)
{
	assert(qbits > 9);	// no primes exist for pbits = 10, qbits = 9
	assert(pbits > qbits);

	const Integer minQ = Integer::Power2(qbits - 1);
	const Integer maxQ = Integer::Power2(qbits) - 1;
	const Integer minP = Integer::Power2(pbits - 1);
	const Integer maxP = Integer::Power2(pbits) - 1;

	Integer r1, r2;
	do
	{
		bool qFound = q.Randomize(rng, minQ, maxQ, Integer::PRIME, 7, 12);
		CRYPTOPP_UNUSED(qFound); assert(qFound);
		bool solutionsExist = SolveModularQuadraticEquation(r1, r2, 1, -1, 1, q);
		CRYPTOPP_UNUSED(solutionsExist); assert(solutionsExist);
	} while (!p.Randomize(rng, minP, maxP, Integer::PRIME, CRT(rng.GenerateBit()?r1:r2, q, 2, 3, EuclideanMultiplicativeInverse(p, 3)), 3*q));
	assert(((p.Squared() - p + 1) % q).IsZero());

	GFP2_ONB<ModularArithmetic> gfp2(p);
	GFP2Element three = gfp2.ConvertIn(3), t;

	while (true)
	{
		g.c1.Randomize(rng, Integer::Zero(), p-1);
		g.c2.Randomize(rng, Integer::Zero(), p-1);
		t = XTR_Exponentiate(g, p+1, p);
		if (t.c1 == t.c2)
			continue;
		g = XTR_Exponentiate(g, (p.Squared()-p+1)/q, p);
		if (g != three)
			break;
	}
	assert(XTR_Exponentiate(g, q, p) == three);
}