module Ethereum::EthashRuby::Utils

Public Instance Methods

decode_int(s) click to toggle source

Assumes little endian bit ordering (same as Intel architectures)

# File lib/ethereum/ethash_ruby/utils.rb, line 42
def decode_int(s)
  s && !s.empty? ? s.unpack('L<').first : 0
end
deserialize_hash(h) click to toggle source
# File lib/ethereum/ethash_ruby/utils.rb, line 29
def deserialize_hash(h)
  (h.size / WORD_BYTES).times.map do |i|
    i *= WORD_BYTES
    decode_int h[i, WORD_BYTES]
  end
end
encode_int(i) click to toggle source
# File lib/ethereum/ethash_ruby/utils.rb, line 36
def encode_int(i)
  # `pack('L<`) will introduce leading zeros
  Ethereum::Utils.int_to_big_endian(i).reverse
end
hash_words(x, &block) click to toggle source
# File lib/ethereum/ethash_ruby/utils.rb, line 19
def hash_words(x, &block)
  x = serialize_hash(x) if x.instance_of?(Array)
  y = block.call(x)
  deserialize_hash(y)
end
keccak256(x) click to toggle source
# File lib/ethereum/ethash_ruby/utils.rb, line 13
def keccak256(x)
  hash_words(x) do |v|
    Ethereum::Utils.keccak256(v)
  end
end
keccak512(x) click to toggle source

sha3 hash function, outputs 64 bytes

# File lib/ethereum/ethash_ruby/utils.rb, line 7
def keccak512(x)
  hash_words(x) do |v|
    Ethereum::Utils.keccak512(v)
  end
end
serialize_hash(h) click to toggle source
# File lib/ethereum/ethash_ruby/utils.rb, line 25
def serialize_hash(h)
  h.map {|x| zpad(encode_int(x), WORD_BYTES) }.join
end
zpad(s, len) click to toggle source
# File lib/ethereum/ethash_ruby/utils.rb, line 46
def zpad(s, len)
  s + "\x00" * [0, len - s.size].max
end