Skip to content

fuzzing: fault injector #1474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions constraint/bls12-377/solver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions constraint/bls12-381/solver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions constraint/bls24-315/solver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions constraint/bls24-317/solver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions constraint/blueprint.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package constraint

import (
"fmt"
"math/rand"
)

type BlueprintID uint32

// Blueprint enable representing heterogeneous constraints or instructions in a constraint system
Expand All @@ -24,6 +29,50 @@
UpdateInstructionTree(inst Instruction, tree InstructionTree) Level
}

var MainFaultInjector = newFaultInjector(0, 1, 0.0001)

type faultInjector struct {
rnd *rand.Rand
faultProb float64
maxFaults int
numFaults int
}

func newFaultInjector(seed int64, maxFaults int, faultProb float64) *faultInjector {
return &faultInjector{
rnd: rand.New(rand.NewSource(seed)),

Check failure on line 43 in constraint/blueprint.go

View workflow job for this annotation

GitHub Actions / staticcheck

G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand) (gosec)
faultProb: faultProb,
maxFaults: maxFaults,
}
}

func (r *faultInjector) Reset(seed int64, maxFaults int, faultProb float64) {
r.rnd = rand.New(rand.NewSource(seed))

Check failure on line 50 in constraint/blueprint.go

View workflow job for this annotation

GitHub Actions / staticcheck

G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand) (gosec)
r.faultProb = faultProb
r.maxFaults = maxFaults
r.numFaults = 0
}

func (r *faultInjector) NumFaults() int {
return r.numFaults
}

func (r *faultInjector) Delta() int64 {
if r.maxFaults <= r.numFaults {
return 0
}
if r.rnd.Float64() < r.faultProb {
d := int64(1)
if r.rnd.Float64() < 0.5 {
d = -1
}
fmt.Printf("faultInjector: %d\n", d)
r.numFaults++
return d
}
return 0
}

// Solver represents the state of a constraint system solver at runtime. Blueprint can interact
// with this object to perform run time logic, solve constraints and assign values in the solution.
type Solver interface {
Expand Down
2 changes: 2 additions & 0 deletions constraint/bn254/solver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions constraint/bw6-633/solver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions constraint/bw6-761/solver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions constraint/tinyfield/solver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func (s *solver) set(id int, value fr.Element) {
if s.solved[id] {
panic("solving the same wire twice should never happen.")
}
delta := constraint.MainFaultInjector.Delta()
value = *(new(fr.Element).Add(&value, new(fr.Element).SetInt64(delta)))
s.values[id] = value
s.solved[id] = true
atomic.AddUint64(&s.nbSolved, 1)
Expand Down
Loading