Skip to content

Instantly share code, notes, and snippets.

@recmo
Created December 2, 2016 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save recmo/2fe7e4179127cc7fd693fe5b876c1525 to your computer and use it in GitHub Desktop.
Save recmo/2fe7e4179127cc7fd693fe5b876c1525 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.0;
contract HexEncoding {
function hexEncodeTest(address addr) returns (bytes32 ret) {
uint x = uint(addr) / 2**32;
// Nibble interleave
x = x & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff;
x = (x | (x * 2**64)) & 0x0000000000000000ffffffffffffffff0000000000000000ffffffffffffffff;
x = (x | (x * 2**32)) & 0x00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff;
x = (x | (x * 2**16)) & 0x0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff;
x = (x | (x * 2** 8)) & 0x00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff;
x = (x | (x * 2** 4)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f;
// Hex encode
uint h = (x & 0x0808080808080808080808080808080808080808080808080808080808080808) / 8;
uint i = (x & 0x0404040404040404040404040404040404040404040404040404040404040404) / 4;
uint j = (x & 0x0202020202020202020202020202020202020202020202020202020202020202) / 2;
x = x + (h & (i | j)) * 0x27 + 0x3030303030303030303030303030303030303030303030303030303030303030;
// Store and load next batch
assembly {
mstore(0, x)
}
x = uint(addr) * 2**96;
// Nibble interleave
x = x & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff;
x = (x | (x * 2**64)) & 0x0000000000000000ffffffffffffffff0000000000000000ffffffffffffffff;
x = (x | (x * 2**32)) & 0x00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff;
x = (x | (x * 2**16)) & 0x0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff;
x = (x | (x * 2** 8)) & 0x00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff;
x = (x | (x * 2** 4)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f;
// Hex encode
h = (x & 0x0808080808080808080808080808080808080808080808080808080808080808) / 8;
i = (x & 0x0404040404040404040404040404040404040404040404040404040404040404) / 4;
j = (x & 0x0202020202020202020202020202020202020202020202020202020202020202) / 2;
x = x + (h & (i | j)) * 0x27 + 0x3030303030303030303030303030303030303030303030303030303030303030;
// Store and hash
assembly {
mstore(32, x)
ret := sha3(0, 40)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment