What is the shortest runtime bytecode for a contract that:
- expects only one uint256 as calldata (no function selector)
- returns the fibonacci number at that index (can start the sequence at either 0 or 1) Please provide all working solutions as you optimize, along with:
- opcodes as words
- comments with contents of stack and memory as appropriate Bonus: optimize for gas costs instead of code size as a reference example, the shortest runtime bytecode for returning the chainId is: 0x463d52593df3
First implementation of a contract with a function returning the Fibonacci number at the Nth index. fib(100) = 61260 gas
2nd implementation using the fallback function to comply to the (no function selector) requiremnt. uint256 is sent as Wei in the transaction. fib(100) = 61406 gas
3rd implementation using the fallback function to comply to the (no function selector) requiremnt, however the result is returned in bytes by the fallback function to comply to (returns the fibonacci number at that index). fib(100) = 60511 gas
0x6080604052600036606060003490506000600190506000600190506000830361004f5760008060405160200161003591906100e5565b6040516020818303038152906040529450505050506100af565b6000600290505b83811015610088576000828461006c919061012f565b905082935080925050808061008090610185565b915050610056565b508060405160200161009a91906100e5565b60405160208183030381529060405293505050505b915050805190602001f35b6000819050919050565b6000819050919050565b6100df6100da826100ba565b6100c4565b82525050565b60006100f182846100ce565b60208201915081905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600061013a826100ba565b9150610145836100ba565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561017a57610179610100565b5b828201905092915050565b6000610190826100ba565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82036101c2576101c1610100565b5b60018201905091905056fea264697066735822122052d5dfafd0113386fabe52dfe63be3012572952e4af766e0d729c3f748649ca564736f6c634300080d0033
PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x0 CALLDATASIZE PUSH1 0x60 PUSH1 0x0 CALLVALUE SWAP1 POP PUSH1 0x0 PUSH1 0x1 SWAP1 POP PUSH1 0x0 PUSH1 0x1 SWAP1 POP PUSH1 0x0 DUP4 SUB PUSH2 0x4F JUMPI PUSH1 0x0 DUP1 PUSH1 0x40 MLOAD PUSH1 0x20 ADD PUSH2 0x35 SWAP2 SWAP1 PUSH2 0xE5 JUMP JUMPDEST PUSH1 0x40 MLOAD PUSH1 0x20 DUP2 DUP4 SUB SUB DUP2 MSTORE SWAP1 PUSH1 0x40 MSTORE SWAP5 POP POP POP POP POP PUSH2 0xAF JUMP JUMPDEST PUSH1 0x0 PUSH1 0x2 SWAP1 POP JUMPDEST DUP4 DUP2 LT ISZERO PUSH2 0x88 JUMPI PUSH1 0x0 DUP3 DUP5 PUSH2 0x6C SWAP2 SWAP1 PUSH2 0x12F JUMP JUMPDEST SWAP1 POP DUP3 SWAP4 POP DUP1 SWAP3 POP POP DUP1 DUP1 PUSH2 0x80 SWAP1 PUSH2 0x185 JUMP JUMPDEST SWAP2 POP POP PUSH2 0x56 JUMP JUMPDEST POP DUP1 PUSH1 0x40 MLOAD PUSH1 0x20 ADD PUSH2 0x9A SWAP2 SWAP1 PUSH2 0xE5 JUMP JUMPDEST PUSH1 0x40 MLOAD PUSH1 0x20 DUP2 DUP4 SUB SUB DUP2 MSTORE SWAP1 PUSH1 0x40 MSTORE SWAP4 POP POP POP POP JUMPDEST SWAP2 POP POP DUP1 MLOAD SWAP1 PUSH1 0x20 ADD RETURN JUMPDEST PUSH1 0x0 DUP2 SWAP1 POP SWAP2 SWAP1 POP JUMP JUMPDEST PUSH1 0x0 DUP2 SWAP1 POP SWAP2 SWAP1 POP JUMP JUMPDEST PUSH2 0xDF PUSH2 0xDA DUP3 PUSH2 0xBA JUMP JUMPDEST PUSH2 0xC4 JUMP JUMPDEST DUP3 MSTORE POP POP JUMP JUMPDEST PUSH1 0x0 PUSH2 0xF1 DUP3 DUP5 PUSH2 0xCE JUMP JUMPDEST PUSH1 0x20 DUP3 ADD SWAP2 POP DUP2 SWAP1 POP SWAP3 SWAP2 POP POP JUMP JUMPDEST PUSH32 0x4E487B7100000000000000000000000000000000000000000000000000000000 PUSH1 0x0 MSTORE PUSH1 0x11 PUSH1 0x4 MSTORE PUSH1 0x24 PUSH1 0x0 REVERT JUMPDEST PUSH1 0x0 PUSH2 0x13A DUP3 PUSH2 0xBA JUMP JUMPDEST SWAP2 POP PUSH2 0x145 DUP4 PUSH2 0xBA JUMP JUMPDEST SWAP3 POP DUP3 PUSH32 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF SUB DUP3 GT ISZERO PUSH2 0x17A JUMPI PUSH2 0x179 PUSH2 0x100 JUMP JUMPDEST JUMPDEST DUP3 DUP3 ADD SWAP1 POP SWAP3 SWAP2 POP POP JUMP JUMPDEST PUSH1 0x0 PUSH2 0x190 DUP3 PUSH2 0xBA JUMP JUMPDEST SWAP2 POP PUSH32 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF DUP3 SUB PUSH2 0x1C2 JUMPI PUSH2 0x1C1 PUSH2 0x100 JUMP JUMPDEST JUMPDEST PUSH1 0x1 DUP3 ADD SWAP1 POP SWAP2 SWAP1 POP JUMP INVALID LOG2 PUSH5 0x6970667358 0x22 SLT KECCAK256 MSTORE 0xD5 0xDF 0xAF 0xD0 GT CALLER DUP7 STATICCALL 0xBE MSTORE 0xDF 0xE6 EXTCODESIZE 0xE3 ADD 0x25 PUSH19 0x952E4AF766E0D729C3F748649CA564736F6C63 NUMBER STOP ADDMOD 0xD STOP CALLER
4th implementation using the above modification but changing the algorithm to Binet's Formula (Found online) to try and reduce gas costs. fib(100) = 29985 gas 1662
5th implementation taking a uint256 as bytes calldata and returning the fibonacci number as bytes. fib(100) = 60856 gas
6th implementation moving all of the fibonacci caluclation into a library. The code in the library does not count as part of the contract bytecode and therefor reduces it.
Through testing multiple methods and different ways of obtaining the same result, contract Fib3 has the shortest runtime bytecode however contract Fib4 only uses half the gas of Fib3