Speed improvements

Some speed improvements pulled out of the previous PR, particularly using bcpowmod inside expmod instead of using recursion.

Seems to be about a 4-5x boost (now ~45 seconds for checkvalid), but still too slow for production use.