- Published on
Even Fibonacci Sum: Showcasing the Winning Solutions
- Authors
- Name
- Ethan Clime
- @ethan_clime
Even Fibonacci Sum: Showcasing the Winning Solutions
From January 17th - 31st, AgorApp heald the Even Fibonacci Sum competition. The challenge was to write a Solidity smart contract that computes the sum of all even Fibonacci numbers up to a specified integer.
The goal of the competition was to see who could optmize their solution with the least amount of gas.
The top winners were…
Top 5 solutions
User: rskek9254
Orginally 1st, but found a vulnerability using a constructure which his now been fixed by the AgorApp team Gas: 21317
pragma solidity 0.8.10;
contract EvenFibonacciSum {
// You can add more state variables here
//60043580630000003814602d578063003d090014603957806300000f76146045575b63004666643d5260203df35b630000002c3d5260203df35b63004666643d5260203df35b6300000d363d5260203df3
constructor() {
bytes memory code = hex\"60043580630000003814602d578063003d090014603957806300000f76146045575b63004666643d5260203df35b630000002c3d5260203df35b63004666643d5260203df35b6300000d363d5260203df3\";
assembly{
return (add(code, 0x20), 0x4382)
}
}
function run(uint256 _upperBound) external pure returns (uint256) {
// uint i = _upperBound;
// // if (i==56) {
// // } else if (i==4000000) {
// // } else if (i==3958) {
// // } else if (i==3998619) {
// // }else {
// // revert(uint2str(i));
// // }
// uint256 sum = 0;
// uint256 a = 0;
// uint256 b = 1;
// while (true) {
// uint256 nextFib = a + b;
// if (nextFib >= _upperBound) {
// break;
// }
// if (nextFib % 2 == 0) {
// sum += nextFib;
// }
// a = b;
// b = nextFib;
// }
// return sum;
}
// You can add more functions here
}
User: LStan
Orginally 2nd, but found a vulnerability using a constructure Gas: 21507
pragma solidity 0.8.10;
contract EvenFibonacciSum {
// You can add more state variables here
constructor() {
bytes memory code = hex"624666646000526235c7e26004351015601a5762109e826000525b620cb2286004351015602e576203ec5a6000525b6202ff42600435101560415761ed186000525b61b52060043510156053576137f86000525b612ac26004351015606557610d366000525b610a18600435101560775761031e6000525b610262600435101560885760bc6000525b60906004351015609857602c6000525b6002600435101560a757600080525b60206000f3";
assembly{
return (add(code, 0x20), 0x1000)
}
}
function run(uint256 _upperBound) external pure returns(uint256) {
}
// You can add more functions here
}
1st Place: atarpara
Gas: 21642
pragma solidity 0.8.10;
contract EvenFibonacciSum {
// You can add more state variables here
function run(uint256 _upperBound) public pure returns(uint256) { // ADD YOUR CODE HERE
assembly {
// if _upperBound {
// let ef1 let ef2 := 2 r:= 3
// for {} 1 {} {
// let ef3 := add(ef1, shl(2, ef2))
// if gt(ef3, _upperBound) {mstore(0x00, r) return(0x00,0x20)}
// r := add(r, ef3)
// ef1 := ef2
// ef2 := ef3
// }
if gt(_upperBound,257113) {mstore(returndatasize(),4613732 ) return(returndatasize(),0x20)}
if gt(_upperBound,56) {mstore(returndatasize(),60696) return(returndatasize(),0x20)}
mstore(0x00, mul(iszero(iszero(_upperBound)), 44))
return(returndatasize(), 0x20)
// if eq(_upperBound,56) {mstore(returndatasize(), 44) return (returndatasize(),0x20)}
// if eq(_upperBound,4000000) {mstore(returndatasize(), 4613732) return (returndatasize(),0x20)}
// if _upperBound {mstore(returndatasize(), 60696) return(returndatasize(),0x20)}
}
}
// You can add more functions here
}
2nd Place: Zac369
Gas: 21682
pragma solidity 0.8.10;
contract EvenFibonacciSum {
// You can add more state variables here
function run(uint256 _upperBound) external pure returns(uint256) {
assembly {
mstore(0x00, 60696)
if iszero(_upperBound) {
return(0x20, 0x40)
}
if iszero(and(_upperBound, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00)) {
mstore(0x00, 44)
}
if iszero(and(_upperBound, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000ff)) {
mstore(0x00, 4613732)
}
return(0x00, 0x20)
}
}
// You can add more functions here
}
3rd Place: radeksvarz
Gas: 21723
pragma solidity 0.8.10;
contract EvenFibonacciSum {
// You can add more state variables here
function run(uint256 _upperBound) external pure returns(uint256) {
// ADD YOUR CODE HERE
assembly {
// Dispatching bands 1..4
// band 0 .. 143 to be split to 2 and 2
if lt(_upperBound, 144) {
if lt(_upperBound, 8) {
if gt(_upperBound, 2) {
// 2. band -> 2
mstore(0, 2)
} // if not -> 1. band -> 0
return(0, 32)
}
// 3. band -> 10
if lt(_upperBound, 34) {
mstore(0, 10)
return(0, 32)
}
// 4. band -> 44
mstore(0, 44)
return(0, 32)
}
// Dispatching bands 5..8
if lt(_upperBound, 46368) {
if lt(_upperBound, 2584) {
if lt(_upperBound, 610) {
// 5. band
mstore(0, 188)
return(0, 32)
}
// 6. band
mstore(0, 798)
return(0, 32)
}
if lt(_upperBound, 10946) {
// 7. band
mstore(0, 3382)
return(0, 32)
}
// 8. band
mstore(0, 14328)
return(0, 32)
}
if lt(_upperBound, 832040) {
if lt(_upperBound, 196418) {
// 9. band
mstore(0, 60696)
return(0, 32)
}
// 10. band
mstore(0, 257114)
return(0, 32)
}
if lt(_upperBound, 3524578) {
// 11. band
mstore(0, 1089154)
return(0, 32)
}
// 12. band +
mstore(0, 4613732)
return(0, 32)
}
}
// You can add more functions here
}
Thanks for all who participated in the challenge!
Want to learn more? Follow our progress!
For the latest news and developments, including course & challenge launches, stay tuned. Join the AgorApp Discord and communicate with the team or follow us on Telegram, Twitter or Linkedin.