BigInteger ElGamal::GetRandBetween(Random &rng, const BigInteger &min, const BigInteger &max) { BigInteger range = max.sub(min); int nBits = range.bitLength(); BigInteger anInt; do { anInt = BigInteger(nBits,rng); } while (anInt.cmp(range)>0); anInt = anInt.add(min); return anInt; }
bool ElGamal::BogusVerify(const BigInteger &M, const BigInteger &a, const BigInteger &b) const { // check a != 0 && a == (g^b * y^a + m) mod q try { if(a.cmp(BigInteger::ZERO)==0) return false; // BigInteger a1 = a.mod(q); // BigInteger b1 = b.mod(q); BigInteger gbq = g.modExp(b,p,recip,nb); BigInteger yaq = y.modExp(a,p,recip,nb); /* cout << "p = " << p << endl; cout << "g = mod(" << g << ",p)" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "y = " << y << endl; cout << "gbq = " << gbq << endl; cout << "yaq = " << yaq << endl;*/ BigInteger result; result = gbq.modMulRecip(yaq,p,recip,nb); // result = gbq.mul(yaq); result = result.add(M); // result = result.mod(p); result = result.mod(q); if(a.cmp(result)==0) return true; else return false; } catch(ArithmeticException &) { } return false; }