Been messing around with different ways of doing this using bn.js. I think my latest commit is the most pithy. But the left and right arrays work too if you want to revert to those.