Crypto and Math functions in Solidity
An overview of the crypto functions and math functions that you can use in Solidity
Table of Contents for Crypto and Math functions in Solidity
I’ve written about using using dates/times in Solidity), but did you also know you can use built in crypto and math functions in Solidity.
keccak256(bytes inputVal) returns (bytes32)
Returns the keccak256 hash of the
bytes input, returning a
This is similar to keccak256().
sha256(bytes inputVal) returns (bytes32)
It returns the SHA256 hash value of the input
ripemd160(bytes value) returns (bytes20)
ripemd160 hash value of the input
ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address)
Recover an address from a elliptic curve signature. You can use this to validate a signed value. For details including important security issues to be aware of see this page
rfirst 32 bytes of sig
ssecond 32 bytes of sig
vlast byte of sig
addmod(uint x, uint y, uint k) returns (uint)
(x + y) % k. The addition part (x+y) is performed with arbitrary precision and does not wrap around at 2**256.
Assert k is != 0.
mulmod(uint x, uint y, uint k) returns (uint)
(x * y) %k), where the multiplication is performed with arbitrary precision and does not wrap around at 2**256.
Also see how to do floating point math in Solidity.
Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!
📙 Solidity Auditing online quiz
Learn how to audit smart contracts by looking at some example code and trying to find the bugs
⛽ Solidity Gas Optimizations Guide
How to optimize and reduce gas usage in your smart contracts in Solidity
🧪 Guide to testing with Foundry
Guide to adding testing for your Solidity contracts, using the Foundry and Forge tools
📌 Guide to UTXO
UTXO and the UTXO set (used by blockchains such as Bitcoin) explained
📐 Solidity Assembly Guide
Introduction guide to using assembly in your Solidity smart contracts
📦 Ethereum EOF format explained
Information explaining what the upcoming Ethereum EOF format is all about