Fixed one-off error in bignum encoding.
Added some unit tests for encoding.