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); }
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; }
bool XTR_DH::Validate(RandomNumberGenerator &rng, unsigned int level) const { bool pass = true; pass = pass && m_p > Integer::One() && m_p.IsOdd(); pass = pass && m_q > Integer::One() && m_q.IsOdd(); GFP2Element three = GFP2_ONB<ModularArithmetic>(m_p).ConvertIn(3); pass = pass && !(m_g.c1.IsNegative() || m_g.c2.IsNegative() || m_g.c1 >= m_p || m_g.c2 >= m_p || m_g == three); if (level >= 1) pass = pass && ((m_p.Squared()-m_p+1)%m_q).IsZero(); if (level >= 2) { pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2); pass = pass && XTR_Exponentiate(m_g, (m_p.Squared()-m_p+1)/m_q, m_p) != three; pass = pass && XTR_Exponentiate(m_g, m_q, m_p) == three; } return pass; }
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); }
void XTR_DH::GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const { Integer x(privateKey, PrivateKeyLength()); GFP2Element y = XTR_Exponentiate(m_g, x, m_p); y.Encode(publicKey, PublicKeyLength()); }