Skip to content

Commit

Permalink
feat: select seeds based on list position
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandreps1123 committed Mar 15, 2024
1 parent 0540591 commit 3f4d80a
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion fuzz/genetic_algorithm.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package fuzz

import (
"math/rand"
"strings"
"time"

"github.com/dogefuzz/dogefuzz/pkg/common"
"github.com/dogefuzz/dogefuzz/pkg/interfaces"
"github.com/ethereum/go-ethereum/accounts/abi"
)

// consts to define what interval of the seeds slice will selected
const FIRST_INTERVAL float64 = 0.6
const SECOND_INTERVAL float64 = 0.8

// consts to define what range interval will used to prioritize some seeds
const FIRST_RANGE float64 = 0.4
const SECOND_RANGE float64 = 0.7

type geneticAlgorithmFuzzer struct {
powerSchedule interfaces.PowerSchedule
solidityService interfaces.SolidityService
Expand Down Expand Up @@ -46,7 +56,12 @@ func (f *geneticAlgorithmFuzzer) GenerateInput(functionId string) ([]interface{}
return nil, err
}

chosenSeeds := common.RandomChoice(seedsList)
var newSeedsList [][]interface{}
for range seedsList {
newSeedsList = append(newSeedsList, rouletteWheelSelection(seedsList))
}

chosenSeeds := common.RandomChoice(newSeedsList)

inputs := make([]interface{}, len(method.Inputs))
for inputsIdx, inputDefinition := range method.Inputs {
Expand All @@ -62,3 +77,23 @@ func (f *geneticAlgorithmFuzzer) GenerateInput(functionId string) ([]interface{}

return inputs, nil
}

func rouletteWheelSelection(seedsList [][]interface{}) []interface{} {
rand.Seed(time.Now().UnixNano())
rnd := rand.Float64()

if rnd >= 0 || rnd < FIRST_INTERVAL {
slice := seedsList[0:int(float64(len(seedsList))*FIRST_RANGE)]

return common.RandomChoice(slice)
} else if rnd >= FIRST_INTERVAL || rnd < SECOND_INTERVAL {
slice := seedsList[int(float64(len(seedsList))*FIRST_RANGE):int(float64(len(seedsList))*SECOND_RANGE)]

return common.RandomChoice(slice)
} else {
slice := seedsList[int(float64(len(seedsList))*SECOND_RANGE):]

return common.RandomChoice(slice)
}

}

0 comments on commit 3f4d80a

Please sign in to comment.