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 18 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: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ coverage: tidy
# target to run all the possible checks; it's a good habit to run it before
# pushing code
check: lint vet test
echo "check done"
echo "check done"
17 changes: 16 additions & 1 deletion sign/bls/bls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,27 @@ import (
"go.dedis.ch/kyber/v3"
"go.dedis.ch/kyber/v3/pairing/bn256"
"go.dedis.ch/kyber/v3/util/random"
"go.dedis.ch/kyber/v3/xof/blake2xb"
)

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

func FuzzBLS(f *testing.F) {
suite := bn256.NewSuite()
private, public := NewKeyPair(suite, random.New())
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, suite)
})
}

func BLSRoutine(t *testing.T, msg []byte, suite *bn256.Suite) {
private, public := NewKeyPair(suite, blake2xb.New(msg))
sig, err := Sign(suite, private, msg)
require.Nil(t, err)
err = Verify(suite, public, msg, sig)
Expand Down
48 changes: 37 additions & 11 deletions sign/cosi/cosi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,50 @@ 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)
}

func testCoSi(t *testing.T, n, f int) {
message := []byte("Hello World Cosi")

// Generate key pairs
var kps []*key.Pair
for i := 0; i < 5; i++ {
kp := key.NewKeyPair(testSuite)
kps = append(kps, kp)
}

testCoSi(t, 2, 0, message, kps)
testCoSi(t, 5, 0, message, kps)
testCoSi(t, 5, 2, message, kps)
testCoSi(t, 5, 4, message, kps)
}

func FuzzCoSi(f *testing.F) {
// Generate key pairs
var kps []*key.Pair
for i := 0; i < 100; i++ {
kp := key.NewKeyPair(testSuite)
kps = append(kps, kp)
}

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, kps)
})
}

func testCoSi(t *testing.T, n, f int, message []byte, kps []*key.Pair) {
var privates []kyber.Scalar
var publics []kyber.Point
for i := 0; i < n; i++ {
kp := key.NewKeyPair(testSuite)
kps = append(kps, kp)
privates = append(privates, kp.Private)
publics = append(publics, kp.Public)
privates = append(privates, kps[i].Private)
publics = append(publics, kps[i].Public)
}

// Init masks
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) {
suite := edwards25519.NewBlakeSHA256Ed25519()
kp := key.NewKeyPair(suite)
AnomalRoil marked this conversation as resolved.
Show resolved Hide resolved

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")
}
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)
})
}
36 changes: 28 additions & 8 deletions sign/tbls/tbls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,45 @@ import (
"go.dedis.ch/kyber/v3/pairing/bn256"
"go.dedis.ch/kyber/v3/share"
"go.dedis.ch/kyber/v3/sign/bls"
"go.dedis.ch/kyber/v3/xof/blake2xb"
)

func TestTBLS(test *testing.T) {
var err error
msg := []byte("Hello threshold Boneh-Lynn-Shacham")
suite := bn256.NewSuite()
n := 10
t := n/2 + 1
secret := suite.G1().Scalar().Pick(suite.RandomStream())
priPoly := share.NewPriPoly(suite.G2(), t, secret, suite.RandomStream())
TBLSRoutine(test, []byte("Hello threshold Boneh-Lynn-Shacham"), 10)
}

func FuzzTBLS(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")
}
TBLSRoutine(t, msg, n)
})
}

func TBLSRoutine(test *testing.T, msg []byte, n int) {
// Use a deterministic seed for the random stream
stream := blake2xb.New(msg)
suite := bn256.NewSuiteRand(stream)
th := n/2 + 1

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

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