building-secure-contracts icon indicating copy to clipboard operation
building-secure-contracts copied to clipboard

Add Echidna tips & tricks

Open montyly opened this issue 4 years ago • 2 comments
trafficstars

  • If you fuzz a parameter that is expected to have only some values, use %:
function f(uint index) public{
   index = `index%2`
   if (index == 0) {...}
   elif (index == 1) {...}
}
  • To fuzz an arbitrary array length, you can use a push/pop approach (explain why?):
uint my_array[]

function push(uint a) public{
  my_array.push(a)
}

function pop() public{
  my_array.pop()
}

montyly avatar Mar 12 '21 09:03 montyly

Note that in some cases the push/pop approach will tend to produce smaller arrays than you want. Omitting pop can be a good idea (indeed, I would wager a decent chunk of the reason swarm testing works is omitting pop).

agroce avatar Mar 12 '21 16:03 agroce

@ggrieco-tob : can you add an example for the push/pop example in https://github.com/crytic/building-secure-contracts/blob/master/program-analysis/echidna/fuzzing_tips.md ?

montyly avatar Oct 26 '21 18:10 montyly

This was added

montyly avatar Dec 01 '22 10:12 montyly