class OpenSSL::PKey::DH

def valid?

lifted more-or-less directly from OpenSSH, dh.c, dh_pub_is_valid.)
Determines whether the pub_key for this key is valid. (This algorithm
def valid?
  return false if pub_key.nil? || pub_key < 0
  bits_set = 0
  pub_key.num_bits.times { |i| bits_set += 1 if pub_key.bit_set?(i) }
  return ( bits_set > 1 && pub_key < p )
end