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.

55 lines
947 B

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package vm
  2. import (
  3. "encoding/binary"
  4. )
  5. var (
  6. Zero = Word{0}
  7. One = Word{1}
  8. )
  9. type Word [32]byte
  10. func (w Word) String() string { return string(w[:]) }
  11. func (w Word) Copy() Word { return w }
  12. func (w Word) Bytes() []byte { return w[:] } // copied.
  13. func (w Word) IsZero() bool {
  14. accum := byte(0)
  15. for _, byt := range w {
  16. accum |= byt
  17. }
  18. return accum == 0
  19. }
  20. func Uint64ToWord(i uint64) Word {
  21. word := Word{}
  22. PutUint64(word[:], i)
  23. return word
  24. }
  25. func BytesToWord(bz []byte) Word {
  26. word := Word{}
  27. copy(word[:], bz)
  28. return word
  29. }
  30. func LeftPadWord(bz []byte) (word Word) {
  31. copy(word[:], bz)
  32. return
  33. }
  34. func RightPadWord(bz []byte) (word Word) {
  35. copy(word[32-len(bz):], bz)
  36. return
  37. }
  38. //-----------------------------------------------------------------------------
  39. func GetUint64(word Word) uint64 {
  40. return binary.LittleEndian.Uint64(word[:])
  41. }
  42. func PutUint64(dest []byte, i uint64) {
  43. binary.LittleEndian.PutUint64(dest, i)
  44. }