bool IsStrongProbablePrime(const Integer &n, const Integer &b) { if (n <= 3) return n==2 || n==3; assert(n>3 && b>1 && b<n-1); if ((n.IsEven() && n!=2) || GCD(b, n) != 1) return false; Integer nminus1 = (n-1); unsigned int a; // calculate a = largest power of 2 that divides (n-1) for (a=0; ; a++) if (nminus1.GetBit(a)) break; Integer m = nminus1>>a; Integer z = a_exp_b_mod_c(b, m, n); if (z==1 || z==nminus1) return true; for (unsigned j=1; j<a; j++) { z = z.Squared()%n; if (z==nminus1) return true; if (z==1) return false; } return false; }
Integer InvertibleRabinFunction::CalculateInverse(const Integer &in) const { DoQuickSanityCheck(); Integer cp=in%m_p, cq=in%m_q; int jp = Jacobi(cp, m_p); int jq = Jacobi(cq, m_q); if (jq==-1) { cp = cp*EuclideanMultiplicativeInverse(m_r, m_p)%m_p; cq = cq*EuclideanMultiplicativeInverse(m_r, m_q)%m_q; } if (jp==-1) { cp = cp*EuclideanMultiplicativeInverse(m_s, m_p)%m_p; cq = cq*EuclideanMultiplicativeInverse(m_s, m_q)%m_q; } cp = ModularSquareRoot(cp, m_p); cq = ModularSquareRoot(cq, m_q); if (jp==-1) cp = m_p-cp; Integer out = CRT(cq, m_q, cp, m_p, m_u); if ((jq==-1 && out.IsEven()) || (jq==1 && out.IsOdd())) out = m_n-out; return out; }
Integer InvertibleRabinFunction::CalculateInverse(const Integer &in) const { Integer cp=in%p, cq=in%q; int jp = Jacobi(cp, p); int jq = Jacobi(cq, q); if (jq==-1) { cp = cp*EuclideanMultiplicativeInverse(r, p)%p; cq = cq*EuclideanMultiplicativeInverse(r, q)%q; } if (jp==-1) { cp = cp*EuclideanMultiplicativeInverse(s, p)%p; cq = cq*EuclideanMultiplicativeInverse(s, q)%q; } cp = ModularSquareRoot(cp, p); cq = ModularSquareRoot(cq, q); if (jp==-1) cp = p-cp; Integer out = CRT(cq, q, cp, p, u); if ((jq==-1 && out.IsEven()) || (jq==1 && out.IsOdd())) out = n-out; return out; }
void InvertibleRSAFunction::Initialize(const Integer &n, const Integer &e, const Integer &d) { m_n = n; m_e = e; m_d = d; Integer r = --(d*e); while (r.IsEven()) r >>= 1; ModularArithmetic modn(n); for (Integer i = 2; ; ++i) { Integer a = modn.Exponentiate(i, r); if (a == 1) continue; Integer b; while (a != -1) { b = modn.Square(a); if (b == 1) { m_p = GCD(a-1, n); m_q = n/m_p; m_dp = m_d % (m_p-1); m_dq = m_d % (m_q-1); m_u = m_q.InverseMod(m_p); return; } a = b; } } }
bool IsLucasProbablePrime(const Integer &n) { if (n <= 1) return false; if (n.IsEven()) return n==2; assert(n>2); Integer b=3; unsigned int i=0; int j; while ((j=Jacobi(b.Squared()-4, n)) == 1) { if (++i==64 && n.IsSquare()) // avoid infinite loop if n is a square return false; ++b; ++b; } if (j==0) return false; else return Lucas(n+1, b, n)==2; }
void InvertibleRSAFunction::Initialize(const Integer &n, const Integer &e, const Integer &d) { if (n.IsEven() || e.IsEven() | d.IsEven()) throw InvalidArgument("InvertibleRSAFunction: input is not a valid RSA private key"); m_n = n; m_e = e; m_d = d; Integer r = --(d*e); unsigned int s = 0; while (r.IsEven()) { r >>= 1; s++; } ModularArithmetic modn(n); for (Integer i = 2; ; ++i) { Integer a = modn.Exponentiate(i, r); if (a == 1) continue; Integer b; unsigned int j = 0; while (a != n-1) { b = modn.Square(a); if (b == 1) { m_p = GCD(a-1, n); m_q = n/m_p; m_dp = m_d % (m_p-1); m_dq = m_d % (m_q-1); m_u = m_q.InverseMod(m_p); return; } if (++j == s) throw InvalidArgument("InvertibleRSAFunction: input is not a valid RSA private key"); a = b; } } }
bool IsStrongLucasProbablePrime(const Integer &n) { if (n <= 1) return false; if (n.IsEven()) return n==2; assert(n>2); Integer b=3; unsigned int i=0; int j; while ((j=Jacobi(b.Squared()-4, n)) == 1) { if (++i==64 && n.IsSquare()) // avoid infinite loop if n is a square return false; ++b; ++b; } if (j==0) return false; Integer n1 = n+1; unsigned int a; // calculate a = largest power of 2 that divides n1 for (a=0; ; a++) if (n1.GetBit(a)) break; Integer m = n1>>a; Integer z = Lucas(m, b, n); if (z==2 || z==n-2) return true; for (i=1; i<a; i++) { z = (z.Squared()-2)%n; if (z==n-2) return true; if (z==2) return false; } return false; }
Integer InvertibleRabinFunction::CalculateInverse(RandomNumberGenerator &rng, const Integer &in) const { DoQuickSanityCheck(); ModularArithmetic modn(m_n); Integer r(rng, Integer::One(), m_n - Integer::One()); r = modn.Square(r); Integer r2 = modn.Square(r); Integer c = modn.Multiply(in, r2); // blind Integer cp=c%m_p, cq=c%m_q; int jp = Jacobi(cp, m_p); int jq = Jacobi(cq, m_q); if (jq==-1) { cp = cp*EuclideanMultiplicativeInverse(m_r, m_p)%m_p; cq = cq*EuclideanMultiplicativeInverse(m_r, m_q)%m_q; } if (jp==-1) { cp = cp*EuclideanMultiplicativeInverse(m_s, m_p)%m_p; cq = cq*EuclideanMultiplicativeInverse(m_s, m_q)%m_q; } cp = ModularSquareRoot(cp, m_p); cq = ModularSquareRoot(cq, m_q); if (jp==-1) cp = m_p-cp; Integer out = CRT(cq, m_q, cp, m_p, m_u); out = modn.Divide(out, r); // unblind if ((jq==-1 && out.IsEven()) || (jq==1 && out.IsOdd())) out = m_n-out; return out; }
void InvertibleRSAFunction::Initialize(RandomNumberGenerator &rng, unsigned int keybits, const Integer &e) { GenerateRandom(rng, MakeParameters(Name::ModulusSize(), (int)keybits)(Name::PublicExponent(), e+e.IsEven())); }