You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
507 B

  1. package vm
  2. import (
  3. "math/big"
  4. )
  5. // To256
  6. //
  7. // "cast" the big int to a 256 big int (i.e., limit to)
  8. var tt256 = new(big.Int).Lsh(big.NewInt(1), 256)
  9. var tt256m1 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
  10. var tt255 = new(big.Int).Lsh(big.NewInt(1), 255)
  11. func U256(x *big.Int) *big.Int {
  12. x.And(x, tt256m1)
  13. return x
  14. }
  15. func S256(x *big.Int) *big.Int {
  16. if x.Cmp(tt255) < 0 {
  17. return x
  18. } else {
  19. // We don't want to modify x, ever
  20. return new(big.Int).Sub(x, tt256)
  21. }
  22. }