Skip to content
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

Added fuzzer tests #500

Merged
merged 20 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions sign/bls/bls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ import (

func TestBLS(t *testing.T) {
msg := []byte("Hello Boneh-Lynn-Shacham")
BLSRoutine(t, msg)
}

func FuzzBLS(f *testing.F) {
f.Fuzz(func(t *testing.T, msg []byte) {
if len(msg) < 1 || len(msg) > 1000 {
t.Skip("msg must have byte length between 1 and 1000")
}
BLSRoutine(t, msg)
})
}

func BLSRoutine(t *testing.T, msg []byte) {
suite := bn256.NewSuite()
private, public := NewKeyPair(suite, random.New())
sig, err := Sign(suite, private, msg)
Expand Down
28 changes: 22 additions & 6 deletions sign/cosi/cosi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,31 @@ func (m *cosiSuite) RandomStream() cipher.Stream { return m.r }
var testSuite = &cosiSuite{edwards25519.NewBlakeSHA256Ed25519(), blake2xb.New(nil)}

func TestCoSi(t *testing.T) {
testCoSi(t, 2, 0)
testCoSi(t, 5, 0)
testCoSi(t, 5, 2)
testCoSi(t, 5, 4)
message := []byte("Hello World Cosi")
testCoSi(t, 2, 0, message)
testCoSi(t, 2, 0, message)
AnomalRoil marked this conversation as resolved.
Show resolved Hide resolved
testCoSi(t, 5, 0, message)
testCoSi(t, 5, 2, message)
testCoSi(t, 5, 4, message)
}

func testCoSi(t *testing.T, n, f int) {
message := []byte("Hello World Cosi")
func FuzzCoSi(f *testing.F) {
f.Fuzz(func(t *testing.T, n, f int, msg []byte) {
if (len(msg) < 1) || (len(msg) > 1000) {
t.Skip("msg must have byte length between 1 and 1000")
}
if n < 1 || n > 100 {
t.Skip("n must be between 1 and 100")
}
if f < 0 || f >= n {
t.Skip("f must be between 0 and n-1")
}

testCoSi(t, n, f, msg)
})
}

func testCoSi(t *testing.T, n, f int, message []byte) {
// Generate key pairs
AnomalRoil marked this conversation as resolved.
Show resolved Hide resolved
var kps []*key.Pair
var privates []kyber.Scalar
Expand Down
17 changes: 17 additions & 0 deletions sign/schnorr/schnorr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing/quick"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.dedis.ch/kyber/v3/group/edwards25519"
"go.dedis.ch/kyber/v3/sign/eddsa"
"go.dedis.ch/kyber/v3/util/key"
Expand Down Expand Up @@ -125,3 +126,19 @@ func TestSchnorrMalleability(t *testing.T) {
err = Verify(suite, kp.Public, msg, s)
assert.Error(t, err, "schnorr signature malleable")
}

func FuzzSchnorr(f *testing.F) {
f.Fuzz(func(t *testing.T, msg []byte) {
if (len(msg) < 1) || (len(msg) > 1000) {
t.Skip("msg must have byte length between 1 and 1000")
}
suite := edwards25519.NewBlakeSHA256Ed25519()
kp := key.NewKeyPair(suite)
AnomalRoil marked this conversation as resolved.
Show resolved Hide resolved

s, err := Sign(suite, kp.Private, msg)
require.NoError(t, err, "Couldn't sign msg: %s: %v", msg, err)

err = Verify(suite, kp.Public, msg, s)
require.NoError(t, err, "Couldn't verify signature: \n%+v\nfor msg:'%s'. Error:\n%v", s, msg, err)
})
}
29 changes: 23 additions & 6 deletions sign/tbls/tbls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,39 @@ import (
)

func TestTBLS(test *testing.T) {
var err error
msg := []byte("Hello threshold Boneh-Lynn-Shacham")
BLSRoutine(test, []byte("Hello threshold Boneh-Lynn-Shacham"), 10)
}

func FuzzBLS(f *testing.F) {
f.Fuzz(func(t *testing.T, msg []byte, n int) {
if (n < 1) || (n > 100) {
t.Skip("n must be between 1 and 100")
}
if (len(msg) < 1) || (len(msg) > 1000) {
t.Skip("msg must have byte length between 1 and 1000")
}
BLSRoutine(t, msg, n)
})
}

func BLSRoutine(test *testing.T, msg []byte, n int) {
suite := bn256.NewSuite()
matteosz marked this conversation as resolved.
Show resolved Hide resolved
n := 10
t := n/2 + 1
th := n/2 + 1

secret := suite.G1().Scalar().Pick(suite.RandomStream())
priPoly := share.NewPriPoly(suite.G2(), t, secret, suite.RandomStream())
priPoly := share.NewPriPoly(suite.G2(), th, secret, suite.RandomStream())
pubPoly := priPoly.Commit(suite.G2().Point().Base())
sigShares := make([][]byte, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this setup phase really needs to be repeated when fuzzing or whether fuzzing should occur on the message only, WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that fuzzing on the parameter n would have been more interesting than using just; I'd say that the cost of repeating these operations is not that expensive and leads some value in increased exploration. But if you feel it's relevant to fuzz only on n I can change it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, fuzzing should be repeatable. Given the same "fuzzing corpus of input messages" I should get the same outputs/results in all runs.

Could we change this code so that there is no randomness outside of the fuzzer msg?
Maybe seed the RandomStream using msg.


for _, x := range priPoly.Shares(n) {
sig, err := Sign(suite, x, msg)
require.Nil(test, err)
sigShares = append(sigShares, sig)
}
sig, err := Recover(suite, pubPoly, msg, sigShares, t, n)

sig, err := Recover(suite, pubPoly, msg, sigShares, th, n)
require.Nil(test, err)

err = bls.Verify(suite, pubPoly.Commit(), msg, sig)
require.Nil(test, err)
}
Loading