We’re rewarding the question askers & reputations are being recalculated! Read more.

Hot answers tagged

123

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 SHA3-256(M) = KECCAK [512] (M || 01, 256). This was different from the padding proposed by the Keccak team in The Keccak SHA-3 submission version 3 (final, winning ...


26

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 types. Thus the value 1 will generate a different hash if it is stored as bytes8, bytes16, bytes32, etc. Since sha3(1) is being passed 1 as a number literal, it ...


24

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 "Keccak hash" in Python For Javascript, this library js-sha3 would involve using the keccak_256 function instead of sha3_256. keccak_256(''); // ...


20

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. Appendix E has the costs for the others. sha256 (SHA2-256) costs: 60 gas + 12 gas for each word (rounded up) for input data to a SHA2-256 operation. ...


15

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 produces identical results to sha3, but with the intent to avoid confusion, especially for developers new to Ethereum.) It's recommended that new code use keccak256 ...


15

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 released in August 2015. Ethereum uses Keccak in its form as it was selected in 2012. Too bad some parameters of the algorithm were tweaked for the final ...


14

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 _from, bytes32 indexed _id, uint _value );


9

Solidity use HEX values internally. > web3.sha3(web3.toHex(1)) "5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2"


8

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 utilize the infamous left-pad package: const jsHashWeb3 = web3.sha3(leftPad((1).toString(16), 64, 0), { encoding: 'hex' }) // ...


8

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'}); As a general side-note: you have to pay gas costs to do any custom work in Solidity so, when possible, you are best off working on the javascript side.


7

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 bytes32s prepended with a 0x called first and second, it looks something like: # keccak, change before upgrading pysha >= 3.10 from sha3 import sha3_256 ...


7

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 draft Keccak submission, only the padding rule has changed. The padding change is the only difference, this allows future tree hashing modes as well as the ...


7

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 it compares the four bytes extracted with the known function selector. It doesn't matter that the function signature you're looking for happens to be 0xFFFFFFFF....


6

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 fix; if it doesn't then you can install it yourself from http://github.com/ethereum/pyrlp. If you don't do this, then there are going to be a few bugs that ...


6

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 in your contract, collisions are unlikely to happen by chance. There was a Reddit thread about this recently, but it seems a bit contrived to me and not a ...


6

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 as the types you're passing in. Solidity docs: keccak256(...) returns (bytes32): compute the Ethereum-SHA-3 (Keccak-256) hash of the (tightly packed) ...


6

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 requires in the neighbourhood of around 700 bytes of free memory, I believe, but everything else requires a fairly slim memory footprint. If you require an ...


6

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. Example: > web3.sha3(web3.utils.toHex("test1") + "0AbdAce70D3790235af448C88547603b945604ea", {encoding:"hex"}); "...


6

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 passes them all. Conclusion: The experimental results show the KECCAK hash function has excellent pseudo randomness, and its security shows that it can be ...


5

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 algorithm. Keccak-256 is the most commonly-used hash function. Keccak-256 is used internally in places like when making hashes for block headers, but the place ...


5

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 method_id If your pysha3 is old (eg pinned atpysha3==0.3) you need: from sha3 import sha3_256 sha3_hash = sha3_256("baz(uint32,bool)").hexdigest() method_id = "...


5

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


4

SHA3 is just a name and the underlying cryptographic algorithms are elected by an international commitee. There is no reason why Ethereum should change such a fundamental thing like the basic hash function. This puts the network consensus in danger and would mean a huge effort in upgrading all software and broadcasting the information to all members of the ...


4

try this snipet function bytes32ToString(bytes32 x) constant returns (string) { bytes memory bytesString = new bytes(32); uint charCount = 0; for (uint j = 0; j < 32; j++) { byte char = byte(bytes32(uint(x) * 2 ** (8 * j))); if (char != 0) { bytesString[charCount] = char; charCount++; } } ...


4

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 public constant anotherBytes32 = "Hello World!"; function areTheyEqual() public pure returns(bool) { return (bytes32_ == anotherBytes32); } } ...


4

It seems like you treated "315f6b6f74000000000000000000000000000000000000000000000000000000" like a string instead of the hexadecimal byte sequence it actually is. Via web3 0.2.x: web3.sha3('315f6b6f74000000000000000000000000000000000000000000000000000000') > "0x90a0076f07f9884805bdc4d668c55756ff42238a0ad7b824b8a267783e822544" web3.sha3('...


3

Hashing is one-way: If you have text you can create a hash, but if you have a hash you can't use it to recreate the text.


3

Like any hash, it has an infinite input space. This enables one to "make a hash" of a super large file. The hash should entirely change if a single bit of data in the source is different - unlike say a CRC32, or a checksum. It means your password could be a million chars long maybe. It's stored on disk as a hash, much smaller in size. Regarding Keccak, it ...


3

Saw the following from a recent paper published on February 19 2016. We have uncovered a worrying lack of defense-in-depth in Bitcoin. In most cases, the failure of a single property in one cryptographic primitive is as bad as multiple failures in several primitives at once. For future versions of Bitcoin, we recommend including various ...


3

For browser-solidity, keccak256 is fixed in version 0.4.4 and later. keccak256 is recommended to use, it's identical to sha3, and is less confusing because Ethereum does not use the final SHA-3 standard, but the original winning algorithm named Keccak. More contracts using keccak256 will prevent confusion such as Why aren't Solidity sha3 hashes not ...


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