Hot answers tagged

158 votes
Accepted

Which cryptographic hash function does Ethereum use?

Ethereum uses KECCAK-256. It should be noted that it does not follow the FIPS-202 based standard (a.k.a SHA-3), which was finalized in August 2015. According to this, NIST changed the padding to ...
user avatar
30 votes
Accepted

What is the cheapest hash function available in solidity?

keccak256 (new alias for sha3) is cheapest. Source: Yellow Paper Appendix G mentions the gas cost of sha3 is: 30 gas + 6 gas for each word (rounded up) for input data to a SHA3 Keccak-256 operation....
user avatar
  • 78.3k
28 votes
Accepted

How does Solidity's sha3 / keccak256 hash uints?

Jehan's answer is great, but we need to explain one more thing: Why does sha3(1) in solidity produce b10e2d...fa0cf6? This is because solidity's sha3 function hashes its inputs based on the argument ...
user avatar
  • 3,420
25 votes
Accepted

Why aren't Solidity sha3 hashes not matching what other sha3 libraries produce?

Ethereum uses Keccak-256, instead of the SHA-3 FIPS 202 standard. In the sha3 libraries you are using, try looking for the option to specify using Keccak-256. For Python see Getting Method ID "...
user avatar
  • 78.3k
21 votes

Which cryptographic hash function does Ethereum use?

To make it clearer that Ethereum uses KECCAK-256 instead of the NIST standardized SHA-3 hash function, Solidity 0.4.3 has introduced keccak256. (It is an alias to sha3, meaning that keccak256 ...
user avatar
  • 78.3k
20 votes

How does the Keccak256 hash function work?

Keccak is nice that it has arbitrary inputs and an infinite input space. This enables one to "make a hash" of a super large file where each input causes the internal state to scramble up ...
user avatar
  • 411
16 votes

What is topics[0] in Event logs?

topics[0] is the hash of the signature of the event. Example from Solidity docs: keccak256("Deposit(address,bytes32,uint256)") is the signature of the event: event Deposit( address indexed ...
user avatar
  • 78.3k
16 votes

Why aren't Solidity sha3 hashes not matching what other sha3 libraries produce?

Extending on this answer: Ethereum uses Keccak hashing, which was submitted to the NIST competition for SHA3. It was subsequently selected as a winner in 2012, but the final standard was only ...
user avatar
  • 2,479
13 votes
Accepted

Difference between keccak256 and sha3

from an answer to What are the key differences between the draft SHA-3 standard and the Keccak submission?: No they did not, the internals and security levels have not been changed from the ...
user avatar
  • 246
11 votes

What if two ether functions have the same signature?

The function signatures need to be unique only within a contract. The Solidity compiler will detect and prevent collisions if they are not unique in this case. If you have fewer than 2^16 functions ...
user avatar
  • 8,652
10 votes

How does Solidity's sha3 / keccak256 hash uints?

Solidity use HEX values internally. > web3.sha3(web3.toHex(1)) "5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2"
user avatar
  • 109
9 votes
Accepted

Sign raw transaction offline with C++ on arduino

You can use our Firefly Hardware Wallet (https://firefly.city) for the ATmega328 to do: secp256k1 signing RLP decoding transactions keccak256 generating checksum addresses (or raw addresses) Signing ...
user avatar
  • 459
9 votes

How to keccak256 multiple types in web3js to match Solidity keccak256?

You can use web3.utils.soliditySha3() Alternatively you can try this way: web3.sha3(web3.utils.toHex(string1) + address1, {encoding:"hex"}); Note that address1 should not be prefixed with 0x. ...
user avatar
  • 10.3k
8 votes

How does Solidity's sha3 / keccak256 hash uints?

Solidity's sha3 function hashes the byte representation of a uint. That is, the number in hex (base 16), padded to 32 bytes. 32 empty bytes in hex representation is 64 zeros. To do this in JS, we can ...
user avatar
  • 733
8 votes

keccak3 in SOLIDITY and in WEB3 different hashes for an address type field

web3.sha3() takes an encoding parameter to specify that you are passing in a hex address: address_string = '0x5b2063246f2191f18f2675cedb8b28102e957458'; web3.sha3(address_string, {encoding: 'hex'}); ...
user avatar
  • 6,101
7 votes
Accepted

How does keccak256 concatenate values inside a Solidity smart contract?

The arguments are expressed as bytes, left-padded with zeroes to the maximum length of the data type you've passed in, and concatenated without any kind of delimiter. In Python, given two hex-encoded ...
user avatar
  • 16.3k
7 votes
Accepted

Is it dangerous to name my function ccccvKygDv?

I believe this is just a bit mask. The code loads the call data, shifts it to the right, and then does a bitwise AND with 0xFFFFFFFF to get the (now) bottom four bytes (the function selector). Then ...
user avatar
  • 26.4k
7 votes
Accepted

How to compare two bytes32?

A comparison between two bytes32 IS possible in solidity. Consider this simple contract: pragma solidity 0.5.4; contract Test { bytes32 public constant bytes32_ = "Hello World!"; bytes32 ...
user avatar
6 votes
Accepted

How to install and test Viper programming language?

You need to install the latest pyethereum; I just uploaded it as pyethereum 1.3.7, or from the state_revamp branch. That should also automatically install the latest pyrlp, which has one compatibility ...
user avatar
6 votes

Different hash values for keccak256 functions?

In your NodeJS you have to fix the input you are passing to its keccak function. The fix might simply be: keccak_256(firstName+lastName+ studentId); but you have to check your NodeJS library as well ...
user avatar
  • 78.3k
6 votes
Accepted

Distribution of KECCAK-256 hash values

I came across this paper that looks at using Keccak-256 (the original one that Ethereum uses, not SHA3) as a PRNG. It subjects the output of the hash to a large number of tests of randomness and it ...
user avatar
  • 8,652
6 votes

Is it safe to use abi.encodePacked in production solidity code?

The function abi.encodePacked() only concatenates its input data in a bytes array in memory it doesn't transform the data in any way. So it is totaly safe to use in production code. The function ...
user avatar
  • 29.1k
6 votes
Accepted

What is the gas cost of keccak256("abc")?

Yes, you are exactly correct, except for the empty string. keccak256("") costs 30 gas. keccak256("0") costs 36 gas. keccak256("0123456789ABCDEF0123456789ABCDEF") still costs 36 gas. keccak256("...
user avatar
  • 9,037
6 votes
Accepted

How to generate the full list of function selectors for a contract?

Maybe you can use the online Ethereum IDE (Remix): Paste the whole code of the address you posted. Select the same compiler version: v0.4.25+commit.59dbf8f1. Select the contract by name: ...
user avatar
  • 2,958
6 votes
Accepted

Why is there a SHA256 pre-compiled contract when SHA256 is also an opcode?

As far as I'm aware there is no SHA256 opcode. I tried finding one but couldn't find - not listed at least in, for example, this list: https://github.com/crytic/evm-opcodes Maybe you're mixing it up ...
user avatar
6 votes
Accepted

Solidity's keccak256 hash doesn't match WEB3 keccak hash

The abi.encode function encode its parameters for a contract call using Solidity's ABI specs. For fixed size types like uintXXX, bool, address, it's quite easy, just pad them to 32 bytes; for dynamic ...
user avatar
  • 29.1k
6 votes
Accepted

How to use ethers keccak256?

Let's look at the function definition for keccak256: export function keccak256(data: BytesLike): string { return '0x' + sha3.keccak_256(arrayify(data)); } The input is not a string - it's a ...
user avatar
5 votes

How to use keccak256 in Solidity?

keccak256 is available in Solidity 0.4.3 and later. If you're using browser-solidity, Solidity 0.4.4 and later is needed: keccak256 Error Undeclared identifier in browser-solidity
user avatar
  • 78.3k
5 votes

Does Ethereum use Ethash or KECCAK-256 (an alias to SHA3) as hash function?

A hash function is different to a proof-of-work algorithm. Much of the effort in following a proof-of-work algorithm involves making hashes, but they're distinct things. Ethash is the proof-of-work ...
user avatar
  • 16.3k
5 votes
Accepted

Getting Method ID "Keccak hash" in Python

With a recent (>=1.0) version of pysha3 you can recreate the method ID with: from sha3 import keccak_256 sha3_hash = keccak_256("baz(uint32,bool)").hexdigest() method_id = "0x"+sha3_hash[:8] print ...
user avatar
  • 16.3k

Only top scored, non community-wiki answers of a minimum length are eligible