added check for g < p and fixed bitNumber