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

Add a fuzz test for Sign+Verify #164

Open
archseer opened this issue Oct 14, 2022 · 0 comments
Open

Add a fuzz test for Sign+Verify #164

archseer opened this issue Oct 14, 2022 · 0 comments

Comments

@archseer
Copy link
Collaborator

I attempted to do this, but in #163 but it's hard to filter out invalid reports. We'd need to run SplitFelt on the report and validate that each segment is a valid felt.

//go:build go1.18
// +build go1.18

package keys

import (
	cryptorand "crypto/rand"
	"encoding/hex"
	"testing"

	"github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/ocr2/medianreport"
	ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2/types"
	"github.com/stretchr/testify/require"
)

// go test -tags=go1.18 -fuzz ./...
func FuzzStarkNetKeyring_Sign_Verify(f *testing.F) {
	kr1, err := NewOCR2Key(cryptorand.Reader)
	require.NoError(f, err)
	kr2, err := NewOCR2Key(cryptorand.Reader)
	require.NoError(f, err)

	digest := "00044e5d4f35325e464c87374b13c512f60e09d1236dd902f4bef4c9aedd7300"
	bytes, err := hex.DecodeString(digest)
	require.NoError(f, err)
	configDigest, err := ocrtypes.BytesToConfigDigest(bytes)
	require.NoError(f, err)

	ctx := ocrtypes.ReportContext{
		ReportTimestamp: ocrtypes.ReportTimestamp{
			ConfigDigest: configDigest,
			Epoch:        1,
			Round:        1,
		},
		ExtraHash: [32]byte{
			255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
			255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
		},
	}
	report := ocrtypes.Report{
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 91, 43, 83, // observations_timestamp
		0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // observers
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, // len
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 150, 2, 210, // observation 1
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 150, 2, 211, // observation 2
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 224, 182, 179, 167, 100, 0, 0, // juels per fee coin (1 with 18 decimal places)
	}

	// Seed with valid report
	f.Add([]byte(report))
	f.Fuzz(func(t *testing.T, report []byte) {
		cdc := medianreport.ReportCodec{}
		// skip invalid reports
		if _, err := cdc.MedianFromReport(report); err != nil {
			t.Skip()
		}
		if _, err := ReportToSigData(ctx, report); err != nil {
			t.Skip()
		}
		sig, err := kr1.Sign(ctx, report)
		require.NoError(t, err)
		result := kr2.Verify(kr1.PublicKey(), ctx, report, sig)
		require.True(t, result)
	})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant