Hot answers tagged

130

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 ...


28

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

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. ...


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(''); // ...


18

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 ...


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

It looks like you're in Python 2. Py2 represents bytes in a latin-1 encoded string. Sometimes you'll see ascii characters, other times you'll see something like \x18 which means the byte 00011000, aka 18 in hex, or 24 as an integer. A private key is just a bunch of bytes back-to-back. raw is the binary representation of the address. The address in addr is ...


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

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

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 ...


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


5

The return value of sha256 is bytes32. So use bytes32 a = sha256(...) and do the same for b. This will fix the compiler errors and a == b will work. But currently you cannot pass a struct to hashing functions like sha256, keccak256: https://github.com/ethereum/solidity/issues/980. So generally the members of the structs have to be compared against each ...


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

In Bitcoin, you can store 80 bytes in one transaction with OP_RETURN. SHA-3(256) has a size of 32 bytes and can be stored in one transaction. The fee for the OP_RETURN transaction is 0.0001 BTC. By today's price, it's $0.177 per tx. You can optimize it by using aggregations services, such as Open Timestamps In Ethereum, there are at two options to store ...


5

In Solidity the sha3() method will use the variable type (as opposed to variable content) to determine the size of it. uint maps to uint256 and that means in your example not the number 0x01 will be hashed, but 0x0000000000000000000000000000000000000000000000000000000000000001 is used as input. Use uint8 for an 8 bit variable (0x01). Also as a reminder, ...


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

I finally found the solution. The function given in this issue was the starting point. To make it work with negative numbers, I had to change it to this: function solSha3 (...args) { args = args.map(arg => { if (typeof arg === 'string') { if (arg.substring(0, 2) === '0x') { return arg.slice(2); } else {...


4

You need to use a big number library because Javascript's bitwise-operators only work on 32-bits. web3.sha3 output is 32 bytes. BigNumber.js that's included in web3.js doesn't support bitwise-operators. One option is to use bn.js, like: const BN = require("bn"); var a = new BN.BigInteger(h1, 16); var b = new BN.BigInteger(h2, 16); var clientHash = '0x' + ...


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

As mentioned by @jaime in the comments you need the position of the mapping variable to find the relation between address and storage key. You can find a code I have written to detect the position for a basic erc20 contract below (using web3.py): import json from web3.auto.infura import w3 from eth_utils import remove_0x_prefix, to_int, ...


4

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("0123456789ABCDEF0123456789ABCDEF_") costs 42 gas. These gas costs do not include costs other than the actual KECCAK256 opcode, such as pushing the pointer and ...


4

First you need to know that sha256 and keccak256 functions are not the same. Check the docs to see available functions. sha256 (with pyhton): >>> sha256("Hello World!").hexdigest() '7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069' keccak256 (with web3): web3.utils.keccak256("Hello World!") '...


3

No. Your bkp is not the SHA3 of your password. It's really quite simple. In the beginning, god said genwallet and... genwallet says: genwallet(opts['seed'],pw,email) You say "here's my email and pw" seed says "give me super random number": seed = random_key().decode('hex') # uses pybitcointools' 3-source random generator so now you need to get ...


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 ...


3

the keccak256() is an alias for the sha3 function to avoid any confusion with the sha-3 standard (The opcode is still called SHA3.) this alias was adopted this month so the old compiler don't recognize it. so use sha3 instead. The available hashing functions are sha3, sha256, ripemd160: pragma solidity ^0.4.0; contract C { function hashingsha3 (...


3

It seems impossible to do so, because Ethash requires a 16 MB pseudorandom cache. This would be expensive to store on the blockchain, as each Ethereum transaction only holds a maximum of 89kB (3 million gas).


3

Try specifying hex encoding explicitly: web3.sha3("0x0000000000000000000000000000000000000000000000000000000000000001", {encoding: "hex"}); '0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6' Without specifying the encoding, the argument is treated as an ascii string by default. This behaviour is changed in web3.js v.1.0.0, but I guess ...


3

The results are for hashing different numbers of zero bytes: keccak256('') is the hash of 0 zero bytes. keccak256(address(0)) is the hash of 20 zero bytes keccak256(uint(0)) is the hash of 32 zero bytes (same for bytes32 type) The way keccak pads the input accounts for the different outputs.


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